@crawlee/cheerio 3.0.0-alpha.2 → 3.0.0-alpha.5

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.
@@ -0,0 +1,620 @@
1
+ /// <reference types="node" />
2
+ import { BasicCrawler, BasicCrawlerOptions } from '@crawlee/basic';
3
+ import { CrawlerExtension, CrawlerHandleFailedRequestInput, CrawlingContext, EnqueueLinksOptions, ProxyConfiguration, ProxyInfo, Request, RequestQueue, Session, storage } from '@crawlee/core';
4
+ import { Awaitable, CheerioRoot } from '@crawlee/utils';
5
+ import { RequestLike, ResponseLike } from 'content-type';
6
+ import { OptionsInit } from 'got-scraping';
7
+ import { IncomingMessage } from 'http';
8
+ export interface CheerioFailedRequestHandlerInput<JSONData = unknown> extends CrawlerHandleFailedRequestInput, CheerioRequestHandlerInputs<JSONData> {
9
+ }
10
+ export declare type CheerioFailedRequestHandler<JSONData = unknown> = (inputs: CheerioFailedRequestHandlerInput<JSONData>) => Awaitable<void>;
11
+ export interface CheerioCrawlerOptions<JSONData = unknown> extends Omit<BasicCrawlerOptions<CheerioCrawlingContext<JSONData>>, 'requestHandler' | 'handleRequestFunction' | 'failedRequestHandler' | 'handleFailedRequestFunction' | 'handleRequestTimeoutSecs'> {
12
+ /**
13
+ * User-provided function that performs the logic of the crawler. It is called for each page
14
+ * loaded and parsed by the crawler.
15
+ *
16
+ * The function receives the following object as an argument:
17
+ * ```
18
+ * {
19
+ * // The Cheerio object's function with the parsed HTML.
20
+ * $: Cheerio,
21
+ *
22
+ * // The request body of the web page, whose type depends on the content type.
23
+ * body: String|Buffer,
24
+ *
25
+ * // The parsed object from JSON for responses with the "application/json" content types.
26
+ * // For other content types it's null.
27
+ * json: Object,
28
+ *
29
+ * // Request object with details of the requested web page
30
+ * request: Request,
31
+ *
32
+ * // Parsed Content-Type HTTP header: { type, encoding }
33
+ * contentType: Object,
34
+ *
35
+ * // An instance of Node's http.IncomingMessage object,
36
+ * response: Object,
37
+ *
38
+ * // Session object, useful to work around anti-scraping protections
39
+ * session: Session
40
+ *
41
+ * // ProxyInfo object with information about currently used proxy
42
+ * proxyInfo: ProxyInfo
43
+ *
44
+ * // The running cheerio crawler instance.
45
+ * crawler: CheerioCrawler
46
+ * }
47
+ * ```
48
+ *
49
+ * Type of `body` depends on the `Content-Type` header of the web page:
50
+ * - String for `text/html`, `application/xhtml+xml`, `application/xml` MIME content types
51
+ * - Buffer for others MIME content types
52
+ *
53
+ * Parsed `Content-Type` header using
54
+ * [content-type package](https://www.npmjs.com/package/content-type)
55
+ * is stored in `contentType`.
56
+ *
57
+ * Cheerio is available only for HTML and XML content types.
58
+ *
59
+ * With the {@link Request} object representing the URL to crawl.
60
+ *
61
+ * If the function returns, the returned promise is awaited by the crawler.
62
+ *
63
+ * If the function throws an exception, the crawler will try to re-crawl the
64
+ * request later, up to `option.maxRequestRetries` times.
65
+ * If all the retries fail, the crawler calls the function
66
+ * provided to the `handleFailedRequestFunction` parameter.
67
+ * To make this work, you should **always**
68
+ * let your function throw exceptions rather than catch them.
69
+ * The exceptions are logged to the request using the
70
+ * {@link Request.pushErrorMessage} function.
71
+ */
72
+ requestHandler: CheerioRequestHandler<JSONData>;
73
+ /**
74
+ * User-provided function that performs the logic of the crawler. It is called for each page
75
+ * loaded and parsed by the crawler.
76
+ *
77
+ * The function receives the following object as an argument:
78
+ * ```
79
+ * {
80
+ * // The Cheerio object's function with the parsed HTML.
81
+ * $: Cheerio,
82
+ *
83
+ * // The request body of the web page, whose type depends on the content type.
84
+ * body: String|Buffer,
85
+ *
86
+ * // The parsed object from JSON for responses with the "application/json" content types.
87
+ * // For other content types it's null.
88
+ * json: Object,
89
+ *
90
+ * // Request object with details of the requested web page
91
+ * request: Request,
92
+ *
93
+ * // Parsed Content-Type HTTP header: { type, encoding }
94
+ * contentType: Object,
95
+ *
96
+ * // An instance of Node's http.IncomingMessage object,
97
+ * response: Object,
98
+ *
99
+ * // Session object, useful to work around anti-scraping protections
100
+ * session: Session
101
+ *
102
+ * // ProxyInfo object with information about currently used proxy
103
+ * proxyInfo: ProxyInfo
104
+ *
105
+ * // The running cheerio crawler instance.
106
+ * crawler: CheerioCrawler
107
+ * }
108
+ * ```
109
+ *
110
+ * Type of `body` depends on the `Content-Type` header of the web page:
111
+ * - String for `text/html`, `application/xhtml+xml`, `application/xml` MIME content types
112
+ * - Buffer for others MIME content types
113
+ *
114
+ * Parsed `Content-Type` header using
115
+ * [content-type package](https://www.npmjs.com/package/content-type)
116
+ * is stored in `contentType`.
117
+ *
118
+ * Cheerio is available only for HTML and XML content types.
119
+ *
120
+ * With the {@link Request} object representing the URL to crawl.
121
+ *
122
+ * If the function returns, the returned promise is awaited by the crawler.
123
+ *
124
+ * If the function throws an exception, the crawler will try to re-crawl the
125
+ * request later, up to `option.maxRequestRetries` times.
126
+ * If all the retries fail, the crawler calls the function
127
+ * provided to the `handleFailedRequestFunction` parameter.
128
+ * To make this work, you should **always**
129
+ * let your function throw exceptions rather than catch them.
130
+ * The exceptions are logged to the request using the
131
+ * {@link Request.pushErrorMessage} function.
132
+ *
133
+ * @deprecated `handlePageFunction` has been renamed to `requestHandler` and will be removed in a future version.
134
+ */
135
+ handlePageFunction?: CheerioRequestHandler<JSONData>;
136
+ /**
137
+ * > This option is deprecated, use `preNavigationHooks` instead.
138
+ *
139
+ * A function that executes before the HTTP request is made to the target resource.
140
+ * This function is suitable for setting dynamic properties such as cookies to the {@link Request}.
141
+ *
142
+ * The function receives the following object as an argument:
143
+ * ```
144
+ * {
145
+ * request: Request,
146
+ * session: Session,
147
+ * proxyInfo: ProxyInfo,
148
+ * crawler: CheerioCrawler,
149
+ * }
150
+ * ```
151
+ * where the {@link Request} instance corresponds to the initialized request
152
+ * and the {@link Session} instance corresponds to used session.
153
+ *
154
+ * The function should modify the properties of the passed {@link Request} instance
155
+ * in place because there are already earlier references to it. Making a copy and returning it from
156
+ * this function is therefore not supported, because it would create inconsistencies where
157
+ * different parts of SDK would have access to a different {@link Request} instance.
158
+ */
159
+ prepareRequestFunction?: PrepareRequest<JSONData>;
160
+ /**
161
+ * > This option is deprecated, use `postNavigationHooks` instead.
162
+ *
163
+ * A function that executes right after the HTTP request is made to the target resource and response is returned.
164
+ * This function is suitable for overriding custom properties of response e.g. setting headers because of response parsing.
165
+ *
166
+ * **Example usage:**
167
+ *
168
+ * ```javascript
169
+ * const cheerioCrawlerOptions = {
170
+ * // ...
171
+ * postResponseFunction: ({ request, response }) => {
172
+ * if (request.userData.parseAsJSON) {
173
+ * response.headers['content-type'] = 'application/json; charset=utf-8';
174
+ * }
175
+ * }
176
+ * }
177
+ * ```
178
+ * The function receives the following object as an argument:
179
+ * ```
180
+ * {
181
+ * response: Object,
182
+ * request: Request,
183
+ * session: Session,
184
+ * proxyInfo: ProxyInfo,
185
+ * crawler: CheerioCrawler,
186
+ * }
187
+ * ```
188
+ * The response is an instance of Node's http.IncomingMessage object.
189
+ */
190
+ postResponseFunction?: PostResponse<JSONData>;
191
+ /**
192
+ * Timeout in which the HTTP request to the resource needs to finish, given in seconds.
193
+ */
194
+ requestTimeoutSecs?: number;
195
+ /**
196
+ * If set to true, SSL certificate errors will be ignored.
197
+ */
198
+ ignoreSslErrors?: boolean;
199
+ /**
200
+ * If set, `CheerioCrawler` will be configured for all connections to use
201
+ * [Apify Proxy](https://console.apify.com/proxy) or your own Proxy URLs provided and rotated according to the configuration.
202
+ * For more information, see the [documentation](https://docs.apify.com/proxy).
203
+ */
204
+ proxyConfiguration?: ProxyConfiguration;
205
+ /**
206
+ * A function to handle requests that failed more than `option.maxRequestRetries` times.
207
+ * The function receives the following object as an argument:
208
+ * ```
209
+ * {
210
+ * error: Error,
211
+ * request: Request,
212
+ * session: Session,
213
+ * $: Cheerio,
214
+ * body: String|Buffer,
215
+ * json: Object,
216
+ * contentType: Object,
217
+ * response: Object,
218
+ * proxyInfo: ProxyInfo,
219
+ * crawler: CheerioCrawler,
220
+ * }
221
+ * ```
222
+ * where the {@link Request} instance corresponds to the failed request, and the `Error` instance
223
+ * represents the last error thrown during processing of the request.
224
+ *
225
+ * See [source code](https://github.com/apify/apify-js/blob/master/src/crawlers/cheerio_crawler.js#L13)
226
+ * for the default implementation of this function.
227
+ */
228
+ failedRequestHandler?: CheerioFailedRequestHandler<JSONData>;
229
+ /**
230
+ * A function to handle requests that failed more than `option.maxRequestRetries` times.
231
+ * The function receives the following object as an argument:
232
+ * ```
233
+ * {
234
+ * error: Error,
235
+ * request: Request,
236
+ * session: Session,
237
+ * $: Cheerio,
238
+ * body: String|Buffer,
239
+ * json: Object,
240
+ * contentType: Object,
241
+ * response: Object,
242
+ * proxyInfo: ProxyInfo,
243
+ * crawler: CheerioCrawler,
244
+ * }
245
+ * ```
246
+ * where the {@link Request} instance corresponds to the failed request, and the `Error` instance
247
+ * represents the last error thrown during processing of the request.
248
+ *
249
+ * See [source code](https://github.com/apify/apify-js/blob/master/src/crawlers/cheerio_crawler.js#L13)
250
+ * for the default implementation of this function.
251
+ *
252
+ * @deprecated `handleFailedRequestFunction` has been renamed to `failedRequestHandler` and will be removed in a future version.
253
+ */
254
+ handleFailedRequestFunction?: CheerioFailedRequestHandler<JSONData>;
255
+ /**
256
+ * Async functions that are sequentially evaluated before the navigation. Good for setting additional cookies
257
+ * or browser properties before navigation. The function accepts two parameters, `crawlingContext` and `gotOptions`,
258
+ * which are passed to the `requestAsBrowser()` function the crawler calls to navigate.
259
+ * Example:
260
+ * ```
261
+ * preNavigationHooks: [
262
+ * async (crawlingContext, gotOptions) => {
263
+ * // ...
264
+ * },
265
+ * ]
266
+ * ```
267
+ */
268
+ preNavigationHooks?: CheerioHook<JSONData>[];
269
+ /**
270
+ * Async functions that are sequentially evaluated after the navigation. Good for checking if the navigation was successful.
271
+ * The function accepts `crawlingContext` as the only parameter.
272
+ * Example:
273
+ * ```
274
+ * postNavigationHooks: [
275
+ * async (crawlingContext) => {
276
+ * // ...
277
+ * },
278
+ * ]
279
+ * ```
280
+ */
281
+ postNavigationHooks?: CheerioHook<JSONData>[];
282
+ /**
283
+ * An array of [MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types)
284
+ * you want the crawler to load and process. By default, only `text/html` and `application/xhtml+xml` MIME types are supported.
285
+ */
286
+ additionalMimeTypes?: string[];
287
+ /**
288
+ * By default `CheerioCrawler` will extract correct encoding from the HTTP response headers.
289
+ * Sadly, there are some websites which use invalid headers. Those are encoded using the UTF-8 encoding.
290
+ * If those sites actually use a different encoding, the response will be corrupted. You can use
291
+ * `suggestResponseEncoding` to fall back to a certain encoding, if you know that your target website uses it.
292
+ * To force a certain encoding, disregarding the response headers, use {@link CheerioCrawlerOptions.forceResponseEncoding}
293
+ * ```
294
+ * // Will fall back to windows-1250 encoding if none found
295
+ * suggestResponseEncoding: 'windows-1250'
296
+ * ```
297
+ */
298
+ suggestResponseEncoding?: string;
299
+ /**
300
+ * By default `CheerioCrawler` will extract correct encoding from the HTTP response headers. Use `forceResponseEncoding`
301
+ * to force a certain encoding, disregarding the response headers.
302
+ * To only provide a default for missing encodings, use {@link CheerioCrawlerOptions.suggestResponseEncoding}
303
+ * ```
304
+ * // Will force windows-1250 encoding even if headers say otherwise
305
+ * forceResponseEncoding: 'windows-1250'
306
+ * ```
307
+ */
308
+ forceResponseEncoding?: string;
309
+ /**
310
+ * Automatically saves cookies to Session. Works only if Session Pool is used.
311
+ *
312
+ * It parses cookie from response "set-cookie" header saves or updates cookies for session and once the session is used for next request.
313
+ * It passes the "Cookie" header to the request with the session cookies.
314
+ */
315
+ persistCookiesPerSession?: boolean;
316
+ }
317
+ export interface PrepareRequestInputs<JSONData = unknown> {
318
+ /**
319
+ * Original instance of the {@link Request} object. Must be modified in-place.
320
+ */
321
+ request: Request;
322
+ /**
323
+ * The current session
324
+ */
325
+ session?: Session;
326
+ /**
327
+ * An object with information about currently used proxy by the crawler
328
+ * and configured by the {@link ProxyConfiguration} class.
329
+ */
330
+ proxyInfo?: ProxyInfo;
331
+ crawler?: CheerioCrawler<JSONData>;
332
+ }
333
+ export declare type PrepareRequest<JSONData = unknown> = (inputs: PrepareRequestInputs<JSONData>) => Awaitable<void>;
334
+ export declare type CheerioHook<JSONData = unknown> = (crawlingContext: CheerioCrawlingContext<JSONData>, gotOptions: OptionsInit) => Awaitable<void>;
335
+ export interface PostResponseInputs<JSONData = unknown> {
336
+ /**
337
+ * stream
338
+ */
339
+ response?: IncomingMessage;
340
+ /**
341
+ * Original instance fo the {Request} object. Must be modified in-place.
342
+ */
343
+ request: Request;
344
+ /**
345
+ * The current session
346
+ */
347
+ session?: Session;
348
+ /**
349
+ * An object with information about currently used proxy by the crawler
350
+ * and configured by the {@link ProxyConfiguration} class.
351
+ */
352
+ proxyInfo?: ProxyInfo;
353
+ crawler: CheerioCrawler<JSONData>;
354
+ }
355
+ export declare type PostResponse<JSONData = unknown> = (inputs: PostResponseInputs<JSONData>) => Awaitable<void>;
356
+ export interface CheerioRequestHandlerInputs<JSONData = unknown> extends CrawlingContext {
357
+ /**
358
+ * The [Cheerio](https://cheerio.js.org/) object with parsed HTML.
359
+ */
360
+ $: CheerioRoot;
361
+ /**
362
+ * The request body of the web page.
363
+ */
364
+ body: (string | Buffer);
365
+ /**
366
+ * The parsed object from JSON string if the response contains the content type application/json.
367
+ */
368
+ json: JSONData;
369
+ /**
370
+ * Parsed `Content-Type header: { type, encoding }`.
371
+ */
372
+ contentType: {
373
+ type: string;
374
+ encoding: string;
375
+ };
376
+ crawler: CheerioCrawler<JSONData>;
377
+ response: IncomingMessage;
378
+ enqueueLinks: (options?: CheerioCrawlerEnqueueLinksOptions) => Promise<storage.BatchAddRequestsResult>;
379
+ }
380
+ export declare type CheerioCrawlingContext<JSONData = unknown> = CheerioRequestHandlerInputs<JSONData>;
381
+ export declare type CheerioRequestHandler<JSONData = unknown> = (inputs: CheerioRequestHandlerInputs<JSONData>) => Awaitable<void>;
382
+ export declare type CheerioCrawlerEnqueueLinksOptions = Omit<EnqueueLinksOptions, 'urls' | 'requestQueue'>;
383
+ /**
384
+ * Provides a framework for the parallel crawling of web pages using plain HTTP requests and
385
+ * [cheerio](https://www.npmjs.com/package/cheerio) HTML parser.
386
+ * The URLs to crawl are fed either from a static list of URLs
387
+ * or from a dynamic queue of URLs enabling recursive crawling of websites.
388
+ *
389
+ * Since `CheerioCrawler` uses raw HTTP requests to download web pages,
390
+ * it is very fast and efficient on data bandwidth. However, if the target website requires JavaScript
391
+ * to display the content, you might need to use {@link PuppeteerCrawler} or {@link PlaywrightCrawler} instead,
392
+ * because it loads the pages using full-featured headless Chrome browser.
393
+ *
394
+ * `CheerioCrawler` downloads each URL using a plain HTTP request,
395
+ * parses the HTML content using [Cheerio](https://www.npmjs.com/package/cheerio)
396
+ * and then invokes the user-provided {@link CheerioCrawlerOptions.requestHandler} to extract page data
397
+ * using a [jQuery](https://jquery.com/)-like interface to the parsed HTML DOM.
398
+ *
399
+ * The source URLs are represented using {@link Request} objects that are fed from
400
+ * {@link RequestList} or {@link RequestQueue} instances provided by the {@link CheerioCrawlerOptions.requestList}
401
+ * or {@link CheerioCrawlerOptions.requestQueue} constructor options, respectively.
402
+ *
403
+ * If both {@link CheerioCrawlerOptions.requestList} and {@link CheerioCrawlerOptions.requestQueue} are used,
404
+ * the instance first processes URLs from the {@link RequestList} and automatically enqueues all of them
405
+ * to {@link RequestQueue} before it starts their processing. This ensures that a single URL is not crawled multiple times.
406
+ *
407
+ * The crawler finishes when there are no more {@link Request} objects to crawl.
408
+ *
409
+ * We can use the `preNavigationHooks` to adjust `gotOptions`:
410
+ *
411
+ * ```
412
+ * preNavigationHooks: [
413
+ * (crawlingContext, gotOptions) => {
414
+ * // ...
415
+ * },
416
+ * ]
417
+ * ```
418
+ *
419
+ * By default, `CheerioCrawler` only processes web pages with the `text/html`
420
+ * and `application/xhtml+xml` MIME content types (as reported by the `Content-Type` HTTP header),
421
+ * and skips pages with other content types. If you want the crawler to process other content types,
422
+ * use the {@link CheerioCrawlerOptions.additionalMimeTypes} constructor option.
423
+ * Beware that the parsing behavior differs for HTML, XML, JSON and other types of content.
424
+ * For details, see {@link CheerioCrawlerOptions.requestHandler}.
425
+ *
426
+ * New requests are only dispatched when there is enough free CPU and memory available,
427
+ * using the functionality provided by the {@link AutoscaledPool} class.
428
+ * All {@link AutoscaledPool} configuration options can be passed to the `autoscaledPoolOptions`
429
+ * parameter of the `CheerioCrawler` constructor. For user convenience, the `minConcurrency` and `maxConcurrency`
430
+ * {@link AutoscaledPool} options are available directly in the `CheerioCrawler` constructor.
431
+ *
432
+ * **Example usage:**
433
+ *
434
+ * ```javascript
435
+ * // Prepare a list of URLs to crawl
436
+ * const requestList = new RequestList({
437
+ * sources: [
438
+ * { url: 'http://www.example.com/page-1' },
439
+ * { url: 'http://www.example.com/page-2' },
440
+ * ],
441
+ * });
442
+ * await requestList.initialize();
443
+ *
444
+ * // Crawl the URLs
445
+ * const crawler = new CheerioCrawler({
446
+ * requestList,
447
+ * handlePageFunction: async ({ request, response, body, contentType, $ }) => {
448
+ * const data = [];
449
+ *
450
+ * // Do some data extraction from the page with Cheerio.
451
+ * $('.some-collection').each((index, el) => {
452
+ * data.push({ title: $(el).find('.some-title').text() });
453
+ * });
454
+ *
455
+ * // Save the data to dataset.
456
+ * await Actor.pushData({
457
+ * url: request.url,
458
+ * html: body,
459
+ * data,
460
+ * })
461
+ * },
462
+ * });
463
+ *
464
+ * await crawler.run();
465
+ * ```
466
+ * @category Crawlers
467
+ */
468
+ export declare class CheerioCrawler<JSONData = unknown> extends BasicCrawler<CheerioCrawlingContext<JSONData>, CheerioFailedRequestHandlerInput<JSONData>> {
469
+ /**
470
+ * A reference to the underlying {@link ProxyConfiguration} class that manages the crawler's proxies.
471
+ * Only available if used by the crawler.
472
+ */
473
+ proxyConfiguration?: ProxyConfiguration;
474
+ protected userRequestHandlerTimeoutMillis: number;
475
+ protected defaultGotoOptions: {
476
+ timeout: number;
477
+ };
478
+ protected preNavigationHooks: CheerioHook<JSONData>[];
479
+ protected postNavigationHooks: CheerioHook<JSONData>[];
480
+ protected persistCookiesPerSession: boolean;
481
+ protected requestTimeoutMillis: number;
482
+ protected ignoreSslErrors: boolean;
483
+ protected suggestResponseEncoding?: string;
484
+ protected forceResponseEncoding?: string;
485
+ protected prepareRequestFunction?: PrepareRequest<JSONData>;
486
+ protected postResponseFunction?: PostResponse<JSONData>;
487
+ protected readonly supportedMimeTypes: Set<string>;
488
+ protected static optionsShape: {
489
+ handlePageFunction: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
490
+ requestTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
491
+ ignoreSslErrors: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
492
+ additionalMimeTypes: import("ow").ArrayPredicate<string>;
493
+ suggestResponseEncoding: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
494
+ forceResponseEncoding: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
495
+ proxyConfiguration: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
496
+ prepareRequestFunction: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
497
+ postResponseFunction: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
498
+ persistCookiesPerSession: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
499
+ preNavigationHooks: import("ow").ArrayPredicate<unknown> & import("ow").BasePredicate<unknown[] | undefined>;
500
+ postNavigationHooks: import("ow").ArrayPredicate<unknown> & import("ow").BasePredicate<unknown[] | undefined>;
501
+ requestList: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
502
+ requestQueue: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
503
+ requestHandler: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
504
+ handleRequestFunction: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
505
+ requestHandlerTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
506
+ handleRequestTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
507
+ failedRequestHandler: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
508
+ handleFailedRequestFunction: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
509
+ maxRequestRetries: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
510
+ maxRequestsPerCrawl: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
511
+ autoscaledPoolOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
512
+ sessionPoolOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
513
+ useSessionPool: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
514
+ minConcurrency: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
515
+ maxConcurrency: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
516
+ log: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
517
+ };
518
+ /**
519
+ * All `CheerioCrawler` parameters are passed via an options object.
520
+ */
521
+ constructor(options: CheerioCrawlerOptions<JSONData>);
522
+ /**
523
+ * **EXPERIMENTAL**
524
+ * Function for attaching CrawlerExtensions such as the Unblockers.
525
+ * @param extension Crawler extension that overrides the crawler configuration.
526
+ */
527
+ use(extension: CrawlerExtension): void;
528
+ /**
529
+ * Wrapper around handlePageFunction that opens and closes pages etc.
530
+ */
531
+ protected _runRequestHandler(crawlingContext: CheerioCrawlingContext<JSONData>): Promise<void>;
532
+ protected _handleNavigation(crawlingContext: CheerioCrawlingContext<JSONData>): Promise<void>;
533
+ /**
534
+ * When users change `request.headers.cookie` inside preNavigationHook, the change would be ignored,
535
+ * as `request.headers` are already merged into the `gotOptions`. This method is using
536
+ * old `request.headers` snapshot (before hooks are executed), makes a diff with the cookie value
537
+ * after hooks are executed, and merges any new cookies back to `gotOptions`.
538
+ *
539
+ * This way we can still use both `gotOptions` and `context.request` in the hooks (not both).
540
+ */
541
+ private _mergeRequestCookieDiff;
542
+ /**
543
+ * Function to make the HTTP request. It performs optimizations
544
+ * on the request such as only downloading the request body if the
545
+ * received content type matches text/html, application/xml, application/xhtml+xml.
546
+ */
547
+ protected _requestFunction({ request, session, proxyUrl, gotOptions }: RequestFunctionOptions): Promise<IncomingMessage>;
548
+ /**
549
+ * Sets the cookie header to `gotOptions` based on provided session and request. If some cookies were already set,
550
+ * the session cookie will be merged with them. User provided cookies on `request` object have precedence.
551
+ */
552
+ private _applySessionCookie;
553
+ /**
554
+ * Encodes and parses response according to the provided content type
555
+ */
556
+ protected _parseResponse(request: Request, responseStream: IncomingMessage): Promise<{
557
+ dom: unknown;
558
+ isXml: boolean;
559
+ response: IncomingMessage;
560
+ contentType: {
561
+ type: string;
562
+ encoding: BufferEncoding;
563
+ };
564
+ body?: undefined;
565
+ } | {
566
+ body: Buffer;
567
+ response: IncomingMessage;
568
+ contentType: {
569
+ type: string;
570
+ encoding: BufferEncoding;
571
+ };
572
+ dom?: undefined;
573
+ isXml?: undefined;
574
+ }>;
575
+ /**
576
+ * Combines the provided `requestOptions` with mandatory (non-overridable) values.
577
+ */
578
+ protected _getRequestOptions(request: Request, session?: Session, proxyUrl?: string, gotOptions?: OptionsInit): OptionsInit & {
579
+ isStream: true;
580
+ };
581
+ protected _encodeResponse(request: Request, response: IncomingMessage, encoding: BufferEncoding): {
582
+ encoding: BufferEncoding;
583
+ response: IncomingMessage;
584
+ };
585
+ protected _parseHtmlToDom(response: IncomingMessage): Promise<unknown>;
586
+ /**
587
+ * Checks and extends supported mime types
588
+ */
589
+ protected _extendSupportedMimeTypes(additionalMimeTypes: (string | RequestLike | ResponseLike)[]): void;
590
+ /**
591
+ * Handles blocked request
592
+ */
593
+ protected _throwOnBlockedRequest(session: Session, statusCode: number): void;
594
+ /**
595
+ * Handles timeout request
596
+ */
597
+ protected _handleRequestTimeout(session?: Session): void;
598
+ private _abortDownloadOfBody;
599
+ /**
600
+ * @internal wraps public utility for mocking purposes
601
+ */
602
+ private _requestAsBrowser;
603
+ }
604
+ interface EnqueueLinksInternalOptions {
605
+ options?: CheerioCrawlerEnqueueLinksOptions;
606
+ $: CheerioRoot | null;
607
+ requestQueue: RequestQueue;
608
+ originalRequestUrl: string;
609
+ finalRequestUrl?: string;
610
+ }
611
+ /** @internal */
612
+ export declare function cheerioCrawlerEnqueueLinks({ options, $, requestQueue, originalRequestUrl, finalRequestUrl }: EnqueueLinksInternalOptions): Promise<storage.BatchAddRequestsResult>;
613
+ interface RequestFunctionOptions {
614
+ request: Request;
615
+ session?: Session;
616
+ proxyUrl?: string;
617
+ gotOptions: OptionsInit;
618
+ }
619
+ export {};
620
+ //# sourceMappingURL=cheerio-crawler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cheerio-crawler.d.ts","sourceRoot":"","sources":["../../src/internals/cheerio-crawler.ts"],"names":[],"mappings":";AAEA,OAAO,EACH,YAAY,EACZ,mBAAmB,EAEtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACH,gBAAgB,EAChB,+BAA+B,EAC/B,eAAe,EAGf,mBAAmB,EAEnB,kBAAkB,EAClB,SAAS,EACT,OAAO,EACP,YAAY,EAEZ,OAAO,EACP,OAAO,EAEV,MAAM,eAAe,CAAC;AACvB,OAAO,EACH,SAAS,EACT,WAAW,EAGd,MAAM,gBAAgB,CAAC;AAExB,OAA0B,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5E,OAAO,EAAe,WAAW,EAA+C,MAAM,cAAc,CAAC;AAGrG,OAAO,EAAuB,eAAe,EAAE,MAAM,MAAM,CAAC;AAoB5D,MAAM,WAAW,gCAAgC,CAAC,QAAQ,GAAG,OAAO,CAAE,SAAQ,+BAA+B,EAAE,2BAA2B,CAAC,QAAQ,CAAC;CAAG;AAEvJ,oBAAY,2BAA2B,CAAC,QAAQ,GAAG,OAAO,IAAI,CAAC,MAAM,EAAE,g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