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;
|
const MaximumDepth = 128;
|
||||||
|
|
||||||
function rewriteBareModuleIdentifiers(file, packageConfig, callback) {
|
function serveMetadata(req, res) {
|
||||||
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.
|
|
||||||
getMetadata(
|
getMetadata(
|
||||||
req.packageDir,
|
req.packageDir,
|
||||||
req.filename,
|
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);
|
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) => {
|
rewriteBareModuleIdentifiers(file, req.packageConfig, (error, code) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
@ -105,7 +95,7 @@ function serveFile(req, res) {
|
||||||
// Cache modules for 1 year.
|
// Cache modules for 1 year.
|
||||||
res
|
res
|
||||||
.set({
|
.set({
|
||||||
"Content-Type": contentType,
|
"Content-Type": "application/javascript; charset=utf-8",
|
||||||
"Content-Length": Buffer.byteLength(code),
|
"Content-Length": Buffer.byteLength(code),
|
||||||
"Cache-Control": "public, max-age=31536000",
|
"Cache-Control": "public, max-age=31536000",
|
||||||
"Cache-Tag": "file,js-file,js-module"
|
"Cache-Tag": "file,js-file,js-module"
|
||||||
|
@ -113,15 +103,20 @@ function serveFile(req, res) {
|
||||||
.send(code);
|
.send(code);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
}
|
||||||
// Serve some other static file.
|
|
||||||
|
function serveStaticFile(req, res) {
|
||||||
|
const file = path.join(req.packageDir, req.filename);
|
||||||
const tags = ["file"];
|
const tags = ["file"];
|
||||||
|
|
||||||
const ext = path.extname(req.filename).substr(1);
|
const ext = path.extname(req.filename).substr(1);
|
||||||
|
|
||||||
if (ext) {
|
if (ext) {
|
||||||
tags.push(`${ext}-file`);
|
tags.push(`${ext}-file`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let contentType = getFileContentType(file);
|
||||||
|
|
||||||
if (contentType === "application/javascript") {
|
if (contentType === "application/javascript") {
|
||||||
contentType += "; charset=utf-8";
|
contentType += "; charset=utf-8";
|
||||||
}
|
}
|
||||||
|
@ -146,9 +141,11 @@ function serveFile(req, res) {
|
||||||
|
|
||||||
stream.pipe(res);
|
stream.pipe(res);
|
||||||
}
|
}
|
||||||
} else if (AutoIndex && req.stats.isDirectory()) {
|
|
||||||
// Serve an HTML directory listing.
|
function serveIndex(req, res) {
|
||||||
getEntries(path.join(req.packageDir, req.filename)).then(
|
const dir = path.join(req.packageDir, req.filename);
|
||||||
|
|
||||||
|
getEntries(dir).then(
|
||||||
entries => {
|
entries => {
|
||||||
const html = renderPage(IndexPage, {
|
const html = renderPage(IndexPage, {
|
||||||
packageInfo: req.packageInfo,
|
packageInfo: req.packageInfo,
|
||||||
|
@ -174,6 +171,24 @@ function serveFile(req, res) {
|
||||||
.send(`Cannot read entries for ${req.packageSpec}${req.filename}`);
|
.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 {
|
} else {
|
||||||
res
|
res
|
||||||
.status(403)
|
.status(403)
|
||||||
|
|
Loading…
Reference in New Issue