webext-messenger 0.17.0-0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,3 @@
1
- import browser from "webextension-polyfill";
2
1
  import { serializeError } from "serialize-error";
3
2
  import { messenger } from "./sender.js";
4
3
  import { handlers, isObject, MessengerError, debug, __webextMessenger, } from "./shared.js";
@@ -32,18 +31,21 @@ action) {
32
31
  const { trace = [] } = options;
33
32
  trace.push(sender);
34
33
  const meta = { trace };
35
- debug(type, "↘️ received", { sender, args, wasForwarded: trace.length > 1 });
36
34
  let handleMessage;
37
35
  if (action === "forward") {
38
36
  debug(type, "🔀 forwarded", { sender, target });
39
37
  handleMessage = async () => messenger(type, meta, target, ...args);
40
38
  }
41
39
  else {
40
+ debug(type, "↘️ received in", getContextName(), {
41
+ sender,
42
+ args,
43
+ wasForwarded: trace.length > 1,
44
+ });
42
45
  const localHandler = handlers.get(type);
43
46
  if (!localHandler) {
44
47
  throw new MessengerError(`No handler registered for ${type} in ${getContextName()}`);
45
48
  }
46
- debug(type, "➡️ will be handled here,", getContextName());
47
49
  handleMessage = async () => localHandler.apply(meta, args);
48
50
  }
49
51
  const response = await handleMessage().then((value) => ({ value }), (error) => ({
@@ -62,7 +64,7 @@ export function registerMethods(methods) {
62
64
  if (handlers.has(type)) {
63
65
  throw new MessengerError(`Handler already set for ${type}`);
64
66
  }
65
- console.debug("Messenger: Registered", type);
67
+ debug("Registered", type);
66
68
  handlers.set(type, method);
67
69
  }
68
70
  browser.runtime.onMessage.addListener(onMessageListener);
@@ -1,4 +1,3 @@
1
- import browser from "webextension-polyfill";
2
1
  import pRetry from "p-retry";
3
2
  import { isBackground } from "webext-detect-page";
4
3
  import { deserializeError } from "serialize-error";
@@ -1,3 +1,15 @@
1
+ const logging = (() => {
2
+ try {
3
+ // @ts-expect-error it would break Webpack
4
+ return process.env.WEBEXT_MESSENGER_LOGGING === "true";
5
+ }
6
+ catch {
7
+ return false;
8
+ }
9
+ })();
10
+ function noop() {
11
+ /* */
12
+ }
1
13
  export const __webextMessenger = true;
2
14
  export function isObject(value) {
3
15
  return typeof value === "object" && value !== null;
@@ -15,12 +27,11 @@ export class MessengerError extends Error {
15
27
  }
16
28
  export const handlers = new Map();
17
29
  // .bind preserves the call location in the console
18
- export const debug = console.debug.bind(console, "Messenger:");
19
- export const warn = console.warn.bind(console, "Messenger:");
30
+ export const debug = logging ? console.debug.bind(console, "Messenger:") : noop;
31
+ export const warn = logging ? console.warn.bind(console, "Messenger:") : noop;
20
32
  export function isErrorObject(error) {
21
- var _a;
22
33
  // eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is a type guard function and it uses ?.
23
- return typeof ((_a = error) === null || _a === void 0 ? void 0 : _a.message) === "string";
34
+ return typeof (error === null || error === void 0 ? void 0 : error.message) === "string";
24
35
  }
25
36
  export async function delay(milliseconds) {
26
37
  return new Promise((resolve) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.17.0-0",
3
+ "version": "0.18.0",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -8,17 +8,9 @@
8
8
  "author": "Federico Brigante for PixieBrix <federico@pixiebrix.com> (https://www.pixiebrix.com)",
9
9
  "type": "module",
10
10
  "main": "distribution/index.js",
11
- "alias": {
12
- "./this-stuff-is-just-for-local-parcel-tests": "./package.json",
13
- "./source/sender.js": "./source/sender.ts",
14
- "./source/receiver.js": "./source/receiver.ts",
15
- "./source/types.js": "./source/types.ts",
16
- "./source/shared.js": "./source/shared.ts",
17
- "./source/thisTarget.js": "./source/thisTarget.ts"
18
- },
19
11
  "scripts": {
20
12
  "build": "tsc",
21
- "demo:watch": "parcel watch --no-cache --no-hmr",
13
+ "demo:watch": "WEBEXT_MESSENGER_LOGGING=true parcel watch --no-cache --no-hmr",
22
14
  "demo:build": "parcel build --no-cache",
23
15
  "prepack": "tsc --sourceMap false",
24
16
  "test": "eslint . && tsc --noEmit",
@@ -89,8 +81,8 @@
89
81
  "*.test.ts"
90
82
  ],
91
83
  "rules": {
92
- "@typescript-eslint/no-non-null-assertion": "off",
93
84
  "@typescript-eslint/no-explicit-any": "off",
85
+ "@typescript-eslint/no-non-null-assertion": "off",
94
86
  "@typescript-eslint/no-unsafe-member-access": "off"
95
87
  }
96
88
  },
@@ -105,31 +97,44 @@
105
97
  ]
106
98
  },
107
99
  "dependencies": {
108
- "p-retry": "^5.0.0",
109
- "serialize-error": "^9.0.0",
110
- "type-fest": "^2.8.0",
111
- "webext-detect-page": "^4.0.0",
112
- "webextension-polyfill": "^0.8.0"
100
+ "p-retry": "^5.1.0",
101
+ "serialize-error": "^9.1.1",
102
+ "type-fest": "^2.12.1",
103
+ "webext-detect-page": "^4.0.1"
113
104
  },
114
105
  "devDependencies": {
115
- "@parcel/config-webextension": "^2.0.1",
106
+ "@parcel/config-webextension": "^2.4.0",
116
107
  "@sindresorhus/tsconfig": "^2.0.0",
117
- "@types/chrome": "^0.0.171",
108
+ "@types/chrome": "^0.0.180",
118
109
  "@types/tape": "^4.13.2",
119
- "@types/webextension-polyfill": "^0.8.2",
120
- "@typescript-eslint/eslint-plugin": "^5.7.0",
121
- "@typescript-eslint/parser": "^5.7.0",
122
- "eslint": "^8.4.1",
123
- "eslint-config-prettier": "^8.3.0",
124
- "eslint-config-xo": "^0.39.0",
125
- "eslint-config-xo-typescript": "^0.47.1",
126
- "eslint-plugin-import": "^2.25.3",
127
- "eslint-plugin-unicorn": "^39.0.0",
110
+ "@types/webextension-polyfill": "^0.8.3",
111
+ "@typescript-eslint/eslint-plugin": "^5.17.0",
112
+ "@typescript-eslint/parser": "^5.17.0",
113
+ "buffer": "^6.0.3",
114
+ "eslint": "^8.12.0",
115
+ "eslint-config-prettier": "^8.5.0",
116
+ "eslint-config-xo": "^0.40.0",
117
+ "eslint-config-xo-typescript": "^0.50.0",
118
+ "eslint-plugin-import": "^2.25.4",
119
+ "eslint-plugin-unicorn": "^41.0.1",
120
+ "events": "^3.3.0",
128
121
  "npm-run-all": "^4.1.5",
129
- "parcel": "^2.0.1",
130
- "tape": "^5.3.2",
131
- "typescript": "^4.5.4",
132
- "webext-content-scripts": "^0.12.0"
122
+ "parcel": "^2.4.0",
123
+ "path-browserify": "^1.0.1",
124
+ "process": "^0.11.10",
125
+ "stream-browserify": "^3.0.0",
126
+ "tape": "^5.5.2",
127
+ "typescript": "^4.6.3",
128
+ "webext-content-scripts": "^1.0.1",
129
+ "webextension-polyfill": "^0.9.0"
130
+ },
131
+ "alias": {
132
+ "./this-stuff-is-just-for-local-parcel-tests": "./package.json",
133
+ "./source/sender.js": "./source/sender.ts",
134
+ "./source/receiver.js": "./source/receiver.ts",
135
+ "./source/types.js": "./source/types.ts",
136
+ "./source/shared.js": "./source/shared.ts",
137
+ "./source/thisTarget.js": "./source/thisTarget.ts"
133
138
  },
134
139
  "targets": {
135
140
  "main": false,