diff options
Diffstat (limited to 'src/cdp/network-buffer.ts')
| -rw-r--r-- | src/cdp/network-buffer.ts | 29 |
1 files changed, 1 insertions, 28 deletions
diff --git a/src/cdp/network-buffer.ts b/src/cdp/network-buffer.ts index c26ae73..f6ad032 100644 --- a/src/cdp/network-buffer.ts +++ b/src/cdp/network-buffer.ts @@ -1,18 +1,3 @@ -/** - * In-memory aggregator for CDP Network.* events. - * - * The CDP event stream is consumed by a single internal loop in session.ts. - * That loop hands Network.* event params to this module, which joins them into - * one record per requestId (request → response → finished/failed). Tools then - * read records via drain(), filtered to what the caller asked for. - * - * Bounded by a fixed capacity (default 500): on overflow the oldest record is - * evicted FIFO. The overflow flag is reported once per drain so tools can warn - * the LLM that data may be missing. - * - * This module has no transport dependency, so the joining/filtering logic stays - * trivially testable in isolation. - */ import { decodeEvent } from "./events"; @@ -24,34 +9,27 @@ export type NetworkRecord = { statusText?: string; type: string; mimeType?: string; - /** Wall-clock ms when requestWillBeSent fired, in Date.now() units. */ requestStartedMs: number; durationMs?: number; requestBodySize: number; responseBodySize?: number; failed?: boolean; errorText?: string; - /** Populated by the tool layer via Network.getResponseBody, never by the aggregator. */ body?: string | null; }; export type NetworkFilter = { - /** Substring match by default. Wrap in slashes (`/foo/`) for regex. */ urlPattern?: string; methodFilter?: string[]; statusFilter?: { min?: number; max?: number }; resourceTypes?: string[]; - /** Only requests whose start is at most N ms ago. */ sinceMs?: number; - /** Cap on the returned records. */ limit?: number; }; export type DrainResult = { readonly records: NetworkRecord[]; - /** Total matches before `limit` was applied. */ readonly total: number; - /** True if the ring buffer evicted at least one record since the last drain. */ readonly bufferOverflowed: boolean; }; @@ -70,15 +48,12 @@ const compileUrlMatcher = (pattern: string): ((url: string) => boolean) => { const re = new RegExp(pattern.slice(1, -1)); return (url) => re.test(url); } catch { - // Fall through to substring match if the pattern is not a valid regex. } } return (url) => url.includes(pattern); }; export const createNetworkBuffer = (capacity = 500): NetworkBuffer => { - // Insertion-ordered map keyed by requestId. Map preserves insertion order in JS, - // so iteration gives us oldest-first eviction without a separate index. const records = new Map<string, NetworkRecord>(); let overflowed = false; @@ -99,7 +74,6 @@ export const createNetworkBuffer = (capacity = 500): NetworkBuffer => { const url = params?.request.url; const method = params?.request.method; if (!id || !url || !method) return; - // Re-fired requestWillBeSent for the same id (e.g. redirect) — refresh in place. records.delete(id); evictOldestIfFull(); const postData = params.request.postData; @@ -166,13 +140,12 @@ export const createNetworkBuffer = (capacity = 500): NetworkBuffer => { if (cutoff !== undefined && r.requestStartedMs < cutoff) continue; if (minStatus !== undefined && (r.status === undefined || r.status < minStatus)) continue; if (maxStatus !== undefined && (r.status === undefined || r.status > maxStatus)) continue; - // Clone so callers can mutate (e.g. attach body) without polluting the buffer. matched.push({ ...r }); } const total = matched.length; const limit = Math.min(filter.limit ?? 50, 500); - const limited = matched.slice(-limit); // most recent N + const limited = matched.slice(-limit); const bufferOverflowed = overflowed; overflowed = false; |
