@@ -85,6 +85,7 @@ export interface WorkersLaunchOptions {
85
85
export class PuppeteerWorkers extends Puppeteer {
86
86
public constructor ( ) {
87
87
super ( { isPuppeteerCore : false } ) ;
88
+ this . acquire = this . acquire . bind ( this ) ;
88
89
this . connect = this . connect . bind ( this ) ;
89
90
this . launch = this . launch . bind ( this ) ;
90
91
this . sessions = this . sessions . bind ( this ) ;
@@ -102,25 +103,7 @@ export class PuppeteerWorkers extends Puppeteer {
102
103
endpoint : BrowserWorker ,
103
104
options ?: WorkersLaunchOptions
104
105
) : 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 ) ;
124
107
return await this . connect ( endpoint , response . sessionId ) ;
125
108
}
126
109
@@ -222,4 +205,36 @@ export class PuppeteerWorkers extends Puppeteer {
222
205
) ;
223
206
}
224
207
}
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
+ }
225
240
}
0 commit comments