@wxt-dev/storage 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.mjs +21 -18
- package/package.json +8 -7
package/dist/index.d.mts
CHANGED
|
@@ -103,6 +103,10 @@ interface WxtStorage {
|
|
|
103
103
|
item: WxtStorageItem<any, any>;
|
|
104
104
|
options?: RemoveItemOptions;
|
|
105
105
|
}>): Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* Removes all items from the provided storage area.
|
|
108
|
+
*/
|
|
109
|
+
clear(base: StorageArea): Promise<void>;
|
|
106
110
|
/**
|
|
107
111
|
* Remove the entire metadata for a key, or specific properties by name.
|
|
108
112
|
*
|
|
@@ -134,7 +138,7 @@ interface WxtStorage {
|
|
|
134
138
|
/**
|
|
135
139
|
* Define a storage item with a default value, type, or versioning.
|
|
136
140
|
*
|
|
137
|
-
* Read full docs: https://wxt.dev/
|
|
141
|
+
* Read full docs: https://wxt.dev/storage.html#defining-storage-items
|
|
138
142
|
*/
|
|
139
143
|
defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey): WxtStorageItem<TValue | null, TMetadata>;
|
|
140
144
|
defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue, TMetadata>;
|
package/dist/index.d.ts
CHANGED
|
@@ -103,6 +103,10 @@ interface WxtStorage {
|
|
|
103
103
|
item: WxtStorageItem<any, any>;
|
|
104
104
|
options?: RemoveItemOptions;
|
|
105
105
|
}>): Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* Removes all items from the provided storage area.
|
|
108
|
+
*/
|
|
109
|
+
clear(base: StorageArea): Promise<void>;
|
|
106
110
|
/**
|
|
107
111
|
* Remove the entire metadata for a key, or specific properties by name.
|
|
108
112
|
*
|
|
@@ -134,7 +138,7 @@ interface WxtStorage {
|
|
|
134
138
|
/**
|
|
135
139
|
* Define a storage item with a default value, type, or versioning.
|
|
136
140
|
*
|
|
137
|
-
* Read full docs: https://wxt.dev/
|
|
141
|
+
* Read full docs: https://wxt.dev/storage.html#defining-storage-items
|
|
138
142
|
*/
|
|
139
143
|
defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey): WxtStorageItem<TValue | null, TMetadata>;
|
|
140
144
|
defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue, TMetadata>;
|
package/dist/index.mjs
CHANGED
|
@@ -42,10 +42,8 @@ function createStorage() {
|
|
|
42
42
|
const mergeMeta = (oldMeta, newMeta) => {
|
|
43
43
|
const newFields = { ...oldMeta };
|
|
44
44
|
Object.entries(newMeta).forEach(([key, value]) => {
|
|
45
|
-
if (value == null)
|
|
46
|
-
|
|
47
|
-
else
|
|
48
|
-
newFields[key] = value;
|
|
45
|
+
if (value == null) delete newFields[key];
|
|
46
|
+
else newFields[key] = value;
|
|
49
47
|
});
|
|
50
48
|
return newFields;
|
|
51
49
|
};
|
|
@@ -151,8 +149,7 @@ function createStorage() {
|
|
|
151
149
|
};
|
|
152
150
|
});
|
|
153
151
|
const areaToDriverMetaKeysMap = keys.reduce((map, key) => {
|
|
154
|
-
|
|
155
|
-
map[_a = key.driverArea] ?? (map[_a] = []);
|
|
152
|
+
map[key.driverArea] ??= [];
|
|
156
153
|
map[key.driverArea].push(key);
|
|
157
154
|
return map;
|
|
158
155
|
}, {});
|
|
@@ -182,7 +179,7 @@ function createStorage() {
|
|
|
182
179
|
const { driverArea, driverKey } = resolveKey(
|
|
183
180
|
"key" in item ? item.key : item.item.key
|
|
184
181
|
);
|
|
185
|
-
areaToKeyValueMap[driverArea]
|
|
182
|
+
areaToKeyValueMap[driverArea] ??= [];
|
|
186
183
|
areaToKeyValueMap[driverArea].push({
|
|
187
184
|
key: driverKey,
|
|
188
185
|
value: item.value
|
|
@@ -205,7 +202,7 @@ function createStorage() {
|
|
|
205
202
|
const { driverArea, driverKey } = resolveKey(
|
|
206
203
|
"key" in item ? item.key : item.item.key
|
|
207
204
|
);
|
|
208
|
-
areaToMetaUpdatesMap[driverArea]
|
|
205
|
+
areaToMetaUpdatesMap[driverArea] ??= [];
|
|
209
206
|
areaToMetaUpdatesMap[driverArea].push({
|
|
210
207
|
key: driverKey,
|
|
211
208
|
properties: item.meta
|
|
@@ -254,7 +251,7 @@ function createStorage() {
|
|
|
254
251
|
opts = key.options;
|
|
255
252
|
}
|
|
256
253
|
const { driverArea, driverKey } = resolveKey(keyStr);
|
|
257
|
-
areaToKeysMap[driverArea]
|
|
254
|
+
areaToKeysMap[driverArea] ??= [];
|
|
258
255
|
areaToKeysMap[driverArea].push(driverKey);
|
|
259
256
|
if (opts?.removeMeta) {
|
|
260
257
|
areaToKeysMap[driverArea].push(getMetaKey(driverKey));
|
|
@@ -267,6 +264,10 @@ function createStorage() {
|
|
|
267
264
|
})
|
|
268
265
|
);
|
|
269
266
|
},
|
|
267
|
+
clear: async (base) => {
|
|
268
|
+
const driver = getDriver(base);
|
|
269
|
+
await driver.clear();
|
|
270
|
+
},
|
|
270
271
|
removeMeta: async (key, properties) => {
|
|
271
272
|
const { driver, driverKey } = resolveKey(key);
|
|
272
273
|
await removeMeta(driver, driverKey, properties);
|
|
@@ -307,14 +308,16 @@ function createStorage() {
|
|
|
307
308
|
driverKey,
|
|
308
309
|
driverMetaKey
|
|
309
310
|
]);
|
|
310
|
-
if (value == null)
|
|
311
|
-
return;
|
|
311
|
+
if (value == null) return;
|
|
312
312
|
const currentVersion = meta?.v ?? 1;
|
|
313
313
|
if (currentVersion > targetVersion) {
|
|
314
314
|
throw Error(
|
|
315
315
|
`Version downgrade detected (v${currentVersion} -> v${targetVersion}) for "${key}"`
|
|
316
316
|
);
|
|
317
317
|
}
|
|
318
|
+
if (currentVersion === targetVersion) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
318
321
|
console.debug(
|
|
319
322
|
`[@wxt-dev/storage] Running storage migration for ${key}: v${currentVersion} -> v${targetVersion}`
|
|
320
323
|
);
|
|
@@ -327,7 +330,7 @@ function createStorage() {
|
|
|
327
330
|
try {
|
|
328
331
|
migratedValue = await migrations?.[migrateToVersion]?.(migratedValue) ?? migratedValue;
|
|
329
332
|
} catch (err) {
|
|
330
|
-
throw
|
|
333
|
+
throw new MigrationError(key, migrateToVersion, {
|
|
331
334
|
cause: err
|
|
332
335
|
});
|
|
333
336
|
}
|
|
@@ -351,8 +354,7 @@ function createStorage() {
|
|
|
351
354
|
const getFallback = () => opts?.fallback ?? opts?.defaultValue ?? null;
|
|
352
355
|
const getOrInitValue = () => initMutex.runExclusive(async () => {
|
|
353
356
|
const value = await driver.getItem(driverKey);
|
|
354
|
-
if (value != null || opts?.init == null)
|
|
355
|
-
return value;
|
|
357
|
+
if (value != null || opts?.init == null) return value;
|
|
356
358
|
const newValue = await opts.init();
|
|
357
359
|
await driver.setItem(driverKey, newValue);
|
|
358
360
|
return newValue;
|
|
@@ -459,6 +461,9 @@ function createDriver(storageArea) {
|
|
|
459
461
|
removeItems: async (keys) => {
|
|
460
462
|
await getStorageArea().remove(keys);
|
|
461
463
|
},
|
|
464
|
+
clear: async () => {
|
|
465
|
+
await getStorageArea().clear();
|
|
466
|
+
},
|
|
462
467
|
snapshot: async () => {
|
|
463
468
|
return await getStorageArea().get();
|
|
464
469
|
},
|
|
@@ -468,10 +473,8 @@ function createDriver(storageArea) {
|
|
|
468
473
|
watch(key, cb) {
|
|
469
474
|
const listener = (changes) => {
|
|
470
475
|
const change = changes[key];
|
|
471
|
-
if (change == null)
|
|
472
|
-
|
|
473
|
-
if (dequal(change.newValue, change.oldValue))
|
|
474
|
-
return;
|
|
476
|
+
if (change == null) return;
|
|
477
|
+
if (dequal(change.newValue, change.oldValue)) return;
|
|
475
478
|
cb(change.newValue ?? null, change.oldValue ?? null);
|
|
476
479
|
};
|
|
477
480
|
getStorageArea().onChanged.addListener(listener);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxt-dev/storage",
|
|
3
3
|
"description": "Web extension storage API provided by WXT, supports all browsers.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.1.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -23,19 +23,20 @@
|
|
|
23
23
|
"email": "aaronklinker1+wxt@gmail.com"
|
|
24
24
|
},
|
|
25
25
|
"license": "MIT",
|
|
26
|
+
"funding": "https://github.com/sponsors/wxt-dev",
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"async-mutex": "^0.5.0",
|
|
28
29
|
"dequal": "^2.0.3"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@aklinker1/check": "^1.4.5",
|
|
32
|
-
"@types/chrome": "^0.0.
|
|
33
|
+
"@types/chrome": "^0.0.280",
|
|
33
34
|
"@webext-core/fake-browser": "^1.3.1",
|
|
34
|
-
"oxlint": "^0.
|
|
35
|
-
"publint": "^0.2.
|
|
36
|
-
"typescript": "^5.6.
|
|
37
|
-
"unbuild": "^
|
|
38
|
-
"vitest": "^
|
|
35
|
+
"oxlint": "^0.11.1",
|
|
36
|
+
"publint": "^0.2.12",
|
|
37
|
+
"typescript": "^5.6.3",
|
|
38
|
+
"unbuild": "^3.5.0",
|
|
39
|
+
"vitest": "^3.0.7"
|
|
39
40
|
},
|
|
40
41
|
"module": "./dist/index.mjs",
|
|
41
42
|
"types": "./dist/index.d.ts",
|