DiffGenerator.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* eslint-disable
  2. no-proto,
  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 DiffGenerator
  15. function ConsistencyError(message) {
  16. const error = new Error(message)
  17. error.name = 'ConsistencyError'
  18. error.__proto__ = ConsistencyError.prototype
  19. return error
  20. }
  21. ConsistencyError.prototype.__proto__ = Error.prototype
  22. const logger = require('@overleaf/logger')
  23. module.exports = DiffGenerator = {
  24. ConsistencyError,
  25. rewindUpdate(content, update) {
  26. for (let j = update.op.length - 1, i = j; j >= 0; j--, i = j) {
  27. const op = update.op[i]
  28. if (op.broken !== true) {
  29. try {
  30. content = DiffGenerator.rewindOp(content, op)
  31. } catch (e) {
  32. if (e instanceof ConsistencyError && (i = update.op.length - 1)) {
  33. // catch known case where the last op in an array has been
  34. // merged into a later op
  35. logger.warn(
  36. { err: e, update, op: JSON.stringify(op) },
  37. 'marking op as broken'
  38. )
  39. op.broken = true
  40. } else {
  41. throw e // rethrow the execption
  42. }
  43. }
  44. }
  45. }
  46. return content
  47. },
  48. rewindOp(content, op) {
  49. let p
  50. if (op.i != null) {
  51. // ShareJS will accept an op where p > content.length when applied,
  52. // and it applies as though p == content.length. However, the op is
  53. // passed to us with the original p > content.length. Detect if that
  54. // is the case with this op, and shift p back appropriately to match
  55. // ShareJS if so.
  56. ;({ p } = op)
  57. const maxP = content.length - op.i.length
  58. if (p > maxP) {
  59. logger.warn({ maxP, p }, 'truncating position to content length')
  60. p = maxP
  61. op.p = p // fix out of range offsets to avoid invalid history exports in ZipManager
  62. }
  63. const textToBeRemoved = content.slice(p, p + op.i.length)
  64. if (op.i !== textToBeRemoved) {
  65. throw new ConsistencyError(
  66. `Inserted content, '${op.i}', does not match text to be removed, '${textToBeRemoved}'`
  67. )
  68. }
  69. return content.slice(0, p) + content.slice(p + op.i.length)
  70. } else if (op.d != null) {
  71. if (op.p > content.length) {
  72. op.p = content.length // fix out of range offsets to avoid invalid history exports in ZipManager
  73. }
  74. return content.slice(0, op.p) + op.d + content.slice(op.p)
  75. } else {
  76. return content
  77. }
  78. },
  79. rewindUpdates(content, updates) {
  80. for (const update of Array.from(updates.reverse())) {
  81. try {
  82. content = DiffGenerator.rewindUpdate(content, update)
  83. } catch (e) {
  84. e.attempted_update = update // keep a record of the attempted update
  85. throw e // rethrow the exception
  86. }
  87. }
  88. return content
  89. },
  90. buildDiff(initialContent, updates) {
  91. let diff = [{ u: initialContent }]
  92. for (const update of Array.from(updates)) {
  93. diff = DiffGenerator.applyUpdateToDiff(diff, update)
  94. }
  95. diff = DiffGenerator.compressDiff(diff)
  96. return diff
  97. },
  98. compressDiff(diff) {
  99. const newDiff = []
  100. for (const part of Array.from(diff)) {
  101. const lastPart = newDiff[newDiff.length - 1]
  102. if (
  103. lastPart != null &&
  104. (lastPart.meta != null ? lastPart.meta.user : undefined) != null &&
  105. (part.meta != null ? part.meta.user : undefined) != null
  106. ) {
  107. if (
  108. lastPart.i != null &&
  109. part.i != null &&
  110. lastPart.meta.user.id === part.meta.user.id
  111. ) {
  112. lastPart.i += part.i
  113. lastPart.meta.start_ts = Math.min(
  114. lastPart.meta.start_ts,
  115. part.meta.start_ts
  116. )
  117. lastPart.meta.end_ts = Math.max(
  118. lastPart.meta.end_ts,
  119. part.meta.end_ts
  120. )
  121. } else if (
  122. lastPart.d != null &&
  123. part.d != null &&
  124. lastPart.meta.user.id === part.meta.user.id
  125. ) {
  126. lastPart.d += part.d
  127. lastPart.meta.start_ts = Math.min(
  128. lastPart.meta.start_ts,
  129. part.meta.start_ts
  130. )
  131. lastPart.meta.end_ts = Math.max(
  132. lastPart.meta.end_ts,
  133. part.meta.end_ts
  134. )
  135. } else {
  136. newDiff.push(part)
  137. }
  138. } else {
  139. newDiff.push(part)
  140. }
  141. }
  142. return newDiff
  143. },
  144. applyOpToDiff(diff, op, meta) {
  145. let consumedDiff
  146. const position = 0
  147. let remainingDiff = diff.slice()
  148. ;({ consumedDiff, remainingDiff } = DiffGenerator._consumeToOffset(
  149. remainingDiff,
  150. op.p
  151. ))
  152. const newDiff = consumedDiff
  153. if (op.i != null) {
  154. newDiff.push({
  155. i: op.i,
  156. meta,
  157. })
  158. } else if (op.d != null) {
  159. ;({ consumedDiff, remainingDiff } =
  160. DiffGenerator._consumeDiffAffectedByDeleteOp(remainingDiff, op, meta))
  161. newDiff.push(...Array.from(consumedDiff || []))
  162. }
  163. newDiff.push(...Array.from(remainingDiff || []))
  164. return newDiff
  165. },
  166. applyUpdateToDiff(diff, update) {
  167. for (const op of Array.from(update.op)) {
  168. if (op.broken !== true) {
  169. diff = DiffGenerator.applyOpToDiff(diff, op, update.meta)
  170. }
  171. }
  172. return diff
  173. },
  174. _consumeToOffset(remainingDiff, totalOffset) {
  175. let part
  176. const consumedDiff = []
  177. let position = 0
  178. while ((part = remainingDiff.shift())) {
  179. const length = DiffGenerator._getLengthOfDiffPart(part)
  180. if (part.d != null) {
  181. consumedDiff.push(part)
  182. } else if (position + length >= totalOffset) {
  183. const partOffset = totalOffset - position
  184. if (partOffset > 0) {
  185. consumedDiff.push(DiffGenerator._slicePart(part, 0, partOffset))
  186. }
  187. if (partOffset < length) {
  188. remainingDiff.unshift(DiffGenerator._slicePart(part, partOffset))
  189. }
  190. break
  191. } else {
  192. position += length
  193. consumedDiff.push(part)
  194. }
  195. }
  196. return {
  197. consumedDiff,
  198. remainingDiff,
  199. }
  200. },
  201. _consumeDiffAffectedByDeleteOp(remainingDiff, deleteOp, meta) {
  202. const consumedDiff = []
  203. let remainingOp = deleteOp
  204. while (remainingOp && remainingDiff.length > 0) {
  205. let newPart
  206. ;({ newPart, remainingDiff, remainingOp } =
  207. DiffGenerator._consumeDeletedPart(remainingDiff, remainingOp, meta))
  208. if (newPart != null) {
  209. consumedDiff.push(newPart)
  210. }
  211. }
  212. return {
  213. consumedDiff,
  214. remainingDiff,
  215. }
  216. },
  217. _consumeDeletedPart(remainingDiff, op, meta) {
  218. let deletedContent, newPart, remainingOp
  219. const part = remainingDiff.shift()
  220. const partLength = DiffGenerator._getLengthOfDiffPart(part)
  221. if (part.d != null) {
  222. // Skip existing deletes
  223. remainingOp = op
  224. newPart = part
  225. } else if (partLength > op.d.length) {
  226. // Only the first bit of the part has been deleted
  227. const remainingPart = DiffGenerator._slicePart(part, op.d.length)
  228. remainingDiff.unshift(remainingPart)
  229. deletedContent = DiffGenerator._getContentOfPart(part).slice(
  230. 0,
  231. op.d.length
  232. )
  233. if (deletedContent !== op.d) {
  234. throw new ConsistencyError(
  235. `deleted content, '${deletedContent}', does not match delete op, '${op.d}'`
  236. )
  237. }
  238. if (part.u != null) {
  239. newPart = {
  240. d: op.d,
  241. meta,
  242. }
  243. } else if (part.i != null) {
  244. newPart = null
  245. }
  246. remainingOp = null
  247. } else if (partLength === op.d.length) {
  248. // The entire part has been deleted, but it is the last part
  249. deletedContent = DiffGenerator._getContentOfPart(part)
  250. if (deletedContent !== op.d) {
  251. throw new ConsistencyError(
  252. `deleted content, '${deletedContent}', does not match delete op, '${op.d}'`
  253. )
  254. }
  255. if (part.u != null) {
  256. newPart = {
  257. d: op.d,
  258. meta,
  259. }
  260. } else if (part.i != null) {
  261. newPart = null
  262. }
  263. remainingOp = null
  264. } else if (partLength < op.d.length) {
  265. // The entire part has been deleted and there is more
  266. deletedContent = DiffGenerator._getContentOfPart(part)
  267. const opContent = op.d.slice(0, deletedContent.length)
  268. if (deletedContent !== opContent) {
  269. throw new ConsistencyError(
  270. `deleted content, '${deletedContent}', does not match delete op, '${opContent}'`
  271. )
  272. }
  273. if (part.u) {
  274. newPart = {
  275. d: part.u,
  276. meta,
  277. }
  278. } else if (part.i != null) {
  279. newPart = null
  280. }
  281. remainingOp = {
  282. p: op.p,
  283. d: op.d.slice(DiffGenerator._getLengthOfDiffPart(part)),
  284. }
  285. }
  286. return {
  287. newPart,
  288. remainingDiff,
  289. remainingOp,
  290. }
  291. },
  292. _slicePart(basePart, from, to) {
  293. let part
  294. if (basePart.u != null) {
  295. part = { u: basePart.u.slice(from, to) }
  296. } else if (basePart.i != null) {
  297. part = { i: basePart.i.slice(from, to) }
  298. }
  299. if (basePart.meta != null) {
  300. part.meta = basePart.meta
  301. }
  302. return part
  303. },
  304. _getLengthOfDiffPart(part) {
  305. return (part.u || part.d || part.i || '').length
  306. },
  307. _getContentOfPart(part) {
  308. return part.u || part.d || part.i || ''
  309. },
  310. }