Add "meta" query param
This commit is contained in:
parent
5d726d6864
commit
de18c52eef
|
@ -39,12 +39,12 @@ Append a `/` at the end of a URL to view a listing of all the files in a package
|
|||
<tr>
|
||||
<td>`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>
|
||||
<td>`json`</td>
|
||||
<td>`meta`</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>
|
||||
</tbody>
|
||||
</table>
|
||||
|
|
|
@ -1,34 +1,38 @@
|
|||
const fs = require('fs')
|
||||
const { join: joinPaths } = require('path')
|
||||
const path = require('path')
|
||||
const { getContentType, getStats, getFileType } = require('./FileUtils')
|
||||
|
||||
const getEntries = (baseDir, path, maximumDepth) =>
|
||||
new Promise((resolve, reject) => {
|
||||
fs.readdir(joinPaths(baseDir, path), (error, files) => {
|
||||
function getEntries(dir, file, maximumDepth) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readdir(path.join(dir, file), (error, files) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
} else {
|
||||
resolve(
|
||||
Promise.all(
|
||||
files.map(f => getStats(joinPaths(baseDir, path, f)))
|
||||
).then(
|
||||
statsArray => Promise.all(statsArray.map(
|
||||
(stats, index) => getMetadata(baseDir, joinPaths(path, files[index]), stats, maximumDepth - 1)
|
||||
))
|
||||
)
|
||||
files.map(function (f) {
|
||||
return getStats(path.join(dir, file, f))
|
||||
})
|
||||
).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) =>
|
||||
new Date(time).toISOString()
|
||||
function formatTime(time) {
|
||||
return new Date(time).toISOString()
|
||||
}
|
||||
|
||||
const getMetadata = (baseDir, path, stats, maximumDepth) => {
|
||||
function getMetadata(dir, file, stats, maximumDepth) {
|
||||
const metadata = {
|
||||
path,
|
||||
lastModified: formatTime(stats.mtime),
|
||||
contentType: getContentType(path),
|
||||
contentType: getContentType(file),
|
||||
path: file,
|
||||
size: stats.size,
|
||||
type: getFileType(stats)
|
||||
}
|
||||
|
@ -36,16 +40,18 @@ const getMetadata = (baseDir, path, stats, maximumDepth) => {
|
|||
if (!stats.isDirectory() || maximumDepth === 0)
|
||||
return Promise.resolve(metadata)
|
||||
|
||||
return getEntries(baseDir, path, maximumDepth).then(files => {
|
||||
return getEntries(dir, file, maximumDepth).then(function (files) {
|
||||
metadata.files = files
|
||||
return metadata
|
||||
})
|
||||
}
|
||||
|
||||
const generateMetadata = (baseDir, path, stats, maximumDepth, callback) =>
|
||||
getMetadata(baseDir, path, stats, maximumDepth)
|
||||
.then(metadata => callback(null, metadata), callback)
|
||||
function generateMetadata(baseDir, path, stats, maximumDepth, callback) {
|
||||
return getMetadata(baseDir, path, stats, maximumDepth).then(function (metadata) {
|
||||
callback(null, metadata)
|
||||
}, callback)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generateMetadata
|
||||
get: generateMetadata
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ const decodeParam = (param) =>
|
|||
|
||||
const ValidQueryKeys = {
|
||||
main: true,
|
||||
meta: true,
|
||||
json: true
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const etag = require('etag')
|
||||
const { generateMetadata } = require('./MetadataUtils')
|
||||
const Metadata = require('./MetadataUtils')
|
||||
const { generateDirectoryIndexHTML } = require('./IndexUtils')
|
||||
const { getContentType } = require('./FileUtils')
|
||||
|
||||
|
@ -33,9 +33,9 @@ function sendFile(res, file, stats, maxAge = 0) {
|
|||
*/
|
||||
function serveFile(autoIndex, maximumDepth) {
|
||||
return function (req, res, next) {
|
||||
// TODO: change query param from "json" to "meta"
|
||||
if (req.query.json != null) {
|
||||
generateMetadata(req.packageDir, req.file, req.stats, maximumDepth, function (error, metadata) {
|
||||
// TODO: remove support for "json" query param
|
||||
if (req.query.meta != null || req.query.json != null) {
|
||||
Metadata.get(req.packageDir, req.file, req.stats, maximumDepth, function (error, metadata) {
|
||||
if (metadata) {
|
||||
res.set('Cache-Control', 'public, max-age=31536000').send(metadata)
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue