@webext-core/proxy-service 1.0.3 → 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 +164 -31
- package/lib/index.js +26 -12
- package/package.json +11 -10
package/README.md
CHANGED
|
@@ -1,107 +1,7 @@
|
|
|
1
1
|
# `@webext-core/proxy-service`
|
|
2
2
|
|
|
3
|
-
|
|
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).
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
pnpm i @webext-core/proxy-service
|
|
7
|
-
```
|
|
5
|
+
## Get Started
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
1. Define the service that will be executed in the background:
|
|
12
|
-
|
|
13
|
-
###### MathService.ts
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
class MathService {
|
|
17
|
-
factorial(x) {
|
|
18
|
-
return x === 1 ? x : this.factorial(x - 1);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const [registerMathService, getMathService] = defineProxyService(
|
|
23
|
-
'MathService',
|
|
24
|
-
() => new MathService(),
|
|
25
|
-
);
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
2. Register your new service at the beginning of your background script:
|
|
29
|
-
|
|
30
|
-
###### background.ts
|
|
31
|
-
|
|
32
|
-
```ts
|
|
33
|
-
import { registerMathService } from './MathService';
|
|
34
|
-
|
|
35
|
-
registerMathService();
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
3. Use `getService` to get a proxied instance of your service, and call methods on it like you would with the real service.
|
|
39
|
-
|
|
40
|
-
###### popup.ts
|
|
41
|
-
|
|
42
|
-
```ts
|
|
43
|
-
import { getMathService } from './MathService';
|
|
44
|
-
|
|
45
|
-
// getMathService will return a "proxy" service when NOT in the background
|
|
46
|
-
const mathService = getMathService();
|
|
47
|
-
|
|
48
|
-
// Use the proxy just like you would the real service!
|
|
49
|
-
await mathService.factorial(100);
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
### Another Example: IndexedDB
|
|
53
|
-
|
|
54
|
-
Since the same IndexedDB database is not available in every JS context of an extension, it's common to use the IndexedDB instance in the background script as a database for browser extensions.
|
|
55
|
-
|
|
56
|
-
`@webext-core/proxy-service` is perfect for this! Here we're using [`idb`](https://www.npmjs.com/package/idb) to simplify it's usage.
|
|
57
|
-
|
|
58
|
-
###### TodoRepo.ts
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
import { IDBPDatabase } from 'idb';
|
|
62
|
-
|
|
63
|
-
interface ITodoRepo {
|
|
64
|
-
getOne: (id: string) => Promise<Todo>;
|
|
65
|
-
getAll: () => Promise<Todo[]>;
|
|
66
|
-
create: (todo: Todo) => Promise<void>;
|
|
67
|
-
update: (todo: Todo) => Promise<void>;
|
|
68
|
-
delete: (todo: Todo) => Promise<void>;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function createIdbTodoRepo(idb: Promise<IDBPDatabase>): ITodoRepo {
|
|
72
|
-
return {
|
|
73
|
-
getOne: async id => (await idb).get('todos', id),
|
|
74
|
-
getAll: async () => (await idb).getAll('todos'),
|
|
75
|
-
create: async todo => (await idb).add('todos', todo),
|
|
76
|
-
update: async todo => (await idb).put('todos', todo),
|
|
77
|
-
delete: async todo => (await idb).delete('todos', todo.id),
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const [registerTodoRepo, getTodoRepo] = defineProxyService('TodoRepo', createIdbTodoRepo);
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
When registering a service with a dependency, you need to pass that dependency into the `registerService` method:
|
|
85
|
-
|
|
86
|
-
###### background.ts
|
|
87
|
-
|
|
88
|
-
```ts
|
|
89
|
-
import { registerTodoRepo } from './TodoRepo';
|
|
90
|
-
import { openDB } from 'idb';
|
|
91
|
-
|
|
92
|
-
const idb = openDB(...);
|
|
93
|
-
|
|
94
|
-
registerTodoRepo(idb);
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
Then you can use `getService` to get a instance of the service anywhere thoughout your extension!
|
|
98
|
-
|
|
99
|
-
###### popup.ts
|
|
100
|
-
|
|
101
|
-
```ts
|
|
102
|
-
import { getTodoRepo } from './TodoRepo';
|
|
103
|
-
|
|
104
|
-
const todoRepo = getTodoRepo();
|
|
105
|
-
|
|
106
|
-
const todos = await todoRepo.getAll();
|
|
107
|
-
```
|
|
7
|
+
See [documentation](https://webext-core.aklinker1.io/proxy-service) to get started!
|
package/lib/index.cjs
CHANGED
|
@@ -76,15 +76,39 @@ function isBackgroundServiceWorker() {
|
|
|
76
76
|
|
|
77
77
|
// src/defineProxyService.ts
|
|
78
78
|
var import_messaging = require("@webext-core/messaging");
|
|
79
|
+
var import_get_value = __toESM(require("get-value"), 1);
|
|
79
80
|
function defineProxyService(name, init, config) {
|
|
80
81
|
let service;
|
|
81
82
|
const messageKey = `proxy-service.${name}`;
|
|
82
83
|
const { onMessage, sendMessage } = (0, import_messaging.defineExtensionMessaging)(config);
|
|
84
|
+
function createProxy(path) {
|
|
85
|
+
const wrapped = () => {
|
|
86
|
+
};
|
|
87
|
+
const proxy = new Proxy(wrapped, {
|
|
88
|
+
apply(_target, _thisArg, args) {
|
|
89
|
+
return __async(this, null, function* () {
|
|
90
|
+
const res = yield sendMessage(messageKey, {
|
|
91
|
+
path,
|
|
92
|
+
args
|
|
93
|
+
});
|
|
94
|
+
return res;
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
get(target, propertyName, receiver) {
|
|
98
|
+
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
99
|
+
return Reflect.get(target, propertyName, receiver);
|
|
100
|
+
}
|
|
101
|
+
return createProxy(path == null ? propertyName : `${path}.${propertyName}`);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
proxy.__proxy = true;
|
|
105
|
+
return proxy;
|
|
106
|
+
}
|
|
83
107
|
return [
|
|
84
108
|
function registerService(...args) {
|
|
85
109
|
service = init(...args);
|
|
86
110
|
onMessage(messageKey, ({ data }) => {
|
|
87
|
-
const method =
|
|
111
|
+
const method = data.path == null ? service : (0, import_get_value.default)(service != null ? service : {}, data.path);
|
|
88
112
|
if (method)
|
|
89
113
|
return Promise.resolve(method.bind(service)(...data.args));
|
|
90
114
|
});
|
|
@@ -92,17 +116,7 @@ function defineProxyService(name, init, config) {
|
|
|
92
116
|
},
|
|
93
117
|
function getService() {
|
|
94
118
|
if (!isBackground())
|
|
95
|
-
return
|
|
96
|
-
get(_, path) {
|
|
97
|
-
return (...args) => __async(this, null, function* () {
|
|
98
|
-
const res = yield sendMessage(messageKey, {
|
|
99
|
-
method: path,
|
|
100
|
-
args
|
|
101
|
-
});
|
|
102
|
-
return res;
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
});
|
|
119
|
+
return createProxy();
|
|
106
120
|
if (service == null) {
|
|
107
121
|
throw Error(
|
|
108
122
|
`Failed to get an instance of ${name}: in background, but registerService has not been called. Did you forget to call registerService?`
|
package/lib/index.d.ts
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import { ExtensionMessagingConfig } from '@webext-core/messaging';
|
|
2
2
|
|
|
3
3
|
declare type Proimsify<T> = T extends Promise<any> ? T : Promise<T>;
|
|
4
|
-
declare type Service =
|
|
4
|
+
declare type Service = ((...args: any[]) => Promise<any>) | {
|
|
5
|
+
[key: string]: any | Service;
|
|
6
|
+
};
|
|
5
7
|
/**
|
|
6
|
-
* Make all functions of
|
|
8
|
+
* Make all functions at all nested levels of an object async
|
|
7
9
|
*/
|
|
8
|
-
declare type
|
|
9
|
-
[fn in keyof TService]: TService[fn] extends (...args: any[]) => any ?
|
|
10
|
+
declare type DeepAsync<TService> = TService extends (...args: any) => any ? ToAsyncFunction<TService> : {
|
|
11
|
+
[fn in keyof TService]: TService[fn] extends (...args: any[]) => any ? ToAsyncFunction<TService[fn]> : TService[fn] extends {
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
} ? DeepAsync<TService[fn]> : never;
|
|
10
14
|
};
|
|
15
|
+
declare type ToAsyncFunction<T extends (...args: any) => any> = (...args: Parameters<T>) => Proimsify<ReturnType<T>>;
|
|
11
16
|
interface ProxyServiceConfig extends ExtensionMessagingConfig {
|
|
12
17
|
}
|
|
13
18
|
|
|
14
|
-
declare
|
|
15
|
-
declare type GetService<TService extends Service> = () => AsyncService<TService>;
|
|
16
|
-
declare function defineProxyService<TService extends Service, TArgs extends any[]>(name: string, init: (...args: TArgs) => TService, config?: ProxyServiceConfig): [registerService: RegisterService<TService, TArgs>, getService: GetService<TService>];
|
|
19
|
+
declare function defineProxyService<TService extends Service, TArgs extends any[]>(name: string, init: (...args: TArgs) => TService, config?: ProxyServiceConfig): [registerService: (...args: TArgs) => TService, getService: () => DeepAsync<TService>];
|
|
17
20
|
|
|
18
21
|
export { ProxyServiceConfig, defineProxyService };
|
package/lib/index.global.js
CHANGED
|
@@ -1042,6 +1042,104 @@ var webExtCoreProxyService = (() => {
|
|
|
1042
1042
|
}
|
|
1043
1043
|
});
|
|
1044
1044
|
|
|
1045
|
+
// ../../node_modules/.pnpm/isobject@3.0.1/node_modules/isobject/index.js
|
|
1046
|
+
var require_isobject = __commonJS({
|
|
1047
|
+
"../../node_modules/.pnpm/isobject@3.0.1/node_modules/isobject/index.js"(exports, module) {
|
|
1048
|
+
"use strict";
|
|
1049
|
+
module.exports = function isObject(val) {
|
|
1050
|
+
return val != null && typeof val === "object" && Array.isArray(val) === false;
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
});
|
|
1054
|
+
|
|
1055
|
+
// ../../node_modules/.pnpm/get-value@3.0.1/node_modules/get-value/index.js
|
|
1056
|
+
var require_get_value = __commonJS({
|
|
1057
|
+
"../../node_modules/.pnpm/get-value@3.0.1/node_modules/get-value/index.js"(exports, module) {
|
|
1058
|
+
var isObject = require_isobject();
|
|
1059
|
+
module.exports = function(target, path, options) {
|
|
1060
|
+
if (!isObject(options)) {
|
|
1061
|
+
options = { default: options };
|
|
1062
|
+
}
|
|
1063
|
+
if (!isValidObject(target)) {
|
|
1064
|
+
return typeof options.default !== "undefined" ? options.default : target;
|
|
1065
|
+
}
|
|
1066
|
+
if (typeof path === "number") {
|
|
1067
|
+
path = String(path);
|
|
1068
|
+
}
|
|
1069
|
+
const isArray = Array.isArray(path);
|
|
1070
|
+
const isString = typeof path === "string";
|
|
1071
|
+
const splitChar = options.separator || ".";
|
|
1072
|
+
const joinChar = options.joinChar || (typeof splitChar === "string" ? splitChar : ".");
|
|
1073
|
+
if (!isString && !isArray) {
|
|
1074
|
+
return target;
|
|
1075
|
+
}
|
|
1076
|
+
if (isString && path in target) {
|
|
1077
|
+
return isValid(path, target, options) ? target[path] : options.default;
|
|
1078
|
+
}
|
|
1079
|
+
let segs = isArray ? path : split(path, splitChar, options);
|
|
1080
|
+
let len = segs.length;
|
|
1081
|
+
let idx = 0;
|
|
1082
|
+
do {
|
|
1083
|
+
let prop = segs[idx];
|
|
1084
|
+
if (typeof prop === "number") {
|
|
1085
|
+
prop = String(prop);
|
|
1086
|
+
}
|
|
1087
|
+
while (prop && prop.slice(-1) === "\\") {
|
|
1088
|
+
prop = join([prop.slice(0, -1), segs[++idx] || ""], joinChar, options);
|
|
1089
|
+
}
|
|
1090
|
+
if (prop in target) {
|
|
1091
|
+
if (!isValid(prop, target, options)) {
|
|
1092
|
+
return options.default;
|
|
1093
|
+
}
|
|
1094
|
+
target = target[prop];
|
|
1095
|
+
} else {
|
|
1096
|
+
let hasProp = false;
|
|
1097
|
+
let n = idx + 1;
|
|
1098
|
+
while (n < len) {
|
|
1099
|
+
prop = join([prop, segs[n++]], joinChar, options);
|
|
1100
|
+
if (hasProp = prop in target) {
|
|
1101
|
+
if (!isValid(prop, target, options)) {
|
|
1102
|
+
return options.default;
|
|
1103
|
+
}
|
|
1104
|
+
target = target[prop];
|
|
1105
|
+
idx = n - 1;
|
|
1106
|
+
break;
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
if (!hasProp) {
|
|
1110
|
+
return options.default;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
} while (++idx < len && isValidObject(target));
|
|
1114
|
+
if (idx === len) {
|
|
1115
|
+
return target;
|
|
1116
|
+
}
|
|
1117
|
+
return options.default;
|
|
1118
|
+
};
|
|
1119
|
+
function join(segs, joinChar, options) {
|
|
1120
|
+
if (typeof options.join === "function") {
|
|
1121
|
+
return options.join(segs);
|
|
1122
|
+
}
|
|
1123
|
+
return segs[0] + joinChar + segs[1];
|
|
1124
|
+
}
|
|
1125
|
+
function split(path, splitChar, options) {
|
|
1126
|
+
if (typeof options.split === "function") {
|
|
1127
|
+
return options.split(path);
|
|
1128
|
+
}
|
|
1129
|
+
return path.split(splitChar);
|
|
1130
|
+
}
|
|
1131
|
+
function isValid(key, target, options) {
|
|
1132
|
+
if (typeof options.isValid === "function") {
|
|
1133
|
+
return options.isValid(key, target);
|
|
1134
|
+
}
|
|
1135
|
+
return true;
|
|
1136
|
+
}
|
|
1137
|
+
function isValidObject(val) {
|
|
1138
|
+
return isObject(val) || Array.isArray(val) || typeof val === "function";
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
});
|
|
1142
|
+
|
|
1045
1143
|
// src/index.ts
|
|
1046
1144
|
var src_exports = {};
|
|
1047
1145
|
__export(src_exports, {
|
|
@@ -1096,22 +1194,28 @@ var webExtCoreProxyService = (() => {
|
|
|
1096
1194
|
};
|
|
1097
1195
|
function defineExtensionMessaging(config) {
|
|
1098
1196
|
let rootListener;
|
|
1197
|
+
function cleanupRootListener() {
|
|
1198
|
+
if (Object.entries(keyListeners).length === 0) {
|
|
1199
|
+
import_webextension_polyfill2.default.runtime.onMessage.removeListener(rootListener);
|
|
1200
|
+
rootListener = void 0;
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1099
1203
|
let keyListeners = {};
|
|
1100
1204
|
let idSeq = Math.floor(Math.random() * 1e4);
|
|
1101
1205
|
function getNextId() {
|
|
1102
1206
|
return idSeq++;
|
|
1103
1207
|
}
|
|
1104
|
-
function onMessage(
|
|
1208
|
+
function onMessage(type, onReceived) {
|
|
1105
1209
|
var _a, _b, _c;
|
|
1106
1210
|
if (rootListener == null) {
|
|
1107
1211
|
(_a = config == null ? void 0 : config.logger) == null ? void 0 : _a.debug(
|
|
1108
|
-
`[messaging] ${
|
|
1212
|
+
`[messaging] ${type} initialized message listener for this context`
|
|
1109
1213
|
);
|
|
1110
1214
|
rootListener = (message, sender) => {
|
|
1111
1215
|
var _a2, _b2, _c2;
|
|
1112
|
-
if (typeof message.
|
|
1216
|
+
if (typeof message.type != "string" || typeof message.timestamp !== "number") {
|
|
1113
1217
|
const err = Error(
|
|
1114
|
-
`[messaging] Unknown message format, must include the '
|
|
1218
|
+
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
1115
1219
|
message
|
|
1116
1220
|
)}`
|
|
1117
1221
|
);
|
|
@@ -1120,8 +1224,8 @@ var webExtCoreProxyService = (() => {
|
|
|
1120
1224
|
}
|
|
1121
1225
|
(_b2 = config == null ? void 0 : config.logger) == null ? void 0 : _b2.debug("[messaging] Received message", message, sender);
|
|
1122
1226
|
const entries = Object.entries(keyListeners);
|
|
1123
|
-
for (const [
|
|
1124
|
-
if (message.
|
|
1227
|
+
for (const [type2, listener] of entries) {
|
|
1228
|
+
if (message.type !== type2)
|
|
1125
1229
|
continue;
|
|
1126
1230
|
(_c2 = config == null ? void 0 : config.logger) == null ? void 0 : _c2.debug(`[messaging] onMessage {id=${message.id}} \u140A\u2500`, message);
|
|
1127
1231
|
const res = listener({
|
|
@@ -1145,34 +1249,31 @@ var webExtCoreProxyService = (() => {
|
|
|
1145
1249
|
});
|
|
1146
1250
|
}
|
|
1147
1251
|
return Promise.resolve({
|
|
1148
|
-
err: `Listener not found for message.type="${message.
|
|
1252
|
+
err: `Listener not found for message.type="${message.type}".
|
|
1149
1253
|
|
|
1150
1254
|
Did you forget to call \`onMessage("getLength", ...)\`?`
|
|
1151
1255
|
});
|
|
1152
1256
|
};
|
|
1153
1257
|
import_webextension_polyfill2.default.runtime.onMessage.addListener(rootListener);
|
|
1154
1258
|
}
|
|
1155
|
-
if (keyListeners[
|
|
1259
|
+
if (keyListeners[type] != null) {
|
|
1156
1260
|
const err = Error(
|
|
1157
|
-
`[messaging] In this JS context, only one listener can be setup for ${
|
|
1261
|
+
`[messaging] In this JS context, only one listener can be setup for ${type}`
|
|
1158
1262
|
);
|
|
1159
1263
|
(_b = config == null ? void 0 : config.logger) == null ? void 0 : _b.error(err);
|
|
1160
1264
|
throw err;
|
|
1161
1265
|
}
|
|
1162
|
-
keyListeners[
|
|
1163
|
-
(_c = config == null ? void 0 : config.logger) == null ? void 0 : _c.log(`[messaging] Added listener for ${
|
|
1266
|
+
keyListeners[type] = onReceived;
|
|
1267
|
+
(_c = config == null ? void 0 : config.logger) == null ? void 0 : _c.log(`[messaging] Added listener for ${type}`);
|
|
1164
1268
|
return function removeListener() {
|
|
1165
|
-
delete keyListeners[
|
|
1166
|
-
|
|
1167
|
-
import_webextension_polyfill2.default.runtime.onMessage.removeListener(rootListener);
|
|
1168
|
-
rootListener = void 0;
|
|
1169
|
-
}
|
|
1269
|
+
delete keyListeners[type];
|
|
1270
|
+
cleanupRootListener();
|
|
1170
1271
|
};
|
|
1171
1272
|
}
|
|
1172
|
-
function sendMessage(
|
|
1273
|
+
function sendMessage(type, data, tabId) {
|
|
1173
1274
|
return __async2(this, null, function* () {
|
|
1174
1275
|
var _a, _b;
|
|
1175
|
-
const message = { id: getNextId(),
|
|
1276
|
+
const message = { id: getNextId(), type, data, timestamp: Date.now() };
|
|
1176
1277
|
(_a = config == null ? void 0 : config.logger) == null ? void 0 : _a.debug(`[messaging] sendMessage {id=${message.id}} \u2500\u1405`, message);
|
|
1177
1278
|
let response;
|
|
1178
1279
|
if (tabId == null)
|
|
@@ -1186,19 +1287,49 @@ Did you forget to call \`onMessage("getLength", ...)\`?`
|
|
|
1186
1287
|
return res;
|
|
1187
1288
|
});
|
|
1188
1289
|
}
|
|
1189
|
-
|
|
1290
|
+
function removeAllMessageListeners() {
|
|
1291
|
+
Object.keys(keyListeners).forEach((type) => {
|
|
1292
|
+
delete keyListeners[type];
|
|
1293
|
+
});
|
|
1294
|
+
cleanupRootListener();
|
|
1295
|
+
}
|
|
1296
|
+
return { onMessage, sendMessage, removeAllMessageListeners };
|
|
1190
1297
|
}
|
|
1191
1298
|
|
|
1192
1299
|
// src/defineProxyService.ts
|
|
1300
|
+
var import_get_value = __toESM(require_get_value(), 1);
|
|
1193
1301
|
function defineProxyService(name, init, config) {
|
|
1194
1302
|
let service;
|
|
1195
1303
|
const messageKey = `proxy-service.${name}`;
|
|
1196
1304
|
const { onMessage, sendMessage } = defineExtensionMessaging(config);
|
|
1305
|
+
function createProxy(path) {
|
|
1306
|
+
const wrapped = () => {
|
|
1307
|
+
};
|
|
1308
|
+
const proxy = new Proxy(wrapped, {
|
|
1309
|
+
apply(_target, _thisArg, args) {
|
|
1310
|
+
return __async(this, null, function* () {
|
|
1311
|
+
const res = yield sendMessage(messageKey, {
|
|
1312
|
+
path,
|
|
1313
|
+
args
|
|
1314
|
+
});
|
|
1315
|
+
return res;
|
|
1316
|
+
});
|
|
1317
|
+
},
|
|
1318
|
+
get(target, propertyName, receiver) {
|
|
1319
|
+
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
1320
|
+
return Reflect.get(target, propertyName, receiver);
|
|
1321
|
+
}
|
|
1322
|
+
return createProxy(path == null ? propertyName : `${path}.${propertyName}`);
|
|
1323
|
+
}
|
|
1324
|
+
});
|
|
1325
|
+
proxy.__proxy = true;
|
|
1326
|
+
return proxy;
|
|
1327
|
+
}
|
|
1197
1328
|
return [
|
|
1198
1329
|
function registerService(...args) {
|
|
1199
1330
|
service = init(...args);
|
|
1200
1331
|
onMessage(messageKey, ({ data }) => {
|
|
1201
|
-
const method =
|
|
1332
|
+
const method = data.path == null ? service : (0, import_get_value.default)(service != null ? service : {}, data.path);
|
|
1202
1333
|
if (method)
|
|
1203
1334
|
return Promise.resolve(method.bind(service)(...data.args));
|
|
1204
1335
|
});
|
|
@@ -1206,17 +1337,7 @@ Did you forget to call \`onMessage("getLength", ...)\`?`
|
|
|
1206
1337
|
},
|
|
1207
1338
|
function getService() {
|
|
1208
1339
|
if (!isBackground())
|
|
1209
|
-
return
|
|
1210
|
-
get(_, path) {
|
|
1211
|
-
return (...args) => __async(this, null, function* () {
|
|
1212
|
-
const res = yield sendMessage(messageKey, {
|
|
1213
|
-
method: path,
|
|
1214
|
-
args
|
|
1215
|
-
});
|
|
1216
|
-
return res;
|
|
1217
|
-
});
|
|
1218
|
-
}
|
|
1219
|
-
});
|
|
1340
|
+
return createProxy();
|
|
1220
1341
|
if (service == null) {
|
|
1221
1342
|
throw Error(
|
|
1222
1343
|
`Failed to get an instance of ${name}: in background, but registerService has not been called. Did you forget to call registerService?`
|
|
@@ -1228,3 +1349,15 @@ Did you forget to call \`onMessage("getLength", ...)\`?`
|
|
|
1228
1349
|
}
|
|
1229
1350
|
return __toCommonJS(src_exports);
|
|
1230
1351
|
})();
|
|
1352
|
+
/*!
|
|
1353
|
+
* get-value <https://github.com/jonschlinkert/get-value>
|
|
1354
|
+
*
|
|
1355
|
+
* Copyright (c) 2014-2018, Jon Schlinkert.
|
|
1356
|
+
* Released under the MIT License.
|
|
1357
|
+
*/
|
|
1358
|
+
/*!
|
|
1359
|
+
* isobject <https://github.com/jonschlinkert/isobject>
|
|
1360
|
+
*
|
|
1361
|
+
* Copyright (c) 2014-2017, Jon Schlinkert.
|
|
1362
|
+
* Released under the MIT License.
|
|
1363
|
+
*/
|
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,16 +40,18 @@
|
|
|
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": {
|