webext-messenger 0.33.0-2 → 0.33.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.
@@ -95,7 +95,7 @@ async function prepareResponse(message, action, meta) {
95
95
  const localHandler = handlers.get(type);
96
96
  if (localHandler) {
97
97
  if ("extensionId" in target && !externalMethods.has(type)) {
98
- throw new MessengerError(`The ${type} is registered in ${getContextName()} for internal use only`);
98
+ throw new MessengerError(`The ${type} handler is registered in ${getContextName()} for internal use only`);
99
99
  }
100
100
  return localHandler.apply(meta, args);
101
101
  }
@@ -1,7 +1,7 @@
1
1
  import pRetry from "p-retry";
2
- import { isBackground } from "webext-detect";
2
+ import { isBackground, isExtensionContext } from "webext-detect";
3
3
  import { deserializeError } from "serialize-error";
4
- import { isObject, MessengerError, __webextMessenger } from "./shared.js";
4
+ import { isObject, MessengerError, ExtensionNotFoundError, __webextMessenger, } from "./shared.js";
5
5
  import { log } from "./logging.js";
6
6
  import { handlers } from "./handlers.js";
7
7
  import { events } from "./events.js";
@@ -10,6 +10,7 @@ const _errorTargetClosedEarly = "A listener indicated an asynchronous response b
10
10
  export const errorTargetClosedEarly = "The target was closed before receiving a response";
11
11
  export const errorTabDoesntExist = "The tab doesn't exist";
12
12
  export const errorTabWasDiscarded = "The tab was discarded";
13
+ const errorExtensionNotFound = "Extension $ID is not installed or externally connectable";
13
14
  function isMessengerResponse(response) {
14
15
  return isObject(response) && response["__webextMessenger"] === true;
15
16
  }
@@ -78,7 +79,12 @@ async function manageMessage(type, target, seq, retry, sendMessage) {
78
79
  attemptCount: error.attemptNumber,
79
80
  },
80
81
  }));
81
- if (wasContextInvalidated()) {
82
+ if ("extensionId" in target &&
83
+ error.message === _errorNonExistingTarget) {
84
+ // The extension is not available and it will not be. Do not retry.
85
+ throw new ExtensionNotFoundError(errorExtensionNotFound.replace("$ID", target.extensionId));
86
+ }
87
+ if (isExtensionContext() && wasContextInvalidated()) {
82
88
  // The error matches the native context invalidated error
83
89
  // *.sendMessage() might fail with a message-specific error that is less useful,
84
90
  // like "Sender closed without responding"
@@ -142,6 +148,9 @@ function messenger(type, options, target, ...args) {
142
148
  options.seq = globalSeq++;
143
149
  const { seq } = options;
144
150
  if ("extensionId" in target) {
151
+ if (!globalThis.chrome?.runtime?.sendMessage) {
152
+ throw new ExtensionNotFoundError(errorExtensionNotFound.replace("$ID", target.extensionId));
153
+ }
145
154
  const sendMessage = async (attemptCount) => {
146
155
  log.debug(type, seq, "↗️ sending message to extension", attemptLog(attemptCount));
147
156
  return chrome.runtime.sendMessage(target.extensionId, makeMessage(type, args, target, options));
@@ -10,6 +10,9 @@ export declare function isObject(value: unknown): value is Record<string, unknow
10
10
  export declare class MessengerError extends Error {
11
11
  name: string;
12
12
  }
13
+ export declare class ExtensionNotFoundError extends MessengerError {
14
+ name: string;
15
+ }
13
16
  export declare function isErrorObject(error: unknown): error is ErrorObject;
14
17
  export declare function delay(milliseconds: number): Promise<void>;
15
18
  export declare function once<Callback extends (...arguments_: unknown[]) => unknown>(function_: Callback): Callback;
@@ -6,6 +6,9 @@ export function isObject(value) {
6
6
  export class MessengerError extends Error {
7
7
  name = "MessengerError";
8
8
  }
9
+ export class ExtensionNotFoundError extends MessengerError {
10
+ name = "ExtensionNotFoundError";
11
+ }
9
12
  addKnownErrorConstructor(MessengerError);
10
13
  export function isErrorObject(error) {
11
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is a type guard function and it uses ?.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.33.0-2",
3
+ "version": "0.33.0",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -29,7 +29,7 @@
29
29
  "p-event": "^6.0.1",
30
30
  "p-retry": "^6.2.1",
31
31
  "serialize-error": "^12.0.0",
32
- "type-fest": "^4.36.0",
32
+ "type-fest": "^4.38.0",
33
33
  "webext-detect": "^5.3.2"
34
34
  },
35
35
  "@parcel/resolver-default": {
@@ -38,7 +38,7 @@
38
38
  "devDependencies": {
39
39
  "@parcel/config-webextension": "^2.11.0",
40
40
  "@sindresorhus/tsconfig": "^7.0.0",
41
- "@types/chrome": "^0.0.307",
41
+ "@types/chrome": "^0.0.313",
42
42
  "@types/tape": "^5.8.1",
43
43
  "buffer": "^6.0.3",
44
44
  "eslint": "^8.57.0",
@@ -51,7 +51,7 @@
51
51
  "stream-browserify": "^3.0.0",
52
52
  "tape": "^5.9.0",
53
53
  "typescript": "^5.8.2",
54
- "vitest": "^3.0.7"
54
+ "vitest": "^3.1.1"
55
55
  },
56
56
  "targets": {
57
57
  "main": false,