@webext-core/proxy-service 1.0.2 → 1.1.0
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 +3 -103
- package/lib/index.cjs +26 -12
- package/lib/index.d.ts +10 -7
- package/lib/index.global.js +1363 -0
- package/lib/index.js +26 -12
- package/package.json +12 -11
package/lib/index.js
CHANGED
|
@@ -45,15 +45,39 @@ function isBackgroundServiceWorker() {
|
|
|
45
45
|
|
|
46
46
|
// src/defineProxyService.ts
|
|
47
47
|
import { defineExtensionMessaging } from "@webext-core/messaging";
|
|
48
|
+
import get from "get-value";
|
|
48
49
|
function defineProxyService(name, init, config) {
|
|
49
50
|
let service;
|
|
50
51
|
const messageKey = `proxy-service.${name}`;
|
|
51
52
|
const { onMessage, sendMessage } = defineExtensionMessaging(config);
|
|
53
|
+
function createProxy(path) {
|
|
54
|
+
const wrapped = () => {
|
|
55
|
+
};
|
|
56
|
+
const proxy = new Proxy(wrapped, {
|
|
57
|
+
apply(_target, _thisArg, args) {
|
|
58
|
+
return __async(this, null, function* () {
|
|
59
|
+
const res = yield sendMessage(messageKey, {
|
|
60
|
+
path,
|
|
61
|
+
args
|
|
62
|
+
});
|
|
63
|
+
return res;
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
get(target, propertyName, receiver) {
|
|
67
|
+
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
68
|
+
return Reflect.get(target, propertyName, receiver);
|
|
69
|
+
}
|
|
70
|
+
return createProxy(path == null ? propertyName : `${path}.${propertyName}`);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
proxy.__proxy = true;
|
|
74
|
+
return proxy;
|
|
75
|
+
}
|
|
52
76
|
return [
|
|
53
77
|
function registerService(...args) {
|
|
54
78
|
service = init(...args);
|
|
55
79
|
onMessage(messageKey, ({ data }) => {
|
|
56
|
-
const method =
|
|
80
|
+
const method = data.path == null ? service : get(service != null ? service : {}, data.path);
|
|
57
81
|
if (method)
|
|
58
82
|
return Promise.resolve(method.bind(service)(...data.args));
|
|
59
83
|
});
|
|
@@ -61,17 +85,7 @@ function defineProxyService(name, init, config) {
|
|
|
61
85
|
},
|
|
62
86
|
function getService() {
|
|
63
87
|
if (!isBackground())
|
|
64
|
-
return
|
|
65
|
-
get(_, path) {
|
|
66
|
-
return (...args) => __async(this, null, function* () {
|
|
67
|
-
const res = yield sendMessage(messageKey, {
|
|
68
|
-
method: path,
|
|
69
|
-
args
|
|
70
|
-
});
|
|
71
|
-
return res;
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
});
|
|
88
|
+
return createProxy();
|
|
75
89
|
if (service == null) {
|
|
76
90
|
throw Error(
|
|
77
91
|
`Failed to get an instance of ${name}: in background, but registerService has not been called. Did you forget to call registerService?`
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webext-core/proxy-service",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "A type-safe wrapper around the web extension messaging APIs that lets you call a function from anywhere, but execute it in the background. Supports all browsers (Chrome, Firefox, Safari, etc)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"web-extension",
|
|
@@ -9,14 +9,13 @@
|
|
|
9
9
|
"chrome-extension",
|
|
10
10
|
"webext",
|
|
11
11
|
"web-ext",
|
|
12
|
-
"web",
|
|
13
12
|
"chrome",
|
|
13
|
+
"firefox",
|
|
14
|
+
"safari",
|
|
14
15
|
"browser",
|
|
15
16
|
"extension",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"service",
|
|
19
|
-
"webextension-polyfill"
|
|
17
|
+
"rpc",
|
|
18
|
+
"service"
|
|
20
19
|
],
|
|
21
20
|
"homepage": "https://github.com/aklinker1/webext-core/tree/main/packages/proxy-service",
|
|
22
21
|
"repository": {
|
|
@@ -41,23 +40,25 @@
|
|
|
41
40
|
}
|
|
42
41
|
},
|
|
43
42
|
"dependencies": {
|
|
43
|
+
"get-value": "^3.0.1",
|
|
44
44
|
"webextension-polyfill": "^0.10.0",
|
|
45
|
-
"@webext-core/messaging": "1.
|
|
45
|
+
"@webext-core/messaging": "1.2.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
+
"@types/get-value": "^3.0.3",
|
|
48
49
|
"@types/webextension-polyfill": "^0.9.1",
|
|
49
50
|
"@vitest/coverage-c8": "^0.24.5",
|
|
50
51
|
"tsup": "^6.4.0",
|
|
51
52
|
"typescript": "^4.8.4",
|
|
52
|
-
"vitest": "
|
|
53
|
-
"@webext-core/fake-browser": "1.0
|
|
53
|
+
"vitest": "0.28.0",
|
|
54
|
+
"@webext-core/fake-browser": "1.2.0",
|
|
54
55
|
"tsconfig": "0.0.0"
|
|
55
56
|
},
|
|
56
57
|
"peerDependencies": {
|
|
57
58
|
"webextension-polyfill": "^0.10.0"
|
|
58
59
|
},
|
|
59
60
|
"scripts": {
|
|
60
|
-
"build": "tsup src/index.ts --clean --out-dir lib --format esm,cjs --
|
|
61
|
+
"build": "tsup src/index.ts --clean --out-dir lib --dts --format esm,cjs,iife --global-name webExtCoreProxyService",
|
|
61
62
|
"build:dependencies": "cd ../.. && turbo run build --filter=@webext-core/proxy-service^...",
|
|
62
63
|
"test": "vitest -r src",
|
|
63
64
|
"test:coverage": "vitest run -r src --coverage",
|