2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:09:33 -04:00
|
|
|
let ShareJsUpdateManager
|
|
|
|
const ShareJsModel = require('./sharejs/server/model')
|
|
|
|
const ShareJsDB = require('./ShareJsDB')
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2021-07-12 12:47:15 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-05-06 06:09:33 -04:00
|
|
|
const Keys = require('./UpdateKeys')
|
|
|
|
const { EventEmitter } = require('events')
|
|
|
|
const util = require('util')
|
|
|
|
const RealTimeRedisManager = require('./RealTimeRedisManager')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const metrics = require('./Metrics')
|
|
|
|
const Errors = require('./Errors')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
ShareJsModel.prototype = {}
|
|
|
|
util.inherits(ShareJsModel, EventEmitter)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const MAX_AGE_OF_OP = 80
|
2017-01-17 05:45:10 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
module.exports = ShareJsUpdateManager = {
|
2023-03-21 08:06:13 -04:00
|
|
|
getNewShareJsModel(projectId, docId, lines, version) {
|
|
|
|
const db = new ShareJsDB(projectId, docId, lines, version)
|
2020-05-06 06:09:33 -04:00
|
|
|
const model = new ShareJsModel(db, {
|
|
|
|
maxDocLength: Settings.max_doc_length,
|
2021-07-13 07:04:42 -04:00
|
|
|
maximumAge: MAX_AGE_OF_OP,
|
2020-05-06 06:09:33 -04:00
|
|
|
})
|
|
|
|
model.db = db
|
|
|
|
return model
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
applyUpdate(projectId, docId, update, lines, version, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.debug({ projectId, docId, update }, 'applying sharejs updates')
|
2020-05-06 06:09:33 -04:00
|
|
|
const jobs = []
|
|
|
|
// record the update version before it is modified
|
|
|
|
const incomingUpdateVersion = update.v
|
|
|
|
// We could use a global model for all docs, but we're hitting issues with the
|
|
|
|
// internal state of ShareJS not being accessible for clearing caches, and
|
|
|
|
// getting stuck due to queued callbacks (line 260 of sharejs/server/model.coffee)
|
|
|
|
// This adds a small but hopefully acceptable overhead (~12ms per 1000 updates on
|
|
|
|
// my 2009 MBP).
|
2023-03-21 08:06:13 -04:00
|
|
|
const model = this.getNewShareJsModel(projectId, docId, lines, version)
|
2020-05-06 06:09:33 -04:00
|
|
|
this._listenForOps(model)
|
2023-03-21 08:06:13 -04:00
|
|
|
const docKey = Keys.combineProjectIdAndDocId(projectId, docId)
|
|
|
|
return model.applyOp(docKey, update, function (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (error != null) {
|
|
|
|
if (error === 'Op already submitted') {
|
|
|
|
metrics.inc('sharejs.already-submitted')
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ projectId, docId, update },
|
2020-05-06 06:09:33 -04:00
|
|
|
'op has already been submitted'
|
|
|
|
)
|
|
|
|
update.dup = true
|
2023-03-21 08:06:13 -04:00
|
|
|
ShareJsUpdateManager._sendOp(projectId, docId, update)
|
2020-05-06 06:09:33 -04:00
|
|
|
} else if (/^Delete component/.test(error)) {
|
|
|
|
metrics.inc('sharejs.delete-mismatch')
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ projectId, docId, update, shareJsErr: error },
|
2020-05-06 06:09:33 -04:00
|
|
|
'sharejs delete does not match'
|
|
|
|
)
|
|
|
|
error = new Errors.DeleteMismatchError(
|
|
|
|
'Delete component does not match'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
} else {
|
|
|
|
metrics.inc('sharejs.other-error')
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.debug({ projectId, docId, error }, 'applied update')
|
|
|
|
return model.getSnapshot(docKey, (error, data) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-05-06 04:39:52 -04:00
|
|
|
const docSizeAfter = data.snapshot.length
|
|
|
|
if (docSizeAfter > Settings.max_doc_length) {
|
|
|
|
const docSizeBefore = lines.join('\n').length
|
|
|
|
const err = new Error(
|
|
|
|
'blocking persistence of ShareJs update: doc size exceeds limits'
|
|
|
|
)
|
|
|
|
logger.error(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ projectId, docId, err, docSizeBefore, docSizeAfter },
|
2021-05-06 04:39:52 -04:00
|
|
|
err.message
|
|
|
|
)
|
|
|
|
metrics.inc('sharejs.other-error')
|
|
|
|
const publicError = 'Update takes doc over max doc size'
|
|
|
|
return callback(publicError)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
// only check hash when present and no other updates have been applied
|
|
|
|
if (update.hash != null && incomingUpdateVersion === version) {
|
|
|
|
const ourHash = ShareJsUpdateManager._computeHash(data.snapshot)
|
|
|
|
if (ourHash !== update.hash) {
|
|
|
|
metrics.inc('sharejs.hash-fail')
|
|
|
|
return callback(new Error('Invalid hash'))
|
|
|
|
} else {
|
|
|
|
metrics.inc('sharejs.hash-pass', 0.001)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const docLines = data.snapshot.split(/\r\n|\n|\r/)
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
docLines,
|
|
|
|
data.v,
|
2023-03-21 08:06:13 -04:00
|
|
|
model.db.appliedOps[docKey] || []
|
2020-05-06 06:09:33 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
_listenForOps(model) {
|
2023-03-21 08:06:13 -04:00
|
|
|
return model.on('applyOp', function (docKey, opData) {
|
|
|
|
const [projectId, docId] = Array.from(Keys.splitProjectIdAndDocId(docKey))
|
|
|
|
return ShareJsUpdateManager._sendOp(projectId, docId, opData)
|
2020-05-06 06:09:33 -04:00
|
|
|
})
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
_sendOp(projectId, docId, op) {
|
|
|
|
return RealTimeRedisManager.sendData({
|
|
|
|
project_id: projectId,
|
|
|
|
doc_id: docId,
|
|
|
|
op,
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2019-04-08 08:43:24 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
_computeHash(content) {
|
|
|
|
return crypto
|
|
|
|
.createHash('sha1')
|
|
|
|
.update('blob ' + content.length + '\x00')
|
|
|
|
.update(content, 'utf8')
|
|
|
|
.digest('hex')
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|