From 1785bac9d2fc5c471c489ef0000ed2dfd19ebfdf Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 4 Dec 2017 09:55:11 +0300 Subject: [PATCH] fix usercss updater: skip if code hasn't changed --- background/update.js | 74 +++++++++++++++++++++----------------------- manage/updater-ui.js | 4 +-- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/background/update.js b/background/update.js index e9018d55..945db534 100644 --- a/background/update.js +++ b/background/update.js @@ -56,26 +56,26 @@ var updater = { 'ignoreDigest' option is set on the second manual individual update check on the manage page. */ - const maybeUpdate = style.usercssData ? maybeUpdateUsercss : maybeUpdateUSO; - return (ignoreDigest ? Promise.resolve() : calcStyleDigest(style)) - .then(checkIfEdited) - .then(maybeUpdate) - .then(maybeValidate) + return Promise.resolve(style) + .then([calcStyleDigest][!ignoreDigest ? 0 : 'skip']) + .then([checkIfEdited][!ignoreDigest ? 0 : 'skip']) + .then([maybeUpdateUSO, maybeUpdateUsercss][style.usercssData ? 1 : 0]) .then(maybeSave) - .then(saved => { - observer(updater.UPDATED, saved); - updater.log(updater.UPDATED + ` #${saved.id} ${saved.name}`); - }) - .catch(err => { - observer(updater.SKIPPED, style, err); - err = err === 0 ? 'server unreachable' : err; - updater.log(updater.SKIPPED + ` (${err}) #${style.id} ${style.name}`); - }); + .then(reportSuccess) + .catch(reportFailure); + + function reportSuccess(saved) { + observer(updater.UPDATED, saved); + updater.log(updater.UPDATED + ` #${saved.id} ${saved.name}`); + } + + function reportFailure(err) { + observer(updater.SKIPPED, style, err); + err = err === 0 ? 'server unreachable' : err; + updater.log(updater.SKIPPED + ` (${err}) #${style.id} ${style.name}`); + } function checkIfEdited(digest) { - if (ignoreDigest) { - return; - } if (style.originalDigest && style.originalDigest !== digest) { return Promise.reject(updater.EDITED); } @@ -95,6 +95,7 @@ var updater = { } function maybeUpdateUsercss() { + // TODO: when sourceCode is > 100kB use http range request(s) for version check return download(style.updateUrl).then(text => { const json = usercss.buildMeta(text); const {usercssData: {version}} = style; @@ -104,6 +105,8 @@ var updater = { // re-install is invalid in a soft upgrade if (!ignoreDigest) { return Promise.reject(updater.SAME_VERSION); + } else if (text === style.sourceCode) { + return Promise.reject(updater.SAME_CODE); } break; case 1: @@ -114,38 +117,31 @@ var updater = { }); } - function maybeValidate(json) { - if (json.usercssData) { - // usercss is already validated while building - return json; - } - if (!styleJSONseemsValid(json)) { + function maybeSave(json = {}) { + // usercss is already validated while building + if (!json.usercssData && !styleJSONseemsValid(json)) { return Promise.reject(updater.ERROR_JSON); } - return json; - } - - function maybeSave(json) { json.id = style.id; json.updateDate = Date.now(); + json.reason = 'update'; + // keep current state + delete json.enabled; + // keep local name customizations + delete json.name; + if (styleSectionsEqual(json, style)) { - // JSONs may have different order of items even if sections are effectively equal - // so we'll update the digest anyway - // always update digest even if (save === false) + // update digest even if save === false as there might be just a space added etc. saveStyle(Object.assign(json, {reason: 'update-digest'})); return Promise.reject(updater.SAME_CODE); } else if (!style.originalDigest && !ignoreDigest) { return Promise.reject(updater.MAYBE_EDITED); } - if (!save) { - return json; - } - json.reason = 'update'; - if (json.usercssData) { - return usercssHelper.save(json); - } - json.name = null; // keep local name customizations - return saveStyle(json); + + return !save ? json : + json.usercssData + ? usercssHelper.save(json) + : saveStyle(json); } function styleJSONseemsValid(json) { diff --git a/manage/updater-ui.js b/manage/updater-ui.js index 7b5360dd..8149000e 100644 --- a/manage/updater-ui.js +++ b/manage/updater-ui.js @@ -122,9 +122,9 @@ function reportUpdateState(state, style, details) { const edited = details === BG.updater.EDITED || details === BG.updater.MAYBE_EDITED; entry.dataset.details = details; if (!details) { - details = t('updateCheckFailServerUnreachable'); + details = t('updateCheckFailServerUnreachable') + '\n' + style.updateUrl; } else if (typeof details === 'number') { - details = t('updateCheckFailBadResponseCode', [details]); + details = t('updateCheckFailBadResponseCode', [details]) + '\n' + style.updateUrl; } else if (details === BG.updater.EDITED) { details = t('updateCheckSkippedLocallyEdited') + '\n' + t('updateCheckManualUpdateHint'); } else if (details === BG.updater.MAYBE_EDITED) {