diff --git a/src/http/core.ts b/src/http/core.ts index d48ad02..357511e 100644 --- a/src/http/core.ts +++ b/src/http/core.ts @@ -10,12 +10,29 @@ import type { RouteDef, HttpOptions } from './types'; import type { StreamEncoder } from '../encoders'; import type { Bus } from '../bus'; -// The project tsconfig's `lib` is `es2020` with no `DOM`, so the Web Crypto global is undeclared -// even though all four supported runtimes provide it. Ambient-declare only what is called — the -// same approach `src/deno.ts` takes for the Deno globals. Deliberately *not* `node:crypto`: this -// file is the runtime-neutral core, and the web-standard global is what Deno, Bun and workerd all -// have without a compatibility flag. -declare const crypto: { randomUUID(): string }; +/** + * `randomUUID`, from wherever this runtime keeps it. Resolved once, at import. + * + * The web-standard global stays the primary path, and that is deliberate rather than stylistic: + * this file is the runtime-neutral core, and Deno, Bun and workerd all expose `crypto` without a + * compatibility flag, while `node:crypto` is unavailable to at least one of them. + * + * Node is the exception. The global arrived **unflagged in 19**, and `engines` promises `>=18`, + * where it does not exist at all — so the previous `declare const crypto` compiled cleanly and then + * threw `ReferenceError: crypto is not defined` on the first request of every Node 18 process. Only + * CI's Node 18 job caught it; every newer Node and every other runtime hides it, which is exactly + * why that job is pinned to the floor rather than to the current release. + * + * The fallback is reached only by a runtime that lacks the global, so workerd never evaluates the + * `require` — and the ESM build has a real one, from the `createRequire` banner in `tsup.config.ts`. + */ +const randomUUID: () => string = (() => { + const webCrypto = (globalThis as { crypto?: { randomUUID?: () => string } }).crypto; + if (typeof webCrypto?.randomUUID === 'function') return () => webCrypto.randomUUID!(); + + // eslint-disable-next-line @typescript-eslint/no-require-imports + return (require('node:crypto') as { randomUUID: () => string }).randomUUID; +})(); /** * A runtime-neutral inbound request. @@ -78,7 +95,7 @@ export function correlateRequest(headers: Record=18`, and the global `crypto` only arrived unflagged in Node 19. That gap +// shipped once: `correlateRequest` compiled cleanly against an ambient declaration and then threw +// `ReferenceError: crypto is not defined` on the first request of every Node 18 process. +// +// CI's Node 18 job is what caught it, and this test is what makes it catchable without one — every +// developer machine and every other runtime has the global, so the bug is invisible where it is +// written. Stubbing the global away reproduces the floor's condition on any Node. +// +// If the floor ever moves above 18, this test and the fallback it guards can both go. +describe('correlateRequest without a global crypto (Node 18)', () => { + it('falls back to node:crypto instead of throwing ReferenceError', async () => { + vi.stubGlobal('crypto', undefined); + vi.resetModules(); + + const { correlateRequest } = await import('../src/http/core'); + const a = correlateRequest({}); + const b = correlateRequest({}); + + expect(a.requestId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/); + expect(a.requestId).not.toBe(b.requestId); + expect(correlateRequest({ 'x-request-id': 'from-gateway' }).requestId).toBe('from-gateway'); + + vi.unstubAllGlobals(); + }); +});