DiffGenerator.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import _ from 'lodash'
  2. import OError from '@overleaf/o-error'
  3. export class ConsistencyError extends OError {}
  4. /**
  5. * Container for functions that need to be mocked in tests
  6. *
  7. * TODO: Rewrite tests in terms of exported functions only
  8. */
  9. export const _mocks = {}
  10. export function buildDiff(initialContent, updates) {
  11. let diff = [{ u: initialContent }]
  12. for (const update of updates) {
  13. diff = applyUpdateToDiff(diff, update)
  14. }
  15. diff = compressDiff(diff)
  16. return diff
  17. }
  18. _mocks.compressDiff = diff => {
  19. const newDiff = []
  20. for (const part of diff) {
  21. const users = part.meta?.users ?? []
  22. if (part.meta?.origin?.kind === 'history-resync') {
  23. // Skip history resync updates. Inserts are converted to unchanged text
  24. // and deletes are skipped, so that they effectively don't appear in the
  25. // diff.
  26. if (part.u != null) {
  27. newDiff.push(part)
  28. } else if (part.i != null) {
  29. newDiff.push({ u: part.i })
  30. }
  31. continue
  32. }
  33. if (newDiff.length === 0) {
  34. // If we haven't seen other parts yet, we have nothing to merge.
  35. newDiff.push(part)
  36. continue
  37. }
  38. const lastPart = newDiff[newDiff.length - 1]
  39. const lastUsers = lastPart.meta?.users ?? []
  40. const usersNotInBothParts = _.xor(users, lastUsers)
  41. if (usersNotInBothParts.length > 0) {
  42. // If the set of users in the last part and this part are not the same, we
  43. // can't merge.
  44. newDiff.push(part)
  45. continue
  46. }
  47. if (lastPart.i != null && part.i != null) {
  48. // Merge two inserts
  49. lastPart.i += part.i
  50. lastPart.meta.start_ts = Math.min(
  51. lastPart.meta.start_ts,
  52. part.meta.start_ts
  53. )
  54. lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts)
  55. } else if (lastPart.d != null && part.d != null) {
  56. // Merge two deletes
  57. lastPart.d += part.d
  58. lastPart.meta.start_ts = Math.min(
  59. lastPart.meta.start_ts,
  60. part.meta.start_ts
  61. )
  62. lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts)
  63. } else {
  64. newDiff.push(part)
  65. }
  66. }
  67. return newDiff
  68. }
  69. export function compressDiff(...args) {
  70. return _mocks.compressDiff(...args)
  71. }
  72. export function applyOpToDiff(diff, op, meta) {
  73. let consumedDiff
  74. let remainingDiff = diff.slice()
  75. ;({ consumedDiff, remainingDiff } = _consumeToOffset(remainingDiff, op.p))
  76. const newDiff = consumedDiff
  77. if (op.i != null) {
  78. newDiff.push({
  79. i: op.i,
  80. meta,
  81. })
  82. } else if (op.d != null) {
  83. ;({ consumedDiff, remainingDiff } = _consumeDiffAffectedByDeleteOp(
  84. remainingDiff,
  85. op,
  86. meta
  87. ))
  88. newDiff.push(...(consumedDiff || []))
  89. }
  90. newDiff.push(...(remainingDiff || []))
  91. return newDiff
  92. }
  93. _mocks.applyUpdateToDiff = (diff, update) => {
  94. for (const op of update.op) {
  95. if (op.broken !== true) {
  96. diff = applyOpToDiff(diff, op, update.meta)
  97. }
  98. }
  99. return diff
  100. }
  101. export function applyUpdateToDiff(...args) {
  102. return _mocks.applyUpdateToDiff(...args)
  103. }
  104. function _consumeToOffset(remainingDiff, totalOffset) {
  105. let part
  106. const consumedDiff = []
  107. let position = 0
  108. while ((part = remainingDiff.shift())) {
  109. const length = _getLengthOfDiffPart(part)
  110. if (part.d != null) {
  111. consumedDiff.push(part)
  112. } else if (position + length >= totalOffset) {
  113. const partOffset = totalOffset - position
  114. if (partOffset > 0) {
  115. consumedDiff.push(_slicePart(part, 0, partOffset))
  116. }
  117. if (partOffset < length) {
  118. remainingDiff.unshift(_slicePart(part, partOffset))
  119. }
  120. break
  121. } else {
  122. position += length
  123. consumedDiff.push(part)
  124. }
  125. }
  126. return {
  127. consumedDiff,
  128. remainingDiff,
  129. }
  130. }
  131. function _consumeDiffAffectedByDeleteOp(remainingDiff, deleteOp, meta) {
  132. const consumedDiff = []
  133. let remainingOp = deleteOp
  134. while (remainingOp && remainingDiff.length > 0) {
  135. let newPart
  136. ;({ newPart, remainingDiff, remainingOp } = _consumeDeletedPart(
  137. remainingDiff,
  138. remainingOp,
  139. meta
  140. ))
  141. if (newPart != null) {
  142. consumedDiff.push(newPart)
  143. }
  144. }
  145. return {
  146. consumedDiff,
  147. remainingDiff,
  148. }
  149. }
  150. function _consumeDeletedPart(remainingDiff, op, meta) {
  151. let deletedContent, newPart, remainingOp
  152. const part = remainingDiff.shift()
  153. const partLength = _getLengthOfDiffPart(part)
  154. if (part.d != null) {
  155. // Skip existing deletes
  156. remainingOp = op
  157. newPart = part
  158. } else if (partLength > op.d.length) {
  159. // Only the first bit of the part has been deleted
  160. const remainingPart = _slicePart(part, op.d.length)
  161. remainingDiff.unshift(remainingPart)
  162. deletedContent = _getContentOfPart(part).slice(0, op.d.length)
  163. if (deletedContent !== op.d) {
  164. throw new ConsistencyError(
  165. `deleted content, '${deletedContent}', does not match delete op, '${op.d}'`
  166. )
  167. }
  168. if (part.u != null) {
  169. newPart = {
  170. d: op.d,
  171. meta,
  172. }
  173. } else if (part.i != null) {
  174. newPart = null
  175. }
  176. remainingOp = null
  177. } else if (partLength === op.d.length) {
  178. // The entire part has been deleted, but it is the last part
  179. deletedContent = _getContentOfPart(part)
  180. if (deletedContent !== op.d) {
  181. throw new ConsistencyError(
  182. `deleted content, '${deletedContent}', does not match delete op, '${op.d}'`
  183. )
  184. }
  185. if (part.u != null) {
  186. newPart = {
  187. d: op.d,
  188. meta,
  189. }
  190. } else if (part.i != null) {
  191. newPart = null
  192. }
  193. remainingOp = null
  194. } else if (partLength < op.d.length) {
  195. // The entire part has been deleted and there is more
  196. deletedContent = _getContentOfPart(part)
  197. const opContent = op.d.slice(0, deletedContent.length)
  198. if (deletedContent !== opContent) {
  199. throw new ConsistencyError(
  200. `deleted content, '${deletedContent}', does not match delete op, '${opContent}'`
  201. )
  202. }
  203. if (part.u) {
  204. newPart = {
  205. d: part.u,
  206. meta,
  207. }
  208. } else if (part.i != null) {
  209. newPart = null
  210. }
  211. remainingOp = {
  212. p: op.p,
  213. d: op.d.slice(_getLengthOfDiffPart(part)),
  214. }
  215. }
  216. return {
  217. newPart,
  218. remainingDiff,
  219. remainingOp,
  220. }
  221. }
  222. function _slicePart(basePart, from, to) {
  223. let part
  224. if (basePart.u != null) {
  225. part = { u: basePart.u.slice(from, to) }
  226. } else if (basePart.i != null) {
  227. part = { i: basePart.i.slice(from, to) }
  228. }
  229. if (basePart.meta != null) {
  230. part.meta = basePart.meta
  231. }
  232. return part
  233. }
  234. function _getLengthOfDiffPart(part) {
  235. return (part.u || part.d || part.i || '').length
  236. }
  237. function _getContentOfPart(part) {
  238. return part.u || part.d || part.i || ''
  239. }