@wxt-dev/storage 1.1.1 → 1.2.5

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 CHANGED
@@ -1,3 +1,5 @@
1
+ import { Browser } from '@wxt-dev/browser';
2
+
1
3
  declare const storage: WxtStorage;
2
4
  interface WxtStorage {
3
5
  /**
@@ -141,7 +143,13 @@ interface WxtStorage {
141
143
  * Read full docs: https://wxt.dev/storage.html#defining-storage-items
142
144
  */
143
145
  defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey): WxtStorageItem<TValue | null, TMetadata>;
144
- defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue, TMetadata>;
146
+ defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue> & {
147
+ fallback: TValue;
148
+ }): WxtStorageItem<TValue, TMetadata>;
149
+ defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue> & {
150
+ defaultValue: TValue;
151
+ }): WxtStorageItem<TValue, TMetadata>;
152
+ defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue | null, TMetadata>;
145
153
  }
146
154
  interface WxtStorageItem<TValue, TMetadata extends Record<string, unknown>> {
147
155
  /**
@@ -244,9 +252,18 @@ interface WxtStorageItemOptions<T> {
244
252
  * A map of version numbers to the functions used to migrate the data to that version.
245
253
  */
246
254
  migrations?: Record<number, (oldValue: any) => any>;
255
+ /**
256
+ * Print debug logs, such as migration process.
257
+ * @default false
258
+ */
259
+ debug?: boolean;
260
+ /**
261
+ * A callback function that runs on migration complete.
262
+ */
263
+ onMigrationComplete?: (migratedValue: T, targetVersion: number) => void;
247
264
  }
248
265
  type StorageAreaChanges = {
249
- [key: string]: chrome.storage.StorageChange;
266
+ [key: string]: Browser.storage.StorageChange;
250
267
  };
251
268
  /**
252
269
  * Same as `Partial`, but includes `| null`. It makes all the properties of an object optional and
@@ -269,4 +286,5 @@ declare class MigrationError extends Error {
269
286
  constructor(key: string, version: number, options?: ErrorOptions);
270
287
  }
271
288
 
272
- export { type GetItemOptions, MigrationError, type RemoveItemOptions, type SnapshotOptions, type StorageArea, type StorageAreaChanges, type StorageItemKey, type Unwatch, type WatchCallback, type WxtStorage, type WxtStorageItem, type WxtStorageItemOptions, storage };
289
+ export { MigrationError, storage };
290
+ export type { GetItemOptions, RemoveItemOptions, SnapshotOptions, StorageArea, StorageAreaChanges, StorageItemKey, Unwatch, WatchCallback, WxtStorage, WxtStorageItem, WxtStorageItemOptions };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Browser } from '@wxt-dev/browser';
2
+
1
3
  declare const storage: WxtStorage;
2
4
  interface WxtStorage {
3
5
  /**
@@ -141,7 +143,13 @@ interface WxtStorage {
141
143
  * Read full docs: https://wxt.dev/storage.html#defining-storage-items
142
144
  */
143
145
  defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey): WxtStorageItem<TValue | null, TMetadata>;
144
- defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue, TMetadata>;
146
+ defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue> & {
147
+ fallback: TValue;
148
+ }): WxtStorageItem<TValue, TMetadata>;
149
+ defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue> & {
150
+ defaultValue: TValue;
151
+ }): WxtStorageItem<TValue, TMetadata>;
152
+ defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue | null, TMetadata>;
145
153
  }
146
154
  interface WxtStorageItem<TValue, TMetadata extends Record<string, unknown>> {
147
155
  /**
@@ -244,9 +252,18 @@ interface WxtStorageItemOptions<T> {
244
252
  * A map of version numbers to the functions used to migrate the data to that version.
245
253
  */
246
254
  migrations?: Record<number, (oldValue: any) => any>;
255
+ /**
256
+ * Print debug logs, such as migration process.
257
+ * @default false
258
+ */
259
+ debug?: boolean;
260
+ /**
261
+ * A callback function that runs on migration complete.
262
+ */
263
+ onMigrationComplete?: (migratedValue: T, targetVersion: number) => void;
247
264
  }
248
265
  type StorageAreaChanges = {
249
- [key: string]: chrome.storage.StorageChange;
266
+ [key: string]: Browser.storage.StorageChange;
250
267
  };
