@webext-core/storage 1.2.0 → 1.2.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/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # `@webext-core/storage`
2
2
 
3
- A type-safe, localStorage-esk wrapper around the web extension storage APIs. Supports all browsers (Chrome, Firefox, Safari, etc).
3
+ A type-safe, localStorage-esk wrapper around the web extension storage APIs.
4
+ Supports all browsers (Chrome, Firefox, Safari, etc).
4
5
 
5
6
  ```bash
6
7
  pnpm i @webext-core/storage
@@ -14,4 +15,5 @@ const value = await localExtStorage.getItem('some-key');
14
15
 
15
16
  ## Get Started
16
17
 
17
- See [documentation](https://webext-core.aklinker1.io/guide/storage/) to get started!
18
+ See [documentation](https://webext-core.aklinker1.io/storage/installation) to
19
+ get started!
package/lib/index.cjs CHANGED
@@ -1,126 +1,119 @@
1
- "use strict";
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
2
3
  var __create = Object.create;
3
4
  var __defProp = Object.defineProperty;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
7
  var __getProtoOf = Object.getPrototypeOf;
7
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
9
  var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
- var __async = (__this, __arguments, generator) => {
26
- return new Promise((resolve, reject) => {
27
- var fulfilled = (value) => {
28
- try {
29
- step(generator.next(value));
30
- } catch (e) {
31
- reject(e);
32
- }
33
- };
34
- var rejected = (value) => {
35
- try {
36
- step(generator.throw(value));
37
- } catch (e) {
38
- reject(e);
39
- }
40
- };
41
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
42
- step((generator = generator.apply(__this, __arguments)).next());
43
- });
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
44
18
  };
45
-
46
- // src/index.ts
47
- var src_exports = {};
48
- __export(src_exports, {
49
- defineExtensionStorage: () => defineExtensionStorage,
50
- localExtStorage: () => localExtStorage,
51
- managedExtStorage: () => managedExtStorage,
52
- sessionExtStorage: () => sessionExtStorage,
53
- syncExtStorage: () => syncExtStorage
54
- });
55
- module.exports = __toCommonJS(src_exports);
56
-
57
- // src/defineExtensionStorage.ts
58
- var import_webextension_polyfill = __toESM(require("webextension-polyfill"), 1);
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
24
+ let webextension_polyfill = require("webextension-polyfill");
25
+ webextension_polyfill = __toESM(webextension_polyfill, 1);
26
+ //#region src/defineExtensionStorage.ts
27
+ /**
28
+ * Create a storage instance with an optional schema, `TSchema`, for type safety.
29
+ *
30
+ * @example
31
+ * import browser from 'webextension-polyfill';
32
+ *
33
+ * interface Schema {
34
+ * installDate: number;
35
+ * }
36
+ * const extensionStorage = defineExtensionStorage<Schema>(browser.storage.local);
37
+ *
38
+ * const date = await extensionStorage.getItem('installDate');
39
+ *
40
+ * @param storage The storage to to use. Either `Browser.storage.local`, `Browser.storage.sync`, or
41
+ * `Browser.storage.managed`.
42
+ */
59
43
  function defineExtensionStorage(storage) {
60
- const onStorageChanged = (changes) => __async(this, null, function* () {
61
- const work = listeners.map(({ key, cb }) => {
62
- if (!(key in changes))
63
- return;
64
- const { newValue, oldValue } = changes[key];
65
- if (newValue === oldValue)
66
- return;
67
- return cb(newValue, oldValue);
68
- });
69
- yield Promise.all(work);
70
- });
71
- let listeners = [];
72
- function addListener(listener) {
73
- if (listeners.length === 0) {
74
- storage.onChanged.addListener(onStorageChanged);
75
- }
76
- listeners.push(listener);
77
- }
78
- function removeListener(listener) {
79
- const i = listeners.indexOf(listener);
80
- if (i >= 0)
81
- listeners.splice(i, 1);
82
- if (listeners.length === 0) {
83
- import_webextension_polyfill.default.storage.onChanged.removeListener(onStorageChanged);
84
- }
85
- }
86
- return {
87
- clear() {
88
- return storage.clear();
89
- },
90
- getItem(key) {
91
- return storage.get(key).then((res) => {
92
- var _a;
93
- return (_a = res[key]) != null ? _a : null;
94
- });
95
- },
96
- setItem(key, value) {
97
- return storage.set({ [key]: value != null ? value : null });
98
- },
99
- removeItem(key) {
100
- return storage.remove(key);
101
- },
102
- onChange(key, cb) {
103
- const listener = {
104
- key,
105
- cb
106
- };
107
- addListener(listener);
108
- return () => removeListener(listener);
109
- }
110
- };
44
+ /**
45
+ * The singleton callback added and removed from the `browser.storage.onChanged` event. It calls
46
+ * all the listeners added to this storage instance.
47
+ */
48
+ const onStorageChanged = async (changes) => {
49
+ const work = listeners.map(({ key, cb }) => {
50
+ if (!(key in changes)) return;
51
+ const { newValue, oldValue } = changes[key];
52
+ if (newValue === oldValue) return;
53
+ return cb(newValue, oldValue);
54
+ });
55
+ await Promise.all(work);
56
+ };
57
+ let listeners = [];
58
+ /**
59
+ * Add the listener to the list of listeners, but also create the singleton listener if this is
60
+ * the first listener.
61
+ */
62
+ function addListener(listener) {
63
+ if (listeners.length === 0) storage.onChanged.addListener(onStorageChanged);
64
+ listeners.push(listener);
65
+ }
66
+ /**
67
+ * Remove the listener from the list, but also unset the singleton listener if no listeners are
68
+ * active.
69
+ */
70
+ function removeListener(listener) {
71
+ const i = listeners.indexOf(listener);
72
+ if (i >= 0) listeners.splice(i, 1);
73
+ if (listeners.length === 0) webextension_polyfill.default.storage.onChanged.removeListener(onStorageChanged);
74
+ }
75
+ return {
76
+ clear() {
77
+ return storage.clear();
78
+ },
79
+ getItem(key) {
80
+ return storage.get(key).then((res) => res[key] ?? null);
81
+ },
82
+ setItem(key, value) {
83
+ return storage.set({ [key]: value ?? null });
84
+ },
85
+ removeItem(key) {
86
+ return storage.remove(key);
87
+ },
88
+ onChange(key, cb) {
89
+ const listener = {
90
+ key,
91
+ cb
92
+ };
93
+ addListener(listener);
94
+ return () => removeListener(listener);
95
+ }
96
+ };
111
97
  }
112
-
113
- // src/index.ts
114
- var import_webextension_polyfill2 = __toESM(require("webextension-polyfill"), 1);
115
- var localExtStorage = defineExtensionStorage(import_webextension_polyfill2.default.storage.local);
116
- var sessionExtStorage = defineExtensionStorage(import_webextension_polyfill2.default.storage.session);
117
- var syncExtStorage = defineExtensionStorage(import_webextension_polyfill2.default.storage.sync);
118
- var managedExtStorage = defineExtensionStorage(import_webextension_polyfill2.default.storage.managed);
119
- // Annotate the CommonJS export names for ESM import in node:
120
- 0 && (module.exports = {
121
- defineExtensionStorage,
122
- localExtStorage,
123
- managedExtStorage,
124
- sessionExtStorage,
125
- syncExtStorage
126
- });
98
+ //#endregion
99
+ //#region src/index.ts
100
+ /** An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area. */
101
+ const localExtStorage = defineExtensionStorage(webextension_polyfill.default.storage.local);
102
+ /**
103
+ * An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area.
104
+ *
105
+ * - Added to Chrome 102 as of May 24th, 2022.
106
+ * - Added to Safari 16.4 as of March 27th, 2023.
107
+ * - Added to Firefox 115 as of July 4th, 2023.
108
+ */
109
+ const sessionExtStorage = defineExtensionStorage(webextension_polyfill.default.storage.session);
110
+ /** An implementation of `ExtensionStorage` based on the `browser.storage.sync` storage area. */
111
+ const syncExtStorage = defineExtensionStorage(webextension_polyfill.default.storage.sync);
112
+ /** An implementation of `ExtensionStorage` based on the `browser.storage.managed` storage area. */
113
+ const managedExtStorage = defineExtensionStorage(webextension_polyfill.default.storage.managed);
114
+ //#endregion
115
+ exports.defineExtensionStorage = defineExtensionStorage;
116
+ exports.localExtStorage = localExtStorage;
117
+ exports.managedExtStorage = managedExtStorage;
118
+ exports.sessionExtStorage = sessionExtStorage;
119
+ exports.syncExtStorage = syncExtStorage;
@@ -0,0 +1,71 @@
1
+ import { Storage } from "webextension-polyfill";
2
+
3
+ //#region src/types.d.ts
4
+ type AnySchema = Record<string, any>;
5
+ /** Call this method to remove the listener that was added. */
6
+ type RemoveListenerCallback = () => void;
7
+ type OnChangeCallback<TSchema extends AnySchema, TKey extends keyof TSchema = keyof TSchema> = (newValue: TSchema[TKey], oldValue: TSchema[TKey] | null) => void;
8
+ /**
9
+ * This is the interface for the storage objects exported from the package. It is similar to
10
+ * `localStorage`, except for a few differences:
11
+ *
12
+ * - _**It's async**_ since the web extension storage APIs are async.
13
+ * - It can store any data type, _**not just strings**_.
14
+ */
15
+ interface ExtensionStorage<TSchema extends AnySchema> {
16
+ /** Clear all values from storage. */
17
+ clear(): Promise<void>;
18
+ /** Return the value in storage or `null` if the item is missing. */
19
+ getItem<TKey extends keyof TSchema>(key: TKey): Promise<Required<TSchema>[TKey] | null>;
20
+ /**
21
+ * Set the key and value in storage. Unlike with `localStorage`, passing `null` or `undefined`
22
+ * will result in `null` being stored for the value.
23
+ */
24
+ setItem<TKey extends keyof TSchema>(key: TKey, value: TSchema[TKey]): Promise<void>;
25
+ /** Remove the value from storage at a key. */
26
+ removeItem<TKey extends keyof TSchema>(key: TKey): Promise<void>;
27
+ /**
28
+ * Add a callback that is executed when a key is changed in the storage. Listeners are executed in
29
+ * parallel, but the first listeners added are always started first.
30
+ *
31
+ * Returns a method that, when called, removes the listener that was added.
32
+ */
33
+ onChange<TKey extends keyof TSchema>(key: TKey, cb: OnChangeCallback<TSchema, TKey>): RemoveListenerCallback;
34
+ }
35
+ //#endregion
36
+ //#region src/defineExtensionStorage.d.ts
37
+ /**
38
+ * Create a storage instance with an optional schema, `TSchema`, for type safety.
39
+ *
40
+ * @example
41
+ * import browser from 'webextension-polyfill';
42
+ *
43
+ * interface Schema {
44
+ * installDate: number;
45
+ * }
46
+ * const extensionStorage = defineExtensionStorage<Schema>(browser.storage.local);
47
+ *
48
+ * const date = await extensionStorage.getItem('installDate');
49
+ *
50
+ * @param storage The storage to to use. Either `Browser.storage.local`, `Browser.storage.sync`, or
51
+ * `Browser.storage.managed`.
52
+ */
53
+ declare function defineExtensionStorage<TSchema extends AnySchema = AnySchema>(storage: Storage.StorageArea): ExtensionStorage<TSchema>;
54
+ //#endregion
55
+ //#region src/index.d.ts
56
+ /** An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area. */
57
+ declare const localExtStorage: ExtensionStorage<AnySchema>;
58
+ /**
59
+ * An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area.
60
+ *
61
+ * - Added to Chrome 102 as of May 24th, 2022.
62
+ * - Added to Safari 16.4 as of March 27th, 2023.
63
+ * - Added to Firefox 115 as of July 4th, 2023.
64
+ */
65
+ declare const sessionExtStorage: ExtensionStorage<AnySchema>;
66
+ /** An implementation of `ExtensionStorage` based on the `browser.storage.sync` storage area. */
67
+ declare const syncExtStorage: ExtensionStorage<AnySchema>;
68
+ /** An implementation of `ExtensionStorage` based on the `browser.storage.managed` storage area. */
69
+ declare const managedExtStorage: ExtensionStorage<AnySchema>;
70
+ //#endregion
71
+ export { type ExtensionStorage, defineExtensionStorage, localExtStorage, managedExtStorage, sessionExtStorage, syncExtStorage };
@@ -0,0 +1,71 @@
1
+ import { Storage } from "webextension-polyfill";
2
+
3
+ //#region src/types.d.ts
4
+ type AnySchema = Record<string, any>;
5
+ /** Call this method to remove the listener that was added. */
6
+ type RemoveListenerCallback = () => void;
7
+ type OnChangeCallback<TSchema extends AnySchema, TKey extends keyof TSchema = keyof TSchema> = (newValue: TSchema[TKey], oldValue: TSchema[TKey] | null) => void;
8
+ /**
9
+ * This is the interface for the storage objects exported from the package. It is similar to
10
+ * `localStorage`, except for a few differences:
11
+ *
12
+ * - _**It's async**_ since the web extension storage APIs are async.
13
+ * - It can store any data type, _**not just strings**_.
14
+ */
15
+ interface ExtensionStorage<TSchema extends AnySchema> {
16
+ /** Clear all values from storage. */
17
+ clear(): Promise<void>;
18
+ /** Return the value in storage or `null` if the item is missing. */
19
+ getItem<TKey extends keyof TSchema>(key: TKey): Promise<Required<TSchema>[TKey] | null>;
20
+ /**
21
+ * Set the key and value in storage. Unlike with `localStorage`, passing `null` or `undefined`
22
+ * will result in `null` being stored for the value.
23
+ */
24
+ setItem<TKey extends keyof TSchema>(key: TKey, value: TSchema[TKey]): Promise<void>;
25
+ /** Remove the value from storage at a key. */
26
+ removeItem<TKey extends keyof TSchema>(key: TKey): Promise<void>;
27
+ /**
28
+ * Add a callback that is executed when a key is changed in the storage. Listeners are executed in
29
+ * parallel, but the first listeners added are always started first.
30
+ *
31
+ * Returns a method that, when called, removes the listener that was added.
32
+ */
33
+ onChange<TKey extends keyof TSchema>(key: TKey, cb: OnChangeCallback<TSchema, TKey>): RemoveListenerCallback;
34
+ }
35
+ //#endregion
36
+ //#region src/defineExtensionStorage.d.ts
37
+ /**
38
+ * Create a storage instance with an optional schema, `TSchema`, for type safety.
39
+ *
40
+ * @example
41
+ * import browser from 'webextension-polyfill';
42
+ *
43
+ * interface Schema {
44
+ * installDate: number;
45
+ * }
46
+ * const extensionStorage = defineExtensionStorage<Schema>(browser.storage.local);
47
+ *
48
+ * const date = await extensionStorage.getItem('installDate');
49
+ *
50
+ * @param storage The storage to to use. Either `Browser.storage.local`, `Browser.storage.sync`, or
51
+ * `Browser.storage.managed`.
52
+ */
53
+ declare function defineExtensionStorage<TSchema extends AnySchema = AnySchema>(storage: Storage.StorageArea): ExtensionStorage<TSchema>;
54
+ //#endregion
55
+ //#region src/index.d.ts
56
+ /** An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area. */
57
+ declare const localExtStorage: ExtensionStorage<AnySchema>;
58
+ /**
59
+ * An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area.
60
+ *
61
+ * - Added to Chrome 102 as of May 24th, 2022.
62
+ * - Added to Safari 16.4 as of March 27th, 2023.
63
+ * - Added to Firefox 115 as of July 4th, 2023.
64
+ */
65
+ declare const sessionExtStorage: ExtensionStorage<AnySchema>;
66
+ /** An implementation of `ExtensionStorage` based on the `browser.storage.sync` storage area. */
67
+ declare const syncExtStorage: ExtensionStorage<AnySchema>;
68
+ /** An implementation of `ExtensionStorage` based on the `browser.storage.managed` storage area. */
69
+ declare const managedExtStorage: ExtensionStorage<AnySchema>;
70
+ //#endregion
71
+ export { type ExtensionStorage, defineExtensionStorage, localExtStorage, managedExtStorage, sessionExtStorage, syncExtStorage };