DiffManager.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 DiffManager;
  9. const UpdatesManager = require("./UpdatesManager");
  10. const DocumentUpdaterManager = require("./DocumentUpdaterManager");
  11. const DiffGenerator = require("./DiffGenerator");
  12. const logger = require("logger-sharelatex");
  13. module.exports = (DiffManager = {
  14. getLatestDocAndUpdates(project_id, doc_id, fromVersion, callback) {
  15. // Get updates last, since then they must be ahead and it
  16. // might be possible to rewind to the same version as the doc.
  17. if (callback == null) { callback = function(error, content, version, updates) {}; }
  18. return DocumentUpdaterManager.getDocument(project_id, doc_id, function(error, content, version) {
  19. if (error != null) { return callback(error); }
  20. if ((fromVersion == null)) { // If we haven't been given a version, just return lastest doc and no updates
  21. return callback(null, content, version, []);
  22. }
  23. return UpdatesManager.getDocUpdatesWithUserInfo(project_id, doc_id, {from: fromVersion}, function(error, updates) {
  24. if (error != null) { return callback(error); }
  25. return callback(null, content, version, updates);
  26. });
  27. });
  28. },
  29. getDiff(project_id, doc_id, fromVersion, toVersion, callback) {
  30. if (callback == null) { callback = function(error, diff) {}; }
  31. return DiffManager.getDocumentBeforeVersion(project_id, doc_id, fromVersion, function(error, startingContent, updates) {
  32. let diff;
  33. if (error != null) {
  34. if (error.message === "broken-history") {
  35. return callback(null, "history unavailable");
  36. } else {
  37. return callback(error);
  38. }
  39. }
  40. const updatesToApply = [];
  41. for (let update of Array.from(updates.slice().reverse())) {
  42. if (update.v <= toVersion) {
  43. updatesToApply.push(update);
  44. }
  45. }
  46. try {
  47. diff = DiffGenerator.buildDiff(startingContent, updatesToApply);
  48. } catch (e) {
  49. return callback(e);
  50. }
  51. return callback(null, diff);
  52. });
  53. },
  54. getDocumentBeforeVersion(project_id, doc_id, version, _callback) {
  55. // Whichever order we get the latest document and the latest updates,
  56. // there is potential for updates to be applied between them so that
  57. // they do not return the same 'latest' versions.
  58. // If this happens, we just retry and hopefully get them at the compatible
  59. // versions.
  60. let retry;
  61. if (_callback == null) { _callback = function(error, document, rewoundUpdates) {}; }
  62. let retries = 3;
  63. const callback = function(error, ...args) {
  64. if (error != null) {
  65. if (error.retry && (retries > 0)) {
  66. logger.warn({error, project_id, doc_id, version, retries}, "retrying getDocumentBeforeVersion");
  67. return retry();
  68. } else {
  69. return _callback(error);
  70. }
  71. } else {
  72. return _callback(null, ...Array.from(args));
  73. }
  74. };
  75. return (retry = function() {
  76. retries--;
  77. return DiffManager._tryGetDocumentBeforeVersion(project_id, doc_id, version, callback);
  78. })();
  79. },
  80. _tryGetDocumentBeforeVersion(project_id, doc_id, version, callback) {
  81. if (callback == null) { callback = function(error, document, rewoundUpdates) {}; }
  82. logger.log({project_id, doc_id, version}, "getting document before version");
  83. return DiffManager.getLatestDocAndUpdates(project_id, doc_id, version, function(error, content, version, updates) {
  84. let startingContent;
  85. if (error != null) { return callback(error); }
  86. // bail out if we hit a broken update
  87. for (let u of Array.from(updates)) {
  88. if (u.broken) {
  89. return callback(new Error("broken-history"));
  90. }
  91. }
  92. // discard any updates which are ahead of this document version
  93. while ((updates[0] != null ? updates[0].v : undefined) >= version) {
  94. updates.shift();
  95. }
  96. const lastUpdate = updates[0];
  97. if ((lastUpdate != null) && (lastUpdate.v !== (version - 1))) {
  98. error = new Error(`latest update version, ${lastUpdate.v}, does not match doc version, ${version}`);
  99. error.retry = true;
  100. return callback(error);
  101. }
  102. logger.log({docVersion: version, lastUpdateVersion: (lastUpdate != null ? lastUpdate.v : undefined), updateCount: updates.length}, "rewinding updates");
  103. const tryUpdates = updates.slice().reverse();
  104. try {
  105. startingContent = DiffGenerator.rewindUpdates(content, tryUpdates);
  106. // tryUpdates is reversed, and any unapplied ops are marked as broken
  107. } catch (e) {
  108. return callback(e);
  109. }
  110. return callback(null, startingContent, tryUpdates);
  111. });
  112. }
  113. });