251
268
  /**
252
269
  * Same as `Partial`, but includes `| null`. It makes all the properties of an object optional and
@@ -269,4 +286,5 @@ declare class MigrationError extends Error {
269
286
  constructor(key: string, version: number, options?: ErrorOptions);
270
287
  }
271
288
 
272
- export { type GetItemOptions, MigrationError, type RemoveItemOptions, type SnapshotOptions, type StorageArea, type StorageAreaChanges, type StorageItemKey, type Unwatch, type WatchCallback, type WxtStorage, type WxtStorageItem, type WxtStorageItemOptions, storage };
289
+ export { MigrationError, storage };
290
+ export type { GetItemOptions, RemoveItemOptions, SnapshotOptions, StorageArea, StorageAreaChanges, StorageItemKey, Unwatch, WatchCallback, WxtStorage, WxtStorageItem, WxtStorageItemOptions };
package/dist/index.mjs CHANGED
@@ -1,13 +1,7 @@
1
1
  import { dequal } from 'dequal/lite';
2
2
  import { Mutex } from 'async-mutex';
3
+ import { browser } from '@wxt-dev/browser';
3
4
 
4
- const browser = (
5
- // @ts-expect-error
6
- globalThis.browser?.runtime?.id == null ? globalThis.chrome : (
7
- // @ts-expect-error
8
- globalThis.browser
9
- )
10
- );
11
5
  const storage = createStorage();
12
6
  function createStorage() {
13
7
  const drivers = {
@@ -213,7 +207,6 @@ function createStorage() {
213
207
  async ([storageArea, updates]) => {
214
208
  const driver = getDriver(storageArea);
215
209
  const metaKeys = updates.map(({ key }) => getMetaKey(key));
216
- console.log(storageArea, metaKeys);
217
210
  const existingMetas = await driver.getItems(metaKeys);
218
211
  const existingMetaMap = Object.fromEntries(
219
212
  existingMetas.map(({ key, value }) => [key, getMetaValue(value)])
@@ -296,7 +289,12 @@ function createStorage() {
296
289
  },
297
290
  defineItem: (key, opts) => {
298
291
  const { driver, driverKey } = resolveKey(key);
299
- const { version: targetVersion = 1, migrations = {} } = opts ?? {};
292
+ const {
293
+ version: targetVersion = 1,
294
+ migrations = {},
295
+ onMigrationComplete,
296
+ debug = false
297
+ } = opts ?? {};
300
298
  if (targetVersion < 1) {
301
299
  throw Error(
302
300
  "Storage item version cannot be less than 1. Initial versions should be set to 1, not 0."
@@ -318,9 +316,11 @@ function createStorage() {
318
316
  if (currentVersion === targetVersion) {
319
317
  return;
320
318
  }
321
- console.debug(
322
- `[@wxt-dev/storage] Running storage migration for ${key}: v${currentVersion} -> v${targetVersion}`
323
- );
319
+ if (debug === true) {
320
+ console.debug(
321
+ `[@wxt-dev/storage] Running storage migration for ${key}: v${currentVersion} -> v${targetVersion}`
322
+ );
323
+ }
324
324
  const migrationsToRun = Array.from(
325
325
  { length: targetVersion - currentVersion },
326
326
  (_, i) => currentVersion + i + 1
@@ -329,6 +329,11 @@ function createStorage() {
329
329
  for (const migrateToVersion of migrationsToRun) {
330
330
  try {
331
331
  migratedValue = await migrations?.[migrateToVersion]?.(migratedValue) ?? migratedValue;
332
+ if (debug === true) {
333
+ console.debug(
334
+ `[@wxt-dev/storage] Storage migration processed for version: v${migrateToVersion}`
335
+ );
336
+ }
332
337
  } catch (err) {
333
338
  throw new MigrationError(key, migrateToVersion, {
334
339
  cause: err
@@ -339,10 +344,13 @@ function createStorage() {
339
344
  { key: driverKey, value: migratedValue },
340
345
  { key: driverMetaKey, value: { ...meta, v: targetVersion } }
341
346
  ]);
342
- console.debug(
343
- `[@wxt-dev/storage] Storage migration completed for ${key} v${targetVersion}`,
344
- { migratedValue }
345
- );
347
+ if (debug === true) {
348
+ console.debug(
349
+ `[@wxt-dev/storage] Storage migration completed for ${key} v${targetVersion}`,
350
+ { migratedValue }
351
+ );
352
+ }
353
+ onMigrationComplete?.(migratedValue, targetVersion);
346
354
  };
347
355
  const migrationsDone = opts?.migrations == null ? Promise.resolve() : migrate().catch((err) => {
348
356
  console.error(
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.1.1",
4
+ "version": "1.2.5",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
@@ -24,19 +24,24 @@
24
24
  },
25
25
  "license": "MIT",
26
26
  "funding": "https://github.com/sponsors/wxt-dev",
27
+ "scripts": {
28
+ "build": "buildc -- unbuild",
29
+ "check": "buildc --deps-only -- check",
30
+ "test": "buildc --deps-only -- vitest"
31
+ },
27
32
  "dependencies": {
33
+ "@wxt-dev/browser": "workspace:^",
28
34
  "async-mutex": "^0.5.0",
29
35
  "dequal": "^2.0.3"
30
36
  },
31
37
  "devDependencies": {
32
- "@aklinker1/check": "^1.4.5",
33
- "@types/chrome": "^0.0.280",
34
- "@webext-core/fake-browser": "^1.3.1",
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"
38
+ "@aklinker1/check": "^2.1.0",
39
+ "@webext-core/fake-browser": "^1.3.2",
40
+ "oxlint": "^1.14.0",
41
+ "publint": "^0.3.12",
42
+ "typescript": "^5.9.2",
43
+ "unbuild": "^3.6.1",
44
+ "vitest": "^3.2.4"
40
45
  },
41
46
  "module": "./dist/index.mjs",
42
47
  "types": "./dist/index.d.ts",
@@ -48,10 +53,5 @@
48
53
  },
49
54
  "files": [
50
55
  "dist"
51
- ],
52
- "scripts": {
53
- "build": "buildc -- unbuild",
54
- "check": "buildc --deps-only -- check",
55
- "test": "buildc --deps-only -- vitest"
56
- }
57
- }
56
+ ]
57
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Aaron
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.