Remove Firebase hosting, back to Express
This commit is contained in:
3
modules/actions/serveAuth.js
Normal file
3
modules/actions/serveAuth.js
Normal file
@ -0,0 +1,3 @@
|
||||
export default function serveAuth(req, res) {
|
||||
res.send({ auth: req.user });
|
||||
}
|
||||
@ -1,8 +1,8 @@
|
||||
import serveAutoIndexPage from './serveAutoIndexPage';
|
||||
import serveHTMLModule from './serveHTMLModule';
|
||||
import serveJavaScriptModule from './serveJavaScriptModule';
|
||||
import serveStaticFile from './serveStaticFile';
|
||||
import serveMetadata from './serveMetadata';
|
||||
import serveStaticFile from './serveStaticFile';
|
||||
|
||||
/**
|
||||
* Send the file, JSON metadata, or HTML directory listing.
|
||||
|
||||
5
modules/actions/servePublicKey.js
Normal file
5
modules/actions/servePublicKey.js
Normal file
@ -0,0 +1,5 @@
|
||||
import { publicKey } from '../utils/secret';
|
||||
|
||||
export default function servePublicKey(req, res) {
|
||||
res.send({ publicKey });
|
||||
}
|
||||
@ -2,7 +2,7 @@ import { subDays, startOfDay, startOfSecond } from 'date-fns';
|
||||
|
||||
import { getStats } from '../utils/stats';
|
||||
|
||||
export default function showStats(req, res) {
|
||||
export default function serveStats(req, res) {
|
||||
let since, until;
|
||||
if (req.query.period) {
|
||||
switch (req.query.period) {
|
||||
@ -1,3 +0,0 @@
|
||||
export default function showAuth(req, res) {
|
||||
res.send({ auth: req.user });
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
import { secretKey } from '../config';
|
||||
|
||||
export default function showPublicKey(req, res) {
|
||||
res.send({ publicKey: secretKey.public });
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
import invariant from 'invariant';
|
||||
|
||||
export const npmRegistryURL =
|
||||
process.env.NPM_REGISTRY_URL || 'https://registry.npmjs.org';
|
||||
|
||||
export const origin = process.env.ORIGIN || 'https://unpkg.com';
|
||||
|
||||
export const secretKey = process.env.SECRET_KEY;
|
||||
|
||||
invariant(secretKey, 'Missing $SECRET_KEY environment variable');
|
||||
@ -1,15 +0,0 @@
|
||||
import { https } from 'firebase-functions';
|
||||
|
||||
// import serveAuth from './serveAuth';
|
||||
import serveMainPage from './serveMainPage';
|
||||
import serveNpmPackageFile from './serveNpmPackageFile';
|
||||
import servePublicKey from './servePublicKey';
|
||||
import serveStats from './serveStats';
|
||||
|
||||
export default {
|
||||
// serveAuth: https.onRequest(serveAuth),
|
||||
serveMainPage: https.onRequest(serveMainPage),
|
||||
serveNpmPackageFile: https.onRequest(serveNpmPackageFile),
|
||||
servePublicKey: https.onRequest(servePublicKey),
|
||||
serveStats: https.onRequest(serveStats)
|
||||
};
|
||||
@ -1,15 +0,0 @@
|
||||
import cors from 'cors';
|
||||
import express from 'express';
|
||||
|
||||
import logging from '../middleware/logging';
|
||||
import userToken from '../middleware/userToken';
|
||||
import showAuth from '../actions/showAuth';
|
||||
|
||||
const app = express.Router();
|
||||
|
||||
app.use(logging);
|
||||
app.use(cors());
|
||||
app.use(userToken);
|
||||
app.use(showAuth);
|
||||
|
||||
export default app;
|
||||
@ -1,11 +0,0 @@
|
||||
import express from 'express';
|
||||
|
||||
import logging from '../middleware/logging';
|
||||
import serveMainPage from '../actions/serveMainPage';
|
||||
|
||||
const app = express.Router();
|
||||
|
||||
app.use(logging);
|
||||
app.use(serveMainPage);
|
||||
|
||||
export default app;
|
||||
@ -1,25 +0,0 @@
|
||||
import cors from 'cors';
|
||||
import express from 'express';
|
||||
|
||||
import fetchPackage from '../middleware/fetchPackage';
|
||||
import findFile from '../middleware/findFile';
|
||||
import logging from '../middleware/logging';
|
||||
import redirectLegacyURLs from '../middleware/redirectLegacyURLs';
|
||||
import validatePackageURL from '../middleware/validatePackageURL';
|
||||
import validatePackageName from '../middleware/validatePackageName';
|
||||
import validateQuery from '../middleware/validateQuery';
|
||||
import serveFile from '../actions/serveFile';
|
||||
|
||||
const app = express.Router();
|
||||
|
||||
app.use(logging);
|
||||
app.use(cors());
|
||||
app.use(redirectLegacyURLs);
|
||||
app.use(validatePackageURL);
|
||||
app.use(validatePackageName);
|
||||
app.use(validateQuery);
|
||||
app.use(fetchPackage);
|
||||
app.use(findFile);
|
||||
app.use(serveFile);
|
||||
|
||||
export default app;
|
||||
@ -1,13 +0,0 @@
|
||||
import cors from 'cors';
|
||||
import express from 'express';
|
||||
|
||||
import logging from '../middleware/logging';
|
||||
import showPublicKey from '../actions/showPublicKey';
|
||||
|
||||
const app = express.Router();
|
||||
|
||||
app.use(logging);
|
||||
app.use(cors());
|
||||
app.use(showPublicKey);
|
||||
|
||||
export default app;
|
||||
@ -1,13 +0,0 @@
|
||||
import cors from 'cors';
|
||||
import express from 'express';
|
||||
|
||||
import logging from '../middleware/logging';
|
||||
import showStats from '../actions/showStats';
|
||||
|
||||
const app = express.Router();
|
||||
|
||||
app.use(logging);
|
||||
app.use(cors());
|
||||
app.use(showStats);
|
||||
|
||||
export default app;
|
||||
5
modules/middleware/cors.js
Normal file
5
modules/middleware/cors.js
Normal file
@ -0,0 +1,5 @@
|
||||
import corsMiddleware from 'cors';
|
||||
|
||||
const cors = corsMiddleware();
|
||||
|
||||
export default cors;
|
||||
@ -1,6 +1,6 @@
|
||||
import morgan from 'morgan';
|
||||
|
||||
const logging = morgan(
|
||||
const logger = morgan(
|
||||
process.env.NODE_ENV === 'development'
|
||||
? 'dev'
|
||||
: ':date[clf] - :method :url :status :res[content-length] - :response-time ms',
|
||||
@ -12,4 +12,4 @@ const logging = morgan(
|
||||
}
|
||||
);
|
||||
|
||||
export default logging;
|
||||
export default logger;
|
||||
12
modules/middleware/staticFiles.js
Normal file
12
modules/middleware/staticFiles.js
Normal file
@ -0,0 +1,12 @@
|
||||
import express from 'express';
|
||||
|
||||
const staticMiddleware = express.static('public', { maxAge: '1y' });
|
||||
|
||||
export default function staticFiles(req, res, next) {
|
||||
if (req.query.meta != null) {
|
||||
// Let ?meta requests fall through.
|
||||
return next();
|
||||
}
|
||||
|
||||
staticMiddleware(req, res, next);
|
||||
}
|
||||
57
modules/server.js
Normal file
57
modules/server.js
Normal file
@ -0,0 +1,57 @@
|
||||
import express from 'express';
|
||||
|
||||
// import serveAuth from './actions/serveAuth';
|
||||
import serveFile from './actions/serveFile';
|
||||
import serveMainPage from './actions/serveMainPage';
|
||||
import servePublicKey from './actions/servePublicKey';
|
||||
import serveStats from './actions/serveStats';
|
||||
|
||||
import cors from './middleware/cors';
|
||||
import fetchPackage from './middleware/fetchPackage';
|
||||
import findFile from './middleware/findFile';
|
||||
import logger from './middleware/logger';
|
||||
import redirectLegacyURLs from './middleware/redirectLegacyURLs';
|
||||
import staticFiles from './middleware/staticFiles';
|
||||
// import userToken from './middleware/userToken';
|
||||
import validatePackageURL from './middleware/validatePackageURL';
|
||||
import validatePackageName from './middleware/validatePackageName';
|
||||
import validateQuery from './middleware/validateQuery';
|
||||
|
||||
import createRouter from './utils/createRouter';
|
||||
|
||||
const port = process.env.PORT || '8080';
|
||||
|
||||
const app = express();
|
||||
|
||||
app.disable('x-powered-by');
|
||||
|
||||
app.use(logger);
|
||||
app.use(staticFiles);
|
||||
|
||||
app.get('/', serveMainPage);
|
||||
|
||||
app.use(redirectLegacyURLs);
|
||||
app.use(cors);
|
||||
|
||||
app.use(
|
||||
'/api',
|
||||
createRouter(app => {
|
||||
// app.get('/auth', userToken, serveAuth);
|
||||
app.get('/public-key', servePublicKey);
|
||||
app.get('/stats', serveStats);
|
||||
})
|
||||
);
|
||||
|
||||
app.get(
|
||||
'*',
|
||||
validatePackageURL,
|
||||
validatePackageName,
|
||||
validateQuery,
|
||||
fetchPackage,
|
||||
findFile,
|
||||
serveFile
|
||||
);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log('Server listening on port %s, Ctrl+C to quit', port);
|
||||
});
|
||||
@ -2,7 +2,7 @@ import crypto from 'crypto';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
import data from './data';
|
||||
import { secretKey } from '../config';
|
||||
import { privateKey, publicKey } from './secret';
|
||||
|
||||
function getCurrentSeconds() {
|
||||
return Math.floor(Date.now() / 1000);
|
||||
@ -21,18 +21,13 @@ export function createToken(scopes = {}) {
|
||||
scopes
|
||||
};
|
||||
|
||||
jwt.sign(
|
||||
payload,
|
||||
secretKey.private,
|
||||
{ algorithm: 'RS256' },
|
||||
(error, token) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(token);
|
||||
}
|
||||
jwt.sign(payload, privateKey, { algorithm: 'RS256' }, (error, token) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(token);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -42,7 +37,7 @@ export function verifyToken(token) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = { algorithms: ['RS256'] };
|
||||
|
||||
jwt.verify(token, secretKey.public, options, (error, payload) => {
|
||||
jwt.verify(token, publicKey, options, (error, payload) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
|
||||
7
modules/utils/createRouter.js
Normal file
7
modules/utils/createRouter.js
Normal file
@ -0,0 +1,7 @@
|
||||
import express from 'express';
|
||||
|
||||
export default function createRouter(configureRouter) {
|
||||
const router = express.Router();
|
||||
configureRouter(router);
|
||||
return router;
|
||||
}
|
||||
@ -1,12 +1,13 @@
|
||||
import url from 'url';
|
||||
import https from 'https';
|
||||
|
||||
import { npmRegistryURL } from '../config';
|
||||
|
||||
import debug from './debug';
|
||||
import bufferStream from './bufferStream';
|
||||
import agent from './registryAgent';
|
||||
|
||||
const npmRegistryURL =
|
||||
process.env.NPM_REGISTRY_URL || 'https://registry.npmjs.org';
|
||||
|
||||
function parseJSON(res) {
|
||||
return bufferStream(res).then(JSON.parse);
|
||||
}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import url from 'url';
|
||||
import { startOfDay, addDays } from 'date-fns';
|
||||
|
||||
import data from '../utils/data';
|
||||
import isValidPackageName from '../utils/isValidPackageName';
|
||||
import parsePackageURL from '../utils/parsePackageURL';
|
||||
import * as cloudflare from '../utils/cloudflare';
|
||||
import * as stats from '../utils/stats';
|
||||
import data from './data';
|
||||
import isValidPackageName from './isValidPackageName';
|
||||
import parsePackageURL from './parsePackageURL';
|
||||
import * as cloudflare from './cloudflare';
|
||||
import * as stats from './stats';
|
||||
|
||||
/**
|
||||
* Domains we want to analyze.
|
||||
@ -1,8 +1,9 @@
|
||||
import babel from '@babel/core';
|
||||
|
||||
import { origin } from '../config';
|
||||
import unpkgRewrite from '../plugins/unpkgRewrite';
|
||||
|
||||
const origin = process.env.ORIGIN || 'https://unpkg.com';
|
||||
|
||||
export default function rewriteBareModuleIdentifiers(code, packageConfig) {
|
||||
const dependencies = Object.assign(
|
||||
{},
|
||||
|
||||
8
modules/utils/secret.js
Normal file
8
modules/utils/secret.js
Normal file
@ -0,0 +1,8 @@
|
||||
import invariant from 'invariant';
|
||||
|
||||
const secretKey = process.env.SECRET_KEY;
|
||||
|
||||
invariant(secretKey, 'Missing $SECRET_KEY environment variable');
|
||||
|
||||
export const privateKey = secretKey.private;
|
||||
export const publicKey = secretKey.public;
|
||||
Reference in New Issue
Block a user