2020-02-17 12:36:01 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-11-25 06:57:20 -05:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-02-17 12:36:02 -05:00
|
|
|
Metrics.initialize('track-changes')
|
2021-07-12 12:47:16 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-17 12:36:02 -05:00
|
|
|
const TrackChangesLogger = logger.initialize('track-changes').logger
|
2020-02-17 12:36:01 -05:00
|
|
|
|
|
|
|
if ((Settings.sentry != null ? Settings.sentry.dsn : undefined) != null) {
|
2020-02-17 12:36:02 -05:00
|
|
|
logger.initializeErrorReporting(Settings.sentry.dsn)
|
2020-02-17 12:36:01 -05:00
|
|
|
}
|
2015-12-07 10:52:21 -05:00
|
|
|
|
2020-02-17 12:36:01 -05:00
|
|
|
// log updates as truncated strings
|
2021-07-13 07:04:43 -04:00
|
|
|
const truncateFn = updates =>
|
2020-02-17 12:36:02 -05:00
|
|
|
JSON.parse(
|
2020-06-04 04:24:21 -04:00
|
|
|
JSON.stringify(updates, function (key, value) {
|
2020-02-17 12:36:02 -05:00
|
|
|
let len
|
|
|
|
if (typeof value === 'string' && (len = value.length) > 80) {
|
|
|
|
return (
|
|
|
|
value.substr(0, 32) +
|
|
|
|
`...(message of length ${len} truncated)...` +
|
|
|
|
value.substr(-32)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2020-02-17 12:36:01 -05:00
|
|
|
TrackChangesLogger.addSerializers({
|
2020-02-17 12:36:02 -05:00
|
|
|
rawUpdate: truncateFn,
|
|
|
|
rawUpdates: truncateFn,
|
|
|
|
newUpdates: truncateFn,
|
2021-07-13 07:04:43 -04:00
|
|
|
lastUpdate: truncateFn,
|
2020-02-17 12:36:02 -05:00
|
|
|
})
|
2016-01-26 06:28:02 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
const Path = require('path')
|
2019-01-08 11:11:56 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
Metrics.memory.monitor(logger)
|
2014-05-09 07:44:13 -04:00
|
|
|
|
2020-06-04 04:37:43 -04:00
|
|
|
const childProcess = require('child_process')
|
2015-06-04 11:24:35 -04:00
|
|
|
|
2020-09-10 07:58:06 -04:00
|
|
|
const mongodb = require('./app/js/mongodb')
|
2020-02-17 12:36:02 -05:00
|
|
|
const HttpController = require('./app/js/HttpController')
|
|
|
|
const express = require('express')
|
2020-06-04 04:24:21 -04:00
|
|
|
const bodyParser = require('body-parser')
|
2020-03-23 06:10:24 -04:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
const app = express()
|
2014-01-27 13:09:37 -05:00
|
|
|
|
2020-03-23 06:10:24 -04:00
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.use(Metrics.http.monitor(logger))
|
2014-02-26 07:11:45 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
Metrics.injectMetricsRoute(app)
|
2019-01-08 11:11:56 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.post('/project/:project_id/doc/:doc_id/flush', HttpController.flushDoc)
|
2013-08-23 09:35:13 -04:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.get('/project/:project_id/doc/:doc_id/diff', HttpController.getDiff)
|
2014-03-04 10:27:03 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.get('/project/:project_id/doc/:doc_id/check', HttpController.checkDoc)
|
2015-12-09 09:57:04 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.get('/project/:project_id/updates', HttpController.getUpdates)
|
2021-02-23 08:57:27 -05:00
|
|
|
app.get('/project/:project_id/export', HttpController.exportProject)
|
2014-03-05 10:59:40 -05:00
|
|
|
|
2021-08-16 08:09:52 -04:00
|
|
|
app.get('/project/:project_id/zip', HttpController.zipProject)
|
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.post('/project/:project_id/flush', HttpController.flushProject)
|
2014-03-21 11:57:17 -04:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.post(
|
|
|
|
'/project/:project_id/doc/:doc_id/version/:version/restore',
|
|
|
|
HttpController.restore
|
|
|
|
)
|
2014-03-10 12:58:26 -04:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.post('/project/:project_id/doc/:doc_id/push', HttpController.pushDocHistory)
|
|
|
|
app.post('/project/:project_id/doc/:doc_id/pull', HttpController.pullDocHistory)
|
2016-03-09 11:56:49 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.post('/flush/all', HttpController.flushAll)
|
|
|
|
app.post('/check/dangling', HttpController.checkDanglingUpdates)
|
2017-04-12 11:34:28 -04:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
let packWorker = null // use a single packing worker
|
2015-06-04 11:24:35 -04:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
app.post('/pack', function (req, res, next) {
|
2020-02-17 12:36:02 -05:00
|
|
|
if (packWorker != null) {
|
|
|
|
return res.send('pack already running')
|
|
|
|
} else {
|
|
|
|
logger.log('running pack')
|
2020-06-04 04:37:43 -04:00
|
|
|
packWorker = childProcess.fork(
|
|
|
|
Path.join(__dirname, '/app/js/PackWorker.js'),
|
|
|
|
[
|
|
|
|
req.query.limit || 1000,
|
|
|
|
req.query.delay || 1000,
|
2021-07-13 07:04:43 -04:00
|
|
|
req.query.timeout || 30 * 60 * 1000,
|
2020-06-04 04:37:43 -04:00
|
|
|
]
|
|
|
|
)
|
2020-06-04 04:24:21 -04:00
|
|
|
packWorker.on('exit', function (code, signal) {
|
2020-02-17 12:36:02 -05:00
|
|
|
logger.log({ code, signal }, 'history auto pack exited')
|
|
|
|
return (packWorker = null)
|
|
|
|
})
|
|
|
|
return res.send('pack started')
|
|
|
|
}
|
|
|
|
})
|
2020-02-17 12:36:01 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.get('/status', (req, res, next) => res.send('track-changes is alive'))
|
2020-02-17 12:36:01 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
app.get('/oops', function (req, res, next) {
|
2020-02-17 12:36:02 -05:00
|
|
|
throw new Error('dummy test error')
|
|
|
|
})
|
2020-02-17 12:36:01 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.get('/check_lock', HttpController.checkLock)
|
2020-02-17 12:36:01 -05:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
app.get('/health_check', HttpController.healthCheck)
|
2020-02-17 12:36:01 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
app.use(function (error, req, res, next) {
|
2020-02-17 12:36:02 -05:00
|
|
|
logger.error({ err: error, req }, 'an internal error occured')
|
2020-03-18 11:35:47 -04:00
|
|
|
return res.sendStatus(500)
|
2020-02-17 12:36:02 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
const port =
|
|
|
|
__guard__(
|
|
|
|
Settings.internal != null ? Settings.internal.trackchanges : undefined,
|
2021-07-13 07:04:43 -04:00
|
|
|
x => x.port
|
2020-02-17 12:36:02 -05:00
|
|
|
) || 3015
|
|
|
|
const host =
|
|
|
|
__guard__(
|
|
|
|
Settings.internal != null ? Settings.internal.trackchanges : undefined,
|
2021-07-13 07:04:43 -04:00
|
|
|
x1 => x1.host
|
2020-02-17 12:36:02 -05:00
|
|
|
) || 'localhost'
|
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2020-09-10 07:58:06 -04:00
|
|
|
mongodb
|
|
|
|
.waitForDb()
|
|
|
|
.then(() => {
|
|
|
|
app.listen(port, host, function (error) {
|
|
|
|
if (error != null) {
|
|
|
|
return logger.error(
|
|
|
|
{ err: error },
|
|
|
|
'could not start track-changes server'
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return logger.info(
|
|
|
|
`trackchanges starting up, listening on ${host}:${port}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-07-13 07:04:43 -04:00
|
|
|
.catch(err => {
|
2020-09-10 07:58:06 -04:00
|
|
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2020-02-17 12:36:01 -05:00
|
|
|
}
|
2018-05-24 07:02:27 -04:00
|
|
|
|
2020-02-17 12:36:02 -05:00
|
|
|
module.exports = app
|
2014-01-27 12:51:09 -05:00
|
|
|
|
2020-02-17 12:36:01 -05:00
|
|
|
function __guard__(value, transform) {
|
2020-02-17 12:36:02 -05:00
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|