Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function maybeServeIndexFile() {
const indexPath = Path.join(path, directoryIndex);
fs.stat(indexPath, function(err, stats) {
if(err) {
if(err.code === 'ENOENT' && !disableIndexes) {
// Fallback to a directory listing instead
serveDirListing();
} else {
// Let the error (likely 404) pass through instead
serveError(path, err);
}
} else {
// Index file found, serve that instead
serveFile(indexPath, stats);
}
});
}
function serveDirListing() {
sh.ls(path, function(err, entries) {
if(err) {
return serveError(path, err);
}
const responseData = formatter.formatDir(route, path, entries);
resolve(new Response(responseData.body, responseData.config));
});
}
maybeServeIndexFile();
}
fs.stat(path, function(err, stats) {
if(err) {
return serveError(path, err);
}
if(stats.isDirectory()) {
serveDir(path);
} else {
serveFile(path, stats);
}
});
});
};