@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/lib/index.mjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import Browser from "webextension-polyfill";
|
|
2
|
+
//#region src/defineExtensionStorage.ts
|
|
3
|
+
/**
|
|
4
|
+
* Create a storage instance with an optional schema, `TSchema`, for type safety.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* import browser from 'webextension-polyfill';
|
|
8
|
+
*
|
|
9
|
+
* interface Schema {
|
|
10
|
+
* installDate: number;
|
|
11
|
+
* }
|
|
12
|
+
* const extensionStorage = defineExtensionStorage<Schema>(browser.storage.local);
|
|
13
|
+
*
|
|
14
|
+
* const date = await extensionStorage.getItem('installDate');
|
|
15
|
+
*
|
|
16
|
+
* @param storage The storage to to use. Either `Browser.storage.local`, `Browser.storage.sync`, or
|
|
17
|
+
* `Browser.storage.managed`.
|
|
18
|
+
*/
|
|
19
|
+
function defineExtensionStorage(storage) {
|
|
20
|
+
/**
|
|
21
|
+
* The singleton callback added and removed from the `browser.storage.onChanged` event. It calls
|
|
22
|
+
* all the listeners added to this storage instance.
|
|
23
|
+
*/
|
|
24
|
+
const onStorageChanged = async (changes) => {
|
|
25
|
+
const work = listeners.map(({ key, cb }) => {
|
|
26
|
+
if (!(key in changes)) return;
|
|
27
|
+
const { newValue, oldValue } = changes[key];
|
|
28
|
+
if (newValue === oldValue) return;
|
|
29
|
+
return cb(newValue, oldValue);
|
|
30
|
+
});
|
|
31
|
+
await Promise.all(work);
|
|
32
|
+
};
|
|
33
|
+
let listeners = [];
|
|
34
|
+
/**
|
|
35
|
+
* Add the listener to the list of listeners, but also create the singleton listener if this is
|
|
36
|
+
* the first listener.
|
|
37
|
+
*/
|
|
38
|
+
function addListener(listener) {
|
|
39
|
+
if (listeners.length === 0) storage.onChanged.addListener(onStorageChanged);
|
|
40
|
+
listeners.push(listener);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Remove the listener from the list, but also unset the singleton listener if no listeners are
|
|
44
|
+
* active.
|
|
45
|
+
*/
|
|
46
|
+
function removeListener(listener) {
|
|
47
|
+
const i = listeners.indexOf(listener);
|
|
48
|
+
if (i >= 0) listeners.splice(i, 1);
|
|
49
|
+
if (listeners.length === 0) Browser.storage.onChanged.removeListener(onStorageChanged);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
clear() {
|
|
53
|
+
return storage.clear();
|
|
54
|
+
},
|
|
55
|
+
getItem(key) {
|
|
56
|
+
return storage.get(key).then((res) => res[key] ?? null);
|
|
57
|
+
},
|
|
58
|
+
setItem(key, value) {
|
|
59
|
+
return storage.set({ [key]: value ?? null });
|
|
60
|
+
},
|
|
61
|
+
removeItem(key) {
|
|
62
|
+
return storage.remove(key);
|
|
63
|
+
},
|
|
64
|
+
onChange(key, cb) {
|
|
65
|
+
const listener = {
|
|
66
|
+
key,
|
|
67
|
+
cb
|
|
68
|
+
};
|
|
69
|
+
addListener(listener);
|
|
70
|
+
return () => removeListener(listener);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/index.ts
|
|
76
|
+
/** An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area. */
|
|
77
|
+
const localExtStorage = defineExtensionStorage(Browser.storage.local);
|
|
78
|
+
/**
|
|
79
|
+
* An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area.
|
|
80
|
+
*
|
|
81
|
+
* - Added to Chrome 102 as of May 24th, 2022.
|
|
82
|
+
* - Added to Safari 16.4 as of March 27th, 2023.
|
|
83
|
+
* - Added to Firefox 115 as of July 4th, 2023.
|
|
84
|
+
*/
|
|
85
|
+
const sessionExtStorage = defineExtensionStorage(Browser.storage.session);
|
|
86
|
+
/** An implementation of `ExtensionStorage` based on the `browser.storage.sync` storage area. */
|
|
87
|
+
const syncExtStorage = defineExtensionStorage(Browser.storage.sync);
|
|
88
|
+
/** An implementation of `ExtensionStorage` based on the `browser.storage.managed` storage area. */
|
|
89
|
+
const managedExtStorage = defineExtensionStorage(Browser.storage.managed);
|
|
90
|
+
//#endregion
|
|
91
|
+
export { defineExtensionStorage, localExtStorage, managedExtStorage, sessionExtStorage, syncExtStorage };
|
package/package.json
CHANGED
|
@@ -1,60 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webext-core/storage",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "A type-safe, localStorage-esk wrapper around the web extension storage APIs. Supports all browsers (Chrome, Firefox, Safari, etc)",
|
|
5
|
-
"license": "MIT",
|
|
6
5
|
"keywords": [
|
|
7
|
-
"
|
|
6
|
+
"browser",
|
|
8
7
|
"browser-extension",
|
|
9
|
-
"chrome-extension",
|
|
10
|
-
"webext",
|
|
11
|
-
"web-ext",
|
|
12
8
|
"chrome",
|
|
9
|
+
"chrome-extension",
|
|
10
|
+
"extension",
|
|
13
11
|
"firefox",
|
|
14
12
|
"safari",
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
13
|
+
"storage",
|
|
14
|
+
"web-ext",
|
|
15
|
+
"web-extension",
|
|
16
|
+
"webext"
|
|
18
17
|
],
|
|
19
18
|
"homepage": "https://github.com/aklinker1/webext-core/tree/main/packages/storage",
|
|
19
|
+
"license": "MIT",
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
|
22
|
-
"url": "https://github.com/aklinker1/webext-core",
|
|
22
|
+
"url": "git+https://github.com/aklinker1/webext-core.git",
|
|
23
23
|
"directory": "packages/storage"
|
|
24
24
|
},
|
|
25
|
-
"type": "module",
|
|
26
|
-
"publishConfig": {
|
|
27
|
-
"access": "public"
|
|
28
|
-
},
|
|
29
25
|
"files": [
|
|
30
26
|
"lib"
|
|
31
27
|
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
32
30
|
"main": "lib/index.cjs",
|
|
33
|
-
"module": "lib/index.
|
|
34
|
-
"types": "lib/index.d.
|
|
31
|
+
"module": "lib/index.mjs",
|
|
32
|
+
"types": "lib/index.d.mts",
|
|
35
33
|
"exports": {
|
|
36
34
|
".": {
|
|
37
|
-
"import":
|
|
38
|
-
|
|
35
|
+
"import": {
|
|
36
|
+
"types": "./lib/index.d.mts",
|
|
37
|
+
"default": "./lib/index.mjs"
|
|
38
|
+
},
|
|
39
|
+
"require": {
|
|
40
|
+
"types": "./lib/index.d.cts",
|
|
41
|
+
"default": "./lib/index.cjs"
|
|
42
|
+
}
|
|
39
43
|
}
|
|
40
44
|
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "buildc -- tsdown",
|
|
50
|
+
"test": "buildc --deps-only -- bun test",
|
|
51
|
+
"test:coverage": "buildc --deps-only -- bun test --coverage",
|
|
52
|
+
"check": "bun run build && check",
|
|
53
|
+
"prepack": "bun run build"
|
|
54
|
+
},
|
|
41
55
|
"dependencies": {
|
|
42
|
-
"webextension-polyfill": "
|
|
56
|
+
"webextension-polyfill": ">=0.10.0"
|
|
43
57
|
},
|
|
44
58
|
"devDependencies": {
|
|
45
|
-
"@types/webextension-polyfill": "^0.
|
|
46
|
-
"@
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"tsconfig": "0.0.0"
|
|
59
|
+
"@types/webextension-polyfill": "^0.10.5",
|
|
60
|
+
"@webext-core/fake-browser": "workspace:*",
|
|
61
|
+
"publint": "catalog:",
|
|
62
|
+
"tsconfig": "workspace:*",
|
|
63
|
+
"tsdown": "catalog:",
|
|
64
|
+
"typescript": "catalog:"
|
|
52
65
|
},
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
"
|
|
58
|
-
"compile": "tsc --noEmit"
|
|
66
|
+
"engines": {
|
|
67
|
+
"node": "*"
|
|
68
|
+
},
|
|
69
|
+
"buildc": {
|
|
70
|
+
"outDir": "lib"
|
|
59
71
|
}
|
|
60
|
-
}
|
|
72
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 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.
|
package/lib/index.d.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { Storage } from 'webextension-polyfill';
|
|
2
|
-
|
|
3
|
-
declare type AnySchema = Record<string, any>;
|
|
4
|
-
/**
|
|
5
|
-
* Call this method to remove the listener that was added.
|
|
6
|
-
*/
|
|
7
|
-
declare type RemoveListenerCallback = () => void;
|
|
8
|
-
declare type OnChangeCallback<TSchema extends AnySchema, TKey extends keyof TSchema = keyof TSchema> = (newValue: TSchema[TKey], oldValue: TSchema[TKey] | null) => void;
|
|
9
|
-
/**
|
|
10
|
-
* This is the interface for the storage objects exported from the package. It is similar to `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
|
-
/**
|
|
17
|
-
* Clear all values from storage.
|
|
18
|
-
*/
|
|
19
|
-
clear(): Promise<void>;
|
|
20
|
-
/**
|
|
21
|
-
* Return the value in storage or `null` if the item is missing.
|
|
22
|
-
*/
|
|
23
|
-
getItem<TKey extends keyof TSchema>(key: TKey): Promise<Required<TSchema>[TKey] | null>;
|
|
24
|
-
/**
|
|
25
|
-
* Set the key and value in storage. Unlike with `localStorage`, passing `null` or `undefined`
|
|
26
|
-
* will result in `null` being stored for the value.
|
|
27
|
-
*/
|
|
28
|
-
setItem<TKey extends keyof TSchema>(key: TKey, value: TSchema[TKey]): Promise<void>;
|
|
29
|
-
/**
|
|
30
|
-
* Remove the value from storage at a key.
|
|
31
|
-
*/
|
|
32
|
-
removeItem<TKey extends keyof TSchema>(key: TKey): Promise<void>;
|
|
33
|
-
/**
|
|
34
|
-
* Add a callback that is executed when a key is changed in the storage. Listeners are executed in
|
|
35
|
-
* parallel, but the first listeners added are always started first.
|
|
36
|
-
*
|
|
37
|
-
* Returns a method that, when called, removes the listener that was added.
|
|
38
|
-
*/
|
|
39
|
-
onChange<TKey extends keyof TSchema>(key: TKey, cb: OnChangeCallback<TSchema, TKey>): RemoveListenerCallback;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Create a storage instance with an optional schema, `TSchema`, for type safety.
|
|
44
|
-
*
|
|
45
|
-
* @param storage The storage to to use. Either `Browser.storage.local`, `Browser.storage.sync`, or `Browser.storage.managed`.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* import browser from 'webextension-polyfill';
|
|
49
|
-
*
|
|
50
|
-
* interface Schema {
|
|
51
|
-
* installDate: number;
|
|
52
|
-
* }
|
|
53
|
-
* const extensionStorage = defineExtensionStorage<Schema>(browser.storage.local);
|
|
54
|
-
*
|
|
55
|
-
* const date = await extensionStorage.getItem("installDate");
|
|
56
|
-
*/
|
|
57
|
-
declare function defineExtensionStorage<TSchema extends AnySchema = AnySchema>(storage: Storage.StorageArea): ExtensionStorage<TSchema>;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area.
|
|
61
|
-
*/
|
|
62
|
-
declare const localExtStorage: ExtensionStorage<AnySchema>;
|
|
63
|
-
/**
|
|
64
|
-
* An implementation of `ExtensionStorage` based on the `browser.storage.sync` storage area.
|
|
65
|
-
*/
|
|
66
|
-
declare const syncExtStorage: ExtensionStorage<AnySchema>;
|
|
67
|
-
/**
|
|
68
|
-
* An implementation of `ExtensionStorage` based on the `browser.storage.managed` storage area.
|
|
69
|
-
*/
|
|
70
|
-
declare const managedExtStorage: ExtensionStorage<AnySchema>;
|
|
71
|
-
|
|
72
|
-
export { ExtensionStorage, defineExtensionStorage, localExtStorage, managedExtStorage, syncExtStorage };
|
package/lib/index.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
var __async = (__this, __arguments, generator) => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
var fulfilled = (value) => {
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
// src/defineExtensionStorage.ts
|
|
23
|
-
import browser from "webextension-polyfill";
|
|
24
|
-
function defineExtensionStorage(storage) {
|
|
25
|
-
const onStorageChanged = (changes) => __async(this, null, function* () {
|
|
26
|
-
const work = listeners.map(({ key, cb }) => {
|
|
27
|
-
if (!(key in changes))
|
|
28
|
-
return;
|
|
29
|
-
const { newValue, oldValue } = changes[key];
|
|
30
|
-
if (newValue === oldValue)
|
|
31
|
-
return;
|
|
32
|
-
return cb(newValue, oldValue);
|
|
33
|
-
});
|
|
34
|
-
yield Promise.all(work);
|
|
35
|
-
});
|
|
36
|
-
let listeners = [];
|
|
37
|
-
function addListener(listener) {
|
|
38
|
-
if (listeners.length === 0) {
|
|
39
|
-
storage.onChanged.addListener(onStorageChanged);
|
|
40
|
-
}
|
|
41
|
-
listeners.push(listener);
|
|
42
|
-
}
|
|
43
|
-
function removeListener(listener) {
|
|
44
|
-
const i = listeners.indexOf(listener);
|
|
45
|
-
if (i >= 0)
|
|
46
|
-
listeners.splice(i, 1);
|
|
47
|
-
if (listeners.length === 0) {
|
|
48
|
-
browser.storage.onChanged.removeListener(onStorageChanged);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return {
|
|
52
|
-
clear() {
|
|
53
|
-
return storage.clear();
|
|
54
|
-
},
|
|
55
|
-
getItem(key) {
|
|
56
|
-
return storage.get(key).then((res) => {
|
|
57
|
-
var _a;
|
|
58
|
-
return (_a = res[key]) != null ? _a : null;
|
|
59
|
-
});
|
|
60
|
-
},
|
|
61
|
-
setItem(key, value) {
|
|
62
|
-
return storage.set({ [key]: value != null ? value : null });
|
|
63
|
-
},
|
|
64
|
-
removeItem(key) {
|
|
65
|
-
return storage.remove(key);
|
|
66
|
-
},
|
|
67
|
-
onChange(key, cb) {
|
|
68
|
-
const listener = {
|
|
69
|
-
key,
|
|
70
|
-
cb
|
|
71
|
-
};
|
|
72
|
-
addListener(listener);
|
|
73
|
-
return () => removeListener(listener);
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// src/index.ts
|
|
79
|
-
import Browser from "webextension-polyfill";
|
|
80
|
-
var localExtStorage = defineExtensionStorage(Browser.storage.local);
|
|
81
|
-
var syncExtStorage = defineExtensionStorage(Browser.storage.sync);
|
|
82
|
-
var managedExtStorage = defineExtensionStorage(Browser.storage.managed);
|
|
83
|
-
export {
|
|
84
|
-
defineExtensionStorage,
|
|
85
|
-
localExtStorage,
|
|
86
|
-
managedExtStorage,
|
|
87
|
-
syncExtStorage
|
|
88
|
-
};
|