From 44e3271c148e991e5547f36e3cfe596d71c91cfc Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Mon, 27 Jul 2026 23:02:59 +0530 Subject: fix(cdp): let evaluateJson target a specific session --- src/cdp/evaluate.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/cdp/evaluate.ts b/src/cdp/evaluate.ts index cd68d61..733a11b 100644 --- a/src/cdp/evaluate.ts +++ b/src/cdp/evaluate.ts @@ -2,22 +2,21 @@ import { type Result, err, ok } from "../util/result"; import { type CdpError, cdpError } from "./errors"; import type { CdpSession } from "./session"; -/** - * Runtime.evaluate, unwrapped and narrowed. Runs `expression`, surfaces a page - * exception as an error, then applies `check` to the returned value — the - * sanctioned way to turn `unknown` evaluate output into a typed `T` without a - * cast. - */ export const evaluateJson = async ( session: CdpSession, expression: string, check: (v: unknown) => v is T, + opts?: { sessionId?: string | undefined; timeoutMs?: number | undefined }, ): Promise> => { - const r = await session.call("Runtime.evaluate", { + const params = { expression, returnByValue: true, awaitPromise: true, - }); + }; + const callOpts = opts?.timeoutMs !== undefined ? { timeoutMs: opts.timeoutMs } : {}; + const r = opts?.sessionId !== undefined + ? await session.callOnTarget("Runtime.evaluate", params, opts.sessionId, callOpts) + : await session.call("Runtime.evaluate", params, callOpts); if (!r.success) return r; const { result, exceptionDetails } = r.data; if (exceptionDetails !== undefined) { @@ -25,7 +24,7 @@ export const evaluateJson = async ( } const value = result.value; if (!check(value)) { - return err(cdpError("invalid_response", `unexpected evaluate shape for: ${expression.slice(0, 60)}`, "Runtime.evaluate")); + return err(cdpError("invalid_response", `unexpected evaluate shape (type: ${result.type}) for: ${expression.slice(0, 60)}`, "Runtime.evaluate")); } return ok(value); }; -- cgit v1.2.3