Skip to content

Commit 77f18af

Browse files
committed
feat: add puppeteer.acquire function
1 parent 7a996a2 commit 77f18af

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

packages/puppeteer-core/src/cloudflare/PuppeteerWorkers.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export interface WorkersLaunchOptions {
8585
export class PuppeteerWorkers extends Puppeteer {
8686
public constructor() {
8787
super({isPuppeteerCore: false});
88+
this.acquire = this.acquire.bind(this);
8889
this.connect = this.connect.bind(this);
8990
this.launch = this.launch.bind(this);
9091
this.sessions = this.sessions.bind(this);
@@ -102,25 +103,7 @@ export class PuppeteerWorkers extends Puppeteer {
102103
endpoint: BrowserWorker,
103104
options?: WorkersLaunchOptions
104105
): Promise<Browser> {
105-
const searchParams = new URLSearchParams();
106-
if (options?.keep_alive) {
107-
searchParams.set('keep_alive', `${options.keep_alive}`);
108-
}
109-
if (options?.location) {
110-
searchParams.set('location', options.location);
111-
}
112-
113-
const acquireUrl = `${FAKE_HOST}/v1/acquire?${searchParams.toString()}`;
114-
const res = await endpoint.fetch(acquireUrl);
115-
const status = res.status;
116-
const text = await res.text();
117-
if (status !== 200) {
118-
throw new Error(
119-
`Unable to create new browser: code: ${status}: message: ${text}`
120-
);
121-
}
122-
// Got a 200, so response text is actually an AcquireResponse
123-
const response: AcquireResponse = JSON.parse(text);
106+
const response: AcquireResponse = await this.acquire(endpoint, options);
124107
return await this.connect(endpoint, response.sessionId);
125108
}
126109

@@ -222,4 +205,36 @@ export class PuppeteerWorkers extends Puppeteer {
222205
);
223206
}
224207
}
208+
209+
/**
210+
* Acquire a new browser session.
211+
*
212+
* @param borwserWorker - BrowserWorker
213+
* @returns a new browser session
214+
*/
215+
public async acquire(
216+
endpoint: BrowserWorker,
217+
options?: WorkersLaunchOptions
218+
): Promise<AcquireResponse> {
219+
const searchParams = new URLSearchParams();
220+
if (options?.keep_alive) {
221+
searchParams.set('keep_alive', `${options.keep_alive}`);
222+
}
223+
if (options?.location) {
224+
searchParams.set('location', options.location);
225+
}
226+
227+
const acquireUrl = `${FAKE_HOST}/v1/acquire?${searchParams.toString()}`;
228+
const res = await endpoint.fetch(acquireUrl);
229+
const status = res.status;
230+
const text = await res.text();
231+
if (status !== 200) {
232+
throw new Error(
233+
`Unable to create new browser: code: ${status}: message: ${text}`
234+
);
235+
}
236+
// Got a 200, so response text is actually an AcquireResponse
237+
const response: AcquireResponse = JSON.parse(text);
238+
return response;
239+
}
225240
}

packages/puppeteer-core/src/puppeteer-cloudflare.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export * from './cloudflare/BrowserWorker.js';
1212

1313
const puppeteer = new PuppeteerWorkers();
1414

15-
export const {connect, history, launch, limits, sessions} = puppeteer;
15+
export const {connect, history, launch, limits, sessions, acquire} = puppeteer;
1616

1717
export * from './cloudflare/PuppeteerWorkers.js';
1818

0 commit comments

Comments
 (0)