Remove Cache-Control header from HTML content

This commit is contained in:
MICHAEL JACKSON 2017-06-09 13:36:31 -07:00
parent db8f1fa5a0
commit 9bc5d47f1e
3 changed files with 12 additions and 18 deletions

View File

@ -103,7 +103,6 @@ const defaultServerConfig = {
// for the middleware // for the middleware
registryURL: process.env.REGISTRY_URL || 'https://registry.npmjs.org', registryURL: process.env.REGISTRY_URL || 'https://registry.npmjs.org',
redirectTTL: process.env.REDIRECT_TTL || 500,
autoIndex: !process.env.DISABLE_INDEX autoIndex: !process.env.DISABLE_INDEX
} }

View File

@ -32,25 +32,22 @@ const sendNotFoundError = (res, what) =>
const sendServerError = (res, error) => const sendServerError = (res, error) =>
sendText(res, 500, `Server error: ${error.message || 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, { res.writeHead(statusCode, {
'Content-Type': 'text/html', 'Content-Type': 'text/html',
'Content-Length': Buffer.byteLength(html), 'Content-Length': Buffer.byteLength(html)
'Cache-Control': `public, max-age=${maxAge}`
}) })
res.end(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 location = res.req && res.req.baseUrl ? res.req.baseUrl + relativeLocation : relativeLocation
const html = `<p>You are being redirected to <a href="${location}">${location}</a>` const html = `<p>You are being redirected to <a href="${location}">${location}</a>`
res.writeHead(statusCode, { res.writeHead(statusCode, {
'Content-Type': 'text/html', 'Content-Type': 'text/html',
'Content-Length': Buffer.byteLength(html), 'Content-Length': Buffer.byteLength(html),
'Cache-Control': `public, max-age=${maxAge}`,
'Location': encodeURI(location) 'Location': encodeURI(location)
}) })

View File

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