mirror of https://github.com/gorhill/uBlock.git
Distinguish diff patch not-available from not-yet-available
A not-yet-available won't cause a list to become candidate for full update. A not-available will cause the list to be candidate for full update.
This commit is contained in:
parent
2d645cdd31
commit
abd8577096
|
@ -1184,6 +1184,8 @@ const getAssetDiffDetails = assetKey => {
|
||||||
if ( cacheEntry.diffName === undefined ) { return; }
|
if ( cacheEntry.diffName === undefined ) { return; }
|
||||||
out.diffName = cacheEntry.diffName;
|
out.diffName = cacheEntry.diffName;
|
||||||
out.patchPath = cacheEntry.diffPath;
|
out.patchPath = cacheEntry.diffPath;
|
||||||
|
out.diffExpires = getUpdateAfterTime(assetKey, true);
|
||||||
|
out.lastModified = cacheEntry.lastModified;
|
||||||
const assetEntry = assetSourceRegistry[assetKey];
|
const assetEntry = assetSourceRegistry[assetKey];
|
||||||
if ( assetEntry === undefined ) { return; }
|
if ( assetEntry === undefined ) { return; }
|
||||||
if ( Array.isArray(assetEntry.cdnURLs) === false ) { return; }
|
if ( Array.isArray(assetEntry.cdnURLs) === false ) { return; }
|
||||||
|
@ -1203,7 +1205,8 @@ async function diffUpdater() {
|
||||||
if ( assetDetails.patchPath === undefined ) { continue; }
|
if ( assetDetails.patchPath === undefined ) { continue; }
|
||||||
if ( assetDetails.diffName === undefined ) { continue; }
|
if ( assetDetails.diffName === undefined ) { continue; }
|
||||||
assetDetails.what = 'update';
|
assetDetails.what = 'update';
|
||||||
if ( (getWriteTime(assetKey) + getUpdateAfterTime(assetKey, true)) > now ) {
|
if ( (getWriteTime(assetKey) + assetDetails.diffExpires) > now ) {
|
||||||
|
assetDetails.fetch = false;
|
||||||
toSoftUpdate.push(assetDetails);
|
toSoftUpdate.push(assetDetails);
|
||||||
} else {
|
} else {
|
||||||
toHardUpdate.push(assetDetails);
|
toHardUpdate.push(assetDetails);
|
||||||
|
@ -1263,6 +1266,13 @@ async function diffUpdater() {
|
||||||
updaterUpdated.push(data.name);
|
updaterUpdated.push(data.name);
|
||||||
} else if ( data.error ) {
|
} else if ( data.error ) {
|
||||||
ubolog(`Diff updater: failed to diff-update ${data.name} using ${data.patchPath}, reason: ${data.error}`);
|
ubolog(`Diff updater: failed to diff-update ${data.name} using ${data.patchPath}, reason: ${data.error}`);
|
||||||
|
} else {
|
||||||
|
ubolog(`Diff updater: Skip diff-updating ${data.name} using ${data.patchPath}, reason: ${data.status}`);
|
||||||
|
if ( data.status === 'nopatch-yet' || data.status === 'nodiff' ) {
|
||||||
|
assetCacheSetDetails(data.name, {
|
||||||
|
writeTime: data.lastModified || 0
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pendingOps -= 1;
|
pendingOps -= 1;
|
||||||
if ( pendingOps === 0 && toSoftUpdate.length !== 0 ) {
|
if ( pendingOps === 0 && toSoftUpdate.length !== 0 ) {
|
||||||
|
|
|
@ -56,6 +56,19 @@ const resolveURL = (path, url) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const expectedTimeFromPatch = assetDetails => {
|
||||||
|
const match = /(\d+)\.(\d+)\.(\d+)\.(\d+)/.exec(assetDetails.patchPath);
|
||||||
|
if ( match === null ) { return 0; }
|
||||||
|
const date = new Date();
|
||||||
|
date.setUTCFullYear(
|
||||||
|
parseInt(match[1], 10),
|
||||||
|
parseInt(match[2], 10) - 1,
|
||||||
|
parseInt(match[3], 10)
|
||||||
|
);
|
||||||
|
date.setUTCHours(0, parseInt(match[4], 10), 0, 0);
|
||||||
|
return date.getTime() + assetDetails.diffExpires;
|
||||||
|
};
|
||||||
|
|
||||||
function parsePatch(patch) {
|
function parsePatch(patch) {
|
||||||
const patchDetails = new Map();
|
const patchDetails = new Map();
|
||||||
const diffLines = patch.split('\n');
|
const diffLines = patch.split('\n');
|
||||||
|
@ -132,6 +145,12 @@ function applyPatch(text, diff) {
|
||||||
return lines.join('\n');
|
return lines.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function hasPatchDetails(assetDetails) {
|
||||||
|
const { patchPath } = assetDetails;
|
||||||
|
const patchFile = basename(patchPath);
|
||||||
|
return patchFile !== '' && patches.has(patchFile);
|
||||||
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
// Async
|
// Async
|
||||||
|
@ -181,9 +200,7 @@ async function fetchPatchDetailsFromCDNs(assetDetails) {
|
||||||
if ( response === undefined ) { continue; }
|
if ( response === undefined ) { continue; }
|
||||||
if ( response.ok !== true ) { continue; }
|
if ( response.ok !== true ) { continue; }
|
||||||
const patchText = await response.text();
|
const patchText = await response.text();
|
||||||
if ( patchText.length === 0 ) { continue; }
|
|
||||||
const patchDetails = parsePatch(patchText);
|
const patchDetails = parsePatch(patchText);
|
||||||
if ( patchDetails === undefined ) { continue; }
|
|
||||||
return {
|
return {
|
||||||
patchURL,
|
patchURL,
|
||||||
patchSize: `${(patchText.length / 1000).toFixed(1)} KB`,
|
patchSize: `${(patchText.length / 1000).toFixed(1)} KB`,
|
||||||
|
@ -200,22 +217,40 @@ async function fetchPatchDetails(assetDetails) {
|
||||||
if ( patches.has(patchFile) ) {
|
if ( patches.has(patchFile) ) {
|
||||||
return patches.get(patchFile);
|
return patches.get(patchFile);
|
||||||
}
|
}
|
||||||
if ( assetDetails.fetch === false ) { return null; }
|
|
||||||
const patchDetailsPromise = fetchPatchDetailsFromCDNs(assetDetails);
|
const patchDetailsPromise = fetchPatchDetailsFromCDNs(assetDetails);
|
||||||
patches.set(patchFile, patchDetailsPromise);
|
patches.set(patchFile, patchDetailsPromise);
|
||||||
return patchDetailsPromise;
|
return patchDetailsPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchAndApplyAllPatches(assetDetails) {
|
async function fetchAndApplyAllPatches(assetDetails) {
|
||||||
|
if ( assetDetails.fetch === false ) {
|
||||||
|
if ( hasPatchDetails(assetDetails) === false ) {
|
||||||
|
assetDetails.status = 'nodiff';
|
||||||
|
return assetDetails;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// uBO-specific, to avoid pointless fetches which are likely to fail
|
||||||
|
// because the patch has not yet been created
|
||||||
|
const patchTime = expectedTimeFromPatch(assetDetails);
|
||||||
|
if ( patchTime > Date.now() ) {
|
||||||
|
assetDetails.status = 'nopatch-yet';
|
||||||
|
return assetDetails;
|
||||||
|
}
|
||||||
const patchData = await fetchPatchDetails(assetDetails);
|
const patchData = await fetchPatchDetails(assetDetails);
|
||||||
if ( patchData === null ) {
|
if ( patchData === null ) {
|
||||||
assetDetails.error = 'nopatch';
|
assetDetails.status = (Date.now() - patchTime) < (4 * assetDetails.diffExpires)
|
||||||
|
? 'nopatch-yet'
|
||||||
|
: 'nopatch';
|
||||||
return assetDetails;
|
return assetDetails;
|
||||||
}
|
}
|
||||||
const { patchDetails } = patchData;
|
const { patchDetails } = patchData;
|
||||||
|
if ( patchDetails instanceof Map === false ) {
|
||||||
|
assetDetails.status = 'nodiff';
|
||||||
|
return assetDetails;
|
||||||
|
}
|
||||||
const diffDetails = patchDetails.get(assetDetails.diffName);
|
const diffDetails = patchDetails.get(assetDetails.diffName);
|
||||||
if ( diffDetails === undefined ) {
|
if ( diffDetails === undefined ) {
|
||||||
assetDetails.error = 'nodiff';
|
assetDetails.status = 'nodiff';
|
||||||
return assetDetails;
|
return assetDetails;
|
||||||
}
|
}
|
||||||
if ( assetDetails.text === undefined ) {
|
if ( assetDetails.text === undefined ) {
|
||||||
|
|
Loading…
Reference in New Issue