ShareJsUpdateManager.js 5.0 KB

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