Skip to content

Commit

Permalink
Feat: discrete connect handling (#114)
Browse files Browse the repository at this point in the history
* Feat: Discrete connect handling

* Clean up connect types; fmt

* Fix accidental tempDir block move

* Cleanup unused fields

* Fix failing tests

* Fmt
  • Loading branch information
chaosharmonic authored Nov 23, 2024
1 parent 326a000 commit 4189e3e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
36 changes: 22 additions & 14 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ async function runCommand(

/** Options for launching a browser */
export interface BrowserOptions {
headless: boolean;
product: "chrome" | "firefox";
headless?: boolean;
product?: "chrome" | "firefox";
}

/**
Expand Down Expand Up @@ -223,13 +223,29 @@ export class Browser {
}
}

export interface LaunchOptions {
headless?: boolean;
export type LaunchOptions = BrowserOptions & {
path?: string;
product?: "chrome" | "firefox";
args?: string[];
wsEndpoint?: string;
cache?: string;
};

export type ConnectOptions = BrowserOptions & {
wsEndpoint: string;
};

/**
* Connects to a given browser over a WebSockets endpoint.
*/
export async function connect(opts: ConnectOptions): Promise<Browser> {
const { wsEndpoint, product = "chrome" } = opts;

const options: BrowserOptions = {
product,
};

const ws = new WebSocket(wsEndpoint);
await websocketReady(ws);
return new Browser(ws, null, options);
}

/**
Expand All @@ -239,7 +255,6 @@ export async function launch(opts?: LaunchOptions): Promise<Browser> {
const headless = opts?.headless ?? true;
const product = opts?.product ?? "chrome";
const args = opts?.args ?? [];
const wsEndpoint = opts?.wsEndpoint;
const cache = opts?.cache;
let path = opts?.path;

Expand All @@ -248,13 +263,6 @@ export async function launch(opts?: LaunchOptions): Promise<Browser> {
product,
};

// Connect to endpoint directly if one was specified
if (wsEndpoint) {
const ws = new WebSocket(wsEndpoint);
await websocketReady(ws);
return new Browser(ws, null, options);
}

if (!path) {
path = await getBinary(product, { cache });
}
Expand Down
6 changes: 3 additions & 3 deletions tests/existing_ws_endpoint_test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { launch } from "../mod.ts";
import { connect, launch } from "../mod.ts";
import { assertThrows } from "@std/assert";
import { assert } from "@std/assert/assert";

Deno.test("Test existing ws endpoint", async () => {
// Spawn one browser instance and spawn another one connecting to the first one
const a = await launch();
const b = await launch({ wsEndpoint: a.wsEndpoint() });
const b = await connect({ wsEndpoint: a.wsEndpoint() });

// Test that second instance works without any process attached
const page = await b.newPage("http://example.com");
Expand All @@ -22,7 +22,7 @@ Deno.test("Test existing ws endpoint", async () => {
Deno.test("Ensure pages are properly closed when closing existing endpoint", async () => {
// Spawn one browser instance and spawn another one connecting to the first one
const a = await launch();
const b = await launch({ wsEndpoint: a.wsEndpoint() });
const b = await connect({ wsEndpoint: a.wsEndpoint() });

// Ensure closing existing endpoint properly clean resources
await b.newPage("http://example.com");
Expand Down

0 comments on commit 4189e3e

Please sign in to comment.