From de18c52eef0f9003176bbf39cc6807972fa620b2 Mon Sep 17 00:00:00 2001 From: MICHAEL JACKSON Date: Thu, 10 Aug 2017 23:12:22 -0700 Subject: [PATCH] Add "meta" query param --- client/Home.md | 6 ++-- server/middleware/MetadataUtils.js | 46 +++++++++++++++++------------- server/middleware/PackageUtils.js | 1 + server/middleware/serveFile.js | 8 +++--- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/client/Home.md b/client/Home.md index a859c18..f4dd485 100644 --- a/client/Home.md +++ b/client/Home.md @@ -39,12 +39,12 @@ Append a `/` at the end of a URL to view a listing of all the files in a package `main` `unpkg`, `browser`, `main` - 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. + 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 - `json` + `meta` `undefined` - 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. + Return metadata about a file in a package as JSON (e.g. `/any/file?meta`) diff --git a/server/middleware/MetadataUtils.js b/server/middleware/MetadataUtils.js index 6fbe7ed..6861078 100644 --- a/server/middleware/MetadataUtils.js +++ b/server/middleware/MetadataUtils.js @@ -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 } diff --git a/server/middleware/PackageUtils.js b/server/middleware/PackageUtils.js index 2925e3f..e86f332 100644 --- a/server/middleware/PackageUtils.js +++ b/server/middleware/PackageUtils.js @@ -7,6 +7,7 @@ const decodeParam = (param) => const ValidQueryKeys = { main: true, + meta: true, json: true } diff --git a/server/middleware/serveFile.js b/server/middleware/serveFile.js index 91c0ead..812e601 100644 --- a/server/middleware/serveFile.js +++ b/server/middleware/serveFile.js @@ -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 {