DiffGenerator.coffee 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 when op.broken isnt true
  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 {err: e, 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. try
  46. content = DiffGenerator.rewindUpdate(content, update)
  47. catch e
  48. e.attempted_update = update # keep a record of the attempted update
  49. throw e # rethrow the exception
  50. return content
  51. buildDiff: (initialContent, updates) ->
  52. diff = [ u: initialContent ]
  53. for update in updates
  54. diff = DiffGenerator.applyUpdateToDiff diff, update
  55. diff = DiffGenerator.compressDiff diff
  56. return diff
  57. compressDiff: (diff) ->
  58. newDiff = []
  59. for part in diff
  60. lastPart = newDiff[newDiff.length - 1]
  61. if lastPart? and lastPart.meta?.user? and part.meta?.user?
  62. if lastPart.i? and part.i? and lastPart.meta.user.id == part.meta.user.id
  63. lastPart.i += part.i
  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 if lastPart.d? and part.d? and lastPart.meta.user.id == part.meta.user.id
  67. lastPart.d += part.d
  68. lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts)
  69. lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts)
  70. else
  71. newDiff.push part
  72. else
  73. newDiff.push part
  74. return newDiff
  75. applyOpToDiff: (diff, op, meta) ->
  76. position = 0
  77. remainingDiff = diff.slice()
  78. {consumedDiff, remainingDiff} = DiffGenerator._consumeToOffset(remainingDiff, op.p)
  79. newDiff = consumedDiff
  80. if op.i?
  81. newDiff.push
  82. i: op.i
  83. meta: meta
  84. else if op.d?
  85. {consumedDiff, remainingDiff} = DiffGenerator._consumeDiffAffectedByDeleteOp remainingDiff, op, meta
  86. newDiff.push(consumedDiff...)
  87. newDiff.push(remainingDiff...)
  88. return newDiff
  89. applyUpdateToDiff: (diff, update) ->
  90. for op in update.op when op.broken isnt true
  91. diff = DiffGenerator.applyOpToDiff diff, op, update.meta
  92. return diff
  93. _consumeToOffset: (remainingDiff, totalOffset) ->
  94. consumedDiff = []
  95. position = 0
  96. while part = remainingDiff.shift()
  97. length = DiffGenerator._getLengthOfDiffPart part
  98. if part.d?
  99. consumedDiff.push part
  100. else if position + length >= totalOffset
  101. partOffset = totalOffset - position
  102. if partOffset > 0
  103. consumedDiff.push DiffGenerator._slicePart part, 0, partOffset
  104. if partOffset < length
  105. remainingDiff.unshift DiffGenerator._slicePart part, partOffset
  106. break
  107. else
  108. position += length
  109. consumedDiff.push part
  110. return {
  111. consumedDiff: consumedDiff
  112. remainingDiff: remainingDiff
  113. }
  114. _consumeDiffAffectedByDeleteOp: (remainingDiff, deleteOp, meta) ->
  115. consumedDiff = []
  116. remainingOp = deleteOp
  117. while remainingOp and remainingDiff.length > 0
  118. {newPart, remainingDiff, remainingOp} = DiffGenerator._consumeDeletedPart remainingDiff, remainingOp, meta
  119. consumedDiff.push newPart if newPart?
  120. return {
  121. consumedDiff: consumedDiff
  122. remainingDiff: remainingDiff
  123. }
  124. _consumeDeletedPart: (remainingDiff, op, meta) ->
  125. part = remainingDiff.shift()
  126. partLength = DiffGenerator._getLengthOfDiffPart part
  127. if part.d?
  128. # Skip existing deletes
  129. remainingOp = op
  130. newPart = part
  131. else if partLength > op.d.length
  132. # Only the first bit of the part has been deleted
  133. remainingPart = DiffGenerator._slicePart part, op.d.length
  134. remainingDiff.unshift remainingPart
  135. deletedContent = DiffGenerator._getContentOfPart(part).slice(0, op.d.length)
  136. if deletedContent != op.d
  137. throw new ConsistencyError("deleted content, '#{deletedContent}', does not match delete op, '#{op.d}'")
  138. if part.u?
  139. newPart =
  140. d: op.d
  141. meta: meta
  142. else if part.i?
  143. newPart = null
  144. remainingOp = null
  145. else if partLength == op.d.length
  146. # The entire part has been deleted, but it is the last part
  147. deletedContent = DiffGenerator._getContentOfPart(part)
  148. if deletedContent != op.d
  149. throw new ConsistencyError("deleted content, '#{deletedContent}', does not match delete op, '#{op.d}'")
  150. if part.u?
  151. newPart =
  152. d: op.d
  153. meta: meta
  154. else if part.i?
  155. newPart = null
  156. remainingOp = null
  157. else if partLength < op.d.length
  158. # The entire part has been deleted and there is more
  159. deletedContent = DiffGenerator._getContentOfPart(part)
  160. opContent = op.d.slice(0, deletedContent.length)
  161. if deletedContent != opContent
  162. throw new ConsistencyError("deleted content, '#{deletedContent}', does not match delete op, '#{opContent}'")
  163. if part.u
  164. newPart =
  165. d: part.u
  166. meta: meta
  167. else if part.i?
  168. newPart = null
  169. remainingOp =
  170. p: op.p, d: op.d.slice(DiffGenerator._getLengthOfDiffPart(part))
  171. return {
  172. newPart: newPart
  173. remainingDiff: remainingDiff
  174. remainingOp: remainingOp
  175. }
  176. _slicePart: (basePart, from, to) ->
  177. if basePart.u?
  178. part = { u: basePart.u.slice(from, to) }
  179. else if basePart.i?
  180. part = { i: basePart.i.slice(from, to) }
  181. if basePart.meta?
  182. part.meta = basePart.meta
  183. return part
  184. _getLengthOfDiffPart: (part) ->
  185. (part.u or part.d or part.i or '').length
  186. _getContentOfPart: (part) ->
  187. part.u or part.d or part.i or ''