DiffGenerator.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * decaffeinate suggestions:
  3. * DS101: Remove unnecessary use of Array.from
  4. * DS102: Remove unnecessary code created because of implicit returns
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. let DiffGenerator;
  9. var ConsistencyError = function(message) {
  10. const error = new Error(message);
  11. error.name = "ConsistencyError";
  12. error.__proto__ = ConsistencyError.prototype;
  13. return error;
  14. };
  15. ConsistencyError.prototype.__proto__ = Error.prototype;
  16. const logger = require("logger-sharelatex");
  17. module.exports = (DiffGenerator = {
  18. ConsistencyError,
  19. rewindUpdate(content, update) {
  20. for (let j = update.op.length - 1, i = j; j >= 0; j--, i = j) {
  21. const op = update.op[i];
  22. if (op.broken !== true) {
  23. try {
  24. content = DiffGenerator.rewindOp(content, op);
  25. } catch (e) {
  26. if (e instanceof ConsistencyError && (i = update.op.length - 1)) {
  27. // catch known case where the last op in an array has been
  28. // merged into a later op
  29. logger.error({err: e, update, op: JSON.stringify(op)}, "marking op as broken");
  30. op.broken = true;
  31. } else {
  32. throw e; // rethrow the execption
  33. }
  34. }
  35. }
  36. }
  37. return content;
  38. },
  39. rewindOp(content, op) {
  40. let p;
  41. if (op.i != null) {
  42. // ShareJS will accept an op where p > content.length when applied,
  43. // and it applies as though p == content.length. However, the op is
  44. // passed to us with the original p > content.length. Detect if that
  45. // is the case with this op, and shift p back appropriately to match
  46. // ShareJS if so.
  47. ({ p } = op);
  48. const max_p = content.length - op.i.length;
  49. if (p > max_p) {
  50. logger.warn({max_p, p}, "truncating position to content length");
  51. p = max_p;
  52. }
  53. const textToBeRemoved = content.slice(p, p + op.i.length);
  54. if (op.i !== textToBeRemoved) {
  55. throw new ConsistencyError(
  56. `Inserted content, '${op.i}', does not match text to be removed, '${textToBeRemoved}'`
  57. );
  58. }
  59. return content.slice(0, p) + content.slice(p + op.i.length);
  60. } else if (op.d != null) {
  61. return content.slice(0, op.p) + op.d + content.slice(op.p);
  62. } else {
  63. return content;
  64. }
  65. },
  66. rewindUpdates(content, updates) {
  67. for (let update of Array.from(updates.reverse())) {
  68. try {
  69. content = DiffGenerator.rewindUpdate(content, update);
  70. } catch (e) {
  71. e.attempted_update = update; // keep a record of the attempted update
  72. throw e; // rethrow the exception
  73. }
  74. }
  75. return content;
  76. },
  77. buildDiff(initialContent, updates) {
  78. let diff = [ {u: initialContent} ];
  79. for (let update of Array.from(updates)) {
  80. diff = DiffGenerator.applyUpdateToDiff(diff, update);
  81. }
  82. diff = DiffGenerator.compressDiff(diff);
  83. return diff;
  84. },
  85. compressDiff(diff) {
  86. const newDiff = [];
  87. for (let part of Array.from(diff)) {
  88. const lastPart = newDiff[newDiff.length - 1];
  89. if ((lastPart != null) && ((lastPart.meta != null ? lastPart.meta.user : undefined) != null) && ((part.meta != null ? part.meta.user : undefined) != null)) {
  90. if ((lastPart.i != null) && (part.i != null) && (lastPart.meta.user.id === part.meta.user.id)) {
  91. lastPart.i += part.i;
  92. lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts);
  93. lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts);
  94. } else if ((lastPart.d != null) && (part.d != null) && (lastPart.meta.user.id === part.meta.user.id)) {
  95. lastPart.d += part.d;
  96. lastPart.meta.start_ts = Math.min(lastPart.meta.start_ts, part.meta.start_ts);
  97. lastPart.meta.end_ts = Math.max(lastPart.meta.end_ts, part.meta.end_ts);
  98. } else {
  99. newDiff.push(part);
  100. }
  101. } else {
  102. newDiff.push(part);
  103. }
  104. }
  105. return newDiff;
  106. },
  107. applyOpToDiff(diff, op, meta) {
  108. let consumedDiff;
  109. const position = 0;
  110. let remainingDiff = diff.slice();
  111. ({consumedDiff, remainingDiff} = DiffGenerator._consumeToOffset(remainingDiff, op.p));
  112. const newDiff = consumedDiff;
  113. if (op.i != null) {
  114. newDiff.push({
  115. i: op.i,
  116. meta
  117. });
  118. } else if (op.d != null) {
  119. ({consumedDiff, remainingDiff} = DiffGenerator._consumeDiffAffectedByDeleteOp(remainingDiff, op, meta));
  120. newDiff.push(...Array.from(consumedDiff || []));
  121. }
  122. newDiff.push(...Array.from(remainingDiff || []));
  123. return newDiff;
  124. },
  125. applyUpdateToDiff(diff, update) {
  126. for (let op of Array.from(update.op)) {
  127. if (op.broken !== true) {
  128. diff = DiffGenerator.applyOpToDiff(diff, op, update.meta);
  129. }
  130. }
  131. return diff;
  132. },
  133. _consumeToOffset(remainingDiff, totalOffset) {
  134. let part;
  135. const consumedDiff = [];
  136. let position = 0;
  137. while ((part = remainingDiff.shift())) {
  138. const length = DiffGenerator._getLengthOfDiffPart(part);
  139. if (part.d != null) {
  140. consumedDiff.push(part);
  141. } else if ((position + length) >= totalOffset) {
  142. const partOffset = totalOffset - position;
  143. if (partOffset > 0) {
  144. consumedDiff.push(DiffGenerator._slicePart(part, 0, partOffset));
  145. }
  146. if (partOffset < length) {
  147. remainingDiff.unshift(DiffGenerator._slicePart(part, partOffset));
  148. }
  149. break;
  150. } else {
  151. position += length;
  152. consumedDiff.push(part);
  153. }
  154. }
  155. return {
  156. consumedDiff,
  157. remainingDiff
  158. };
  159. },
  160. _consumeDiffAffectedByDeleteOp(remainingDiff, deleteOp, meta) {
  161. const consumedDiff = [];
  162. let remainingOp = deleteOp;
  163. while (remainingOp && (remainingDiff.length > 0)) {
  164. let newPart;
  165. ({newPart, remainingDiff, remainingOp} = DiffGenerator._consumeDeletedPart(remainingDiff, remainingOp, meta));
  166. if (newPart != null) { consumedDiff.push(newPart); }
  167. }
  168. return {
  169. consumedDiff,
  170. remainingDiff
  171. };
  172. },
  173. _consumeDeletedPart(remainingDiff, op, meta) {
  174. let deletedContent, newPart, remainingOp;
  175. const part = remainingDiff.shift();
  176. const partLength = DiffGenerator._getLengthOfDiffPart(part);
  177. if (part.d != null) {
  178. // Skip existing deletes
  179. remainingOp = op;
  180. newPart = part;
  181. } else if (partLength > op.d.length) {
  182. // Only the first bit of the part has been deleted
  183. const remainingPart = DiffGenerator._slicePart(part, op.d.length);
  184. remainingDiff.unshift(remainingPart);
  185. deletedContent = DiffGenerator._getContentOfPart(part).slice(0, op.d.length);
  186. if (deletedContent !== op.d) {
  187. throw new ConsistencyError(`deleted content, '${deletedContent}', does not match delete op, '${op.d}'`);
  188. }
  189. if (part.u != null) {
  190. newPart = {
  191. d: op.d,
  192. meta
  193. };
  194. } else if (part.i != null) {
  195. newPart = null;
  196. }
  197. remainingOp = null;
  198. } else if (partLength === op.d.length) {
  199. // The entire part has been deleted, but it is the last part
  200. deletedContent = DiffGenerator._getContentOfPart(part);
  201. if (deletedContent !== op.d) {
  202. throw new ConsistencyError(`deleted content, '${deletedContent}', does not match delete op, '${op.d}'`);
  203. }
  204. if (part.u != null) {
  205. newPart = {
  206. d: op.d,
  207. meta
  208. };
  209. } else if (part.i != null) {
  210. newPart = null;
  211. }
  212. remainingOp = null;
  213. } else if (partLength < op.d.length) {
  214. // The entire part has been deleted and there is more
  215. deletedContent = DiffGenerator._getContentOfPart(part);
  216. const opContent = op.d.slice(0, deletedContent.length);
  217. if (deletedContent !== opContent) {
  218. throw new ConsistencyError(`deleted content, '${deletedContent}', does not match delete op, '${opContent}'`);
  219. }
  220. if (part.u) {
  221. newPart = {
  222. d: part.u,
  223. meta
  224. };
  225. } else if (part.i != null) {
  226. newPart = null;
  227. }
  228. remainingOp =
  229. {p: op.p, d: op.d.slice(DiffGenerator._getLengthOfDiffPart(part))};
  230. }
  231. return {
  232. newPart,
  233. remainingDiff,
  234. remainingOp
  235. };
  236. },
  237. _slicePart(basePart, from, to) {
  238. let part;
  239. if (basePart.u != null) {
  240. part = { u: basePart.u.slice(from, to) };
  241. } else if (basePart.i != null) {
  242. part = { i: basePart.i.slice(from, to) };
  243. }
  244. if (basePart.meta != null) {
  245. part.meta = basePart.meta;
  246. }
  247. return part;
  248. },
  249. _getLengthOfDiffPart(part) {
  250. return (part.u || part.d || part.i || '').length;
  251. },
  252. _getContentOfPart(part) {
  253. return part.u || part.d || part.i || '';
  254. }
  255. });