Refactor serveFile into separate functions for different use cases
This commit is contained in:
parent
8c6912e1a1
commit
34922f6c1b
|
@ -20,32 +20,7 @@ const AutoIndex = !process.env.DISABLE_INDEX;
|
|||
*/
|
||||
const MaximumDepth = 128;
|
||||
|
||||
function rewriteBareModuleIdentifiers(file, packageConfig, callback) {
|
||||
const dependencies = Object.assign(
|
||||
{},
|
||||
packageConfig.peerDependencies,
|
||||
packageConfig.dependencies
|
||||
);
|
||||
|
||||
const options = {
|
||||
// Ignore .babelrc and package.json babel config
|
||||
// because we haven't installed dependencies so
|
||||
// we can't load plugins; see #84
|
||||
babelrc: false,
|
||||
plugins: [unpkgRewrite(dependencies)]
|
||||
};
|
||||
|
||||
babel.transformFile(file, options, (error, result) => {
|
||||
callback(error, result && result.code);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the file, JSON metadata, or HTML directory listing.
|
||||
*/
|
||||
function serveFile(req, res) {
|
||||
if (req.query.meta != null) {
|
||||
// Serve JSON metadata.
|
||||
function serveMetadata(req, res) {
|
||||
getMetadata(
|
||||
req.packageDir,
|
||||
req.filename,
|
||||
|
@ -72,16 +47,31 @@ function serveFile(req, res) {
|
|||
}
|
||||
}
|
||||
);
|
||||
} else if (req.stats.isFile()) {
|
||||
// Serve a file.
|
||||
}
|
||||
|
||||
function rewriteBareModuleIdentifiers(file, packageConfig, callback) {
|
||||
const dependencies = Object.assign(
|
||||
{},
|
||||
packageConfig.peerDependencies,
|
||||
packageConfig.dependencies
|
||||
);
|
||||
|
||||
const options = {
|
||||
// Ignore .babelrc and package.json babel config
|
||||
// because we haven't installed dependencies so
|
||||
// we can't load plugins; see #84
|
||||
babelrc: false,
|
||||
plugins: [unpkgRewrite(dependencies)]
|
||||
};
|
||||
|
||||
babel.transformFile(file, options, (error, result) => {
|
||||
callback(error, result && result.code);
|
||||
});
|
||||
}
|
||||
|
||||
function serveJavaScriptModule(req, res) {
|
||||
const file = path.join(req.packageDir, req.filename);
|
||||
|
||||
let contentType = getFileContentType(file);
|
||||
|
||||
if (contentType === "application/javascript" && req.query.module != null) {
|
||||
contentType += "; charset=utf-8";
|
||||
|
||||
// Serve a JavaScript module.
|
||||
rewriteBareModuleIdentifiers(file, req.packageConfig, (error, code) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
|
@ -105,7 +95,7 @@ function serveFile(req, res) {
|
|||
// Cache modules for 1 year.
|
||||
res
|
||||
.set({
|
||||
"Content-Type": contentType,
|
||||
"Content-Type": "application/javascript; charset=utf-8",
|
||||
"Content-Length": Buffer.byteLength(code),
|
||||
"Cache-Control": "public, max-age=31536000",
|
||||
"Cache-Tag": "file,js-file,js-module"
|
||||
|
@ -113,15 +103,20 @@ function serveFile(req, res) {
|
|||
.send(code);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Serve some other static file.
|
||||
}
|
||||
|
||||
function serveStaticFile(req, res) {
|
||||
const file = path.join(req.packageDir, req.filename);
|
||||
const tags = ["file"];
|
||||
|
||||
const ext = path.extname(req.filename).substr(1);
|
||||
|
||||
if (ext) {
|
||||
tags.push(`${ext}-file`);
|
||||
}
|
||||
|
||||
let contentType = getFileContentType(file);
|
||||
|
||||
if (contentType === "application/javascript") {
|
||||
contentType += "; charset=utf-8";
|
||||
}
|
||||
|
@ -145,10 +140,12 @@ function serveFile(req, res) {
|
|||
});
|
||||
|
||||
stream.pipe(res);
|
||||
}
|
||||
} else if (AutoIndex && req.stats.isDirectory()) {
|
||||
// Serve an HTML directory listing.
|
||||
getEntries(path.join(req.packageDir, req.filename)).then(
|
||||
}
|
||||
|
||||
function serveIndex(req, res) {
|
||||
const dir = path.join(req.packageDir, req.filename);
|
||||
|
||||
getEntries(dir).then(
|
||||
entries => {
|
||||
const html = renderPage(IndexPage, {
|
||||
packageInfo: req.packageInfo,
|
||||
|
@ -174,6 +171,24 @@ function serveFile(req, res) {
|
|||
.send(`Cannot read entries for ${req.packageSpec}${req.filename}`);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the file, JSON metadata, or HTML directory listing.
|
||||
*/
|
||||
function serveFile(req, res) {
|
||||
if (req.query.meta != null) {
|
||||
serveMetadata(req, res);
|
||||
} else if (req.stats.isFile()) {
|
||||
const contentType = getFileContentType(req.filename);
|
||||
|
||||
if (contentType === "application/javascript" && req.query.module != null) {
|
||||
serveJavaScriptModule(req, res);
|
||||
} else {
|
||||
serveStaticFile(req, res);
|
||||
}
|
||||
} else if (req.stats.isDirectory() && AutoIndex) {
|
||||
serveIndex(req, res);
|
||||
} else {
|
||||
res
|
||||
.status(403)
|
||||
|
|
Loading…
Reference in New Issue