@crawlee/playwright 4.0.0-beta.33 → 4.0.0-beta.330
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.
- package/README.md +15 -11
- package/index.d.ts +4 -3
- package/index.js +2 -2
- package/internals/adaptive-playwright-crawler.d.ts +156 -82
- package/internals/adaptive-playwright-crawler.js +561 -300
- package/internals/enqueue-links/click-elements.d.ts +21 -58
- package/internals/enqueue-links/click-elements.js +48 -61
- package/internals/playwright-browser-pool.d.ts +55 -0
- package/internals/playwright-browser-pool.js +34 -0
- package/internals/playwright-crawler.d.ts +179 -171
- package/internals/playwright-crawler.js +121 -77
- package/internals/playwright-launcher.d.ts +30 -22
- package/internals/playwright-launcher.js +24 -18
- package/internals/utils/playwright-utils.d.ts +66 -54
- package/internals/utils/playwright-utils.js +128 -137
- package/internals/utils/rendering-type-prediction.d.ts +42 -13
- package/internals/utils/rendering-type-prediction.js +83 -50
- package/package.json +20 -23
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/internals/adaptive-playwright-crawler.d.ts.map +0 -1
- package/internals/adaptive-playwright-crawler.js.map +0 -1
- package/internals/enqueue-links/click-elements.d.ts.map +0 -1
- package/internals/enqueue-links/click-elements.js.map +0 -1
- package/internals/playwright-crawler.d.ts.map +0 -1
- package/internals/playwright-crawler.js.map +0 -1
- package/internals/playwright-launcher.d.ts.map +0 -1
- package/internals/playwright-launcher.js.map +0 -1
- package/internals/utils/playwright-utils.d.ts.map +0 -1
- package/internals/utils/playwright-utils.js.map +0 -1
- package/internals/utils/rendering-type-prediction.d.ts.map +0 -1
- package/internals/utils/rendering-type-prediction.js.map +0 -1
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
2
|
+
import { BrowserCrawler, DownloadStartedError, RequestState, Router, serviceLocator } from '@crawlee/browser';
|
|
3
|
+
import { assertBrowserPoolNotConfigured, parseArgument, schemas } from '@crawlee/utils/internal';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { playwrightBrowserPool } from './playwright-browser-pool.js';
|
|
6
|
+
import { blockRequests, compileScript, enqueueLinksByClickingElements, gotoExtended, handleCloudflareChallenge, infiniteScroll, injectFile, injectJQuery, parseWithCheerio, saveSnapshot, } from './utils/playwright-utils.js';
|
|
5
7
|
/**
|
|
6
8
|
* Provides a simple framework for parallel crawling of web pages
|
|
7
9
|
* using headless Chromium, Firefox and Webkit browsers with [Playwright](https://github.com/microsoft/playwright).
|
|
@@ -13,24 +15,26 @@ import { gotoExtended, playwrightUtils } from './utils/playwright-utils.js';
|
|
|
13
15
|
* If the target website doesn't need JavaScript, consider using {@link CheerioCrawler},
|
|
14
16
|
* which downloads the pages using raw HTTP requests and is about 10x faster.
|
|
15
17
|
*
|
|
16
|
-
* The source URLs are represented using {@link Request} objects that are fed from
|
|
17
|
-
* {@link
|
|
18
|
-
*
|
|
18
|
+
* The source URLs are represented using {@link Request} objects that are fed from the
|
|
19
|
+
* {@link IRequestManager|request manager} provided via the {@link PlaywrightCrawlerOptions.requestManager|`requestManager`}
|
|
20
|
+
* constructor option (a {@link RequestQueue} is itself a request manager). To read from a read-only source such
|
|
21
|
+
* as a {@link RequestList} while still being able to enqueue new requests, combine it with a queue into a
|
|
22
|
+
* {@link RequestManagerTandem} via {@link IRequestLoader.toTandem|`requestLoader.toTandem()`} and pass the
|
|
23
|
+
* result as `requestManager`.
|
|
19
24
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* to {@link RequestQueue} before it starts their processing. This ensures that a single URL is not crawled multiple times.
|
|
25
|
+
* > The {@link PlaywrightCrawlerOptions.requestList|`requestList`} and {@link PlaywrightCrawlerOptions.requestQueue|`requestQueue`}
|
|
26
|
+
* > options are deprecated; they are still accepted and folded into a single `requestManager` for back-compat.
|
|
23
27
|
*
|
|
24
28
|
* The crawler finishes when there are no more {@link Request} objects to crawl.
|
|
25
29
|
*
|
|
26
30
|
* `PlaywrightCrawler` opens a new Chrome page (i.e. tab) for each {@link Request} object to crawl
|
|
27
31
|
* and then calls the function provided by user as the {@link PlaywrightCrawlerOptions.requestHandler} option.
|
|
28
32
|
*
|
|
29
|
-
* New pages are only opened when there is enough free CPU and memory available,
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* {@link
|
|
33
|
+
* New pages are only opened when there is enough free CPU and memory available, as judged by the crawler's
|
|
34
|
+
* {@link ConcurrencySystem}.
|
|
35
|
+
* Concurrency is tuned via the `minConcurrency`, `maxConcurrency` and `maxRequestsPerMinute` options of the
|
|
36
|
+
* `PlaywrightCrawler` constructor, or, for finer control, by injecting a pre-configured
|
|
37
|
+
* {@link ConcurrencySystem|`concurrencySystem`}.
|
|
34
38
|
*
|
|
35
39
|
* Note that the pool of Playwright instances is internally managed by the [BrowserPool](https://github.com/apify/browser-pool) class.
|
|
36
40
|
*
|
|
@@ -66,113 +70,153 @@ import { gotoExtended, playwrightUtils } from './utils/playwright-utils.js';
|
|
|
66
70
|
* @category Crawlers
|
|
67
71
|
*/
|
|
68
72
|
export class PlaywrightCrawler extends BrowserCrawler {
|
|
73
|
+
/**
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
69
76
|
static optionsShape = {
|
|
70
77
|
...BrowserCrawler.optionsShape,
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
ignoreIframes: ow.optional.boolean,
|
|
74
|
-
ignoreShadowRoots: ow.optional.boolean,
|
|
78
|
+
launchContext: schemas.anyObject.default(() => ({})),
|
|
79
|
+
headless: z.boolean().optional(),
|
|
75
80
|
};
|
|
81
|
+
/** @internal */
|
|
82
|
+
static optionsSchema = z.strictObject(PlaywrightCrawler.optionsShape);
|
|
76
83
|
/**
|
|
77
84
|
* All `PlaywrightCrawler` parameters are passed via an options object.
|
|
78
85
|
*/
|
|
79
86
|
constructor(options = {}) {
|
|
80
|
-
|
|
81
|
-
const { launchContext
|
|
82
|
-
const browserPoolOptions = {
|
|
83
|
-
...options.browserPoolOptions,
|
|
84
|
-
};
|
|
87
|
+
const parsedOptions = parseArgument(options, PlaywrightCrawler.optionsSchema, 'PlaywrightCrawlerOptions');
|
|
88
|
+
const { launchContext, headless, configuration, ...browserCrawlerOptions } = parsedOptions;
|
|
85
89
|
if (launchContext.proxyUrl) {
|
|
86
90
|
throw new Error('PlaywrightCrawlerOptions.launchContext.proxyUrl is not allowed in PlaywrightCrawler.' +
|
|
87
91
|
'Use PlaywrightCrawlerOptions.proxyConfiguration');
|
|
88
92
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
+
if (options.browserPool) {
|
|
94
|
+
// The raw options, not the parsed ones: `launchContext` has a default, so by now it is always set.
|
|
95
|
+
assertBrowserPoolNotConfigured(new.target.name, {
|
|
96
|
+
launchContext: options.launchContext,
|
|
97
|
+
headless: options.headless,
|
|
98
|
+
});
|
|
93
99
|
}
|
|
94
|
-
if (headless != null) {
|
|
95
|
-
launchContext.launchOptions ??= {};
|
|
96
|
-
launchContext.launchOptions.headless = headless;
|
|
97
|
-
}
|
|
98
|
-
const playwrightLauncher = new PlaywrightLauncher(launchContext, options.configuration);
|
|
99
|
-
browserPoolOptions.browserPlugins = [playwrightLauncher.createBrowserPlugin()];
|
|
100
100
|
super({
|
|
101
101
|
...browserCrawlerOptions,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
contextPipelineBuilder:
|
|
102
|
+
configuration,
|
|
103
|
+
browserPoolBuilder: (remoteBrowser) => playwrightBrowserPool({ launchContext, headless, configuration, remoteBrowser }),
|
|
104
|
+
contextPipelineBuilder: () => this.#buildContextPipeline(),
|
|
105
105
|
});
|
|
106
106
|
}
|
|
107
|
-
buildContextPipeline() {
|
|
108
|
-
return
|
|
107
|
+
#buildContextPipeline() {
|
|
108
|
+
return this.buildContextPipeline().compose(this.#enhanceContext.bind(this));
|
|
109
109
|
}
|
|
110
|
-
async
|
|
111
|
-
|
|
110
|
+
async navigationHandler(crawlingContext, gotoOptions) {
|
|
111
|
+
const { page } = crawlingContext;
|
|
112
|
+
const { promise: downloadStarted, resolve } = Promise.withResolvers();
|
|
113
|
+
const onDownload = () => resolve();
|
|
114
|
+
page.once('download', onDownload);
|
|
115
|
+
try {
|
|
116
|
+
return await gotoExtended(page, crawlingContext.request, gotoOptions);
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
// `goto` can reject before the `download` event arrives, and `listDownloads()` would miss it.
|
|
120
|
+
// ponytail: fixed 5 s bound, tie it to the navigation window if downloads start slower than that
|
|
121
|
+
if (error.message?.includes('Download is starting')) {
|
|
122
|
+
await Promise.race([downloadStarted, delay(5_000, undefined, { ref: false })]);
|
|
123
|
+
throw new DownloadStartedError(error.message, { cause: error });
|
|
124
|
+
}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
page.off('download', onDownload);
|
|
129
|
+
}
|
|
112
130
|
}
|
|
113
|
-
async enhanceContext(context) {
|
|
131
|
+
async #enhanceContext(context) {
|
|
114
132
|
const waitForSelector = async (selector, timeoutMs = 5_000) => {
|
|
115
133
|
const locator = context.page.locator(selector).first();
|
|
116
134
|
await locator.waitFor({ timeout: timeoutMs, state: 'attached' });
|
|
117
135
|
};
|
|
136
|
+
const parseWithCheerioFn = async (selector, timeoutMs = 5_000) => {
|
|
137
|
+
if (selector) {
|
|
138
|
+
await waitForSelector(selector, timeoutMs);
|
|
139
|
+
}
|
|
140
|
+
return parseWithCheerio(context.page, this.ignoreShadowRoots, this.ignoreIframes);
|
|
141
|
+
};
|
|
142
|
+
const querySelectorAll = async (selector, timeoutMs) => {
|
|
143
|
+
const matches = (await parseWithCheerioFn(selector, timeoutMs))(selector);
|
|
144
|
+
// The browser located it but the Cheerio snapshot does not have it - a Playwright-only selector,
|
|
145
|
+
// or an element under a shadow root/iframe the snapshot was told to ignore.
|
|
146
|
+
if (matches.length === 0) {
|
|
147
|
+
throw new Error(`Selector '${selector}' not found in the page snapshot.`);
|
|
148
|
+
}
|
|
149
|
+
return matches;
|
|
150
|
+
};
|
|
151
|
+
const downloads = [];
|
|
152
|
+
context.page.on('download', (download) => downloads.push(download));
|
|
118
153
|
return {
|
|
119
|
-
injectFile: async (filePath, options) =>
|
|
154
|
+
injectFile: async (filePath, options) => injectFile(context.page, filePath, options),
|
|
120
155
|
injectJQuery: async () => {
|
|
121
156
|
if (context.request.state === RequestState.BEFORE_NAV) {
|
|
122
157
|
context.log.warning('Using injectJQuery() in preNavigationHooks leads to unstable results. Use it in a postNavigationHook or a requestHandler instead.');
|
|
123
|
-
await
|
|
158
|
+
await injectJQuery(context.page);
|
|
124
159
|
return;
|
|
125
160
|
}
|
|
126
|
-
await
|
|
161
|
+
await injectJQuery(context.page, { surviveNavigations: false });
|
|
127
162
|
},
|
|
128
|
-
blockRequests: async (options) =>
|
|
163
|
+
blockRequests: async (options) => blockRequests(context.page, options),
|
|
129
164
|
waitForSelector,
|
|
130
|
-
parseWithCheerio:
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
165
|
+
parseWithCheerio: parseWithCheerioFn,
|
|
166
|
+
querySelectorAll,
|
|
167
|
+
querySelectorOne: async (selector, timeoutMs) => (await querySelectorAll(selector, timeoutMs)).first(),
|
|
168
|
+
infiniteScroll: async (options) => infiniteScroll(context.page, options),
|
|
169
|
+
listDownloads: async () => downloads,
|
|
170
|
+
saveSnapshot: async (options) => saveSnapshot(context.page, {
|
|
171
|
+
...options,
|
|
172
|
+
configuration: serviceLocator.getConfiguration(),
|
|
173
|
+
}),
|
|
174
|
+
enqueueLinksByClickingElements: async (options) => enqueueLinksByClickingElements({
|
|
139
175
|
...options,
|
|
140
176
|
page: context.page,
|
|
141
|
-
|
|
177
|
+
requestManager: this.requestManager,
|
|
142
178
|
}),
|
|
143
|
-
compileScript: (scriptString, ctx) =>
|
|
144
|
-
closeCookieModals: async () => playwrightUtils.closeCookieModals(context.page),
|
|
179
|
+
compileScript: (scriptString, ctx) => compileScript(scriptString, ctx),
|
|
145
180
|
handleCloudflareChallenge: async (options) => {
|
|
146
|
-
|
|
181
|
+
let response;
|
|
182
|
+
try {
|
|
183
|
+
response = context.response;
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
// the getter throws before navigation, with `skipNavigation`, and when navigation produced no response
|
|
187
|
+
}
|
|
188
|
+
return handleCloudflareChallenge(context.page, context.request.url, options, response);
|
|
147
189
|
},
|
|
148
190
|
};
|
|
149
191
|
}
|
|
150
192
|
}
|
|
151
193
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* Defaults to the {@link PlaywrightCrawlingContext}.
|
|
194
|
+
* Returns a `postNavigationHooks`-ready hook that runs {@link PlaywrightContextUtils.handleCloudflareChallenge}
|
|
195
|
+
* and propagates the post-challenge {@link Response} back into the crawling context via its return value.
|
|
155
196
|
*
|
|
156
|
-
*
|
|
197
|
+
* Cloudflare serves its challenge pages with a 403 status, which is part of the default `blockedStatusCodes`.
|
|
198
|
+
* Keep it there - the hook runs before the blocked-status check and replaces the response on a solved
|
|
199
|
+
* challenge, so solved requests pass it, while unsolved or undetected challenges keep the 403 and get
|
|
200
|
+
* retried with a fresh session.
|
|
157
201
|
*
|
|
202
|
+
* **Example usage**
|
|
158
203
|
* ```ts
|
|
159
|
-
* import { PlaywrightCrawler,
|
|
160
|
-
*
|
|
161
|
-
* const router = createPlaywrightRouter();
|
|
162
|
-
* router.addHandler('label-a', async (ctx) => {
|
|
163
|
-
* ctx.log.info('...');
|
|
164
|
-
* });
|
|
165
|
-
* router.addDefaultHandler(async (ctx) => {
|
|
166
|
-
* ctx.log.info('...');
|
|
167
|
-
* });
|
|
204
|
+
* import { PlaywrightCrawler, handleCloudflareChallengeHook } from 'crawlee';
|
|
168
205
|
*
|
|
169
206
|
* const crawler = new PlaywrightCrawler({
|
|
170
|
-
*
|
|
207
|
+
* postNavigationHooks: [handleCloudflareChallengeHook()],
|
|
171
208
|
* });
|
|
172
|
-
* await crawler.run();
|
|
173
209
|
* ```
|
|
174
210
|
*/
|
|
175
|
-
export function
|
|
176
|
-
return
|
|
211
|
+
export function handleCloudflareChallengeHook(options) {
|
|
212
|
+
return async (context) => {
|
|
213
|
+
const response = await context.handleCloudflareChallenge(options);
|
|
214
|
+
if (response !== undefined) {
|
|
215
|
+
return { response };
|
|
216
|
+
}
|
|
217
|
+
return undefined;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
export function createPlaywrightRouter(routesOrSchemas) {
|
|
221
|
+
return Router.create(routesOrSchemas);
|
|
177
222
|
}
|
|
178
|
-
//# sourceMappingURL=playwright-crawler.js.map
|
|
@@ -3,6 +3,7 @@ import { BrowserLauncher, Configuration } from '@crawlee/browser';
|
|
|
3
3
|
import { PlaywrightPlugin } from '@crawlee/browser-pool';
|
|
4
4
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
5
5
|
import type { Browser, BrowserType, LaunchOptions } from 'playwright';
|
|
6
|
+
import { z } from 'zod';
|
|
6
7
|
/**
|
|
7
8
|
* Apify extends the launch options of Playwright.
|
|
8
9
|
* You can use any of the Playwright compatible
|
|
@@ -70,33 +71,41 @@ export interface PlaywrightLaunchContext extends BrowserLaunchContext<LaunchOpti
|
|
|
70
71
|
* @ignore
|
|
71
72
|
*/
|
|
72
73
|
export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin> {
|
|
73
|
-
readonly
|
|
74
|
+
readonly configuration: Configuration;
|
|
75
|
+
/**
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
74
78
|
protected static optionsShape: {
|
|
79
|
+
proxyUrl: z.ZodOptional<z.ZodURL>;
|
|
80
|
+
useChrome: z.ZodOptional<z.ZodBoolean>;
|
|
81
|
+
useIncognitoPages: z.ZodOptional<z.ZodBoolean>;
|
|
82
|
+
browserPerProxy: z.ZodOptional<z.ZodBoolean>;
|
|
83
|
+
ignoreProxyCertificate: z.ZodOptional<z.ZodBoolean>;
|
|
84
|
+
userDataDir: z.ZodOptional<z.ZodString>;
|
|
75
85
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
launchContextOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
79
|
-
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
80
|
-
proxyUrl: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
|
|
81
|
-
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
82
|
-
useChrome: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
86
|
+
launchOptions: z.ZodOptional<z.ZodCustom<import("@crawlee/types").Dictionary, import("@crawlee/types").Dictionary>>;
|
|
87
|
+
userAgent: z.ZodOptional<z.ZodString>;
|
|
83
88
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
launcher: z.ZodOptional<z.ZodCustom<import("@crawlee/types").Dictionary, import("@crawlee/types").Dictionary>>;
|
|
90
|
+
};
|
|
91
|
+
/** @internal */
|
|
92
|
+
protected static optionsSchema: z.ZodObject<{
|
|
93
|
+
proxyUrl: z.ZodOptional<z.ZodURL>;
|
|
94
|
+
useChrome: z.ZodOptional<z.ZodBoolean>;
|
|
95
|
+
useIncognitoPages: z.ZodOptional<z.ZodBoolean>;
|
|
96
|
+
browserPerProxy: z.ZodOptional<z.ZodBoolean>;
|
|
97
|
+
ignoreProxyCertificate: z.ZodOptional<z.ZodBoolean>;
|
|
98
|
+
userDataDir: z.ZodOptional<z.ZodString>;
|
|
91
99
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
92
|
-
launchOptions: import("
|
|
100
|
+
launchOptions: z.ZodOptional<z.ZodCustom<import("@crawlee/types").Dictionary, import("@crawlee/types").Dictionary>>;
|
|
101
|
+
userAgent: z.ZodOptional<z.ZodString>;
|
|
93
102
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
94
|
-
|
|
95
|
-
}
|
|
103
|
+
launcher: z.ZodOptional<z.ZodCustom<import("@crawlee/types").Dictionary, import("@crawlee/types").Dictionary>>;
|
|
104
|
+
}, z.core.$strict>;
|
|
96
105
|
/**
|
|
97
106
|
* All `PlaywrightLauncher` parameters are passed via this launchContext object.
|
|
98
107
|
*/
|
|
99
|
-
constructor(launchContext?: PlaywrightLaunchContext,
|
|
108
|
+
constructor(launchContext?: PlaywrightLaunchContext, configuration?: Configuration);
|
|
100
109
|
}
|
|
101
110
|
/**
|
|
102
111
|
* Launches headless browsers using Playwright pre-configured to work within the Apify platform.
|
|
@@ -127,9 +136,8 @@ export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin
|
|
|
127
136
|
* Optional settings passed to `browserType.launch()`. In addition to
|
|
128
137
|
* [Playwright's options](https://playwright.dev/docs/api/class-browsertype?_highlight=launch#browsertypelaunchoptions)
|
|
129
138
|
* the object may contain our own {@link PlaywrightLaunchContext} that enable additional features.
|
|
130
|
-
* @param [
|
|
139
|
+
* @param [configuration]
|
|
131
140
|
* @returns
|
|
132
141
|
* Promise that resolves to Playwright's `Browser` instance.
|
|
133
142
|
*/
|
|
134
|
-
export declare function launchPlaywright(launchContext?: PlaywrightLaunchContext,
|
|
135
|
-
//# sourceMappingURL=playwright-launcher.d.ts.map
|
|
143
|
+
export declare function launchPlaywright(launchContext?: PlaywrightLaunchContext, configuration?: Configuration): Promise<Browser>;
|
|
@@ -1,34 +1,41 @@
|
|
|
1
1
|
import { BrowserLauncher, Configuration } from '@crawlee/browser';
|
|
2
|
-
import { PlaywrightPlugin } from '@crawlee/browser-pool';
|
|
3
|
-
import
|
|
2
|
+
import { PlaywrightPlugin, RemotePlaywrightPlugin } from '@crawlee/browser-pool';
|
|
3
|
+
import { parseArgument, schemas } from '@crawlee/utils/internal';
|
|
4
|
+
import { z } from 'zod';
|
|
4
5
|
/**
|
|
5
6
|
* `PlaywrightLauncher` is based on the `BrowserLauncher`. It launches `playwright` browser instance.
|
|
6
7
|
* @ignore
|
|
7
8
|
*/
|
|
8
9
|
export class PlaywrightLauncher extends BrowserLauncher {
|
|
9
|
-
|
|
10
|
+
configuration;
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
10
14
|
static optionsShape = {
|
|
11
15
|
...BrowserLauncher.optionsShape,
|
|
12
|
-
launcher
|
|
13
|
-
|
|
16
|
+
// Passthrough schema — the launcher module object must keep its prototype through parsing.
|
|
17
|
+
launcher: schemas.anyObject.optional(),
|
|
14
18
|
};
|
|
19
|
+
/** @internal */
|
|
20
|
+
static optionsSchema = z.strictObject(PlaywrightLauncher.optionsShape);
|
|
15
21
|
/**
|
|
16
22
|
* All `PlaywrightLauncher` parameters are passed via this launchContext object.
|
|
17
23
|
*/
|
|
18
|
-
constructor(launchContext = {},
|
|
19
|
-
|
|
20
|
-
const { launcher = BrowserLauncher.requireLauncherOrThrow('playwright', 'apify/actor-node-playwright-*').chromium, } =
|
|
21
|
-
const { launchOptions = {}, ...rest } =
|
|
24
|
+
constructor(launchContext = {}, configuration = Configuration.getGlobalConfiguration()) {
|
|
25
|
+
const parsedContext = parseArgument(launchContext, PlaywrightLauncher.optionsSchema, 'PlaywrightLaunchContext');
|
|
26
|
+
const { launcher = BrowserLauncher.requireLauncherOrThrow('playwright', 'apify/actor-node-playwright-*').chromium, } = parsedContext;
|
|
27
|
+
const { launchOptions = {}, ...rest } = parsedContext;
|
|
22
28
|
super({
|
|
23
29
|
...rest,
|
|
24
30
|
launchOptions: {
|
|
25
31
|
...launchOptions,
|
|
26
|
-
executablePath: getDefaultExecutablePath(
|
|
32
|
+
executablePath: getDefaultExecutablePath(parsedContext, configuration),
|
|
27
33
|
},
|
|
28
34
|
launcher,
|
|
29
|
-
},
|
|
30
|
-
this.
|
|
35
|
+
}, configuration);
|
|
36
|
+
this.configuration = configuration;
|
|
31
37
|
this.Plugin = PlaywrightPlugin;
|
|
38
|
+
this.RemotePlugin = RemotePlaywrightPlugin;
|
|
32
39
|
}
|
|
33
40
|
}
|
|
34
41
|
/**
|
|
@@ -36,8 +43,8 @@ export class PlaywrightLauncher extends BrowserLauncher {
|
|
|
36
43
|
* @returns default path to browser.
|
|
37
44
|
* @ignore
|
|
38
45
|
*/
|
|
39
|
-
function getDefaultExecutablePath(launchContext,
|
|
40
|
-
const pathFromPlaywrightImage =
|
|
46
|
+
function getDefaultExecutablePath(launchContext, configuration) {
|
|
47
|
+
const pathFromPlaywrightImage = configuration.defaultBrowserPath;
|
|
41
48
|
const { launchOptions = {} } = launchContext;
|
|
42
49
|
if (launchOptions.executablePath) {
|
|
43
50
|
return launchOptions.executablePath;
|
|
@@ -79,12 +86,11 @@ function getDefaultExecutablePath(launchContext, config) {
|
|
|
79
86
|
* Optional settings passed to `browserType.launch()`. In addition to
|
|
80
87
|
* [Playwright's options](https://playwright.dev/docs/api/class-browsertype?_highlight=launch#browsertypelaunchoptions)
|
|
81
88
|
* the object may contain our own {@link PlaywrightLaunchContext} that enable additional features.
|
|
82
|
-
* @param [
|
|
89
|
+
* @param [configuration]
|
|
83
90
|
* @returns
|
|
84
91
|
* Promise that resolves to Playwright's `Browser` instance.
|
|
85
92
|
*/
|
|
86
|
-
export async function launchPlaywright(launchContext,
|
|
87
|
-
const playwrightLauncher = new PlaywrightLauncher(launchContext,
|
|
93
|
+
export async function launchPlaywright(launchContext, configuration = Configuration.getGlobalConfiguration()) {
|
|
94
|
+
const playwrightLauncher = new PlaywrightLauncher(launchContext, configuration);
|
|
88
95
|
return playwrightLauncher.launch();
|
|
89
96
|
}
|
|
90
|
-
//# sourceMappingURL=playwright-launcher.js.map
|