| 1 | import { AsyncLocalStorage } from "node:async_hooks"; |
| 2 | |
| 3 | export interface BrowserRequestTrace { |
| 4 | requestId: string; operationId?: string; tabId?: string; method: string; |
| 5 | phase: string; elapsedMs: number; errorKind?: string; command?: string; target?: string; |
| 6 | } |
| 7 | const requests = new AsyncLocalStorage<{ emit(event: BrowserRequestTrace): void; request: Omit<BrowserRequestTrace, "phase" | "elapsedMs"> }>(); |
| 8 | export function withBrowserDiagnosticRequest<T>(request: Omit<BrowserRequestTrace, "phase" | "elapsedMs">, emit: (event: BrowserRequestTrace) => void, work: () => T): T { |
| 9 | return requests.run({ request, emit }, work); |
| 10 | } |
| 11 | export function traceBrowserCommand(command: string, target: string, phase: string, elapsedMs: number): void { |
| 12 | const context = requests.getStore(); |
| 13 | context?.emit({ ...context.request, command, target, phase, elapsedMs }); |
| 14 | } |
| 15 |