diff options
author | David Mehren | 2021-02-16 20:15:15 +0100 |
---|---|---|
committer | GitHub | 2021-02-16 20:15:15 +0100 |
commit | 59819be34c2428450528362a5bbb74b6424d15d2 (patch) | |
tree | ad80595f5dd94a7ac63bc1481943ae7b356ddb17 /lib/workers/dmpWorker.js | |
parent | 6b8fa94402539168a5bd574ddcc17eeae4910bd0 (diff) | |
parent | 136d895d155f28c2e75b3af206549acaa2a354ed (diff) |
Merge pull request #872 from hedgedoc/renovate/master-major-linters
Diffstat (limited to '')
-rw-r--r-- | lib/workers/dmpWorker.js | 86 |
1 files changed, 50 insertions, 36 deletions
diff --git a/lib/workers/dmpWorker.js b/lib/workers/dmpWorker.js index ca68b4ab..7b5439c7 100644 --- a/lib/workers/dmpWorker.js +++ b/lib/workers/dmpWorker.js @@ -1,10 +1,10 @@ 'use strict' // external modules -var DiffMatchPatch = require('diff-match-patch') -var dmp = new DiffMatchPatch() +const DiffMatchPatch = require('diff-match-patch') +const dmp = new DiffMatchPatch() // core -var logger = require('../logger') +const logger = require('../logger') process.on('message', function (data) { if (!data || !data.msg || !data.cacheKey) { @@ -12,11 +12,16 @@ process.on('message', function (data) { } switch (data.msg) { case 'create patch': - if (!data.hasOwnProperty('lastDoc') || !data.hasOwnProperty('currDoc')) { - return logger.error('dmp worker error: not enough data on create patch') + if ( + !Object.prototype.hasOwnProperty.call(data, 'lastDoc') || + !Object.prototype.hasOwnProperty.call(data, 'currDoc') + ) { + return logger.error( + 'dmp worker error: not enough data on create patch' + ) } try { - var patch = createPatch(data.lastDoc, data.currDoc) + const patch = createPatch(data.lastDoc, data.currDoc) process.send({ msg: 'check', result: patch, @@ -32,11 +37,16 @@ process.on('message', function (data) { } break case 'get revision': - if (!data.hasOwnProperty('revisions') || !data.hasOwnProperty('count')) { - return logger.error('dmp worker error: not enough data on get revision') + if ( + !Object.prototype.hasOwnProperty.call(data, 'revisions') || + !Object.prototype.hasOwnProperty.call(data, 'count') + ) { + return logger.error( + 'dmp worker error: not enough data on get revision' + ) } try { - var result = getRevision(data.revisions, data.count) + const result = getRevision(data.revisions, data.count) process.send({ msg: 'check', result: result, @@ -55,31 +65,31 @@ process.on('message', function (data) { }) function createPatch (lastDoc, currDoc) { - var msStart = (new Date()).getTime() - var diff = dmp.diff_main(lastDoc, currDoc) - var patch = dmp.patch_make(lastDoc, diff) + const msStart = new Date().getTime() + const diff = dmp.diff_main(lastDoc, currDoc) + let patch = dmp.patch_make(lastDoc, diff) patch = dmp.patch_toText(patch) - var msEnd = (new Date()).getTime() + const msEnd = new Date().getTime() logger.debug(patch) - logger.debug((msEnd - msStart) + 'ms') + logger.debug(msEnd - msStart + 'ms') return patch } function getRevision (revisions, count) { - var msStart = (new Date()).getTime() - var startContent = null - var lastPatch = [] - var applyPatches = [] - var authorship = [] + const msStart = new Date().getTime() + let startContent = null + let lastPatch = [] + let applyPatches = [] + let authorship = [] if (count <= Math.round(revisions.length / 2)) { // start from top to target for (let i = 0; i < count; i++) { - let revision = revisions[i] + const revision = revisions[i] if (i === 0) { startContent = revision.content || revision.lastContent } if (i !== count - 1) { - let patch = dmp.patch_fromText(revision.patch) + const patch = dmp.patch_fromText(revision.patch) applyPatches = applyPatches.concat(patch) } lastPatch = revision.patch @@ -88,21 +98,25 @@ function getRevision (revisions, count) { // swap DIFF_INSERT and DIFF_DELETE to achieve unpatching for (let i = 0, l = applyPatches.length; i < l; i++) { for (let j = 0, m = applyPatches[i].diffs.length; j < m; j++) { - var diff = applyPatches[i].diffs[j] - if (diff[0] === DiffMatchPatch.DIFF_INSERT) { diff[0] = DiffMatchPatch.DIFF_DELETE } else if (diff[0] === DiffMatchPatch.DIFF_DELETE) { diff[0] = DiffMatchPatch.DIFF_INSERT } + const diff = applyPatches[i].diffs[j] + if (diff[0] === DiffMatchPatch.DIFF_INSERT) { + diff[0] = DiffMatchPatch.DIFF_DELETE + } else if (diff[0] === DiffMatchPatch.DIFF_DELETE) { + diff[0] = DiffMatchPatch.DIFF_INSERT + } } } } else { // start from bottom to target - var l = revisions.length - 1 - for (var i = l; i >= count - 1; i--) { - let revision = revisions[i] + const l = revisions.length - 1 + for (let i = l; i >= count - 1; i--) { + const revision = revisions[i] if (i === l) { startContent = revision.lastContent authorship = revision.authorship } if (revision.patch) { - let patch = dmp.patch_fromText(revision.patch) + const patch = dmp.patch_fromText(revision.patch) applyPatches = applyPatches.concat(patch) } lastPatch = revision.patch @@ -110,18 +124,18 @@ function getRevision (revisions, count) { } } try { - var finalContent = dmp.patch_apply(applyPatches, startContent)[0] + const finalContent = dmp.patch_apply(applyPatches, startContent)[0] + const data = { + content: finalContent, + patch: dmp.patch_fromText(lastPatch), + authorship: authorship + } + const msEnd = new Date().getTime() + logger.debug(msEnd - msStart + 'ms') + return data } catch (err) { throw new Error(err) } - var data = { - content: finalContent, - patch: dmp.patch_fromText(lastPatch), - authorship: authorship - } - var msEnd = (new Date()).getTime() - logger.debug((msEnd - msStart) + 'ms') - return data } // log uncaught exception |