@crawlee/playwright 4.0.0-rc.0 → 4.0.0-rc.2

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,7 +1,8 @@
1
1
  export * from '@crawlee/browser';
2
2
  export * from './internals/playwright-browser-pool.js';
3
3
  export * from './internals/playwright-crawler.js';
4
- export * from './internals/playwright-launcher.js';
4
+ export { launchPlaywright } from './internals/playwright-launcher.js';
5
+ export type { PlaywrightLaunchContext } from './internals/playwright-launcher.js';
5
6
  export * from './internals/adaptive-playwright-crawler.js';
6
7
  export { RenderingTypePredictor } from './internals/utils/rendering-type-prediction.js';
7
8
  export * as playwrightUtils from './internals/utils/playwright-utils.js';
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from '@crawlee/browser';
2
2
  export * from './internals/playwright-browser-pool.js';
3
3
  export * from './internals/playwright-crawler.js';
4
- export * from './internals/playwright-launcher.js';
4
+ export { launchPlaywright } from './internals/playwright-launcher.js';
5
5
  export * from './internals/adaptive-playwright-crawler.js';
6
6
  export { RenderingTypePredictor } from './internals/utils/rendering-type-prediction.js';
7
7
  export * as playwrightUtils from './internals/utils/playwright-utils.js';
@@ -1,15 +1,15 @@
1
- import type { BrowserHook, LoadedRequest, Request, RouterHandler, RouteSchemas, RoutesFromSchemas } from '@crawlee/browser';
2
- import type { BasicCrawlerOptions } from '@crawlee/basic';
1
+ import type { BrowserHook, LoadedRequest, 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, StorageTransactionView } from '@crawlee/core';
4
+ import { CrawlingRequest } from '@crawlee/browser';
5
+ import type { StorageTransactionView } from '@crawlee/core';
5
6
  import type { Dictionary, Awaitable } from '@crawlee/types';
6
- import { type CheerioRoot } from '@crawlee/utils/internal';
7
- import { type Cheerio } from 'cheerio';
7
+ import { type Cheerio, type CheerioAPI } from 'cheerio';
8
8
  import type { AnyNode } from 'domhandler';
9
9
  // @ts-ignore optional peer dependency or compatibility with es2022
10
10
  import type { Page } from 'playwright';
11
11
  import { z } from 'zod';
12
- import type { PlaywrightCrawlingContext, PlaywrightGotoOptions } from './playwright-crawler.js';
12
+ import type { PlaywrightCrawlerOptions, PlaywrightCrawlingContext, PlaywrightGotoOptions } from './playwright-crawler.js';
13
13
  import { type IRenderingTypePredictor } from './utils/rendering-type-prediction.js';
14
14
  declare const adaptiveStatisticStateSchema: z.ZodObject<{
15
15
  httpOnlyRequestHandlerRuns: z.ZodDefault<z.ZodNumber>;
@@ -35,7 +35,7 @@ export declare const adaptivePlaywrightCrawlerStatisticState: {
35
35
  }, z.core.$strip>;
36
36
  };
