webext-messenger 0.18.0 → 0.19.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.
@@ -1,6 +1,7 @@
1
1
  import { PublicMethod, PublicMethodWithTarget, Options, Target, PageTarget } from "./types.js";
2
2
  import { SetReturnType } from "type-fest";
3
- export declare const errorNonExistingTarget = "Could not establish connection. Receiving end does not exist.";
3
+ export declare const errorTargetClosedEarly = "The target was closed before receiving a response";
4
+ export declare const errorTabDoesntExist = "The tab doesn't exist";
4
5
  declare function messenger<Type extends keyof MessengerMethods, Method extends MessengerMethods[Type]>(type: Type, options: {
5
6
  isNotification: true;
6
7
  }, target: Target | PageTarget, ...args: Parameters<Method>): void;
@@ -1,8 +1,13 @@
1
1
  import pRetry from "p-retry";
2
2
  import { isBackground } from "webext-detect-page";
3
+ import { doesTabExist } from "webext-tools";
3
4
  import { deserializeError } from "serialize-error";
4
5
  import { isObject, MessengerError, __webextMessenger, handlers, debug, warn, } from "./shared.js";
5
- export const errorNonExistingTarget = "Could not establish connection. Receiving end does not exist.";
6
+ const _errorNonExistingTarget = "Could not establish connection. Receiving end does not exist.";
7
+ // https://github.com/mozilla/webextension-polyfill/issues/384
8
+ const _errorTargetClosedEarly = "A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received";
9
+ export const errorTargetClosedEarly = "The target was closed before receiving a response";
10
+ export const errorTabDoesntExist = "The tab doesn't exist";
6
11
  function isMessengerResponse(response) {
7
12
  return isObject(response) && response["__webextMessenger"] === true;
8
13
  }
@@ -35,11 +40,19 @@ async function manageMessage(type, target, sendMessage) {
35
40
  minTimeout: 100,
36
41
  factor: 1.3,
37
42
  maxRetryTime: 4000,
38
- onFailedAttempt(error) {
43
+ async onFailedAttempt(error) {
44
+ if (error.message === _errorTargetClosedEarly) {
45
+ throw new Error(errorTargetClosedEarly);
46
+ }
39
47
  if (
40
48
  // Don't retry sending to the background page unless it really hasn't loaded yet
41
49
  (target.page !== "background" && error instanceof MessengerError) ||
42
- String(error.message).startsWith(errorNonExistingTarget)) {
50
+ String(error.message).startsWith(_errorNonExistingTarget)) {
51
+ if (browser.tabs &&
52
+ typeof target.tabId === "number" &&
53
+ !(await doesTabExist(target.tabId))) {
54
+ throw new Error(errorTabDoesntExist);
55
+ }
43
56
  debug(type, "will retry. Attempt", error.attemptNumber);
44
57
  }
45
58
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -98,9 +98,10 @@
98
98
  },
99
99
  "dependencies": {
100
100
  "p-retry": "^5.1.0",
101
- "serialize-error": "^9.1.1",
101
+ "serialize-error": "^11.0.0",
102
102
  "type-fest": "^2.12.1",
103
- "webext-detect-page": "^4.0.1"
103
+ "webext-detect-page": "^4.0.1",
104
+ "webext-tools": "^1.1.0"
104
105
  },
105
106
  "devDependencies": {
106
107
  "@parcel/config-webextension": "^2.4.0",
package/readme.md CHANGED
@@ -14,3 +14,7 @@ npm install webext-messenger
14
14
  ```js
15
15
  import messenger from "webext-messenger";
16
16
  ```
17
+
18
+ ## Context
19
+
20
+ - [Initial considerations for this library](https://github.com/pixiebrix/webext-messenger/issues/1)