Experimental port to Firebase hosting
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
const BlacklistAPI = require('../BlacklistAPI');
|
||||
import { includesPackage } from '../utils/blacklist';
|
||||
|
||||
function checkBlacklist(req, res, next) {
|
||||
BlacklistAPI.includesPackage(req.packageName).then(
|
||||
export default function checkBlacklist(req, res, next) {
|
||||
includesPackage(req.packageName).then(
|
||||
blacklisted => {
|
||||
// Disallow packages that have been blacklisted.
|
||||
if (blacklisted) {
|
||||
@ -21,5 +21,3 @@ function checkBlacklist(req, res, next) {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = checkBlacklist;
|
||||
|
||||
@ -1,29 +0,0 @@
|
||||
const invariant = require('invariant');
|
||||
|
||||
const createAssets = require('./utils/createAssets');
|
||||
|
||||
/**
|
||||
* An express middleware that sets req.assets from the
|
||||
* latest result from a running webpack compiler (i.e. using
|
||||
* webpack-dev-middleware). Should only be used in dev.
|
||||
*/
|
||||
function devAssets(webpackCompiler) {
|
||||
let assets;
|
||||
webpackCompiler.plugin('done', stats => {
|
||||
assets = createAssets(stats.toJson());
|
||||
});
|
||||
|
||||
return (req, res, next) => {
|
||||
invariant(
|
||||
assets != null,
|
||||
'devAssets middleware needs a running compiler; ' +
|
||||
'use webpack-dev-middleware in front of devAssets'
|
||||
);
|
||||
|
||||
req.assets = assets;
|
||||
|
||||
next();
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = devAssets;
|
||||
@ -1,10 +1,10 @@
|
||||
const semver = require('semver');
|
||||
import semver from 'semver';
|
||||
|
||||
const addLeadingSlash = require('../utils/addLeadingSlash');
|
||||
const createPackageURL = require('../utils/createPackageURL');
|
||||
const createSearch = require('../utils/createSearch');
|
||||
const getNpmPackageInfo = require('../utils/getNpmPackageInfo');
|
||||
const incrementCounter = require('../utils/incrementCounter');
|
||||
import addLeadingSlash from '../utils/addLeadingSlash';
|
||||
import createPackageURL from '../utils/createPackageURL';
|
||||
import createSearch from '../utils/createSearch';
|
||||
import getNpmPackageInfo from '../utils/getNpmPackageInfo';
|
||||
// import incrementCounter from '../utils/incrementCounter';
|
||||
|
||||
function tagRedirect(req, res) {
|
||||
const version = req.packageInfo['dist-tags'][req.packageVersion];
|
||||
@ -64,11 +64,11 @@ function filenameRedirect(req, res) {
|
||||
|
||||
// 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
|
||||
);
|
||||
// incrementCounter(
|
||||
// 'package-json-custom-main',
|
||||
// req.packageSpec + '?main=' + req.query.main,
|
||||
// 1
|
||||
// );
|
||||
} else if (
|
||||
req.packageConfig.unpkg &&
|
||||
typeof req.packageConfig.unpkg === 'string'
|
||||
@ -83,7 +83,7 @@ function filenameRedirect(req, res) {
|
||||
|
||||
// Count which packages are using this so we can warn them when we
|
||||
// remove this functionality.
|
||||
incrementCounter('package-json-browser-fallback', req.packageSpec, 1);
|
||||
// incrementCounter('package-json-browser-fallback', req.packageSpec, 1);
|
||||
} else {
|
||||
filename = req.packageConfig.main || '/index.js';
|
||||
}
|
||||
@ -111,7 +111,7 @@ 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.
|
||||
*/
|
||||
function fetchPackage(req, res, next) {
|
||||
export default function fetchPackage(req, res, next) {
|
||||
getNpmPackageInfo(req.packageName).then(
|
||||
packageInfo => {
|
||||
if (packageInfo == null || packageInfo.versions == null) {
|
||||
@ -149,5 +149,3 @@ function fetchPackage(req, res, next) {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = fetchPackage;
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
const path = require('path');
|
||||
import path from 'path';
|
||||
|
||||
const addLeadingSlash = require('../utils/addLeadingSlash');
|
||||
const createPackageURL = require('../utils/createPackageURL');
|
||||
const createSearch = require('../utils/createSearch');
|
||||
const fetchNpmPackage = require('../utils/fetchNpmPackage');
|
||||
const getIntegrity = require('../utils/getIntegrity');
|
||||
const getContentType = require('../utils/getContentType');
|
||||
import addLeadingSlash from '../utils/addLeadingSlash';
|
||||
import createPackageURL from '../utils/createPackageURL';
|
||||
import createSearch from '../utils/createSearch';
|
||||
import fetchNpmPackage from '../utils/fetchNpmPackage';
|
||||
import getIntegrity from '../utils/getIntegrity';
|
||||
import getContentType from '../utils/getContentType';
|
||||
|
||||
function indexRedirect(req, res, entry) {
|
||||
// Redirect to the index file so relative imports
|
||||
@ -124,7 +124,7 @@ const multipleSlash = /\/\/+/;
|
||||
* Fetch and search the archive to try and find the requested file.
|
||||
* Redirect to the "index" file if a directory was requested.
|
||||
*/
|
||||
function findFile(req, res, next) {
|
||||
export default function findFile(req, res, next) {
|
||||
fetchNpmPackage(req.packageConfig).then(tarballStream => {
|
||||
const entryName = req.filename
|
||||
.replace(multipleSlash, '/')
|
||||
@ -173,5 +173,3 @@ function findFile(req, res, next) {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = findFile;
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
const createSearch = require('../utils/createSearch');
|
||||
import createSearch from '../utils/createSearch';
|
||||
|
||||
/**
|
||||
* Redirect old URLs that we no longer support.
|
||||
*/
|
||||
function redirectLegacyURLs(req, res, next) {
|
||||
export default function redirectLegacyURLs(req, res, next) {
|
||||
// Permanently redirect /_meta/path to /path?meta.
|
||||
if (req.path.match(/^\/_meta\//)) {
|
||||
req.query.meta = '';
|
||||
@ -19,5 +19,3 @@ function redirectLegacyURLs(req, res, next) {
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = redirectLegacyURLs;
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
* Adds the given scope to the array in req.auth if the user has sufficient
|
||||
* permissions. Otherwise rejects the request.
|
||||
*/
|
||||
function requireAuth(scope) {
|
||||
export default function requireAuth(scope) {
|
||||
let checkScopes;
|
||||
if (scope.includes('.')) {
|
||||
const parts = scope.split('.');
|
||||
@ -36,5 +36,3 @@ function requireAuth(scope) {
|
||||
next();
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = requireAuth;
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const invariant = require('invariant');
|
||||
|
||||
const createAssets = require('./utils/createAssets');
|
||||
|
||||
/**
|
||||
* An express middleware that sets req.assets from the build
|
||||
* info in the given stats file. Should be used in production.
|
||||
*/
|
||||
function staticAssets(webpackStatsFile) {
|
||||
let stats;
|
||||
try {
|
||||
stats = JSON.parse(fs.readFileSync(webpackStatsFile, 'utf8'));
|
||||
} catch (error) {
|
||||
invariant(
|
||||
false,
|
||||
'staticAssets middleware cannot read the build stats in %s; ' +
|
||||
'run the `build` script before starting the server',
|
||||
webpackStatsFile
|
||||
);
|
||||
}
|
||||
|
||||
const assets = createAssets(stats);
|
||||
|
||||
return (req, res, next) => {
|
||||
req.assets = assets;
|
||||
next();
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = staticAssets;
|
||||
@ -1,6 +1,4 @@
|
||||
const AuthAPI = require('../AuthAPI');
|
||||
|
||||
const ReadMethods = { GET: true, HEAD: true };
|
||||
import { verifyToken } from '../utils/auth';
|
||||
|
||||
function decodeBase64(string) {
|
||||
return Buffer.from(string, 'base64').toString();
|
||||
@ -9,22 +7,20 @@ function decodeBase64(string) {
|
||||
/**
|
||||
* Sets req.user from the payload in the auth token in the request.
|
||||
*/
|
||||
function userToken(req, res, next) {
|
||||
if (req.user) {
|
||||
export default function userToken(req, res, next) {
|
||||
if (req.user !== undefined) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const auth = req.get('Authorization');
|
||||
const token = auth
|
||||
? decodeBase64(auth)
|
||||
: (ReadMethods[req.method] ? req.query : req.body).token;
|
||||
const token = auth && decodeBase64(auth);
|
||||
|
||||
if (!token) {
|
||||
req.user = null;
|
||||
return next();
|
||||
}
|
||||
|
||||
AuthAPI.verifyToken(token).then(
|
||||
verifyToken(token).then(
|
||||
payload => {
|
||||
req.user = payload;
|
||||
next();
|
||||
@ -44,5 +40,3 @@ function userToken(req, res, next) {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = userToken;
|
||||
|
||||
@ -1,40 +0,0 @@
|
||||
/**
|
||||
* Creates an assets object that is stored on req.assets.
|
||||
*/
|
||||
function createAssets(webpackStats) {
|
||||
const { publicPath, assetsByChunkName } = webpackStats;
|
||||
|
||||
/**
|
||||
* Returns a public URL to the given asset.
|
||||
*/
|
||||
const createURL = asset => publicPath + asset;
|
||||
|
||||
/**
|
||||
* Returns an array of URLs to all assets in the given chunks.
|
||||
*/
|
||||
const getAll = (chunks = ['main']) =>
|
||||
(Array.isArray(chunks) ? chunks : [chunks])
|
||||
.reduce((memo, chunk) => memo.concat(assetsByChunkName[chunk] || []), [])
|
||||
.map(createURL);
|
||||
|
||||
/**
|
||||
* Returns an array of URLs to all JavaScript files in the given chunks.
|
||||
*/
|
||||
const getScripts = (...chunks) =>
|
||||
getAll(...chunks).filter(asset => /\.js$/.test(asset));
|
||||
|
||||
/**
|
||||
* Returns an array of URLs to all CSS files in the given chunks.
|
||||
*/
|
||||
const getStyles = (...chunks) =>
|
||||
getAll(...chunks).filter(asset => /\.css$/.test(asset));
|
||||
|
||||
return {
|
||||
createURL,
|
||||
getAll,
|
||||
getScripts,
|
||||
getStyles
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = createAssets;
|
||||
@ -1,4 +1,4 @@
|
||||
const validateNpmPackageName = require('validate-npm-package-name');
|
||||
import validateNpmPackageName from 'validate-npm-package-name';
|
||||
|
||||
const hexValue = /^[a-f0-9]+$/i;
|
||||
|
||||
@ -9,7 +9,7 @@ function isHash(value) {
|
||||
/**
|
||||
* Reject requests for invalid npm package names.
|
||||
*/
|
||||
function validatePackageName(req, res, next) {
|
||||
export default function validatePackageName(req, res, next) {
|
||||
if (isHash(req.packageName)) {
|
||||
return res
|
||||
.status(403)
|
||||
@ -30,5 +30,3 @@ function validatePackageName(req, res, next) {
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = validatePackageName;
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
const parsePackageURL = require('../utils/parsePackageURL');
|
||||
import parsePackageURL from '../utils/parsePackageURL';
|
||||
|
||||
/**
|
||||
* Parse the URL and add various properties to the request object to
|
||||
* do with the package/file being requested. Reject invalid URLs.
|
||||
*/
|
||||
function validatePackageURL(req, res, next) {
|
||||
export default function validatePackageURL(req, res, next) {
|
||||
const url = parsePackageURL(req.url);
|
||||
|
||||
if (url == null) {
|
||||
@ -21,5 +21,3 @@ function validatePackageURL(req, res, next) {
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = validatePackageURL;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const createSearch = require('../utils/createSearch');
|
||||
import createSearch from '../utils/createSearch';
|
||||
|
||||
const knownQueryParams = {
|
||||
main: true, // Deprecated, see #63
|
||||
@ -23,12 +23,10 @@ function sanitizeQuery(originalQuery) {
|
||||
/**
|
||||
* Reject URLs with invalid query parameters to increase cache hit rates.
|
||||
*/
|
||||
function validateQuery(req, res, next) {
|
||||
export default function validateQuery(req, res, next) {
|
||||
if (!Object.keys(req.query).every(isKnownQueryParam)) {
|
||||
return res.redirect(302, req.path + createSearch(sanitizeQuery(req.query)));
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = validateQuery;
|
||||
|
||||
Reference in New Issue
Block a user