Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function storeConfig(config) {
try {
writeFileAtomic.sync(serverlessrcPath, JSON.stringify(config, null, 2));
} catch (error) {
if (process.env.SLS_DEBUG) {
process.stdout.write(`${chalk.red(error.stack)}\n`);
process.stdout.write(
`Serverless: ${chalk.red(`Unable to store serverless config due to ${error.code} error`)}\n`
);
}
try {
return JSON.parse(readFileSync(serverlessrcPath));
} catch (readError) {
// Ignore
}
return {};
}
return config;
}
_write(value) {
let data = this.serialize(value);
if (this.encryptionKey) {
const initializationVector = crypto.randomBytes(16);
const password = crypto.pbkdf2Sync(this.encryptionKey, initializationVector.toString(), 10000, 32, 'sha512');
const cipher = crypto.createCipheriv(encryptionAlgorithm, password, initializationVector);
data = Buffer.concat([initializationVector, Buffer.from(':'), cipher.update(Buffer.from(data)), cipher.final()]);
}
// Temporary workaround for Conf being packaged in a Ubuntu Snap app.
// See https://github.com/sindresorhus/conf/pull/82
if (process.env.SNAP) {
fs.writeFileSync(this.path, data);
} else {
writeFileAtomic.sync(this.path, data);
}
}
set all(val) {
try {
// Make sure the folder exists as it could have been deleted in the meantime
makeDir.sync(path.dirname(this.path))
writeFileAtomic.sync(this.path, JSON.stringify(val, null, '\t'))
} catch (err) {
// Improve the message of permission errors
if (err.code === 'EACCES') {
err.message = `${err.message}\n${permissionError}\n`
}
throw err
}
}
const inputSourceMap = getSourceMap(filename, inputCode);
if (inputSourceMap) {
options.inputSourceMap = inputSourceMap;
}
const {code, map} = babel.transformSync(inputCode, options);
if (map) {
// Save source map
const mapPath = `${cachePath}.map`;
writeFileAtomic.sync(mapPath, JSON.stringify(map));
// Append source map comment to transformed code so that other libraries
// (like nyc) can find the source map.
const comment = convertSourceMap.generateMapFileComment(mapPath);
writeFileAtomic.sync(cachePath, `${code}\n${comment}`);
} else {
writeFileAtomic.sync(cachePath, code);
}
return cachePath;
};
}
try {
return JSON.parse(fs.readFileSync(this.path, 'utf8'));
} catch (error) {
// Create directory if it doesn't exist
if (error.code === 'ENOENT') {
return {};
}
// Improve the message of permission errors
if (error.code === 'EACCES') {
error.message = `${error.message}\n${permissionError}\n`;
}
// Empty the file if it encounters invalid JSON
if (error.name === 'SyntaxError') {
writeFileAtomic.sync(this.path, '', writeFileOptions);
return {};
}
throw error;
}
}
onHash(input, metadata, hash);
let result;
let retry = 0;
/* eslint-disable-next-line no-constant-condition */
while (true) {
try {
return fs.readFileSync(cachedPath, encoding);
} catch (_) {
if (!result) {
result = transform(input, metadata, hash);
}
try {
writeFileAtomic.sync(cachedPath, result, {encoding});
return result;
} catch (error) {
/* Likely https://github.com/npm/write-file-atomic/issues/28
* Make up to 3 attempts to read or write the cache. */
retry++;
if (retry > 3) {
throw error;
}
}
}
}
};
}
this.lock(release => {
// Write atomically
writeFileAtomic(this.file, JSON.stringify(data, null, 2), err => {
// Release file
release()
if (err) {
return reject(err)
}
resolve()
})
})
})
let indent = opts.indent;
if (opts.detectIndent) {
try {
const file = fs.readFileSync(fp, 'utf8');
indent = detectIndent(file).indent;
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}
const json = JSON.stringify(data, opts.replacer, indent);
return writeFileAtomic.sync(fp, `${json}\n`, {mode: opts.mode});
};
}
const {snapPath} = this;
const buffer = encodeSnapshots(this.snapshotsByHash);
const reportPath = path.join(this.dir, this.reportFile);
const existingReport = this.appendOnly ? tryRead(reportPath) : null;
const reportBuffer = existingReport ?
appendReportEntries(existingReport, this.reportEntries) :
generateReport(this.relFile, this.snapFile, this.reportEntries);
makeDir.sync(this.dir);
const paths = [snapPath, reportPath];
const tmpfileCreated = tmpfile => paths.push(tmpfile);
writeFileAtomic.sync(snapPath, buffer, {tmpfileCreated});
writeFileAtomic.sync(reportPath, reportBuffer, {tmpfileCreated});
return paths;
}
}
set all(value) {
try {
mkdir(path.dirname(this.path));
value = JSON.stringify(value);
if (!this.options.raw) {
value = base64.encode(value)
}
writeFileAtomic.sync(this.path, value);
} catch (error) {
throw error;
}
}