Add "meta" query param

This commit is contained in:
MICHAEL JACKSON 2017-08-10 23:12:22 -07:00
parent 5d726d6864
commit de18c52eef
4 changed files with 34 additions and 27 deletions

View File

@ -39,12 +39,12 @@ Append a `/` at the end of a URL to view a listing of all the files in a package
<tr> <tr>
<td>`main`</td> <td>`main`</td>
<td>`unpkg`, `browser`, `main`</td> <td>`unpkg`, `browser`, `main`</td>
<td>The name of the field in [package.json](https://docs.npmjs.com/files/package.json) to use as the main entry point when there is no file path in the URL.</td> <td>The name of the field in [package.json](https://docs.npmjs.com/files/package.json) to use as the main entry point when there is no file path in the URL</td>
</tr> </tr>
<tr> <tr>
<td>`json`</td> <td>`meta`</td>
<td>`undefined`</td> <td>`undefined`</td>
<td>Return a recursive list of metadata about all the files in a directory as JSON (e.g. `/any/path/?json`). Note: this only works for directories.</td> <td>Return metadata about a file in a package as JSON (e.g. `/any/file?meta`)</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@ -1,34 +1,38 @@
const fs = require('fs') const fs = require('fs')
const { join: joinPaths } = require('path') const path = require('path')
const { getContentType, getStats, getFileType } = require('./FileUtils') const { getContentType, getStats, getFileType } = require('./FileUtils')
const getEntries = (baseDir, path, maximumDepth) => function getEntries(dir, file, maximumDepth) {
new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.readdir(joinPaths(baseDir, path), (error, files) => { fs.readdir(path.join(dir, file), (error, files) => {
if (error) { if (error) {
reject(error) reject(error)
} else { } else {
resolve( resolve(
Promise.all( Promise.all(
files.map(f => getStats(joinPaths(baseDir, path, f))) files.map(function (f) {
).then( return getStats(path.join(dir, file, f))
statsArray => Promise.all(statsArray.map( })
(stats, index) => getMetadata(baseDir, joinPaths(path, files[index]), stats, maximumDepth - 1) ).then(function (statsArray) {
)) return Promise.all(statsArray.map(function (stats, index) {
) return getMetadata(dir, path.join(file, files[index]), stats, maximumDepth - 1)
}))
})
) )
} }
}) })
}) })
}
const formatTime = (time) => function formatTime(time) {
new Date(time).toISOString() return new Date(time).toISOString()
}
const getMetadata = (baseDir, path, stats, maximumDepth) => { function getMetadata(dir, file, stats, maximumDepth) {
const metadata = { const metadata = {
path,
lastModified: formatTime(stats.mtime), lastModified: formatTime(stats.mtime),
contentType: getContentType(path), contentType: getContentType(file),
path: file,
size: stats.size, size: stats.size,
type: getFileType(stats) type: getFileType(stats)
} }
@ -36,16 +40,18 @@ const getMetadata = (baseDir, path, stats, maximumDepth) => {
if (!stats.isDirectory() || maximumDepth === 0) if (!stats.isDirectory() || maximumDepth === 0)
return Promise.resolve(metadata) return Promise.resolve(metadata)
return getEntries(baseDir, path, maximumDepth).then(files => { return getEntries(dir, file, maximumDepth).then(function (files) {
metadata.files = files metadata.files = files
return metadata return metadata
}) })
} }
const generateMetadata = (baseDir, path, stats, maximumDepth, callback) => function generateMetadata(baseDir, path, stats, maximumDepth, callback) {
getMetadata(baseDir, path, stats, maximumDepth) return getMetadata(baseDir, path, stats, maximumDepth).then(function (metadata) {
.then(metadata => callback(null, metadata), callback) callback(null, metadata)
}, callback)
}
module.exports = { module.exports = {
generateMetadata get: generateMetadata
} }

View File

@ -7,6 +7,7 @@ const decodeParam = (param) =>
const ValidQueryKeys = { const ValidQueryKeys = {
main: true, main: true,
meta: true,
json: true json: true
} }

View File

@ -1,7 +1,7 @@
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const etag = require('etag') const etag = require('etag')
const { generateMetadata } = require('./MetadataUtils') const Metadata = require('./MetadataUtils')
const { generateDirectoryIndexHTML } = require('./IndexUtils') const { generateDirectoryIndexHTML } = require('./IndexUtils')
const { getContentType } = require('./FileUtils') const { getContentType } = require('./FileUtils')
@ -33,9 +33,9 @@ function sendFile(res, file, stats, maxAge = 0) {
*/ */
function serveFile(autoIndex, maximumDepth) { function serveFile(autoIndex, maximumDepth) {
return function (req, res, next) { return function (req, res, next) {
// TODO: change query param from "json" to "meta" // TODO: remove support for "json" query param
if (req.query.json != null) { if (req.query.meta != null || req.query.json != null) {
generateMetadata(req.packageDir, req.file, req.stats, maximumDepth, function (error, metadata) { Metadata.get(req.packageDir, req.file, req.stats, maximumDepth, function (error, metadata) {
if (metadata) { if (metadata) {
res.set('Cache-Control', 'public, max-age=31536000').send(metadata) res.set('Cache-Control', 'public, max-age=31536000').send(metadata)
} else { } else {