@webext-core/proxy-service 1.2.0 → 1.2.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 +1 -1
- package/lib/index.cjs +8 -0
- package/lib/index.d.ts +26 -11
- package/lib/index.global.js +346 -103
- package/lib/index.js +4 -0
- package/package.json +16 -16
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -4,4 +4,4 @@ A type-safe wrapper around the web extension messaging APIs that lets you call a
|
|
|
4
4
|
|
|
5
5
|
## Get Started
|
|
6
6
|
|
|
7
|
-
See [documentation](https://webext-core.aklinker1.io/proxy-service) to get started!
|
|
7
|
+
See [documentation](https://webext-core.aklinker1.io/guide/proxy-service/) to get started!
|
package/lib/index.cjs
CHANGED
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
20
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -66,6 +70,7 @@ function canAccessExtensionApi() {
|
|
|
66
70
|
return !!((_a = import_webextension_polyfill.default.runtime) == null ? void 0 : _a.id);
|
|
67
71
|
}
|
|
68
72
|
var KNOWN_BACKGROUND_PAGE_PATHNAMES = [
|
|
73
|
+
// Firefox
|
|
69
74
|
"/_generated_background_page.html"
|
|
70
75
|
];
|
|
71
76
|
function isBackgroundPage() {
|
|
@@ -86,6 +91,7 @@ function defineProxyService(name, init, config) {
|
|
|
86
91
|
const wrapped = () => {
|
|
87
92
|
};
|
|
88
93
|
const proxy = new Proxy(wrapped, {
|
|
94
|
+
// Executed when the object is called as a function
|
|
89
95
|
apply(_target, _thisArg, args) {
|
|
90
96
|
return __async(this, null, function* () {
|
|
91
97
|
const res = yield sendMessage(messageKey, {
|
|
@@ -95,6 +101,7 @@ function defineProxyService(name, init, config) {
|
|
|
95
101
|
return res;
|
|
96
102
|
});
|
|
97
103
|
},
|
|
104
|
+
// Executed when accessing a property on an object
|
|
98
105
|
get(target, propertyName, receiver) {
|
|
99
106
|
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
100
107
|
return Reflect.get(target, propertyName, receiver);
|
|
@@ -143,6 +150,7 @@ function flattenPromise(promise) {
|
|
|
143
150
|
return fn.apply(thisArg, args);
|
|
144
151
|
});
|
|
145
152
|
},
|
|
153
|
+
// Executed when accessing a property on an object
|
|
146
154
|
get(target, propertyName, receiver) {
|
|
147
155
|
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
148
156
|
return Reflect.get(target, propertyName, receiver);
|
package/lib/index.d.ts
CHANGED
|
@@ -1,35 +1,50 @@
|
|
|
1
1
|
import { ExtensionMessagingConfig } from '@webext-core/messaging';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
type Proimsify<T> = T extends Promise<any> ? T : Promise<T>;
|
|
4
|
+
type Service = ((...args: any[]) => Promise<any>) | {
|
|
5
5
|
[key: string]: any | Service;
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* A type that ensures a service has only async methods.
|
|
9
|
+
* - ***If all methods are async***, it returns the original type.
|
|
10
|
+
* - ***If the service has non-async methods***, it returns a `DeepAsync` of the service.
|
|
10
11
|
*/
|
|
11
|
-
|
|
12
|
+
type ProxyService<TService> = TService extends DeepAsync<TService> ? TService : DeepAsync<TService>;
|
|
12
13
|
/**
|
|
13
|
-
*
|
|
14
|
+
* A recursive type that deeply converts all methods in `TService` to be async.
|
|
14
15
|
*/
|
|
15
|
-
|
|
16
|
+
type DeepAsync<TService> = TService extends (...args: any) => any ? ToAsyncFunction<TService> : TService extends {
|
|
16
17
|
[key: string]: any;
|
|
17
18
|
} ? {
|
|
18
19
|
[fn in keyof TService]: DeepAsync<TService[fn]>;
|
|
19
20
|
} : never;
|
|
20
|
-
|
|
21
|
+
type ToAsyncFunction<T extends (...args: any) => any> = (...args: Parameters<T>) => Proimsify<ReturnType<T>>;
|
|
22
|
+
/**
|
|
23
|
+
* Configure a proxy service's behavior. It uses `@webext-core/messaging` internally, so any
|
|
24
|
+
* config from `ExtensionMessagingConfig` can be passed as well.
|
|
25
|
+
*/
|
|
21
26
|
interface ProxyServiceConfig extends ExtensionMessagingConfig {
|
|
22
27
|
}
|
|
23
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Utility for creating a service whose functions are executed in the background script regardless
|
|
31
|
+
* of the JS context the they are called from.
|
|
32
|
+
*
|
|
33
|
+
* @param name A unique name for the service. Used to identify which service is being executed.
|
|
34
|
+
* @param init A function that returns your real service implementation. If args are listed,
|
|
35
|
+
* `registerService` will require the same set of arguments.
|
|
36
|
+
* @param config
|
|
37
|
+
* @returns
|
|
38
|
+
* - `registerService`: Used to register your service in the background
|
|
39
|
+
* - `getService`: Used to get an instance of the service anywhere in the extension.
|
|
40
|
+
*/
|
|
24
41
|
declare function defineProxyService<TService extends Service, TArgs extends any[]>(name: string, init: (...args: TArgs) => TService, config?: ProxyServiceConfig): [registerService: (...args: TArgs) => TService, getService: () => ProxyService<TService>];
|
|
25
42
|
|
|
26
43
|
/**
|
|
27
44
|
* Given a promise of a variable, return a proxy to that awaits the promise internally so you don't
|
|
28
45
|
* have to call `await` twice.
|
|
29
46
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* This is meant to be used to simplify service implementations, like so:
|
|
47
|
+
* > This can be used to simplify handling `Promise<Dependency>` passed in your services.
|
|
33
48
|
*
|
|
34
49
|
* @example
|
|
35
50
|
* function createService(dependencyPromise: Promise<SomeDependency>) {
|
package/lib/index.global.js
CHANGED
|
@@ -6,6 +6,7 @@ var webExtCoreProxyService = (() => {
|
|
|
6
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
8
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
10
|
var __commonJS = (cb, mod) => function __require() {
|
|
10
11
|
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
12
|
};
|
|
@@ -22,10 +23,18 @@ var webExtCoreProxyService = (() => {
|
|
|
22
23
|
return to;
|
|
23
24
|
};
|
|
24
25
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
30
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
31
|
mod
|
|
27
32
|
));
|
|
28
33
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
34
|
+
var __publicField = (obj, key, value) => {
|
|
35
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
36
|
+
return value;
|
|
37
|
+
};
|
|
29
38
|
var __async = (__this, __arguments, generator) => {
|
|
30
39
|
return new Promise((resolve, reject) => {
|
|
31
40
|
var fulfilled = (value) => {
|
|
@@ -47,9 +56,9 @@ var webExtCoreProxyService = (() => {
|
|
|
47
56
|
});
|
|
48
57
|
};
|
|
49
58
|
|
|
50
|
-
// ../../node_modules
|
|
59
|
+
// ../../node_modules/webextension-polyfill/dist/browser-polyfill.js
|
|
51
60
|
var require_browser_polyfill = __commonJS({
|
|
52
|
-
"../../node_modules
|
|
61
|
+
"../../node_modules/webextension-polyfill/dist/browser-polyfill.js"(exports, module) {
|
|
53
62
|
(function(global, factory) {
|
|
54
63
|
if (typeof define === "function" && define.amd) {
|
|
55
64
|
define("webextension-polyfill", ["module"], factory);
|
|
@@ -1042,9 +1051,9 @@ var webExtCoreProxyService = (() => {
|
|
|
1042
1051
|
}
|
|
1043
1052
|
});
|
|
1044
1053
|
|
|
1045
|
-
// ../../node_modules
|
|
1054
|
+
// ../../node_modules/isobject/index.js
|
|
1046
1055
|
var require_isobject = __commonJS({
|
|
1047
|
-
"../../node_modules
|
|
1056
|
+
"../../node_modules/isobject/index.js"(exports, module) {
|
|
1048
1057
|
"use strict";
|
|
1049
1058
|
module.exports = function isObject(val) {
|
|
1050
1059
|
return val != null && typeof val === "object" && Array.isArray(val) === false;
|
|
@@ -1052,9 +1061,9 @@ var webExtCoreProxyService = (() => {
|
|
|
1052
1061
|
}
|
|
1053
1062
|
});
|
|
1054
1063
|
|
|
1055
|
-
// ../../node_modules
|
|
1064
|
+
// ../../node_modules/get-value/index.js
|
|
1056
1065
|
var require_get_value = __commonJS({
|
|
1057
|
-
"../../node_modules
|
|
1066
|
+
"../../node_modules/get-value/index.js"(exports, module) {
|
|
1058
1067
|
var isObject = require_isobject();
|
|
1059
1068
|
module.exports = function(target, path, options) {
|
|
1060
1069
|
if (!isObject(options)) {
|
|
@@ -1162,6 +1171,7 @@ var webExtCoreProxyService = (() => {
|
|
|
1162
1171
|
return !!((_a = import_webextension_polyfill.default.runtime) == null ? void 0 : _a.id);
|
|
1163
1172
|
}
|
|
1164
1173
|
var KNOWN_BACKGROUND_PAGE_PATHNAMES = [
|
|
1174
|
+
// Firefox
|
|
1165
1175
|
"/_generated_background_page.html"
|
|
1166
1176
|
];
|
|
1167
1177
|
function isBackgroundPage() {
|
|
@@ -1171,8 +1181,214 @@ var webExtCoreProxyService = (() => {
|
|
|
1171
1181
|
return typeof window === "undefined";
|
|
1172
1182
|
}
|
|
1173
1183
|
|
|
1174
|
-
//
|
|
1175
|
-
var
|
|
1184
|
+
// ../../node_modules/serialize-error/error-constructors.js
|
|
1185
|
+
var list = [
|
|
1186
|
+
// Native ES errors https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
|
|
1187
|
+
EvalError,
|
|
1188
|
+
RangeError,
|
|
1189
|
+
ReferenceError,
|
|
1190
|
+
SyntaxError,
|
|
1191
|
+
TypeError,
|
|
1192
|
+
URIError,
|
|
1193
|
+
// Built-in errors
|
|
1194
|
+
globalThis.DOMException,
|
|
1195
|
+
// Node-specific errors
|
|
1196
|
+
// https://nodejs.org/api/errors.html
|
|
1197
|
+
globalThis.AssertionError,
|
|
1198
|
+
globalThis.SystemError
|
|
1199
|
+
].filter(Boolean).map(
|
|
1200
|
+
(constructor) => [constructor.name, constructor]
|
|
1201
|
+
);
|
|
1202
|
+
var errorConstructors = new Map(list);
|
|
1203
|
+
var error_constructors_default = errorConstructors;
|
|
1204
|
+
|
|
1205
|
+
// ../../node_modules/serialize-error/index.js
|
|
1206
|
+
var NonError = class extends Error {
|
|
1207
|
+
constructor(message) {
|
|
1208
|
+
super(NonError._prepareSuperMessage(message));
|
|
1209
|
+
__publicField(this, "name", "NonError");
|
|
1210
|
+
}
|
|
1211
|
+
static _prepareSuperMessage(message) {
|
|
1212
|
+
try {
|
|
1213
|
+
return JSON.stringify(message);
|
|
1214
|
+
} catch (e) {
|
|
1215
|
+
return String(message);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1219
|
+
var commonProperties = [
|
|
1220
|
+
{
|
|
1221
|
+
property: "name",
|
|
1222
|
+
enumerable: false
|
|
1223
|
+
},
|
|
1224
|
+
{
|
|
1225
|
+
property: "message",
|
|
1226
|
+
enumerable: false
|
|
1227
|
+
},
|
|
1228
|
+
{
|
|
1229
|
+
property: "stack",
|
|
1230
|
+
enumerable: false
|
|
1231
|
+
},
|
|
1232
|
+
{
|
|
1233
|
+
property: "code",
|
|
1234
|
+
enumerable: true
|
|
1235
|
+
},
|
|
1236
|
+
{
|
|
1237
|
+
property: "cause",
|
|
1238
|
+
enumerable: false
|
|
1239
|
+
}
|
|
1240
|
+
];
|
|
1241
|
+
var toJsonWasCalled = /* @__PURE__ */ new WeakSet();
|
|
1242
|
+
var toJSON = (from) => {
|
|
1243
|
+
toJsonWasCalled.add(from);
|
|
1244
|
+
const json = from.toJSON();
|
|
1245
|
+
toJsonWasCalled.delete(from);
|
|
1246
|
+
return json;
|
|
1247
|
+
};
|
|
1248
|
+
var getErrorConstructor = (name) => {
|
|
1249
|
+
var _a;
|
|
1250
|
+
return (_a = error_constructors_default.get(name)) != null ? _a : Error;
|
|
1251
|
+
};
|
|
1252
|
+
var destroyCircular = ({
|
|
1253
|
+
from,
|
|
1254
|
+
seen,
|
|
1255
|
+
to,
|
|
1256
|
+
forceEnumerable,
|
|
1257
|
+
maxDepth,
|
|
1258
|
+
depth,
|
|
1259
|
+
useToJSON,
|
|
1260
|
+
serialize
|
|
1261
|
+
}) => {
|
|
1262
|
+
if (!to) {
|
|
1263
|
+
if (Array.isArray(from)) {
|
|
1264
|
+
to = [];
|
|
1265
|
+
} else if (!serialize && isErrorLike(from)) {
|
|
1266
|
+
const Error2 = getErrorConstructor(from.name);
|
|
1267
|
+
to = new Error2();
|
|
1268
|
+
} else {
|
|
1269
|
+
to = {};
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
seen.push(from);
|
|
1273
|
+
if (depth >= maxDepth) {
|
|
1274
|
+
return to;
|
|
1275
|
+
}
|
|
1276
|
+
if (useToJSON && typeof from.toJSON === "function" && !toJsonWasCalled.has(from)) {
|
|
1277
|
+
return toJSON(from);
|
|
1278
|
+
}
|
|
1279
|
+
const continueDestroyCircular = (value) => destroyCircular({
|
|
1280
|
+
from: value,
|
|
1281
|
+
seen: [...seen],
|
|
1282
|
+
forceEnumerable,
|
|
1283
|
+
maxDepth,
|
|
1284
|
+
depth,
|
|
1285
|
+
useToJSON,
|
|
1286
|
+
serialize
|
|
1287
|
+
});
|
|
1288
|
+
for (const [key, value] of Object.entries(from)) {
|
|
1289
|
+
if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
|
|
1290
|
+
to[key] = "[object Buffer]";
|
|
1291
|
+
continue;
|
|
1292
|
+
}
|
|
1293
|
+
if (value !== null && typeof value === "object" && typeof value.pipe === "function") {
|
|
1294
|
+
to[key] = "[object Stream]";
|
|
1295
|
+
continue;
|
|
1296
|
+
}
|
|
1297
|
+
if (typeof value === "function") {
|
|
1298
|
+
continue;
|
|
1299
|
+
}
|
|
1300
|
+
if (!value || typeof value !== "object") {
|
|
1301
|
+
try {
|
|
1302
|
+
to[key] = value;
|
|
1303
|
+
} catch (e) {
|
|
1304
|
+
}
|
|
1305
|
+
continue;
|
|
1306
|
+
}
|
|
1307
|
+
if (!seen.includes(from[key])) {
|
|
1308
|
+
depth++;
|
|
1309
|
+
to[key] = continueDestroyCircular(from[key]);
|
|
1310
|
+
continue;
|
|
1311
|
+
}
|
|
1312
|
+
to[key] = "[Circular]";
|
|
1313
|
+
}
|
|
1314
|
+
for (const { property, enumerable } of commonProperties) {
|
|
1315
|
+
if (typeof from[property] !== "undefined" && from[property] !== null) {
|
|
1316
|
+
Object.defineProperty(to, property, {
|
|
1317
|
+
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
|
|
1318
|
+
enumerable: forceEnumerable ? true : enumerable,
|
|
1319
|
+
configurable: true,
|
|
1320
|
+
writable: true
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
return to;
|
|
1325
|
+
};
|
|
1326
|
+
function serializeError(value, options = {}) {
|
|
1327
|
+
const {
|
|
1328
|
+
maxDepth = Number.POSITIVE_INFINITY,
|
|
1329
|
+
useToJSON = true
|
|
1330
|
+
} = options;
|
|
1331
|
+
if (typeof value === "object" && value !== null) {
|
|
1332
|
+
return destroyCircular({
|
|
1333
|
+
from: value,
|
|
1334
|
+
seen: [],
|
|
1335
|
+
forceEnumerable: true,
|
|
1336
|
+
maxDepth,
|
|
1337
|
+
depth: 0,
|
|
1338
|
+
useToJSON,
|
|
1339
|
+
serialize: true
|
|
1340
|
+
});
|
|
1341
|
+
}
|
|
1342
|
+
if (typeof value === "function") {
|
|
1343
|
+
return `[Function: ${value.name || "anonymous"}]`;
|
|
1344
|
+
}
|
|
1345
|
+
return value;
|
|
1346
|
+
}
|
|
1347
|
+
function deserializeError(value, options = {}) {
|
|
1348
|
+
const { maxDepth = Number.POSITIVE_INFINITY } = options;
|
|
1349
|
+
if (value instanceof Error) {
|
|
1350
|
+
return value;
|
|
1351
|
+
}
|
|
1352
|
+
if (isMinimumViableSerializedError(value)) {
|
|
1353
|
+
const Error2 = getErrorConstructor(value.name);
|
|
1354
|
+
return destroyCircular({
|
|
1355
|
+
from: value,
|
|
1356
|
+
seen: [],
|
|
1357
|
+
to: new Error2(),
|
|
1358
|
+
maxDepth,
|
|
1359
|
+
depth: 0,
|
|
1360
|
+
serialize: false
|
|
1361
|
+
});
|
|
1362
|
+
}
|
|
1363
|
+
return new NonError(value);
|
|
1364
|
+
}
|
|
1365
|
+
function isErrorLike(value) {
|
|
1366
|
+
return Boolean(value) && typeof value === "object" && "name" in value && "message" in value && "stack" in value;
|
|
1367
|
+
}
|
|
1368
|
+
function isMinimumViableSerializedError(value) {
|
|
1369
|
+
return Boolean(value) && typeof value === "object" && "message" in value && !Array.isArray(value);
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
// ../messaging/lib/chunk-BQLFSFFZ.js
|
|
1373
|
+
var __defProp2 = Object.defineProperty;
|
|
1374
|
+
var __defProps = Object.defineProperties;
|
|
1375
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
1376
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
1377
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
1378
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
1379
|
+
var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1380
|
+
var __spreadValues = (a, b) => {
|
|
1381
|
+
for (var prop in b || (b = {}))
|
|
1382
|
+
if (__hasOwnProp2.call(b, prop))
|
|
1383
|
+
__defNormalProp2(a, prop, b[prop]);
|
|
1384
|
+
if (__getOwnPropSymbols)
|
|
1385
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
1386
|
+
if (__propIsEnum.call(b, prop))
|
|
1387
|
+
__defNormalProp2(a, prop, b[prop]);
|
|
1388
|
+
}
|
|
1389
|
+
return a;
|
|
1390
|
+
};
|
|
1391
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
1176
1392
|
var __async2 = (__this, __arguments, generator) => {
|
|
1177
1393
|
return new Promise((resolve, reject) => {
|
|
1178
1394
|
var fulfilled = (value) => {
|
|
@@ -1193,103 +1409,121 @@ var webExtCoreProxyService = (() => {
|
|
|
1193
1409
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
1194
1410
|
});
|
|
1195
1411
|
};
|
|
1196
|
-
function
|
|
1197
|
-
let
|
|
1412
|
+
function defineGenericMessanging(config) {
|
|
1413
|
+
let removeRootListener;
|
|
1414
|
+
let perTypeListeners = {};
|
|
1198
1415
|
function cleanupRootListener() {
|
|
1199
|
-
if (Object.entries(
|
|
1200
|
-
|
|
1201
|
-
|
|
1416
|
+
if (Object.entries(perTypeListeners).length === 0) {
|
|
1417
|
+
removeRootListener == null ? void 0 : removeRootListener();
|
|
1418
|
+
removeRootListener = void 0;
|
|
1202
1419
|
}
|
|
1203
1420
|
}
|
|
1204
|
-
let keyListeners = {};
|
|
1205
1421
|
let idSeq = Math.floor(Math.random() * 1e4);
|
|
1206
1422
|
function getNextId() {
|
|
1207
1423
|
return idSeq++;
|
|
1208
1424
|
}
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1425
|
+
return {
|
|
1426
|
+
sendMessage(type, data, ...args) {
|
|
1427
|
+
return __async2(this, null, function* () {
|
|
1428
|
+
var _a2, _b, _c, _d;
|
|
1429
|
+
const _message = {
|
|
1430
|
+
id: getNextId(),
|
|
1431
|
+
type,
|
|
1432
|
+
data,
|
|
1433
|
+
timestamp: Date.now()
|
|
1434
|
+
};
|
|
1435
|
+
const message = (_b = yield (_a2 = config.verifyMessageData) == null ? void 0 : _a2.call(config, _message)) != null ? _b : _message;
|
|
1436
|
+
(_c = config.logger) == null ? void 0 : _c.debug(`[messaging] sendMessage {id=${message.id}} \u2500\u1405`, message, ...args);
|
|
1437
|
+
const response = yield config.sendMessage(message, ...args);
|
|
1438
|
+
const { res, err } = response != null ? response : { err: new Error("No response") };
|
|
1439
|
+
(_d = config.logger) == null ? void 0 : _d.debug(`[messaging] sendMessage {id=${message.id}} \u140A\u2500`, { res, err });
|
|
1440
|
+
if (err != null)
|
|
1441
|
+
throw deserializeError(err);
|
|
1442
|
+
return res;
|
|
1443
|
+
});
|
|
1444
|
+
},
|
|
1445
|
+
onMessage(type, onReceived) {
|
|
1446
|
+
var _a2, _b, _c;
|
|
1447
|
+
if (removeRootListener == null) {
|
|
1448
|
+
(_a2 = config.logger) == null ? void 0 : _a2.debug(
|
|
1449
|
+
`[messaging] "${type}" initialized the message listener for this context`
|
|
1450
|
+
);
|
|
1451
|
+
removeRootListener = config.addRootListener((message) => {
|
|
1452
|
+
var _a3, _b2;
|
|
1453
|
+
if (typeof message.type != "string" || typeof message.timestamp !== "number") {
|
|
1454
|
+
if (config.breakError) {
|
|
1455
|
+
return;
|
|
1456
|
+
}
|
|
1457
|
+
const err = Error(
|
|
1458
|
+
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
1459
|
+
message
|
|
1460
|
+
)}`
|
|
1461
|
+
);
|
|
1462
|
+
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
1463
|
+
throw err;
|
|
1464
|
+
}
|
|
1465
|
+
(_b2 = config == null ? void 0 : config.logger) == null ? void 0 : _b2.debug("[messaging] Received message", message);
|
|
1466
|
+
const listener = perTypeListeners[message.type];
|
|
1467
|
+
if (listener == null)
|
|
1468
|
+
return;
|
|
1469
|
+
const res = listener(message);
|
|
1237
1470
|
return Promise.resolve(res).then((res2) => {
|
|
1238
|
-
var
|
|
1239
|
-
(
|
|
1471
|
+
var _a4, _b3;
|
|
1472
|
+
return (_b3 = (_a4 = config.verifyMessageData) == null ? void 0 : _a4.call(config, res2)) != null ? _b3 : res2;
|
|
1473
|
+
}).then((res2) => {
|
|
1474
|
+
var _a4;
|
|
1475
|
+
(_a4 = config == null ? void 0 : config.logger) == null ? void 0 : _a4.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { res: res2 });
|
|
1240
1476
|
return { res: res2 };
|
|
1241
1477
|
}).catch((err) => {
|
|
1242
|
-
var
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
errMessage = err.message;
|
|
1246
|
-
else
|
|
1247
|
-
errMessage = String(err);
|
|
1248
|
-
(_a3 = config == null ? void 0 : config.logger) == null ? void 0 : _a3.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { err });
|
|
1249
|
-
return { err: errMessage };
|
|
1478
|
+
var _a4;
|
|
1479
|
+
(_a4 = config == null ? void 0 : config.logger) == null ? void 0 : _a4.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { err });
|
|
1480
|
+
return { err: serializeError(err) };
|
|
1250
1481
|
});
|
|
1251
|
-
}
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1484
|
+
if (perTypeListeners[type] != null) {
|
|
1485
|
+
const err = Error(
|
|
1486
|
+
`[messaging] In this JS context, only one listener can be setup for ${type}`
|
|
1487
|
+
);
|
|
1488
|
+
(_b = config.logger) == null ? void 0 : _b.error(err);
|
|
1489
|
+
throw err;
|
|
1490
|
+
}
|
|
1491
|
+
perTypeListeners[type] = onReceived;
|
|
1492
|
+
(_c = config.logger) == null ? void 0 : _c.log(`[messaging] Added listener for ${type}`);
|
|
1493
|
+
return () => {
|
|
1494
|
+
delete perTypeListeners[type];
|
|
1495
|
+
cleanupRootListener();
|
|
1252
1496
|
};
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
);
|
|
1259
|
-
(_b = config == null ? void 0 : config.logger) == null ? void 0 : _b.error(err);
|
|
1260
|
-
throw err;
|
|
1261
|
-
}
|
|
1262
|
-
keyListeners[type] = onReceived;
|
|
1263
|
-
(_c = config == null ? void 0 : config.logger) == null ? void 0 : _c.log(`[messaging] Added listener for ${type}`);
|
|
1264
|
-
return function removeListener() {
|
|
1265
|
-
delete keyListeners[type];
|
|
1497
|
+
},
|
|
1498
|
+
removeAllListeners() {
|
|
1499
|
+
Object.keys(perTypeListeners).forEach((type) => {
|
|
1500
|
+
delete perTypeListeners[type];
|
|
1501
|
+
});
|
|
1266
1502
|
cleanupRootListener();
|
|
1267
|
-
}
|
|
1268
|
-
}
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1503
|
+
}
|
|
1504
|
+
};
|
|
1505
|
+
}
|
|
1506
|
+
|
|
1507
|
+
// ../messaging/lib/index.js
|
|
1508
|
+
var import_webextension_polyfill2 = __toESM(require_browser_polyfill(), 1);
|
|
1509
|
+
function defineExtensionMessaging(config) {
|
|
1510
|
+
return defineGenericMessanging(__spreadProps(__spreadValues({}, config), {
|
|
1511
|
+
sendMessage(message, tabId) {
|
|
1275
1512
|
if (tabId == null)
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
cleanupRootListener();
|
|
1291
|
-
}
|
|
1292
|
-
return { onMessage, sendMessage, removeAllMessageListeners };
|
|
1513
|
+
return import_webextension_polyfill2.default.runtime.sendMessage(message);
|
|
1514
|
+
return import_webextension_polyfill2.default.tabs.sendMessage(tabId, message);
|
|
1515
|
+
},
|
|
1516
|
+
addRootListener(processMessage) {
|
|
1517
|
+
const listener = (message, sender) => {
|
|
1518
|
+
if (typeof message === "object")
|
|
1519
|
+
return processMessage(__spreadProps(__spreadValues({}, message), { sender }));
|
|
1520
|
+
else
|
|
1521
|
+
return processMessage(message);
|
|
1522
|
+
};
|
|
1523
|
+
import_webextension_polyfill2.default.runtime.onMessage.addListener(listener);
|
|
1524
|
+
return () => import_webextension_polyfill2.default.runtime.onMessage.removeListener(listener);
|
|
1525
|
+
}
|
|
1526
|
+
}));
|
|
1293
1527
|
}
|
|
1294
1528
|
|
|
1295
1529
|
// src/defineProxyService.ts
|
|
@@ -1302,6 +1536,7 @@ var webExtCoreProxyService = (() => {
|
|
|
1302
1536
|
const wrapped = () => {
|
|
1303
1537
|
};
|
|
1304
1538
|
const proxy = new Proxy(wrapped, {
|
|
1539
|
+
// Executed when the object is called as a function
|
|
1305
1540
|
apply(_target, _thisArg, args) {
|
|
1306
1541
|
return __async(this, null, function* () {
|
|
1307
1542
|
const res = yield sendMessage(messageKey, {
|
|
@@ -1311,6 +1546,7 @@ var webExtCoreProxyService = (() => {
|
|
|
1311
1546
|
return res;
|
|
1312
1547
|
});
|
|
1313
1548
|
},
|
|
1549
|
+
// Executed when accessing a property on an object
|
|
1314
1550
|
get(target, propertyName, receiver) {
|
|
1315
1551
|
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
1316
1552
|
return Reflect.get(target, propertyName, receiver);
|
|
@@ -1359,6 +1595,7 @@ var webExtCoreProxyService = (() => {
|
|
|
1359
1595
|
return fn.apply(thisArg, args);
|
|
1360
1596
|
});
|
|
1361
1597
|
},
|
|
1598
|
+
// Executed when accessing a property on an object
|
|
1362
1599
|
get(target, propertyName, receiver) {
|
|
1363
1600
|
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
1364
1601
|
return Reflect.get(target, propertyName, receiver);
|
|
@@ -1376,15 +1613,21 @@ var webExtCoreProxyService = (() => {
|
|
|
1376
1613
|
}
|
|
1377
1614
|
return __toCommonJS(src_exports);
|
|
1378
1615
|
})();
|
|
1379
|
-
/*!
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1616
|
+
/*! Bundled license information:
|
|
1617
|
+
|
|
1618
|
+
isobject/index.js:
|
|
1619
|
+
(*!
|
|
1620
|
+
* isobject <https://github.com/jonschlinkert/isobject>
|
|
1621
|
+
*
|
|
1622
|
+
* Copyright (c) 2014-2017, Jon Schlinkert.
|
|
1623
|
+
* Released under the MIT License.
|
|
1624
|
+
*)
|
|
1625
|
+
|
|
1626
|
+
get-value/index.js:
|
|
1627
|
+
(*!
|
|
1628
|
+
* get-value <https://github.com/jonschlinkert/get-value>
|
|
1629
|
+
*
|
|
1630
|
+
* Copyright (c) 2014-2018, Jon Schlinkert.
|
|
1631
|
+
* Released under the MIT License.
|
|
1632
|
+
*)
|
|
1633
|
+
*/
|
package/lib/index.js
CHANGED
|
@@ -34,6 +34,7 @@ function canAccessExtensionApi() {
|
|
|
34
34
|
return !!((_a = Browser.runtime) == null ? void 0 : _a.id);
|
|
35
35
|
}
|
|
36
36
|
var KNOWN_BACKGROUND_PAGE_PATHNAMES = [
|
|
37
|
+
// Firefox
|
|
37
38
|
"/_generated_background_page.html"
|
|
38
39
|
];
|
|
39
40
|
function isBackgroundPage() {
|
|
@@ -54,6 +55,7 @@ function defineProxyService(name, init, config) {
|
|
|
54
55
|
const wrapped = () => {
|
|
55
56
|
};
|
|
56
57
|
const proxy = new Proxy(wrapped, {
|
|
58
|
+
// Executed when the object is called as a function
|
|
57
59
|
apply(_target, _thisArg, args) {
|
|
58
60
|
return __async(this, null, function* () {
|
|
59
61
|
const res = yield sendMessage(messageKey, {
|
|
@@ -63,6 +65,7 @@ function defineProxyService(name, init, config) {
|
|
|
63
65
|
return res;
|
|
64
66
|
});
|
|
65
67
|
},
|
|
68
|
+
// Executed when accessing a property on an object
|
|
66
69
|
get(target, propertyName, receiver) {
|
|
67
70
|
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
68
71
|
return Reflect.get(target, propertyName, receiver);
|
|
@@ -111,6 +114,7 @@ function flattenPromise(promise) {
|
|
|
111
114
|
return fn.apply(thisArg, args);
|
|
112
115
|
});
|
|
113
116
|
},
|
|
117
|
+
// Executed when accessing a property on an object
|
|
114
118
|
get(target, propertyName, receiver) {
|
|
115
119
|
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
116
120
|
return Reflect.get(target, propertyName, receiver);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webext-core/proxy-service",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.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
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -39,6 +39,14 @@
|
|
|
39
39
|
"require": "./lib/index.cjs"
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "buildc -- tsup src/index.ts --clean --out-dir lib --dts --format esm,cjs,iife --global-name webExtCoreProxyService",
|
|
44
|
+
"test": "buildc --deps-only -- vitest -r src",
|
|
45
|
+
"test:coverage": "buildc --deps-only -- vitest run -r src --coverage",
|
|
46
|
+
"check": "buildc --deps-only -- bun check:typescript && bun check:type-tests",
|
|
47
|
+
"check:typescript": "tsc --noEmit",
|
|
48
|
+
"check:type-tests": "vitest run --typecheck types"
|
|
49
|
+
},
|
|
42
50
|
"dependencies": {
|
|
43
51
|
"get-value": "^3.0.1",
|
|
44
52
|
"webextension-polyfill": "^0.10.0"
|
|
@@ -46,23 +54,15 @@
|
|
|
46
54
|
"devDependencies": {
|
|
47
55
|
"@types/get-value": "^3.0.3",
|
|
48
56
|
"@types/webextension-polyfill": "^0.9.1",
|
|
49
|
-
"@
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"vitest": "0.28.0",
|
|
53
|
-
"@webext-core/fake-browser": "1.2.2",
|
|
54
|
-
"@webext-core/messaging": "1.3.1",
|
|
55
|
-
"tsconfig": "0.0.0"
|
|
57
|
+
"@webext-core/fake-browser": "workspace:*",
|
|
58
|
+
"@webext-core/messaging": "workspace:*",
|
|
59
|
+
"tsconfig": "workspace:*"
|
|
56
60
|
},
|
|
57
61
|
"peerDependencies": {
|
|
58
|
-
"webextension-polyfill": "
|
|
62
|
+
"webextension-polyfill": ">=0.10.0",
|
|
59
63
|
"@webext-core/messaging": ">=1.3.1"
|
|
60
64
|
},
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"build:dependencies": "cd ../.. && turbo run build --filter=@webext-core/proxy-service^...",
|
|
64
|
-
"test": "vitest -r src",
|
|
65
|
-
"test:coverage": "vitest run -r src --coverage",
|
|
66
|
-
"compile": "tsc --noEmit"
|
|
65
|
+
"buildc": {
|
|
66
|
+
"outDir": "lib"
|
|
67
67
|
}
|
|
68
|
-
}
|
|
68
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Aaron
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|