Decaffeinate: convert individual files to js

This commit is contained in:
Simon Detheridge 2019-12-16 10:52:40 +00:00
parent d70a33c4f5
commit 1d295ef7e4

View file

@ -1,147 +1,173 @@
Metrics = require "metrics-sharelatex" /*
Metrics.initialize("filestore") * decaffeinate suggestions:
express = require('express') * DS102: Remove unnecessary code created because of implicit returns
bodyParser = require "body-parser" * DS207: Consider shorter variations of null checks
logger = require('logger-sharelatex') * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
logger.initialize("filestore") */
settings = require("settings-sharelatex") const Metrics = require("metrics-sharelatex");
request = require("request") Metrics.initialize("filestore");
fileController = require("./app/js/FileController") const express = require('express');
bucketController = require("./app/js/BucketController") const bodyParser = require("body-parser");
keyBuilder = require("./app/js/KeyBuilder") let logger = require('logger-sharelatex');
healthCheckController = require("./app/js/HealthCheckController") logger.initialize("filestore");
domain = require("domain") const settings = require("settings-sharelatex");
appIsOk = true const request = require("request");
app = express() const fileController = require("./app/js/FileController");
const bucketController = require("./app/js/BucketController");
const keyBuilder = require("./app/js/KeyBuilder");
const healthCheckController = require("./app/js/HealthCheckController");
const domain = require("domain");
let appIsOk = true;
const app = express();
if settings.sentry?.dsn? if ((settings.sentry != null ? settings.sentry.dsn : undefined) != null) {
logger.initializeErrorReporting(settings.sentry.dsn) logger.initializeErrorReporting(settings.sentry.dsn);
}
Metrics.open_sockets.monitor(logger) Metrics.open_sockets.monitor(logger);
Metrics.event_loop?.monitor(logger) if (Metrics.event_loop != null) {
Metrics.memory.monitor(logger) Metrics.event_loop.monitor(logger);
}
Metrics.memory.monitor(logger);
app.use Metrics.http.monitor(logger) app.use(Metrics.http.monitor(logger));
app.use (req, res, next)-> app.use(function(req, res, next){
Metrics.inc "http-request" Metrics.inc("http-request");
next() return next();
});
app.use (req, res, next) -> app.use(function(req, res, next) {
requestDomain = domain.create() const requestDomain = domain.create();
requestDomain.add req requestDomain.add(req);
requestDomain.add res requestDomain.add(res);
requestDomain.on "error", (err)-> requestDomain.on("error", function(err){
try try {
# request a shutdown to prevent memory leaks // request a shutdown to prevent memory leaks
beginShutdown() beginShutdown();
if !res.headerSent if (!res.headerSent) {
res.send(500, "uncaught exception") res.send(500, "uncaught exception");
logger = require('logger-sharelatex') }
req = logger = require('logger-sharelatex');
body:req.body req = {
headers:req.headers body:req.body,
url:req.url headers:req.headers,
key: req.key url:req.url,
key: req.key,
statusCode: req.statusCode statusCode: req.statusCode
err = };
message: err.message err = {
stack: err.stack message: err.message,
name: err.name stack: err.stack,
type: err.type name: err.name,
type: err.type,
arguments: err.arguments arguments: err.arguments
logger.err err:err, req:req, res:res, "uncaught exception thrown on request" };
catch exception return logger.err({err, req, res}, "uncaught exception thrown on request");
logger.err err: exception, "exception in request domain handler" } catch (exception) {
requestDomain.run next return logger.err({err: exception}, "exception in request domain handler");
}
});
return requestDomain.run(next);
});
app.use (req, res, next) -> app.use(function(req, res, next) {
if not appIsOk if (!appIsOk) {
# when shutting down, close any HTTP keep-alive connections // when shutting down, close any HTTP keep-alive connections
res.set 'Connection', 'close' res.set('Connection', 'close');
next() }
return next();
});
Metrics.injectMetricsRoute(app) Metrics.injectMetricsRoute(app);
app.head "/project/:project_id/file/:file_id", keyBuilder.userFileKey, fileController.getFileHead app.head("/project/:project_id/file/:file_id", keyBuilder.userFileKey, fileController.getFileHead);
app.get "/project/:project_id/file/:file_id", keyBuilder.userFileKey, fileController.getFile app.get("/project/:project_id/file/:file_id", keyBuilder.userFileKey, fileController.getFile);
app.post "/project/:project_id/file/:file_id", keyBuilder.userFileKey, fileController.insertFile app.post("/project/:project_id/file/:file_id", keyBuilder.userFileKey, fileController.insertFile);
app.put "/project/:project_id/file/:file_id", keyBuilder.userFileKey, bodyParser.json(), fileController.copyFile app.put("/project/:project_id/file/:file_id", keyBuilder.userFileKey, bodyParser.json(), fileController.copyFile);
app.del "/project/:project_id/file/:file_id", keyBuilder.userFileKey, fileController.deleteFile app.del("/project/:project_id/file/:file_id", keyBuilder.userFileKey, fileController.deleteFile);
app.head "/template/:template_id/v/:version/:format", keyBuilder.templateFileKey, fileController.getFileHead app.head("/template/:template_id/v/:version/:format", keyBuilder.templateFileKey, fileController.getFileHead);
app.get "/template/:template_id/v/:version/:format", keyBuilder.templateFileKey, fileController.getFile app.get("/template/:template_id/v/:version/:format", keyBuilder.templateFileKey, fileController.getFile);
app.get "/template/:template_id/v/:version/:format/:sub_type", keyBuilder.templateFileKey, fileController.getFile app.get("/template/:template_id/v/:version/:format/:sub_type", keyBuilder.templateFileKey, fileController.getFile);
app.post "/template/:template_id/v/:version/:format", keyBuilder.templateFileKey, fileController.insertFile app.post("/template/:template_id/v/:version/:format", keyBuilder.templateFileKey, fileController.insertFile);
app.head "/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, fileController.getFileHead app.head("/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, fileController.getFileHead);
app.get "/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, fileController.getFile app.get("/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, fileController.getFile);
app.post "/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, fileController.insertFile app.post("/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, fileController.insertFile);
app.put "/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, bodyParser.json(), fileController.copyFile app.put("/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, bodyParser.json(), fileController.copyFile);
app.del "/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, fileController.deleteFile app.del("/project/:project_id/public/:public_file_id", keyBuilder.publicFileKey, fileController.deleteFile);
app.get "/project/:project_id/size", keyBuilder.publicProjectKey, fileController.directorySize app.get("/project/:project_id/size", keyBuilder.publicProjectKey, fileController.directorySize);
app.get "/bucket/:bucket/key/*", bucketController.getFile app.get("/bucket/:bucket/key/*", bucketController.getFile);
app.get "/heapdump", (req, res)-> app.get("/heapdump", (req, res) => require('heapdump').writeSnapshot('/tmp/' + Date.now() + '.filestore.heapsnapshot', (err, filename) => res.send(filename)));
require('heapdump').writeSnapshot '/tmp/' + Date.now() + '.filestore.heapsnapshot', (err, filename)->
res.send filename
app.post "/shutdown", (req, res)-> app.post("/shutdown", function(req, res){
appIsOk = false appIsOk = false;
res.send() return res.send();
});
app.get '/status', (req, res)-> app.get('/status', function(req, res){
if appIsOk if (appIsOk) {
res.send('filestore sharelatex up') return res.send('filestore sharelatex up');
else } else {
logger.log "app is not ok - shutting down" logger.log("app is not ok - shutting down");
res.send("server is being shut down", 500) return res.send("server is being shut down", 500);
}
});
app.get "/health_check", healthCheckController.check app.get("/health_check", healthCheckController.check);
app.get '*', (req, res)-> app.get('*', (req, res) => res.send(404));
res.send 404
beginShutdown = () -> var beginShutdown = function() {
if appIsOk if (appIsOk) {
appIsOk = false appIsOk = false;
# hard-terminate this process if graceful shutdown fails // hard-terminate this process if graceful shutdown fails
killTimer = setTimeout () -> const killTimer = setTimeout(() => process.exit(1)
process.exit 1 , 120*1000);
, 120*1000 if (typeof killTimer.unref === 'function') {
killTimer.unref?() # prevent timer from keeping process alive killTimer.unref();
server.close () -> } // prevent timer from keeping process alive
logger.log "closed all connections" server.close(function() {
Metrics.close() logger.log("closed all connections");
process.disconnect?() Metrics.close();
logger.log "server will stop accepting connections" return (typeof process.disconnect === 'function' ? process.disconnect() : undefined);
});
return logger.log("server will stop accepting connections");
}
};
port = settings.internal.filestore.port or 3009 const port = settings.internal.filestore.port || 3009;
host = "0.0.0.0" const host = "0.0.0.0";
if !module.parent # Called directly if (!module.parent) { // Called directly
server = app.listen port, host, (error) -> var server = app.listen(port, host, error => logger.info(`Filestore starting up, listening on ${host}:${port}`));
logger.info "Filestore starting up, listening on #{host}:#{port}" }
module.exports = app module.exports = app;
process.on 'SIGTERM', () -> process.on('SIGTERM', function() {
logger.log("filestore got SIGTERM, shutting down gracefully") logger.log("filestore got SIGTERM, shutting down gracefully");
beginShutdown() return beginShutdown();
});
if global.gc? if (global.gc != null) {
gcTimer = setInterval () -> let oneMinute;
global.gc() const gcTimer = setInterval(function() {
logger.log process.memoryUsage(), "global.gc" global.gc();
, 3 * oneMinute = 60 * 1000 return logger.log(process.memoryUsage(), "global.gc");
gcTimer.unref() }
, 3 * (oneMinute = 60 * 1000));
gcTimer.unref();
}