Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function sendfile(ctx, filepath, options = {}) {
// decode for `/%E4%B8%AD%E6%96%87`
// normalize for `//index`
let file = files.get(filepath);
// try to load file
if (!file) {
const s = await fs.stat(filepath);
if (!s.isFile()) {
throw Error('file not found');
}
file = loadFile(filepath, options, files);
}
ctx.status = 200;
if (options.gzip) ctx.vary('Accept-Encoding');
if (!file.buffer) {
const stats = await fs.stat(file.path);
if (stats.mtime > file.mtime) {
file.mtime = stats.mtime;
file.md5 = null;
return __awaiter(this, void 0, void 0, function* () {
console.log('ApiClientCLI validate.');
if (!PROTO_DIR) {
throw new Error('--proto is required');
}
if (!OUTPUT_DIR) {
throw new Error('--output is required');
}
let protoStat = yield LibFs.stat(PROTO_DIR);
if (!protoStat.isDirectory()) {
throw new Error('--proto is not a directory');
}
let outputStat = yield LibFs.stat(OUTPUT_DIR);
if (!outputStat.isDirectory()) {
throw new Error('--output is not a directory');
}
});
}
private async _validate() {
console.log('ServiceCLI validate.');
if (!PROTO_DIR) {
throw new Error('--proto is required');
}
if (!OUTPUT_DIR) {
throw new Error('--output is required');
}
let protoStat = await LibFs.stat(PROTO_DIR);
if (!protoStat.isDirectory()) {
throw new Error('--proto is not a directory');
}
let outputStat = await LibFs.stat(OUTPUT_DIR);
if (!outputStat.isDirectory()) {
throw new Error('--output is not a directory');
}
}
async clean ( target : string, condition ?: StoragePredicate ) : Promise {
target = this.storage.getPath( target );
condition = condition || ( () => true );
const release = await this.semaphore.acquire();
try {
if ( await fs.exists( target ) ) {
const stats = await fs.stat( target );
if ( stats.isFile() ) {
await this.cleanFile( stats, target, condition );
} else {
await this.cleanFolder( stats, target, condition, false );
}
}
} catch ( error ) {
await this.storage.server.onError.notify( error );
} finally {
release();
}
}
let filesInDir = await Promise.all(files.map(async (f) => {
const file = path.resolve(await directory, f);
const stat = await fs.stat(file);
if (!ignore.find((ignorePattern) => file.search(ignorePattern) !== -1)) {
if (stat.isDirectory()) {
return getFiles(file, { pattern, ignore });
} else if (file.search(pattern) > -1) {
return file;
}
}
}));
async function applyPossibleConfig(filename, config) {
if (!filename.startsWith('bulk-decaffeinate') ||
(await stat(filename)).isDirectory()) {
return config;
}
if (filename.endsWith('.config.js')) {
return applyConfig(filename, config);
} else {
return config;
}
}
function readCert (hostname, opts) {
if (lock[hostname]) return lock[hostname];
var certPath = path.join(CERT_PATH, hostname);
var stdio = opts.debug ? "pipe" : "ignore";
return lock[hostname] = fs.stat(certPath)
.then(function (stats) {
var age = +new Date - stats.mtime;
if (age > EXPIRE_TIME) throw new Error("AutoSNI: Expired certificate.");
return Promise.all([
fs.readFile(path.join(certPath, "/cert.pem")),
fs.readFile(path.join(certPath, "/key.pem")),
]).then(function (data) {
setTimeout(function () { delete lock[hostname]; }, EXPIRE_TIME - age);
return {
created: stats.mtime,
cert: data[0],
key: data[1]
}
})
})
.catch(function (err) {
return __awaiter(this, void 0, void 0, function* () {
debug('ServiceCLI validate.');
if (!PROTO_DIR) {
throw new Error('--result is required');
}
if (!OUTPUT_DIR) {
throw new Error('--output is required');
}
let protoStat = yield LibFs.stat(PROTO_DIR);
if (!protoStat.isDirectory()) {
throw new Error('--result is not a directory');
}
let outputStat = yield LibFs.stat(OUTPUT_DIR);
if (!outputStat.isDirectory()) {
throw new Error('--output is not a directory');
}
});
}
const dirname = path.dirname(url);
const fileName = path.basename(url, '.md');
const infoPath = path.resolve(dirname, 'info.json');
infoCache[infoPath] =
infoCache[infoPath] ||
((fs.existsSync(infoPath))
? JSON.parse((await fs.readFile(infoPath, { encoding: 'utf-8' })) || '{}')
: {});
const meta = (infoCache[infoPath].meta = infoCache[infoPath].meta || {});
if (fs.existsSync(url)) {
meta[fileName] = meta[fileName] || {};
const { isWIP } = await utils.getMdInfo(url);
if (!isWIP && !meta[fileName].ctime) {
meta[fileName].ctime = +(await fs.stat(url)).mtime;
} else if (isWIP && meta[fileName].ctime) {
delete meta[fileName].ctime;
} else {
return;
}
} else if (meta[fileName]) {
delete meta[fileName];
} else {
return;
}
needUpdate[infoPath] = true;
})
);
protected async * runSingle ( base : string, file : string, stats ?: fs.Stats ) : AsyncIterableIterator<[ string, fs.Stats ]> {
if ( !stats ) {
stats = await fs.stat( file );
}
if ( stats.isFile() ) {
if ( !await this.isExcluded( base, file, stats ) ) {
yield this.yieldFileRecord( base, file, stats );
}
} else if ( stats.isDirectory() ) {
const children = await fs.readdir( file );
for ( let child of children ) {
const fullChild = path.join( file, child );
stats = await fs.stat( fullChild );
if ( !await this.isExcluded( base, fullChild, stats ) ) {
if ( stats.isFile() ) {