DiffGenerator.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. var ConsistencyError = function (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('logger-sharelatex')
  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.error(
  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. ;({
  157. consumedDiff,
  158. remainingDiff
  159. } = DiffGenerator._consumeDiffAffectedByDeleteOp(remainingDiff, op, meta))
  160. newDiff.push(...Array.from(consumedDiff || []))
  161. }
  162. newDiff.push(...Array.from(remainingDiff || []))
  163. return newDiff
  164. },
  165. applyUpdateToDiff(diff, update) {
  166. for (const op of Array.from(update.op)) {
  167. if (op.broken !== true) {
  168. diff = DiffGenerator.applyOpToDiff(diff, op, update.meta)
  169. }
  170. }
  171. return diff
  172. },
  173. _consumeToOffset(remainingDiff, totalOffset) {
  174. let part
  175. const consumedDiff = []
  176. let position = 0
  177. while ((part = remainingDiff.shift())) {
  178. const length = DiffGenerator._getLengthOfDiffPart(part)
  179. if (part.d != null) {
  180. consumedDiff.push(part)
  181. } else if (position + length >= totalOffset) {
  182. const partOffset = totalOffset - position
  183. if (partOffset > 0) {
  184. consumedDiff.push(DiffGenerator._slicePart(part, 0, partOffset))
  185. }
  186. if (partOffset < length) {
  187. remainingDiff.unshift(DiffGenerator._slicePart(part, partOffset))
  188. }
  189. break
  190. } else {
  191. position += length
  192. consumedDiff.push(part)
  193. }
  194. }
  195. return {
  196. consumedDiff,
  197. remainingDiff
  198. }
  199. },
  200. _consumeDiffAffectedByDeleteOp(remainingDiff, deleteOp, meta) {
  201. const consumedDiff = []
  202. let remainingOp = deleteOp
  203. while (remainingOp && remainingDiff.length > 0) {
  204. let newPart
  205. ;({
  206. newPart,
  207. remainingDiff,
  208. remainingOp
  209. } = DiffGenerator._consumeDeletedPart(remainingDiff, remainingOp, meta))
  210. if (newPart != null) {
  211. consumedDiff.push(newPart)
  212. }
  213. }
  214. return {
  215. consumedDiff,
  216. remainingDiff
  217. }
  218. },
  219. _consumeDeletedPart(remainingDiff, op, meta) {
  220. let deletedContent, newPart, remainingOp
  221. const part = remainingDiff.shift()
  222. const partLength = DiffGenerator._getLengthOfDiffPart(part)
  223. if (part.d != null) {
  224. // Skip existing deletes
  225. remainingOp = op
  226. newPart = part
  227. } else if (partLength > op.d.length) {
  228. // Only the first bit of the part has been deleted
  229. const remainingPart = DiffGenerator._slicePart(part, op.d.length)
  230. remainingDiff.unshift(remainingPart)
  231. deletedContent = DiffGenerator._getContentOfPart(part).slice(
  232. 0,
  233. op.d.length
  234. )
  235. if (deletedContent !== op.d) {
  236. throw new ConsistencyError(
  237. `deleted content, '${deletedContent}', does not match delete op, '${op.d}'`
  238. )
  239. }
  240. if (part.u != null) {
  241. newPart = {
  242. d: op.d,
  243. meta
  244. }
  245. } else if (part.i != null) {
  246. newPart = null
  247. }
  248. remainingOp = null
  249. } else if (partLength === op.d.length) {
  250. // The entire part has been deleted, but it is the last part
  251. deletedContent = DiffGenerator._getContentOfPart(part)
  252. if (deletedContent !== op.d) {
  253. throw new ConsistencyError(
  254. `deleted content, '${deletedContent}', does not match delete op, '${op.d}'`
  255. )
  256. }
  257. if (part.u != null) {
  258. newPart = {
  259. d: op.d,
  260. meta
  261. }
  262. } else if (part.i != null) {
  263. newPart = null
  264. }
  265. remainingOp = null
  266. } else if (partLength < op.d.length) {
  267. // The entire part has been deleted and there is more
  268. deletedContent = DiffGenerator._getContentOfPart(part)
  269. const opContent = op.d.slice(0, deletedContent.length)
  270. if (deletedContent !== opContent) {
  271. throw new ConsistencyError(
  272. `deleted content, '${deletedContent}', does not match delete op, '${opContent}'`
  273. )
  274. }
  275. if (part.u) {
  276. newPart = {
  277. d: part.u,
  278. meta
  279. }
  280. } else if (part.i != null) {
  281. newPart = null
  282. }
  283. remainingOp = {
  284. p: op.p,
  285. d: op.d.slice(DiffGenerator._getLengthOfDiffPart(part))
  286. }
  287. }
  288. return {
  289. newPart,
  290. remainingDiff,
  291. remainingOp
  292. }
  293. },
  294. _slicePart(basePart, from, to) {
  295. let part
  296. if (basePart.u != null) {
  297. part = { u: basePart.u.slice(from, to) }
  298. } else if (basePart.i != null) {
  299. part = { i: basePart.i.slice(from, to) }
  300. }
  301. if (basePart.meta != null) {
  302. part.meta = basePart.meta
  303. }
  304. return part
  305. },
  306. _getLengthOfDiffPart(part) {
  307. return (part.u || part.d || part.i || '').length
  308. },
  309. _getContentOfPart(part) {
  310. return part.u || part.d || part.i || ''
  311. }
  312. }