unpkg/modules/createServer.js

157 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-08-03 00:34:00 +00:00
import cors from 'cors';
2019-07-10 00:21:25 +00:00
import express from 'express';
2019-08-03 00:34:00 +00:00
import morgan from 'morgan';
2019-07-10 00:21:25 +00:00
import serveDirectoryBrowser from './actions/serveDirectoryBrowser.js';
import serveDirectoryMetadata from './actions/serveDirectoryMetadata.js';
import serveFileBrowser from './actions/serveFileBrowser.js';
import serveFileMetadata from './actions/serveFileMetadata.js';
2019-07-10 00:21:25 +00:00
import serveFile from './actions/serveFile.js';
import serveMainPage from './actions/serveMainPage.js';
import serveModule from './actions/serveModule.js';
2019-07-10 00:21:25 +00:00
import serveStats from './actions/serveStats.js';
import allowQuery from './middleware/allowQuery.js';
import findEntry from './middleware/findEntry.js';
2019-08-08 16:46:12 +00:00
import noQuery from './middleware/noQuery.js';
2019-07-10 00:21:25 +00:00
import redirectLegacyURLs from './middleware/redirectLegacyURLs.js';
2019-08-03 00:34:00 +00:00
import requestLog from './middleware/requestLog.js';
import validateFilename from './middleware/validateFilename.js';
import validatePackagePathname from './middleware/validatePackagePathname.js';
2019-07-10 00:21:25 +00:00
import validatePackageName from './middleware/validatePackageName.js';
import validatePackageVersion from './middleware/validatePackageVersion.js';
2019-07-10 00:21:25 +00:00
function createApp(callback) {
2019-07-10 00:21:25 +00:00
const app = express();
callback(app);
return app;
}
2019-07-10 00:21:25 +00:00
export default function createServer() {
return createApp(app => {
app.disable('x-powered-by');
app.enable('trust proxy');
app.enable('strict routing');
2019-07-10 00:21:25 +00:00
2019-08-03 00:34:00 +00:00
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
app.use(cors());
2019-08-08 21:24:19 +00:00
app.use(express.static('public', { maxAge: '1y' }));
2019-07-10 00:21:25 +00:00
// Special startup request from App Engine
// https://cloud.google.com/appengine/docs/standard/nodejs/how-instances-are-managed
app.get('/_ah/start', (req, res) => {
res.status(200).end();
});
2019-07-10 00:21:25 +00:00
2019-08-03 00:34:00 +00:00
app.use(requestLog);
app.get('/', serveMainPage);
app.get('/api/stats', serveStats);
2019-07-10 00:21:25 +00:00
app.use(redirectLegacyURLs);
2019-07-10 00:21:25 +00:00
app.use(
'/browse',
createApp(app => {
app.enable('strict routing');
2019-07-10 00:21:25 +00:00
app.get(
'*/',
2019-08-08 16:46:12 +00:00
noQuery(),
validatePackagePathname,
validatePackageName,
validatePackageVersion,
serveDirectoryBrowser
);
app.get(
'*',
2019-08-08 16:46:12 +00:00
noQuery(),
validatePackagePathname,
validatePackageName,
validatePackageVersion,
serveFileBrowser
);
})
);
// We need to route in this weird way because Express
// doesn't have a way to route based on query params.
const metadataApp = createApp(app => {
app.enable('strict routing');
app.get(
'*/',
allowQuery('meta'),
validatePackagePathname,
validatePackageName,
validatePackageVersion,
validateFilename,
serveDirectoryMetadata
);
app.get(
'*',
allowQuery('meta'),
validatePackagePathname,
validatePackageName,
validatePackageVersion,
validateFilename,
serveFileMetadata
);
});
app.use((req, res, next) => {
if (req.query.meta != null) {
metadataApp(req, res);
} else {
next();
}
});
// We need to route in this weird way because Express
// doesn't have a way to route based on query params.
const moduleApp = createApp(app => {
app.enable('strict routing');
app.get(
'*',
allowQuery('module'),
validatePackagePathname,
validatePackageName,
validatePackageVersion,
validateFilename,
findEntry,
serveModule
);
});
app.use((req, res, next) => {
if (req.query.module != null) {
moduleApp(req, res);
} else {
next();
}
});
// Send old */ requests to the new /browse UI.
app.get('*/', (req, res) => {
res.redirect(302, '/browse' + req.url);
});
app.get(
'*',
noQuery(),
validatePackagePathname,
validatePackageName,
validatePackageVersion,
validateFilename,
findEntry,
serveFile
);
});
2019-07-10 00:21:25 +00:00
}