fix usercss updater: skip if code hasn't changed

This commit is contained in:
tophf 2017-12-04 09:55:11 +03:00
parent 01e8c3b080
commit 1785bac9d2
2 changed files with 37 additions and 41 deletions

View File

@ -56,26 +56,26 @@ var updater = {
'ignoreDigest' option is set on the second manual individual update check on the manage page. 'ignoreDigest' option is set on the second manual individual update check on the manage page.
*/ */
const maybeUpdate = style.usercssData ? maybeUpdateUsercss : maybeUpdateUSO; return Promise.resolve(style)
return (ignoreDigest ? Promise.resolve() : calcStyleDigest(style)) .then([calcStyleDigest][!ignoreDigest ? 0 : 'skip'])
.then(checkIfEdited) .then([checkIfEdited][!ignoreDigest ? 0 : 'skip'])
.then(maybeUpdate) .then([maybeUpdateUSO, maybeUpdateUsercss][style.usercssData ? 1 : 0])
.then(maybeValidate)
.then(maybeSave) .then(maybeSave)
.then(saved => { .then(reportSuccess)
.catch(reportFailure);
function reportSuccess(saved) {
observer(updater.UPDATED, saved); observer(updater.UPDATED, saved);
updater.log(updater.UPDATED + ` #${saved.id} ${saved.name}`); updater.log(updater.UPDATED + ` #${saved.id} ${saved.name}`);
}) }
.catch(err => {
function reportFailure(err) {
observer(updater.SKIPPED, style, err); observer(updater.SKIPPED, style, err);
err = err === 0 ? 'server unreachable' : err; err = err === 0 ? 'server unreachable' : err;
updater.log(updater.SKIPPED + ` (${err}) #${style.id} ${style.name}`); updater.log(updater.SKIPPED + ` (${err}) #${style.id} ${style.name}`);
}); }
function checkIfEdited(digest) { function checkIfEdited(digest) {
if (ignoreDigest) {
return;
}
if (style.originalDigest && style.originalDigest !== digest) { if (style.originalDigest && style.originalDigest !== digest) {
return Promise.reject(updater.EDITED); return Promise.reject(updater.EDITED);
} }
@ -95,6 +95,7 @@ var updater = {
} }
function maybeUpdateUsercss() { function maybeUpdateUsercss() {
// TODO: when sourceCode is > 100kB use http range request(s) for version check
return download(style.updateUrl).then(text => { return download(style.updateUrl).then(text => {
const json = usercss.buildMeta(text); const json = usercss.buildMeta(text);
const {usercssData: {version}} = style; const {usercssData: {version}} = style;
@ -104,6 +105,8 @@ var updater = {
// re-install is invalid in a soft upgrade // re-install is invalid in a soft upgrade
if (!ignoreDigest) { if (!ignoreDigest) {
return Promise.reject(updater.SAME_VERSION); return Promise.reject(updater.SAME_VERSION);
} else if (text === style.sourceCode) {
return Promise.reject(updater.SAME_CODE);
} }
break; break;
case 1: case 1:
@ -114,38 +117,31 @@ var updater = {
}); });
} }
function maybeValidate(json) { function maybeSave(json = {}) {
if (json.usercssData) {
// usercss is already validated while building // usercss is already validated while building
return json; if (!json.usercssData && !styleJSONseemsValid(json)) {
}
if (!styleJSONseemsValid(json)) {
return Promise.reject(updater.ERROR_JSON); return Promise.reject(updater.ERROR_JSON);
} }
return json;
}
function maybeSave(json) {
json.id = style.id; json.id = style.id;
json.updateDate = Date.now(); json.updateDate = Date.now();
json.reason = 'update';
// keep current state
delete json.enabled;
// keep local name customizations
delete json.name;
if (styleSectionsEqual(json, style)) { if (styleSectionsEqual(json, style)) {
// JSONs may have different order of items even if sections are effectively equal // update digest even if save === false as there might be just a space added etc.
// so we'll update the digest anyway
// always update digest even if (save === false)
saveStyle(Object.assign(json, {reason: 'update-digest'})); saveStyle(Object.assign(json, {reason: 'update-digest'}));
return Promise.reject(updater.SAME_CODE); return Promise.reject(updater.SAME_CODE);
} else if (!style.originalDigest && !ignoreDigest) { } else if (!style.originalDigest && !ignoreDigest) {
return Promise.reject(updater.MAYBE_EDITED); return Promise.reject(updater.MAYBE_EDITED);
} }
if (!save) {
return json; return !save ? json :
} json.usercssData
json.reason = 'update'; ? usercssHelper.save(json)
if (json.usercssData) { : saveStyle(json);
return usercssHelper.save(json);
}
json.name = null; // keep local name customizations
return saveStyle(json);
} }
function styleJSONseemsValid(json) { function styleJSONseemsValid(json) {

View File

@ -122,9 +122,9 @@ function reportUpdateState(state, style, details) {
const edited = details === BG.updater.EDITED || details === BG.updater.MAYBE_EDITED; const edited = details === BG.updater.EDITED || details === BG.updater.MAYBE_EDITED;
entry.dataset.details = details; entry.dataset.details = details;
if (!details) { if (!details) {
details = t('updateCheckFailServerUnreachable'); details = t('updateCheckFailServerUnreachable') + '\n' + style.updateUrl;
} else if (typeof details === 'number') { } else if (typeof details === 'number') {
details = t('updateCheckFailBadResponseCode', [details]); details = t('updateCheckFailBadResponseCode', [details]) + '\n' + style.updateUrl;
} else if (details === BG.updater.EDITED) { } else if (details === BG.updater.EDITED) {
details = t('updateCheckSkippedLocallyEdited') + '\n' + t('updateCheckManualUpdateHint'); details = t('updateCheckSkippedLocallyEdited') + '\n' + t('updateCheckManualUpdateHint');
} else if (details === BG.updater.MAYBE_EDITED) { } else if (details === BG.updater.MAYBE_EDITED) {