@webext-core/storage 1.1.3 → 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 +4 -2
- package/lib/index.cjs +109 -113
- package/lib/index.d.cts +71 -0
- package/lib/index.d.mts +71 -0
- package/lib/index.global.js +1 -1116
- package/lib/index.mjs +91 -0
- package/package.json +45 -33
- package/LICENSE +0 -21
- package/lib/index.d.ts +0 -72
- package/lib/index.js +0 -88
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.
|
|
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/
|
|
18
|
+
See [documentation](https://webext-core.aklinker1.io/storage/installation) to
|
|
19
|
+
get started!
|
package/lib/index.cjs
CHANGED
|
@@ -1,123 +1,119 @@
|
|
|
1
|
-
"
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
+
*/
|
|
58
43
|
function defineExtensionStorage(storage) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
+
};
|
|
110
97
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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;
|
package/lib/index.d.cts
ADDED
|
@@ -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 };
|
package/lib/index.d.mts
ADDED
|
@@ -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 };
|