ShareJsUpdateManager.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let ShareJsUpdateManager
  16. const ShareJsModel = require('./sharejs/server/model')
  17. const ShareJsDB = require('./ShareJsDB')
  18. const logger = require('logger-sharelatex')
  19. const Settings = require('@overleaf/settings')
  20. const Keys = require('./UpdateKeys')
  21. const { EventEmitter } = require('events')
  22. const util = require('util')
  23. const RealTimeRedisManager = require('./RealTimeRedisManager')
  24. const crypto = require('crypto')
  25. const metrics = require('./Metrics')
  26. const Errors = require('./Errors')
  27. ShareJsModel.prototype = {}
  28. util.inherits(ShareJsModel, EventEmitter)
  29. const MAX_AGE_OF_OP = 80
  30. module.exports = ShareJsUpdateManager = {
  31. getNewShareJsModel(project_id, doc_id, lines, version) {
  32. const db = new ShareJsDB(project_id, doc_id, lines, version)
  33. const model = new ShareJsModel(db, {
  34. maxDocLength: Settings.max_doc_length,
  35. maximumAge: MAX_AGE_OF_OP,
  36. })
  37. model.db = db
  38. return model
  39. },
  40. applyUpdate(project_id, doc_id, update, lines, version, callback) {
  41. if (callback == null) {
  42. callback = function (error, updatedDocLines) {}
  43. }
  44. logger.log({ project_id, doc_id, update }, 'applying sharejs updates')
  45. const jobs = []
  46. // record the update version before it is modified
  47. const incomingUpdateVersion = update.v
  48. // We could use a global model for all docs, but we're hitting issues with the
  49. // internal state of ShareJS not being accessible for clearing caches, and
  50. // getting stuck due to queued callbacks (line 260 of sharejs/server/model.coffee)
  51. // This adds a small but hopefully acceptable overhead (~12ms per 1000 updates on
  52. // my 2009 MBP).
  53. const model = this.getNewShareJsModel(project_id, doc_id, lines, version)
  54. this._listenForOps(model)
  55. const doc_key = Keys.combineProjectIdAndDocId(project_id, doc_id)
  56. return model.applyOp(doc_key, update, function (error) {
  57. if (error != null) {
  58. if (error === 'Op already submitted') {
  59. metrics.inc('sharejs.already-submitted')
  60. logger.warn(
  61. { project_id, doc_id, update },
  62. 'op has already been submitted'
  63. )
  64. update.dup = true
  65. ShareJsUpdateManager._sendOp(project_id, doc_id, update)
  66. } else if (/^Delete component/.test(error)) {
  67. metrics.inc('sharejs.delete-mismatch')
  68. logger.warn(
  69. { project_id, doc_id, update, shareJsErr: error },
  70. 'sharejs delete does not match'
  71. )
  72. error = new Errors.DeleteMismatchError(
  73. 'Delete component does not match'
  74. )
  75. return callback(error)
  76. } else {
  77. metrics.inc('sharejs.other-error')
  78. return callback(error)
  79. }
  80. }
  81. logger.log({ project_id, doc_id, error }, 'applied update')
  82. return model.getSnapshot(doc_key, (error, data) => {
  83. if (error != null) {
  84. return callback(error)
  85. }
  86. const docSizeAfter = data.snapshot.length
  87. if (docSizeAfter > Settings.max_doc_length) {
  88. const docSizeBefore = lines.join('\n').length
  89. const err = new Error(
  90. 'blocking persistence of ShareJs update: doc size exceeds limits'
  91. )
  92. logger.error(
  93. { project_id, doc_id, err, docSizeBefore, docSizeAfter },
  94. err.message
  95. )
  96. metrics.inc('sharejs.other-error')
  97. const publicError = 'Update takes doc over max doc size'
  98. return callback(publicError)
  99. }
  100. // only check hash when present and no other updates have been applied
  101. if (update.hash != null && incomingUpdateVersion === version) {
  102. const ourHash = ShareJsUpdateManager._computeHash(data.snapshot)
  103. if (ourHash !== update.hash) {
  104. metrics.inc('sharejs.hash-fail')
  105. return callback(new Error('Invalid hash'))
  106. } else {
  107. metrics.inc('sharejs.hash-pass', 0.001)
  108. }
  109. }
  110. const docLines = data.snapshot.split(/\r\n|\n|\r/)
  111. return callback(
  112. null,
  113. docLines,
  114. data.v,
  115. model.db.appliedOps[doc_key] || []
  116. )
  117. })
  118. })
  119. },
  120. _listenForOps(model) {
  121. return model.on('applyOp', function (doc_key, opData) {
  122. const [project_id, doc_id] = Array.from(
  123. Keys.splitProjectIdAndDocId(doc_key)
  124. )
  125. return ShareJsUpdateManager._sendOp(project_id, doc_id, opData)
  126. })
  127. },
  128. _sendOp(project_id, doc_id, op) {
  129. return RealTimeRedisManager.sendData({ project_id, doc_id, op })
  130. },
  131. _computeHash(content) {
  132. return crypto
  133. .createHash('sha1')
  134. .update('blob ' + content.length + '\x00')
  135. .update(content, 'utf8')
  136. .digest('hex')
  137. },
  138. }