DiffGenerator.js 9.1 KB

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