@webext-core/messaging 2.3.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 +4 -2
- package/lib/{chunk-BQLFSFFZ.js → chunk-GMKXECDR.js} +10 -9
- package/lib/{generic-683db69b.d.ts → generic-c22f9780.d.ts} +10 -8
- package/lib/index.cjs +12 -11
- package/lib/index.d.ts +2 -2
- package/lib/index.global.js +53 -199
- package/lib/index.js +1 -1
- package/lib/page.cjs +28 -30
- package/lib/page.d.ts +8 -2
- package/lib/page.global.js +72 -232
- package/lib/page.js +17 -18
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# `@webext-core/messaging`
|
|
2
2
|
|
|
3
|
-
A light-weight, type-safe wrapper around the `browser.runtime` messaging APIs.
|
|
3
|
+
A light-weight, type-safe wrapper around the `browser.runtime` messaging APIs.
|
|
4
|
+
Supports all browsers (Chrome, Firefox, Safari).
|
|
4
5
|
|
|
5
6
|
```ts
|
|
6
7
|
// ./messaging.ts
|
|
@@ -33,4 +34,5 @@ console.log(length); // 11
|
|
|
33
34
|
|
|
34
35
|
## Get Started
|
|
35
36
|
|
|
36
|
-
See [documentation](https://webext-core.aklinker1.io/
|
|
37
|
+
See [documentation](https://webext-core.aklinker1.io/messaging/installation) to
|
|
38
|
+
get started!
|
|
@@ -51,7 +51,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
51
51
|
};
|
|
52
52
|
|
|
53
53
|
// src/generic.ts
|
|
54
|
-
import { serializeError, deserializeError } from "serialize-error";
|
|
54
|
+
import { serializeError, deserializeError } from "@aklinker1/zero-serialize-error";
|
|
55
55
|
function defineGenericMessanging(config) {
|
|
56
56
|
let removeRootListener;
|
|
57
57
|
let perTypeListeners = {};
|
|
@@ -94,16 +94,17 @@ function defineGenericMessanging(config) {
|
|
|
94
94
|
removeRootListener = config.addRootListener((message) => {
|
|
95
95
|
var _a3, _b2;
|
|
96
96
|
if (typeof message.type != "string" || typeof message.timestamp !== "number") {
|
|
97
|
-
if (config.
|
|
97
|
+
if (config.throwOnUnknownMessageFormat) {
|
|
98
|
+
const err = Error(
|
|
99
|
+
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
100
|
+
message
|
|
101
|
+
)}`
|
|
102
|
+
);
|
|
103
|
+
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
104
|
+
throw err;
|
|
105
|
+
} else {
|
|
98
106
|
return;
|
|
99
107
|
}
|
|
100
|
-
const err = Error(
|
|
101
|
-
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
102
|
-
message
|
|
103
|
-
)}`
|
|
104
|
-
);
|
|
105
|
-
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
106
|
-
throw err;
|
|
107
108
|
}
|
|
108
109
|
(_b2 = config == null ? void 0 : config.logger) == null ? void 0 : _b2.debug("[messaging] Received message", message);
|
|
109
110
|
const listener = perTypeListeners[message.type];
|
|
@@ -17,7 +17,7 @@ type MaybePromise<T> = Promise<T> | T;
|
|
|
17
17
|
*
|
|
18
18
|
* > Internally, this is just an object with random keys for the data and return types.
|
|
19
19
|
*
|
|
20
|
-
* @deprecated Use the function syntax instead: <https://webext-core.aklinker1.io/
|
|
20
|
+
* @deprecated Use the function syntax instead: <https://webext-core.aklinker1.io/messaging/protocol-maps.html#syntax>
|
|
21
21
|
*
|
|
22
22
|
* @example
|
|
23
23
|
* interface ProtocolMap {
|
|
@@ -62,11 +62,13 @@ interface BaseMessagingConfig {
|
|
|
62
62
|
*/
|
|
63
63
|
logger?: Logger;
|
|
64
64
|
/**
|
|
65
|
-
*
|
|
65
|
+
* When a message is received that doesn't follow `@webext-core/messaging`'s format, it is ignored.
|
|
66
66
|
*
|
|
67
|
-
*
|
|
67
|
+
* Set this value to true to throw an error instead of ignoring it.
|
|
68
|
+
*
|
|
69
|
+
* @default false
|
|
68
70
|
*/
|
|
69
|
-
|
|
71
|
+
throwOnUnknownMessageFormat?: boolean;
|
|
70
72
|
}
|
|
71
73
|
interface NamespaceMessagingConfig extends BaseMessagingConfig {
|
|
72
74
|
/**
|
|
@@ -114,8 +116,8 @@ interface GenericMessenger<TProtocolMap extends Record<string, any>, TMessageExt
|
|
|
114
116
|
* @param data The data to send with the message.
|
|
115
117
|
* @param args Different messengers will have additional arguments to configure how a message gets sent.
|
|
116
118
|
*/
|
|
117
|
-
sendMessage<TType extends keyof TProtocolMap>(type: TType, ...args: GetDataType<TProtocolMap[TType]> extends undefined ? [data?: undefined, ...args: TSendMessageArgs] : never): Promise<GetReturnType<TProtocolMap[TType]>>;
|
|
118
|
-
sendMessage<TType extends keyof TProtocolMap>(type: TType, data: GetDataType<TProtocolMap[TType]>, ...args: TSendMessageArgs): Promise<GetReturnType<TProtocolMap[TType]>>;
|
|
119
|
+
sendMessage<TType extends keyof TProtocolMap>(this: void, type: TType, ...args: GetDataType<TProtocolMap[TType]> extends undefined ? [data?: undefined, ...args: TSendMessageArgs] : never): Promise<GetReturnType<TProtocolMap[TType]>>;
|
|
120
|
+
sendMessage<TType extends keyof TProtocolMap>(this: void, type: TType, data: GetDataType<TProtocolMap[TType]>, ...args: TSendMessageArgs): Promise<GetReturnType<TProtocolMap[TType]>>;
|
|
119
121
|
/**
|
|
120
122
|
* Trigger a callback when a message of the requested type is received. You cannot setup multiple
|
|
121
123
|
* listeners for the same message type in the same JS context.
|
|
@@ -125,11 +127,11 @@ interface GenericMessenger<TProtocolMap extends Record<string, any>, TMessageExt
|
|
|
125
127
|
* @param type The message type to listen for. Call `sendMessage` with the same type to trigger this listener.
|
|
126
128
|
* @param onReceived The callback executed when a message is received.
|
|
127
129
|
*/
|
|
128
|
-
onMessage<TType extends keyof TProtocolMap>(type: TType, onReceived: (message: Message<TProtocolMap, TType> & TMessageExtension) => void | MaybePromise<GetReturnType<TProtocolMap[TType]>>): RemoveListenerCallback;
|
|
130
|
+
onMessage<TType extends keyof TProtocolMap>(this: void, type: TType, onReceived: (message: Message<TProtocolMap, TType> & TMessageExtension) => void | MaybePromise<GetReturnType<TProtocolMap[TType]>>): RemoveListenerCallback;
|
|
129
131
|
/**
|
|
130
132
|
* Removes all listeners.
|
|
131
133
|
*/
|
|
132
|
-
removeAllListeners(): void;
|
|
134
|
+
removeAllListeners(this: void): void;
|
|
133
135
|
}
|
|
134
136
|
|
|
135
137
|
export { BaseMessagingConfig as B, GenericMessenger as G, Logger as L, MaybePromise as M, NamespaceMessagingConfig as N, ProtocolWithReturn as P, RemoveListenerCallback as R, GetDataType as a, GetReturnType as b, Message as c };
|
package/lib/index.cjs
CHANGED
|
@@ -75,7 +75,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
75
75
|
var import_webextension_polyfill = __toESM(require("webextension-polyfill"), 1);
|
|
76
76
|
|
|
77
77
|
// src/generic.ts
|
|
78
|
-
var
|
|
78
|
+
var import_zero_serialize_error = require("@aklinker1/zero-serialize-error");
|
|
79
79
|
function defineGenericMessanging(config) {
|
|
80
80
|
let removeRootListener;
|
|
81
81
|
let perTypeListeners = {};
|
|
@@ -105,7 +105,7 @@ function defineGenericMessanging(config) {
|
|
|
105
105
|
const { res, err } = response != null ? response : { err: new Error("No response") };
|
|
106
106
|
(_d = config.logger) == null ? void 0 : _d.debug(`[messaging] sendMessage {id=${message.id}} \u140A\u2500`, { res, err });
|
|
107
107
|
if (err != null)
|
|
108
|
-
throw (0,
|
|
108
|
+
throw (0, import_zero_serialize_error.deserializeError)(err);
|
|
109
109
|
return res;
|
|
110
110
|
});
|
|
111
111
|
},
|
|
@@ -118,16 +118,17 @@ function defineGenericMessanging(config) {
|
|
|
118
118
|
removeRootListener = config.addRootListener((message) => {
|
|
119
119
|
var _a3, _b2;
|
|
120
120
|
if (typeof message.type != "string" || typeof message.timestamp !== "number") {
|
|
121
|
-
if (config.
|
|
121
|
+
if (config.throwOnUnknownMessageFormat) {
|
|
122
|
+
const err = Error(
|
|
123
|
+
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
124
|
+
message
|
|
125
|
+
)}`
|
|
126
|
+
);
|
|
127
|
+
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
128
|
+
throw err;
|
|
129
|
+
} else {
|
|
122
130
|
return;
|
|
123
131
|
}
|
|
124
|
-
const err = Error(
|
|
125
|
-
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
126
|
-
message
|
|
127
|
-
)}`
|
|
128
|
-
);
|
|
129
|
-
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
130
|
-
throw err;
|
|
131
132
|
}
|
|
132
133
|
(_b2 = config == null ? void 0 : config.logger) == null ? void 0 : _b2.debug("[messaging] Received message", message);
|
|
133
134
|
const listener = perTypeListeners[message.type];
|
|
@@ -144,7 +145,7 @@ function defineGenericMessanging(config) {
|
|
|
144
145
|
}).catch((err) => {
|
|
145
146
|
var _a4;
|
|
146
147
|
(_a4 = config == null ? void 0 : config.logger) == null ? void 0 : _a4.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { err });
|
|
147
|
-
return { err: (0,
|
|
148
|
+
return { err: (0, import_zero_serialize_error.serializeError)(err) };
|
|
148
149
|
});
|
|
149
150
|
});
|
|
150
151
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as BaseMessagingConfig, G as GenericMessenger } from './generic-
|
|
2
|
-
export { a as GetDataType, b as GetReturnType, L as Logger, M as MaybePromise, c as Message, N as NamespaceMessagingConfig, P as ProtocolWithReturn, R as RemoveListenerCallback } from './generic-
|
|
1
|
+
import { B as BaseMessagingConfig, G as GenericMessenger } from './generic-c22f9780.js';
|
|
2
|
+
export { a as GetDataType, b as GetReturnType, L as Logger, M as MaybePromise, c as Message, N as NamespaceMessagingConfig, P as ProtocolWithReturn, R as RemoveListenerCallback } from './generic-c22f9780.js';
|
|
3
3
|
import { Runtime } from 'webextension-polyfill';
|
|
4
4
|
|
|
5
5
|
/**
|
package/lib/index.global.js
CHANGED
|
@@ -47,24 +47,20 @@ var webExtCoreMessaging = (() => {
|
|
|
47
47
|
mod
|
|
48
48
|
));
|
|
49
49
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
50
|
-
var __publicField = (obj, key, value) => {
|
|
51
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
52
|
-
return value;
|
|
53
|
-
};
|
|
54
50
|
var __async = (__this, __arguments, generator) => {
|
|
55
51
|
return new Promise((resolve, reject) => {
|
|
56
52
|
var fulfilled = (value) => {
|
|
57
53
|
try {
|
|
58
54
|
step(generator.next(value));
|
|
59
|
-
} catch (
|
|
60
|
-
reject(
|
|
55
|
+
} catch (e2) {
|
|
56
|
+
reject(e2);
|
|
61
57
|
}
|
|
62
58
|
};
|
|
63
59
|
var rejected = (value) => {
|
|
64
60
|
try {
|
|
65
61
|
step(generator.throw(value));
|
|
66
|
-
} catch (
|
|
67
|
-
reject(
|
|
62
|
+
} catch (e2) {
|
|
63
|
+
reject(e2);
|
|
68
64
|
}
|
|
69
65
|
};
|
|
70
66
|
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
@@ -1076,192 +1072,49 @@ var webExtCoreMessaging = (() => {
|
|
|
1076
1072
|
// src/extension.ts
|
|
1077
1073
|
var import_webextension_polyfill = __toESM(require_browser_polyfill(), 1);
|
|
1078
1074
|
|
|
1079
|
-
// ../../node_modules/serialize-error/
|
|
1080
|
-
var
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
TypeError,
|
|
1087
|
-
URIError,
|
|
1088
|
-
// Built-in errors
|
|
1089
|
-
globalThis.DOMException,
|
|
1090
|
-
// Node-specific errors
|
|
1091
|
-
// https://nodejs.org/api/errors.html
|
|
1092
|
-
globalThis.AssertionError,
|
|
1093
|
-
globalThis.SystemError
|
|
1094
|
-
].filter(Boolean).map(
|
|
1095
|
-
(constructor) => [constructor.name, constructor]
|
|
1096
|
-
);
|
|
1097
|
-
var errorConstructors = new Map(list);
|
|
1098
|
-
var error_constructors_default = errorConstructors;
|
|
1099
|
-
|
|
1100
|
-
// ../../node_modules/serialize-error/index.js
|
|
1101
|
-
var NonError = class extends Error {
|
|
1102
|
-
constructor(message) {
|
|
1103
|
-
super(NonError._prepareSuperMessage(message));
|
|
1104
|
-
__publicField(this, "name", "NonError");
|
|
1105
|
-
}
|
|
1106
|
-
static _prepareSuperMessage(message) {
|
|
1107
|
-
try {
|
|
1108
|
-
return JSON.stringify(message);
|
|
1109
|
-
} catch (e) {
|
|
1110
|
-
return String(message);
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
};
|
|
1114
|
-
var commonProperties = [
|
|
1115
|
-
{
|
|
1116
|
-
property: "name",
|
|
1117
|
-
enumerable: false
|
|
1118
|
-
},
|
|
1119
|
-
{
|
|
1120
|
-
property: "message",
|
|
1121
|
-
enumerable: false
|
|
1122
|
-
},
|
|
1123
|
-
{
|
|
1124
|
-
property: "stack",
|
|
1125
|
-
enumerable: false
|
|
1126
|
-
},
|
|
1127
|
-
{
|
|
1128
|
-
property: "code",
|
|
1129
|
-
enumerable: true
|
|
1130
|
-
},
|
|
1131
|
-
{
|
|
1132
|
-
property: "cause",
|
|
1133
|
-
enumerable: false
|
|
1134
|
-
}
|
|
1135
|
-
];
|
|
1136
|
-
var toJsonWasCalled = /* @__PURE__ */ new WeakSet();
|
|
1137
|
-
var toJSON = (from) => {
|
|
1138
|
-
toJsonWasCalled.add(from);
|
|
1139
|
-
const json = from.toJSON();
|
|
1140
|
-
toJsonWasCalled.delete(from);
|
|
1141
|
-
return json;
|
|
1142
|
-
};
|
|
1143
|
-
var getErrorConstructor = (name) => {
|
|
1075
|
+
// ../../node_modules/@aklinker1/zero-serialize-error/dist/index.mjs
|
|
1076
|
+
var e = `string`;
|
|
1077
|
+
var t = `object`;
|
|
1078
|
+
function n(n2) {
|
|
1079
|
+
return n2 != null && typeof n2 === t && typeof n2.name === e && typeof n2.message === e && typeof n2.stack === e;
|
|
1080
|
+
}
|
|
1081
|
+
function r(e2) {
|
|
1144
1082
|
var _a;
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
useToJSON,
|
|
1155
|
-
serialize
|
|
1156
|
-
}) => {
|
|
1157
|
-
if (!to) {
|
|
1158
|
-
if (Array.isArray(from)) {
|
|
1159
|
-
to = [];
|
|
1160
|
-
} else if (!serialize && isErrorLike(from)) {
|
|
1161
|
-
const Error2 = getErrorConstructor(from.name);
|
|
1162
|
-
to = new Error2();
|
|
1163
|
-
} else {
|
|
1164
|
-
to = {};
|
|
1165
|
-
}
|
|
1166
|
-
}
|
|
1167
|
-
seen.push(from);
|
|
1168
|
-
if (depth >= maxDepth) {
|
|
1169
|
-
return to;
|
|
1083
|
+
if (typeof e2 !== t || e2 == null)
|
|
1084
|
+
return e2;
|
|
1085
|
+
if (e2 instanceof Error)
|
|
1086
|
+
return __spreadValues(__spreadValues({ name: e2.name, message: e2.message, stack: (_a = e2.stack) != null ? _a : `` }, e2.cause ? { cause: r(e2.cause) } : {}), r(__spreadValues({}, e2)));
|
|
1087
|
+
if (Array.isArray(e2)) {
|
|
1088
|
+
let t2 = [];
|
|
1089
|
+
for (let n3 of e2)
|
|
1090
|
+
t2.push(r(n3));
|
|
1091
|
+
return t2;
|
|
1170
1092
|
}
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
from: value,
|
|
1176
|
-
seen: [...seen],
|
|
1177
|
-
forceEnumerable,
|
|
1178
|
-
maxDepth,
|
|
1179
|
-
depth,
|
|
1180
|
-
useToJSON,
|
|
1181
|
-
serialize
|
|
1182
|
-
});
|
|
1183
|
-
for (const [key, value] of Object.entries(from)) {
|
|
1184
|
-
if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
|
|
1185
|
-
to[key] = "[object Buffer]";
|
|
1186
|
-
continue;
|
|
1187
|
-
}
|
|
1188
|
-
if (value !== null && typeof value === "object" && typeof value.pipe === "function") {
|
|
1189
|
-
to[key] = "[object Stream]";
|
|
1190
|
-
continue;
|
|
1191
|
-
}
|
|
1192
|
-
if (typeof value === "function") {
|
|
1193
|
-
continue;
|
|
1194
|
-
}
|
|
1195
|
-
if (!value || typeof value !== "object") {
|
|
1196
|
-
try {
|
|
1197
|
-
to[key] = value;
|
|
1198
|
-
} catch (e) {
|
|
1199
|
-
}
|
|
1200
|
-
continue;
|
|
1201
|
-
}
|
|
1202
|
-
if (!seen.includes(from[key])) {
|
|
1203
|
-
depth++;
|
|
1204
|
-
to[key] = continueDestroyCircular(from[key]);
|
|
1205
|
-
continue;
|
|
1206
|
-
}
|
|
1207
|
-
to[key] = "[Circular]";
|
|
1208
|
-
}
|
|
1209
|
-
for (const { property, enumerable } of commonProperties) {
|
|
1210
|
-
if (typeof from[property] !== "undefined" && from[property] !== null) {
|
|
1211
|
-
Object.defineProperty(to, property, {
|
|
1212
|
-
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
|
|
1213
|
-
enumerable: forceEnumerable ? true : enumerable,
|
|
1214
|
-
configurable: true,
|
|
1215
|
-
writable: true
|
|
1216
|
-
});
|
|
1217
|
-
}
|
|
1218
|
-
}
|
|
1219
|
-
return to;
|
|
1220
|
-
};
|
|
1221
|
-
function serializeError(value, options = {}) {
|
|
1222
|
-
const {
|
|
1223
|
-
maxDepth = Number.POSITIVE_INFINITY,
|
|
1224
|
-
useToJSON = true
|
|
1225
|
-
} = options;
|
|
1226
|
-
if (typeof value === "object" && value !== null) {
|
|
1227
|
-
return destroyCircular({
|
|
1228
|
-
from: value,
|
|
1229
|
-
seen: [],
|
|
1230
|
-
forceEnumerable: true,
|
|
1231
|
-
maxDepth,
|
|
1232
|
-
depth: 0,
|
|
1233
|
-
useToJSON,
|
|
1234
|
-
serialize: true
|
|
1235
|
-
});
|
|
1236
|
-
}
|
|
1237
|
-
if (typeof value === "function") {
|
|
1238
|
-
return `[Function: ${value.name || "anonymous"}]`;
|
|
1239
|
-
}
|
|
1240
|
-
return value;
|
|
1093
|
+
let n2 = /* @__PURE__ */ Object.create(null);
|
|
1094
|
+
for (let [t2, i2] of Object.entries(e2))
|
|
1095
|
+
n2[t2] = r(i2);
|
|
1096
|
+
return n2;
|
|
1241
1097
|
}
|
|
1242
|
-
function
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1098
|
+
function i(e2) {
|
|
1099
|
+
if (n(e2)) {
|
|
1100
|
+
let t2 = Error(e2.message, e2.cause ? { cause: i(e2.cause) } : void 0);
|
|
1101
|
+
t2.name = e2.name, t2.stack = e2.stack;
|
|
1102
|
+
for (let [n2, r3] of Object.entries(e2))
|
|
1103
|
+
n2 !== `name` && n2 !== `message` && n2 !== `stack` && n2 !== `cause` && (t2[n2] = r3);
|
|
1104
|
+
return t2;
|
|
1246
1105
|
}
|
|
1247
|
-
if (
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
depth: 0,
|
|
1255
|
-
serialize: false
|
|
1256
|
-
});
|
|
1106
|
+
if (e2 == null || typeof e2 !== t)
|
|
1107
|
+
return e2;
|
|
1108
|
+
if (Array.isArray(e2)) {
|
|
1109
|
+
let t2 = [];
|
|
1110
|
+
for (let n2 of e2)
|
|
1111
|
+
t2.push(i(n2));
|
|
1112
|
+
return t2;
|
|
1257
1113
|
}
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
return
|
|
1262
|
-
}
|
|
1263
|
-
function isMinimumViableSerializedError(value) {
|
|
1264
|
-
return Boolean(value) && typeof value === "object" && "message" in value && !Array.isArray(value);
|
|
1114
|
+
let r2 = /* @__PURE__ */ Object.create(null);
|
|
1115
|
+
for (let [t2, n2] of Object.entries(e2))
|
|
1116
|
+
r2[t2] = i(n2);
|
|
1117
|
+
return r2;
|
|
1265
1118
|
}
|
|
1266
1119
|
|
|
1267
1120
|
// src/generic.ts
|
|
@@ -1294,7 +1147,7 @@ var webExtCoreMessaging = (() => {
|
|
|
1294
1147
|
const { res, err } = response != null ? response : { err: new Error("No response") };
|
|
1295
1148
|
(_d = config.logger) == null ? void 0 : _d.debug(`[messaging] sendMessage {id=${message.id}} \u140A\u2500`, { res, err });
|
|
1296
1149
|
if (err != null)
|
|
1297
|
-
throw
|
|
1150
|
+
throw i(err);
|
|
1298
1151
|
return res;
|
|
1299
1152
|
});
|
|
1300
1153
|
},
|
|
@@ -1307,16 +1160,17 @@ var webExtCoreMessaging = (() => {
|
|
|
1307
1160
|
removeRootListener = config.addRootListener((message) => {
|
|
1308
1161
|
var _a3, _b2;
|
|
1309
1162
|
if (typeof message.type != "string" || typeof message.timestamp !== "number") {
|
|
1310
|
-
if (config.
|
|
1163
|
+
if (config.throwOnUnknownMessageFormat) {
|
|
1164
|
+
const err = Error(
|
|
1165
|
+
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
1166
|
+
message
|
|
1167
|
+
)}`
|
|
1168
|
+
);
|
|
1169
|
+
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
1170
|
+
throw err;
|
|
1171
|
+
} else {
|
|
1311
1172
|
return;
|
|
1312
1173
|
}
|
|
1313
|
-
const err = Error(
|
|
1314
|
-
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
1315
|
-
message
|
|
1316
|
-
)}`
|
|
1317
|
-
);
|
|
1318
|
-
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
1319
|
-
throw err;
|
|
1320
1174
|
}
|
|
1321
1175
|
(_b2 = config == null ? void 0 : config.logger) == null ? void 0 : _b2.debug("[messaging] Received message", message);
|
|
1322
1176
|
const listener = perTypeListeners[message.type];
|
|
@@ -1333,7 +1187,7 @@ var webExtCoreMessaging = (() => {
|
|
|
1333
1187
|
}).catch((err) => {
|
|
1334
1188
|
var _a4;
|
|
1335
1189
|
(_a4 = config == null ? void 0 : config.logger) == null ? void 0 : _a4.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { err });
|
|
1336
|
-
return { err:
|
|
1190
|
+
return { err: r(err) };
|
|
1337
1191
|
});
|
|
1338
1192
|
});
|
|
1339
1193
|
}
|
package/lib/index.js
CHANGED
package/lib/page.cjs
CHANGED
|
@@ -74,11 +74,8 @@ __export(page_exports, {
|
|
|
74
74
|
});
|
|
75
75
|
module.exports = __toCommonJS(page_exports);
|
|
76
76
|
|
|
77
|
-
// src/window.ts
|
|
78
|
-
var import_uid = require("uid");
|
|
79
|
-
|
|
80
77
|
// src/generic.ts
|
|
81
|
-
var
|
|
78
|
+
var import_zero_serialize_error = require("@aklinker1/zero-serialize-error");
|
|
82
79
|
function defineGenericMessanging(config) {
|
|
83
80
|
let removeRootListener;
|
|
84
81
|
let perTypeListeners = {};
|
|
@@ -108,7 +105,7 @@ function defineGenericMessanging(config) {
|
|
|
108
105
|
const { res, err } = response != null ? response : { err: new Error("No response") };
|
|
109
106
|
(_d = config.logger) == null ? void 0 : _d.debug(`[messaging] sendMessage {id=${message.id}} \u140A\u2500`, { res, err });
|
|
110
107
|
if (err != null)
|
|
111
|
-
throw (0,
|
|
108
|
+
throw (0, import_zero_serialize_error.deserializeError)(err);
|
|
112
109
|
return res;
|
|
113
110
|
});
|
|
114
111
|
},
|
|
@@ -121,16 +118,17 @@ function defineGenericMessanging(config) {
|
|
|
121
118
|
removeRootListener = config.addRootListener((message) => {
|
|
122
119
|
var _a3, _b2;
|
|
123
120
|
if (typeof message.type != "string" || typeof message.timestamp !== "number") {
|
|
124
|
-
if (config.
|
|
121
|
+
if (config.throwOnUnknownMessageFormat) {
|
|
122
|
+
const err = Error(
|
|
123
|
+
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
124
|
+
message
|
|
125
|
+
)}`
|
|
126
|
+
);
|
|
127
|
+
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
128
|
+
throw err;
|
|
129
|
+
} else {
|
|
125
130
|
return;
|
|
126
131
|
}
|
|
127
|
-
const err = Error(
|
|
128
|
-
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
129
|
-
message
|
|
130
|
-
)}`
|
|
131
|
-
);
|
|
132
|
-
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
133
|
-
throw err;
|
|
134
132
|
}
|
|
135
133
|
(_b2 = config == null ? void 0 : config.logger) == null ? void 0 : _b2.debug("[messaging] Received message", message);
|
|
136
134
|
const listener = perTypeListeners[message.type];
|
|
@@ -147,7 +145,7 @@ function defineGenericMessanging(config) {
|
|
|
147
145
|
}).catch((err) => {
|
|
148
146
|
var _a4;
|
|
149
147
|
(_a4 = config == null ? void 0 : config.logger) == null ? void 0 : _a4.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { err });
|
|
150
|
-
return { err: (0,
|
|
148
|
+
return { err: (0, import_zero_serialize_error.serializeError)(err) };
|
|
151
149
|
});
|
|
152
150
|
});
|
|
153
151
|
}
|
|
@@ -174,16 +172,24 @@ function defineGenericMessanging(config) {
|
|
|
174
172
|
};
|
|
175
173
|
}
|
|
176
174
|
|
|
175
|
+
// src/utils.ts
|
|
176
|
+
function prepareCustomEventDict(data, options = { targetScope: window != null ? window : void 0 }) {
|
|
177
|
+
return typeof cloneInto !== "undefined" ? cloneInto(data, options.targetScope) : data;
|
|
178
|
+
}
|
|
179
|
+
function createId() {
|
|
180
|
+
return Math.random().toString(36).substring(2, 10);
|
|
181
|
+
}
|
|
182
|
+
|
|
177
183
|
// src/window.ts
|
|
178
184
|
var REQUEST_TYPE = "@webext-core/messaging/window";
|
|
179
185
|
var RESPONSE_TYPE = "@webext-core/messaging/window/response";
|
|
180
186
|
function defineWindowMessaging(config) {
|
|
181
187
|
const namespace = config.namespace;
|
|
182
|
-
const instanceId = (
|
|
188
|
+
const instanceId = createId();
|
|
183
189
|
let removeAdditionalListeners = [];
|
|
184
|
-
const sendWindowMessage = (message, targetOrigin) => new Promise((res) => {
|
|
190
|
+
const sendWindowMessage = (message, targetOrigin, targetWindow) => new Promise((res) => {
|
|
185
191
|
const responseListener = (event) => {
|
|
186
|
-
if (event.data.type === RESPONSE_TYPE && event.data.namespace === namespace && event.data.instanceId !== instanceId && event.data.message.type === message.type) {
|
|
192
|
+
if (event.data.type === RESPONSE_TYPE && event.data.namespace === namespace && event.data.instanceId !== instanceId && event.data.message.type === message.type && event.data.message.id === message.id) {
|
|
187
193
|
res(event.data.response);
|
|
188
194
|
removeResponseListener();
|
|
189
195
|
}
|
|
@@ -191,14 +197,14 @@ function defineWindowMessaging(config) {
|
|
|
191
197
|
const removeResponseListener = () => window.removeEventListener("message", responseListener);
|
|
192
198
|
removeAdditionalListeners.push(removeResponseListener);
|
|
193
199
|
window.addEventListener("message", responseListener);
|
|
194
|
-
window.postMessage(
|
|
200
|
+
(targetWindow != null ? targetWindow : window).postMessage(
|
|
195
201
|
{ type: REQUEST_TYPE, message, senderOrigin: location.origin, namespace, instanceId },
|
|
196
202
|
targetOrigin != null ? targetOrigin : "*"
|
|
197
203
|
);
|
|
198
204
|
});
|
|
199
205
|
const messenger = defineGenericMessanging(__spreadProps(__spreadValues({}, config), {
|
|
200
|
-
sendMessage(message, targetOrigin) {
|
|
201
|
-
return sendWindowMessage(message, targetOrigin);
|
|
206
|
+
sendMessage(message, targetOrigin, targetWindow) {
|
|
207
|
+
return sendWindowMessage(message, targetOrigin, targetWindow);
|
|
202
208
|
},
|
|
203
209
|
addRootListener(processMessage) {
|
|
204
210
|
const listener = (event) => __async(this, null, function* () {
|
|
@@ -226,25 +232,17 @@ function defineWindowMessaging(config) {
|
|
|
226
232
|
});
|
|
227
233
|
}
|
|
228
234
|
|
|
229
|
-
// src/custom-event.ts
|
|
230
|
-
var import_uid2 = require("uid");
|
|
231
|
-
|
|
232
|
-
// src/utils.ts
|
|
233
|
-
function prepareCustomEventDict(data, options = { targetScope: window != null ? window : void 0 }) {
|
|
234
|
-
return typeof cloneInto !== "undefined" ? cloneInto(data, options.targetScope) : data;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
235
|
// src/custom-event.ts
|
|
238
236
|
var REQUEST_EVENT = "@webext-core/messaging/custom-events";
|
|
239
237
|
var RESPONSE_EVENT = "@webext-core/messaging/custom-events/response";
|
|
240
238
|
function defineCustomEventMessaging(config) {
|
|
241
239
|
const namespace = config.namespace;
|
|
242
|
-
const instanceId = (
|
|
240
|
+
const instanceId = createId();
|
|
243
241
|
const removeAdditionalListeners = [];
|
|
244
242
|
const sendCustomMessage = (requestEvent) => new Promise((res) => {
|
|
245
243
|
const responseListener = (e) => {
|
|
246
244
|
const { detail } = e;
|
|
247
|
-
if (detail.namespace === namespace && detail.instanceId !== instanceId && detail.message.type === requestEvent.detail.message.type) {
|
|
245
|
+
if (detail.namespace === namespace && detail.instanceId !== instanceId && detail.message.type === requestEvent.detail.message.type && detail.message.id === requestEvent.detail.message.id) {
|
|
248
246
|
res(detail.response);
|
|
249
247
|
}
|
|
250
248
|
};
|
package/lib/page.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { N as NamespaceMessagingConfig, G as GenericMessenger } from './generic-
|
|
1
|
+
import { N as NamespaceMessagingConfig, G as GenericMessenger } from './generic-c22f9780.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Configuration passed into `defineWindowMessaging`.
|
|
@@ -11,8 +11,14 @@ interface WindowMessagingConfig extends NamespaceMessagingConfig {
|
|
|
11
11
|
*
|
|
12
12
|
* > See <https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#targetorigin> for more
|
|
13
13
|
* details.
|
|
14
|
+
*
|
|
15
|
+
* message is posted on window which can as per your need like
|
|
16
|
+
* parent window in iframe -> window.parent
|
|
17
|
+
* iframe content window -> iframe.contentWindow
|
|
18
|
+
* opener original window -> window.opener
|
|
19
|
+
* by default global window is used to send mesage
|
|
14
20
|
*/
|
|
15
|
-
type WindowSendMessageArgs = [targetOrigin?: string];
|
|
21
|
+
type WindowSendMessageArgs = [targetOrigin?: string, targetWindow?: Window];
|
|
16
22
|
type WindowMessenger<TProtocolMap extends Record<string, any>> = GenericMessenger<TProtocolMap, {}, WindowSendMessageArgs>;
|
|
17
23
|
/**
|
|
18
24
|
* Returns a `WindowMessenger`. It is backed by the `window.postMessage` API. It can be used to
|
package/lib/page.global.js
CHANGED
|
@@ -46,24 +46,20 @@ var webExtCoreMessaging = (() => {
|
|
|
46
46
|
return to;
|
|
47
47
|
};
|
|
48
48
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
49
|
-
var __publicField = (obj, key, value) => {
|
|
50
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
51
|
-
return value;
|
|
52
|
-
};
|
|
53
49
|
var __async = (__this, __arguments, generator) => {
|
|
54
50
|
return new Promise((resolve, reject) => {
|
|
55
51
|
var fulfilled = (value) => {
|
|
56
52
|
try {
|
|
57
53
|
step(generator.next(value));
|
|
58
|
-
} catch (
|
|
59
|
-
reject(
|
|
54
|
+
} catch (e2) {
|
|
55
|
+
reject(e2);
|
|
60
56
|
}
|
|
61
57
|
};
|
|
62
58
|
var rejected = (value) => {
|
|
63
59
|
try {
|
|
64
60
|
step(generator.throw(value));
|
|
65
|
-
} catch (
|
|
66
|
-
reject(
|
|
61
|
+
} catch (e2) {
|
|
62
|
+
reject(e2);
|
|
67
63
|
}
|
|
68
64
|
};
|
|
69
65
|
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
@@ -78,209 +74,49 @@ var webExtCoreMessaging = (() => {
|
|
|
78
74
|
defineWindowMessaging: () => defineWindowMessaging
|
|
79
75
|
});
|
|
80
76
|
|
|
81
|
-
// ../../node_modules/
|
|
82
|
-
var
|
|
83
|
-
var
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
while (IDX--)
|
|
87
|
-
HEX[IDX] = (IDX + 256).toString(16).substring(1);
|
|
88
|
-
function uid(len) {
|
|
89
|
-
var i = 0, tmp = len || 11;
|
|
90
|
-
if (!BUFFER || IDX + tmp > SIZE * 2) {
|
|
91
|
-
for (BUFFER = "", IDX = 0; i < SIZE; i++) {
|
|
92
|
-
BUFFER += HEX[Math.random() * 256 | 0];
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return BUFFER.substring(IDX, IDX++ + tmp);
|
|
77
|
+
// ../../node_modules/@aklinker1/zero-serialize-error/dist/index.mjs
|
|
78
|
+
var e = `string`;
|
|
79
|
+
var t = `object`;
|
|
80
|
+
function n(n2) {
|
|
81
|
+
return n2 != null && typeof n2 === t && typeof n2.name === e && typeof n2.message === e && typeof n2.stack === e;
|
|
96
82
|
}
|
|
97
|
-
|
|
98
|
-
// ../../node_modules/serialize-error/error-constructors.js
|
|
99
|
-
var list = [
|
|
100
|
-
// Native ES errors https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
|
|
101
|
-
EvalError,
|
|
102
|
-
RangeError,
|
|
103
|
-
ReferenceError,
|
|
104
|
-
SyntaxError,
|
|
105
|
-
TypeError,
|
|
106
|
-
URIError,
|
|
107
|
-
// Built-in errors
|
|
108
|
-
globalThis.DOMException,
|
|
109
|
-
// Node-specific errors
|
|
110
|
-
// https://nodejs.org/api/errors.html
|
|
111
|
-
globalThis.AssertionError,
|
|
112
|
-
globalThis.SystemError
|
|
113
|
-
].filter(Boolean).map(
|
|
114
|
-
(constructor) => [constructor.name, constructor]
|
|
115
|
-
);
|
|
116
|
-
var errorConstructors = new Map(list);
|
|
117
|
-
var error_constructors_default = errorConstructors;
|
|
118
|
-
|
|
119
|
-
// ../../node_modules/serialize-error/index.js
|
|
120
|
-
var NonError = class extends Error {
|
|
121
|
-
constructor(message) {
|
|
122
|
-
super(NonError._prepareSuperMessage(message));
|
|
123
|
-
__publicField(this, "name", "NonError");
|
|
124
|
-
}
|
|
125
|
-
static _prepareSuperMessage(message) {
|
|
126
|
-
try {
|
|
127
|
-
return JSON.stringify(message);
|
|
128
|
-
} catch (e) {
|
|
129
|
-
return String(message);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
var commonProperties = [
|
|
134
|
-
{
|
|
135
|
-
property: "name",
|
|
136
|
-
enumerable: false
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
property: "message",
|
|
140
|
-
enumerable: false
|
|
141
|
-
},
|
|
142
|
-
{
|
|
143
|
-
property: "stack",
|
|
144
|
-
enumerable: false
|
|
145
|
-
},
|
|
146
|
-
{
|
|
147
|
-
property: "code",
|
|
148
|
-
enumerable: true
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
property: "cause",
|
|
152
|
-
enumerable: false
|
|
153
|
-
}
|
|
154
|
-
];
|
|
155
|
-
var toJsonWasCalled = /* @__PURE__ */ new WeakSet();
|
|
156
|
-
var toJSON = (from) => {
|
|
157
|
-
toJsonWasCalled.add(from);
|
|
158
|
-
const json = from.toJSON();
|
|
159
|
-
toJsonWasCalled.delete(from);
|
|
160
|
-
return json;
|
|
161
|
-
};
|
|
162
|
-
var getErrorConstructor = (name) => {
|
|
83
|
+
function r(e2) {
|
|
163
84
|
var _a;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
useToJSON,
|
|
174
|
-
serialize
|
|
175
|
-
}) => {
|
|
176
|
-
if (!to) {
|
|
177
|
-
if (Array.isArray(from)) {
|
|
178
|
-
to = [];
|
|
179
|
-
} else if (!serialize && isErrorLike(from)) {
|
|
180
|
-
const Error2 = getErrorConstructor(from.name);
|
|
181
|
-
to = new Error2();
|
|
182
|
-
} else {
|
|
183
|
-
to = {};
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
seen.push(from);
|
|
187
|
-
if (depth >= maxDepth) {
|
|
188
|
-
return to;
|
|
189
|
-
}
|
|
190
|
-
if (useToJSON && typeof from.toJSON === "function" && !toJsonWasCalled.has(from)) {
|
|
191
|
-
return toJSON(from);
|
|
192
|
-
}
|
|
193
|
-
const continueDestroyCircular = (value) => destroyCircular({
|
|
194
|
-
from: value,
|
|
195
|
-
seen: [...seen],
|
|
196
|
-
forceEnumerable,
|
|
197
|
-
maxDepth,
|
|
198
|
-
depth,
|
|
199
|
-
useToJSON,
|
|
200
|
-
serialize
|
|
201
|
-
});
|
|
202
|
-
for (const [key, value] of Object.entries(from)) {
|
|
203
|
-
if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
|
|
204
|
-
to[key] = "[object Buffer]";
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
if (value !== null && typeof value === "object" && typeof value.pipe === "function") {
|
|
208
|
-
to[key] = "[object Stream]";
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
if (typeof value === "function") {
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
if (!value || typeof value !== "object") {
|
|
215
|
-
try {
|
|
216
|
-
to[key] = value;
|
|
217
|
-
} catch (e) {
|
|
218
|
-
}
|
|
219
|
-
continue;
|
|
220
|
-
}
|
|
221
|
-
if (!seen.includes(from[key])) {
|
|
222
|
-
depth++;
|
|
223
|
-
to[key] = continueDestroyCircular(from[key]);
|
|
224
|
-
continue;
|
|
225
|
-
}
|
|
226
|
-
to[key] = "[Circular]";
|
|
227
|
-
}
|
|
228
|
-
for (const { property, enumerable } of commonProperties) {
|
|
229
|
-
if (typeof from[property] !== "undefined" && from[property] !== null) {
|
|
230
|
-
Object.defineProperty(to, property, {
|
|
231
|
-
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
|
|
232
|
-
enumerable: forceEnumerable ? true : enumerable,
|
|
233
|
-
configurable: true,
|
|
234
|
-
writable: true
|
|
235
|
-
});
|
|
236
|
-
}
|
|
85
|
+
if (typeof e2 !== t || e2 == null)
|
|
86
|
+
return e2;
|
|
87
|
+
if (e2 instanceof Error)
|
|
88
|
+
return __spreadValues(__spreadValues({ name: e2.name, message: e2.message, stack: (_a = e2.stack) != null ? _a : `` }, e2.cause ? { cause: r(e2.cause) } : {}), r(__spreadValues({}, e2)));
|
|
89
|
+
if (Array.isArray(e2)) {
|
|
90
|
+
let t2 = [];
|
|
91
|
+
for (let n3 of e2)
|
|
92
|
+
t2.push(r(n3));
|
|
93
|
+
return t2;
|
|
237
94
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
maxDepth = Number.POSITIVE_INFINITY,
|
|
243
|
-
useToJSON = true
|
|
244
|
-
} = options;
|
|
245
|
-
if (typeof value === "object" && value !== null) {
|
|
246
|
-
return destroyCircular({
|
|
247
|
-
from: value,
|
|
248
|
-
seen: [],
|
|
249
|
-
forceEnumerable: true,
|
|
250
|
-
maxDepth,
|
|
251
|
-
depth: 0,
|
|
252
|
-
useToJSON,
|
|
253
|
-
serialize: true
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
if (typeof value === "function") {
|
|
257
|
-
return `[Function: ${value.name || "anonymous"}]`;
|
|
258
|
-
}
|
|
259
|
-
return value;
|
|
95
|
+
let n2 = /* @__PURE__ */ Object.create(null);
|
|
96
|
+
for (let [t2, i2] of Object.entries(e2))
|
|
97
|
+
n2[t2] = r(i2);
|
|
98
|
+
return n2;
|
|
260
99
|
}
|
|
261
|
-
function
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
100
|
+
function i(e2) {
|
|
101
|
+
if (n(e2)) {
|
|
102
|
+
let t2 = Error(e2.message, e2.cause ? { cause: i(e2.cause) } : void 0);
|
|
103
|
+
t2.name = e2.name, t2.stack = e2.stack;
|
|
104
|
+
for (let [n2, r3] of Object.entries(e2))
|
|
105
|
+
n2 !== `name` && n2 !== `message` && n2 !== `stack` && n2 !== `cause` && (t2[n2] = r3);
|
|
106
|
+
return t2;
|
|
265
107
|
}
|
|
266
|
-
if (
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
depth: 0,
|
|
274
|
-
serialize: false
|
|
275
|
-
});
|
|
108
|
+
if (e2 == null || typeof e2 !== t)
|
|
109
|
+
return e2;
|
|
110
|
+
if (Array.isArray(e2)) {
|
|
111
|
+
let t2 = [];
|
|
112
|
+
for (let n2 of e2)
|
|
113
|
+
t2.push(i(n2));
|
|
114
|
+
return t2;
|
|
276
115
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return
|
|
281
|
-
}
|
|
282
|
-
function isMinimumViableSerializedError(value) {
|
|
283
|
-
return Boolean(value) && typeof value === "object" && "message" in value && !Array.isArray(value);
|
|
116
|
+
let r2 = /* @__PURE__ */ Object.create(null);
|
|
117
|
+
for (let [t2, n2] of Object.entries(e2))
|
|
118
|
+
r2[t2] = i(n2);
|
|
119
|
+
return r2;
|
|
284
120
|
}
|
|
285
121
|
|
|
286
122
|
// src/generic.ts
|
|
@@ -313,7 +149,7 @@ var webExtCoreMessaging = (() => {
|
|
|
313
149
|
const { res, err } = response != null ? response : { err: new Error("No response") };
|
|
314
150
|
(_d = config.logger) == null ? void 0 : _d.debug(`[messaging] sendMessage {id=${message.id}} \u140A\u2500`, { res, err });
|
|
315
151
|
if (err != null)
|
|
316
|
-
throw
|
|
152
|
+
throw i(err);
|
|
317
153
|
return res;
|
|
318
154
|
});
|
|
319
155
|
},
|
|
@@ -326,16 +162,17 @@ var webExtCoreMessaging = (() => {
|
|
|
326
162
|
removeRootListener = config.addRootListener((message) => {
|
|
327
163
|
var _a3, _b2;
|
|
328
164
|
if (typeof message.type != "string" || typeof message.timestamp !== "number") {
|
|
329
|
-
if (config.
|
|
165
|
+
if (config.throwOnUnknownMessageFormat) {
|
|
166
|
+
const err = Error(
|
|
167
|
+
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
168
|
+
message
|
|
169
|
+
)}`
|
|
170
|
+
);
|
|
171
|
+
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
172
|
+
throw err;
|
|
173
|
+
} else {
|
|
330
174
|
return;
|
|
331
175
|
}
|
|
332
|
-
const err = Error(
|
|
333
|
-
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
|
|
334
|
-
message
|
|
335
|
-
)}`
|
|
336
|
-
);
|
|
337
|
-
(_a3 = config.logger) == null ? void 0 : _a3.error(err);
|
|
338
|
-
throw err;
|
|
339
176
|
}
|
|
340
177
|
(_b2 = config == null ? void 0 : config.logger) == null ? void 0 : _b2.debug("[messaging] Received message", message);
|
|
341
178
|
const listener = perTypeListeners[message.type];
|
|
@@ -352,7 +189,7 @@ var webExtCoreMessaging = (() => {
|
|
|
352
189
|
}).catch((err) => {
|
|
353
190
|
var _a4;
|
|
354
191
|
(_a4 = config == null ? void 0 : config.logger) == null ? void 0 : _a4.debug(`[messaging] onMessage {id=${message.id}} \u2500\u1405`, { err });
|
|
355
|
-
return { err:
|
|
192
|
+
return { err: r(err) };
|
|
356
193
|
});
|
|
357
194
|
});
|
|
358
195
|
}
|
|
@@ -379,16 +216,24 @@ var webExtCoreMessaging = (() => {
|
|
|
379
216
|
};
|
|
380
217
|
}
|
|
381
218
|
|
|
219
|
+
// src/utils.ts
|
|
220
|
+
function prepareCustomEventDict(data, options = { targetScope: window != null ? window : void 0 }) {
|
|
221
|
+
return typeof cloneInto !== "undefined" ? cloneInto(data, options.targetScope) : data;
|
|
222
|
+
}
|
|
223
|
+
function createId() {
|
|
224
|
+
return Math.random().toString(36).substring(2, 10);
|
|
225
|
+
}
|
|
226
|
+
|
|
382
227
|
// src/window.ts
|
|
383
228
|
var REQUEST_TYPE = "@webext-core/messaging/window";
|
|
384
229
|
var RESPONSE_TYPE = "@webext-core/messaging/window/response";
|
|
385
230
|
function defineWindowMessaging(config) {
|
|
386
231
|
const namespace = config.namespace;
|
|
387
|
-
const instanceId =
|
|
232
|
+
const instanceId = createId();
|
|
388
233
|
let removeAdditionalListeners = [];
|
|
389
|
-
const sendWindowMessage = (message, targetOrigin) => new Promise((res) => {
|
|
234
|
+
const sendWindowMessage = (message, targetOrigin, targetWindow) => new Promise((res) => {
|
|
390
235
|
const responseListener = (event) => {
|
|
391
|
-
if (event.data.type === RESPONSE_TYPE && event.data.namespace === namespace && event.data.instanceId !== instanceId && event.data.message.type === message.type) {
|
|
236
|
+
if (event.data.type === RESPONSE_TYPE && event.data.namespace === namespace && event.data.instanceId !== instanceId && event.data.message.type === message.type && event.data.message.id === message.id) {
|
|
392
237
|
res(event.data.response);
|
|
393
238
|
removeResponseListener();
|
|
394
239
|
}
|
|
@@ -396,14 +241,14 @@ var webExtCoreMessaging = (() => {
|
|
|
396
241
|
const removeResponseListener = () => window.removeEventListener("message", responseListener);
|
|
397
242
|
removeAdditionalListeners.push(removeResponseListener);
|
|
398
243
|
window.addEventListener("message", responseListener);
|
|
399
|
-
window.postMessage(
|
|
244
|
+
(targetWindow != null ? targetWindow : window).postMessage(
|
|
400
245
|
{ type: REQUEST_TYPE, message, senderOrigin: location.origin, namespace, instanceId },
|
|
401
246
|
targetOrigin != null ? targetOrigin : "*"
|
|
402
247
|
);
|
|
403
248
|
});
|
|
404
249
|
const messenger = defineGenericMessanging(__spreadProps(__spreadValues({}, config), {
|
|
405
|
-
sendMessage(message, targetOrigin) {
|
|
406
|
-
return sendWindowMessage(message, targetOrigin);
|
|
250
|
+
sendMessage(message, targetOrigin, targetWindow) {
|
|
251
|
+
return sendWindowMessage(message, targetOrigin, targetWindow);
|
|
407
252
|
},
|
|
408
253
|
addRootListener(processMessage) {
|
|
409
254
|
const listener = (event) => __async(this, null, function* () {
|
|
@@ -431,22 +276,17 @@ var webExtCoreMessaging = (() => {
|
|
|
431
276
|
});
|
|
432
277
|
}
|
|
433
278
|
|
|
434
|
-
// src/utils.ts
|
|
435
|
-
function prepareCustomEventDict(data, options = { targetScope: window != null ? window : void 0 }) {
|
|
436
|
-
return typeof cloneInto !== "undefined" ? cloneInto(data, options.targetScope) : data;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
279
|
// src/custom-event.ts
|
|
440
280
|
var REQUEST_EVENT = "@webext-core/messaging/custom-events";
|
|
441
281
|
var RESPONSE_EVENT = "@webext-core/messaging/custom-events/response";
|
|
442
282
|
function defineCustomEventMessaging(config) {
|
|
443
283
|
const namespace = config.namespace;
|
|
444
|
-
const instanceId =
|
|
284
|
+
const instanceId = createId();
|
|
445
285
|
const removeAdditionalListeners = [];
|
|
446
286
|
const sendCustomMessage = (requestEvent) => new Promise((res) => {
|
|
447
|
-
const responseListener = (
|
|
448
|
-
const { detail } =
|
|
449
|
-
if (detail.namespace === namespace && detail.instanceId !== instanceId && detail.message.type === requestEvent.detail.message.type) {
|
|
287
|
+
const responseListener = (e2) => {
|
|
288
|
+
const { detail } = e2;
|
|
289
|
+
if (detail.namespace === namespace && detail.instanceId !== instanceId && detail.message.type === requestEvent.detail.message.type && detail.message.id === requestEvent.detail.message.id) {
|
|
450
290
|
res(detail.response);
|
|
451
291
|
}
|
|
452
292
|
};
|
|
@@ -465,8 +305,8 @@ var webExtCoreMessaging = (() => {
|
|
|
465
305
|
return sendCustomMessage(requestEvent);
|
|
466
306
|
},
|
|
467
307
|
addRootListener(processMessage) {
|
|
468
|
-
const requestListener = (
|
|
469
|
-
const _a =
|
|
308
|
+
const requestListener = (e2) => __async(this, null, function* () {
|
|
309
|
+
const _a = e2, { detail } = _a, event = __objRest(_a, ["detail"]);
|
|
470
310
|
if (detail.namespace !== namespace || detail.instanceId === instanceId)
|
|
471
311
|
return;
|
|
472
312
|
const message = __spreadProps(__spreadValues({}, detail.message), { event });
|
package/lib/page.js
CHANGED
|
@@ -4,19 +4,26 @@ import {
|
|
|
4
4
|
__spreadProps,
|
|
5
5
|
__spreadValues,
|
|
6
6
|
defineGenericMessanging
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-GMKXECDR.js";
|
|
8
|
+
|
|
9
|
+
// src/utils.ts
|
|
10
|
+
function prepareCustomEventDict(data, options = { targetScope: window != null ? window : void 0 }) {
|
|
11
|
+
return typeof cloneInto !== "undefined" ? cloneInto(data, options.targetScope) : data;
|
|
12
|
+
}
|
|
13
|
+
function createId() {
|
|
14
|
+
return Math.random().toString(36).substring(2, 10);
|
|
15
|
+
}
|
|
8
16
|
|
|
9
17
|
// src/window.ts
|
|
10
|
-
import { uid } from "uid";
|
|
11
18
|
var REQUEST_TYPE = "@webext-core/messaging/window";
|
|
12
19
|
var RESPONSE_TYPE = "@webext-core/messaging/window/response";
|
|
13
20
|
function defineWindowMessaging(config) {
|
|
14
21
|
const namespace = config.namespace;
|
|
15
|
-
const instanceId =
|
|
22
|
+
const instanceId = createId();
|
|
16
23
|
let removeAdditionalListeners = [];
|
|
17
|
-
const sendWindowMessage = (message, targetOrigin) => new Promise((res) => {
|
|
24
|
+
const sendWindowMessage = (message, targetOrigin, targetWindow) => new Promise((res) => {
|
|
18
25
|
const responseListener = (event) => {
|
|
19
|
-
if (event.data.type === RESPONSE_TYPE && event.data.namespace === namespace && event.data.instanceId !== instanceId && event.data.message.type === message.type) {
|
|
26
|
+
if (event.data.type === RESPONSE_TYPE && event.data.namespace === namespace && event.data.instanceId !== instanceId && event.data.message.type === message.type && event.data.message.id === message.id) {
|
|
20
27
|
res(event.data.response);
|
|
21
28
|
removeResponseListener();
|
|
22
29
|
}
|
|
@@ -24,14 +31,14 @@ function defineWindowMessaging(config) {
|
|
|
24
31
|
const removeResponseListener = () => window.removeEventListener("message", responseListener);
|
|
25
32
|
removeAdditionalListeners.push(removeResponseListener);
|
|
26
33
|
window.addEventListener("message", responseListener);
|
|
27
|
-
window.postMessage(
|
|
34
|
+
(targetWindow != null ? targetWindow : window).postMessage(
|
|
28
35
|
{ type: REQUEST_TYPE, message, senderOrigin: location.origin, namespace, instanceId },
|
|
29
36
|
targetOrigin != null ? targetOrigin : "*"
|
|
30
37
|
);
|
|
31
38
|
});
|
|
32
39
|
const messenger = defineGenericMessanging(__spreadProps(__spreadValues({}, config), {
|
|
33
|
-
sendMessage(message, targetOrigin) {
|
|
34
|
-
return sendWindowMessage(message, targetOrigin);
|
|
40
|
+
sendMessage(message, targetOrigin, targetWindow) {
|
|
41
|
+
return sendWindowMessage(message, targetOrigin, targetWindow);
|
|
35
42
|
},
|
|
36
43
|
addRootListener(processMessage) {
|
|
37
44
|
const listener = (event) => __async(this, null, function* () {
|
|
@@ -59,25 +66,17 @@ function defineWindowMessaging(config) {
|
|
|
59
66
|
});
|
|
60
67
|
}
|
|
61
68
|
|
|
62
|
-
// src/custom-event.ts
|
|
63
|
-
import { uid as uid2 } from "uid";
|
|
64
|
-
|
|
65
|
-
// src/utils.ts
|
|
66
|
-
function prepareCustomEventDict(data, options = { targetScope: window != null ? window : void 0 }) {
|
|
67
|
-
return typeof cloneInto !== "undefined" ? cloneInto(data, options.targetScope) : data;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
69
|
// src/custom-event.ts
|
|
71
70
|
var REQUEST_EVENT = "@webext-core/messaging/custom-events";
|
|
72
71
|
var RESPONSE_EVENT = "@webext-core/messaging/custom-events/response";
|
|
73
72
|
function defineCustomEventMessaging(config) {
|
|
74
73
|
const namespace = config.namespace;
|
|
75
|
-
const instanceId =
|
|
74
|
+
const instanceId = createId();
|
|
76
75
|
const removeAdditionalListeners = [];
|
|
77
76
|
const sendCustomMessage = (requestEvent) => new Promise((res) => {
|
|
78
77
|
const responseListener = (e) => {
|
|
79
78
|
const { detail } = e;
|
|
80
|
-
if (detail.namespace === namespace && detail.instanceId !== instanceId && detail.message.type === requestEvent.detail.message.type) {
|
|
79
|
+
if (detail.namespace === namespace && detail.instanceId !== instanceId && detail.message.type === requestEvent.detail.message.type && detail.message.id === requestEvent.detail.message.id) {
|
|
81
80
|
res(detail.response);
|
|
82
81
|
}
|
|
83
82
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webext-core/messaging",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "Light weight, type-safe wrapper around the web extension messaging APIs. Supports all browsers (Chrome, Firefox, Safari)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -61,11 +61,11 @@
|
|
|
61
61
|
"test:node": "buildc --deps-only -- vitest -r src --config ../vitest.config.node.ts",
|
|
62
62
|
"test:browser": "buildc --deps-only -- vitest -r src --config ../vitest.config.browser.ts",
|
|
63
63
|
"test:coverage": "buildc --deps-only -- bun run test:node --coverage && bun run test:browser --coverage",
|
|
64
|
-
"check": "buildc --deps-only -- tsc --noEmit"
|
|
64
|
+
"check": "buildc --deps-only -- tsc --noEmit",
|
|
65
|
+
"prepack": "bun run build"
|
|
65
66
|
},
|
|
66
67
|
"dependencies": {
|
|
67
|
-
"serialize-error": "^
|
|
68
|
-
"uid": "^2.0.2",
|
|
68
|
+
"@aklinker1/zero-serialize-error": "^1.0.2",
|
|
69
69
|
"webextension-polyfill": "^0.10.0"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|