From f5ba8365dae9b4123330eed87fd24487a7157df8 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sat, 11 Nov 2023 12:05:07 +0800 Subject: [PATCH 1/6] Log error in case of multiple injection --- source/global.d.ts | 2 ++ source/index.ts | 2 ++ source/thisTarget.ts | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/source/global.d.ts b/source/global.d.ts index 1aa4667..bf458d3 100644 --- a/source/global.d.ts +++ b/source/global.d.ts @@ -2,4 +2,6 @@ import type { Browser } from "webextension-polyfill"; declare global { const browser: Browser; + // eslint-disable-next-line no-var -- https://stackoverflow.com/a/69208755/288906 + var __webextMessenger: string; } diff --git a/source/index.ts b/source/index.ts index d1f96d5..c1ba403 100644 --- a/source/index.ts +++ b/source/index.ts @@ -8,4 +8,6 @@ export { toggleLogging } from "./logging.js"; import { initPrivateApi } from "./thisTarget.js"; +// Required side effect to better track errors: +// https://github.com/pixiebrix/webext-messenger/pull/80 initPrivateApi(); diff --git a/source/thisTarget.ts b/source/thisTarget.ts index 6866449..7282d05 100644 --- a/source/thisTarget.ts +++ b/source/thisTarget.ts @@ -173,6 +173,19 @@ export async function getTopLevelFrame(): Promise { } export function initPrivateApi(): void { + // Improve DX by informing the developer that it's being loaded the wrong way + // https://github.com/pixiebrix/webext-messenger/issues/88 + if (globalThis.__webextMessenger) { + console.error( + "webext-messenger has already been imported in this context. This is a fatal error.\nhttps://github.com/pixiebrix/webext-messenger/issues/88", + { + existing: globalThis.__webextMessenger, + current: import.meta.url, + } + ); + } + + globalThis.__webextMessenger = import.meta.url; if (isExtensionContext()) { // Only `runtime` pages can handle this message but I can't remove it because its listener // also serves the purpose of throwing a specific error when no methods have been registered. From 4e34b799d274a178053965ea3335a32e8ce81078 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sat, 11 Nov 2023 12:06:37 +0800 Subject: [PATCH 2/6] Update engines --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 690dd9b..05a86bd 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,9 @@ "webext-content-scripts": "^2.5.5", "webextension-polyfill": "^0.10.0" }, + "engines": { + "node": ">=18" + }, "alias": { "./this-stuff-is-just-for-local-parcel-tests": "https://github.com/parcel-bundler/parcel/issues/4936", "./source/**/*.js": "./source/$1/$2.ts" From aafeba366b1bd9e46d67a0d522cb6ee156fbd536 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sat, 11 Nov 2023 14:26:42 +0800 Subject: [PATCH 3/6] Improve error display --- source/global.d.ts | 2 +- source/thisTarget.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/source/global.d.ts b/source/global.d.ts index bf458d3..b78bc98 100644 --- a/source/global.d.ts +++ b/source/global.d.ts @@ -2,6 +2,6 @@ import type { Browser } from "webextension-polyfill"; declare global { const browser: Browser; - // eslint-disable-next-line no-var -- https://stackoverflow.com/a/69208755/288906 + // eslint-disable-next-line no-var -- `let/const` behave differently https://stackoverflow.com/a/69208755/288906 var __webextMessenger: string; } diff --git a/source/thisTarget.ts b/source/thisTarget.ts index 7282d05..42193c0 100644 --- a/source/thisTarget.ts +++ b/source/thisTarget.ts @@ -176,16 +176,16 @@ export function initPrivateApi(): void { // Improve DX by informing the developer that it's being loaded the wrong way // https://github.com/pixiebrix/webext-messenger/issues/88 if (globalThis.__webextMessenger) { + // TODO: Use Error#cause after https://bugs.chromium.org/p/chromium/issues/detail?id=1211260 + console.log(globalThis.__webextMessenger.replace(/^Error: /, "")); console.error( - "webext-messenger has already been imported in this context. This is a fatal error.\nhttps://github.com/pixiebrix/webext-messenger/issues/88", - { - existing: globalThis.__webextMessenger, - current: import.meta.url, - } + "webext-messenger: Duplicate execution. This is a fatal error.\nhttps://github.com/pixiebrix/webext-messenger/issues/88" ); + return; } - globalThis.__webextMessenger = import.meta.url; + // Use Error to capture the stack and make it easier to find the cause + globalThis.__webextMessenger = new Error("First execution").stack!; if (isExtensionContext()) { // Only `runtime` pages can handle this message but I can't remove it because its listener // also serves the purpose of throwing a specific error when no methods have been registered. From 0b6689896aafd0b20a99053880692dfeaf720863 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sat, 11 Nov 2023 14:50:57 +0800 Subject: [PATCH 4/6] Discard changes to package.json --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index 05a86bd..690dd9b 100644 --- a/package.json +++ b/package.json @@ -50,9 +50,6 @@ "webext-content-scripts": "^2.5.5", "webextension-polyfill": "^0.10.0" }, - "engines": { - "node": ">=18" - }, "alias": { "./this-stuff-is-just-for-local-parcel-tests": "https://github.com/parcel-bundler/parcel/issues/4936", "./source/**/*.js": "./source/$1/$2.ts" From 33ccd8d17f04d212b614cb4907944c0b4f9c4e61 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sat, 11 Nov 2023 14:53:03 +0800 Subject: [PATCH 5/6] Match style in first log --- source/thisTarget.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/thisTarget.ts b/source/thisTarget.ts index 42193c0..ec92d73 100644 --- a/source/thisTarget.ts +++ b/source/thisTarget.ts @@ -177,7 +177,9 @@ export function initPrivateApi(): void { // https://github.com/pixiebrix/webext-messenger/issues/88 if (globalThis.__webextMessenger) { // TODO: Use Error#cause after https://bugs.chromium.org/p/chromium/issues/detail?id=1211260 - console.log(globalThis.__webextMessenger.replace(/^Error: /, "")); + console.log( + globalThis.__webextMessenger.replace(/^Error/, "webext-messenger") + ); console.error( "webext-messenger: Duplicate execution. This is a fatal error.\nhttps://github.com/pixiebrix/webext-messenger/issues/88" ); From 8286d89c4647de086604e0cb8d2d58eddeac75fa Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Tue, 14 Nov 2023 18:49:55 +0800 Subject: [PATCH 6/6] 0.25.0-0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 14c9acb..078701f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "webext-messenger", - "version": "0.24.0", + "version": "0.25.0-0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "webext-messenger", - "version": "0.24.0", + "version": "0.25.0-0", "license": "MIT", "dependencies": { "p-retry": "^6.0.0", diff --git a/package.json b/package.json index 690dd9b..aff6473 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webext-messenger", - "version": "0.24.0", + "version": "0.25.0-0", "description": "Browser Extension component messaging framework", "keywords": [], "repository": "pixiebrix/webext-messenger",