2018-05-17 22:21:35 +00:00
|
|
|
const semver = require("semver");
|
|
|
|
|
2018-07-04 23:30:11 +00:00
|
|
|
const addLeadingSlash = require("../utils/addLeadingSlash");
|
2018-05-17 22:21:35 +00:00
|
|
|
const createPackageURL = require("../utils/createPackageURL");
|
2018-07-14 22:12:37 +00:00
|
|
|
const createSearch = require("../utils/createSearch");
|
2018-07-15 00:41:58 +00:00
|
|
|
const getNpmPackageInfo = require("../utils/getNpmPackageInfo");
|
2018-07-04 23:30:11 +00:00
|
|
|
const incrementCounter = require("../utils/incrementCounter");
|
2018-05-17 22:21:35 +00:00
|
|
|
|
|
|
|
function tagRedirect(req, res) {
|
2018-07-04 23:30:11 +00:00
|
|
|
const version = req.packageInfo["dist-tags"][req.packageVersion];
|
|
|
|
|
2018-05-17 22:21:35 +00:00
|
|
|
// Cache tag redirects for 1 minute.
|
|
|
|
res
|
|
|
|
.set({
|
|
|
|
"Cache-Control": "public, max-age=60",
|
|
|
|
"Cache-Tag": "redirect,tag-redirect"
|
|
|
|
})
|
|
|
|
.redirect(
|
|
|
|
302,
|
2018-07-04 23:30:11 +00:00
|
|
|
createPackageURL(req.packageName, version, req.filename, req.search)
|
2018-05-17 22:21:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function semverRedirect(req, res) {
|
|
|
|
const maxVersion = semver.maxSatisfying(
|
|
|
|
Object.keys(req.packageInfo.versions),
|
|
|
|
req.packageVersion
|
|
|
|
);
|
|
|
|
|
|
|
|
if (maxVersion) {
|
|
|
|
// Cache semver redirects for 1 minute.
|
|
|
|
res
|
|
|
|
.set({
|
|
|
|
"Cache-Control": "public, max-age=60",
|
|
|
|
"Cache-Tag": "redirect,semver-redirect"
|
|
|
|
})
|
|
|
|
.redirect(
|
|
|
|
302,
|
|
|
|
createPackageURL(req.packageName, maxVersion, req.filename, req.search)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
res
|
|
|
|
.status(404)
|
|
|
|
.type("text")
|
|
|
|
.send(`Cannot find package ${req.packageSpec}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 23:30:11 +00:00
|
|
|
function filenameRedirect(req, res) {
|
|
|
|
let filename;
|
|
|
|
if (req.query.module != null) {
|
|
|
|
// See https://github.com/rollup/rollup/wiki/pkg.module
|
|
|
|
filename =
|
|
|
|
req.packageConfig.module ||
|
|
|
|
req.packageConfig["jsnext:main"] ||
|
|
|
|
"/index.js";
|
|
|
|
} else if (
|
|
|
|
req.query.main &&
|
|
|
|
req.packageConfig[req.query.main] &&
|
|
|
|
typeof req.packageConfig[req.query.main] === "string"
|
|
|
|
) {
|
|
|
|
// Deprecated, see #63
|
|
|
|
filename = req.packageConfig[req.query.main];
|
|
|
|
|
|
|
|
// Count which packages are using this so we can warn them when we
|
|
|
|
// remove this functionality.
|
|
|
|
incrementCounter(
|
|
|
|
"package-json-custom-main",
|
|
|
|
req.packageSpec + "?main=" + req.query.main,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
req.packageConfig.unpkg &&
|
|
|
|
typeof req.packageConfig.unpkg === "string"
|
|
|
|
) {
|
|
|
|
filename = req.packageConfig.unpkg;
|
|
|
|
} else if (
|
|
|
|
req.packageConfig.browser &&
|
|
|
|
typeof req.packageConfig.browser === "string"
|
|
|
|
) {
|
|
|
|
// Deprecated, see #63
|
|
|
|
filename = req.packageConfig.browser;
|
|
|
|
|
|
|
|
// Count which packages are using this so we can warn them when we
|
|
|
|
// remove this functionality.
|
|
|
|
incrementCounter("package-json-browser-fallback", req.packageSpec, 1);
|
|
|
|
} else {
|
|
|
|
filename = req.packageConfig.main || "/index.js";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the exact filename so relative imports
|
|
|
|
// and URLs resolve correctly.
|
|
|
|
res
|
|
|
|
.set({
|
2018-08-08 01:59:45 +00:00
|
|
|
"Cache-Control": "public,max-age=31536000", // 1 year
|
2018-07-04 23:30:11 +00:00
|
|
|
"Cache-Tag": "redirect,filename-redirect"
|
|
|
|
})
|
|
|
|
.redirect(
|
|
|
|
302,
|
|
|
|
createPackageURL(
|
|
|
|
req.packageName,
|
|
|
|
req.packageVersion,
|
|
|
|
addLeadingSlash(filename),
|
|
|
|
createSearch(req.query)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-17 22:21:35 +00:00
|
|
|
/**
|
|
|
|
* Fetch the package metadata and tarball from npm. Redirect to the exact
|
2018-07-04 23:30:11 +00:00
|
|
|
* version if the request targets a tag or uses a semver version, or to the
|
|
|
|
* exact filename if the request omits the filename.
|
2018-05-17 22:21:35 +00:00
|
|
|
*/
|
|
|
|
function fetchPackage(req, res, next) {
|
2018-07-15 00:41:58 +00:00
|
|
|
getNpmPackageInfo(req.packageName).then(
|
2018-05-21 22:44:00 +00:00
|
|
|
packageInfo => {
|
|
|
|
if (packageInfo == null || packageInfo.versions == null) {
|
|
|
|
return res
|
|
|
|
.status(404)
|
|
|
|
.type("text")
|
|
|
|
.send(`Cannot find package "${req.packageName}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
req.packageInfo = packageInfo;
|
2018-07-04 23:30:11 +00:00
|
|
|
req.packageConfig = req.packageInfo.versions[req.packageVersion];
|
2018-05-21 22:44:00 +00:00
|
|
|
|
2018-07-04 23:30:11 +00:00
|
|
|
if (!req.packageConfig) {
|
2018-08-08 01:59:45 +00:00
|
|
|
// Redirect to a fully-resolved version.
|
2018-07-04 23:30:11 +00:00
|
|
|
if (req.packageVersion in req.packageInfo["dist-tags"]) {
|
|
|
|
return tagRedirect(req, res);
|
|
|
|
} else {
|
|
|
|
return semverRedirect(req, res);
|
|
|
|
}
|
2018-05-21 22:44:00 +00:00
|
|
|
}
|
2018-07-04 23:30:11 +00:00
|
|
|
|
|
|
|
if (!req.filename) {
|
|
|
|
return filenameRedirect(req, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
2018-05-21 22:44:00 +00:00
|
|
|
},
|
|
|
|
error => {
|
2018-05-17 22:21:35 +00:00
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.type("text")
|
|
|
|
.send(`Cannot get info for package "${req.packageName}"`);
|
|
|
|
}
|
2018-05-21 22:44:00 +00:00
|
|
|
);
|
2018-05-17 22:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = fetchPackage;
|