@webext-core/proxy-service 3.0.0 → 3.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.d.ts → dist/index.d.mts} +26 -31
- package/dist/index.iife.js +1 -0
- package/dist/index.mjs +64 -0
- package/dist/types.d.mts +12 -0
- package/dist/types.mjs +1 -0
- package/package.json +39 -34
- package/lib/index.global.js +0 -1478
- package/lib/index.js +0 -95
- package/lib/types.d.ts +0 -15
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 };
|