diff --git a/README.md b/README.md index 4a89cfe8..ba94e4fe 100644 --- a/README.md +++ b/README.md @@ -449,6 +449,12 @@ artifacts now that the deploy pipeline owns the build step. - Re-exports `isDevMode` (derived from `IS_DEV_MODE`) for modules, publishes `window.__BITVID_DEV_MODE__`, and centralizes the global configuration surface. +- **Runtime globals**: + - `window.__BITVID_NIP07_LOGIN_TIMEOUT_MS__` overrides the default NIP-07 + login timeout (milliseconds) used when waiting for extension responses. + - `window.__BITVID_NIP07_ENABLE_VARIANT_TIMEOUT_MS__` overrides the timeout + (milliseconds) for `nostr.enable()` permission variants. Values must be + positive to take effect. - **`js/utils/logger.js`**: - Provides the shared `logger` utility. Route user-facing errors through `logger.user` and keep experimental diagnostics on `logger.dev` so operators diff --git a/js/nostr/nip07Permissions.js b/js/nostr/nip07Permissions.js index 9721b926..c3e7a935 100644 --- a/js/nostr/nip07Permissions.js +++ b/js/nostr/nip07Permissions.js @@ -1,15 +1,14 @@ import { devLogger, userLogger } from "../utils/logger.js"; -export const NIP07_LOGIN_TIMEOUT_MS = 60_000; // 60 seconds +export const NIP07_LOGIN_TIMEOUT_MS = 20_000; // 20 seconds export const NIP07_LOGIN_TIMEOUT_ERROR_MESSAGE = "Timed out waiting for the NIP-07 extension. Confirm the extension prompt in your browser toolbar and try again."; const NIP07_PERMISSIONS_STORAGE_KEY = "bitvid:nip07:permissions"; // Give the NIP-07 extension enough time to surface its approval prompt and let -// users unlock/authorize it. Seven seconds proved too aggressive once vendors -// started requiring an unlock step, so we extend the window substantially while +// users unlock/authorize it. The default is tuned to keep login responsive while // still allowing manual overrides via __BITVID_NIP07_ENABLE_VARIANT_TIMEOUT_MS__. -const DEFAULT_ENABLE_VARIANT_TIMEOUT_MS = 45_000; +const DEFAULT_ENABLE_VARIANT_TIMEOUT_MS = 5_000; export const DEFAULT_NIP07_ENCRYPTION_METHODS = Object.freeze([ // Encryption helpers — request both legacy NIP-04 and modern NIP-44 upfront @@ -89,6 +88,21 @@ class Nip07RequestQueue { // while allowing critical tasks (e.g. blocklist decryption) to jump the line. const requestQueue = new Nip07RequestQueue(); +export function getNip07LoginTimeoutMs() { + const overrideValue = + typeof globalThis !== "undefined" && + globalThis !== null && + Number.isFinite(globalThis.__BITVID_NIP07_LOGIN_TIMEOUT_MS__) + ? Math.floor(globalThis.__BITVID_NIP07_LOGIN_TIMEOUT_MS__) + : null; + + if (overrideValue !== null && overrideValue > 0) { + return Math.max(1_000, overrideValue); + } + + return NIP07_LOGIN_TIMEOUT_MS; +} + export function getEnableVariantTimeoutMs() { const overrideValue = typeof globalThis !== "undefined" && @@ -207,7 +221,7 @@ export function clearStoredNip07Permissions() { export function withNip07Timeout( operation, { - timeoutMs = NIP07_LOGIN_TIMEOUT_MS, + timeoutMs = getNip07LoginTimeoutMs(), message = NIP07_LOGIN_TIMEOUT_ERROR_MESSAGE, } = {}, ) { @@ -215,7 +229,7 @@ export function withNip07Timeout( const effectiveTimeout = Number.isFinite(numericTimeout) && numericTimeout > 0 ? numericTimeout - : NIP07_LOGIN_TIMEOUT_MS; + : getNip07LoginTimeoutMs(); let timeoutId; const timeoutPromise = new Promise((_, reject) => { @@ -247,7 +261,7 @@ export async function runNip07WithRetry( operation, { label = "NIP-07 operation", - timeoutMs = NIP07_LOGIN_TIMEOUT_MS, + timeoutMs = getNip07LoginTimeoutMs(), retryMultiplier = 2, priority = NIP07_PRIORITY.NORMAL, } = {}, @@ -322,9 +336,10 @@ export async function requestEnablePermissions( } const enableTimeoutMs = getEnableVariantTimeoutMs(); + const loginTimeoutMs = getNip07LoginTimeoutMs(); const primaryEnableTimeoutMs = Math.min(15_000, enableTimeoutMs); const fallbackEnableTimeoutMs = Math.min( - NIP07_LOGIN_TIMEOUT_MS, + loginTimeoutMs, enableTimeoutMs, ); @@ -372,6 +387,7 @@ export const __testExports = { runNip07WithRetry, withNip07Timeout, getEnableVariantTimeoutMs, + getNip07LoginTimeoutMs, readStoredNip07Permissions, writeStoredNip07Permissions, clearStoredNip07Permissions,