DiffManager.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS207: Consider shorter variations of null checks
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  9. */
  10. let DiffManager
  11. const UpdatesManager = require('./UpdatesManager')
  12. const DocumentUpdaterManager = require('./DocumentUpdaterManager')
  13. const DiffGenerator = require('./DiffGenerator')
  14. const logger = require('@overleaf/logger')
  15. module.exports = DiffManager = {
  16. getLatestDocAndUpdates(projectId, docId, fromVersion, callback) {
  17. // Get updates last, since then they must be ahead and it
  18. // might be possible to rewind to the same version as the doc.
  19. if (callback == null) {
  20. callback = function () {}
  21. }
  22. return DocumentUpdaterManager.getDocument(
  23. projectId,
  24. docId,
  25. function (error, content, version) {
  26. if (error != null) {
  27. return callback(error)
  28. }
  29. if (fromVersion == null) {
  30. // If we haven't been given a version, just return lastest doc and no updates
  31. return callback(null, content, version, [])
  32. }
  33. return UpdatesManager.getDocUpdatesWithUserInfo(
  34. projectId,
  35. docId,
  36. { from: fromVersion },
  37. function (error, updates) {
  38. if (error != null) {
  39. return callback(error)
  40. }
  41. return callback(null, content, version, updates)
  42. }
  43. )
  44. }
  45. )
  46. },
  47. getDiff(projectId, docId, fromVersion, toVersion, callback) {
  48. if (callback == null) {
  49. callback = function () {}
  50. }
  51. return DiffManager.getDocumentBeforeVersion(
  52. projectId,
  53. docId,
  54. fromVersion,
  55. function (error, startingContent, updates) {
  56. let diff
  57. if (error != null) {
  58. if (error.message === 'broken-history') {
  59. return callback(null, 'history unavailable')
  60. } else {
  61. return callback(error)
  62. }
  63. }
  64. const updatesToApply = []
  65. for (const update of Array.from(updates.slice().reverse())) {
  66. if (update.v <= toVersion) {
  67. updatesToApply.push(update)
  68. }
  69. }
  70. try {
  71. diff = DiffGenerator.buildDiff(startingContent, updatesToApply)
  72. } catch (e) {
  73. return callback(e)
  74. }
  75. return callback(null, diff)
  76. }
  77. )
  78. },
  79. getDocumentBeforeVersion(projectId, docId, version, _callback) {
  80. // Whichever order we get the latest document and the latest updates,
  81. // there is potential for updates to be applied between them so that
  82. // they do not return the same 'latest' versions.
  83. // If this happens, we just retry and hopefully get them at the compatible
  84. // versions.
  85. let retry
  86. if (_callback == null) {
  87. _callback = function () {}
  88. }
  89. let retries = 3
  90. const callback = function (error, ...args) {
  91. if (error != null) {
  92. if (error.retry && retries > 0) {
  93. logger.warn(
  94. { error, projectId, docId, version, retries },
  95. 'retrying getDocumentBeforeVersion'
  96. )
  97. return retry()
  98. } else {
  99. return _callback(error)
  100. }
  101. } else {
  102. return _callback(null, ...Array.from(args))
  103. }
  104. }
  105. return (retry = function () {
  106. retries--
  107. return DiffManager._tryGetDocumentBeforeVersion(
  108. projectId,
  109. docId,
  110. version,
  111. callback
  112. )
  113. })()
  114. },
  115. _tryGetDocumentBeforeVersion(projectId, docId, version, callback) {
  116. if (callback == null) {
  117. callback = function () {}
  118. }
  119. logger.debug(
  120. { projectId, docId, version },
  121. 'getting document before version'
  122. )
  123. return DiffManager.getLatestDocAndUpdates(
  124. projectId,
  125. docId,
  126. version,
  127. function (error, content, version, updates) {
  128. let startingContent
  129. if (error != null) {
  130. return callback(error)
  131. }
  132. // bail out if we hit a broken update
  133. for (const u of Array.from(updates)) {
  134. if (u.broken) {
  135. return callback(new Error('broken-history'))
  136. }
  137. }
  138. // discard any updates which are ahead of this document version
  139. while ((updates[0] != null ? updates[0].v : undefined) >= version) {
  140. updates.shift()
  141. }
  142. const lastUpdate = updates[0]
  143. if (lastUpdate != null && lastUpdate.v !== version - 1) {
  144. error = new Error(
  145. `latest update version, ${lastUpdate.v}, does not match doc version, ${version}`
  146. )
  147. error.retry = true
  148. return callback(error)
  149. }
  150. logger.debug(
  151. {
  152. docVersion: version,
  153. lastUpdateVersion: lastUpdate != null ? lastUpdate.v : undefined,
  154. updateCount: updates.length,
  155. },
  156. 'rewinding updates'
  157. )
  158. const tryUpdates = updates.slice().reverse()
  159. try {
  160. startingContent = DiffGenerator.rewindUpdates(content, tryUpdates)
  161. // tryUpdates is reversed, and any unapplied ops are marked as broken
  162. } catch (e) {
  163. return callback(e)
  164. }
  165. return callback(null, startingContent, tryUpdates)
  166. }
  167. )
  168. },
  169. }