@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 CHANGED
@@ -1,7 +1,10 @@
1
1
  # `@webext-core/proxy-service`
2
2
 
3
- 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).
3
+ A type-safe wrapper around the web extension messaging APIs that lets you call a
4
+ function from anywhere, but execute it in the background. Supports all browsers
5
+ (Chrome, Firefox, Safari, etc).
4
6
 
5
7
  ## Get Started
6
8
 
7
- See [documentation](https://webext-core.aklinker1.io/guide/proxy-service/) to get started!
9
+ See [documentation](https://webext-core.aklinker1.io/proxy-service/installation)
10
+ to get started!
package/lib/index.cjs ADDED
@@ -0,0 +1,67 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _webext_core_messaging = require("@webext-core/messaging");
3
+ //#region src/index.ts
4
+ /**
5
+ * Create a proxy service that uses the message APIs to proxy function calls to the real service
6
+ * registered in the background with `registerService`.
7
+ *
8
+ * @param key The service key to listen for, must be the same string as the one used in
9
+ * `registerService`.
10
+ */
11
+ function createProxyService(key, config) {
12
+ return createProxy(defineProxyMessaging(key, config));
13
+ }
14
+ /**
15
+ * Sets up message listeners that receive messages from proxies created with `createProxyService`.
16
+ *
17
+ * @param key The service key to listen for, must be the same string as the one used in
18
+ * `createProxyService`.
19
+ * @param realService The real service instance that will handle the requests.
20
+ */
21
+ function registerService(key, realService, config) {
22
+ const messenger = defineProxyMessaging(key, config);
23
+ return messenger.onMessage(messenger.messageKey, ({ data }) => {
24
+ const method = data.path == null ? realService : get(realService ?? {}, data.path);
25
+ if (method) return Promise.resolve(method.bind(realService)(...data.args));
26
+ });
27
+ }
28
+ function isProxyService(obj) {
29
+ return obj?.[ProxyServiceSymbol] === true;
30
+ }
31
+ function defineProxyMessaging(key, config) {
32
+ const messaging = (0, _webext_core_messaging.defineExtensionMessaging)(config);
33
+ return {
34
+ messageKey: `proxy-service.${key}`,
35
+ ...messaging
36
+ };
37
+ }
38
+ /**
39
+ * Create and returns a "deep" proxy. Every property that is accessed returns another proxy, and
40
+ * when a function is called at any depth, a message is sent to the background.
41
+ */
42
+ function createProxy(messenger, path) {
43
+ const wrapped = (() => {});
44
+ const proxy = new Proxy(wrapped, {
45
+ async apply(_target, _thisArg, args) {
46
+ return await messenger.sendMessage(messenger.messageKey, {
47
+ path,
48
+ args
49
+ });
50
+ },
51
+ get(target, propertyName, receiver) {
52
+ if (typeof propertyName === "symbol") return Reflect.get(target, propertyName, receiver);
53
+ return createProxy(messenger, path == null ? [propertyName] : path.concat([propertyName]));
54
+ }
55
+ });
56
+ proxy[ProxyServiceSymbol] = true;
57
+ return proxy;
58
+ }
59
+ const ProxyServiceSymbol = Symbol();
60
+ function get(obj, path) {
61
+ if (path.length === 0) return obj;
62
+ return path.reduce((acc, key) => acc?.[key], obj);
63
+ }
64
+ //#endregion
65
+ exports.createProxyService = createProxyService;
66
+ exports.isProxyService = isProxyService;
67
+ exports.registerService = registerService;
@@ -1,56 +1,52 @@
1
- import { ExtensionMessagingConfig, RemoveListenerCallback } from '@webext-core/messaging';
2
- import { DeepAsync, Service } from './types.js';
1
+ import { DeepAsync, Service } from "./types.cjs";
2
+ import { ExtensionMessagingConfig, RemoveListenerCallback } from "@webext-core/messaging";
3
3
 
4
+ //#region src/index.d.ts
4
5
  /**
5
6
  * A type that ensures a service has only async methods.
6
7
  *
7
- * - ***If all methods are async***, it returns the original type.
8
- * - ***If the service has non-async methods***, it returns a `DeepAsync` of the service.
8
+ * - _**If all methods are async**_, it returns the original type.
9
+ * - _**If the service has non-async methods**_, it returns a `DeepAsync` of the service.
9
10
  */
10
11
  type ProxyService<T> = T extends DeepAsync<T> ? T : DeepAsync<T>;
11
12
  /**
12
- * Create a proxy service that uses the message APIs to proxy function calls to
13
- * the real service registered in the background with `registerService`.
13
+ * Create a proxy service that uses the message APIs to proxy function calls to the real service
14
+ * registered in the background with `registerService`.
14
15
  *
15
- * @param key The service key to listen for, must be the same string as the one
16
- * used in `registerService`.
16
+ * @param key The service key to listen for, must be the same string as the one used in
17
+ * `registerService`.
17
18
  */
18
19
  declare function createProxyService<T extends Service>(key: ProxyServiceKey<T> | string, config?: ExtensionMessagingConfig): ProxyService<T>;
19
20
  /**
20
- * Sets up message listeners that receive messages from proxies created with
21
- * `createProxyService`.
21
+ * Sets up message listeners that receive messages from proxies created with `createProxyService`.
22
22
  *
23
- * @param key The service key to listen for, must be the same string as the one
24
- * used in `createProxyService`.
23
+ * @param key The service key to listen for, must be the same string as the one used in
24
+ * `createProxyService`.
25
25
  * @param realService The real service instance that will handle the requests.
26
26
  */
27
27
  declare function registerService<T extends Service, K extends string = ProxyServiceKey<T> | string>(key: K, realService: T, config?: ExtensionMessagingConfig): RemoveListenerCallback;
28
28
  declare function isProxyService<T>(obj: unknown): obj is ProxyService<T>;
29
- interface ProxyServiceConstraint<_> {
30
- }
29
+ interface ProxyServiceConstraint<_> {}
31
30
  /**
32
- * Used to constrain a service's type between calls to `createProxyService` and
33
- * `registerService`.
31
+ * Used to constrain a service's type between calls to `createProxyService` and `registerService`.
34
32
  *
35
33
  * @example
36
- * ```ts
37
- * // utils/proxy-service-keys.ts
38
- * import type { ProxyServiceKey } from '@webext-core/proxy-service';
39
- * import type { MathService } from './math-service';
34
+ * // utils/proxy-service-keys.ts
35
+ * import type { ProxyServiceKey } from '@webext-core/proxy-service';
36
+ * import type { MathService } from './math-service';
40
37
  *
41
- * export const PROXY_SERVICE_KEY = 'math-service' as ProxyServiceKey<MathService>;
38
+ * export const PROXY_SERVICE_KEY = 'math-service' as ProxyServiceKey<MathService>;
42
39
  *
43
- * // background.ts
44
- * import { PROXY_SERVICE_KEY } from './utils/proxy-service-keys';
40
+ * // background.ts
41
+ * import { PROXY_SERVICE_KEY } from './utils/proxy-service-keys';
45
42
  *
46
- * registerService(PROXY_SERVICE_KEY, new MathService())
43
+ * registerService(PROXY_SERVICE_KEY, new MathService());
47
44
  *
48
- * // content-script.ts
49
- * import { PROXY_SERVICE_KEY } from './utils/proxy-service-keys';
45
+ * // content-script.ts
46
+ * import { PROXY_SERVICE_KEY } from './utils/proxy-service-keys';
50
47
  *
51
- * const mathService = await createProxyService(PROXY_SERVICE_KEY);
52
- * ```
48
+ * const mathService = await createProxyService(PROXY_SERVICE_KEY);
53
49
  */
54
50
  type ProxyServiceKey<T> = string & ProxyServiceConstraint<T>;
55
-
56
- export { ProxyService, ProxyServiceKey, createProxyService, isProxyService, registerService };
51
+ //#endregion
52
+ export { ProxyService, ProxyServiceKey, createProxyService, isProxyService, registerService };
@@ -0,0 +1,52 @@
1
+ import { DeepAsync, Service } from "./types.mjs";
2
+ import { ExtensionMessagingConfig, RemoveListenerCallback } from "@webext-core/messaging";
3
+
4
+ //#region src/index.d.ts
5
+ /**
6
+ * A type that ensures a service has only async methods.
7
+ *
8
+ * - _**If all methods are async**_, it returns the original type.
9
+ * - _**If the service has non-async methods**_, it returns a `DeepAsync` of the service.
10
+ */
11
+ type ProxyService<T> = T extends DeepAsync<T> ? T : DeepAsync<T>;
12
+ /**
13
+ * Create a proxy service that uses the message APIs to proxy function calls to the real service
14
+ * registered in the background with `registerService`.
15
+ *
16
+ * @param key The service key to listen for, must be the same string as the one used in
17
+ * `registerService`.
18
+ */
19
+ declare function createProxyService<T extends Service>(key: ProxyServiceKey<T> | string, config?: ExtensionMessagingConfig): ProxyService<T>;
20
+ /**
21
+ * Sets up message listeners that receive messages from proxies created with `createProxyService`.
22
+ *
23
+ * @param key The service key to listen for, must be the same string as the one used in
24
+ * `createProxyService`.
25
+ * @param realService The real service instance that will handle the requests.
26
+ */
27
+ declare function registerService<T extends Service, K extends string = ProxyServiceKey<T> | string>(key: K, realService: T, config?: ExtensionMessagingConfig): RemoveListenerCallback;
28
+ declare function isProxyService<T>(obj: unknown): obj is ProxyService<T>;
29
+ interface ProxyServiceConstraint<_> {}
30
+ /**
31
+ * Used to constrain a service's type between calls to `createProxyService` and `registerService`.
32
+ *
33
+ * @example
34
+ * // utils/proxy-service-keys.ts
35
+ * import type { ProxyServiceKey } from '@webext-core/proxy-service';
36
+ * import type { MathService } from './math-service';
37
+ *
38
+ * export const PROXY_SERVICE_KEY = 'math-service' as ProxyServiceKey<MathService>;
39
+ *
40
+ * // background.ts
41
+ * import { PROXY_SERVICE_KEY } from './utils/proxy-service-keys';
42
+ *
43
+ * registerService(PROXY_SERVICE_KEY, new MathService());
44
+ *
45
+ * // content-script.ts
46
+ * import { PROXY_SERVICE_KEY } from './utils/proxy-service-keys';
47
+ *
48
+ * const mathService = await createProxyService(PROXY_SERVICE_KEY);
49
+ */
50
+ type ProxyServiceKey<T> = string & ProxyServiceConstraint<T>;
51
+ //#endregion
52
+ export { ProxyService, ProxyServiceKey, createProxyService, isProxyService, registerService };