| Server IP : 159.203.156.69 / Your IP : 216.73.216.28 Web Server : nginx/1.24.0 System : Linux main-ubuntu 6.8.0-71-generic #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/tanviranik.com/node_modules/vinext/dist/server/ |
Upload File : |
{"version":3,"file":"socket-error-backstop.js","names":[],"sources":["../../src/server/socket-error-backstop.ts"],"sourcesContent":["/**\n * Process-level backstop for peer-disconnect errors that escape\n * per-connection / per-request error guards.\n *\n * Three real call sites in vinext that hit this:\n * - `fromWeb(fetch().body).pipe(res)` in proxyExternalRewriteNode.\n * - Streaming surfaces inside `@vitejs/plugin-rsc` with their own\n * pipe topology (destinations aren't inbound connection sockets).\n * - Outbound sockets created by middleware `fetch()`.\n *\n * Node's `pipe()` re-emits source errors onto the destination when\n * the destination has no `'error'` listener, throwing synchronously\n * inside a `nextTick` callback. The throw escapes to\n * `uncaughtException`, where this listener filters it.\n *\n * Filters strictly on peer-disconnect codes (ECONNRESET / EPIPE /\n * ECONNABORTED) and synchronously re-throws everything else,\n * preserving Node's default crash semantics for genuine bugs. This\n * is more conservative than Next.js's equivalent\n * (`router-server.ts`'s log-only handler), which silently swallows\n * every uncaught — vinext keeps real bugs surfacing.\n *\n * **Installed at module load.** Earlier iterations tried to gate\n * install via Vite's `config()` hook (`command === \"serve\"`) so it\n * was strictly dev-only, but the hook didn't fire reliably in\n * vite-plus's lifecycle — install was silently skipped. The\n * connection-level guard from #911 confirms `configureServer`-tied\n * lifecycle hooks are timing-fragile too. Module-load install is\n * the only place reliably observed to fire (verified via the\n * `VINEXT_DEBUG_SOCKET_ERRORS` marker).\n *\n * **Prerender check is dynamic, not install-time.** Prerender (in\n * `build/prerender.ts` and `build/run-prerender.ts`) calls\n * `startProdServer()` from inside `vinext build` to render pages\n * against a real HTTP server. User `fetch()` calls during prerender\n * hit external APIs that can drop connections; absorbing those would\n * silently produce corrupt prerendered output instead of crashing\n * the build. Prerender already sets `VINEXT_PRERENDER=1` for its\n * own purposes — the listener checks it at fire time and re-throws\n * unconditionally when set, acting as if no listener were installed.\n * Doing the check at install time wouldn't work: `index.ts` loads at\n * Vite plugin import, well before prerender starts, so by the time\n * `VINEXT_PRERENDER` is set the listener has already been installed\n * (we cannot uninstall and re-install per phase).\n *\n * Side-effect of the unconditional re-throw during prerender: an\n * orchestrator-induced ECONNRESET (e.g. the prerender's own HTTP\n * client aborting a stuck route fetch) will surface the build crash\n * as `Error: read ECONNRESET` rather than the underlying route\n * failure. Acceptable trade-off — a hung prerender route is itself\n * a build problem worth surfacing — but the resulting stack will\n * point at the disconnect, not the cause. Set\n * `VINEXT_DEBUG_SOCKET_ERRORS=1` to log peer-disconnect codes if\n * you need to disambiguate.\n *\n * **Test skip is install-time.** Vitest workers that import\n * `index.ts` directly should never have the listener installed —\n * peer-disconnect errors during test runs should surface normally.\n * `process.env.VITEST === \"true\"` is set by Vitest in every worker;\n * `NODE_ENV === \"test\"` covers other test runners that follow the\n * standard convention.\n *\n * **Listener ordering.** `index.ts` is imported synchronously at the\n * top of every user's `vite.config.ts`, so vinext's listener registers\n * before most user / tooling listeners (Sentry, OpenTelemetry,\n * structured logging). For peer-disconnect codes the early-return is\n * fine. For non-peer-disconnect errors the synchronous re-throw still\n * crashes the process with the original stack, but listeners\n * registered after vinext don't observe the event. Users who need\n * crash-reporter visibility for non-peer-disconnect errors must\n * register their handler before importing vinext.\n *\n * **Symbol.for caveat.** `Symbol.for(\"vinext.socketErrorBackstop\")`\n * is process-global, so if two different vinext versions are loaded\n * in the same process the first to evaluate wins and the second's\n * filter rules silently don't apply.\n *\n * Set `VINEXT_DEBUG_SOCKET_ERRORS=1` to log a one-line marker each\n * time the listener absorbs an error.\n */\nconst SOCKET_BACKSTOP_FLAG = Symbol.for(\"vinext.socketErrorBackstop\");\n\n/**\n * Pure predicate: returns the peer-disconnect code when `err` carries\n * one of `ECONNRESET` / `EPIPE` / `ECONNABORTED`, otherwise `undefined`.\n * Exported for unit testing in isolation (no process-state mutation).\n */\nexport function peerDisconnectCode(err: unknown): string | undefined {\n const code = (err as { code?: string } | null)?.code;\n return code === \"ECONNRESET\" || code === \"EPIPE\" || code === \"ECONNABORTED\" ? code : undefined;\n}\n\n/**\n * Test-only: returns whether the backstop has been installed in this\n * process. Used by the unit test to assert idempotent install via the\n * Symbol.for guard. Not part of the public API.\n */\nexport function isSocketErrorBackstopInstalled(): boolean {\n return Boolean(\n (process as typeof process & { [SOCKET_BACKSTOP_FLAG]?: true })[SOCKET_BACKSTOP_FLAG],\n );\n}\n\nexport function installSocketErrorBackstop(): void {\n const proc = process as typeof process & { [SOCKET_BACKSTOP_FLAG]?: true };\n if (proc[SOCKET_BACKSTOP_FLAG]) return;\n if (process.env.VITEST === \"true\" || process.env.NODE_ENV === \"test\") return;\n proc[SOCKET_BACKSTOP_FLAG] = true;\n\n const debug = process.env.VINEXT_DEBUG_SOCKET_ERRORS === \"1\";\n if (debug) console.warn(\"[vinext] socket-error backstop installed\");\n process.on(\"uncaughtException\", (err: Error) => {\n if (process.env.VINEXT_PRERENDER === \"1\") throw err;\n const code = peerDisconnectCode(err);\n if (code) {\n if (debug) console.warn(`[vinext] absorbed uncaughtException ${code}`);\n return;\n }\n throw err;\n });\n process.on(\"unhandledRejection\", (reason: unknown) => {\n if (process.env.VINEXT_PRERENDER === \"1\") throw reason;\n const code = peerDisconnectCode(reason);\n if (code) {\n if (debug) console.warn(`[vinext] absorbed unhandledRejection ${code}`);\n return;\n }\n throw reason;\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgFA,MAAM,uBAAuB,OAAO,IAAI,6BAA6B;;;;;;AAOrE,SAAgB,mBAAmB,KAAkC;CACnE,MAAM,OAAQ,KAAkC;CAChD,OAAO,SAAS,gBAAgB,SAAS,WAAW,SAAS,iBAAiB,OAAO,KAAA;;;;;;;AAQvF,SAAgB,iCAA0C;CACxD,OAAO,QACJ,QAA+D,sBACjE;;AAGH,SAAgB,6BAAmC;CACjD,MAAM,OAAO;CACb,IAAI,KAAK,uBAAuB;CAChC,IAAI,QAAQ,IAAI,WAAW,UAAU,QAAQ,IAAI,aAAa,QAAQ;CACtE,KAAK,wBAAwB;CAE7B,MAAM,QAAQ,QAAQ,IAAI,+BAA+B;CACzD,IAAI,OAAO,QAAQ,KAAK,2CAA2C;CACnE,QAAQ,GAAG,sBAAsB,QAAe;EAC9C,IAAI,QAAQ,IAAI,qBAAqB,KAAK,MAAM;EAChD,MAAM,OAAO,mBAAmB,IAAI;EACpC,IAAI,MAAM;GACR,IAAI,OAAO,QAAQ,KAAK,uCAAuC,OAAO;GACtE;;EAEF,MAAM;GACN;CACF,QAAQ,GAAG,uBAAuB,WAAoB;EACpD,IAAI,QAAQ,IAAI,qBAAqB,KAAK,MAAM;EAChD,MAAM,OAAO,mBAAmB,OAAO;EACvC,IAAI,MAAM;GACR,IAAI,OAAO,QAAQ,KAAK,wCAAwC,OAAO;GACvE;;EAEF,MAAM;GACN"}