37
37
  export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary = any> extends CrawlingContext<UserData> {
38
- request: LoadedRequest<Request<UserData>>;
38
+ request: LoadedRequest<CrawlingRequest<UserData>>;
39
39
  /**
40
40
  * The HTTP response, either from the HTTP client or from the initial request from playwright's navigation.
41
41
  */
@@ -44,16 +44,17 @@ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary =
44
44
  * Playwright Page object. If accessed in HTTP-only rendering, this will throw an error and make the AdaptivePlaywrightCrawlerContext retry the request in a browser.
45
45
  */
46
46
  page: Page;
47
- /**
48
- * Wait for an element matching the selector to appear and return a Cheerio object of the first matched element.
49
- * Timeout defaults to 5s.
50
- */
51
- querySelector(selector: string, timeoutMs?: number): Promise<Cheerio<AnyNode>>;
52
47
  /**
53
48
  * Wait for an element matching the selector to appear and return a Cheerio object of all matched elements.
49
+ * Throws when nothing matches - in the HTTP-only run, that makes the crawler retry the request in a browser.
54
50
  * Timeout defaults to 5s.
55
51
  */
56
52
  querySelectorAll(selector: string, timeoutMs?: number): Promise<Cheerio<AnyNode>>;
53
+ /**
54
+ * Like {@link AdaptivePlaywrightCrawlerContext.querySelectorAll|`querySelectorAll`}, but returns only the
55
+ * first match.
56
+ */
57
+ querySelectorOne(selector: string, timeoutMs?: number): Promise<Cheerio<AnyNode>>;
57
58
  /**
58
59
  * Wait for an element matching the selector to appear.
59
60
  * Timeout defaults to 5s.
@@ -80,19 +81,30 @@ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary =
80
81
  * });
81
82
  * ```
82
83
  */
83
- parseWithCheerio(selector?: string, timeoutMs?: number): Promise<CheerioRoot>;
84
+ parseWithCheerio(selector?: string, timeoutMs?: number): Promise<CheerioAPI>;
84
85
  enqueueLinks(options?: EnqueueLinksOptions): Promise<unknown>;
85
86
  }
