@crawlee/core 4.0.0-beta.12 → 4.0.0-beta.121
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 +17 -13
- package/autoscaling/autoscaled_pool.d.ts +67 -172
- package/autoscaling/autoscaled_pool.js +165 -320
- package/autoscaling/client_load_signal.d.ts +55 -0
- package/autoscaling/client_load_signal.js +73 -0
- package/autoscaling/concurrency_system.d.ts +268 -0
- package/autoscaling/concurrency_system.js +351 -0
- package/autoscaling/cpu_load_signal.d.ts +43 -0
- package/autoscaling/cpu_load_signal.js +47 -0
- package/autoscaling/event_loop_load_signal.d.ts +51 -0
- package/autoscaling/event_loop_load_signal.js +60 -0
- package/autoscaling/index.d.ts +6 -1
- package/autoscaling/index.js +6 -1
- package/autoscaling/load_signal.d.ts +99 -0
- package/autoscaling/load_signal.js +104 -0
- package/autoscaling/memory_load_signal.d.ts +47 -0
- package/autoscaling/memory_load_signal.js +106 -0
- package/autoscaling/snapshotter.d.ts +58 -163
- package/autoscaling/snapshotter.js +45 -263
- package/autoscaling/system_status.d.ts +62 -84
- package/autoscaling/system_status.js +92 -122
- package/autoscaling/weighted_avg.d.ts +5 -0
- package/autoscaling/weighted_avg.js +14 -0
- package/byte_utils.d.ts +17 -0
- package/byte_utils.js +42 -0
- package/configuration.d.ts +96 -223
- package/configuration.js +170 -222
- package/cookie_utils.d.ts +4 -3
- package/cookie_utils.js +22 -13
- package/crawlers/context_pipeline.d.ts +10 -1
- package/crawlers/context_pipeline.js +31 -8
- package/crawlers/crawler_commons.d.ts +90 -83
- package/crawlers/crawler_commons.js +1 -116
- package/crawlers/error_snapshotter.d.ts +2 -5
- package/crawlers/error_snapshotter.js +7 -8
- package/crawlers/error_tracker.d.ts +0 -1
- package/crawlers/error_tracker.js +0 -1
- package/crawlers/index.d.ts +1 -3
- package/crawlers/index.js +0 -3
- package/crawlers/internals/types.d.ts +0 -1
- package/crawlers/internals/types.js +0 -1
- package/crawlers/statistics.d.ts +143 -59
- package/crawlers/statistics.js +243 -153
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/enqueue_links/enqueue_links.d.ts +59 -68
- package/enqueue_links/enqueue_links.js +57 -62
- package/enqueue_links/index.d.ts +0 -1
- package/enqueue_links/index.js +0 -1
- package/enqueue_links/shared.d.ts +40 -27
- package/enqueue_links/shared.js +90 -68
- package/errors.d.ts +72 -4
- package/errors.js +89 -5
- package/events/event_manager.d.ts +35 -9
- package/events/event_manager.js +10 -12
- package/events/index.d.ts +0 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -3
- package/events/local_event_manager.js +39 -13
- package/http.d.ts +9 -0
- package/http.js +28 -0
- package/index.d.ts +7 -4
- package/index.js +6 -3
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +82 -3
- package/log.js +106 -1
- package/memory-storage/consts.d.ts +4 -0
- package/memory-storage/consts.js +4 -0
- package/memory-storage/index.d.ts +1 -0
- package/memory-storage/index.js +1 -0
- package/memory-storage/memory-storage.d.ts +38 -0
- package/memory-storage/memory-storage.js +130 -0
- package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
- package/memory-storage/resource-clients/common/base-client.js +6 -0
- package/memory-storage/resource-clients/dataset.d.ts +40 -0
- package/memory-storage/resource-clients/dataset.js +114 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +204 -0
- package/memory-storage/resource-clients/request-queue.d.ts +77 -0
- package/memory-storage/resource-clients/request-queue.js +422 -0
- package/memory-storage/utils.d.ts +16 -0
- package/memory-storage/utils.js +41 -0
- package/owned_or_injected.d.ts +58 -0
- package/owned_or_injected.js +98 -0
- package/package.json +13 -12
- package/proxy_configuration.d.ts +24 -132
- package/proxy_configuration.js +24 -143
- package/recoverable_state.d.ts +140 -0
- package/recoverable_state.js +212 -0
- package/request.d.ts +86 -17
- package/request.js +120 -41
- package/router.d.ts +193 -21
- package/router.js +188 -43
- package/serialization.d.ts +0 -1
- package/serialization.js +9 -11
- package/service_locator.d.ts +165 -0
- package/service_locator.js +253 -0
- package/session_pool/consts.d.ts +1 -2
- package/session_pool/consts.js +1 -2
- package/session_pool/errors.d.ts +0 -1
- package/session_pool/errors.js +0 -1
- package/session_pool/fingerprint.d.ts +9 -0
- package/session_pool/fingerprint.js +30 -0
- package/session_pool/index.d.ts +0 -2
- package/session_pool/index.js +0 -2
- package/session_pool/session.d.ts +35 -89
- package/session_pool/session.js +82 -142
- package/session_pool/session_pool.d.ts +69 -90
- package/session_pool/session_pool.js +151 -150
- package/storages/batched_adds.d.ts +37 -0
- package/storages/batched_adds.js +73 -0
- package/storages/dataset.d.ts +114 -54
- package/storages/dataset.js +285 -144
- package/storages/index.d.ts +10 -8
- package/storages/index.js +8 -8
- package/storages/key_value_store.d.ts +185 -42
- package/storages/key_value_store.js +424 -151
- package/storages/key_value_store_codec.d.ts +32 -0
- package/storages/key_value_store_codec.js +113 -0
- package/storages/request_dedup_cache.d.ts +22 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +52 -116
- package/storages/request_list.js +159 -133
- package/storages/request_loader.d.ts +101 -0
- package/storages/request_loader.js +1 -0
- package/storages/request_manager.d.ts +33 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +97 -0
- package/storages/request_manager_tandem.js +197 -0
- package/storages/request_queue.d.ts +290 -47
- package/storages/request_queue.js +757 -216
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +37 -88
- package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +137 -143
- package/storages/storage_instance_manager.d.ts +87 -0
- package/storages/storage_instance_manager.js +256 -0
- package/storages/storage_stats.d.ts +48 -0
- package/storages/storage_stats.js +29 -0
- package/storages/throttling_request_manager.d.ts +216 -0
- package/storages/throttling_request_manager.js +453 -0
- package/storages/transaction.d.ts +252 -0
- package/storages/transaction.js +251 -0
- package/storages/utils.d.ts +58 -11
- package/storages/utils.js +64 -13
- package/system-info/cpu-info.d.ts +67 -0
- package/system-info/cpu-info.js +216 -0
- package/system-info/memory-info.d.ts +31 -0
- package/system-info/memory-info.js +115 -0
- package/system-info/ps-tree.d.ts +17 -0
- package/system-info/ps-tree.js +144 -0
- package/system-info/runtime.d.ts +14 -0
- package/system-info/runtime.js +80 -0
- package/typedefs.d.ts +0 -6
- package/typedefs.js +0 -1
- package/url.d.ts +9 -0
- package/url.js +11 -0
- package/validators.d.ts +8 -1
- package/validators.js +10 -3
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js.map +0 -1
- package/configuration.d.ts.map +0 -1
- package/configuration.js.map +0 -1
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js.map +0 -1
- package/crawlers/context_pipeline.d.ts.map +0 -1
- package/crawlers/context_pipeline.js.map +0 -1
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_utils.d.ts +0 -10
- package/crawlers/crawler_utils.d.ts.map +0 -1
- package/crawlers/crawler_utils.js +0 -12
- package/crawlers/crawler_utils.js.map +0 -1
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js.map +0 -1
- package/crawlers/internals/types.d.ts.map +0 -1
- package/crawlers/internals/types.js.map +0 -1
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js.map +0 -1
- package/errors.d.ts.map +0 -1
- package/errors.js.map +0 -1
- package/events/event_manager.d.ts.map +0 -1
- package/events/event_manager.js.map +0 -1
- package/events/index.d.ts.map +0 -1
- package/events/index.js.map +0 -1
- package/events/local_event_manager.d.ts.map +0 -1
- package/events/local_event_manager.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -134
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -33
- package/http_clients/base-http-client.js.map +0 -1
- package/http_clients/form-data-like.d.ts +0 -67
- package/http_clients/form-data-like.d.ts.map +0 -1
- package/http_clients/form-data-like.js +0 -5
- package/http_clients/form-data-like.js.map +0 -1
- package/http_clients/got-scraping-http-client.d.ts +0 -15
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -69
- package/http_clients/got-scraping-http-client.js.map +0 -1
- package/http_clients/index.d.ts +0 -3
- package/http_clients/index.d.ts.map +0 -1
- package/http_clients/index.js +0 -3
- package/http_clients/index.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/log.d.ts.map +0 -1
- package/log.js.map +0 -1
- package/proxy_configuration.d.ts.map +0 -1
- package/proxy_configuration.js.map +0 -1
- package/request.d.ts.map +0 -1
- package/request.js.map +0 -1
- package/router.d.ts.map +0 -1
- package/router.js.map +0 -1
- package/serialization.d.ts.map +0 -1
- package/serialization.js.map +0 -1
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js.map +0 -1
- package/session_pool/events.d.ts +0 -3
- package/session_pool/events.d.ts.map +0 -1
- package/session_pool/events.js +0 -3
- package/session_pool/events.js.map +0 -1
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js.map +0 -1
- package/storages/access_checking.d.ts +0 -13
- package/storages/access_checking.d.ts.map +0 -1
- package/storages/access_checking.js +0 -14
- package/storages/access_checking.js.map +0 -1
- package/storages/dataset.d.ts.map +0 -1
- package/storages/dataset.js.map +0 -1
- package/storages/index.d.ts.map +0 -1
- package/storages/index.js.map +0 -1
- package/storages/key_value_store.d.ts.map +0 -1
- package/storages/key_value_store.js.map +0 -1
- package/storages/request_list.d.ts.map +0 -1
- package/storages/request_list.js.map +0 -1
- package/storages/request_provider.d.ts +0 -308
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -555
- package/storages/request_provider.js.map +0 -1
- package/storages/request_queue.d.ts.map +0 -1
- package/storages/request_queue.js.map +0 -1
- package/storages/request_queue_v2.d.ts +0 -87
- package/storages/request_queue_v2.d.ts.map +0 -1
- package/storages/request_queue_v2.js +0 -438
- package/storages/request_queue_v2.js.map +0 -1
- package/storages/sitemap_request_list.d.ts.map +0 -1
- package/storages/sitemap_request_list.js.map +0 -1
- package/storages/storage_manager.d.ts +0 -58
- package/storages/storage_manager.d.ts.map +0 -1
- package/storages/storage_manager.js +0 -105
- package/storages/storage_manager.js.map +0 -1
- package/storages/utils.d.ts.map +0 -1
- package/storages/utils.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
- package/typedefs.d.ts.map +0 -1
- package/typedefs.js.map +0 -1
- package/validators.d.ts.map +0 -1
- package/validators.js.map +0 -1
|
@@ -1,25 +1,29 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BatchAddRequestsResult, Dictionary } from '@crawlee/types';
|
|
2
2
|
import { type RobotsTxtFile } from '@crawlee/utils';
|
|
3
3
|
import type { SetRequired } from 'type-fest';
|
|
4
|
-
import
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
reason: 'robotsTxt';
|
|
9
|
-
}) => Awaitable<void>;
|
|
4
|
+
import { Request } from '../request.js';
|
|
5
|
+
import type { IRequestManager } from '../storages/request_manager.js';
|
|
6
|
+
import type { AddRequestsBatchedOptions, AddRequestsBatchedResult, RequestQueueOperationOptions } from '../storages/request_queue.js';
|
|
7
|
+
import type { RequestTransform, SkippedRequestCallback, UrlPatternInput } from './shared.js';
|
|
10
8
|
export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
|
|
11
9
|
/** Limit the amount of actually enqueued URLs to this number. Useful for testing across the entire crawling scope. */
|
|
12
10
|
limit?: number;
|
|
13
11
|
/** An array of URLs to enqueue. */
|
|
14
12
|
urls?: readonly string[];
|
|
15
|
-
/** A request
|
|
16
|
-
|
|
13
|
+
/** A request manager to which the URLs will be enqueued. */
|
|
14
|
+
requestManager?: IRequestManager;
|
|
17
15
|
/** A CSS selector matching links to be enqueued. */
|
|
18
16
|
selector?: string;
|
|
19
17
|
/** Sets {@link Request.userData} for newly enqueued requests. */
|
|
20
18
|
userData?: Dictionary;
|
|
21
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Sets {@link Request.label} for newly enqueued requests.
|
|
21
|
+
*
|
|
22
|
+
* Can be overwritten by `transformRequestFunction`.
|
|
23
|
+
*/
|
|
22
24
|
label?: string;
|
|
25
|
+
/** Sets {@link Request.sessionId} for newly enqueued requests. */
|
|
26
|
+
sessionId?: string;
|
|
23
27
|
/**
|
|
24
28
|
* If set to `true`, tells the crawler to skip navigation and process the request directly.
|
|
25
29
|
* @default false
|
|
@@ -31,67 +35,37 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
|
|
|
31
35
|
*/
|
|
32
36
|
baseUrl?: string;
|
|
33
37
|
/**
|
|
34
|
-
* An array of
|
|
35
|
-
* containing glob pattern strings matching the URLs to be enqueued.
|
|
38
|
+
* An array of URL patterns that URLs must match to be enqueued.
|
|
36
39
|
*
|
|
37
|
-
*
|
|
38
|
-
* All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
|
|
39
|
-
*
|
|
40
|
-
* The matching is always case-insensitive.
|
|
41
|
-
* If you need case-sensitive matching, use `regexps` property directly.
|
|
42
|
-
*
|
|
43
|
-
* If `globs` is an empty array or `undefined`, and `regexps` are also not defined, then the function
|
|
44
|
-
* enqueues the links with the same subdomain.
|
|
45
|
-
*/
|
|
46
|
-
globs?: readonly GlobInput[];
|
|
47
|
-
/**
|
|
48
|
-
* An array of glob pattern strings, regexp patterns or plain objects
|
|
49
|
-
* containing patterns matching URLs that will **never** be enqueued.
|
|
50
|
-
*
|
|
51
|
-
* The plain objects must include either the `glob` property or the `regexp` property.
|
|
52
|
-
* All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
|
|
40
|
+
* Accepts glob pattern strings, `{ glob: string }` objects, `RegExp` instances, or `{ regexp: RegExp }` objects.
|
|
53
41
|
*
|
|
54
42
|
* Glob matching is always case-insensitive.
|
|
55
|
-
* If you need case-sensitive matching,
|
|
56
|
-
*/
|
|
57
|
-
exclude?: readonly (GlobInput | RegExpInput)[];
|
|
58
|
-
/**
|
|
59
|
-
* An array of regular expressions or plain objects
|
|
60
|
-
* containing regular expressions matching the URLs to be enqueued.
|
|
43
|
+
* If you need case-sensitive matching, use a `RegExp`.
|
|
61
44
|
*
|
|
62
|
-
* The
|
|
63
|
-
*
|
|
45
|
+
* The patterns are combined with the {@link EnqueueLinksOptions.strategy|`strategy`} using AND logic - a URL
|
|
46
|
+
* must match at least one `include` pattern **and** satisfy the strategy to be enqueued. To match URLs across
|
|
47
|
+
* hostnames, pass an explicit {@link EnqueueStrategy.All} strategy.
|
|
64
48
|
*
|
|
65
|
-
* If `
|
|
66
|
-
*
|
|
49
|
+
* If `undefined`, the links are enqueued based on the {@link EnqueueLinksOptions.strategy|`strategy`} alone.
|
|
50
|
+
* Passing an empty array is not allowed.
|
|
67
51
|
*/
|
|
68
|
-
|
|
52
|
+
include?: readonly UrlPatternInput[];
|
|
69
53
|
/**
|
|
70
|
-
*
|
|
71
|
-
* Please use `globs` or `regexps` instead.
|
|
72
|
-
*
|
|
73
|
-
* An array of {@link PseudoUrl} strings or plain objects
|
|
74
|
-
* containing {@link PseudoUrl} strings matching the URLs to be enqueued.
|
|
75
|
-
*
|
|
76
|
-
* The plain objects must include at least the `purl` property, which holds the pseudo-URL string.
|
|
77
|
-
* All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
|
|
78
|
-
*
|
|
79
|
-
* With a pseudo-URL string, the matching is always case-insensitive.
|
|
80
|
-
* If you need case-sensitive matching, use `regexps` property directly.
|
|
54
|
+
* An array of URL patterns. Matching URLs will **not** be enqueued.
|
|
81
55
|
*
|
|
82
|
-
*
|
|
83
|
-
* enqueues the links with the same subdomain.
|
|
56
|
+
* Accepts glob pattern strings, `{ glob: string }` objects, `RegExp` instances, or `{ regexp: RegExp }` objects.
|
|
84
57
|
*
|
|
85
|
-
*
|
|
58
|
+
* Glob matching is always case-insensitive.
|
|
59
|
+
* If you need case-sensitive matching, use a `RegExp`.
|
|
86
60
|
*/
|
|
87
|
-
|
|
61
|
+
exclude?: readonly UrlPatternInput[];
|
|
88
62
|
/**
|
|
89
|
-
*
|
|
90
|
-
* to remove
|
|
63
|
+
* After request options are filtered by `include`/`exclude` patterns, this function can be used
|
|
64
|
+
* to remove them or modify their contents such as `userData`, `payload` or, most importantly `uniqueKey`. This is useful
|
|
91
65
|
* when you need to enqueue multiple `Requests` to the queue that share the same URL, but differ in methods or payloads,
|
|
92
66
|
* or to dynamically update or create `userData`.
|
|
93
67
|
*
|
|
94
|
-
* For example: by adding `keepUrlFragment: true` to the
|
|
68
|
+
* For example: by adding `keepUrlFragment: true` to the request options, URL fragments will not be removed
|
|
95
69
|
* when `uniqueKey` is computed.
|
|
96
70
|
*
|
|
97
71
|
* **Example:**
|
|
@@ -105,9 +79,13 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
|
|
|
105
79
|
* }
|
|
106
80
|
* ```
|
|
107
81
|
*
|
|
108
|
-
* Note that `transformRequestFunction` has
|
|
109
|
-
*
|
|
110
|
-
*
|
|
82
|
+
* Note that `transformRequestFunction` has the highest priority and can overwrite
|
|
83
|
+
* the global `label` option.
|
|
84
|
+
*
|
|
85
|
+
* The function receives a {@link RequestOptions} object and can return either:
|
|
86
|
+
* - The modified {@link RequestOptions} object
|
|
87
|
+
* - `'unchanged'` to keep the original options as-is
|
|
88
|
+
* - A falsy value or `'skip'` to exclude the request from the queue
|
|
111
89
|
*/
|
|
112
90
|
transformRequestFunction?: RequestTransform;
|
|
113
91
|
/**
|
|
@@ -138,10 +116,21 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
|
|
|
138
116
|
* RobotsTxtFile instance for the current request that triggered the `enqueueLinks`.
|
|
139
117
|
* If provided, disallowed URLs will be ignored.
|
|
140
118
|
*/
|
|
141
|
-
robotsTxtFile?: RobotsTxtFile
|
|
119
|
+
robotsTxtFile?: Pick<RobotsTxtFile, 'isAllowed'>;
|
|
120
|
+
/**
|
|
121
|
+
* Mirrors {@link BasicCrawlerOptions.respectRobotsTxtFile}: pass `false` to disable filtering or
|
|
122
|
+
* `{ userAgent }` to evaluate rules for a specific user-agent. Defaults to `*` when
|
|
123
|
+
* {@link EnqueueLinksOptions.robotsTxtFile|`robotsTxtFile`} is provided.
|
|
124
|
+
*/
|
|
125
|
+
respectRobotsTxtFile?: boolean | {
|
|
126
|
+
userAgent?: string;
|
|
127
|
+
};
|
|
142
128
|
/**
|
|
143
129
|
* When a request is skipped for some reason, you can use this callback to act on it.
|
|
144
|
-
* This is currently fired
|
|
130
|
+
* This is currently fired for requests skipped
|
|
131
|
+
* 1. based on robots.txt file,
|
|
132
|
+
* 2. because they don't match enqueueLinks filters,
|
|
133
|
+
* 3. or because the maxRequestsPerCrawl limit has been reached
|
|
145
134
|
*/
|
|
146
135
|
onSkippedRequest?: SkippedRequestCallback;
|
|
147
136
|
}
|
|
@@ -200,17 +189,16 @@ export declare enum EnqueueStrategy {
|
|
|
200
189
|
* This function enqueues the urls provided to the {@link RequestQueue} provided. If you want to automatically find and enqueue links,
|
|
201
190
|
* you should use the context-aware `enqueueLinks` function provided on the crawler contexts.
|
|
202
191
|
*
|
|
203
|
-
* Optionally, the function allows you to filter the target links' URLs using an array of
|
|
204
|
-
* and override settings of the enqueued {@link Request} objects.
|
|
192
|
+
* Optionally, the function allows you to filter the target links' URLs using an array of glob or regexp patterns.
|
|
205
193
|
*
|
|
206
194
|
* **Example usage**
|
|
207
195
|
*
|
|
208
196
|
* ```javascript
|
|
209
197
|
* await enqueueLinks({
|
|
210
198
|
* urls: aListOfFoundUrls,
|
|
211
|
-
*
|
|
199
|
+
* requestManager,
|
|
212
200
|
* selector: 'a.product-detail',
|
|
213
|
-
*
|
|
201
|
+
* include: [
|
|
214
202
|
* 'https://www.example.com/handbags/*',
|
|
215
203
|
* 'https://www.example.com/purses/*'
|
|
216
204
|
* ],
|
|
@@ -220,7 +208,11 @@ export declare enum EnqueueStrategy {
|
|
|
220
208
|
* @param options All `enqueueLinks()` parameters are passed via an options object.
|
|
221
209
|
* @returns Promise that resolves to {@link BatchAddRequestsResult} object.
|
|
222
210
|
*/
|
|
223
|
-
export declare function enqueueLinks(options: SetRequired<EnqueueLinksOptions, '
|
|
211
|
+
export declare function enqueueLinks(options: SetRequired<Omit<EnqueueLinksOptions, 'requestManager'>, 'urls'> & {
|
|
212
|
+
requestManager: {
|
|
213
|
+
addRequestsBatched: (requests: Request<Dictionary>[], options: AddRequestsBatchedOptions) => Promise<AddRequestsBatchedResult>;
|
|
214
|
+
};
|
|
215
|
+
}): Promise<BatchAddRequestsResult>;
|
|
224
216
|
/**
|
|
225
217
|
* @internal
|
|
226
218
|
* This method helps resolve the baseUrl that will be used for filtering in {@link enqueueLinks}.
|
|
@@ -239,4 +231,3 @@ export interface ResolveBaseUrl {
|
|
|
239
231
|
originalRequestUrl: string;
|
|
240
232
|
finalRequestUrl?: string;
|
|
241
233
|
}
|
|
242
|
-
//# sourceMappingURL=enqueue_links.d.ts.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import ow from 'ow';
|
|
2
2
|
import { getDomain } from 'tldts';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
3
|
+
import { Request } from '../request.js';
|
|
4
|
+
import { applyRequestTransform, constructUrlPatternObjects, createRequestOptions, filterRequestOptionsByPatterns, } from './shared.js';
|
|
5
5
|
/**
|
|
6
6
|
* The different enqueueing strategies available.
|
|
7
7
|
*
|
|
@@ -58,17 +58,16 @@ export var EnqueueStrategy;
|
|
|
58
58
|
* This function enqueues the urls provided to the {@link RequestQueue} provided. If you want to automatically find and enqueue links,
|
|
59
59
|
* you should use the context-aware `enqueueLinks` function provided on the crawler contexts.
|
|
60
60
|
*
|
|
61
|
-
* Optionally, the function allows you to filter the target links' URLs using an array of
|
|
62
|
-
* and override settings of the enqueued {@link Request} objects.
|
|
61
|
+
* Optionally, the function allows you to filter the target links' URLs using an array of glob or regexp patterns.
|
|
63
62
|
*
|
|
64
63
|
* **Example usage**
|
|
65
64
|
*
|
|
66
65
|
* ```javascript
|
|
67
66
|
* await enqueueLinks({
|
|
68
67
|
* urls: aListOfFoundUrls,
|
|
69
|
-
*
|
|
68
|
+
* requestManager,
|
|
70
69
|
* selector: 'a.product-detail',
|
|
71
|
-
*
|
|
70
|
+
* include: [
|
|
72
71
|
* 'https://www.example.com/handbags/*',
|
|
73
72
|
* 'https://www.example.com/purses/*'
|
|
74
73
|
* ],
|
|
@@ -85,52 +84,33 @@ export async function enqueueLinks(options) {
|
|
|
85
84
|
'Check out our guide on how to use enqueueLinks() here: https://crawlee.dev/js/docs/examples/crawl-relative-links',
|
|
86
85
|
].join('\n'));
|
|
87
86
|
}
|
|
87
|
+
const urlPatternValidator = ow.any(ow.string, ow.regExp, ow.object.hasKeys('glob'), ow.object.hasKeys('regexp'));
|
|
88
88
|
ow(options, ow.object.exactShape({
|
|
89
89
|
urls: ow.array.ofType(ow.string),
|
|
90
|
-
|
|
90
|
+
requestManager: ow.object.hasKeys('addRequestsBatched'),
|
|
91
91
|
robotsTxtFile: ow.optional.object.hasKeys('isAllowed'),
|
|
92
|
+
respectRobotsTxtFile: ow.optional.any(ow.boolean, ow.object.exactShape({ userAgent: ow.optional.string })),
|
|
92
93
|
onSkippedRequest: ow.optional.function,
|
|
93
94
|
forefront: ow.optional.boolean,
|
|
94
95
|
skipNavigation: ow.optional.boolean,
|
|
96
|
+
sessionId: ow.optional.string,
|
|
95
97
|
limit: ow.optional.number,
|
|
96
98
|
selector: ow.optional.string,
|
|
97
99
|
baseUrl: ow.optional.string,
|
|
98
100
|
userData: ow.optional.object,
|
|
99
101
|
label: ow.optional.string,
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
exclude: ow.optional.array.ofType(ow.any(ow.string, ow.regExp, ow.object.hasKeys('glob'), ow.object.hasKeys('regexp'))),
|
|
103
|
-
regexps: ow.optional.array.ofType(ow.any(ow.regExp, ow.object.hasKeys('regexp'))),
|
|
102
|
+
include: ow.optional.array.minLength(1).ofType(urlPatternValidator),
|
|
103
|
+
exclude: ow.optional.array.ofType(urlPatternValidator),
|
|
104
104
|
transformRequestFunction: ow.optional.function,
|
|
105
105
|
strategy: ow.optional.string.oneOf(Object.values(EnqueueStrategy)),
|
|
106
106
|
waitForAllRequestsToBeAdded: ow.optional.boolean,
|
|
107
107
|
}));
|
|
108
|
-
const {
|
|
109
|
-
const urlExcludePatternObjects = [];
|
|
110
|
-
const urlPatternObjects = [];
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
urlExcludePatternObjects.push(...constructGlobObjectsFromGlobs([excl]));
|
|
115
|
-
}
|
|
116
|
-
else if (excl instanceof RegExp || 'regexp' in excl) {
|
|
117
|
-
urlExcludePatternObjects.push(...constructRegExpObjectsFromRegExps([excl]));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (pseudoUrls?.length) {
|
|
122
|
-
log.deprecated('`pseudoUrls` option is deprecated, use `globs` or `regexps` instead');
|
|
123
|
-
urlPatternObjects.push(...constructRegExpObjectsFromPseudoUrls(pseudoUrls));
|
|
124
|
-
}
|
|
125
|
-
if (globs?.length) {
|
|
126
|
-
urlPatternObjects.push(...constructGlobObjectsFromGlobs(globs));
|
|
127
|
-
}
|
|
128
|
-
if (regexps?.length) {
|
|
129
|
-
urlPatternObjects.push(...constructRegExpObjectsFromRegExps(regexps));
|
|
130
|
-
}
|
|
131
|
-
if (!urlPatternObjects.length) {
|
|
132
|
-
options.strategy ??= EnqueueStrategy.SameHostname;
|
|
133
|
-
}
|
|
108
|
+
const { requestManager, limit, urls, include, exclude, transformRequestFunction, forefront, waitForAllRequestsToBeAdded, robotsTxtFile, onSkippedRequest, } = options;
|
|
109
|
+
const urlExcludePatternObjects = exclude?.length ? constructUrlPatternObjects(exclude) : [];
|
|
110
|
+
const urlPatternObjects = include?.length ? constructUrlPatternObjects(include) : [];
|
|
111
|
+
// The strategy always applies, even when `include` patterns are provided - the two are AND-ed together
|
|
112
|
+
// (a URL must match an `include` pattern *and* satisfy the strategy). This mirrors crawlee-python.
|
|
113
|
+
options.strategy ??= EnqueueStrategy.SameHostname;
|
|
134
114
|
const enqueueStrategyPatterns = [];
|
|
135
115
|
if (options.baseUrl) {
|
|
136
116
|
const url = new URL(options.baseUrl);
|
|
@@ -167,44 +147,60 @@ export async function enqueueLinks(options) {
|
|
|
167
147
|
break;
|
|
168
148
|
}
|
|
169
149
|
}
|
|
150
|
+
async function reportSkippedRequests(skippedRequests, reason) {
|
|
151
|
+
if (onSkippedRequest && skippedRequests.length > 0) {
|
|
152
|
+
await Promise.all(skippedRequests.map((request) => {
|
|
153
|
+
return onSkippedRequest({
|
|
154
|
+
url: request.url,
|
|
155
|
+
reason: request.skippedReason ?? reason,
|
|
156
|
+
});
|
|
157
|
+
}));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
170
160
|
let requestOptions = createRequestOptions(urls, options);
|
|
171
|
-
if (robotsTxtFile) {
|
|
161
|
+
if (robotsTxtFile && options.respectRobotsTxtFile !== false) {
|
|
162
|
+
const robotsUserAgent = typeof options.respectRobotsTxtFile === 'object' ? (options.respectRobotsTxtFile.userAgent ?? '*') : '*';
|
|
172
163
|
const skippedRequests = [];
|
|
173
164
|
requestOptions = requestOptions.filter((request) => {
|
|
174
|
-
if (robotsTxtFile.isAllowed(request.url)) {
|
|
165
|
+
if (robotsTxtFile.isAllowed(request.url, robotsUserAgent)) {
|
|
175
166
|
return true;
|
|
176
167
|
}
|
|
177
168
|
skippedRequests.push(request);
|
|
178
169
|
return false;
|
|
179
170
|
});
|
|
180
|
-
|
|
181
|
-
await Promise.all(skippedRequests.map((request) => {
|
|
182
|
-
return onSkippedRequest({ url: request.url, reason: 'robotsTxt' });
|
|
183
|
-
}));
|
|
184
|
-
}
|
|
171
|
+
await reportSkippedRequests(skippedRequests, 'robotsTxt');
|
|
185
172
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
function createFilteredRequests() {
|
|
192
|
-
// No user provided patterns means we can skip an extra filtering step
|
|
173
|
+
async function createFilteredRequests() {
|
|
174
|
+
const skippedRequests = [];
|
|
175
|
+
// Step 1: Filter request options by exclude patterns, user include patterns, and strategy patterns.
|
|
176
|
+
let filteredOptions;
|
|
193
177
|
if (urlPatternObjects.length === 0) {
|
|
194
|
-
|
|
178
|
+
filteredOptions = filterRequestOptionsByPatterns(requestOptions, enqueueStrategyPatterns.length > 0 ? enqueueStrategyPatterns : undefined, urlExcludePatternObjects, options.strategy, (url) => skippedRequests.push(url));
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
// Filter by user patterns first (with exclude)
|
|
182
|
+
const afterUserPatterns = filterRequestOptionsByPatterns(requestOptions, urlPatternObjects, urlExcludePatternObjects, options.strategy, (url) => skippedRequests.push(url));
|
|
183
|
+
// ...then filter by the enqueue links strategy (making this an AND check)
|
|
184
|
+
filteredOptions = filterRequestOptionsByPatterns(afterUserPatterns, enqueueStrategyPatterns.length > 0 ? enqueueStrategyPatterns : undefined, [], options.strategy, (url) => skippedRequests.push(url));
|
|
195
185
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
186
|
+
await reportSkippedRequests(skippedRequests.map((url) => ({ url })), 'filters');
|
|
187
|
+
// Step 2: Apply transformRequestFunction on request options - it has the highest priority
|
|
188
|
+
if (transformRequestFunction) {
|
|
189
|
+
const skippedByTransform = [];
|
|
190
|
+
filteredOptions = applyRequestTransform(filteredOptions, transformRequestFunction, (r) => skippedByTransform.push(r));
|
|
191
|
+
await reportSkippedRequests(skippedByTransform, 'transform');
|
|
192
|
+
}
|
|
193
|
+
// Step 3: Create Request instances from the final request options
|
|
194
|
+
return filteredOptions.map((opts) => new Request(opts));
|
|
200
195
|
}
|
|
201
|
-
|
|
202
|
-
if (limit)
|
|
203
|
-
requests = requests.slice(0, limit);
|
|
204
|
-
const { addedRequests } = await requestQueue.addRequestsBatched(requests, {
|
|
196
|
+
const { addedRequests, requestsOverLimit } = await requestManager.addRequestsBatched(await createFilteredRequests(), {
|
|
205
197
|
forefront,
|
|
206
198
|
waitForAllRequestsToBeAdded,
|
|
199
|
+
maxNewRequests: limit,
|
|
207
200
|
});
|
|
201
|
+
if (requestsOverLimit?.length !== undefined && requestsOverLimit.length > 0) {
|
|
202
|
+
await reportSkippedRequests(requestsOverLimit.map((r) => ({ url: typeof r === 'string' ? r : r.url })), 'enqueueLimit');
|
|
203
|
+
}
|
|
208
204
|
return { processedRequests: addedRequests, unprocessedRequests: [] };
|
|
209
205
|
}
|
|
210
206
|
/**
|
|
@@ -242,9 +238,8 @@ export function resolveBaseUrlForEnqueueLinksFiltering({ enqueueStrategy, finalR
|
|
|
242
238
|
return originalUrlOrigin;
|
|
243
239
|
}
|
|
244
240
|
/**
|
|
245
|
-
* Internal function that changes the enqueue
|
|
241
|
+
* Internal function that changes the enqueue glob patterns to match both http and https
|
|
246
242
|
*/
|
|
247
243
|
function ignoreHttpSchema(pattern) {
|
|
248
244
|
return pattern.replace(/^(https?):\/\//, 'http{s,}://');
|
|
249
245
|
}
|
|
250
|
-
//# sourceMappingURL=enqueue_links.js.map
|
package/enqueue_links/index.d.ts
CHANGED
package/enqueue_links/index.js
CHANGED
|
@@ -1,33 +1,30 @@
|
|
|
1
|
+
import type { Awaitable } from '@crawlee/types';
|
|
1
2
|
import type { RequestOptions } from '../request.js';
|
|
2
|
-
import { Request } from '../request.js';
|
|
3
3
|
import type { EnqueueLinksOptions } from './enqueue_links.js';
|
|
4
|
-
export { tryAbsoluteURL } from '@crawlee/utils';
|
|
5
|
-
export
|
|
4
|
+
export { tryAbsoluteURL } from '@crawlee/utils/internal';
|
|
5
|
+
export interface UrlPatternObject {
|
|
6
6
|
glob?: string;
|
|
7
7
|
regexp?: RegExp;
|
|
8
|
-
}
|
|
9
|
-
export
|
|
10
|
-
purl: string;
|
|
11
|
-
} & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
|
|
12
|
-
export type PseudoUrlInput = string | PseudoUrlObject;
|
|
13
|
-
export type GlobObject = {
|
|
8
|
+
}
|
|
9
|
+
export interface GlobObject {
|
|
14
10
|
glob: string;
|
|
15
|
-
}
|
|
11
|
+
}
|
|
16
12
|
export type GlobInput = string | GlobObject;
|
|
17
|
-
export
|
|
13
|
+
export interface RegExpObject {
|
|
18
14
|
regexp: RegExp;
|
|
19
|
-
}
|
|
15
|
+
}
|
|
20
16
|
export type RegExpInput = RegExp | RegExpObject;
|
|
17
|
+
/** Unified URL pattern input — accepts glob strings, glob objects, RegExp instances, or regexp objects. */
|
|
18
|
+
export type UrlPatternInput = GlobInput | RegExpInput;
|
|
19
|
+
export type SkippedRequestReason = 'robotsTxt' | 'limit' | 'enqueueLimit' | 'filters' | 'transform' | 'redirect' | 'depth';
|
|
20
|
+
export type SkippedRequestCallback = (args: {
|
|
21
|
+
url: string;
|
|
22
|
+
reason: SkippedRequestReason;
|
|
23
|
+
}) => Awaitable<void>;
|
|
21
24
|
/**
|
|
22
25
|
* @ignore
|
|
23
26
|
*/
|
|
24
|
-
export declare function updateEnqueueLinksPatternCache(item: GlobInput | RegExpInput
|
|
25
|
-
/**
|
|
26
|
-
* Helper factory used in the `enqueueLinks()` and enqueueLinksByClickingElements() function
|
|
27
|
-
* to construct RegExps from PseudoUrl strings.
|
|
28
|
-
* @ignore
|
|
29
|
-
*/
|
|
30
|
-
export declare function constructRegExpObjectsFromPseudoUrls(pseudoUrls: readonly PseudoUrlInput[]): RegExpObject[];
|
|
27
|
+
export declare function updateEnqueueLinksPatternCache(item: GlobInput | RegExpInput, pattern: RegExpObject | GlobObject): void;
|
|
31
28
|
/**
|
|
32
29
|
* Helper factory used in the `enqueueLinks()` and enqueueLinksByClickingElements() function
|
|
33
30
|
* to construct Glob objects from Glob pattern strings.
|
|
@@ -45,23 +42,39 @@ export declare function validateGlobPattern(glob: string): string;
|
|
|
45
42
|
*/
|
|
46
43
|
export declare function constructRegExpObjectsFromRegExps(regexps: readonly RegExpInput[]): RegExpObject[];
|
|
47
44
|
/**
|
|
45
|
+
* Helper factory used in the `enqueueLinks()` function to construct UrlPatternObjects
|
|
46
|
+
* from a mixed array of glob strings, glob objects, RegExp instances, and regexp objects.
|
|
48
47
|
* @ignore
|
|
49
48
|
*/
|
|
50
|
-
export declare function
|
|
51
|
-
export declare function filterRequestsByPatterns(requests: Request[], patterns?: UrlPatternObject[]): Request[];
|
|
49
|
+
export declare function constructUrlPatternObjects(patterns: readonly UrlPatternInput[]): UrlPatternObject[];
|
|
52
50
|
/**
|
|
51
|
+
* Filters request options by URL patterns.
|
|
52
|
+
*
|
|
53
|
+
* When `includePatterns` is empty/undefined, all options pass through (only exclude filtering applies).
|
|
53
54
|
* @ignore
|
|
54
55
|
*/
|
|
55
|
-
export declare function
|
|
56
|
+
export declare function filterRequestOptionsByPatterns(requestOptions: RequestOptions[], includePatterns: UrlPatternObject[] | undefined, excludePatterns?: UrlPatternObject[], strategy?: EnqueueLinksOptions['strategy'], onSkippedUrl?: (url: string) => void): RequestOptions[];
|
|
56
57
|
/**
|
|
57
|
-
*
|
|
58
|
-
|
|
58
|
+
* @ignore
|
|
59
|
+
*/
|
|
60
|
+
export declare function createRequestOptions(sources: readonly (string | Record<string, unknown>)[], options?: Pick<EnqueueLinksOptions, 'label' | 'userData' | 'baseUrl' | 'skipNavigation' | 'sessionId' | 'strategy'>): RequestOptions[];
|
|
61
|
+
/**
|
|
62
|
+
* Takes a {@link RequestOptions} object and changes its attributes in a desired way. This user-function is used
|
|
63
|
+
* by {@link enqueueLinks} to modify request options before they are converted to {@link Request} instances.
|
|
59
64
|
*/
|
|
60
65
|
export interface RequestTransform {
|
|
61
66
|
/**
|
|
62
67
|
* @param original Request options to be modified.
|
|
63
|
-
* @returns The modified request options to enqueue
|
|
68
|
+
* @returns The modified request options to enqueue, `'unchanged'` to keep the original options as-is,
|
|
69
|
+
* or a falsy value / `'skip'` to exclude the request from the queue.
|
|
64
70
|
*/
|
|
65
|
-
(original: RequestOptions): RequestOptions | false | undefined | null;
|
|
71
|
+
(original: RequestOptions): RequestOptions | false | undefined | null | 'skip' | 'unchanged';
|
|
66
72
|
}
|
|
67
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Applies a {@link RequestTransform} function to a list of request options.
|
|
75
|
+
* Options for which the transform returns a falsy value are removed from the list.
|
|
76
|
+
* @param onSkipped Called with the original request options when the transform returns a falsy value (i.e. the request is skipped).
|
|
77
|
+
* @ignore
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
export declare function applyRequestTransform(requestOptions: RequestOptions[], transformFn: RequestTransform, onSkipped?: (requestOptions: RequestOptions) => void): RequestOptions[];
|