Simplify server config

This commit is contained in:
MICHAEL JACKSON
2017-08-12 10:38:50 -07:00
parent 4653ee9e42
commit 5f2805c2e9
5 changed files with 53 additions and 58 deletions

View File

@ -59,7 +59,7 @@ const createServer = (config) => {
maxAge: '365d' maxAge: '365d'
})) }))
app.use(middleware(config)) app.use(middleware())
const server = http.createServer(app) const server = http.createServer(app)
@ -86,11 +86,7 @@ const createServer = (config) => {
const defaultServerConfig = { const defaultServerConfig = {
id: 1, id: 1,
port: parseInt(process.env.PORT, 10) || 5000, port: parseInt(process.env.PORT, 10) || 5000,
publicDir: 'public', publicDir: 'public'
// for the middleware
autoIndex: !process.env.DISABLE_INDEX,
blacklist: require('./PackageBlacklist').blacklist
} }
const startServer = (serverConfig = {}) => { const startServer = (serverConfig = {}) => {

View File

@ -1,14 +1,14 @@
const blacklist = require('../PackageBlacklist').blacklist
/** /**
* Check the blacklist to see if we can serve files from this package. * Check the blacklist to see if we can serve files from this package.
*/ */
function checkBlacklist(blacklist) { function checkBlacklist(req, res, next) {
return function (req, res, next) {
if (blacklist.includes(req.packageName)) { if (blacklist.includes(req.packageName)) {
res.status(403).send(`Package ${req.packageName} is blacklisted`) res.status(403).send(`Package ${req.packageName} is blacklisted`)
} else { } else {
next() next()
} }
} }
}
module.exports = checkBlacklist module.exports = checkBlacklist

View File

@ -5,7 +5,7 @@ const PackageURL = require('../PackageURL')
/** /**
* Fetch the package from the registry and store a local copy on disk. * Fetch the package from the registry and store a local copy on disk.
* Redirect if the URL does not specify an exact req.packageVersion number. * Redirect if the URL does not specify an exact version number.
*/ */
function fetchPackage(req, res, next) { function fetchPackage(req, res, next) {
PackageInfo.get(req.packageName, function (error, packageInfo) { PackageInfo.get(req.packageName, function (error, packageInfo) {

View File

@ -7,12 +7,7 @@ const serveFile = require('./serveFile')
/** /**
* Creates and returns a function that can be used in the "request" * Creates and returns a function that can be used in the "request"
* event of a standard node HTTP server. Options are: * event of a standard node HTTP server. Supported URL schemes are:
*
* - registryURL The URL of the npm registry (defaults to https://registry.npmjs.org)
* - autoIndex Automatically generate index HTML pages for directories (defaults to true)
*
* Supported URL schemes are:
* *
* /history@1.12.5/umd/History.min.js (recommended) * /history@1.12.5/umd/History.min.js (recommended)
* /history@1.12.5 (package.json's main is implied) * /history@1.12.5 (package.json's main is implied)
@ -25,19 +20,15 @@ const serveFile = require('./serveFile')
* /history@latest/umd/History.min.js (redirects to version) * /history@latest/umd/History.min.js (redirects to version)
* /history@^1/umd/History.min.js (redirects to max satisfying version) * /history@^1/umd/History.min.js (redirects to max satisfying version)
*/ */
function createRequestHandler(options = {}) { function createRequestHandler() {
const autoIndex = options.autoIndex !== false
const maximumDepth = options.maximumDepth || Number.MAX_VALUE
const blacklist = options.blacklist || []
const app = express.Router() const app = express.Router()
app.use( app.use(
parseURL, parseURL,
checkBlacklist(blacklist), checkBlacklist,
fetchPackage, fetchPackage,
findFile, findFile,
serveFile(autoIndex, maximumDepth) serveFile
) )
return app return app

View File

@ -5,6 +5,16 @@ const Metadata = require('./MetadataUtils')
const { generateDirectoryIndexHTML } = require('./IndexUtils') const { generateDirectoryIndexHTML } = require('./IndexUtils')
const { getContentType } = require('./FileUtils') const { getContentType } = require('./FileUtils')
/**
* Automatically generate HTML pages that show package contents.
*/
const AutoIndex = !process.env.DISABLE_INDEX
/**
* Maximum recursion depth for ?meta listings.
*/
const MaximumDepth = 128
function sendFile(res, file, stats) { function sendFile(res, file, stats) {
let contentType = getContentType(file) let contentType = getContentType(file)
@ -30,11 +40,10 @@ function sendFile(res, file, stats) {
/** /**
* Send the file, JSON metadata, or HTML directory listing. * Send the file, JSON metadata, or HTML directory listing.
*/ */
function serveFile(autoIndex, maximumDepth) { function serveFile(req, res, next) {
return function (req, res, next) {
// TODO: remove support for "json" query param // TODO: remove support for "json" query param
if (req.query.meta != null || req.query.json != null) { if (req.query.meta != null || req.query.json != null) {
Metadata.get(req.packageDir, req.file, req.stats, maximumDepth, function (error, metadata) { Metadata.get(req.packageDir, req.file, req.stats, MaximumDepth, function (error, metadata) {
if (error) { if (error) {
console.error(error) console.error(error)
res.status(500).send(`Cannot generate JSON metadata for ${req.packageSpec}${req.filename}`) res.status(500).send(`Cannot generate JSON metadata for ${req.packageSpec}${req.filename}`)
@ -49,7 +58,7 @@ function serveFile(autoIndex, maximumDepth) {
// TODO: use res.sendFile instead of our own sendFile? // TODO: use res.sendFile instead of our own sendFile?
sendFile(res, path.join(req.packageDir, req.file), req.stats) sendFile(res, path.join(req.packageDir, req.file), req.stats)
} else if (autoIndex && req.stats.isDirectory()) { } else if (AutoIndex && req.stats.isDirectory()) {
generateDirectoryIndexHTML(req.packageInfo, req.packageVersion, req.packageDir, req.file, function (error, html) { generateDirectoryIndexHTML(req.packageInfo, req.packageVersion, req.packageDir, req.file, function (error, html) {
if (error) { if (error) {
console.error(error) console.error(error)
@ -63,6 +72,5 @@ function serveFile(autoIndex, maximumDepth) {
res.status(403).send(`Cannot serve ${req.packageSpec}${req.filename}; it's not a file`) res.status(403).send(`Cannot serve ${req.packageSpec}${req.filename}; it's not a file`)
} }
} }
}
module.exports = serveFile module.exports = serveFile