2014-03-01 08:31:34 -05:00
|
|
|
ConsistencyError = (message) ->
|
|
|
|
error = new Error(message)
|
|
|
|
error.name = "ConsistencyError"
|
|
|
|
error.__proto__ = ConsistencyError.prototype
|
|
|
|
return error
|
|
|
|
ConsistencyError.prototype.__proto__ = Error.prototype
|
|
|
|
|
2015-09-25 08:44:44 -04:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
2014-03-01 08:31:34 -05:00
|
|
|
module.exports = DiffGenerator =
|
|
|
|
ConsistencyError: ConsistencyError
|
|
|
|
|
|
|
|
rewindUpdate: (content, update) ->
|
2015-12-09 10:13:37 -05:00
|
|
|
for op, i in update.op by -1 when op.broken isnt true
|
2015-12-04 08:57:15 -05:00
|
|
|
try
|
|
|
|
content = DiffGenerator.rewindOp content, op
|
|
|
|
catch e
|
2015-12-09 09:57:04 -05:00
|
|
|
if e instanceof ConsistencyError and i = update.op.length - 1
|
|
|
|
# catch known case where the last op in an array has been
|
|
|
|
# merged into a later op
|
2016-09-22 06:13:26 -04:00
|
|
|
logger.error {err: e, update, op: JSON.stringify(op)}, "marking op as broken"
|
2015-12-04 08:57:15 -05:00
|
|
|
op.broken = true
|
|
|
|
else
|
|
|
|
throw e # rethrow the execption
|
2014-03-11 11:24:38 -04:00
|
|
|
return content
|
|
|
|
|
|
|
|
rewindOp: (content, op) ->
|
2014-03-01 08:31:34 -05:00
|
|
|
if op.i?
|
2015-09-25 08:44:44 -04:00
|
|
|
# ShareJS will accept an op where p > content.length when applied,
|
|
|
|
# and it applies as though p == content.length. However, the op is
|
|
|
|
# passed to us with the original p > content.length. Detect if that
|
|
|
|
# is the case with this op, and shift p back appropriately to match
|
|
|
|
# ShareJS if so.
|
|
|
|
p = op.p
|
|
|
|
max_p = content.length - op.i.length
|
|
|
|
if p > max_p
|
|
|
|
logger.warn {max_p, p}, "truncating position to content length"
|
|
|
|
p = max_p
|
|
|
|
|
|
|
|
textToBeRemoved = content.slice(p, p + op.i.length)
|
2014-03-01 08:31:34 -05:00
|
|
|
if op.i != textToBeRemoved
|
|
|
|
throw new ConsistencyError(
|
|
|
|
"Inserted content, '#{op.i}', does not match text to be removed, '#{textToBeRemoved}'"
|
|
|
|
)
|
|
|
|
|
2015-09-25 08:44:44 -04:00
|
|
|
return content.slice(0, p) + content.slice(p + op.i.length)
|
2014-03-01 08:31:34 -05:00
|
|
|
|
|
|
|
else if op.d?
|
|
|
|
return content.slice(0, op.p) + op.d + content.slice(op.p)
|
2017-01-12 04:04:50 -05:00
|
|
|
|
|
|
|
else
|
|
|
|
return content
|
2014-03-01 08:31:34 -05:00
|
|
|
|
|
|
|
rewindUpdates: (content, updates) ->
|
|
|
|
for update in updates.reverse()
|
2016-04-07 09:45:51 -04:00
|
|
|
try
|
|
|
|
content = DiffGenerator.rewindUpdate(content, update)
|
|
|
|
catch e
|
|
|
|
e.attempted_update = update # keep a record of the attempted update
|
|
|
|
throw e # rethrow the exception
|
2014-03-01 08:31:34 -05:00
|
|
|
return content
|
|
|
|
|
|
|
|
buildDiff: (initialContent, updates) ->
|
2014-03-03 12:39:59 -05:00
|
|
|
diff = [ u: initialContent ]
|
|
|
|
for update in updates
|
|
|
|
diff = DiffGenerator.applyUpdateToDiff diff, update
|
2014-03-12 09:27:18 -04:00
|
|
|
diff = DiffGenerator.compressDiff diff
|
2014-03-03 12:39:59 -05:00
|
|
|
return diff
|
2014-03-01 08:31:34 -05:00
|
|
|
|
2014-03-12 09:27:18 -04:00
|
|
|
compressDiff: (diff) ->
|
|
|
|
newDiff = []
|
|
|
|
for part in diff
|
|
|
|
lastPart = newDiff[newDiff.length - 1]
|
2014-03-12 10:04:33 -04:00
|
|
|
if lastPart? and lastPart.meta?.user? and part.meta?.user?
|
|
|
|
if lastPart.i? and part.i? and lastPart.meta.user.id == part.meta.user.id
|
|
|
|
lastPart.i += part.i
|
|
|
|
lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts)
|
|
|
|
lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts)
|
|
|
|
else if lastPart.d? and part.d? and lastPart.meta.user.id == part.meta.user.id
|
|
|
|
lastPart.d += part.d
|
|
|
|
lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts)
|
|
|
|
lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts)
|
|
|
|
else
|
|
|
|
newDiff.push part
|
2014-03-12 09:27:18 -04:00
|
|
|
else
|
|
|
|
newDiff.push part
|
|
|
|
return newDiff
|
|
|
|
|
2014-03-11 11:24:38 -04:00
|
|
|
applyOpToDiff: (diff, op, meta) ->
|
2014-03-01 08:31:34 -05:00
|
|
|
position = 0
|
|
|
|
|
|
|
|
remainingDiff = diff.slice()
|
|
|
|
{consumedDiff, remainingDiff} = DiffGenerator._consumeToOffset(remainingDiff, op.p)
|
|
|
|
newDiff = consumedDiff
|
|
|
|
|
|
|
|
if op.i?
|
|
|
|
newDiff.push
|
|
|
|
i: op.i
|
2014-03-11 11:24:38 -04:00
|
|
|
meta: meta
|
2014-03-01 08:31:34 -05:00
|
|
|
else if op.d?
|
2014-03-11 11:24:38 -04:00
|
|
|
{consumedDiff, remainingDiff} = DiffGenerator._consumeDiffAffectedByDeleteOp remainingDiff, op, meta
|
2014-03-01 08:31:34 -05:00
|
|
|
newDiff.push(consumedDiff...)
|
|
|
|
|
|
|
|
newDiff.push(remainingDiff...)
|
|
|
|
|
|
|
|
return newDiff
|
|
|
|
|
2014-03-11 11:24:38 -04:00
|
|
|
applyUpdateToDiff: (diff, update) ->
|
2015-12-04 08:57:15 -05:00
|
|
|
for op in update.op when op.broken isnt true
|
2014-03-11 11:24:38 -04:00
|
|
|
diff = DiffGenerator.applyOpToDiff diff, op, update.meta
|
|
|
|
return diff
|
2014-03-01 08:31:34 -05:00
|
|
|
|
|
|
|
_consumeToOffset: (remainingDiff, totalOffset) ->
|
|
|
|
consumedDiff = []
|
|
|
|
position = 0
|
|
|
|
while part = remainingDiff.shift()
|
|
|
|
length = DiffGenerator._getLengthOfDiffPart part
|
|
|
|
if part.d?
|
|
|
|
consumedDiff.push part
|
|
|
|
else if position + length >= totalOffset
|
|
|
|
partOffset = totalOffset - position
|
|
|
|
if partOffset > 0
|
|
|
|
consumedDiff.push DiffGenerator._slicePart part, 0, partOffset
|
|
|
|
if partOffset < length
|
|
|
|
remainingDiff.unshift DiffGenerator._slicePart part, partOffset
|
2014-03-17 10:54:02 -04:00
|
|
|
break
|
2014-03-01 08:31:34 -05:00
|
|
|
else
|
|
|
|
position += length
|
|
|
|
consumedDiff.push part
|
2014-03-17 10:54:02 -04:00
|
|
|
|
|
|
|
return {
|
|
|
|
consumedDiff: consumedDiff
|
|
|
|
remainingDiff: remainingDiff
|
|
|
|
}
|
2014-03-01 08:31:34 -05:00
|
|
|
|
2014-03-11 11:24:38 -04:00
|
|
|
_consumeDiffAffectedByDeleteOp: (remainingDiff, deleteOp, meta) ->
|
2014-03-01 08:31:34 -05:00
|
|
|
consumedDiff = []
|
2014-03-11 11:24:38 -04:00
|
|
|
remainingOp = deleteOp
|
2014-03-17 10:54:02 -04:00
|
|
|
while remainingOp and remainingDiff.length > 0
|
2014-03-11 11:24:38 -04:00
|
|
|
{newPart, remainingDiff, remainingOp} = DiffGenerator._consumeDeletedPart remainingDiff, remainingOp, meta
|
2014-03-01 08:31:34 -05:00
|
|
|
consumedDiff.push newPart if newPart?
|
|
|
|
return {
|
|
|
|
consumedDiff: consumedDiff
|
|
|
|
remainingDiff: remainingDiff
|
|
|
|
}
|
|
|
|
|
2014-03-11 11:24:38 -04:00
|
|
|
_consumeDeletedPart: (remainingDiff, op, meta) ->
|
2014-03-01 08:31:34 -05:00
|
|
|
part = remainingDiff.shift()
|
|
|
|
partLength = DiffGenerator._getLengthOfDiffPart part
|
|
|
|
|
|
|
|
if part.d?
|
|
|
|
# Skip existing deletes
|
2014-03-11 11:24:38 -04:00
|
|
|
remainingOp = op
|
2014-03-01 08:31:34 -05:00
|
|
|
newPart = part
|
|
|
|
|
|
|
|
else if partLength > op.d.length
|
|
|
|
# Only the first bit of the part has been deleted
|
|
|
|
remainingPart = DiffGenerator._slicePart part, op.d.length
|
|
|
|
remainingDiff.unshift remainingPart
|
|
|
|
|
|
|
|
deletedContent = DiffGenerator._getContentOfPart(part).slice(0, op.d.length)
|
|
|
|
if deletedContent != op.d
|
|
|
|
throw new ConsistencyError("deleted content, '#{deletedContent}', does not match delete op, '#{op.d}'")
|
|
|
|
|
|
|
|
if part.u?
|
|
|
|
newPart =
|
|
|
|
d: op.d
|
2014-03-11 11:24:38 -04:00
|
|
|
meta: meta
|
2014-03-01 08:31:34 -05:00
|
|
|
else if part.i?
|
|
|
|
newPart = null
|
|
|
|
|
2014-03-11 11:24:38 -04:00
|
|
|
remainingOp = null
|
2014-03-01 08:31:34 -05:00
|
|
|
|
|
|
|
else if partLength == op.d.length
|
|
|
|
# The entire part has been deleted, but it is the last part
|
|
|
|
|
|
|
|
deletedContent = DiffGenerator._getContentOfPart(part)
|
|
|
|
if deletedContent != op.d
|
|
|
|
throw new ConsistencyError("deleted content, '#{deletedContent}', does not match delete op, '#{op.d}'")
|
|
|
|
|
|
|
|
if part.u?
|
|
|
|
newPart =
|
|
|
|
d: op.d
|
2014-03-11 11:24:38 -04:00
|
|
|
meta: meta
|
2014-03-01 08:31:34 -05:00
|
|
|
else if part.i?
|
|
|
|
newPart = null
|
|
|
|
|
2014-03-11 11:24:38 -04:00
|
|
|
remainingOp = null
|
2014-03-01 08:31:34 -05:00
|
|
|
|
|
|
|
else if partLength < op.d.length
|
|
|
|
# The entire part has been deleted and there is more
|
|
|
|
|
|
|
|
deletedContent = DiffGenerator._getContentOfPart(part)
|
|
|
|
opContent = op.d.slice(0, deletedContent.length)
|
|
|
|
if deletedContent != opContent
|
|
|
|
throw new ConsistencyError("deleted content, '#{deletedContent}', does not match delete op, '#{opContent}'")
|
|
|
|
|
|
|
|
if part.u
|
|
|
|
newPart =
|
|
|
|
d: part.u
|
2014-03-11 11:24:38 -04:00
|
|
|
meta: meta
|
2014-03-01 08:31:34 -05:00
|
|
|
else if part.i?
|
|
|
|
newPart = null
|
|
|
|
|
2014-03-11 11:24:38 -04:00
|
|
|
remainingOp =
|
|
|
|
p: op.p, d: op.d.slice(DiffGenerator._getLengthOfDiffPart(part))
|
2014-03-01 08:31:34 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
newPart: newPart
|
|
|
|
remainingDiff: remainingDiff
|
2014-03-11 11:24:38 -04:00
|
|
|
remainingOp: remainingOp
|
2014-03-01 08:31:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_slicePart: (basePart, from, to) ->
|
|
|
|
if basePart.u?
|
|
|
|
part = { u: basePart.u.slice(from, to) }
|
|
|
|
else if basePart.i?
|
|
|
|
part = { i: basePart.i.slice(from, to) }
|
|
|
|
if basePart.meta?
|
|
|
|
part.meta = basePart.meta
|
|
|
|
return part
|
|
|
|
|
|
|
|
_getLengthOfDiffPart: (part) ->
|
2014-03-13 10:30:01 -04:00
|
|
|
(part.u or part.d or part.i or '').length
|
2014-03-01 08:31:34 -05:00
|
|
|
|
|
|
|
_getContentOfPart: (part) ->
|
2014-03-13 10:30:01 -04:00
|
|
|
part.u or part.d or part.i or ''
|