From c242a8d4c17884f4b0f3893393817e0fab0eae86 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Thu, 13 Jun 2019 09:36:19 +0100 Subject: [PATCH] Merge pull request #1853 from overleaf/bg-check-doc-hashes-staged-rollout enable sharejs hash checking GitOrigin-RevId: 2c71d239b2f5ee2bee6baf03dd54aaa8afc6fae7 --- services/web/app/views/project/editor.pug | 3 +++ services/web/public/js/libs/sharejs.js | 14 +++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/services/web/app/views/project/editor.pug b/services/web/app/views/project/editor.pug index f8c37d65e5..04db699119 100644 --- a/services/web/app/views/project/editor.pug +++ b/services/web/app/views/project/editor.pug @@ -175,6 +175,9 @@ block requirejs window.aceWorkerPath = "#{aceWorkerPath}"; window.pdfCMapsPath = "#{pdfCMapsPath}" window.uiConfig = JSON.parse('!{JSON.stringify(uiConfig).replace(/\//g, "\\/")}'); + //- enable doc hash checking for projects created after 2019-06-09 + //- this is for a staged rollout, eventually we will turn this on for all projects + window.useShareJsHash = (window.project_id > "5cfd8f700000000000000000") - if (settings.overleaf != null) script(type='text/javascript'). diff --git a/services/web/public/js/libs/sharejs.js b/services/web/public/js/libs/sharejs.js index c42bbcaba3..bed5ebab4f 100644 --- a/services/web/public/js/libs/sharejs.js +++ b/services/web/public/js/libs/sharejs.js @@ -1303,9 +1303,17 @@ define(['ace/ace','libs/sha1'], function () { this.emit('flipped_pending_to_inflight'); - if (window.sl_debugging) { - // send git hash of current snapshot when debugging - var sha1 = CryptoJS.SHA1("blob " + this.snapshot.length + "\x00" + this.snapshot).toString() + if (window.useShareJsHash || window.sl_debugging) { + var now = Date.now() + var age = this.__lastSubmitTimestamp && (now - this.__lastSubmitTimestamp) + var RECOMPUTE_HASH_INTERVAL = 5000 + // check the document hash regularly (but not if we have checked in the last 5 seconds) + var needToRecomputeHash = !this.__lastSubmitTimestamp || (age > RECOMPUTE_HASH_INTERVAL) || (age < 0) + if (needToRecomputeHash || window.sl_debugging) { + // send git hash of current snapshot + var sha1 = CryptoJS.SHA1("blob " + this.snapshot.length + "\x00" + this.snapshot).toString() + this.__lastSubmitTimestamp = now; + } } // console.log "SENDING OP TO SERVER", @inflightOp, @version