@webext-core/messaging 1.4.0 → 2.0.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.
@@ -11,7 +11,7 @@ interface Logger {
11
11
  * Either a Promise of a type, or that type directly. Used to indicate that a method can by sync or
12
12
  * async.
13
13
  */
14
- declare type MaybePromise<T> = Promise<T> | T;
14
+ type MaybePromise<T> = Promise<T> | T;
15
15
  /**
16
16
  * Used to add a return type to a message in the protocol map.
17
17
  *
@@ -40,17 +40,17 @@ interface ProtocolWithReturn<TData, TReturn> {
40
40
  /**
41
41
  * Given a function declaration, `ProtocolWithReturn`, or a value, return the message's data type.
42
42
  */
43
- declare type GetDataType<T> = T extends (...args: infer Args) => any ? Args['length'] extends 0 | 1 ? Args[0] : never : T extends ProtocolWithReturn<any, any> ? T['BtVgCTPYZu'] : T;
43
+ type GetDataType<T> = T extends (...args: infer Args) => any ? Args['length'] extends 0 | 1 ? Args[0] : never : T extends ProtocolWithReturn<any, any> ? T['BtVgCTPYZu'] : T;
44
44
  /**
45
45
  * Given a function declaration, `ProtocolWithReturn`, or a value, return the message's return type.
46
46
  */
47
- declare type GetReturnType<T> = T extends (...args: any[]) => infer R ? R : T extends ProtocolWithReturn<any, any> ? T['RrhVseLgZW'] : void;
47
+ type GetReturnType<T> = T extends (...args: any[]) => infer R ? R : T extends ProtocolWithReturn<any, any> ? T['RrhVseLgZW'] : void;
48
48
  /**
49
49
  * Call to ensure an active listener has been removed.
50
50
  *
51
51
  * If the listener has already been removed with `Messenger.removeAllListeners`, this is a noop.
52
52
  */
53
- declare type RemoveListenerCallback = () => void;
53
+ type RemoveListenerCallback = () => void;
54
54
  /**
55
55
  * Shared configuration between all the different messengers.
56
56
  */
package/lib/index.cjs CHANGED
@@ -35,6 +35,10 @@ var __copyProps = (to, from, except, desc) => {
35
35
  return to;
36
36
  };
37
37
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
38
+ // If the importer is in node compatibility mode or this is not an ESM
39
+ // file that has been converted to a CommonJS file using a Babel-
40
+ // compatible transform (i.e. "__esModule" has not been set), then set
41
+ // "default" to the CommonJS "module.exports" for node compatibility.
38
42
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
39
43
  mod
40
44
  ));
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { B as BaseMessagingConfig, G as GenericMessenger } from './generic-e806d3a5.js';
2
- export { B as BaseMessagingConfig, 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-e806d3a5.js';
1
+ import { B as BaseMessagingConfig, G as GenericMessenger } from './generic-77a327a6.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-77a327a6.js';
3
3
  import { Runtime } from 'webextension-polyfill';
4
4
 
5
5
  /**
@@ -23,7 +23,7 @@ interface ExtensionMessage {
23
23
  *
24
24
  * You cannot message between tabs directly. It must go through the background script.
25
25
  */
26
- declare type ExtensionSendMessageArgs = [
26
+ type ExtensionSendMessageArgs = [
27
27
  /**
28
28
  * The tab to send a message to.
29
29
  */
@@ -32,7 +32,7 @@ declare type ExtensionSendMessageArgs = [
32
32
  /**
33
33
  * Messenger returned by `defineExtensionMessaging`.
34
34
  */
35
- declare type ExtensionMessenger<TProtocolMap extends Record<string, any>> = GenericMessenger<TProtocolMap, ExtensionMessage, ExtensionSendMessageArgs>;
35
+ type ExtensionMessenger<TProtocolMap extends Record<string, any>> = GenericMessenger<TProtocolMap, ExtensionMessage, ExtensionSendMessageArgs>;
36
36
  /**
37
37
  * Returns an `ExtensionMessenger` that is backed by the `browser.runtime.sendMessage` and
38
38
  * `browser.tabs.sendMessage` APIs.
@@ -41,4 +41,4 @@ declare type ExtensionMessenger<TProtocolMap extends Record<string, any>> = Gene
41
41
  */
42
42
  declare function defineExtensionMessaging<TProtocolMap extends Record<string, any> = Record<string, any>>(config?: ExtensionMessagingConfig): ExtensionMessenger<TProtocolMap>;
43
43
 
44
- export { ExtensionMessage, ExtensionMessagingConfig, ExtensionMessenger, ExtensionSendMessageArgs, defineExtensionMessaging };
44
+ export { BaseMessagingConfig, ExtensionMessage, ExtensionMessagingConfig, ExtensionMessenger, ExtensionSendMessageArgs, defineExtensionMessaging };
@@ -39,6 +39,10 @@ var webExtCoreMessaging = (() => {
39
39
  return to;
40
40
  };
41
41
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
42
+ // If the importer is in node compatibility mode or this is not an ESM
43
+ // file that has been converted to a CommonJS file using a Babel-
44
+ // compatible transform (i.e. "__esModule" has not been set), then set
45
+ // "default" to the CommonJS "module.exports" for node compatibility.
42
46
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
43
47
  mod
44
48
  ));
@@ -68,9 +72,9 @@ var webExtCoreMessaging = (() => {
68
72
  });
69
73
  };
70
74
 
71
- // ../../node_modules/.pnpm/webextension-polyfill@0.10.0/node_modules/webextension-polyfill/dist/browser-polyfill.js
75
+ // ../../node_modules/webextension-polyfill/dist/browser-polyfill.js
72
76
  var require_browser_polyfill = __commonJS({
73
- "../../node_modules/.pnpm/webextension-polyfill@0.10.0/node_modules/webextension-polyfill/dist/browser-polyfill.js"(exports, module) {
77
+ "../../node_modules/webextension-polyfill/dist/browser-polyfill.js"(exports, module) {
74
78
  (function(global, factory) {
75
79
  if (typeof define === "function" && define.amd) {
76
80
  define("webextension-polyfill", ["module"], factory);
@@ -1072,15 +1076,19 @@ var webExtCoreMessaging = (() => {
1072
1076
  // src/extension.ts
1073
1077
  var import_webextension_polyfill = __toESM(require_browser_polyfill(), 1);
1074
1078
 
1075
- // ../../node_modules/.pnpm/serialize-error@11.0.0/node_modules/serialize-error/error-constructors.js
1079
+ // ../../node_modules/serialize-error/error-constructors.js
1076
1080
  var list = [
1081
+ // Native ES errors https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
1077
1082
  EvalError,
1078
1083
  RangeError,
1079
1084
  ReferenceError,
1080
1085
  SyntaxError,
1081
1086
  TypeError,
1082
1087
  URIError,
1088
+ // Built-in errors
1083
1089
  globalThis.DOMException,
1090
+ // Node-specific errors
1091
+ // https://nodejs.org/api/errors.html
1084
1092
  globalThis.AssertionError,
1085
1093
  globalThis.SystemError
1086
1094
  ].filter(Boolean).map(
@@ -1089,7 +1097,7 @@ var webExtCoreMessaging = (() => {
1089
1097
  var errorConstructors = new Map(list);
1090
1098
  var error_constructors_default = errorConstructors;
1091
1099
 
1092
- // ../../node_modules/.pnpm/serialize-error@11.0.0/node_modules/serialize-error/index.js
1100
+ // ../../node_modules/serialize-error/index.js
1093
1101
  var NonError = class extends Error {
1094
1102
  constructor(message) {
1095
1103
  super(NonError._prepareSuperMessage(message));
@@ -1125,11 +1133,11 @@ var webExtCoreMessaging = (() => {
1125
1133
  enumerable: false
1126
1134
  }
1127
1135
  ];
1128
- var toJsonWasCalled = Symbol(".toJSON was called");
1136
+ var toJsonWasCalled = /* @__PURE__ */ new WeakSet();
1129
1137
  var toJSON = (from) => {
1130
- from[toJsonWasCalled] = true;
1138
+ toJsonWasCalled.add(from);
1131
1139
  const json = from.toJSON();
1132
- delete from[toJsonWasCalled];
1140
+ toJsonWasCalled.delete(from);
1133
1141
  return json;
1134
1142
  };
1135
1143
  var getErrorConstructor = (name) => {
@@ -1160,7 +1168,7 @@ var webExtCoreMessaging = (() => {
1160
1168
  if (depth >= maxDepth) {
1161
1169
  return to;
1162
1170
  }
1163
- if (useToJSON && typeof from.toJSON === "function" && from[toJsonWasCalled] !== true) {
1171
+ if (useToJSON && typeof from.toJSON === "function" && !toJsonWasCalled.has(from)) {
1164
1172
  return toJSON(from);
1165
1173
  }
1166
1174
  const continueDestroyCircular = (value) => destroyCircular({
@@ -1173,7 +1181,7 @@ var webExtCoreMessaging = (() => {
1173
1181
  serialize
1174
1182
  });
1175
1183
  for (const [key, value] of Object.entries(from)) {
1176
- if (typeof Buffer === "function" && Buffer.isBuffer(value)) {
1184
+ if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
1177
1185
  to[key] = "[object Buffer]";
1178
1186
  continue;
1179
1187
  }
@@ -1185,7 +1193,10 @@ var webExtCoreMessaging = (() => {
1185
1193
  continue;
1186
1194
  }
1187
1195
  if (!value || typeof value !== "object") {
1188
- to[key] = value;
1196
+ try {
1197
+ to[key] = value;
1198
+ } catch (e) {
1199
+ }
1189
1200
  continue;
1190
1201
  }
1191
1202
  if (!seen.includes(from[key])) {
@@ -1208,7 +1219,6 @@ var webExtCoreMessaging = (() => {
1208
1219
  return to;
1209
1220
  };
1210
1221
  function serializeError(value, options = {}) {
1211
- var _a;
1212
1222
  const {
1213
1223
  maxDepth = Number.POSITIVE_INFINITY,
1214
1224
  useToJSON = true
@@ -1225,7 +1235,7 @@ var webExtCoreMessaging = (() => {
1225
1235
  });
1226
1236
  }
1227
1237
  if (typeof value === "function") {
1228
- return `[Function: ${(_a = value.name) != null ? _a : "anonymous"}]`;
1238
+ return `[Function: ${value.name || "anonymous"}]`;
1229
1239
  }
1230
1240
  return value;
1231
1241
  }
package/lib/page.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { N as NamespaceMessagingConfig, G as GenericMessenger } from './generic-e806d3a5.js';
1
+ import { N as NamespaceMessagingConfig, G as GenericMessenger } from './generic-77a327a6.js';
2
2
 
3
3
  /**
4
4
  * Configuration passed into `defineWindowMessaging`.
@@ -12,8 +12,8 @@ interface WindowMessagingConfig extends NamespaceMessagingConfig {
12
12
  * > See <https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#targetorigin> for more
13
13
  * details.
14
14
  */
15
- declare type WindowSendMessageArgs = [targetOrigin?: string];
16
- declare type WindowMessenger<TProtocolMap extends Record<string, any>> = GenericMessenger<TProtocolMap, {}, WindowSendMessageArgs>;
15
+ type WindowSendMessageArgs = [targetOrigin?: string];
16
+ type WindowMessenger<TProtocolMap extends Record<string, any>> = GenericMessenger<TProtocolMap, {}, WindowSendMessageArgs>;
17
17
  /**
18
18
  * Returns a `WindowMessenger`. It is backed by the `window.postMessage` API. It can be used to
19
19
  * communicate between:
@@ -55,7 +55,7 @@ interface CustomEventMessage {
55
55
  /**
56
56
  * Messenger returned by `defineCustomEventMessenger`.
57
57
  */
58
- declare type CustomEventMessenger<TProtocolMap extends Record<string, any>> = GenericMessenger<TProtocolMap, CustomEventMessage, [
58
+ type CustomEventMessenger<TProtocolMap extends Record<string, any>> = GenericMessenger<TProtocolMap, CustomEventMessage, [
59
59
  ]>;
60
60
  /**
61
61
  * Creates a `CustomEventMessenger`. This messenger is backed by the `CustomEvent` APIs. It can be
@@ -78,15 +78,19 @@ var webExtCoreMessaging = (() => {
78
78
  defineWindowMessaging: () => defineWindowMessaging
79
79
  });
80
80
 
81
- // ../../node_modules/.pnpm/serialize-error@11.0.0/node_modules/serialize-error/error-constructors.js
81
+ // ../../node_modules/serialize-error/error-constructors.js
82
82
  var list = [
83
+ // Native ES errors https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
83
84
  EvalError,
84
85
  RangeError,
85
86
  ReferenceError,
86
87
  SyntaxError,
87
88
  TypeError,
88
89
  URIError,
90
+ // Built-in errors
89
91
  globalThis.DOMException,
92
+ // Node-specific errors
93
+ // https://nodejs.org/api/errors.html
90
94
  globalThis.AssertionError,
91
95
  globalThis.SystemError
92
96
  ].filter(Boolean).map(
@@ -95,7 +99,7 @@ var webExtCoreMessaging = (() => {
95
99
  var errorConstructors = new Map(list);
96
100
  var error_constructors_default = errorConstructors;
97
101
 
98
- // ../../node_modules/.pnpm/serialize-error@11.0.0/node_modules/serialize-error/index.js
102
+ // ../../node_modules/serialize-error/index.js
99
103
  var NonError = class extends Error {
100
104
  constructor(message) {
101
105
  super(NonError._prepareSuperMessage(message));
@@ -131,11 +135,11 @@ var webExtCoreMessaging = (() => {
131
135
  enumerable: false
132
136
  }
133
137
  ];
134
- var toJsonWasCalled = Symbol(".toJSON was called");
138
+ var toJsonWasCalled = /* @__PURE__ */ new WeakSet();
135
139
  var toJSON = (from) => {
136
- from[toJsonWasCalled] = true;
140
+ toJsonWasCalled.add(from);
137
141
  const json = from.toJSON();
138
- delete from[toJsonWasCalled];
142
+ toJsonWasCalled.delete(from);
139
143
  return json;
140
144
  };
141
145
  var getErrorConstructor = (name) => {
@@ -166,7 +170,7 @@ var webExtCoreMessaging = (() => {
166
170
  if (depth >= maxDepth) {
167
171
  return to;
168
172
  }
169
- if (useToJSON && typeof from.toJSON === "function" && from[toJsonWasCalled] !== true) {
173
+ if (useToJSON && typeof from.toJSON === "function" && !toJsonWasCalled.has(from)) {
170
174
  return toJSON(from);
171
175
  }
172
176
  const continueDestroyCircular = (value) => destroyCircular({
@@ -179,7 +183,7 @@ var webExtCoreMessaging = (() => {
179
183
  serialize
180
184
  });
181
185
  for (const [key, value] of Object.entries(from)) {
182
- if (typeof Buffer === "function" && Buffer.isBuffer(value)) {
186
+ if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
183
187
  to[key] = "[object Buffer]";
184
188
  continue;
185
189
  }
@@ -191,7 +195,10 @@ var webExtCoreMessaging = (() => {
191
195
  continue;
192
196
  }
193
197
  if (!value || typeof value !== "object") {
194
- to[key] = value;
198
+ try {
199
+ to[key] = value;
200
+ } catch (e) {
201
+ }
195
202
  continue;
196
203
  }
197
204
  if (!seen.includes(from[key])) {
@@ -214,7 +221,6 @@ var webExtCoreMessaging = (() => {
214
221
  return to;
215
222
  };
216
223
  function serializeError(value, options = {}) {
217
- var _a;
218
224
  const {
219
225
  maxDepth = Number.POSITIVE_INFINITY,
220
226
  useToJSON = true
@@ -231,7 +237,7 @@ var webExtCoreMessaging = (() => {
231
237
  });
232
238
  }
233
239
  if (typeof value === "function") {
234
- return `[Function: ${(_a = value.name) != null ? _a : "anonymous"}]`;
240
+ return `[Function: ${value.name || "anonymous"}]`;
235
241
  }
236
242
  return value;
237
243
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webext-core/messaging",
3
- "version": "1.4.0",
3
+ "version": "2.0.0",
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": [
@@ -45,24 +45,25 @@
45
45
  "types": "./lib/page.d.ts"
46
46
  }
47
47
  },
48
+ "scripts": {
49
+ "build": "buildc -- tsup src/index.ts src/page.ts --clean --out-dir lib --dts --format esm,cjs,iife --global-name webExtCoreMessaging",
50
+ "test": "buildc --deps-only -- bun run test:node && bun run test:browser",
51
+ "test:node": "buildc --deps-only -- vitest -r src --config ../vitest.config.node.ts",
52
+ "test:browser": "buildc --deps-only -- vitest -r src --config ../vitest.config.browser.ts",
53
+ "test:coverage": "buildc --deps-only -- bun run test:node --coverage && bun run test:browser --coverage",
54
+ "check": "buildc --deps-only -- tsc --noEmit"
55
+ },
48
56
  "dependencies": {
49
- "webextension-polyfill": "^0.10.0",
50
- "serialize-error": "^11.0.0"
57
+ "serialize-error": "^11.0.0",
58
+ "uid": "^2.0.2",
59
+ "webextension-polyfill": "^0.10.0"
51
60
  },
52
61
  "devDependencies": {
53
62
  "@types/webextension-polyfill": "^0.9.1",
54
- "@vitest/coverage-c8": "^0.24.5",
55
- "tsup": "^6.4.0",
56
- "typescript": "^4.8.4",
57
- "vitest": "^0.28.5",
58
- "@webext-core/fake-browser": "1.2.2",
59
- "tsconfig": "0.0.0"
63
+ "@webext-core/fake-browser": "workspace:*",
64
+ "tsconfig": "workspace:*"
60
65
  },
61
- "scripts": {
62
- "build": "tsup src/index.ts src/page.ts --clean --out-dir lib --dts --format esm,cjs,iife --global-name webExtCoreMessaging",
63
- "build:dependencies": "cd ../.. && turbo run build --filter=@webext-core/messaging^...",
64
- "test": "vitest -r src",
65
- "test:coverage": "vitest run -r src --coverage",
66
- "compile": "vitest typecheck"
66
+ "buildc": {
67
+ "outDir": "lib"
67
68
  }
68
- }
69
+ }
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.