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
|
|
|
|
2019-07-25 00:55:13 +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';
|
2019-07-25 00:55:13 +00:00
|
|
|
import serveModule from './actions/serveModule.js';
|
2019-07-10 00:21:25 +00:00
|
|
|
import serveStats from './actions/serveStats.js';
|
|
|
|
|
2019-08-08 17:00:34 +00:00
|
|
|
import allowQuery from './middleware/allowQuery.js';
|
2019-07-25 00:55:13 +00:00
|
|
|
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';
|
2019-07-25 00:55:13 +00:00
|
|
|
import validateFilename from './middleware/validateFilename.js';
|
2019-08-08 22:13:19 +00:00
|
|
|
import validatePackagePathname from './middleware/validatePackagePathname.js';
|
2019-07-10 00:21:25 +00:00
|
|
|
import validatePackageName from './middleware/validatePackageName.js';
|
2019-08-08 22:13:19 +00:00
|
|
|
import validatePackageVersion from './middleware/validatePackageVersion.js';
|
2019-07-10 00:21:25 +00:00
|
|
|
|
2019-07-25 00:55:13 +00:00
|
|
|
function createApp(callback) {
|
2019-07-10 00:21:25 +00:00
|
|
|
const app = express();
|
2019-07-25 00:55:13 +00:00
|
|
|
callback(app);
|
|
|
|
return app;
|
|
|
|
}
|
2019-07-10 00:21:25 +00:00
|
|
|
|
2019-07-25 00:55:13 +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
|
|
|
|
2019-07-25 00:55:13 +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);
|
|
|
|
|
2019-07-25 00:55:13 +00:00
|
|
|
app.get('/', serveMainPage);
|
|
|
|
app.get('/api/stats', serveStats);
|
2019-07-10 00:21:25 +00:00
|
|
|
|
2019-07-25 00:55:13 +00:00
|
|
|
app.use(redirectLegacyURLs);
|
2019-07-10 00:21:25 +00:00
|
|
|
|
2019-07-25 00:55:13 +00:00
|
|
|
app.use(
|
|
|
|
'/browse',
|
|
|
|
createApp(app => {
|
|
|
|
app.enable('strict routing');
|
2019-07-10 00:21:25 +00:00
|
|
|
|
2019-07-25 00:55:13 +00:00
|
|
|
app.get(
|
|
|
|
'*/',
|
2019-08-08 16:46:12 +00:00
|
|
|
noQuery(),
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackagePathname,
|
2019-07-25 00:55:13 +00:00
|
|
|
validatePackageName,
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackageVersion,
|
2019-07-25 00:55:13 +00:00
|
|
|
serveDirectoryBrowser
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'*',
|
2019-08-08 16:46:12 +00:00
|
|
|
noQuery(),
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackagePathname,
|
2019-07-25 00:55:13 +00:00
|
|
|
validatePackageName,
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackageVersion,
|
2019-07-25 00:55:13 +00:00
|
|
|
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(
|
|
|
|
'*/',
|
2019-08-08 17:00:34 +00:00
|
|
|
allowQuery('meta'),
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackagePathname,
|
2019-07-25 00:55:13 +00:00
|
|
|
validatePackageName,
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackageVersion,
|
2019-07-25 00:55:13 +00:00
|
|
|
validateFilename,
|
|
|
|
serveDirectoryMetadata
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'*',
|
2019-08-08 17:00:34 +00:00
|
|
|
allowQuery('meta'),
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackagePathname,
|
2019-07-25 00:55:13 +00:00
|
|
|
validatePackageName,
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackageVersion,
|
2019-07-25 00:55:13 +00:00
|
|
|
validateFilename,
|
|
|
|
serveFileMetadata
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
if (req.query.meta != null) {
|
|
|
|
metadataApp(req, res);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-08 17:00:34 +00:00
|
|
|
// We need to route in this weird way because Express
|
|
|
|
// doesn't have a way to route based on query params.
|
2019-07-25 00:55:13 +00:00
|
|
|
const moduleApp = createApp(app => {
|
|
|
|
app.enable('strict routing');
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'*',
|
2019-08-08 17:00:34 +00:00
|
|
|
allowQuery('module'),
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackagePathname,
|
2019-07-25 00:55:13 +00:00
|
|
|
validatePackageName,
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackageVersion,
|
2019-07-25 00:55:13 +00:00
|
|
|
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(
|
|
|
|
'*',
|
2019-08-08 17:00:34 +00:00
|
|
|
noQuery(),
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackagePathname,
|
2019-07-25 00:55:13 +00:00
|
|
|
validatePackageName,
|
2019-08-08 22:13:19 +00:00
|
|
|
validatePackageVersion,
|
2019-07-25 00:55:13 +00:00
|
|
|
validateFilename,
|
|
|
|
findEntry,
|
|
|
|
serveFile
|
|
|
|
);
|
|
|
|
});
|
2019-07-10 00:21:25 +00:00
|
|
|
}
|