|
| 1 | +import { RingBuffer } from "@reprojs/sdk-utils" |
| 2 | + |
| 3 | +export interface NetworkEntry { |
| 4 | + id: string |
| 5 | + ts: number |
| 6 | + method: string |
| 7 | + url: string |
| 8 | + status: number | null |
| 9 | + durationMs: number | null |
| 10 | + size: number | null |
| 11 | + initiator: "fetch" | "xhr" |
| 12 | + requestHeaders?: Record<string, string> |
| 13 | + responseHeaders?: Record<string, string> |
| 14 | + requestBody?: string |
| 15 | + responseBody?: string |
| 16 | + error?: string |
| 17 | +} |
| 18 | + |
| 19 | +const SENTINEL = "__reprojs_patched" |
| 20 | + |
| 21 | +function rid() { |
| 22 | + return Math.random().toString(36).slice(2, 10) + Date.now().toString(36) |
| 23 | +} |
| 24 | + |
| 25 | +function headersToMap(h: HeadersInit | undefined, denylist: string[]): Record<string, string> { |
| 26 | + const out: Record<string, string> = {} |
| 27 | + if (!h) return out |
| 28 | + const set = new Set(denylist.map((k) => k.toLowerCase())) |
| 29 | + const add = (k: string, v: string) => { |
| 30 | + const lk = k.toLowerCase() |
| 31 | + out[lk] = set.has(lk) ? "[redacted]" : v |
| 32 | + } |
| 33 | + if (h instanceof Headers) { |
| 34 | + h.forEach((v, k) => add(k, v)) |
| 35 | + } else if (Array.isArray(h)) { |
| 36 | + for (const [k, v] of h) add(k, v) |
| 37 | + } else { |
| 38 | + for (const [k, v] of Object.entries(h)) add(k, v) |
| 39 | + } |
| 40 | + return out |
| 41 | +} |
| 42 | + |
| 43 | +export interface NetworkCollector { |
| 44 | + start: () => void |
| 45 | + stop: () => void |
| 46 | + snapshot: () => NetworkEntry[] |
| 47 | + clear: () => void |
| 48 | +} |
| 49 | + |
| 50 | +export function createNetworkCollector(opts: { |
| 51 | + max: number |
| 52 | + captureBodies: boolean |
| 53 | + redact: { headerDenylist: string[]; bodyRedactKeys: string[] } |
| 54 | +}): NetworkCollector { |
| 55 | + const buf = new RingBuffer<NetworkEntry>(opts.max) |
| 56 | + let originalFetch: typeof fetch | null = null |
| 57 | + |
| 58 | + function start() { |
| 59 | + const currentFetch = globalThis.fetch as typeof fetch & { [SENTINEL]?: boolean } |
| 60 | + if (currentFetch && !currentFetch[SENTINEL]) { |
| 61 | + originalFetch = currentFetch |
| 62 | + const wrapped = ((input: RequestInfo | URL, init?: RequestInit) => { |
| 63 | + const id = rid() |
| 64 | + const ts = Date.now() |
| 65 | + const method = (init?.method ?? "GET").toUpperCase() |
| 66 | + const url = |
| 67 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url |
| 68 | + const requestHeaders = headersToMap(init?.headers, opts.redact.headerDenylist) |
| 69 | + const t0 = Date.now() |
| 70 | + const original = originalFetch |
| 71 | + if (!original) return Promise.reject(new Error("fetch not captured")) |
| 72 | + return original(input as RequestInfo, init).then( |
| 73 | + (res) => { |
| 74 | + const clen = Number(res.headers.get("content-length") ?? "NaN") |
| 75 | + try { |
| 76 | + buf.push({ |
| 77 | + id, |
| 78 | + ts, |
| 79 | + method, |
| 80 | + url, |
| 81 | + status: res.status, |
| 82 | + durationMs: Date.now() - t0, |
| 83 | + size: Number.isNaN(clen) ? null : clen, |
| 84 | + initiator: "fetch", |
| 85 | + requestHeaders, |
| 86 | + responseHeaders: headersToMap(res.headers, opts.redact.headerDenylist), |
| 87 | + }) |
| 88 | + } catch { |
| 89 | + // fail-open |
| 90 | + } |
| 91 | + return res |
| 92 | + }, |
| 93 | + (err: unknown) => { |
| 94 | + try { |
| 95 | + buf.push({ |
| 96 | + id, |
| 97 | + ts, |
| 98 | + method, |
| 99 | + url, |
| 100 | + status: null, |
| 101 | + durationMs: Date.now() - t0, |
| 102 | + size: null, |
| 103 | + initiator: "fetch", |
| 104 | + requestHeaders, |
| 105 | + error: err instanceof Error ? err.message : String(err), |
| 106 | + }) |
| 107 | + } catch { |
| 108 | + // fail-open |
| 109 | + } |
| 110 | + throw err |
| 111 | + }, |
| 112 | + ) |
| 113 | + }) as typeof fetch & { [SENTINEL]?: boolean } |
| 114 | + wrapped[SENTINEL] = true |
| 115 | + globalThis.fetch = wrapped |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + function stop() { |
| 120 | + if (originalFetch) { |
| 121 | + globalThis.fetch = originalFetch |
| 122 | + originalFetch = null |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return { |
| 127 | + start, |
| 128 | + stop, |
| 129 | + snapshot: () => buf.peek(), |
| 130 | + clear: () => buf.clear(), |
| 131 | + } |
| 132 | +} |
0 commit comments