@crawlee/playwright 4.0.0-beta.102 → 4.0.0-beta.104

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.
@@ -25,7 +25,7 @@ declare class AdaptivePlaywrightCrawlerStatistics extends Statistics {
25
25
  trackBrowserRequestHandlerRun(): void;
26
26
  trackRenderingTypeMisprediction(): void;
27
27
  }
28
- export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary = Dictionary> extends CrawlingContext<UserData> {
28
+ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary = any> extends CrawlingContext<UserData> {
29
29
  request: LoadedRequest<Request<UserData>>;
30
30
  /**
31
31
  * The HTTP response, either from the HTTP client or from the initial request from playwright's navigation.
@@ -186,7 +186,7 @@ export declare class AdaptivePlaywrightCrawler<ContextExtension = Dictionary<nev
186
186
  private staticContextPipeline;
187
187
  private browserContextPipeline;
188
188
  private individualRequestHandlerTimeoutMillis;
189
- readonly stats: AdaptivePlaywrightCrawlerStatistics;
189
+ get stats(): AdaptivePlaywrightCrawlerStatistics;
190
190
  private resultObjects;
191
191
  private inFlightRenderingTypeDetections;
192
192
  private teardownHooks;
@@ -90,17 +90,30 @@ export class AdaptivePlaywrightCrawler extends BasicCrawler {
90
90
  staticContextPipeline;
91
91
  browserContextPipeline;
92
92
  individualRequestHandlerTimeoutMillis;
93
+ // The constructor always injects an `AdaptivePlaywrightCrawlerStatistics`, so narrowing the cast is sound.
94
+ get stats() {
95
+ return super.stats;
96
+ }
93
97
  resultObjects = new WeakMap();
94
98
  inFlightRenderingTypeDetections = 0;
95
99
  teardownHooks = [];
96
100
  constructor(options = {}) {
97
- const { requestHandler, renderingTypeDetectionRatio = 0.1, renderingTypePredictor, resultChecker, shouldPropagateError, resultComparator, statisticsOptions, preventDirectStorageAccess = true, requestHandlerTimeoutSecs = 60, errorHandler, failedRequestHandler, preNavigationHooks = [], postNavigationHooks = [], extendContext, contextPipelineBuilder, ...rest } = options;
101
+ const { requestHandler, renderingTypeDetectionRatio = 0.1, renderingTypePredictor, resultChecker, shouldPropagateError, resultComparator, statistics, preventDirectStorageAccess = true, requestHandlerTimeoutSecs = 60, errorHandler, failedRequestHandler, preNavigationHooks = [], postNavigationHooks = [], extendContext, contextPipelineBuilder, ...rest } = options;
102
+ if (statistics !== undefined && !(statistics instanceof AdaptivePlaywrightCrawlerStatistics)) {
103
+ throw new Error('AdaptivePlaywrightCrawler tracks extra fields on its own Statistics subclass and cannot use a ' +
104
+ 'plain `statistics` instance. Omit the option to let the crawler build its own.');
105
+ }
98
106
  super({
99
107
  ...rest,
100
108
  errorHandler,
101
109
  failedRequestHandler,
102
110
  requestHandler,
103
111
  requestHandlerTimeoutSecs,
112
+ // Inject our subclass so the base tracks the extra adaptive fields instead of building a plain `Statistics`.
113
+ statistics: statistics ??
114
+ new AdaptivePlaywrightCrawlerStatistics({
115
+ logMessage: `${AdaptivePlaywrightCrawler.name} request statistics:`,
116
+ }),
104
117
  contextPipelineBuilder: contextPipelineBuilder ?? (() => this.buildContextPipeline()),
105
118
  });
106
119
  this.individualRequestHandlerTimeoutMillis = requestHandlerTimeoutSecs * 1000;
@@ -134,18 +147,14 @@ export class AdaptivePlaywrightCrawler extends BasicCrawler {
134
147
  // each hook's overrides at runtime regardless of the static type.
135
148
  const staticCrawler = new CheerioCrawler({
136
149
  ...rest,
137
- statisticsOptions: {
138
- persistenceOptions: { enable: false },
139
- },
150
+ statistics: new Statistics({ persistenceOptions: { enable: false } }),
140
151
  preNavigationHooks,
141
152
  postNavigationHooks,
142
153
  extendContext,
143
154
  });
144
155
  const browserCrawler = new PlaywrightCrawler({
145
156
  ...rest,
146
- statisticsOptions: {
147
- persistenceOptions: { enable: false },
148
- },
157
+ statistics: new Statistics({ persistenceOptions: { enable: false } }),
149
158
  preNavigationHooks: preNavigationHooks,
150
159
  postNavigationHooks: postNavigationHooks,
151
160
  extendContext,
@@ -157,10 +166,6 @@ export class AdaptivePlaywrightCrawler extends BasicCrawler {
157
166
  this.browserContextPipeline = browserCrawler.contextPipeline.compose({
158
167
  action: this.adaptPlaywrightContext.bind(this),
159
168
  });
160
- this.stats = new AdaptivePlaywrightCrawlerStatistics({
161
- logMessage: `${this.log.getOptions().prefix} request statistics:`,
162
- ...statisticsOptions,
163
- });
164
169
  this.preventDirectStorageAccess = preventDirectStorageAccess;
165
170
  }
166
171
  async _init() {
@@ -8,11 +8,9 @@ import type { EnqueueLinksByClickingElementsOptions } from './enqueue-links/clic
8
8
  import type { PlaywrightLaunchContext } from './playwright-launcher.js';
9
9
  import type { BlockRequestsOptions, DirectNavigationOptions, HandleCloudflareChallengeOptions, InfiniteScrollOptions, InjectFileOptions, PlaywrightContextUtils, SaveSnapshotOptions } from './utils/playwright-utils.js';
10
10
  export type PlaywrightGotoOptions = NonNullable<Parameters<Page['goto']>[1]>;
11
- export interface PlaywrightCrawlingContext<UserData extends Dictionary = Dictionary> extends BrowserCrawlingContext<Page, Response, UserData, PlaywrightGotoOptions>, PlaywrightContextUtils {
12
- }
13
- // @ts-ignore optional peer dependency or compatibility with es2022
14
- export interface PlaywrightHook extends BrowserHook<PlaywrightCrawlingContext> {
11
+ export interface PlaywrightCrawlingContext<UserData extends Dictionary = any> extends BrowserCrawlingContext<Page, Response, UserData, PlaywrightGotoOptions>, PlaywrightContextUtils {
15
12
  }
13
+ export type PlaywrightHook<UserData extends Dictionary = any> = BrowserHook<PlaywrightCrawlingContext<UserData>>;
16
14
  export interface PlaywrightCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends PlaywrightCrawlingContext = PlaywrightCrawlingContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<PlaywrightCrawlingContext['request']>>> extends BrowserCrawlerOptions<Page, Response, PlaywrightCrawlingContext, ContextExtension, ExtendedContext, {
17
15
  browserPlugins: [PlaywrightPlugin];
18
16
  }, Routes> {
@@ -59,7 +57,7 @@ export interface PlaywrightCrawlerOptions<ContextExtension = Dictionary<never>,
59
57
  * ]
60
58
  * ```
61
59
  */
62
- preNavigationHooks?: BrowserHook<PlaywrightCrawlingContext, ContextExtension>[];
60
+ preNavigationHooks?: BrowserHook<PlaywrightCrawlingContext<GetUserDataFromRequest<ExtendedContext['request']>>, ContextExtension>[];
63
61
  /**
64
62
  * Async functions that are sequentially evaluated after the navigation. Good for checking if the navigation was successful.
65
63
  * The function accepts `crawlingContext` as the only parameter. A hook may optionally return a partial object
@@ -76,7 +74,7 @@ export interface PlaywrightCrawlerOptions<ContextExtension = Dictionary<never>,
76
74
  * ]
77
75
  * ```
78
76
  */
79
- postNavigationHooks?: BrowserHook<PlaywrightCrawlingContext, ContextExtension>[];
77
+ postNavigationHooks?: BrowserHook<PlaywrightCrawlingContext<GetUserDataFromRequest<ExtendedContext['request']>>, ContextExtension>[];
80
78
  }
81
79
  /**
82
80
  * Provides a simple framework for parallel crawling of web pages
@@ -238,7 +236,7 @@ export declare class PlaywrightCrawler<ContextExtension = Dictionary<never>, Ext
238
236
  // @ts-ignore optional peer dependency or compatibility with es2022
239
237
  keepAlive: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
240
238
  // @ts-ignore optional peer dependency or compatibility with es2022
241
- statisticsOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
239
+ statistics: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
242
240
  // @ts-ignore optional peer dependency or compatibility with es2022
243
241
  id: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
244
242
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crawlee/playwright",
3
- "version": "4.0.0-beta.102",
3
+ "version": "4.0.0-beta.104",
4
4
  "description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
5
5
  "engines": {
6
6
  "node": ">=22.0.0"
@@ -49,13 +49,13 @@
49
49
  "dependencies": {
50
50
  "@apify/datastructures": "^2.0.3",
51
51
  "@apify/timeout": "^0.4.4",
52
- "@crawlee/basic": "4.0.0-beta.102",
53
- "@crawlee/browser": "4.0.0-beta.102",
54
- "@crawlee/browser-pool": "4.0.0-beta.102",
55
- "@crawlee/cheerio": "4.0.0-beta.102",
56
- "@crawlee/core": "4.0.0-beta.102",
57
- "@crawlee/types": "4.0.0-beta.102",
58
- "@crawlee/utils": "4.0.0-beta.102",
52
+ "@crawlee/basic": "4.0.0-beta.104",
53
+ "@crawlee/browser": "4.0.0-beta.104",
54
+ "@crawlee/browser-pool": "4.0.0-beta.104",
55
+ "@crawlee/cheerio": "4.0.0-beta.104",
56
+ "@crawlee/core": "4.0.0-beta.104",
57
+ "@crawlee/types": "4.0.0-beta.104",
58
+ "@crawlee/utils": "4.0.0-beta.104",
59
59
  "cheerio": "^1.0.0",
60
60
  "idcac-playwright": "^0.1.3",
61
61
  "jquery": "^3.7.1",
@@ -85,5 +85,5 @@
85
85
  }
86
86
  }
87
87
  },
88
- "gitHead": "19eec2f388401dc75cf82d8cdcad5dd27143b1aa"
88
+ "gitHead": "1c761e689e56ac06b67fc4f1dc091f693e024d12"
89
89
  }