Skip to content

Commit

Permalink
cachestorage.js: Refuse backend changes after initialization
Browse files Browse the repository at this point in the history
Enforce the existing cache storage implementation requirement that the
backend not change at runtime:

- Warn if `select()` is not called prior to first use.

- Refuse changes after a backend has been chosen (via `select()` or by
  default on first use).
  • Loading branch information
jacobmcnamee committed Mar 22, 2024
1 parent 0a7e2d2 commit 8f7b7a7
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/js/cachestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import * as s14e from './s14e-serializer.js';

const STORAGE_NAME = 'uBlock0CacheStorage';
const extensionStorage = webext.storage.local;
const defaultFastCache = 'indexedDB';

const keysFromGetArg = arg => {
if ( arg === null || arg === undefined ) { return []; }
Expand All @@ -45,8 +46,6 @@ const keysFromGetArg = arg => {
return Object.keys(arg);
};

let fastCache = 'indexedDB';

/*******************************************************************************
*
* Extension storage
Expand All @@ -56,6 +55,15 @@ let fastCache = 'indexedDB';
* */

const cacheStorage = (( ) => {
let fastCache;

const getFastCacheStorage = ( ) => {
if ( fastCache === undefined ) {
ubolog(`Cache storage backend not selected, using default`);
fastCache = defaultFastCache;
}
return cacheAPIs[fastCache];
};

const exGet = (api, wanted, outbin) => {
return api.get(wanted).then(inbin => {
Expand Down Expand Up @@ -96,7 +104,7 @@ const cacheStorage = (( ) => {
get(argbin) {
const outbin = {};
return exGet(
cacheAPIs[fastCache],
getFastCacheStorage(),
keysFromGetArg(argbin),
outbin
).then(wanted => {
Expand All @@ -123,7 +131,7 @@ const cacheStorage = (( ) => {

async keys(regex) {
const results = await Promise.all([
cacheAPIs[fastCache].keys(regex),
getFastCacheStorage().keys(regex),
extensionStorage.get(null).catch(( ) => {}),
]);
const keys = new Set(results[0]);
Expand All @@ -144,28 +152,38 @@ const cacheStorage = (( ) => {
promises.push(compress(serializedbin, key, rawbin[key]));
}
await Promise.all(promises);
cacheAPIs[fastCache].set(rawbin, serializedbin);
getFastCacheStorage().set(rawbin, serializedbin);
return extensionStorage.set(serializedbin).catch(reason => {
ubolog(reason);
});
},

remove(...args) {
cacheAPIs[fastCache].remove(...args);
getFastCacheStorage().remove(...args);
return extensionStorage.remove(...args).catch(reason => {
ubolog(reason);
});
},

clear(...args) {
cacheAPIs[fastCache].clear(...args);
getFastCacheStorage().clear(...args);
return extensionStorage.clear(...args).catch(reason => {
ubolog(reason);
});
},

select(api) {
if ( cacheAPIs.hasOwnProperty(api) === false ) { return fastCache; }
if ( fastCache !== undefined ) {
ubolog(`Refusing cache storage backend change`);
return fastCache;
}
if ( cacheAPIs.hasOwnProperty(api) === false ) {
if ( api !== undefined && api !== 'unset' ) {
ubolog(`Ignoring unimplemented cache storage backend`);
}
fastCache = defaultFastCache;
return fastCache;
}
fastCache = api;
for ( const k of Object.keys(cacheAPIs) ) {
if ( k === api ) { continue; }
Expand Down

0 comments on commit 8f7b7a7

Please sign in to comment.