Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

refactor(browser): share scenario transaction identity indexes

morluto407a71805079
diff --git a/src/browser/assets/ScenarioScriptCapture.ts b/src/browser/assets/ScenarioScriptCapture.ts
index 9bb08fbc9..e2638b7e7 100644
--- a/src/browser/assets/ScenarioScriptCapture.ts
+++ b/src/browser/assets/ScenarioScriptCapture.ts
@@ -2,22 +2,18 @@ import type { BrowserScenarioCapture } from "../../domain/browserScenarioCapture
import type { BrowserScenarioEvent } from "../../domain/browserScenarioCaptureValues.js";
import type { BrowserNetworkBody } from "../../domain/browserNetworkEvidence.js";
import type { CapturedWebScript } from "../../domain/webScriptExport.js";
+import {
+ indexBrowserScenarioTransactions,
+ type BrowserScenarioTransactionIndex,
+} from "../../domain/browserScenarioTransactions.js";
type RequestEvent = Extract<BrowserScenarioEvent, { kind: "request" }>;
-type ResponseEvent = Extract<BrowserScenarioEvent, { kind: "response" }>;
-type ContentEvent = Extract<BrowserScenarioEvent, { kind: "network-content" }>;
-
-interface ResponseIndex {
- readonly responses: Map<string, ResponseEvent[]>;
- readonly contents: Map<number, ContentEvent[]>;
- readonly requests: Map<string, number>;
-}
/** Join script response bytes only through explicit transaction and event identity. */
export const scenarioScripts = (
capture: BrowserScenarioCapture,
): CapturedWebScript[] => {
- const index = indexResponses(capture.events.items);
+ const index = indexBrowserScenarioTransactions(capture.events.items);
return capture.events.items.flatMap((request): CapturedWebScript[] => {
if (request.kind !== "request" || request.resource_type !== "script")
return [];
@@ -25,40 +21,24 @@ export const scenarioScripts = (
});
};
-const indexResponses = (
- events: readonly BrowserScenarioEvent[],
-): ResponseIndex => {
- const index: ResponseIndex = {
- responses: new Map(),
- contents: new Map(),
- requests: new Map(),
- };
- for (const event of events) {
- if (event.kind === "request")
- index.requests.set(
- event.transaction_id,
- (index.requests.get(event.transaction_id) ?? 0) + 1,
- );
- if (event.kind === "response")
- append(index.responses, event.transaction_id, event);
- if (event.kind === "network-content" && event.phase === "response")
- append(index.contents, event.source_event_sequence, event);
- }
- return index;
-};
-
const projectRequest = (
request: RequestEvent,
- index: ResponseIndex,
+ index: BrowserScenarioTransactionIndex,
): CapturedWebScript => {
const id = request.transaction_id;
const matches = index.responses.get(id) ?? [];
const response = matches.length === 1 ? matches[0] : undefined;
const bodies =
- response === undefined ? [] : (index.contents.get(response.sequence) ?? []);
+ response === undefined
+ ? []
+ : (index.contents.get(response.sequence) ?? []).filter(
+ ({ phase }) => phase === "response",
+ );
const body = bodies.length === 1 ? bodies[0]?.body : undefined;
const ambiguous =
- index.requests.get(id) !== 1 || matches.length > 1 || bodies.length > 1;
+ index.requestCounts.get(id) !== 1 ||
+ matches.length > 1 ||
+ bodies.length > 1;
return {
source: {
kind: "scenario-response",
@@ -115,13 +95,3 @@ const projectBody = (
representation: body.representation,
};
};
-
-const append = <Key, Value>(
- map: Map<Key, Value[]>,
- key: Key,
- value: Value,
-): void => {
- const existing = map.get(key);
- if (existing === undefined) map.set(key, [value]);
- else existing.push(value);
-};
diff --git a/src/domain/analysisView/scenarioCaptureRecords.ts b/src/domain/analysisView/scenarioCaptureRecords.ts
index f5d0d2751..ab144a6ba 100644
--- a/src/domain/analysisView/scenarioCaptureRecords.ts
+++ b/src/domain/analysisView/scenarioCaptureRecords.ts
@@ -9,6 +9,10 @@ import {
type BrowserScenarioCapture,
} from "../browserScenarioCapture.js";
import type { BrowserScenarioEvent } from "../browserScenarioCaptureValues.js";
+import {
+ indexBrowserScenarioTransactions,
+ type BrowserScenarioTransactionIndex,
+} from "../browserScenarioTransactions.js";
import type { JsonValue } from "../jsonValue.js";
import { err, ok, type Result } from "../result.js";
import type { WebNetworkCaptureRecord } from "../webNetworkCapture.js";
@@ -18,7 +22,6 @@ import type { WebNetworkProjectionAccumulator } from "./WebNetworkProjectionAccu
import { textBytes } from "./webNetworkRouteTemplates.js";
type RequestEvent = Extract<BrowserScenarioEvent, { kind: "request" }>;
-type ResponseEvent = Extract<BrowserScenarioEvent, { kind: "response" }>;
type ContentEvent = Extract<BrowserScenarioEvent, { kind: "network-content" }>;
type CapturedBody = Extract<BrowserNetworkBody, { state: "captured" }>;
@@ -29,51 +32,9 @@ export interface ScenarioCaptureRecords {
readonly limitations: readonly string[];
}
-interface TransactionIndex {
- readonly requests: { readonly event: RequestEvent; readonly item: number }[];
- readonly requestCounts: Map<string, number>;
- readonly responses: Map<string, ResponseEvent[]>;
- /** Content events keyed by the sequence of the request or response event they belong to. */
- readonly contents: Map<number, ContentEvent[]>;
-}
-
-const append = <Key, Value>(
- map: Map<Key, Value[]>,
- key: Key,
- value: Value,
-): void => {
- const existing = map.get(key);
- if (existing === undefined) map.set(key, [value]);
- else existing.push(value);
-};
-
-const indexTransactions = (
- events: readonly BrowserScenarioEvent[],
-): TransactionIndex => {
- const index: TransactionIndex = {
- requests: [],
- requestCounts: new Map(),
- responses: new Map(),
- contents: new Map(),
- };
- for (const [item, event] of events.entries()) {
- if (event.kind === "request") {
- index.requests.push({ event, item });
- index.requestCounts.set(
- event.transaction_id,
- (index.requestCounts.get(event.transaction_id) ?? 0) + 1,
- );
- } else if (event.kind === "response")
- append(index.responses, event.transaction_id, event);
- else if (event.kind === "network-content")
- append(index.contents, event.source_event_sequence, event);
- }
- return index;
-};
-
/** The one content event of an exchange phase, or undefined when absent or ambiguous. */
const onlyContent = (
- index: TransactionIndex,
+ index: BrowserScenarioTransactionIndex,
sequence: number | undefined,
): ContentEvent | undefined => {
const found =
@@ -140,8 +101,8 @@ const reportedRequest = (
const record = (
ordinal: number,
- observed: TransactionIndex["requests"][number],
- index: TransactionIndex,
+ observed: { readonly event: RequestEvent; readonly item: number },
+ index: BrowserScenarioTransactionIndex,
purpose: "manifest" | "replay",
): {
readonly record: WebNetworkCaptureRecord;
@@ -226,13 +187,14 @@ export const scenarioCaptureRecords = (
capture: BrowserScenarioCapture,
purpose: "manifest" | "replay",
): ScenarioCaptureRecords => {
- const index = indexTransactions(capture.events.items);
+ const index = indexBrowserScenarioTransactions(capture.events.items);
const records: WebNetworkCaptureRecord[] = [];
let withoutBody = 0;
let representedCount = 0;
let redactedBodies = 0;
- for (const [ordinal, observed] of index.requests.entries()) {
- const projected = record(ordinal, observed, index, purpose);
+ for (const [item, event] of capture.events.items.entries()) {
+ if (event.kind !== "request") continue;
+ const projected = record(records.length, { event, item }, index, purpose);
records.push(projected.record);
if (!projected.represented) continue;
representedCount += 1;
diff --git a/src/domain/browserScenarioTransactions.ts b/src/domain/browserScenarioTransactions.ts
new file mode 100644
index 000000000..39501bc7b
--- /dev/null
+++ b/src/domain/browserScenarioTransactions.ts
@@ -0,0 +1,44 @@
+import type { BrowserScenarioEvent } from "./browserScenarioCaptureValues.js";
+
+type ResponseEvent = Extract<BrowserScenarioEvent, { kind: "response" }>;
+type ContentEvent = Extract<BrowserScenarioEvent, { kind: "network-content" }>;
+
+/** Shared event identity lookups; projections retain their own ambiguity policy. */
+export interface BrowserScenarioTransactionIndex {
+ readonly requestCounts: ReadonlyMap<string, number>;
+ readonly responses: ReadonlyMap<string, readonly ResponseEvent[]>;
+ readonly contents: ReadonlyMap<number, readonly ContentEvent[]>;
+}
+
+/** Index the captured transaction and source-event identities without interpreting content. */
+export const indexBrowserScenarioTransactions = (
+ events: readonly BrowserScenarioEvent[],
+): BrowserScenarioTransactionIndex => {
+ const requestCounts = new Map<string, number>();
+ const responses = new Map<string, ResponseEvent[]>();
+ const contents = new Map<number, ContentEvent[]>();
+
+ for (const event of events) {
+ if (event.kind === "request")
+ requestCounts.set(
+ event.transaction_id,
+ (requestCounts.get(event.transaction_id) ?? 0) + 1,
+ );
+ else if (event.kind === "response")
+ append(responses, event.transaction_id, event);
+ else if (event.kind === "network-content")
+ append(contents, event.source_event_sequence, event);
+ }
+
+ return { requestCounts, responses, contents };
+};
+
+const append = <Key, Value>(
+ map: Map<Key, Value[]>,
+ key: Key,
+ value: Value,
+): void => {
+ const existing = map.get(key);
+ if (existing === undefined) map.set(key, [value]);
+ else existing.push(value);
+};