all repos — sz-chrome-remote-cdp @ 12d7a38ba0f1b1e4e301fce81e22f4b93036d211

pi-browser-harness, patched to drive a Chrome on another host over CDP, with castle_cdp_search web search

src/cdp/network-buffer.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
/**
 * 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";

export type NetworkRecord = {
  requestId: string;
  method: string;
  url: string;
  status?: number;
  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;
};

export type NetworkBuffer = {
  ingestRequestWillBeSent(p: unknown): void;
  ingestResponseReceived(p: unknown): void;
  ingestLoadingFinished(p: unknown): void;
  ingestLoadingFailed(p: unknown): void;
  drain(filter: NetworkFilter): DrainResult;
  clear(): void;
};

const compileUrlMatcher = (pattern: string): ((url: string) => boolean) => {
  if (pattern.length >= 2 && pattern.startsWith("/") && pattern.endsWith("/")) {
    try {
      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;

  const evictOldestIfFull = (): void => {
    while (records.size >= capacity) {
      const oldest = records.keys().next();
      if (oldest.done) return;
      records.delete(oldest.value);
      overflowed = true;
    }
  };

  return {
    ingestRequestWillBeSent(p) {
      const decoded = decodeEvent("Network.requestWillBeSent", p);
      const params = decoded.success ? decoded.data : undefined;
      const id = params?.requestId;
      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;
      records.set(id, {
        requestId: id,
        method,
        url,
        type: params.type ?? "Other",
        requestStartedMs: Date.now(),
        requestBodySize: postData ? Buffer.byteLength(postData, "utf8") : 0,
      });
    },

    ingestResponseReceived(p) {
      const decoded = decodeEvent("Network.responseReceived", p);
      const params = decoded.success ? decoded.data : undefined;
      const id = params?.requestId;
      if (!id) return;
      const r = records.get(id);
      if (!r) return;
      if (params?.response?.status !== undefined) r.status = params.response.status;
      if (params?.response?.statusText !== undefined && params.response.statusText !== "") r.statusText = params.response.statusText;
      if (params?.response?.mimeType !== undefined) r.mimeType = params.response.mimeType;
      // type is more accurate on responseReceived (CDP sometimes refines it)
      if (params?.type !== undefined) r.type = params.type;
    },

    ingestLoadingFinished(p) {
      const decoded = decodeEvent("Network.loadingFinished", p);
      const params = decoded.success ? decoded.data : undefined;
      const id = params?.requestId;
      if (!id) return;
      const r = records.get(id);
      if (!r) return;
      if (params?.encodedDataLength !== undefined) r.responseBodySize = params.encodedDataLength;
      r.durationMs = Date.now() - r.requestStartedMs;
    },

    ingestLoadingFailed(p) {
      const decoded = decodeEvent("Network.loadingFailed", p);
      const params = decoded.success ? decoded.data : undefined;
      const id = params?.requestId;
      if (!id) return;
      const r = records.get(id);
      if (!r) return;
      r.failed = true;
      if (params?.errorText !== undefined) r.errorText = params.errorText;
      r.durationMs = Date.now() - r.requestStartedMs;
    },

    drain(filter) {
      const matchUrl = filter.urlPattern !== undefined ? compileUrlMatcher(filter.urlPattern) : undefined;
      const methods = filter.methodFilter ? new Set(filter.methodFilter.map((m) => m.toUpperCase())) : undefined;
      const types = filter.resourceTypes ? new Set(filter.resourceTypes.map((t) => t.toLowerCase())) : undefined;
      const minStatus = filter.statusFilter?.min;
      const maxStatus = filter.statusFilter?.max;
      const cutoff = filter.sinceMs !== undefined ? Date.now() - filter.sinceMs : undefined;

      const matched: NetworkRecord[] = [];
      for (const r of records.values()) {
        if (matchUrl && !matchUrl(r.url)) continue;
        if (methods && !methods.has(r.method.toUpperCase())) continue;
        if (types && !types.has(r.type.toLowerCase())) continue;
        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 bufferOverflowed = overflowed;
      overflowed = false;

      return { records: limited, total, bufferOverflowed };
    },

    clear() {
      records.clear();
      overflowed = false;
    },
  };
};