@@ -35,6 +35,7 @@ import * as s14e from './s14e-serializer.js';
35
35
36
36
const STORAGE_NAME = 'uBlock0CacheStorage' ;
37
37
const extensionStorage = webext . storage . local ;
38
+ const defaultFastCache = 'indexedDB' ;
38
39
39
40
const keysFromGetArg = arg => {
40
41
if ( arg === null || arg === undefined ) { return [ ] ; }
@@ -45,8 +46,6 @@ const keysFromGetArg = arg => {
45
46
return Object . keys ( arg ) ;
46
47
} ;
47
48
48
- let fastCache = 'indexedDB' ;
49
-
50
49
/*******************************************************************************
51
50
*
52
51
* Extension storage
@@ -56,6 +55,15 @@ let fastCache = 'indexedDB';
56
55
* */
57
56
58
57
const cacheStorage = ( ( ) => {
58
+ let fastCache ;
59
+
60
+ const getFastCacheStorage = ( ) => {
61
+ if ( fastCache === undefined ) {
62
+ ubolog ( `Cache storage backend not selected, using default` ) ;
63
+ fastCache = defaultFastCache ;
64
+ }
65
+ return cacheAPIs [ fastCache ] ;
66
+ } ;
59
67
60
68
const exGet = ( api , wanted , outbin ) => {
61
69
return api . get ( wanted ) . then ( inbin => {
@@ -96,7 +104,7 @@ const cacheStorage = (( ) => {
96
104
get ( argbin ) {
97
105
const outbin = { } ;
98
106
return exGet (
99
- cacheAPIs [ fastCache ] ,
107
+ getFastCacheStorage ( ) ,
100
108
keysFromGetArg ( argbin ) ,
101
109
outbin
102
110
) . then ( wanted => {
@@ -123,7 +131,7 @@ const cacheStorage = (( ) => {
123
131
124
132
async keys ( regex ) {
125
133
const results = await Promise . all ( [
126
- cacheAPIs [ fastCache ] . keys ( regex ) ,
134
+ getFastCacheStorage ( ) . keys ( regex ) ,
127
135
extensionStorage . get ( null ) . catch ( ( ) => { } ) ,
128
136
] ) ;
129
137
const keys = new Set ( results [ 0 ] ) ;
@@ -144,28 +152,38 @@ const cacheStorage = (( ) => {
144
152
promises . push ( compress ( serializedbin , key , rawbin [ key ] ) ) ;
145
153
}
146
154
await Promise . all ( promises ) ;
147
- cacheAPIs [ fastCache ] . set ( rawbin , serializedbin ) ;
155
+ getFastCacheStorage ( ) . set ( rawbin , serializedbin ) ;
148
156
return extensionStorage . set ( serializedbin ) . catch ( reason => {
149
157
ubolog ( reason ) ;
150
158
} ) ;
151
159
} ,
152
160
153
161
remove ( ...args ) {
154
- cacheAPIs [ fastCache ] . remove ( ...args ) ;
162
+ getFastCacheStorage ( ) . remove ( ...args ) ;
155
163
return extensionStorage . remove ( ...args ) . catch ( reason => {
156
164
ubolog ( reason ) ;
157
165
} ) ;
158
166
} ,
159
167
160
168
clear ( ...args ) {
161
- cacheAPIs [ fastCache ] . clear ( ...args ) ;
169
+ getFastCacheStorage ( ) . clear ( ...args ) ;
162
170
return extensionStorage . clear ( ...args ) . catch ( reason => {
163
171
ubolog ( reason ) ;
164
172
} ) ;
165
173
} ,
166
174
167
175
select ( api ) {
168
- if ( cacheAPIs . hasOwnProperty ( api ) === false ) { return fastCache ; }
176
+ if ( fastCache !== undefined ) {
177
+ ubolog ( `Refusing cache storage backend change` ) ;
178
+ return fastCache ;
179
+ }
180
+ if ( cacheAPIs . hasOwnProperty ( api ) === false ) {
181
+ if ( api !== undefined && api !== 'unset' ) {
182
+ ubolog ( `Ignoring unimplemented cache storage backend` ) ;
183
+ }
184
+ fastCache = defaultFastCache ;
185
+ return fastCache ;
186
+ }
169
187
fastCache = api ;
170
188
for ( const k of Object . keys ( cacheAPIs ) ) {
171
189
if ( k === api ) { continue ; }
0 commit comments