all repos — sz-chrome-remote-cdp @ master

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
import { decodeEvent, type EventParamsOf } from "./events";
import { createRecordStore } from "./record-store";

export type NetworkRecord = {
  requestId: string;
  method: string;
  url: string;
  status?: number;
  statusText?: string;
  type: string;
  mimeType?: string;
  requestStartedMs: number;
  durationMs?: number;
  requestBodySize: number;
  responseBodySize?: number;
  failed?: boolean;
  errorText?: string;
  body?: string | null;
};

export type NetworkFilter = {
  urlPattern?: string;
  methodFilter?: string[];
  statusFilter?: { min?: number; max?: number };
  resourceTypes?: string[];
  sinceMs?: number;
  limit?: number;
};

export type DrainResult = {
  readonly records: NetworkRecord[];
  readonly total: number;
  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 {}
  }
  return (url) => url.includes(pattern);
};

type UpdateEvent = "Network.responseReceived" | "Network.loadingFinished" | "Network.loadingFailed";

export const createNetworkBuffer = (capacity = 500): NetworkBuffer => {
  const records = createRecordStore<string, NetworkRecord>(capacity);

  // All three follow-up events say the same thing first: decode, find the request they belong to, give up quietly if it is gone.
  const update = <E extends UpdateEvent>(
    method: E,
    p: unknown,
    apply: (r: NetworkRecord, params: EventParamsOf<E>) => void,
  ): void => {
    const decoded = decodeEvent(method, p);
    if (!decoded.success) return;
    const id = decoded.data.requestId;
    if (!id) return;
    const r = records.get(id);
    if (!r) return;
    apply(r, decoded.data);
  };

  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;
      records.delete(id);
      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) {
      update("Network.responseReceived", p, (r, params) => {
        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) {
      update("Network.loadingFinished", p, (r, params) => {
        if (params.encodedDataLength !== undefined) r.responseBodySize = params.encodedDataLength;
        r.durationMs = Date.now() - r.requestStartedMs;
      });
    },

    ingestLoadingFailed(p) {
      update("Network.loadingFailed", p, (r, params) => {
        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;
        matched.push({ ...r });
      }

      return records.page(matched, filter.limit);
    },

    clear() {
      records.clear();
    },
  };
};