webext-messenger 0.15.0-4 → 0.16.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.
@@ -2,7 +2,7 @@ import browser from "webextension-polyfill";
2
2
  import { serializeError } from "serialize-error";
3
3
  import { messenger } from "./sender.js";
4
4
  import { handlers, isObject, MessengerError, debug, __webextMessenger, } from "./shared.js";
5
- import { getContextName } from "webext-detect-page";
5
+ import { getContextName, isBackground } from "webext-detect-page";
6
6
  import { getActionForMessage, nameThisTarget } from "./thisTarget.js";
7
7
  export function isMessengerMessage(message) {
8
8
  return (isObject(message) &&
@@ -16,7 +16,7 @@ function onMessageListener(message, sender) {
16
16
  // TODO: Add test for this eventuality: ignore unrelated messages
17
17
  return;
18
18
  }
19
- // Target check must be synchronous (`await` means we're handing the message)
19
+ // Target check must be synchronous (`await` means we're handling the message)
20
20
  const action = getActionForMessage(sender, message.target);
21
21
  if (action === "ignore") {
22
22
  return;
@@ -25,36 +25,39 @@ function onMessageListener(message, sender) {
25
25
  }
26
26
  // This function can only be called when the message *will* be handled locally.
27
27
  // Returning "undefined" or throwing an error will still handle it.
28
- async function handleMessage(message, sender, action) {
29
- const { type, target, args, options: { trace } = {} } = message;
30
- debug(type, "↘️ received", { sender, args });
28
+ async function handleMessage(message, sender,
29
+ // Once messages reach this function they cannot be "ignored", they're already being handled
30
+ action) {
31
+ const { type, target, args, options = {} } = message;
32
+ const { trace = [] } = options;
33
+ trace.push(sender);
34
+ const meta = { trace };
35
+ debug(type, "↘️ received", { sender, args, wasForwarded: trace.length > 1 });
31
36
  let handleMessage;
32
37
  if (action === "forward") {
33
38
  debug(type, "🔀 forwarded", { sender, target });
34
- handleMessage = async () => messenger(type, { trace }, target, ...args);
39
+ handleMessage = async () => messenger(type, meta, target, ...args);
35
40
  }
36
41
  else {
37
42
  const localHandler = handlers.get(type);
38
43
  if (!localHandler) {
39
44
  throw new MessengerError(`No handler registered for ${type} in ${getContextName()}`);
40
45
  }
41
- debug(type, "➡️ will be handled here");
42
- const meta = { trace: [sender] };
46
+ debug(type, "➡️ will be handled here,", getContextName());
43
47
  handleMessage = async () => localHandler.apply(meta, args);
44
48
  }
45
- return handleMessage()
46
- .then((value) => ({ value }), (error) => ({
47
- // Errors must be serialized because the stacktraces are currently lost on Chrome
49
+ const response = await handleMessage().then((value) => ({ value }), (error) => ({
50
+ // Errors must be serialized because the stack traces are currently lost on Chrome
48
51
  // and https://github.com/mozilla/webextension-polyfill/issues/210
49
52
  error: serializeError(error),
50
- }))
51
- .then((response) => {
52
- debug(type, "↗️ responding", response);
53
- return { ...response, __webextMessenger };
54
- });
53
+ }));
54
+ debug(type, "↗️ responding", response);
55
+ return { ...response, __webextMessenger };
55
56
  }
56
57
  export function registerMethods(methods) {
57
- void nameThisTarget();
58
+ if (!isBackground()) {
59
+ void nameThisTarget();
60
+ }
58
61
  for (const [type, method] of Object.entries(methods)) {
59
62
  if (handlers.has(type)) {
60
63
  throw new MessengerError(`Handler already set for ${type}`);
@@ -1,6 +1,6 @@
1
1
  import browser from "webextension-polyfill";
2
2
  import pRetry from "p-retry";
3
- import { isBackgroundPage } from "webext-detect-page";
3
+ import { isBackground } from "webext-detect-page";
4
4
  import { deserializeError } from "serialize-error";
5
5
  import { isObject, MessengerError, __webextMessenger, handlers, debug, warn, } from "./shared.js";
6
6
  export const errorNonExistingTarget = "Could not establish connection. Receiving end does not exist.";
@@ -17,15 +17,15 @@ function makeMessage(type, args, target, options) {
17
17
  };
18
18
  }
19
19
  // Do not turn this into an `async` function; Notifications must turn `void`
20
- function manageConnection(type, options, sendMessage) {
20
+ function manageConnection(type, options, target, sendMessage) {
21
21
  if (!options.isNotification) {
22
- return manageMessage(type, sendMessage);
22
+ return manageMessage(type, target, sendMessage);
23
23
  }
24
24
  void sendMessage().catch((error) => {
25
25
  debug(type, "notification failed", { error });
26
26
  });
27
27
  }
28
- async function manageMessage(type, sendMessage) {
28
+ async function manageMessage(type, target, sendMessage) {
29
29
  const response = await pRetry(async () => {
30
30
  const response = await sendMessage();
31
31
  if (!isMessengerResponse(response)) {
@@ -37,7 +37,9 @@ async function manageMessage(type, sendMessage) {
37
37
  factor: 1.3,
38
38
  maxRetryTime: 4000,
39
39
  onFailedAttempt(error) {
40
- if (error instanceof MessengerError ||
40
+ if (
41
+ // Don't retry sending to the background page unless it really hasn't loaded yet
42
+ (target.page !== "background" && error instanceof MessengerError) ||
41
43
  String(error.message).startsWith(errorNonExistingTarget)) {
42
44
  debug(type, "will retry. Attempt", error.attemptNumber);
43
45
  }
@@ -56,7 +58,7 @@ async function manageMessage(type, sendMessage) {
56
58
  function messenger(type, options, target, ...args) {
57
59
  // Message goes to extension page
58
60
  if ("page" in target) {
59
- if (target.page === "background" && isBackgroundPage()) {
61
+ if (target.page === "background" && isBackground()) {
60
62
  const handler = handlers.get(type);
61
63
  if (handler) {
62
64
  warn(type, "is being handled locally");
@@ -68,11 +70,11 @@ function messenger(type, options, target, ...args) {
68
70
  debug(type, "↗️ sending message to runtime");
69
71
  return browser.runtime.sendMessage(makeMessage(type, args, target, options));
70
72
  };
71
- return manageConnection(type, options, sendMessage);
73
+ return manageConnection(type, options, target, sendMessage);
72
74
  }
73
75
  // Contexts without direct Tab access must go through background
74
76
  if (!browser.tabs) {
75
- return manageConnection(type, options, async () => {
77
+ return manageConnection(type, options, target, async () => {
76
78
  debug(type, "↗️ sending message to runtime");
77
79
  return browser.runtime.sendMessage(makeMessage(type, args, target, options));
78
80
  });
@@ -80,7 +82,7 @@ function messenger(type, options, target, ...args) {
80
82
  // `frameId` must be specified. If missing, the message is sent to every frame
81
83
  const { tabId, frameId = 0 } = target;
82
84
  // Message tab directly
83
- return manageConnection(type, options, async () => {
85
+ return manageConnection(type, options, target, async () => {
84
86
  debug(type, "↗️ sending message to tab", tabId, "frame", frameId);
85
87
  return browser.tabs.sendMessage(tabId, makeMessage(type, args, target, options), {
86
88
  frameId,
@@ -1,9 +1,4 @@
1
- import { MessengerMeta, Sender } from "./types.js";
2
- interface AnyTarget {
3
- tabId?: number | "this";
4
- frameId?: number;
5
- page?: string;
6
- }
1
+ import { AnyTarget, MessengerMeta, Sender } from "./types.js";
7
2
  export declare function getActionForMessage(from: Sender, { ...to }: AnyTarget): "respond" | "forward" | "ignore";
8
3
  export declare function nameThisTarget(): Promise<void>;
9
4
  declare function __getTabData(this: MessengerMeta): AnyTarget;
@@ -1,4 +1,4 @@
1
- import { isBackgroundPage, isContentScript, isExtensionContext, } from "webext-detect-page";
1
+ import { isBackground, isContentScript, isExtensionContext, } from "webext-detect-page";
2
2
  import { messenger } from "./sender.js";
3
3
  import { registerMethods } from "./receiver.js";
4
4
  import { debug } from "./shared.js";
@@ -26,7 +26,7 @@ export function getActionForMessage(from, { ...to } // Clone object because we'r
26
26
  // If this *was* the target, then probably no one else answered
27
27
  return "ignore";
28
28
  }
29
- // If requests "this" tab, then set it to allow the next condition
29
+ // Set "this" tab to the current tabId
30
30
  if (to.tabId === "this" && thisTarget.tabId === ((_a = from.tab) === null || _a === void 0 ? void 0 : _a.id)) {
31
31
  to.tabId = thisTarget.tabId;
32
32
  }
@@ -53,11 +53,11 @@ function __getTabData() {
53
53
  return { tabId: (_b = (_a = this.trace[0]) === null || _a === void 0 ? void 0 : _a.tab) === null || _b === void 0 ? void 0 : _b.id, frameId: (_c = this.trace[0]) === null || _c === void 0 ? void 0 : _c.frameId };
54
54
  }
55
55
  export function initPrivateApi() {
56
+ if (isBackground()) {
57
+ thisTarget = { page: "background" };
58
+ }
56
59
  if (isExtensionContext()) {
57
60
  // Any context can handler this message
58
61
  registerMethods({ __getTabData });
59
62
  }
60
- if (isBackgroundPage()) {
61
- thisTarget = { page: "background" };
62
- }
63
63
  }
@@ -45,6 +45,11 @@ export declare type MessengerMessage = Message & {
45
45
  /** Guarantees that a message is meant to be handled by this library */
46
46
  __webextMessenger: true;
47
47
  };
48
+ export interface AnyTarget {
49
+ tabId?: number | "this";
50
+ frameId?: number;
51
+ page?: string;
52
+ }
48
53
  export interface Target {
49
54
  tabId: number;
50
55
  frameId?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.15.0-4",
3
+ "version": "0.16.0",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -107,19 +107,19 @@
107
107
  "dependencies": {
108
108
  "p-retry": "^5.0.0",
109
109
  "serialize-error": "^9.0.0",
110
- "type-fest": "^2.6.0",
111
- "webext-detect-page": "^3.1.0",
110
+ "type-fest": "^2.8.0",
111
+ "webext-detect-page": "^4.0.0",
112
112
  "webextension-polyfill": "^0.8.0"
113
113
  },
114
114
  "devDependencies": {
115
115
  "@parcel/config-webextension": "^2.0.1",
116
116
  "@sindresorhus/tsconfig": "^2.0.0",
117
- "@types/chrome": "^0.0.164",
117
+ "@types/chrome": "^0.0.171",
118
118
  "@types/tape": "^4.13.2",
119
119
  "@types/webextension-polyfill": "^0.8.2",
120
- "@typescript-eslint/eslint-plugin": "^5.4.0",
121
- "@typescript-eslint/parser": "^5.4.0",
122
- "eslint": "^8.3.0",
120
+ "@typescript-eslint/eslint-plugin": "^5.7.0",
121
+ "@typescript-eslint/parser": "^5.7.0",
122
+ "eslint": "^8.4.1",
123
123
  "eslint-config-prettier": "^8.3.0",
124
124
  "eslint-config-xo": "^0.39.0",
125
125
  "eslint-config-xo-typescript": "^0.47.1",
@@ -128,8 +128,8 @@
128
128
  "npm-run-all": "^4.1.5",
129
129
  "parcel": "^2.0.1",
130
130
  "tape": "^5.3.2",
131
- "typescript": "^4.5.2",
132
- "webext-content-scripts": "^0.10.1"
131
+ "typescript": "^4.5.4",
132
+ "webext-content-scripts": "^0.12.0"
133
133
  },
134
134
  "targets": {
135
135
  "main": false,