@crawlee/playwright 4.0.0-beta.99 → 4.0.0-rc.1

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 CHANGED
@@ -34,7 +34,7 @@ Crawlee is available as the [`crawlee`](https://www.npmjs.com/package/crawlee) N
34
34
 
35
35
  We recommend visiting the [Introduction tutorial](https://crawlee.dev/js/docs/introduction) in Crawlee documentation for more information.
36
36
 
37
- > Crawlee requires **Node.js 16 or higher**.
37
+ > Crawlee requires **Node.js 22.13 or higher**.
38
38
 
39
39
  ### With Crawlee CLI
40
40
 
package/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export * from '@crawlee/browser';
2
+ export * from './internals/playwright-browser-pool.js';
2
3
  export * from './internals/playwright-crawler.js';
3
- export * from './internals/playwright-launcher.js';
4
+ export { launchPlaywright } from './internals/playwright-launcher.js';
5
+ export type { PlaywrightLaunchContext } from './internals/playwright-launcher.js';
4
6
  export * from './internals/adaptive-playwright-crawler.js';
5
7
  export { RenderingTypePredictor } from './internals/utils/rendering-type-prediction.js';
6
8
  export * as playwrightUtils from './internals/utils/playwright-utils.js';
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from '@crawlee/browser';
2
+ export * from './internals/playwright-browser-pool.js';
2
3
  export * from './internals/playwright-crawler.js';
3
- export * from './internals/playwright-launcher.js';
4
+ export { launchPlaywright } from './internals/playwright-launcher.js';
4
5
  export * from './internals/adaptive-playwright-crawler.js';
5
6
  export { RenderingTypePredictor } from './internals/utils/rendering-type-prediction.js';
6
7
  export * as playwrightUtils from './internals/utils/playwright-utils.js';
@@ -1,32 +1,40 @@
1
- import type { BrowserHook, LoadedRequest, Request, RouterHandler, RouteSchemas, RoutesFromSchemas } from '@crawlee/browser';
2
- import type { BasicCrawlerOptions } from '@crawlee/basic';
1
+ import type { BrowserHook, LoadedRequest, CrawlingRequest, RouterHandler, RouteSchemas, RoutesFromSchemas } from '@crawlee/browser';
2
+ import type { BasicCrawlerOptions, CrawlingContext, EnqueueLinksOptions, GetUserDataFromRequest, RouterRoutes } from '@crawlee/basic';
3
3
  import { BasicCrawler } from '@crawlee/basic';
4
- import type { ContextPipeline, CrawlingContext, EnqueueLinksOptions, GetUserDataFromRequest, RouterRoutes, StatisticsOptions, StatisticState } from '@crawlee/core';
5
- import { RequestHandlerResult, Statistics } from '@crawlee/core';
4
+ import type { StorageTransactionView } from '@crawlee/core';
6
5
  import type { Dictionary, Awaitable } from '@crawlee/types';
7
- import { type CheerioRoot } from '@crawlee/utils';
8
- import { type Cheerio } from 'cheerio';
6
+ import { type Cheerio, type CheerioAPI } from 'cheerio';
9
7
  import type { AnyNode } from 'domhandler';
10
8
  // @ts-ignore optional peer dependency or compatibility with es2022
11
9
  import type { Page } from 'playwright';
12
- import type { PlaywrightCrawlingContext, PlaywrightGotoOptions } from './playwright-crawler.js';
10
+ import { z } from 'zod';
11
+ import type { PlaywrightCrawlerOptions, PlaywrightCrawlingContext, PlaywrightGotoOptions } from './playwright-crawler.js';
13
12
  import { type IRenderingTypePredictor } from './utils/rendering-type-prediction.js';
14
- interface AdaptivePlaywrightCrawlerStatisticState extends StatisticState {
15
- httpOnlyRequestHandlerRuns?: number;
16
- browserRequestHandlerRuns?: number;
17
- renderingTypeMispredictions?: number;
18
- }
19
- declare class AdaptivePlaywrightCrawlerStatistics extends Statistics {
20
- state: AdaptivePlaywrightCrawlerStatisticState;
21
- constructor(options?: StatisticsOptions);
22
- reset(): void;
23
- protected maybeLoadStatistics(): Promise<void>;
24
- trackHttpOnlyRequestHandlerRun(): void;
25
- trackBrowserRequestHandlerRun(): void;
26
- trackRenderingTypeMisprediction(): void;
27
- }
28
- export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary = Dictionary> extends CrawlingContext<UserData> {
29
- request: LoadedRequest<Request<UserData>>;
13
+ declare const adaptiveStatisticStateSchema: z.ZodObject<{
14
+ httpOnlyRequestHandlerRuns: z.ZodDefault<z.ZodNumber>;
15
+ browserRequestHandlerRuns: z.ZodDefault<z.ZodNumber>;
16
+ renderingTypeMispredictions: z.ZodDefault<z.ZodNumber>;
17
+ }, z.core.$strip>;
18
+ /**
19
+ * The extra statistics fields {@link AdaptivePlaywrightCrawler} tracks on top of the built-in
20
+ * {@link StatisticState} ones. They are available on `crawler.statistics.state` and are persisted with the rest of
21
+ * the statistics.
22
+ */
23
+ export type AdaptivePlaywrightCrawlerStatisticState = z.infer<typeof adaptiveStatisticStateSchema>;
24
+ /**
25
+ * The {@link AdaptivePlaywrightCrawlerStatisticState} fields as a {@link Statistics} state extension, defaults
26
+ * and all. A {@link Statistics} instance to be injected into an {@link AdaptivePlaywrightCrawler} has to carry
27
+ * them - `deserialize.extend()` your own fields onto this one and pass the result as `stateExtension`.
28
+ */
29
+ export declare const adaptivePlaywrightCrawlerStatisticState: {
30
+ deserialize: z.ZodObject<{
31
+ httpOnlyRequestHandlerRuns: z.ZodDefault<z.ZodNumber>;
32
+ browserRequestHandlerRuns: z.ZodDefault<z.ZodNumber>;
33
+ renderingTypeMispredictions: z.ZodDefault<z.ZodNumber>;
34
+ }, z.core.$strip>;
35
+ };
36
+ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary = any> extends CrawlingContext<UserData> {
37
+ request: LoadedRequest<CrawlingRequest<UserData>>;
30
38
  /**
31
39
  * The HTTP response, either from the HTTP client or from the initial request from playwright's navigation.
32
40
  */
@@ -71,19 +79,19 @@ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary =
71
79
  * });
72
80
  * ```
73
81
  */
74
- parseWithCheerio(selector?: string, timeoutMs?: number): Promise<CheerioRoot>;
82
+ parseWithCheerio(selector?: string, timeoutMs?: number): Promise<CheerioAPI>;
75
83
  enqueueLinks(options?: EnqueueLinksOptions): Promise<unknown>;
76
84
  }
77
85
  interface AdaptiveHookContext extends Pick<AdaptivePlaywrightCrawlerContext, 'id' | 'session' | 'proxyInfo' | 'log'> {
78
86
  page?: Page;
79
- request: Request;
87
+ request: CrawlingRequest;
80
88
  gotoOptions?: PlaywrightGotoOptions;
81
89
  }
82
90
  type AdaptiveHook<ContextExtension = Dictionary<never>> = BrowserHook<AdaptiveHookContext, ContextExtension>;
83
91
  type AdaptivePostNavigationHook<ContextExtension = Dictionary<never>> = BrowserHook<Omit<AdaptiveHookContext, 'request'> & {
84
- request: LoadedRequest<Request>;
92
+ request: LoadedRequest<CrawlingRequest>;
85
93
  }, ContextExtension>;
86
- export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<AdaptivePlaywrightCrawlerContext['request']>>> extends Omit<BasicCrawlerOptions<AdaptivePlaywrightCrawlerContext, ContextExtension, ExtendedContext, Routes>, 'preNavigationHooks' | 'postNavigationHooks'> {
94
+ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<AdaptivePlaywrightCrawlerContext['request']>>, StatisticStateExtension extends AdaptivePlaywrightCrawlerStatisticState = AdaptivePlaywrightCrawlerStatisticState> extends Omit<BasicCrawlerOptions<AdaptivePlaywrightCrawlerContext, ContextExtension, ExtendedContext, Routes, StatisticStateExtension>, 'preNavigationHooks' | 'postNavigationHooks' | 'contextPipelineBuilder'>, Pick<PlaywrightCrawlerOptions, 'launchContext' | 'headless' | 'browserPool' | 'remoteBrowser'> {
87
95
  /**
88
96
  * Async functions that are sequentially evaluated before the navigation. Good for setting additional cookies.
89
97
  * The function accepts a subset of the crawling context. If you attempt to access the `page` property during HTTP-only crawling,
@@ -108,11 +116,12 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
108
116
  */
109
117
  renderingTypeDetectionRatio?: number;
110
118
  /**
111
- * An optional callback that is called on dataset items found by the request handler in plain HTTP mode.
119
+ * An optional callback that is called on the storage writes recorded by the request handler in plain
120
+ * HTTP mode (exposed as a read-only {@link StorageTransactionView}).
112
121
  * If it returns false, the request is retried in a browser.
113
- * If no callback is specified, every dataset item is considered valid.
122
+ * If no callback is specified, every result is considered valid.
114
123
  */
115
- resultChecker?: (result: RequestHandlerResult) => boolean;
124
+ resultChecker?: (result: StorageTransactionView) => boolean;
116
125
  /**
117
126
  * An optional callback that decides whether an error thrown during the plain HTTP request handler
118
127
  * should be propagated (instead of falling back to browser navigation).
@@ -135,18 +144,13 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
135
144
  *
136
145
  * For a stricter, ready-made comparator that also takes enqueued requests and key-value store changes into account, see {@link fullResultComparator}.
137
146
  */
138
- resultComparator?: (resultA: RequestHandlerResult, resultB: RequestHandlerResult) => boolean | 'equal' | 'different' | 'inconclusive';
147
+ resultComparator?: (resultA: StorageTransactionView, resultB: StorageTransactionView) => boolean | 'equal' | 'different' | 'inconclusive';
139
148
  /**
140
149
  * A custom rendering type predictor. A predictor passed here is borrowed - the crawler never drives its
141
150
  * lifecycle, so set it up yourself (the built-in {@link RenderingTypePredictor} needs `initialize()`).
142
151
  * Omit the option and the crawler builds its own from `renderingTypeDetectionRatio` - and initializes it.
143
152
  */
144
153
  renderingTypePredictor?: IRenderingTypePredictor;
145
- /**
146
- * Prevent direct access to storage in request handlers (only allow using context helpers).
147
- * Defaults to `true`
148
- */
149
- preventDirectStorageAccess?: boolean;
150
154
  }
151
155
  /**
152
156
  * An extension of {@link PlaywrightCrawler} that uses a more limited request handler interface so that it is able to switch to HTTP-only crawling when it detects it may be possible.
@@ -177,45 +181,31 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
177
181
  *
178
182
  * @experimental
179
183
  */
180
- export declare class AdaptivePlaywrightCrawler<ContextExtension = Dictionary<never>, ExtendedContext extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<AdaptivePlaywrightCrawlerContext['request']>>> extends BasicCrawler<AdaptivePlaywrightCrawlerContext, ContextExtension, ExtendedContext, Routes> {
181
- private renderingTypePredictor;
182
- private resultChecker;
183
- private shouldPropagateError;
184
- private resultComparator;
185
- private preventDirectStorageAccess;
186
- private staticContextPipeline;
187
- private browserContextPipeline;
188
- private individualRequestHandlerTimeoutMillis;
189
- readonly stats: AdaptivePlaywrightCrawlerStatistics;
190
- private resultObjects;
191
- private inFlightRenderingTypeDetections;
192
- private teardownHooks;
193
- constructor(options?: AdaptivePlaywrightCrawlerOptions<ContextExtension, ExtendedContext, Routes>);
194
- protected _init(): Promise<void>;
195
- protected buildContextPipeline(): ContextPipeline<CrawlingContext<Dictionary>, CrawlingContext<Dictionary> & {
196
- readonly request: LoadedRequest<Request<Dictionary>>;
197
- readonly response: Response;
198
- readonly page: Page;
199
- readonly querySelector: AdaptivePlaywrightCrawlerContext["querySelector"];
200
- readonly querySelectorAll: AdaptivePlaywrightCrawlerContext["querySelectorAll"];
201
- readonly waitForSelector: AdaptivePlaywrightCrawlerContext["waitForSelector"];
202
- readonly parseWithCheerio: AdaptivePlaywrightCrawlerContext["parseWithCheerio"];
203
- }>;
204
- private adaptCheerioContext;
205
- private adaptPlaywrightContext;
206
- private crawlOne;
184
+ export declare class AdaptivePlaywrightCrawler<ContextExtension = Dictionary<never>, ExtendedContext extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<AdaptivePlaywrightCrawlerContext['request']>>, StatisticStateExtension extends AdaptivePlaywrightCrawlerStatisticState = AdaptivePlaywrightCrawlerStatisticState> extends BasicCrawler<AdaptivePlaywrightCrawlerContext, ContextExtension, ExtendedContext, Routes, StatisticStateExtension> {
185
+ #private;
186
+ constructor(options?: AdaptivePlaywrightCrawlerOptions<ContextExtension, ExtendedContext, Routes, StatisticStateExtension>);
187
+ protected init(): Promise<void>;
207
188
  protected runRequestHandler(crawlingContext: CrawlingContext): Promise<void>;
208
- private commitResult;
209
- private allowStorageAccess;
210
189
  /**
211
- * Reading the pending request count queries the underlying request manager, which counts as storage access.
212
- * Since this is internal crawler bookkeeping used to compute the `enqueueLinks` limit (not user-initiated storage
213
- * access), it must be allowed even while a request handler runs inside the storage-access guard.
190
+ * Number of rendering type detections that have not settled yet, including results the predictor is
191
+ * still persisting.
192
+ */
193
+ get inFlightRenderingTypeDetectionCount(): number;
194
+ /**
195
+ * Waits for in-flight rendering type detections to settle, bounded by `timeoutMillis` (defaults to the
196
+ * internal timeout).
197
+ */
198
+ drainRenderingDetections({ timeoutMillis }?: {
199
+ timeoutMillis?: number;
200
+ }): Promise<void>;
201
+ /**
202
+ * Stops the crawler immediately, but not before rendering type detections already under way (and results
203
+ * the predictor is still persisting) have settled - see
204
+ * {@link AdaptivePlaywrightCrawler.drainRenderingDetections|`drainRenderingDetections()`}. Requests
205
+ * that are still running are not waited for, unlike {@link BasicCrawler.stop|`stop()`}.
214
206
  */
215
- protected getPendingRequestCountApproximation(): Promise<number>;
216
- private enqueueLinks;
217
- private createLogProxy;
218
207
  teardown(): Promise<void>;
208
+ destroy(): Promise<void>;
219
209
  }
220
210
  export declare function createAdaptivePlaywrightRouter<Context extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<Context['request']>>>(routes?: RouterRoutes<Context, Routes>): RouterHandler<Context, Routes>;
221
211
  export declare function createAdaptivePlaywrightRouter<Context extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext, UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(routes?: RouterRoutes<Context, Record<string, UserData>>): RouterHandler<Context, Record<string, UserData>>;
@@ -243,5 +233,5 @@ export declare function createAdaptivePlaywrightRouter<Context extends AdaptiveP
243
233
  * });
244
234
  * ```
245
235
  */
246
- export declare function fullResultComparator(resultA: RequestHandlerResult, resultB: RequestHandlerResult): boolean;
236
+ export declare function fullResultComparator(resultA: StorageTransactionView, resultB: StorageTransactionView): boolean;
247
237
  export {};