Files
core/test/node-floor.test.ts
T
chrnx 9b5347aa75
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 13s
CI / build (pull_request) Successful in 23s
CI / runtimes (deno) (pull_request) Successful in 17s
CI / runtimes (edge) (pull_request) Successful in 28s
fix(observability): correlate requests on Node 18, where there is no global crypto
`engines` promises `>=18`. The global `crypto` arrived unflagged in Node 19.
Between those two facts, `correlateRequest` compiled cleanly against an ambient
`declare const crypto` and then threw `ReferenceError: crypto is not defined` on
the first request of every Node 18 process — which is to say the observability
work made the framework unusable on its own stated floor.

Nothing local could see it. Every developer machine here runs 20 or 22, and
Deno, Bun and workerd all expose the global. CI's Node 18 job caught it on the
promotion pull request, which is the entire reason that job is pinned to the
floor rather than to the current release.

The global stays the primary path, because it is why this file does not simply
import node:crypto: it is the runtime-neutral core, and node:crypto is not
available to all four runtimes. The fallback is reached only where the global is
absent, so workerd never evaluates the require, and the ESM build has a real one
from the createRequire banner already in tsup.config.ts. Verified in both built
formats rather than assumed — esbuild rewrites it to require("crypto"), which is
the same module.

test/node-floor.test.ts stubs the global away to reproduce the floor's condition
on any Node, and was confirmed to fail against the previous code. The bug was
invisible where it was written; this makes it visible there.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-08-17 18:11:50 -05:00

28 lines
1.3 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
// The `engines` floor is `>=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();
});
});