aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmail.com>2026-07-27 23:02:59 +0530
committerAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-02 19:12:46 +0530
commit44e3271c148e991e5547f36e3cfe596d71c91cfc (patch)
tree9835c1d35d7db96ed89a0d2f0d8d2fbe13305943 /src
parent9e9bf2a3badb59d0ce5bdb47c4e9d09ffb259e69 (diff)
fix(cdp): let evaluateJson target a specific session
Diffstat (limited to 'src')
-rw-r--r--src/cdp/evaluate.ts17
1 files changed, 8 insertions, 9 deletions
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 <T>(
session: CdpSession,
expression: string,
check: (v: unknown) => v is T,
+ opts?: { sessionId?: string | undefined; timeoutMs?: number | undefined },
): Promise<Result<T, CdpError>> => {
- 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 <T>(
}
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);
};