diff --git a/README.md b/README.md index 19ad608..027abdb 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ process.exit(await runner.runTests()); Check [Options](#options) for full option list. -## Handle +## HostHandle APIs similar to [`JSHandle` in Playwright](https://playwright.dev/docs/api/class-jshandle). diff --git a/src/WS/WSClient.ts b/src/WS/WSClient.ts index 2fa7ae3..99bf1c3 100644 --- a/src/WS/WSClient.ts +++ b/src/WS/WSClient.ts @@ -7,7 +7,7 @@ import { import RouteHandler, { RouteHandlerCallback, RouteMatcher, RouteOptions } from './route/RouteHandler.js'; import RouteRequest from './route/RouteRequest.js'; import Route from './route/Route.js'; -import NodeHandle from './handle/NodeHandle.js'; +import HostHandle from './handle/HostHandle.js'; export default class WSClient { private readonly uuid: string; @@ -25,8 +25,8 @@ export default class WSClient { resolve(); }, { once: true }); }); - this.pageHandle = new NodeHandle(0, this.ws); - this.contextHandle = new NodeHandle(1, this.ws); + this.pageHandle = new HostHandle(0, this.ws); + this.contextHandle = new HostHandle(1, this.ws); } private routes: RouteHandler[] = []; @@ -150,7 +150,7 @@ export default class WSClient { await route.innerContinue(); } - readonly pageHandle: NodeHandle; + readonly pageHandle: HostHandle; - readonly contextHandle: NodeHandle; + readonly contextHandle: HostHandle; } diff --git a/src/WS/handle/NodeHandle.ts b/src/WS/handle/HostHandle.ts similarity index 89% rename from src/WS/handle/NodeHandle.ts rename to src/WS/handle/HostHandle.ts index 8b3aa62..ed014ed 100644 --- a/src/WS/handle/NodeHandle.ts +++ b/src/WS/handle/HostHandle.ts @@ -15,7 +15,7 @@ export type Unboxed = ? RegExp : Arg extends Error ? Error - : Arg extends NodeHandle + : Arg extends HostHandle ? T : Arg extends [infer A0, ...infer Rest] ? [Unboxed, ...Unboxed] @@ -47,7 +47,7 @@ const finalizationRegistry = new FinalizationRegistry(({ id, ws }: { })); }); -export default class NodeHandle extends Serializer.Handle { +export default class HostHandle extends Serializer.Handle { /** * Share the handle ID with an object so that the matching handles will keep referencing the node * target until all ID users are disposed or garbage-collected. @@ -116,19 +116,19 @@ export default class NodeHandle extends Serializer.Handle { nodeFunction: NodeFunctionOn, arg: Arg, createHandle: true, - ): Promise>; + ): Promise>; private async innerEvaluate( nodeFunction: NodeFunctionOn, arg: Arg, createHandle: boolean, - ): Promise> { + ): Promise> { return this.act({ action: 'evaluate', fn: String(nodeFunction), arg: JSON.stringify(Serializer.serializeValue(arg)), h: createHandle, }, (result) => { - if (createHandle) return new NodeHandle(result as number, this.ws); + if (createHandle) return new HostHandle(result as number, this.ws); return result as R; }); } @@ -142,11 +142,11 @@ export default class NodeHandle extends Serializer.Handle { evaluateHandle( nodeFunction: NodeFunctionOn, arg: Arg, - ): Promise>; + ): Promise>; evaluateHandle( nodeFunction: NodeFunctionOn, arg?: unknown, - ): Promise>; + ): Promise>; async evaluateHandle(nodeFunction: NodeFunctionOn, arg: Arg) { return this.innerEvaluate(nodeFunction, arg, true); } @@ -174,22 +174,22 @@ export default class NodeHandle extends Serializer.Handle { this.disposed = true; } - async getProperties(): Promise> { + async getProperties(): Promise> { return this.act({ action: 'get-properties', }, (result) => { - const propertiesMap: Map = new Map(); + const propertiesMap: Map = new Map(); (result as [string, number][]).forEach(([name, handleID]) => { - propertiesMap.set(name, new NodeHandle(handleID, this.ws)); + propertiesMap.set(name, new HostHandle(handleID, this.ws)); }); return propertiesMap; }); } - async getProperty(propertyName: string): Promise { + async getProperty(propertyName: string): Promise { return this.act({ action: 'get-property', name: propertyName, - }, (result) => new NodeHandle(result as number, this.ws)); + }, (result) => new HostHandle(result as number, this.ws)); } } diff --git a/src/WS/route/RouteRequest.ts b/src/WS/route/RouteRequest.ts index 2107bb5..b6963e5 100644 --- a/src/WS/route/RouteRequest.ts +++ b/src/WS/route/RouteRequest.ts @@ -1,5 +1,5 @@ import type playwright from 'playwright-core'; -import NodeHandle from '../handle/NodeHandle.js'; +import HostHandle from '../handle/HostHandle.js'; import { RouteRequestMeta } from '../message.js'; import { FallbackOverrides } from './Route.js'; @@ -10,8 +10,8 @@ export default class RouteRequest { private readonly ws: WebSocket, ) { const { frame, serviceWorker } = requestMeta; - if (frame !== null) NodeHandle.share(frame, ws, this); - if (serviceWorker !== null) NodeHandle.share(serviceWorker, ws, this); + if (frame !== null) HostHandle.share(frame, ws, this); + if (serviceWorker !== null) HostHandle.share(serviceWorker, ws, this); } private fallbackOverrides: FallbackOverrides = {}; @@ -50,7 +50,7 @@ export default class RouteRequest { if (this.requestMeta.frame === null) { throw new Error('Service Worker requests do not have an associated frame'); } - return new NodeHandle(this.requestMeta.frame, this.ws); + return new HostHandle(this.requestMeta.frame, this.ws); } headerValue(name: string) { @@ -127,7 +127,7 @@ export default class RouteRequest { serviceWorker() { if (this.requestMeta.serviceWorker === null) return null; - return new NodeHandle(this.requestMeta.serviceWorker, this.ws); + return new HostHandle(this.requestMeta.serviceWorker, this.ws); } url() { diff --git a/test/handle/basic.test.ts b/test/handle/basic.test.ts index 42aeb27..f78bed1 100644 --- a/test/handle/basic.test.ts +++ b/test/handle/basic.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from '../default.setup.js'; -import NodeHandle from '../../src/WS/handle/NodeHandle.js'; +import HostHandle from '../../src/WS/handle/HostHandle.js'; import { pageHandle } from '../../src/index.js'; describe('handle', () => { @@ -94,7 +94,7 @@ describe('handle', () => { return page.context(); }); expect(evaluated).toBe(true); - expect(contextHandle).toBeInstanceOf(NodeHandle); + expect(contextHandle).toBeInstanceOf(HostHandle); const browserVersion = await contextHandle.evaluate((context) => ( context.browser()?.version() )); @@ -122,7 +122,7 @@ describe('handle', () => { expect(props).toHaveLength(1); const [key, value] = props[0]; expect(key).toBe('p'); - expect(value).toBeInstanceOf(NodeHandle); + expect(value).toBeInstanceOf(HostHandle); await expect(value.jsonValue()).resolves.toEqual([1, 2]); }); @@ -132,8 +132,8 @@ describe('handle', () => { objHandle.getProperty('p'), objHandle.getProperty('not-exist'), ]); - expect(propHandle).toBeInstanceOf(NodeHandle); - expect(notExistPropHandle).toBeInstanceOf(NodeHandle); + expect(propHandle).toBeInstanceOf(HostHandle); + expect(notExistPropHandle).toBeInstanceOf(HostHandle); await Promise.all([ expect(propHandle.jsonValue()).resolves.toEqual([1, 2]), expect(notExistPropHandle.jsonValue()).resolves.toBe(undefined),