refactor(browser): return complete scenario observations
This commit is contained in:
@@ -15,8 +15,6 @@ import {
|
||||
type BrowserScenarioEvent,
|
||||
} from "../domain/browserScenarioCapture.js";
|
||||
import type { BrowserScenarioSecrets } from "./BrowserScenarioSecrets.js";
|
||||
import type { BrowserScenarioCaptureBudget } from "./PlaywrightScenarioArtifacts.js";
|
||||
import type { BrowserScenario } from "../domain/browserScenario.js";
|
||||
|
||||
type EventName =
|
||||
| "console"
|
||||
@@ -34,61 +32,25 @@ type UnindexedEvent = BrowserScenarioEvent extends infer Event
|
||||
: never
|
||||
: never;
|
||||
|
||||
type LimitedSection = "frames" | "workers" | "popups" | "websockets";
|
||||
type TruncatedSection = "events" | LimitedSection;
|
||||
|
||||
interface EventCaptureOptions {
|
||||
readonly page: Page;
|
||||
readonly enabled: ReadonlySet<EventName>;
|
||||
readonly limits: Pick<
|
||||
BrowserScenario["limits"],
|
||||
| "max_events"
|
||||
| "max_frames"
|
||||
| "max_workers"
|
||||
| "max_popups"
|
||||
| "max_websockets"
|
||||
>;
|
||||
readonly secrets: BrowserScenarioSecrets;
|
||||
readonly budget: BrowserScenarioCaptureBudget;
|
||||
readonly allowedOrigins: readonly string[];
|
||||
}
|
||||
|
||||
/** Bounded, arrival-ordered Playwright event capture with current-step attribution. */
|
||||
/** Arrival-ordered Playwright event capture with current-step attribution. */
|
||||
export class PlaywrightScenarioEvents {
|
||||
private readonly items: BrowserScenarioEvent[] = [];
|
||||
private readonly truncated = new Set<TruncatedSection>();
|
||||
private readonly admitted = {
|
||||
frames: new WeakSet<object>(),
|
||||
workers: new WeakSet<object>(),
|
||||
popups: new WeakSet<object>(),
|
||||
websockets: new WeakSet<object>(),
|
||||
};
|
||||
private readonly rejected = {
|
||||
frames: new WeakSet<object>(),
|
||||
workers: new WeakSet<object>(),
|
||||
popups: new WeakSet<object>(),
|
||||
websockets: new WeakSet<object>(),
|
||||
};
|
||||
private readonly counts: Record<LimitedSection, number> = {
|
||||
frames: 0,
|
||||
workers: 0,
|
||||
popups: 0,
|
||||
websockets: 0,
|
||||
};
|
||||
private dropped = 0;
|
||||
private stepIndex = 0;
|
||||
private sequence = 0;
|
||||
private readonly enabled: ReadonlySet<EventName>;
|
||||
private readonly limits: EventCaptureOptions["limits"];
|
||||
private readonly secrets: BrowserScenarioSecrets;
|
||||
private readonly budget: BrowserScenarioCaptureBudget;
|
||||
private readonly allowedOrigins: ReadonlySet<string>;
|
||||
|
||||
constructor(options: EventCaptureOptions) {
|
||||
this.enabled = options.enabled;
|
||||
this.limits = options.limits;
|
||||
this.secrets = options.secrets;
|
||||
this.budget = options.budget;
|
||||
this.allowedOrigins = new Set(options.allowedOrigins);
|
||||
this.observePage(options.page);
|
||||
}
|
||||
@@ -112,55 +74,21 @@ export class PlaywrightScenarioEvents {
|
||||
} {
|
||||
return {
|
||||
retained: this.items.length,
|
||||
dropped: this.dropped,
|
||||
dropped: 0,
|
||||
items: this.items,
|
||||
};
|
||||
}
|
||||
|
||||
truncationSections(): readonly TruncatedSection[] {
|
||||
return [...this.truncated].sort();
|
||||
}
|
||||
|
||||
private push(event: UnindexedEvent): void {
|
||||
this.sequence += 1;
|
||||
if (this.items.length >= this.limits.max_events) {
|
||||
this.dropped += 1;
|
||||
this.truncated.add("events");
|
||||
return;
|
||||
}
|
||||
const parsed = browserScenarioEventSchema.parse({
|
||||
...event,
|
||||
sequence: this.sequence,
|
||||
step_index: this.stepIndex,
|
||||
});
|
||||
if (!this.budget.claimMetadata(Buffer.byteLength(JSON.stringify(parsed)))) {
|
||||
this.dropped += 1;
|
||||
this.truncated.add("events");
|
||||
return;
|
||||
}
|
||||
this.items.push(parsed);
|
||||
}
|
||||
|
||||
private admit(
|
||||
section: LimitedSection,
|
||||
value: object,
|
||||
maximum: number,
|
||||
): boolean {
|
||||
if (this.admitted[section].has(value)) return true;
|
||||
if (this.rejected[section].has(value)) return false;
|
||||
if (this.counts[section] >= maximum) {
|
||||
this.rejected[section].add(value);
|
||||
this.sequence += 1;
|
||||
this.dropped += 1;
|
||||
this.truncated.add("events");
|
||||
this.truncated.add(section);
|
||||
return false;
|
||||
}
|
||||
this.admitted[section].add(value);
|
||||
this.counts[section] += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
private observePage(page: Page): void {
|
||||
if (this.enabled.has("console"))
|
||||
page.on("console", (message) => {
|
||||
@@ -171,11 +99,11 @@ export class PlaywrightScenarioEvents {
|
||||
if (this.pageInScope(page))
|
||||
this.push({
|
||||
kind: "page-error",
|
||||
message: this.secrets.redact(error.message).slice(0, 65_536),
|
||||
message: this.secrets.redact(error.message),
|
||||
stack:
|
||||
error.stack === undefined
|
||||
? null
|
||||
: this.secrets.redact(error.stack).slice(0, 262_144),
|
||||
: this.secrets.redact(error.stack),
|
||||
});
|
||||
});
|
||||
if (this.enabled.has("network")) {
|
||||
@@ -234,30 +162,27 @@ export class PlaywrightScenarioEvents {
|
||||
const location = message.location();
|
||||
this.push({
|
||||
kind: "console",
|
||||
level: this.secrets.redact(message.type()).slice(0, 64),
|
||||
text: this.secrets.redact(message.text()).slice(0, 65_536),
|
||||
level: this.secrets.redact(message.type()),
|
||||
text: this.secrets.redact(message.text()),
|
||||
url: location.url === "" ? null : this.safeUrl(location.url),
|
||||
});
|
||||
}
|
||||
|
||||
private request(kind: "request" | "request-failed", request: Request): void {
|
||||
const observation = {
|
||||
method: this.secrets.redact(request.method()).slice(0, 32),
|
||||
method: this.secrets.redact(request.method()),
|
||||
url: this.safeUrl(request.url()),
|
||||
resource_type: this.secrets.redact(request.resourceType()).slice(0, 64),
|
||||
resource_type: this.secrets.redact(request.resourceType()),
|
||||
header_names: Object.keys(request.headers())
|
||||
.map((name) => this.secrets.redact(name).slice(0, 256))
|
||||
.sort()
|
||||
.slice(0, 256),
|
||||
.map((name) => this.secrets.redact(name))
|
||||
.sort(),
|
||||
};
|
||||
if (kind === "request-failed") {
|
||||
this.push({
|
||||
...observation,
|
||||
kind,
|
||||
status: null,
|
||||
failure: this.secrets
|
||||
.redact(request.failure()?.errorText ?? "unknown")
|
||||
.slice(0, 1_024),
|
||||
failure: this.secrets.redact(request.failure()?.errorText ?? "unknown"),
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -273,20 +198,18 @@ export class PlaywrightScenarioEvents {
|
||||
const request = response.request();
|
||||
this.push({
|
||||
kind: "response",
|
||||
method: this.secrets.redact(request.method()).slice(0, 32),
|
||||
method: this.secrets.redact(request.method()),
|
||||
url: this.safeUrl(response.url()),
|
||||
resource_type: this.secrets.redact(request.resourceType()).slice(0, 64),
|
||||
resource_type: this.secrets.redact(request.resourceType()),
|
||||
status: response.status(),
|
||||
header_names: Object.keys(response.headers())
|
||||
.map((name) => this.secrets.redact(name).slice(0, 256))
|
||||
.sort()
|
||||
.slice(0, 256),
|
||||
.map((name) => this.secrets.redact(name))
|
||||
.sort(),
|
||||
failure: null,
|
||||
});
|
||||
}
|
||||
|
||||
private webSocket(page: Page, socket: WebSocket): void {
|
||||
if (!this.admit("websockets", socket, this.limits.max_websockets)) return;
|
||||
const url = this.safeUrl(socket.url());
|
||||
this.push({ kind: "websocket-opened", url });
|
||||
socket.on("framesent", ({ payload }) => {
|
||||
@@ -320,14 +243,14 @@ export class PlaywrightScenarioEvents {
|
||||
});
|
||||
return;
|
||||
}
|
||||
const text = this.secrets.redact(payload).slice(0, 65_536);
|
||||
const text = this.secrets.redact(payload);
|
||||
this.push({
|
||||
kind,
|
||||
url,
|
||||
payload_type: "text",
|
||||
payload_bytes: bytes,
|
||||
payload_text: text,
|
||||
truncated: text.length < payload.length,
|
||||
truncated: false,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -335,19 +258,14 @@ export class PlaywrightScenarioEvents {
|
||||
kind: "frame-attached" | "frame-detached" | "frame-navigated",
|
||||
frame: Frame,
|
||||
): void {
|
||||
if (!this.admit("frames", frame, this.limits.max_frames)) return;
|
||||
this.push({
|
||||
kind,
|
||||
url: frame.url() === "" ? null : this.safeUrl(frame.url()),
|
||||
name:
|
||||
frame.name() === ""
|
||||
? null
|
||||
: this.secrets.redact(frame.name()).slice(0, 1_024),
|
||||
name: frame.name() === "" ? null : this.secrets.redact(frame.name()),
|
||||
});
|
||||
}
|
||||
|
||||
private worker(page: Page, worker: Worker): void {
|
||||
if (!this.admit("workers", worker, this.limits.max_workers)) return;
|
||||
const details = {
|
||||
url: this.safeUrl(worker.url()),
|
||||
name: null,
|
||||
@@ -364,7 +282,6 @@ export class PlaywrightScenarioEvents {
|
||||
void page.close().catch(() => undefined);
|
||||
return;
|
||||
}
|
||||
if (!this.admit("popups", page, this.limits.max_popups)) return;
|
||||
const opened = {
|
||||
url: page.url() === "" ? null : this.safeUrl(page.url()),
|
||||
name: null,
|
||||
@@ -385,9 +302,7 @@ export class PlaywrightScenarioEvents {
|
||||
private download(download: Download): void {
|
||||
this.push({
|
||||
kind: "download-cancelled",
|
||||
suggested_filename: this.secrets
|
||||
.redact(download.suggestedFilename())
|
||||
.slice(0, 1_024),
|
||||
suggested_filename: this.secrets.redact(download.suggestedFilename()),
|
||||
url: this.safeUrl(download.url()),
|
||||
});
|
||||
void download.cancel().catch(() => undefined);
|
||||
|
||||
Reference in New Issue
Block a user