DiffGenerator.js 8.1 KB

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