Get tests passing again
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
import semver from 'semver';
|
||||
|
||||
import addLeadingSlash from '../utils/addLeadingSlash';
|
||||
import createPackageURL from '../utils/createPackageURL';
|
||||
import createSearch from '../utils/createSearch';
|
||||
import { getPackageInfo as getNpmPackageInfo } from '../utils/npm';
|
||||
import addLeadingSlash from '../utils/addLeadingSlash.js';
|
||||
import createPackageURL from '../utils/createPackageURL.js';
|
||||
import createSearch from '../utils/createSearch.js';
|
||||
import { getPackageInfo as getNpmPackageInfo } from '../utils/npm.js';
|
||||
|
||||
function tagRedirect(req, res) {
|
||||
const version = req.packageInfo['dist-tags'][req.packageVersion];
|
||||
@ -114,41 +114,41 @@ function filenameRedirect(req, res) {
|
||||
* version if the request targets a tag or uses a semver version, or to the
|
||||
* exact filename if the request omits the filename.
|
||||
*/
|
||||
export default function fetchPackage(req, res, next) {
|
||||
getNpmPackageInfo(req.packageName).then(
|
||||
packageInfo => {
|
||||
if (packageInfo == null || packageInfo.versions == null) {
|
||||
return res
|
||||
.status(404)
|
||||
.type('text')
|
||||
.send(`Cannot find package "${req.packageName}"`);
|
||||
}
|
||||
export default async function fetchPackage(req, res, next) {
|
||||
let packageInfo;
|
||||
try {
|
||||
packageInfo = await getNpmPackageInfo(req.packageName);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
req.packageInfo = packageInfo;
|
||||
req.packageConfig = req.packageInfo.versions[req.packageVersion];
|
||||
return res
|
||||
.status(500)
|
||||
.type('text')
|
||||
.send(`Cannot get info for package "${req.packageName}"`);
|
||||
}
|
||||
|
||||
if (!req.packageConfig) {
|
||||
// Redirect to a fully-resolved version.
|
||||
if (req.packageVersion in req.packageInfo['dist-tags']) {
|
||||
return tagRedirect(req, res);
|
||||
} else {
|
||||
return semverRedirect(req, res);
|
||||
}
|
||||
}
|
||||
if (packageInfo == null || packageInfo.versions == null) {
|
||||
return res
|
||||
.status(404)
|
||||
.type('text')
|
||||
.send(`Cannot find package "${req.packageName}"`);
|
||||
}
|
||||
|
||||
if (!req.filename) {
|
||||
return filenameRedirect(req, res);
|
||||
}
|
||||
req.packageInfo = packageInfo;
|
||||
req.packageConfig = req.packageInfo.versions[req.packageVersion];
|
||||
|
||||
next();
|
||||
},
|
||||
error => {
|
||||
console.error(error);
|
||||
|
||||
return res
|
||||
.status(500)
|
||||
.type('text')
|
||||
.send(`Cannot get info for package "${req.packageName}"`);
|
||||
if (!req.packageConfig) {
|
||||
// Redirect to a fully-resolved version.
|
||||
if (req.packageVersion in req.packageInfo['dist-tags']) {
|
||||
return tagRedirect(req, res);
|
||||
} else {
|
||||
return semverRedirect(req, res);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (!req.filename) {
|
||||
return filenameRedirect(req, res);
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import path from 'path';
|
||||
|
||||
import addLeadingSlash from '../utils/addLeadingSlash';
|
||||
import createPackageURL from '../utils/createPackageURL';
|
||||
import createSearch from '../utils/createSearch';
|
||||
import { fetchPackage as fetchNpmPackage } from '../utils/npm';
|
||||
import getIntegrity from '../utils/getIntegrity';
|
||||
import getContentType from '../utils/getContentType';
|
||||
import addLeadingSlash from '../utils/addLeadingSlash.js';
|
||||
import createPackageURL from '../utils/createPackageURL.js';
|
||||
import createSearch from '../utils/createSearch.js';
|
||||
import { fetchPackage as fetchNpmPackage } from '../utils/npm.js';
|
||||
import getIntegrity from '../utils/getIntegrity.js';
|
||||
import getContentType from '../utils/getContentType.js';
|
||||
|
||||
function indexRedirect(req, res, entry) {
|
||||
// Redirect to the index file so relative imports
|
||||
@ -143,62 +143,59 @@ const trailingSlash = /\/$/;
|
||||
* Fetch and search the archive to try and find the requested file.
|
||||
* Redirect to the "index" file if a directory was requested.
|
||||
*/
|
||||
export default function findFile(req, res, next) {
|
||||
fetchNpmPackage(req.packageConfig).then(tarballStream => {
|
||||
const wantsIndex = trailingSlash.test(req.filename);
|
||||
export default async function findFile(req, res, next) {
|
||||
const wantsIndex = trailingSlash.test(req.filename);
|
||||
|
||||
// The name of the file/directory we're looking for.
|
||||
const entryName = req.filename
|
||||
.replace(multipleSlash, '/')
|
||||
.replace(trailingSlash, '')
|
||||
.replace(leadingSlash, '');
|
||||
// The name of the file/directory we're looking for.
|
||||
const entryName = req.filename
|
||||
.replace(multipleSlash, '/')
|
||||
.replace(trailingSlash, '')
|
||||
.replace(leadingSlash, '');
|
||||
|
||||
searchEntries(tarballStream, entryName, wantsIndex).then(
|
||||
({ entries, foundEntry }) => {
|
||||
if (!foundEntry) {
|
||||
return res
|
||||
.status(404)
|
||||
.set({
|
||||
'Cache-Control': 'public, max-age=31536000', // 1 year
|
||||
'Cache-Tag': 'missing, missing-entry'
|
||||
})
|
||||
.type('text')
|
||||
.send(`Cannot find "${req.filename}" in ${req.packageSpec}`);
|
||||
}
|
||||
const tarballStream = await fetchNpmPackage(req.packageConfig);
|
||||
const { entries, foundEntry } = await searchEntries(
|
||||
tarballStream,
|
||||
entryName,
|
||||
wantsIndex
|
||||
);
|
||||
|
||||
// If the foundEntry is a directory and there is no trailing slash
|
||||
// on the request path, we need to redirect to some "index" file
|
||||
// inside that directory. This is so our URLs work in a similar way
|
||||
// to require("lib") in node where it searches for `lib/index.js`
|
||||
// and `lib/index.json` when `lib` is a directory.
|
||||
if (foundEntry.type === 'directory' && !wantsIndex) {
|
||||
const indexEntry =
|
||||
entries[path.join(entryName, 'index.js')] ||
|
||||
entries[path.join(entryName, 'index.json')];
|
||||
if (!foundEntry) {
|
||||
return res
|
||||
.status(404)
|
||||
.set({
|
||||
'Cache-Control': 'public, max-age=31536000', // 1 year
|
||||
'Cache-Tag': 'missing, missing-entry'
|
||||
})
|
||||
.type('text')
|
||||
.send(`Cannot find "${req.filename}" in ${req.packageSpec}`);
|
||||
}
|
||||
|
||||
if (indexEntry && indexEntry.type === 'file') {
|
||||
return indexRedirect(req, res, indexEntry);
|
||||
} else {
|
||||
return res
|
||||
.status(404)
|
||||
.set({
|
||||
'Cache-Control': 'public, max-age=31536000', // 1 year
|
||||
'Cache-Tag': 'missing, missing-index'
|
||||
})
|
||||
.type('text')
|
||||
.send(
|
||||
`Cannot find an index in "${req.filename}" in ${
|
||||
req.packageSpec
|
||||
}`
|
||||
);
|
||||
}
|
||||
}
|
||||
// If the foundEntry is a directory and there is no trailing slash
|
||||
// on the request path, we need to redirect to some "index" file
|
||||
// inside that directory. This is so our URLs work in a similar way
|
||||
// to require("lib") in node where it searches for `lib/index.js`
|
||||
// and `lib/index.json` when `lib` is a directory.
|
||||
if (foundEntry.type === 'directory' && !wantsIndex) {
|
||||
const indexEntry =
|
||||
entries[path.join(entryName, 'index.js')] ||
|
||||
entries[path.join(entryName, 'index.json')];
|
||||
|
||||
req.entries = entries;
|
||||
req.entry = foundEntry;
|
||||
if (indexEntry && indexEntry.type === 'file') {
|
||||
return indexRedirect(req, res, indexEntry);
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
);
|
||||
});
|
||||
return res
|
||||
.status(404)
|
||||
.set({
|
||||
'Cache-Control': 'public, max-age=31536000', // 1 year
|
||||
'Cache-Tag': 'missing, missing-index'
|
||||
})
|
||||
.type('text')
|
||||
.send(`Cannot find an index in "${req.filename}" in ${req.packageSpec}`);
|
||||
}
|
||||
|
||||
req.entries = entries;
|
||||
req.entry = foundEntry;
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
@ -1,15 +1,20 @@
|
||||
import createSearch from '../utils/createSearch.js';
|
||||
|
||||
/**
|
||||
* Redirect old URLs that we no longer support.
|
||||
*/
|
||||
export default function redirectLegacyURLs(req, res, next) {
|
||||
// Permanently redirect /_meta/path to /_metadata/path
|
||||
// Permanently redirect /_meta/path to /path?meta
|
||||
if (req.path.match(/^\/_meta\//)) {
|
||||
return res.redirect(301, '/_metadata' + req.path.substr(6));
|
||||
req.query.meta = '';
|
||||
return res.redirect(301, req.path.substr(6) + createSearch(req.query));
|
||||
}
|
||||
|
||||
// Permanently redirect /path?json => /path?meta
|
||||
if (req.query.json != null) {
|
||||
return res.redirect(301, '/_metadata' + req.path);
|
||||
delete req.query.json;
|
||||
req.query.meta = '';
|
||||
return res.redirect(301, req.path + createSearch(req.query));
|
||||
}
|
||||
|
||||
next();
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import parsePackageURL from '../utils/parsePackageURL';
|
||||
import parsePackageURL from '../utils/parsePackageURL.js';
|
||||
|
||||
/**
|
||||
* Parse the URL and add various properties to the request object to
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import createSearch from '../utils/createSearch';
|
||||
import createSearch from '../utils/createSearch.js';
|
||||
|
||||
const knownQueryParams = {
|
||||
main: true, // Deprecated, see #63
|
||||
|
||||
Reference in New Issue
Block a user