diff --git a/js/nostr/client.js b/js/nostr/client.js index 2b5ff8d6..7483262f 100644 --- a/js/nostr/client.js +++ b/js/nostr/client.js @@ -62,6 +62,7 @@ import { logCountTimeoutCleanupFailure, logRelayCountFailure, } from "./countDiagnostics.js"; +import "./maxListenerDiagnostics.js"; import { publishEventToRelay, publishEventToRelays, diff --git a/js/nostr/countDiagnostics.js b/js/nostr/countDiagnostics.js index c731cf0f..7f63c6b0 100644 --- a/js/nostr/countDiagnostics.js +++ b/js/nostr/countDiagnostics.js @@ -1,14 +1,14 @@ // js/nostr/countDiagnostics.js -// Centralized helpers for gating high-volume COUNT diagnostics behind -// verbose dev mode and deduplicating repeated warnings so the console stays -// readable during normal development sessions. +// Centralized helpers for gating high-volume COUNT diagnostics (and other +// verbose nostr warnings) behind verbose dev mode while deduplicating repeated +// messages so the console stays readable during normal development sessions. import { devLogger } from "../utils/logger.js"; import { isVerboseDevMode } from "../config.js"; const seenWarningKeys = new Set(); -function isVerboseCountLoggingEnabled() { +export function isVerboseDiagnosticsEnabled() { if (typeof window !== "undefined" && window) { const runtimeFlag = window.__BITVID_VERBOSE_DEV_MODE__; if (typeof runtimeFlag === "boolean") { @@ -19,7 +19,7 @@ function isVerboseCountLoggingEnabled() { } function logCountWarning(message, args = [], { key, throttle = true } = {}) { - if (!isVerboseCountLoggingEnabled()) { + if (!isVerboseDiagnosticsEnabled()) { return; } diff --git a/js/nostr/maxListenerDiagnostics.js b/js/nostr/maxListenerDiagnostics.js new file mode 100644 index 00000000..09e3bbda --- /dev/null +++ b/js/nostr/maxListenerDiagnostics.js @@ -0,0 +1,36 @@ +// js/nostr/maxListenerDiagnostics.js +// Applies verbose-dev-mode-aware defaults to EventEmitter listener limits so +// high-volume MaxListeners warnings stay hidden unless explicitly requested. + +import EventEmitter from "events"; + +import { isVerboseDiagnosticsEnabled } from "./countDiagnostics.js"; + +const DEFAULT_WARNING_THRESHOLD = 10; + +function getTargetMaxListeners() { + return isVerboseDiagnosticsEnabled() ? DEFAULT_WARNING_THRESHOLD : 0; +} + +function applyMaxListenerPreference() { + try { + const targetLimit = getTargetMaxListeners(); + if (typeof EventEmitter.defaultMaxListeners === "number") { + if (EventEmitter.defaultMaxListeners !== targetLimit) { + EventEmitter.defaultMaxListeners = targetLimit; + } + return; + } + + EventEmitter.defaultMaxListeners = targetLimit; + } catch (error) { + // Swallow errors silently — diagnostics are best-effort and shouldn't + // interfere with runtime behavior if the events shim is unavailable. + } +} + +applyMaxListenerPreference(); + +export function refreshMaxListenerPreference() { + applyMaxListenerPreference(); +}