Files
reverse-engineering-anything/tests/fixtures/fakeCdpSend.ts
T
morluto 254bb21857 refactor(browser): send only CDP commands with registered result schemas (#10666)
* refactor(browser): send only CDP commands with registered result schemas

* refactor(browser): open Playwright CDP sessions only through the registered-command owner

* fix(browser): keep the restore obligation for an applied override with a malformed reply

A schema-rejected success reply means the browser applied the Emulation
override, so the attached environment owner records the reset obligation
before reporting the protocol error. Only a rejected or unanswered command
leaves no obligation.

* refactor(browser): open verifier-script CDP sessions through the registered-command owner (#10710)

* fix(browser): register the CDP commands JavaScript verifiers send

The verifier scripts send Browser.close, Page.createIsolatedWorld, Page.navigate, Page.reload, Target.activateTarget, Target.closeTarget and Target.createTarget through CdpConnection, which now admits every reply through its command's result schema. Register those schemas, refuse an unregistered run-time name as a typed malformed reply instead of a TypeError, and check script send literals against the registry.

* fix(browser): keep Target.closeTarget's success member for the verifiers
2026-11-15 14:49:33 +08:00

39 lines
1.3 KiB
TypeScript

import type { CdpConnection } from "../../src/browser/CdpConnection.js";
import {
admitCdpReply,
type CdpCommand,
} from "../../src/browser/protocol/CdpCommands.js";
type RawCdpReply = (
method: string,
params: Readonly<Record<string, unknown>>,
sessionId: string | undefined,
signal: AbortSignal | undefined,
) => unknown;
/**
* A `CdpConnection.send` fake that answers with raw wire results. Registered
* commands are admitted by their result schemas exactly as the real connection
* admits them, so a fake cannot hand consumers an unparsed reply.
*/
export const fakeCdpSend = (reply: RawCdpReply): CdpConnection["send"] =>
(async (
method: CdpCommand,
params?: Readonly<Record<string, unknown>>,
sessionId?: string,
signal?: AbortSignal,
) =>
admitCdpReply(
method,
await reply(method, params ?? {}, sessionId, signal),
"inspect_web_page",
)) as CdpConnection["send"];
/** Parse a raw `Page.getFrameTree` wire result as the connection would. */
export const frameTreeReply = (raw: unknown) =>
admitCdpReply("Page.getFrameTree", raw, "inspect_web_page");
/** Parse a raw `Page.getResourceTree` wire result as the connection would. */
export const resourceTreeReply = (raw: unknown) =>
admitCdpReply("Page.getResourceTree", raw, "inspect_web_page");