webext-messenger 0.23.0 → 0.23.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ import { type PublicMethod, type PublicMethodWithTarget, type Options, type Targ
2
2
  import { type SetReturnType } from "type-fest";
3
3
  export declare const errorTargetClosedEarly = "The target was closed before receiving a response";
4
4
  export declare const errorTabDoesntExist = "The tab doesn't exist";
5
+ export declare const errorTabWasDiscarded = "The tab was discarded";
5
6
  declare function messenger<Type extends keyof MessengerMethods, Method extends MessengerMethods[Type]>(type: Type, options: {
6
7
  isNotification: true;
7
8
  }, target: Target | PageTarget, ...args: Parameters<Method>): void;
@@ -1,6 +1,5 @@
1
1
  import pRetry from "p-retry";
2
2
  import { isBackground } from "webext-detect-page";
3
- import { doesTabExist } from "webext-tools";
4
3
  import { deserializeError } from "serialize-error";
5
4
  import { isObject, MessengerError, __webextMessenger, log } from "./shared.js";
6
5
  import { handlers } from "./handlers.js";
@@ -9,6 +8,7 @@ const _errorNonExistingTarget = "Could not establish connection. Receiving end d
9
8
  const _errorTargetClosedEarly = "A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received";
10
9
  export const errorTargetClosedEarly = "The target was closed before receiving a response";
11
10
  export const errorTabDoesntExist = "The tab doesn't exist";
11
+ export const errorTabWasDiscarded = "The tab was discarded";
12
12
  function isMessengerResponse(response) {
13
13
  return isObject(response) && response["__webextMessenger"] === true;
14
14
  }
@@ -65,23 +65,29 @@ async function manageMessage(type, target, seq, sendMessage) {
65
65
  if (error.message === _errorTargetClosedEarly) {
66
66
  throw new Error(errorTargetClosedEarly);
67
67
  }
68
- if (
68
+ if (!(
69
+ // If NONE of these conditions is true, stop retrying
69
70
  // Don't retry sending to the background page unless it really hasn't loaded yet
70
- (target.page !== "background" && error instanceof MessengerError) ||
71
+ ((target.page !== "background" &&
72
+ error instanceof MessengerError) ||
71
73
  // Page or its content script not yet loaded
72
74
  error.message === _errorNonExistingTarget ||
73
75
  // `registerMethods` not yet loaded
74
- String(error.message).startsWith("No handlers registered in ")) {
75
- if (browser.tabs &&
76
- typeof target.tabId === "number" &&
77
- !(await doesTabExist(target.tabId))) {
76
+ String(error.message).startsWith("No handlers registered in ")))) {
77
+ throw error;
78
+ }
79
+ if (browser.tabs && typeof target.tabId === "number") {
80
+ try {
81
+ const tabInfo = await browser.tabs.get(target.tabId);
82
+ if (tabInfo.discarded) {
83
+ throw new Error(errorTabWasDiscarded);
84
+ }
85
+ }
86
+ catch {
78
87
  throw new Error(errorTabDoesntExist);
79
88
  }
80
- log.debug(type, seq, "will retry. Attempt", error.attemptNumber);
81
- }
82
- else {
83
- throw error;
84
89
  }
90
+ log.debug(type, seq, "will retry. Attempt", error.attemptNumber);
85
91
  },
86
92
  }).catch((error) => {
87
93
  if (error?.message === _errorNonExistingTarget) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.23.0",
3
+ "version": "0.23.1",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -22,8 +22,7 @@
22
22
  "p-retry": "^6.0.0",
23
23
  "serialize-error": "^11.0.2",
24
24
  "type-fest": "^4.3.1",
25
- "webext-detect-page": "^4.1.1",
26
- "webext-tools": "^1.1.3"
25
+ "webext-detect-page": "^4.1.1"
27
26
  },
28
27
  "devDependencies": {
29
28
  "@parcel/config-webextension": "^2.6.2",
@@ -32,7 +31,7 @@
32
31
  "@types/tape": "^5.6.1",
33
32
  "@types/webextension-polyfill": "^0.10.2",
34
33
  "buffer": "^6.0.3",
35
- "eslint": "^8.29.0",
34
+ "eslint": "^8.50.0",
36
35
  "eslint-config-pixiebrix": "^0.27.2",
37
36
  "events": "^3.3.0",
38
37
  "npm-run-all": "^4.1.5",
@@ -40,7 +39,7 @@
40
39
  "path-browserify": "^1.0.1",
41
40
  "process": "^0.11.10",
42
41
  "stream-browserify": "^3.0.0",
43
- "tape": "^5.6.6",
42
+ "tape": "^5.7.0",
44
43
  "typescript": "^5.2.2",
45
44
  "webext-content-scripts": "^2.5.5",
46
45
  "webextension-polyfill": "^0.10.0"