86
- interface AdaptiveHookContext extends Pick<AdaptivePlaywrightCrawlerContext, 'id' | 'session' | 'proxyInfo' | 'log'> {
87
+ interface AdaptiveHookContext extends Pick<AdaptivePlaywrightCrawlerContext, 'id' | 'session' | 'proxyInfo' | 'log' | 'extendTimeout'> {
87
88
  page?: Page;
88
- request: Request;
89
+ request: CrawlingRequest;
89
90
  gotoOptions?: PlaywrightGotoOptions;
90
91
  }
91
92
  type AdaptiveHook<ContextExtension = Dictionary<never>> = BrowserHook<AdaptiveHookContext, ContextExtension>;
92
93
  type AdaptivePostNavigationHook<ContextExtension = Dictionary<never>> = BrowserHook<Omit<AdaptiveHookContext, 'request'> & {
93
- request: LoadedRequest<Request>;
94
+ request: LoadedRequest<CrawlingRequest>;
94
95
  }, ContextExtension>;
95
- 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'> {
96
+ /** Passed to {@link AdaptivePlaywrightCrawlerOptions.resultChecker} and `resultComparator` alongside the results. */
97
+ export interface AdaptiveResultContext {
98
+ /** The request whose handler produced the results. */
99
+ request: CrawlingRequest;
100
+ }
101
+ 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'> {
102
+ /**
103
+ * Timeout for the navigation phase of each attempt, in seconds, shared by the `preNavigationHooks`, the
104
+ * navigation and the `postNavigationHooks`. Applies to both the HTTP-only and the browser attempt. When not
105
+ * set, each attempt keeps its own default: 30 seconds for HTTP-only and 60 seconds for the browser.
106
+ */
107
+ navigationTimeoutSecs?: number;
96
108
  /**
97
109
  * Async functions that are sequentially evaluated before the navigation. Good for setting additional cookies.
98
110
  * The function accepts a subset of the crawling context. If you attempt to access the `page` property during HTTP-only crawling,
@@ -122,7 +134,7 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
122
134
  * If it returns false, the request is retried in a browser.
123
135
  * If no callback is specified, every result is considered valid.
124
136
  */
125
- resultChecker?: (result: StorageTransactionView) => boolean;
137
+ resultChecker?: (result: StorageTransactionView, context: AdaptiveResultContext) => boolean;
126
138
  /**
127
139
  * An optional callback that decides whether an error thrown during the plain HTTP request handler
128
140
  * should be propagated (instead of falling back to browser navigation).
@@ -145,7 +157,7 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
145
157
  *
146
158
  * For a stricter, ready-made comparator that also takes enqueued requests and key-value store changes into account, see {@link fullResultComparator}.
147
159
  */
148
- resultComparator?: (resultA: StorageTransactionView, resultB: StorageTransactionView) => boolean | 'equal' | 'different' | 'inconclusive';
160
+ resultComparator?: (resultA: StorageTransactionView, resultB: StorageTransactionView, context: AdaptiveResultContext) => boolean | 'equal' | 'different' | 'inconclusive';
149
161
  /**
150
162
  * A custom rendering type predictor. A predictor passed here is borrowed - the crawler never drives its
151
163
  * lifecycle, so set it up yourself (the built-in {@link RenderingTypePredictor} needs `initialize()`).
@@ -161,9 +173,9 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
161
173
  * ```javascript
162
174
  * const crawler = new AdaptivePlaywrightCrawler({
163
175
  * renderingTypeDetectionRatio: 0.1,
164
- * async requestHandler({ querySelector, pushData, enqueueLinks, request, log }) {
176
+ * async requestHandler({ querySelectorAll, pushData, enqueueLinks, request, log }) {
165
177
  * // This function is called to extract data from a single web page
166
- * const $prices = await querySelector('span.price')
178
+ * const $prices = await querySelectorAll('span.price')
167
179
  *
168
180
  * await pushData({
169
181
  * url: request.url,
@@ -179,36 +191,32 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
179
191
  * 'http://www.example.com/page-2',
180
192
  * ]);
181
193
  * ```
182
- *
183
- * @experimental
184
194
  */
185
195
  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> {
186
196
  #private;
187
197
  constructor(options?: AdaptivePlaywrightCrawlerOptions<ContextExtension, ExtendedContext, Routes, StatisticStateExtension>);
188
198
  protected init(): Promise<void>;
189
- protected buildContextPipeline(): ContextPipeline<CrawlingContext<Dictionary>, CrawlingContext<Dictionary> & {
190
- readonly request: LoadedRequest<Request<Dictionary>>;
191
- readonly response: Response;
192
- readonly page: Page;
193
- readonly querySelector: AdaptivePlaywrightCrawlerContext["querySelector"];
194
- readonly querySelectorAll: AdaptivePlaywrightCrawlerContext["querySelectorAll"];
195
- readonly waitForSelector: AdaptivePlaywrightCrawlerContext["waitForSelector"];
196
- readonly parseWithCheerio: AdaptivePlaywrightCrawlerContext["parseWithCheerio"];
197
- readonly enqueueLinks: AdaptivePlaywrightCrawlerContext["enqueueLinks"];
198
- }>;
199
- private adaptCheerioContext;
200
- private adaptPlaywrightContext;
201
- /**
202
- * Runs one request handler attempt inside its own {@link StorageTransaction}, wrapping the inner
203
- * (static or browser) context pipeline. The transaction is pushed to `transactions` *at creation
204
- * time, before the `try`* - the `ok: false` branch of the returned {@link Result} carries no
205
- * result, and failed attempts are routine here. The caller owns the outcome and disposal.
206
- */
207
- private crawlOne;
208
199
  protected runRequestHandler(crawlingContext: CrawlingContext): Promise<void>;
209
- private enqueueLinks;
210
- private createLogProxy;
200
+ /**
201
+ * Number of rendering type detections that have not settled yet, including results the predictor is
202
+ * still persisting.
203
+ */
204
+ get inFlightRenderingTypeDetectionCount(): number;
205
+ /**
206
+ * Waits for in-flight rendering type detections to settle, bounded by `timeoutMillis` (defaults to the
207
+ * internal timeout).
208
+ */
209
+ drainRenderingDetections({ timeoutMillis }?: {
210
+ timeoutMillis?: number;
211
+ }): Promise<void>;
212
+ /**
213
+ * Stops the crawler immediately, but not before rendering type detections already under way (and results
214
+ * the predictor is still persisting) have settled - see
215
+ * {@link AdaptivePlaywrightCrawler.drainRenderingDetections|`drainRenderingDetections()`}. Requests
216
+ * that are still running are not waited for, unlike {@link BasicCrawler.stop|`stop()`}.
217
+ */
211
218
  teardown(): Promise<void>;
219
+ destroy(): Promise<void>;
212
220
  }
213
221
  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>;
214
222
  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>>;