import { AsyncLocalStorage } from 'node:async_hooks'; import { TestStepType } from '../models/test-step'; interface ProfilerRef { shouldSkip(url: string): boolean; trackOnFail: boolean; fallbackSteps: TestStepType[]; } /** * FetchInterceptor subscribes to undici diagnostics_channel events to capture * outgoing fetch() calls as REQUEST-type steps in the AsyncLocalStorage store. * * Works with Node.js 18+ global fetch (which uses undici internally). * Gracefully skips subscription on Node < 18 (no global fetch). * * Key design decisions: * - WeakMap keyed on undici request objects (same reference across all events) * - ALS context captured at undici:request:create time, NOT at headers time. * This is critical because undici connection pooling can break AsyncLocalStorage * propagation between the create and headers events when connections are reused. * - globalThis.fetch wrapping intercepts error response bodies (undici:request:bodyChunkReceived * is not available in Node 22's built-in undici) * - All handlers wrapped in try/catch (INTC-06: silent failure) * * Event flow: * undici:request:create → capture method, URL, start time, ALS context → WeakMap * undici:request:headers → build step using WeakMap data, push to ALS accumulator * undici:request:trailers → cleanup WeakMap entry * fetch wrapper → update response_body for error responses (>= 400) */ export declare class FetchInterceptor { private readonly store; private readonly profiler; private readonly pendingRequests; private readonly bodyHandlers; private readonly createHandler; private readonly headersHandler; private readonly trailersHandler; private origFetch; constructor(store: AsyncLocalStorage, profiler: ProfilerRef); /** * Subscribe to undici diagnostics_channel events and wrap global fetch for * error response body capture. * * No-op on Node < 18 (no global fetch) or if diagnostics_channel unavailable. */ subscribe(): void; /** * Unsubscribe from diagnostics_channel events and restore original global fetch. */ unsubscribe(): void; private handleCreate; private handleHeaders; private handleTrailers; /** * Called by the fetch wrapper when an error response body is available. * Finds the most recently added step for this URL/status and updates its response_body. */ private applyResponseBody; } export {};