ShareJsUpdateManager.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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('settings-sharelatex')
  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. // only check hash when present and no other updates have been applied
  87. if (update.hash != null && incomingUpdateVersion === version) {
  88. const ourHash = ShareJsUpdateManager._computeHash(data.snapshot)
  89. if (ourHash !== update.hash) {
  90. metrics.inc('sharejs.hash-fail')
  91. return callback(new Error('Invalid hash'))
  92. } else {
  93. metrics.inc('sharejs.hash-pass', 0.001)
  94. }
  95. }
  96. const docLines = data.snapshot.split(/\r\n|\n|\r/)
  97. return callback(
  98. null,
  99. docLines,
  100. data.v,
  101. model.db.appliedOps[doc_key] || []
  102. )
  103. })
  104. })
  105. },
  106. _listenForOps(model) {
  107. return model.on('applyOp', function (doc_key, opData) {
  108. const [project_id, doc_id] = Array.from(
  109. Keys.splitProjectIdAndDocId(doc_key)
  110. )
  111. return ShareJsUpdateManager._sendOp(project_id, doc_id, opData)
  112. })
  113. },
  114. _sendOp(project_id, doc_id, op) {
  115. return RealTimeRedisManager.sendData({ project_id, doc_id, op })
  116. },
  117. _computeHash(content) {
  118. return crypto
  119. .createHash('sha1')
  120. .update('blob ' + content.length + '\x00')
  121. .update(content, 'utf8')
  122. .digest('hex')
  123. }
  124. }