Keep `assetKey` and `diffName` well separated

This commit is contained in:
Raymond Hill 2023-11-13 14:55:52 -05:00
parent 7a2994c74e
commit 92c0088f64
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 20 additions and 50 deletions

View File

@ -172,27 +172,6 @@ const isDiffUpdatableAsset = content => {
data.diffPath.startsWith('%') === false; data.diffPath.startsWith('%') === false;
}; };
const assetKeyFromDiffName = diffName => {
const entry = assetCacheRegistry[diffName];
if ( entry instanceof Object ) {
if ( typeof entry.diffPath === 'string' ) {
if ( entry.diffPath.endsWith(`#${diffName}`) ) { return diffName; }
}
if ( typeof entry.diffName === 'string' ) {
if ( entry.diffName === diffName ) { return diffName; }
}
}
for ( const [ assetKey, entry ] of Object.entries(assetCacheRegistry) ) {
if ( typeof entry.diffPath === 'string' ) {
if ( entry.diffPath.endsWith(`#${diffName}`) ) { return assetKey; }
}
if ( typeof entry.diffName === 'string' ) {
if ( entry.diffName === diffName ) { return assetKey; }
}
}
return '';
};
/******************************************************************************/ /******************************************************************************/
// favorLocal: avoid making network requests whenever possible // favorLocal: avoid making network requests whenever possible
@ -1190,17 +1169,18 @@ let updaterAssetDelay = updaterAssetDelayDefault;
let updaterAuto = false; let updaterAuto = false;
const getAssetDiffDetails = assetKey => { const getAssetDiffDetails = assetKey => {
const out = { name: assetKey }; const out = { assetKey };
const cacheEntry = assetCacheRegistry[assetKey]; const cacheEntry = assetCacheRegistry[assetKey];
if ( cacheEntry === undefined ) { return; } if ( cacheEntry === undefined ) { return; }
out.patchPath = cacheEntry.diffPath; out.patchPath = cacheEntry.diffPath;
if ( out.patchPath === undefined ) { return; } if ( out.patchPath === undefined ) { return; }
out.diffName = cacheEntry.diffName; const match = /#.+$/.exec(out.patchPath);
if ( out.diffName === undefined ) { if ( match !== null ) {
const match = /#.+$/.exec(out.patchPath);
if ( match === null ) { return; }
out.diffName = match[0].slice(1); out.diffName = match[0].slice(1);
} else {
out.diffName = cacheEntry.diffName;
} }
if ( out.diffName === undefined ) { return; }
out.diffExpires = getUpdateAfterTime(assetKey, true); out.diffExpires = getUpdateAfterTime(assetKey, true);
out.lastModified = cacheEntry.lastModified; out.lastModified = cacheEntry.lastModified;
out.writeTime = cacheEntry.writeTime; out.writeTime = cacheEntry.writeTime;
@ -1246,17 +1226,15 @@ async function diffUpdater() {
const checkAndCorrectDiffPath = data => { const checkAndCorrectDiffPath = data => {
if ( typeof data.text !== 'string' ) { return; } if ( typeof data.text !== 'string' ) { return; }
if ( data.text === '' ) { return; } if ( data.text === '' ) { return; }
const assetKey = assetKeyFromDiffName(data.name);
if ( assetKey === '' ) { return; }
const metadata = extractMetadataFromList(data.text, [ 'Diff-Path' ]); const metadata = extractMetadataFromList(data.text, [ 'Diff-Path' ]);
if ( metadata instanceof Object === false ) { return; } if ( metadata instanceof Object === false ) { return; }
if ( metadata.diffPath === data.patchPath ) { return; } if ( metadata.diffPath === data.patchPath ) { return; }
assetCacheSetDetails(assetKey, metadata.diffPath); assetCacheSetDetails(data.assetKey, metadata.diffPath);
}; };
bc.onmessage = ev => { bc.onmessage = ev => {
const data = ev.data; const data = ev.data;
if ( data.what === 'ready' ) { if ( data.what === 'ready' ) {
ubolog('Diff updater: hard updating', toHardUpdate.map(v => v.name).join()); ubolog('Diff updater: hard updating', toHardUpdate.map(v => v.assetKey).join());
while ( toHardUpdate.length !== 0 ) { while ( toHardUpdate.length !== 0 ) {
const assetDetails = toHardUpdate.shift(); const assetDetails = toHardUpdate.shift();
assetDetails.fetch = true; assetDetails.fetch = true;
@ -1270,9 +1248,8 @@ async function diffUpdater() {
return; return;
} }
if ( data.status === 'needtext' ) { if ( data.status === 'needtext' ) {
ubolog('Diff updater: need text for', data.name); ubolog('Diff updater: need text for', data.assetKey);
const assetKey = assetKeyFromDiffName(data.name); assetCacheRead(data.assetKey).then(result => {
assetCacheRead(assetKey).then(result => {
data.text = result.content; data.text = result.content;
data.status = undefined; data.status = undefined;
checkAndCorrectDiffPath(data); checkAndCorrectDiffPath(data);
@ -1281,7 +1258,7 @@ async function diffUpdater() {
return; return;
} }
if ( data.status === 'updated' ) { if ( data.status === 'updated' ) {
ubolog(`Diff updater: successfully patched ${data.name} using ${data.patchURL} (${data.patchSize})`); ubolog(`Diff updater: successfully patched ${data.assetKey} using ${data.patchURL} (${data.patchSize})`);
const metadata = extractMetadataFromList(data.text, [ const metadata = extractMetadataFromList(data.text, [
'Last-Modified', 'Last-Modified',
'Expires', 'Expires',
@ -1289,30 +1266,28 @@ async function diffUpdater() {
'Diff-Path', 'Diff-Path',
'Diff-Expires', 'Diff-Expires',
]); ]);
const assetKey = assetKeyFromDiffName(data.name); assetCacheWrite(data.assetKey, {
assetCacheWrite(assetKey, {
content: data.text, content: data.text,
resourceTime: metadata.lastModified || 0, resourceTime: metadata.lastModified || 0,
}); });
assetCacheSetDetails(assetKey, metadata); assetCacheSetDetails(data.assetKey, metadata);
updaterUpdated.push(assetKey); updaterUpdated.push(data.assetKey);
} else if ( data.error ) { } else if ( data.error ) {
ubolog(`Diff updater: failed to update ${data.name} using ${data.patchPath}\n\treason: ${data.error}`); ubolog(`Diff updater: failed to update ${data.assetKey} using ${data.patchPath}\n\treason: ${data.error}`);
} else if ( data.status === 'nopatch-yet' || data.status === 'nodiff' ) { } else if ( data.status === 'nopatch-yet' || data.status === 'nodiff' ) {
ubolog(`Diff updater: skip update of ${data.name} using ${data.patchPath}\n\treason: ${data.status}`); ubolog(`Diff updater: skip update of ${data.assetKey} using ${data.patchPath}\n\treason: ${data.status}`);
const assetKey = assetKeyFromDiffName(data.name); assetCacheSetDetails(data.assetKey, { writeTime: data.writeTime });
assetCacheSetDetails(assetKey, { writeTime: data.writeTime });
vAPI.messaging.broadcast({ vAPI.messaging.broadcast({
what: 'assetUpdated', what: 'assetUpdated',
key: assetKey, key: data.assetKey,
cached: true, cached: true,
}); });
} else { } else {
ubolog(`Diff updater: ${data.name} / ${data.patchPath} / ${data.status}`); ubolog(`Diff updater: ${data.assetKey} / ${data.patchPath} / ${data.status}`);
} }
pendingOps -= 1; pendingOps -= 1;
if ( pendingOps === 0 && toSoftUpdate.length !== 0 ) { if ( pendingOps === 0 && toSoftUpdate.length !== 0 ) {
ubolog('Diff updater: soft updating', toSoftUpdate.map(v => v.name).join()); ubolog('Diff updater: soft updating', toSoftUpdate.map(v => v.assetKey).join());
while ( toSoftUpdate.length !== 0 ) { while ( toSoftUpdate.length !== 0 ) {
bc.postMessage(toSoftUpdate.shift()); bc.postMessage(toSoftUpdate.shift());
pendingOps += 1; pendingOps += 1;

View File

@ -180,11 +180,6 @@ async function applyPatchAndValidate(assetDetails, diffDetails) {
return false; return false;
} }
assetDetails.text = textAfter; assetDetails.text = textAfter;
const head = textAfter.slice(0, 1024);
let match = /^! Diff-Path: (\S+)/m.exec(head);
assetDetails.patchPath = match ? match[1] : undefined;
match = /^! Diff-Name: (\S+)/m.exec(head);
assetDetails.diffName = match ? match[1] : undefined;
return true; return true;
} }