From 34e621fcc888e71f90543839b938989a0973344f Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Thu, 26 Oct 2023 13:28:11 +0800 Subject: [PATCH] Move logging toggle to a safer location --- source/index.ts | 2 +- source/logging.ts | 17 +++++++++++++++++ source/receiver.ts | 3 ++- source/sender.ts | 3 ++- source/shared.ts | 18 ------------------ source/thisTarget.ts | 5 +++-- 6 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 source/logging.ts diff --git a/source/index.ts b/source/index.ts index 583fe7f..d1f96d5 100644 --- a/source/index.ts +++ b/source/index.ts @@ -4,7 +4,7 @@ export * from "./receiver.js"; export * from "./sender.js"; export * from "./types.js"; export { getThisFrame, getTopLevelFrame } from "./thisTarget.js"; -export { toggleLogging } from "./shared.js"; +export { toggleLogging } from "./logging.js"; import { initPrivateApi } from "./thisTarget.js"; diff --git a/source/logging.ts b/source/logging.ts new file mode 100644 index 0000000..901c69d --- /dev/null +++ b/source/logging.ts @@ -0,0 +1,17 @@ +/* Warning: Do not use import browser-polyfill directly or indirectly */ + +// .bind preserves the call location in the console +const debug = console.debug.bind(console, "Messenger:"); +const warn = console.warn.bind(console, "Messenger:"); + +const noop: (...args: unknown[]) => void = () => { + /* */ +}; + +// Default to "no logs" +export const log = { debug: noop, warn: noop }; + +export function toggleLogging(enabled: boolean): void { + log.debug = enabled ? debug : noop; + log.warn = enabled ? warn : noop; +} diff --git a/source/receiver.ts b/source/receiver.ts index e87e773..453acb9 100644 --- a/source/receiver.ts +++ b/source/receiver.ts @@ -8,7 +8,8 @@ import { type Method, type Sender, } from "./types.js"; -import { isObject, MessengerError, log, __webextMessenger } from "./shared.js"; +import { isObject, MessengerError, __webextMessenger } from "./shared.js"; +import { log } from "./logging.js"; import { getActionForMessage } from "./thisTarget.js"; import { didUserRegisterMethods, handlers } from "./handlers.js"; diff --git a/source/sender.ts b/source/sender.ts index 26b387c..cca5bb9 100644 --- a/source/sender.ts +++ b/source/sender.ts @@ -12,7 +12,8 @@ import { type PageTarget, type AnyTarget, } from "./types.js"; -import { isObject, MessengerError, __webextMessenger, log } from "./shared.js"; +import { isObject, MessengerError, __webextMessenger } from "./shared.js"; +import { log } from "./logging.js"; import { type SetReturnType } from "type-fest"; import { handlers } from "./handlers.js"; diff --git a/source/shared.ts b/source/shared.ts index 5d18ddb..324d269 100644 --- a/source/shared.ts +++ b/source/shared.ts @@ -13,10 +13,6 @@ export function isObject(value: unknown): value is Record { return typeof value === "object" && value !== null; } -function noop() { - /* */ -} - export class MessengerError extends Error { override name = "MessengerError"; } @@ -24,20 +20,6 @@ export class MessengerError extends Error { // @ts-expect-error Wrong `errorConstructors` types errorConstructors.set("MessengerError", MessengerError); -// .bind preserves the call location in the console -const debug = console.debug.bind(console, "Messenger:"); -const warn = console.warn.bind(console, "Messenger:"); - -export const log = { debug, warn }; - -export function toggleLogging(enabled: boolean): void { - log.debug = enabled ? debug : noop; - log.warn = enabled ? warn : noop; -} - -// Default to "no logs" -toggleLogging(false); - export function isErrorObject(error: unknown): error is ErrorObject { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is a type guard function and it uses ?. return typeof (error as any)?.message === "string"; diff --git a/source/thisTarget.ts b/source/thisTarget.ts index 6bd6fe5..a20d387 100644 --- a/source/thisTarget.ts +++ b/source/thisTarget.ts @@ -14,7 +14,8 @@ import { type Sender, type FrameTarget, } from "./types.js"; -import { log, MessengerError, once } from "./shared.js"; +import { MessengerError, once } from "./shared.js"; +import { log } from "./logging.js"; import { type Entries } from "type-fest"; /** @@ -152,7 +153,7 @@ export function __getTabData(this: MessengerMeta): AnyTarget { } export async function getThisFrame(): Promise { - await storeTabData(); // It should already have been called by we still need to await it + await storeTabData(); // It should already have been called but we still need to await it const { tabId, frameId } = thisTarget;