diff --git a/server/index.js b/server/index.js index df3af46..1936d6b 100644 --- a/server/index.js +++ b/server/index.js @@ -103,7 +103,6 @@ const defaultServerConfig = { // for the middleware registryURL: process.env.REGISTRY_URL || 'https://registry.npmjs.org', - redirectTTL: process.env.REDIRECT_TTL || 500, autoIndex: !process.env.DISABLE_INDEX } diff --git a/server/middleware/ResponseUtils.js b/server/middleware/ResponseUtils.js index 0a1f77c..146e28d 100644 --- a/server/middleware/ResponseUtils.js +++ b/server/middleware/ResponseUtils.js @@ -32,25 +32,22 @@ const sendNotFoundError = (res, what) => const sendServerError = (res, error) => sendText(res, 500, `Server error: ${error.message || error}`) -const sendHTML = (res, html, maxAge = 0, statusCode = 200) => { +const sendHTML = (res, html, statusCode = 200) => { res.writeHead(statusCode, { 'Content-Type': 'text/html', - 'Content-Length': Buffer.byteLength(html), - 'Cache-Control': `public, max-age=${maxAge}` + 'Content-Length': Buffer.byteLength(html) }) res.end(html) } -const sendRedirect = (res, relativeLocation, maxAge = 0, statusCode = 302) => { +const sendRedirect = (res, relativeLocation, statusCode = 302) => { const location = res.req && res.req.baseUrl ? res.req.baseUrl + relativeLocation : relativeLocation - const html = `

You are being redirected to ${location}` res.writeHead(statusCode, { 'Content-Type': 'text/html', 'Content-Length': Buffer.byteLength(html), - 'Cache-Control': `public, max-age=${maxAge}`, 'Location': encodeURI(location) }) diff --git a/server/middleware/index.js b/server/middleware/index.js index d2ce57b..381de74 100644 --- a/server/middleware/index.js +++ b/server/middleware/index.js @@ -18,9 +18,9 @@ const { sendHTML } = require('./ResponseUtils') -const OneMinute = 60 -const OneDay = OneMinute * 60 * 24 -const OneYear = OneDay * 365 +const oneMinute = 60 +const oneDay = oneMinute * 60 * 24 +const oneYear = oneDay * 365 const checkLocalCache = (dir, callback) => statFile(joinPaths(dir, 'package.json'), (error, stats) => { @@ -72,7 +72,6 @@ const resolveFile = (path, useIndex, callback) => { * event of a standard node HTTP server. Options are: * * - registryURL The URL of the npm registry (defaults to https://registry.npmjs.org) - * - redirectTTL The TTL (in seconds) for redirects (defaults to 0) * - autoIndex Automatically generate index HTML pages for directories (defaults to true) * - maximumDepth The maximum recursion depth when generating metadata * @@ -91,7 +90,6 @@ const resolveFile = (path, useIndex, callback) => { */ const createRequestHandler = (options = {}) => { const registryURL = options.registryURL || 'https://registry.npmjs.org' - const redirectTTL = options.redirectTTL || 0 const autoIndex = options.autoIndex !== false const maximumDepth = options.maximumDepth || Number.MAX_VALUE @@ -141,12 +139,12 @@ const createRequestHandler = (options = {}) => { } }) } else if (version in tags) { - sendRedirect(res, createPackageURL(packageName, tags[version], filename, search), redirectTTL) + sendRedirect(res, createPackageURL(packageName, tags[version], filename, search)) } else { const maxVersion = maxSatisfyingVersion(Object.keys(versions), version) if (maxVersion) { - sendRedirect(res, createPackageURL(packageName, maxVersion, filename, search), redirectTTL) + sendRedirect(res, createPackageURL(packageName, maxVersion, filename, search)) } else { sendNotFoundError(res, `package ${displayName}`) } @@ -169,7 +167,7 @@ const createRequestHandler = (options = {}) => { sendNotFoundError(res, `file "${filename}" in package ${displayName}`) } else if (stats.isDirectory() && pathname[pathname.length - 1] !== '/') { // Append `/` to directory URLs - sendRedirect(res, pathname + '/' + search, OneYear) + sendRedirect(res, pathname + '/' + search) } else { next(file.replace(packageDir, ''), stats) } @@ -227,13 +225,13 @@ const createRequestHandler = (options = {}) => { if (query.json != null) { generateMetadata(baseDir, path, stats, maximumDepth, (error, metadata) => { if (metadata) { - sendJSON(res, metadata, OneYear) + sendJSON(res, metadata, oneYear) } else { sendServerError(res, `unable to generate JSON metadata for ${displayName}${filename}`) } }) } else if (stats.isFile()) { - sendFile(res, joinPaths(baseDir, path), stats, OneYear) + sendFile(res, joinPaths(baseDir, path), stats, oneYear) } else if (autoIndex && stats.isDirectory()) { getPackageInfo(registryURL, packageName, (error, packageInfo) => { if (error) { @@ -241,7 +239,7 @@ const createRequestHandler = (options = {}) => { } else { generateDirectoryIndexHTML(packageInfo, version, baseDir, path, (error, html) => { if (html) { - sendHTML(res, html, OneYear) + sendHTML(res, html) } else { sendServerError(res, `unable to generate index page for ${displayName}${filename}`) }