@webext-core/proxy-service 2.0.0-rc.1 → 2.0.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 +5 -2
- package/lib/index.cjs +67 -0
- package/lib/{index.d.ts → index.d.cts} +26 -30
- package/lib/index.d.mts +52 -0
- package/lib/index.global.js +1 -1478
- package/lib/index.mjs +64 -0
- package/lib/types.cjs +0 -0
- package/lib/types.d.cts +12 -0
- package/lib/types.d.mts +12 -0
- package/lib/types.mjs +1 -0
- package/package.json +50 -28
- package/lib/index.js +0 -95
- package/lib/types.d.ts +0 -15
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { defineExtensionMessaging } from "@webext-core/messaging";
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* Create a proxy service that uses the message APIs to proxy function calls to the real service
|
|
5
|
+
* registered in the background with `registerService`.
|
|
6
|
+
*
|
|
7
|
+
* @param key The service key to listen for, must be the same string as the one used in
|
|
8
|
+
* `registerService`.
|
|
9
|
+
*/
|
|
10
|
+
function createProxyService(key, config) {
|
|
11
|
+
return createProxy(defineProxyMessaging(key, config));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Sets up message listeners that receive messages from proxies created with `createProxyService`.
|
|
15
|
+
*
|
|
16
|
+
* @param key The service key to listen for, must be the same string as the one used in
|
|
17
|
+
* `createProxyService`.
|
|
18
|
+
* @param realService The real service instance that will handle the requests.
|
|
19
|
+
*/
|
|
20
|
+
function registerService(key, realService, config) {
|
|
21
|
+
const messenger = defineProxyMessaging(key, config);
|
|
22
|
+
return messenger.onMessage(messenger.messageKey, ({ data }) => {
|
|
23
|
+
const method = data.path == null ? realService : get(realService ?? {}, data.path);
|
|
24
|
+
if (method) return Promise.resolve(method.bind(realService)(...data.args));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function isProxyService(obj) {
|
|
28
|
+
return obj?.[ProxyServiceSymbol] === true;
|
|
29
|
+
}
|
|
30
|
+
function defineProxyMessaging(key, config) {
|
|
31
|
+
const messaging = defineExtensionMessaging(config);
|
|
32
|
+
return {
|
|
33
|
+
messageKey: `proxy-service.${key}`,
|
|
34
|
+
...messaging
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create and returns a "deep" proxy. Every property that is accessed returns another proxy, and
|
|
39
|
+
* when a function is called at any depth, a message is sent to the background.
|
|
40
|
+
*/
|
|
41
|
+
function createProxy(messenger, path) {
|
|
42
|
+
const wrapped = (() => {});
|
|
43
|
+
const proxy = new Proxy(wrapped, {
|
|
44
|
+
async apply(_target, _thisArg, args) {
|
|
45
|
+
return await messenger.sendMessage(messenger.messageKey, {
|
|
46
|
+
path,
|
|
47
|
+
args
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
get(target, propertyName, receiver) {
|
|
51
|
+
if (typeof propertyName === "symbol") return Reflect.get(target, propertyName, receiver);
|
|
52
|
+
return createProxy(messenger, path == null ? [propertyName] : path.concat([propertyName]));
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
proxy[ProxyServiceSymbol] = true;
|
|
56
|
+
return proxy;
|
|
57
|
+
}
|
|
58
|
+
const ProxyServiceSymbol = Symbol();
|
|
59
|
+
function get(obj, path) {
|
|
60
|
+
if (path.length === 0) return obj;
|
|
61
|
+
return path.reduce((acc, key) => acc?.[key], obj);
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
export { createProxyService, isProxyService, registerService };
|
package/lib/types.cjs
ADDED
|
File without changes
|
package/lib/types.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type Proimsify<T> = T extends Promise<any> ? T : Promise<T>;
|
|
3
|
+
type Service = ((...args: any[]) => Promise<any>) | {
|
|
4
|
+
[key: string]: any | Service;
|
|
5
|
+
};
|
|
6
|
+
/** A recursive type that deeply converts all methods in `TService` to be async. */
|
|
7
|
+
type DeepAsync<TService> = TService extends ((...args: any) => any) ? ToAsyncFunction<TService> : TService extends {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
} ? { [fn in keyof TService]: DeepAsync<TService[fn]> } : never;
|
|
10
|
+
type ToAsyncFunction<T extends (...args: any) => any> = (...args: Parameters<T>) => Proimsify<ReturnType<T>>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { DeepAsync, Proimsify, Service };
|
package/lib/types.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type Proimsify<T> = T extends Promise<any> ? T : Promise<T>;
|
|
3
|
+
type Service = ((...args: any[]) => Promise<any>) | {
|
|
4
|
+
[key: string]: any | Service;
|
|
5
|
+
};
|
|
6
|
+
/** A recursive type that deeply converts all methods in `TService` to be async. */
|
|
7
|
+
type DeepAsync<TService> = TService extends ((...args: any) => any) ? ToAsyncFunction<TService> : TService extends {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
} ? { [fn in keyof TService]: DeepAsync<TService[fn]> } : never;
|
|
10
|
+
type ToAsyncFunction<T extends (...args: any) => any> = (...args: Parameters<T>) => Proimsify<ReturnType<T>>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { DeepAsync, Proimsify, Service };
|
package/lib/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,63 +1,85 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webext-core/proxy-service",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
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
|
-
"license": "MIT",
|
|
6
5
|
"keywords": [
|
|
7
|
-
"
|
|
6
|
+
"browser",
|
|
8
7
|
"browser-extension",
|
|
9
|
-
"chrome-extension",
|
|
10
|
-
"webext",
|
|
11
|
-
"web-ext",
|
|
12
8
|
"chrome",
|
|
13
|
-
"
|
|
14
|
-
"safari",
|
|
15
|
-
"browser",
|
|
9
|
+
"chrome-extension",
|
|
16
10
|
"extension",
|
|
11
|
+
"firefox",
|
|
17
12
|
"rpc",
|
|
18
|
-
"
|
|
13
|
+
"safari",
|
|
14
|
+
"service",
|
|
15
|
+
"web-ext",
|
|
16
|
+
"web-extension",
|
|
17
|
+
"webext"
|
|
19
18
|
],
|
|
20
19
|
"homepage": "https://github.com/aklinker1/webext-core/tree/main/packages/proxy-service",
|
|
20
|
+
"license": "MIT",
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
23
|
-
"url": "https://github.com/aklinker1/webext-core",
|
|
23
|
+
"url": "git+https://github.com/aklinker1/webext-core.git",
|
|
24
24
|
"directory": "packages/proxy-service"
|
|
25
25
|
},
|
|
26
|
-
"type": "module",
|
|
27
|
-
"publishConfig": {
|
|
28
|
-
"access": "public"
|
|
29
|
-
},
|
|
30
26
|
"files": [
|
|
31
27
|
"lib"
|
|
32
28
|
],
|
|
29
|
+
"type": "module",
|
|
30
|
+
"sideEffects": false,
|
|
33
31
|
"main": "lib/index.cjs",
|
|
34
|
-
"module": "lib/index.
|
|
35
|
-
"types": "lib/index.d.
|
|
32
|
+
"module": "lib/index.mjs",
|
|
33
|
+
"types": "lib/index.d.mts",
|
|
36
34
|
"exports": {
|
|
37
35
|
".": {
|
|
38
|
-
"
|
|
39
|
-
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./lib/index.d.mts",
|
|
38
|
+
"default": "./lib/index.mjs"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./lib/index.d.cts",
|
|
42
|
+
"default": "./lib/index.cjs"
|
|
43
|
+
}
|
|
40
44
|
},
|
|
41
|
-
"./types":
|
|
45
|
+
"./types": {
|
|
46
|
+
"import": {
|
|
47
|
+
"types": "./lib/types.d.mts",
|
|
48
|
+
"default": "./lib/types.mjs"
|
|
49
|
+
},
|
|
50
|
+
"require": {
|
|
51
|
+
"types": "./lib/types.d.cts",
|
|
52
|
+
"default": "./lib/types.cjs"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
42
58
|
},
|
|
43
59
|
"scripts": {
|
|
44
|
-
"build": "buildc --
|
|
45
|
-
"test": "buildc --deps-only --
|
|
46
|
-
"test:coverage": "buildc --deps-only --
|
|
47
|
-
"check": "
|
|
60
|
+
"build": "buildc -- tsdown",
|
|
61
|
+
"test": "buildc --deps-only -- bun test",
|
|
62
|
+
"test:coverage": "buildc --deps-only -- bun test --coverage",
|
|
63
|
+
"check": "bun run build && check",
|
|
64
|
+
"prepack": "bun run build"
|
|
48
65
|
},
|
|
49
66
|
"devDependencies": {
|
|
50
67
|
"@types/get-value": "^3.0.3",
|
|
51
68
|
"@types/webextension-polyfill": "^0.9.1",
|
|
52
|
-
"@webext-core/fake-browser": "
|
|
53
|
-
"@webext-core/messaging": "
|
|
54
|
-
"
|
|
69
|
+
"@webext-core/fake-browser": "workspace:*",
|
|
70
|
+
"@webext-core/messaging": "workspace:*",
|
|
71
|
+
"publint": "catalog:",
|
|
72
|
+
"tsconfig": "workspace:*",
|
|
73
|
+
"tsdown": "catalog:",
|
|
74
|
+
"typescript": "catalog:",
|
|
55
75
|
"webextension-polyfill": "^0.10.0"
|
|
56
76
|
},
|
|
57
77
|
"peerDependencies": {
|
|
58
|
-
"webextension-polyfill": ">=0.10.0",
|
|
59
78
|
"@webext-core/messaging": ">=1.3.1"
|
|
60
79
|
},
|
|
80
|
+
"engines": {
|
|
81
|
+
"node": "*"
|
|
82
|
+
},
|
|
61
83
|
"buildc": {
|
|
62
84
|
"outDir": "lib"
|
|
63
85
|
}
|
package/lib/index.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
-
var __spreadValues = (a, b) => {
|
|
7
|
-
for (var prop in b || (b = {}))
|
|
8
|
-
if (__hasOwnProp.call(b, prop))
|
|
9
|
-
__defNormalProp(a, prop, b[prop]);
|
|
10
|
-
if (__getOwnPropSymbols)
|
|
11
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
-
if (__propIsEnum.call(b, prop))
|
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
|
14
|
-
}
|
|
15
|
-
return a;
|
|
16
|
-
};
|
|
17
|
-
var __async = (__this, __arguments, generator) => {
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
var fulfilled = (value) => {
|
|
20
|
-
try {
|
|
21
|
-
step(generator.next(value));
|
|
22
|
-
} catch (e) {
|
|
23
|
-
reject(e);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
var rejected = (value) => {
|
|
27
|
-
try {
|
|
28
|
-
step(generator.throw(value));
|
|
29
|
-
} catch (e) {
|
|
30
|
-
reject(e);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// src/index.ts
|
|
39
|
-
import {
|
|
40
|
-
defineExtensionMessaging
|
|
41
|
-
} from "@webext-core/messaging";
|
|
42
|
-
function createProxyService(key, config) {
|
|
43
|
-
return createProxy(defineProxyMessaging(key, config));
|
|
44
|
-
}
|
|
45
|
-
function registerService(key, realService, config) {
|
|
46
|
-
const messenger = defineProxyMessaging(key, config);
|
|
47
|
-
return messenger.onMessage(messenger.messageKey, ({ data }) => {
|
|
48
|
-
const method = data.path == null ? realService : get(realService != null ? realService : {}, data.path);
|
|
49
|
-
if (method)
|
|
50
|
-
return Promise.resolve(method.bind(realService)(...data.args));
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
function isProxyService(obj) {
|
|
54
|
-
return (obj == null ? void 0 : obj[ProxyServiceSymbol]) === true;
|
|
55
|
-
}
|
|
56
|
-
function defineProxyMessaging(key, config) {
|
|
57
|
-
const messaging = defineExtensionMessaging(config);
|
|
58
|
-
return __spreadValues({
|
|
59
|
-
messageKey: `proxy-service.${key}`
|
|
60
|
-
}, messaging);
|
|
61
|
-
}
|
|
62
|
-
function createProxy(messenger, path) {
|
|
63
|
-
const wrapped = () => {
|
|
64
|
-
};
|
|
65
|
-
const proxy = new Proxy(wrapped, {
|
|
66
|
-
apply(_target, _thisArg, args) {
|
|
67
|
-
return __async(this, null, function* () {
|
|
68
|
-
const res = yield messenger.sendMessage(messenger.messageKey, {
|
|
69
|
-
path,
|
|
70
|
-
args
|
|
71
|
-
});
|
|
72
|
-
return res;
|
|
73
|
-
});
|
|
74
|
-
},
|
|
75
|
-
get(target, propertyName, receiver) {
|
|
76
|
-
if (typeof propertyName === "symbol") {
|
|
77
|
-
return Reflect.get(target, propertyName, receiver);
|
|
78
|
-
}
|
|
79
|
-
return createProxy(messenger, path == null ? [propertyName] : path.concat([propertyName]));
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
proxy[ProxyServiceSymbol] = true;
|
|
83
|
-
return proxy;
|
|
84
|
-
}
|
|
85
|
-
var ProxyServiceSymbol = Symbol();
|
|
86
|
-
function get(obj, path) {
|
|
87
|
-
if (path.length === 0)
|
|
88
|
-
return obj;
|
|
89
|
-
return path.reduce((acc, key) => acc == null ? void 0 : acc[key], obj);
|
|
90
|
-
}
|
|
91
|
-
export {
|
|
92
|
-
createProxyService,
|
|
93
|
-
isProxyService,
|
|
94
|
-
registerService
|
|
95
|
-
};
|
package/lib/types.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
type Proimsify<T> = T extends Promise<any> ? T : Promise<T>;
|
|
2
|
-
type Service = ((...args: any[]) => Promise<any>) | {
|
|
3
|
-
[key: string]: any | Service;
|
|
4
|
-
};
|
|
5
|
-
/**
|
|
6
|
-
* A recursive type that deeply converts all methods in `TService` to be async.
|
|
7
|
-
*/
|
|
8
|
-
type DeepAsync<TService> = TService extends (...args: any) => any ? ToAsyncFunction<TService> : TService extends {
|
|
9
|
-
[key: string]: any;
|
|
10
|
-
} ? {
|
|
11
|
-
[fn in keyof TService]: DeepAsync<TService[fn]>;
|
|
12
|
-
} : never;
|
|
13
|
-
type ToAsyncFunction<T extends (...args: any) => any> = (...args: Parameters<T>) => Proimsify<ReturnType<T>>;
|
|
14
|
-
|
|
15
|
-
export { DeepAsync, Proimsify, Service };
|