webext-messenger 0.18.1 → 0.19.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.1",
3
+ "version": "0.19.1",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -78,7 +78,8 @@
78
78
  "overrides": [
79
79
  {
80
80
  "files": [
81
- "*.test.ts"
81
+ "*.test.ts",
82
+ "testingApi.ts"
82
83
  ],
83
84
  "rules": {
84
85
  "@typescript-eslint/no-explicit-any": "off",
@@ -97,35 +98,36 @@
97
98
  ]
98
99
  },
99
100
  "dependencies": {
100
- "p-retry": "^5.1.0",
101
- "serialize-error": "^10.0.0",
102
- "type-fest": "^2.12.1",
103
- "webext-detect-page": "^4.0.1"
101
+ "p-retry": "^5.1.1",
102
+ "serialize-error": "^11.0.0",
103
+ "type-fest": "^2.13.0",
104
+ "webext-detect-page": "^4.0.1",
105
+ "webext-tools": "^1.1.0"
104
106
  },
105
107
  "devDependencies": {
106
- "@parcel/config-webextension": "^2.4.0",
107
- "@sindresorhus/tsconfig": "^2.0.0",
108
- "@types/chrome": "^0.0.180",
108
+ "@parcel/config-webextension": "^2.6.0",
109
+ "@sindresorhus/tsconfig": "^3.0.1",
110
+ "@types/chrome": "^0.0.188",
109
111
  "@types/tape": "^4.13.2",
110
- "@types/webextension-polyfill": "^0.8.3",
111
- "@typescript-eslint/eslint-plugin": "^5.17.0",
112
- "@typescript-eslint/parser": "^5.17.0",
112
+ "@types/webextension-polyfill": "^0.9.0",
113
+ "@typescript-eslint/eslint-plugin": "^5.27.0",
114
+ "@typescript-eslint/parser": "^5.27.0",
113
115
  "buffer": "^6.0.3",
114
- "eslint": "^8.12.0",
116
+ "eslint": "^8.17.0",
115
117
  "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",
118
+ "eslint-config-xo": "^0.41.0",
119
+ "eslint-config-xo-typescript": "^0.51.1",
120
+ "eslint-plugin-import": "^2.26.0",
121
+ "eslint-plugin-unicorn": "^42.0.0",
120
122
  "events": "^3.3.0",
121
123
  "npm-run-all": "^4.1.5",
122
- "parcel": "^2.4.0",
124
+ "parcel": "^2.6.0",
123
125
  "path-browserify": "^1.0.1",
124
126
  "process": "^0.11.10",
125
127
  "stream-browserify": "^3.0.0",
126
- "tape": "^5.5.2",
127
- "typescript": "^4.6.3",
128
- "webext-content-scripts": "^1.0.1",
128
+ "tape": "^5.5.3",
129
+ "typescript": "^4.7.3",
130
+ "webext-content-scripts": "^1.0.2",
129
131
  "webextension-polyfill": "^0.9.0"
130
132
  },
131
133
  "alias": {
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)