@boxlite-ai/boxlite 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,67 +1,79 @@
1
1
  /**
2
- * BrowserBox - Secure browser with remote debugging.
2
+ * BrowserBox - Secure browser with Playwright Server.
3
3
  *
4
4
  * Provides a minimal, elegant API for running isolated browsers that can be
5
- * controlled from outside using standard tools like Puppeteer or Playwright.
5
+ * controlled from outside using Playwright. Supports all browser types:
6
+ * chromium, firefox, and webkit.
6
7
  */
7
- import { SimpleBox, type SimpleBoxOptions } from './simplebox.js';
8
+ import { SimpleBox, type SimpleBoxOptions } from "./simplebox.js";
8
9
  /**
9
10
  * Browser type supported by BrowserBox.
10
11
  */
11
- export type BrowserType = 'chromium' | 'firefox' | 'webkit';
12
+ export type BrowserType = "chromium" | "firefox" | "webkit";
12
13
  /**
13
14
  * Options for creating a BrowserBox.
14
15
  */
15
- export interface BrowserBoxOptions extends Omit<SimpleBoxOptions, 'image' | 'cpus' | 'memoryMib'> {
16
+ export interface BrowserBoxOptions extends Omit<SimpleBoxOptions, "image" | "cpus" | "memoryMib"> {
16
17
  /** Browser type (default: 'chromium') */
17
18
  browser?: BrowserType;
18
19
  /** Memory in MiB (default: 2048) */
19
20
  memoryMib?: number;
20
21
  /** Number of CPU cores (default: 2) */
21
22
  cpus?: number;
23
+ /** Host port for Playwright Server WebSocket connection (default: 3000) */
24
+ port?: number;
25
+ /** Host port for CDP/Puppeteer connection (default: 9222) */
26
+ cdpPort?: number;
22
27
  }
23
28
  /**
24
- * Secure browser environment with remote debugging.
29
+ * Secure browser environment with Playwright Server.
25
30
  *
26
- * Auto-starts a browser with Chrome DevTools Protocol enabled.
27
- * Connect from outside using Puppeteer, Playwright, Selenium, or DevTools.
31
+ * Auto-starts a browser with Playwright Server enabled for remote control.
32
+ * Connect from outside using Playwright's `connect()` method.
28
33
  *
29
34
  * ## Usage
30
35
  *
31
36
  * ```typescript
32
- * const browser = new BrowserBox();
37
+ * import { BrowserBox } from '@boxlite-ai/boxlite';
38
+ * import { chromium } from 'playwright-core';
39
+ *
40
+ * const box = new BrowserBox({ browser: 'chromium' });
33
41
  * try {
34
- * await browser.start(); // Manually start browser
35
- * console.log(`Connect to: ${browser.endpoint()}`);
36
- * // Use Puppeteer/Playwright from your host to connect
37
- * await new Promise(resolve => setTimeout(resolve, 60000));
42
+ * const ws = await box.playwrightEndpoint();
43
+ * const browser = await chromium.connect(ws);
44
+ *
45
+ * const page = await browser.newPage();
46
+ * await page.goto('https://example.com');
47
+ * console.log(await page.title());
48
+ *
49
+ * await browser.close();
38
50
  * } finally {
39
- * await browser.stop();
51
+ * await box.stop();
40
52
  * }
41
53
  * ```
42
54
  *
43
- * ## Example with custom options
55
+ * ## All browsers supported
44
56
  *
45
57
  * ```typescript
46
- * const browser = new BrowserBox({
47
- * browser: 'firefox',
48
- * memoryMib: 4096,
49
- * cpus: 4
50
- * });
51
- * try {
52
- * await browser.start();
53
- * const endpoint = browser.endpoint();
54
- * // Connect using Playwright or Puppeteer
55
- * } finally {
56
- * await browser.stop();
57
- * }
58
+ * // WebKit works!
59
+ * const box = new BrowserBox({ browser: 'webkit' });
60
+ * const ws = await box.playwrightEndpoint();
61
+ * const browser = await webkit.connect(ws);
58
62
  * ```
59
63
  */
60
64
  export declare class BrowserBox extends SimpleBox {
65
+ /** Playwright Docker image with all browsers pre-installed */
61
66
  private static readonly DEFAULT_IMAGE;
62
- private static readonly PORTS;
67
+ /** Playwright version - must match the Docker image */
68
+ private static readonly PLAYWRIGHT_VERSION;
69
+ /** Default port for Playwright Server */
70
+ private static readonly DEFAULT_PORT;
63
71
  private readonly _browser;
64
- private readonly _port;
72
+ private readonly _guestPort;
73
+ private readonly _hostPort;
74
+ private readonly _cdpGuestPort;
75
+ private _playwrightStarted;
76
+ private _cdpStarted;
65
77
  /**
66
78
  * Create a new BrowserBox.
67
79
  *
@@ -70,7 +82,7 @@ export declare class BrowserBox extends SimpleBox {
70
82
  * @example
71
83
  * ```typescript
72
84
  * const browser = new BrowserBox({
73
- * browser: 'chromium',
85
+ * browser: 'webkit', // All browsers work!
74
86
  * memoryMib: 2048,
75
87
  * cpus: 2
76
88
  * });
@@ -78,68 +90,120 @@ export declare class BrowserBox extends SimpleBox {
78
90
  */
79
91
  constructor(options?: BrowserBoxOptions);
80
92
  /**
81
- * Start the browser with remote debugging enabled.
93
+ * Start the Playwright Server with the configured browser.
82
94
  *
83
- * Call this method after creating the BrowserBox to start the browser process.
84
- * Waits for the browser to be ready before returning.
95
+ * The Playwright Server binds to 0.0.0.0, so no proxy is needed.
96
+ * It handles all browser types natively.
85
97
  *
86
- * @param timeout - Maximum time to wait for browser to start in seconds (default: 30)
87
- * @throws {TimeoutError} If browser doesn't start within timeout
98
+ * @param timeout - Maximum time to wait for server to start in seconds (default: 60)
99
+ * @throws {TimeoutError} If server doesn't start within timeout
100
+ */
101
+ start(timeout?: number): Promise<void>;
102
+ /** Start Playwright Server (works for ALL browsers). */
103
+ private _startPlaywrightServer;
104
+ /** Wait for Playwright Server to be ready. */
105
+ private _waitForPlaywrightServer;
106
+ /** Start browser with remote debugging for Puppeteer (CDP for Chromium, WebDriver BiDi for Firefox). */
107
+ private _startCdpBrowser;
108
+ /** Start Chromium with CDP remote debugging. */
109
+ private _startChromiumCdp;
110
+ /** Start Firefox with WebDriver BiDi remote debugging. */
111
+ private _startFirefoxBiDi;
112
+ /** Wait for Firefox WebDriver BiDi server to be ready. */
113
+ private _waitForBiDiServer;
114
+ /** Start Python TCP forwarder to route traffic through the working port 3000. */
115
+ private _startCdpForwarder;
116
+ /** Wait for CDP server to be ready. */
117
+ private _waitForCdpServer;
118
+ /** Ensure Playwright server is started. */
119
+ private _ensurePlaywrightStarted;
120
+ /** Ensure CDP browser is started. */
121
+ private _ensureCdpStarted;
122
+ /**
123
+ * Get the WebSocket endpoint for Playwright connect().
124
+ *
125
+ * This is the primary method for Playwright connections.
126
+ * The returned URL can be used with Playwright's `connect()` method.
127
+ *
128
+ * @param timeout - Optional timeout to wait for server to start (starts automatically if not started)
129
+ * @returns WebSocket endpoint URL (e.g., 'ws://localhost:3000/')
88
130
  *
89
131
  * @example
90
132
  * ```typescript
91
- * const browser = new BrowserBox();
92
- * try {
93
- * await browser.start();
94
- * console.log(`Connect to: ${browser.endpoint()}`);
95
- * } finally {
96
- * await browser.stop();
97
- * }
133
+ * const box = new BrowserBox({ browser: 'chromium' });
134
+ * const ws = await box.playwrightEndpoint();
135
+ * const browser = await chromium.connect(ws);
98
136
  * ```
99
137
  */
100
- start(timeout?: number): Promise<void>;
138
+ playwrightEndpoint(timeout?: number): Promise<string>;
101
139
  /**
102
- * Wait for the browser process to be running.
103
- *
104
- * @param processPattern - Pattern to match browser process name
105
- * @param timeout - Maximum wait time in seconds
106
- * @throws {TimeoutError} If browser doesn't start within timeout
140
+ * @deprecated Use playwrightEndpoint() instead.
107
141
  */
108
- private waitForBrowser;
142
+ wsEndpoint(timeout?: number): Promise<string>;
109
143
  /**
110
- * Get the connection endpoint for remote debugging.
144
+ * Get the WebSocket endpoint for CDP/BiDi connections.
111
145
  *
112
- * Returns the HTTP endpoint URL for Chrome DevTools Protocol.
113
- * Use this with Puppeteer, Playwright, or Selenium to connect to the browser.
146
+ * This is the generic endpoint that works with Puppeteer, Selenium, or any
147
+ * other CDP/BiDi client. Works with chromium (CDP) and firefox (WebDriver BiDi).
148
+ * WebKit is not supported - use playwrightEndpoint() with Playwright instead.
114
149
  *
115
- * @returns HTTP endpoint URL (e.g., 'http://localhost:9222')
150
+ * @param timeout - Optional timeout to wait for browser to start
151
+ * @returns WebSocket endpoint URL
152
+ * @throws {BoxliteError} If browser type is webkit
116
153
  *
117
154
  * @example
118
155
  * ```typescript
119
- * const browser = new BrowserBox();
120
- * try {
121
- * await browser.start();
122
- * const url = browser.endpoint();
156
+ * // Chromium (CDP)
157
+ * const box = new BrowserBox({ browser: 'chromium' });
158
+ * const wsEndpoint = await box.endpoint();
159
+ * const browser = await puppeteer.connect({ browserWSEndpoint: wsEndpoint });
123
160
  *
124
- * // Use with Puppeteer:
125
- * // const browserWSEndpoint = await fetch(`${url}/json/version`)
126
- * // .then(r => r.json())
127
- * // .then(d => d.webSocketDebuggerUrl);
128
- * // const browser = await puppeteer.connect({ browserWSEndpoint });
161
+ * // Firefox (WebDriver BiDi)
162
+ * const box = new BrowserBox({ browser: 'firefox' });
163
+ * const wsEndpoint = await box.endpoint();
164
+ * const browser = await puppeteer.connect({
165
+ * browserWSEndpoint: wsEndpoint,
166
+ * protocol: 'webDriverBiDi'
167
+ * });
168
+ * // Note: Firefox headless has a limitation where newPage() hangs.
169
+ * // Use browser.pages()[0] instead of browser.newPage().
170
+ * const page = (await browser.pages())[0];
171
+ * ```
172
+ */
173
+ endpoint(timeout?: number): Promise<string>;
174
+ /**
175
+ * @deprecated Use endpoint() instead.
176
+ */
177
+ puppeteerEndpoint(timeout?: number): Promise<string>;
178
+ /**
179
+ * @deprecated Use endpoint() instead. This method only works with chromium.
180
+ */
181
+ cdpEndpoint(timeout?: number): Promise<string>;
182
+ /**
183
+ * Connect to the browser using Playwright.
184
+ *
185
+ * Convenience method that returns a connected Playwright Browser instance.
186
+ * Requires playwright-core to be installed.
129
187
  *
130
- * // Use with Playwright:
131
- * // const browser = await chromium.connectOverCDP(url);
132
- * } finally {
133
- * await browser.stop();
134
- * }
188
+ * @param options - Connection options
189
+ * @returns Connected Playwright Browser instance
190
+ *
191
+ * @example
192
+ * ```typescript
193
+ * const box = new BrowserBox({ browser: 'webkit' });
194
+ * const browser = await box.connect();
195
+ * const page = await browser.newPage();
196
+ * await page.goto('https://example.com');
135
197
  * ```
136
198
  */
137
- endpoint(): string;
199
+ connect(options?: {
200
+ timeout?: number;
201
+ }): Promise<unknown>;
138
202
  /**
139
- * Override async disposal to start browser automatically.
203
+ * Get the browser type.
140
204
  *
141
- * @internal
205
+ * @returns The browser type ('chromium', 'firefox', or 'webkit')
142
206
  */
143
- [Symbol.asyncDispose](): Promise<void>;
207
+ get browser(): BrowserType;
144
208
  }
145
209
  //# sourceMappingURL=browserbox.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"browserbox.d.ts","sourceRoot":"","sources":["../lib/browserbox.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAIlE;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,UAAU,GAAG,SAAS,GAAG,QAAQ,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAAC,gBAAgB,EAAE,OAAO,GAAG,MAAM,GAAG,WAAW,CAAC;IAC/F,yCAAyC;IACzC,OAAO,CAAC,EAAE,WAAW,CAAC;IAEtB,oCAAoC;IACpC,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,uCAAuC;IACvC,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,qBAAa,UAAW,SAAQ,SAAS;IACvC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAgD;IAErF,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAI3B;IAEF,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAc;IACvC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAE/B;;;;;;;;;;;;;OAaG;gBACS,OAAO,GAAE,iBAAsB;IAmB3C;;;;;;;;;;;;;;;;;;;OAmBG;IACG,KAAK,CAAC,OAAO,GAAE,MAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAkChD;;;;;;OAMG;YACW,cAAc;IAuB5B;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,QAAQ,IAAI,MAAM;IAIlB;;;;OAIG;IACG,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7C"}
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