diff options
Diffstat (limited to 'src/js/assets.js')
-rw-r--r-- | src/js/assets.js | 183 |
1 files changed, 90 insertions, 93 deletions
diff --git a/src/js/assets.js b/src/js/assets.js index 69c2ef3..e1bc4e6 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -53,10 +53,13 @@ let remoteServerFriendly = false; const stringIsNotEmpty = s => typeof s === 'string' && s !== ''; const parseExpires = s => { - const matches = s.match(/(\d+)\s*([dhm]?)/i); + const matches = s.match(/(\d+)\s*([wdhm]?)/i); if ( matches === null ) { return; } let updateAfter = parseInt(matches[1], 10); - if ( matches[2] === 'h' ) { + if ( updateAfter === 0 ) { return; } + if ( matches[2] === 'w' ) { + updateAfter *= 7 * 24; + } else if ( matches[2] === 'h' ) { updateAfter = Math.max(updateAfter, 4) / 24; } else if ( matches[2] === 'm' ) { updateAfter = Math.max(updateAfter, 240) / 1440; @@ -428,7 +431,7 @@ assets.fetchFilterList = async function(mainlistURL) { continue; } if ( result instanceof Object === false ) { continue; } - const content = result.content; + const content = result.content.trimEnd() + '\n'; const slices = sfp.utils.preparser.splitter( content, vAPI.webextFlavor.env @@ -500,7 +503,7 @@ assets.fetchFilterList = async function(mainlistURL) { resourceTime, content: allParts.length === 1 ? allParts[0] - : allParts.join('') + '\n' + : allParts.join('') }; }; @@ -525,12 +528,12 @@ function getAssetSourceRegistry() { assetSourceRegistryPromise = cacheStorage.get( 'assetSourceRegistry' ).then(bin => { - if ( - bin instanceof Object && - bin.assetSourceRegistry instanceof Object - ) { - assetSourceRegistry = bin.assetSourceRegistry; - return assetSourceRegistry; + if ( bin instanceof Object ) { + if ( bin.assetSourceRegistry instanceof Object ) { + assetSourceRegistry = bin.assetSourceRegistry; + ubolog('Loaded assetSourceRegistry'); + return assetSourceRegistry; + } } return assets.fetchText( µb.assetsBootstrapLocation || µb.assetsJsonPath @@ -540,6 +543,7 @@ function getAssetSourceRegistry() { : assets.fetchText(µb.assetsJsonPath); }).then(details => { updateAssetSourceRegistry(details.content, true); + ubolog('Loaded assetSourceRegistry'); return assetSourceRegistry; }); }); @@ -670,49 +674,36 @@ let assetCacheRegistryPromise; let assetCacheRegistry = {}; function getAssetCacheRegistry() { - if ( assetCacheRegistryPromise === undefined ) { - assetCacheRegistryPromise = cacheStorage.get( - 'assetCacheRegistry' - ).then(bin => { - if ( - bin instanceof Object && - bin.assetCacheRegistry instanceof Object - ) { - if ( Object.keys(assetCacheRegistry).length === 0 ) { - assetCacheRegistry = bin.assetCacheRegistry; - } else { - console.error( - 'getAssetCacheRegistry(): assetCacheRegistry reassigned!' - ); - if ( - Object.keys(bin.assetCacheRegistry).sort().join() !== - Object.keys(assetCacheRegistry).sort().join() - ) { - console.error( - 'getAssetCacheRegistry(): assetCacheRegistry changes overwritten!' - ); - } - } - } - return assetCacheRegistry; - }); + if ( assetCacheRegistryPromise !== undefined ) { + return assetCacheRegistryPromise; } - + assetCacheRegistryPromise = cacheStorage.get( + 'assetCacheRegistry' + ).then(bin => { + if ( bin instanceof Object === false ) { return; } + if ( bin.assetCacheRegistry instanceof Object === false ) { return; } + if ( Object.keys(assetCacheRegistry).length !== 0 ) { + return console.error('getAssetCacheRegistry(): assetCacheRegistry reassigned!'); + } + ubolog('Loaded assetCacheRegistry'); + assetCacheRegistry = bin.assetCacheRegistry; + }).then(( ) => + assetCacheRegistry + ); return assetCacheRegistryPromise; } const saveAssetCacheRegistry = (( ) => { - const save = function() { + const save = ( ) => { timer.off(); - cacheStorage.set({ assetCacheRegistry }); + return cacheStorage.set({ assetCacheRegistry }); }; const timer = vAPI.defer.create(save); - return function(lazily) { - if ( lazily ) { - timer.offon({ sec: 30 }); - } else { - save(); + return (throttle = 0) => { + if ( throttle === 0 ) { + return save(); } + timer.offon({ sec: throttle }); }; })(); @@ -723,7 +714,9 @@ async function assetCacheRead(assetKey, updateReadTime = false) { const reportBack = function(content) { if ( content instanceof Blob ) { content = ''; } const details = { assetKey, content }; - if ( content === '' ) { details.error = 'ENOTFOUND'; } + if ( content === '' || content === undefined ) { + details.error = 'ENOTFOUND'; + } return details; }; @@ -739,55 +732,39 @@ async function assetCacheRead(assetKey, updateReadTime = false) { ) + ' ms'; } - if ( - bin instanceof Object === false || - bin.hasOwnProperty(internalKey) === false - ) { - return reportBack(''); - } + if ( bin instanceof Object === false ) { return reportBack(''); } + if ( bin.hasOwnProperty(internalKey) === false ) { return reportBack(''); } const entry = assetCacheRegistry[assetKey]; - if ( entry === undefined ) { - return reportBack(''); - } + if ( entry === undefined ) { return reportBack(''); } entry.readTime = Date.now(); if ( updateReadTime ) { - saveAssetCacheRegistry(true); + saveAssetCacheRegistry(23); } return reportBack(bin[internalKey]); } -async function assetCacheWrite(assetKey, details) { - let content = ''; - let options = {}; - if ( typeof details === 'string' ) { - content = details; - } else if ( details instanceof Object ) { - content = details.content || ''; - options = details; - } - - if ( content === '' ) { +async function assetCacheWrite(assetKey, content, options = {}) { + if ( content === '' || content === undefined ) { return assetCacheRemove(assetKey); } const cacheDict = await getAssetCacheRegistry(); - let entry = cacheDict[assetKey]; - if ( entry === undefined ) { - entry = cacheDict[assetKey] = {}; - } + const { resourceTime, url } = options; + const entry = cacheDict[assetKey] || {}; entry.writeTime = entry.readTime = Date.now(); - entry.resourceTime = options.resourceTime || 0; - if ( typeof options.url === 'string' ) { - entry.remoteURL = options.url; + entry.resourceTime = resourceTime || 0; + if ( typeof url === 'string' ) { + entry.remoteURL = url; } - cacheStorage.set({ - assetCacheRegistry, - [`cache/${assetKey}`]: content - }); + cacheDict[assetKey] = entry; + + await cacheStorage.set({ [`cache/${assetKey}`]: content }); + + saveAssetCacheRegistry(3); const result = { assetKey, content }; // https://github.com/uBlockOrigin/uBlock-issues/issues/248 @@ -797,21 +774,31 @@ async function assetCacheWrite(assetKey, details) { return result; } -async function assetCacheRemove(pattern) { +async function assetCacheRemove(pattern, options = {}) { const cacheDict = await getAssetCacheRegistry(); const removedEntries = []; const removedContent = []; for ( const assetKey in cacheDict ) { - if ( pattern instanceof RegExp && !pattern.test(assetKey) ) { - continue; - } - if ( typeof pattern === 'string' && assetKey !== pattern ) { - continue; + if ( pattern instanceof RegExp ) { + if ( pattern.test(assetKey) === false ) { continue; } + } else if ( typeof pattern === 'string' ) { + if ( assetKey !== pattern ) { continue; } } removedEntries.push(assetKey); - removedContent.push('cache/' + assetKey); + removedContent.push(`cache/${assetKey}`); delete cacheDict[assetKey]; } + if ( options.janitor && pattern instanceof RegExp ) { + const re = new RegExp( + pattern.source.replace(/^\^/, '^cache\\/'), + pattern.flags + ); + const keys = await cacheStorage.keys(re); + for ( const key of keys ) { + removedContent.push(key); + ubolog(`Removing stray ${key}`); + } + } if ( removedContent.length !== 0 ) { await Promise.all([ cacheStorage.remove(removedContent), @@ -851,7 +838,7 @@ async function assetCacheSetDetails(assetKey, details) { } } if ( modified ) { - saveAssetCacheRegistry(); + saveAssetCacheRegistry(3); } } @@ -977,8 +964,7 @@ assets.get = async function(assetKey, options = {}) { } if ( details.content === '' ) { continue; } if ( reIsExternalPath.test(contentURL) && options.dontCache !== true ) { - assetCacheWrite(assetKey, { - content: details.content, + assetCacheWrite(assetKey, details.content, { url: contentURL, silent: options.silent === true, }); @@ -1054,8 +1040,7 @@ async function getRemote(assetKey, options = {}) { } // Success - assetCacheWrite(assetKey, { - content: result.content, + assetCacheWrite(assetKey, result.content, { url: contentURL, resourceTime: result.resourceTime || 0, }); @@ -1098,6 +1083,17 @@ assets.put = async function(assetKey, content) { /******************************************************************************/ +assets.toCache = async function(assetKey, content) { + return assetCacheWrite(assetKey, content); +}; + +assets.fromCache = async function(assetKey) { + const details = await assetCacheRead(assetKey); + return details && details.content; +}; + +/******************************************************************************/ + assets.metadata = async function() { await Promise.all([ getAssetSourceRegistry(), @@ -1144,8 +1140,8 @@ assets.metadata = async function() { assets.purge = assetCacheMarkAsDirty; -assets.remove = function(pattern) { - return assetCacheRemove(pattern); +assets.remove = function(...args) { + return assetCacheRemove(...args); }; assets.rmrf = function() { @@ -1297,8 +1293,7 @@ async function diffUpdater() { 'Diff-Path', 'Diff-Expires', ]); - assetCacheWrite(data.assetKey, { - content: data.text, + assetCacheWrite(data.assetKey, data.text, { resourceTime: metadata.lastModified || 0, }); metadata.diffUpdated = true; @@ -1330,6 +1325,8 @@ async function diffUpdater() { terminate(); }; const worker = new Worker('js/diff-updater.js'); + }).catch(reason => { + ubolog(`Diff updater: ${reason}`); }); } |