Redirect ?json and /_meta to ?meta

This commit is contained in:
MICHAEL JACKSON 2017-08-18 15:49:12 -07:00
parent e86421240d
commit 6b482f1099
5 changed files with 37 additions and 52 deletions

View File

@ -43,8 +43,8 @@ Append a `/` at the end of a URL to view a listing of all the files in a package
</tr> </tr>
<tr> <tr>
<td>`meta`</td> <td>`meta`</td>
<td>`undefined`</td> <td></td>
<td>Return metadata about a file in a package as JSON (e.g. `/any/file?meta`)</td> <td>Return metadata about any file in a package as JSON (e.g. `/any/file?meta`)</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@ -11,7 +11,6 @@ const checkBlacklist = require('./middleware/checkBlacklist')
const parsePackageURL = require('./middleware/parsePackageURL') const parsePackageURL = require('./middleware/parsePackageURL')
const fetchFile = require('./middleware/fetchFile') const fetchFile = require('./middleware/fetchFile')
const serveFile = require('./middleware/serveFile') const serveFile = require('./middleware/serveFile')
const serveMetadata = require('./middleware/serveMetadata')
/** /**
* A list of packages we refuse to serve. * A list of packages we refuse to serve.
@ -74,13 +73,6 @@ function createServer() {
maxAge: '365d' maxAge: '365d'
})) }))
app.use('/_meta',
parsePackageURL,
checkBlacklist(PackageBlacklist),
fetchFile,
serveMetadata
)
app.use('/', app.use('/',
parsePackageURL, parsePackageURL,
checkBlacklist(PackageBlacklist), checkBlacklist(PackageBlacklist),

View File

@ -3,7 +3,6 @@ const validateNPMPackageName = require('validate-npm-package-name')
const PackageURL = require('../PackageURL') const PackageURL = require('../PackageURL')
const KnownQueryParams = { const KnownQueryParams = {
json: true, // deprecated
main: true, main: true,
meta: true meta: true
} }
@ -16,23 +15,30 @@ function queryIsKnown(query) {
return Object.keys(query).every(isKnownQueryParam) return Object.keys(query).every(isKnownQueryParam)
} }
function sanitizeQuery(query) { function createSearch(query, withMeta) {
const saneQuery = {} let search = ''
Object.keys(query).forEach(function (param) { if (query.main)
if (isKnownQueryParam(param)) search += `main=${encodeURIComponent(query.main)}`
saneQuery[param] = query[param]
})
return saneQuery // Do this manually because stringify uses ?meta= for { meta: true }
if (query.meta != null || query.json != null || withMeta)
search += (search ? '&' : '') + 'meta'
return search ? `?${search}` : ''
} }
/** /**
* Parse and validate the URL. * Parse and validate the URL.
*/ */
function parsePackageURL(req, res, next) { function parsePackageURL(req, res, next) {
// Redirect /_meta/pkg to /pkg?meta.
if (req.path.match(/^\/_meta\//))
return res.redirect(req.path.substr(6) + createSearch(req.query, true))
const url = PackageURL.parse(req.url) const url = PackageURL.parse(req.url)
// Do not allow invalid URLs.
if (url == null) if (url == null)
return res.status(403).type('text').send(`Invalid URL: ${req.url}`) return res.status(403).type('text').send(`Invalid URL: ${req.url}`)
@ -45,10 +51,8 @@ function parsePackageURL(req, res, next) {
// Redirect requests with unknown query params to their equivalents // Redirect requests with unknown query params to their equivalents
// with only known params so they can be served from the cache. This // with only known params so they can be served from the cache. This
// prevents people using random query params designed to bust the cache. // prevents people using random query params designed to bust the cache.
if (!queryIsKnown(url.query)) { if (!queryIsKnown(url.query))
const search = qs.stringify(sanitizeQuery(url.query)) return res.redirect(url.pathname + createSearch(url.query))
return res.redirect(url.pathname + (search ? `?${search}` : ''))
}
req.packageName = url.packageName req.packageName = url.packageName
req.packageVersion = url.packageVersion req.packageVersion = url.packageVersion

View File

@ -1,7 +1,7 @@
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const qs = require('querystring')
const etag = require('etag') const etag = require('etag')
const getMetadata = require('./utils/getMetadata')
const getFileContentType = require('./utils/getFileContentType') const getFileContentType = require('./utils/getFileContentType')
const getIndexHTML = require('./utils/getIndexHTML') const getIndexHTML = require('./utils/getIndexHTML')
@ -10,6 +10,11 @@ const getIndexHTML = require('./utils/getIndexHTML')
*/ */
const AutoIndex = !process.env.DISABLE_INDEX 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 = getFileContentType(file) let contentType = getFileContentType(file)
@ -36,12 +41,19 @@ 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(req, res, next) { function serveFile(req, res, next) {
if (req.query.meta != null || req.query.json != null) { if (req.query.meta != null) {
// Preserve support for ?meta and ?json for backwards compat. getMetadata(req.packageDir, req.file, req.stats, MaximumDepth, function (error, metadata) {
delete req.query.meta if (error) {
delete req.query.json console.error(error)
const search = qs.stringify(req.query) res.status(500).type('text').send(`Cannot generate metadata for ${req.packageSpec}${req.filename}`)
res.redirect(`/_meta${req.pathname}${search}`) } else {
// Cache metadata for 1 year.
res.set({
'Cache-Control': 'public, max-age=31536000',
'Cache-Tag': 'meta'
}).send(metadata)
}
})
} else if (req.stats.isFile()) { } else if (req.stats.isFile()) {
// Cache files for 1 year. // Cache files for 1 year.
res.set({ res.set({

View File

@ -1,23 +0,0 @@
const getMetadata = require('./utils/getMetadata')
/**
* Maximum recursion depth for meta listings.
*/
const MaximumDepth = 128
function serveMetadata(req, res) {
getMetadata(req.packageDir, req.file, req.stats, MaximumDepth, function (error, metadata) {
if (error) {
console.error(error)
res.status(500).type('text').send(`Cannot generate metadata for ${req.packageSpec}${req.filename}`)
} else {
// Cache metadata for 1 year.
res.set({
'Cache-Control': 'public, max-age=31536000',
'Cache-Tag': 'meta'
}).send(metadata)
}
})
}
module.exports = serveMetadata