DiffGenerator.coffee 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ConsistencyError = (message) ->
  2. error = new Error(message)
  3. error.name = "ConsistencyError"
  4. error.__proto__ = ConsistencyError.prototype
  5. return error
  6. ConsistencyError.prototype.__proto__ = Error.prototype
  7. logger = require "logger-sharelatex"
  8. module.exports = DiffGenerator =
  9. ConsistencyError: ConsistencyError
  10. rewindUpdate: (content, update) ->
  11. for op, i in update.op by -1
  12. try
  13. content = DiffGenerator.rewindOp content, op
  14. catch e
  15. if e instanceof ConsistencyError and i = update.op.length - 1
  16. # catch known case where the last op in an array has been
  17. # merged into a later op
  18. logger.error {update, op: JSON.stringify(op)}, "marking op as broken"
  19. op.broken = true
  20. else
  21. throw e # rethrow the execption
  22. return content
  23. rewindOp: (content, op) ->
  24. if op.i?
  25. # ShareJS will accept an op where p > content.length when applied,
  26. # and it applies as though p == content.length. However, the op is
  27. # passed to us with the original p > content.length. Detect if that
  28. # is the case with this op, and shift p back appropriately to match
  29. # ShareJS if so.
  30. p = op.p
  31. max_p = content.length - op.i.length
  32. if p > max_p
  33. logger.warn {max_p, p}, "truncating position to content length"
  34. p = max_p
  35. textToBeRemoved = content.slice(p, p + op.i.length)
  36. if op.i != textToBeRemoved
  37. throw new ConsistencyError(
  38. "Inserted content, '#{op.i}', does not match text to be removed, '#{textToBeRemoved}'"
  39. )
  40. return content.slice(0, p) + content.slice(p + op.i.length)
  41. else if op.d?
  42. return content.slice(0, op.p) + op.d + content.slice(op.p)
  43. rewindUpdates: (content, updates) ->
  44. for update in updates.reverse()
  45. content = DiffGenerator.rewindUpdate(content, update)
  46. return content
  47. buildDiff: (initialContent, updates) ->
  48. diff = [ u: initialContent ]
  49. for update in updates
  50. diff = DiffGenerator.applyUpdateToDiff diff, update
  51. diff = DiffGenerator.compressDiff diff
  52. return diff
  53. compressDiff: (diff) ->
  54. newDiff = []
  55. for part in diff
  56. lastPart = newDiff[newDiff.length - 1]
  57. if lastPart? and lastPart.meta?.user? and part.meta?.user?
  58. if lastPart.i? and part.i? and lastPart.meta.user.id == part.meta.user.id
  59. lastPart.i += part.i
  60. lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts)
  61. lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts)
  62. else if lastPart.d? and part.d? and lastPart.meta.user.id == part.meta.user.id
  63. lastPart.d += part.d
  64. lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts)
  65. lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts)
  66. else
  67. newDiff.push part
  68. else
  69. newDiff.push part
  70. return newDiff
  71. applyOpToDiff: (diff, op, meta) ->
  72. position = 0
  73. remainingDiff = diff.slice()
  74. {consumedDiff, remainingDiff} = DiffGenerator._consumeToOffset(remainingDiff, op.p)
  75. newDiff = consumedDiff
  76. if op.i?
  77. newDiff.push
  78. i: op.i
  79. meta: meta
  80. else if op.d?
  81. {consumedDiff, remainingDiff} = DiffGenerator._consumeDiffAffectedByDeleteOp remainingDiff, op, meta
  82. newDiff.push(consumedDiff...)
  83. newDiff.push(remainingDiff...)
  84. return newDiff
  85. applyUpdateToDiff: (diff, update) ->
  86. for op in update.op when op.broken isnt true
  87. diff = DiffGenerator.applyOpToDiff diff, op, update.meta
  88. return diff
  89. _consumeToOffset: (remainingDiff, totalOffset) ->
  90. consumedDiff = []
  91. position = 0
  92. while part = remainingDiff.shift()
  93. length = DiffGenerator._getLengthOfDiffPart part
  94. if part.d?
  95. consumedDiff.push part
  96. else if position + length >= totalOffset
  97. partOffset = totalOffset - position
  98. if partOffset > 0
  99. consumedDiff.push DiffGenerator._slicePart part, 0, partOffset
  100. if partOffset < length
  101. remainingDiff.unshift DiffGenerator._slicePart part, partOffset
  102. break
  103. else
  104. position += length
  105. consumedDiff.push part
  106. return {
  107. consumedDiff: consumedDiff
  108. remainingDiff: remainingDiff
  109. }
  110. _consumeDiffAffectedByDeleteOp: (remainingDiff, deleteOp, meta) ->
  111. consumedDiff = []
  112. remainingOp = deleteOp
  113. while remainingOp and remainingDiff.length > 0
  114. {newPart, remainingDiff, remainingOp} = DiffGenerator._consumeDeletedPart remainingDiff, remainingOp, meta
  115. consumedDiff.push newPart if newPart?
  116. return {
  117. consumedDiff: consumedDiff
  118. remainingDiff: remainingDiff
  119. }
  120. _consumeDeletedPart: (remainingDiff, op, meta) ->
  121. part = remainingDiff.shift()
  122. partLength = DiffGenerator._getLengthOfDiffPart part
  123. if part.d?
  124. # Skip existing deletes
  125. remainingOp = op
  126. newPart = part
  127. else if partLength > op.d.length
  128. # Only the first bit of the part has been deleted
  129. remainingPart = DiffGenerator._slicePart part, op.d.length
  130. remainingDiff.unshift remainingPart
  131. deletedContent = DiffGenerator._getContentOfPart(part).slice(0, op.d.length)
  132. if deletedContent != op.d
  133. throw new ConsistencyError("deleted content, '#{deletedContent}', does not match delete op, '#{op.d}'")
  134. if part.u?
  135. newPart =
  136. d: op.d
  137. meta: meta
  138. else if part.i?
  139. newPart = null
  140. remainingOp = null
  141. else if partLength == op.d.length
  142. # The entire part has been deleted, but it is the last part
  143. deletedContent = DiffGenerator._getContentOfPart(part)
  144. if deletedContent != op.d
  145. throw new ConsistencyError("deleted content, '#{deletedContent}', does not match delete op, '#{op.d}'")
  146. if part.u?
  147. newPart =
  148. d: op.d
  149. meta: meta
  150. else if part.i?
  151. newPart = null
  152. remainingOp = null
  153. else if partLength < op.d.length
  154. # The entire part has been deleted and there is more
  155. deletedContent = DiffGenerator._getContentOfPart(part)
  156. opContent = op.d.slice(0, deletedContent.length)
  157. if deletedContent != opContent
  158. throw new ConsistencyError("deleted content, '#{deletedContent}', does not match delete op, '#{opContent}'")
  159. if part.u
  160. newPart =
  161. d: part.u
  162. meta: meta
  163. else if part.i?
  164. newPart = null
  165. remainingOp =
  166. p: op.p, d: op.d.slice(DiffGenerator._getLengthOfDiffPart(part))
  167. return {
  168. newPart: newPart
  169. remainingDiff: remainingDiff
  170. remainingOp: remainingOp
  171. }
  172. _slicePart: (basePart, from, to) ->
  173. if basePart.u?
  174. part = { u: basePart.u.slice(from, to) }
  175. else if basePart.i?
  176. part = { i: basePart.i.slice(from, to) }
  177. if basePart.meta?
  178. part.meta = basePart.meta
  179. return part
  180. _getLengthOfDiffPart: (part) ->
  181. (part.u or part.d or part.i or '').length
  182. _getContentOfPart: (part) ->
  183. part.u or part.d or part.i or ''