@crawlee/core 4.0.0-beta.15 → 4.0.0-beta.150
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 +14 -14
- package/autoscaling/autoscaled_pool.d.ts +73 -177
- package/autoscaling/autoscaled_pool.js +182 -329
- package/autoscaling/concurrency_system.d.ts +268 -0
- package/autoscaling/concurrency_system.js +364 -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 +98 -0
- package/autoscaling/load_signal.js +103 -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/storage_backend_load_signal.d.ts +56 -0
- package/autoscaling/storage_backend_load_signal.js +73 -0
- package/autoscaling/system_status.d.ts +67 -89
- 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 +3 -2
- package/cookie_utils.js +18 -7
- package/crawlers/context_pipeline.d.ts +10 -1
- package/crawlers/context_pipeline.js +31 -8
- package/crawlers/crawler_commons.d.ts +90 -126
- package/crawlers/crawler_commons.js +1 -108
- 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 +187 -64
- package/crawlers/statistics.js +354 -165
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/enqueue_links/enqueue_links.d.ts +61 -154
- package/enqueue_links/enqueue_links.js +40 -232
- package/enqueue_links/index.d.ts +0 -1
- package/enqueue_links/index.js +0 -1
- package/enqueue_links/shared.d.ts +55 -28
- package/enqueue_links/shared.js +116 -69
- package/errors.d.ts +72 -4
- package/errors.js +89 -5
- package/events/event_manager.d.ts +35 -9
- package/events/event_manager.js +12 -13
- 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 -5
- package/index.js +8 -4
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +77 -1
- package/log.js +104 -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 +44 -0
- package/memory-storage/memory-storage.js +160 -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 +106 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +199 -0
- package/memory-storage/resource-clients/request-queue.d.ts +77 -0
- package/memory-storage/resource-clients/request-queue.js +407 -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 +22 -128
- package/proxy_configuration.js +32 -144
- package/recoverable_state.d.ts +83 -51
- package/recoverable_state.js +163 -72
- package/request.d.ts +57 -16
- package/request.js +130 -69
- package/router.d.ts +193 -21
- package/router.js +188 -43
- package/serialization.d.ts +0 -1
- package/serialization.js +15 -15
- 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 +39 -89
- package/session_pool/session.js +102 -159
- package/session_pool/session_pool.d.ts +67 -91
- package/session_pool/session_pool.js +196 -187
- package/storages/batched_adds.d.ts +37 -0
- package/storages/batched_adds.js +73 -0
- package/storages/dataset.d.ts +109 -56
- package/storages/dataset.js +284 -149
- package/storages/index.d.ts +9 -9
- package/storages/index.js +7 -9
- package/storages/key_value_store.d.ts +183 -48
- package/storages/key_value_store.js +444 -171
- 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 +53 -115
- package/storages/request_list.js +194 -167
- package/storages/request_loader.d.ts +133 -0
- package/storages/request_loader.js +36 -0
- package/storages/request_manager.d.ts +109 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +55 -23
- package/storages/request_manager_tandem.js +139 -57
- package/storages/request_queue.d.ts +286 -48
- package/storages/request_queue.js +757 -218
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +43 -91
- package/storages/sitemap_request_loader.js +439 -0
- 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 +222 -0
- package/storages/throttling_request_manager.js +780 -0
- package/storages/transaction.d.ts +252 -0
- package/storages/transaction.js +251 -0
- package/storages/utils.d.ts +59 -11
- package/storages/utils.js +75 -15
- 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 +22 -18
- package/validators.js +13 -18
- 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 -140
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -40
- 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 -20
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -85
- 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/recoverable_state.d.ts.map +0 -1
- package/recoverable_state.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_list_adapter.d.ts +0 -58
- package/storages/request_list_adapter.d.ts.map +0 -1
- package/storages/request_list_adapter.js +0 -81
- package/storages/request_list_adapter.js.map +0 -1
- package/storages/request_manager_tandem.d.ts.map +0 -1
- package/storages/request_manager_tandem.js.map +0 -1
- package/storages/request_provider.d.ts +0 -371
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -585
- 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 +0 -430
- 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/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,231 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { EnqueueStrategy } from '@crawlee/utils';
|
|
2
2
|
import { getDomain } from 'tldts';
|
|
3
|
-
|
|
4
|
-
import { constructGlobObjectsFromGlobs, constructRegExpObjectsFromPseudoUrls, constructRegExpObjectsFromRegExps, createRequestOptions, createRequests, filterRequestsByPatterns, } from './shared.js';
|
|
5
|
-
/**
|
|
6
|
-
* The different enqueueing strategies available.
|
|
7
|
-
*
|
|
8
|
-
* Depending on the strategy you select, we will only check certain parts of the URLs found. Here is a diagram of each URL part and their name:
|
|
9
|
-
*
|
|
10
|
-
* ```md
|
|
11
|
-
* Protocol Domain
|
|
12
|
-
* ┌────┐ ┌─────────┐
|
|
13
|
-
* https://example.crawlee.dev/...
|
|
14
|
-
* │ └─────────────────┤
|
|
15
|
-
* │ Hostname │
|
|
16
|
-
* │ │
|
|
17
|
-
* └─────────────────────────┘
|
|
18
|
-
* Origin
|
|
19
|
-
*```
|
|
20
|
-
*
|
|
21
|
-
* - The `Protocol` is usually `http` or `https`
|
|
22
|
-
* - The `Domain` represents the path without any possible subdomains to a website. For example, `crawlee.dev` is the domain of `https://example.crawlee.dev/`
|
|
23
|
-
* - The `Hostname` is the full path to a website, including any subdomains. For example, `example.crawlee.dev` is the hostname of `https://example.crawlee.dev/`
|
|
24
|
-
* - The `Origin` is the combination of the `Protocol` and `Hostname`. For example, `https://example.crawlee.dev` is the origin of `https://example.crawlee.dev/`
|
|
25
|
-
*/
|
|
26
|
-
export var EnqueueStrategy;
|
|
27
|
-
(function (EnqueueStrategy) {
|
|
28
|
-
/**
|
|
29
|
-
* Matches any URLs found
|
|
30
|
-
*/
|
|
31
|
-
EnqueueStrategy["All"] = "all";
|
|
32
|
-
/**
|
|
33
|
-
* Matches any URLs that have the same hostname.
|
|
34
|
-
* For example, `https://wow.example.com/hello` will be matched for a base url of `https://wow.example.com/`, but
|
|
35
|
-
* `https://example.com/hello` will not be matched.
|
|
36
|
-
*
|
|
37
|
-
* > This strategy will match both `http` and `https` protocols regardless of the base URL protocol.
|
|
38
|
-
*/
|
|
39
|
-
EnqueueStrategy["SameHostname"] = "same-hostname";
|
|
40
|
-
/**
|
|
41
|
-
* Matches any URLs that have the same domain as the base URL.
|
|
42
|
-
* For example, `https://wow.an.example.com` and `https://example.com` will both be matched for a base url of
|
|
43
|
-
* `https://example.com`.
|
|
44
|
-
*
|
|
45
|
-
* > This strategy will match both `http` and `https` protocols regardless of the base URL protocol.
|
|
46
|
-
*/
|
|
47
|
-
EnqueueStrategy["SameDomain"] = "same-domain";
|
|
48
|
-
/**
|
|
49
|
-
* Matches any URLs that have the same hostname and protocol.
|
|
50
|
-
* For example, `https://wow.example.com/hello` will be matched for a base url of `https://wow.example.com/`, but
|
|
51
|
-
* `http://wow.example.com/hello` will not be matched.
|
|
52
|
-
*
|
|
53
|
-
* > This strategy will ensure the protocol of the base URL is the same as the protocol of the URL to be enqueued.
|
|
54
|
-
*/
|
|
55
|
-
EnqueueStrategy["SameOrigin"] = "same-origin";
|
|
56
|
-
})(EnqueueStrategy || (EnqueueStrategy = {}));
|
|
57
|
-
/**
|
|
58
|
-
* This function enqueues the urls provided to the {@link RequestQueue} provided. If you want to automatically find and enqueue links,
|
|
59
|
-
* you should use the context-aware `enqueueLinks` function provided on the crawler contexts.
|
|
60
|
-
*
|
|
61
|
-
* Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions
|
|
62
|
-
* and override settings of the enqueued {@link Request} objects.
|
|
63
|
-
*
|
|
64
|
-
* **Example usage**
|
|
65
|
-
*
|
|
66
|
-
* ```javascript
|
|
67
|
-
* await enqueueLinks({
|
|
68
|
-
* urls: aListOfFoundUrls,
|
|
69
|
-
* requestQueue,
|
|
70
|
-
* selector: 'a.product-detail',
|
|
71
|
-
* globs: [
|
|
72
|
-
* 'https://www.example.com/handbags/*',
|
|
73
|
-
* 'https://www.example.com/purses/*'
|
|
74
|
-
* ],
|
|
75
|
-
* });
|
|
76
|
-
* ```
|
|
77
|
-
*
|
|
78
|
-
* @param options All `enqueueLinks()` parameters are passed via an options object.
|
|
79
|
-
* @returns Promise that resolves to {@link BatchAddRequestsResult} object.
|
|
80
|
-
*/
|
|
81
|
-
export async function enqueueLinks(options) {
|
|
82
|
-
if (!options || Object.keys(options).length === 0) {
|
|
83
|
-
throw new RangeError([
|
|
84
|
-
'enqueueLinks() was called without the required options. You can only do that when you use the `crawlingContext.enqueueLinks()` method in request handlers.',
|
|
85
|
-
'Check out our guide on how to use enqueueLinks() here: https://crawlee.dev/js/docs/examples/crawl-relative-links',
|
|
86
|
-
].join('\n'));
|
|
87
|
-
}
|
|
88
|
-
ow(options, ow.object.exactShape({
|
|
89
|
-
urls: ow.array.ofType(ow.string),
|
|
90
|
-
requestQueue: ow.object.hasKeys('addRequestsBatched'),
|
|
91
|
-
robotsTxtFile: ow.optional.object.hasKeys('isAllowed'),
|
|
92
|
-
onSkippedRequest: ow.optional.function,
|
|
93
|
-
forefront: ow.optional.boolean,
|
|
94
|
-
skipNavigation: ow.optional.boolean,
|
|
95
|
-
limit: ow.optional.number,
|
|
96
|
-
selector: ow.optional.string,
|
|
97
|
-
baseUrl: ow.optional.string,
|
|
98
|
-
userData: ow.optional.object,
|
|
99
|
-
label: ow.optional.string,
|
|
100
|
-
pseudoUrls: ow.optional.array.ofType(ow.any(ow.string, ow.object.hasKeys('purl'))),
|
|
101
|
-
globs: ow.optional.array.ofType(ow.any(ow.string, ow.object.hasKeys('glob'))),
|
|
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'))),
|
|
104
|
-
transformRequestFunction: ow.optional.function,
|
|
105
|
-
strategy: ow.optional.string.oneOf(Object.values(EnqueueStrategy)),
|
|
106
|
-
waitForAllRequestsToBeAdded: ow.optional.boolean,
|
|
107
|
-
}));
|
|
108
|
-
const { requestQueue, limit, urls, pseudoUrls, exclude, globs, regexps, transformRequestFunction, forefront, waitForAllRequestsToBeAdded, robotsTxtFile, onSkippedRequest, } = options;
|
|
109
|
-
const urlExcludePatternObjects = [];
|
|
110
|
-
const urlPatternObjects = [];
|
|
111
|
-
if (exclude?.length) {
|
|
112
|
-
for (const excl of exclude) {
|
|
113
|
-
if (typeof excl === 'string' || 'glob' in excl) {
|
|
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
|
-
}
|
|
134
|
-
const enqueueStrategyPatterns = [];
|
|
135
|
-
if (options.baseUrl) {
|
|
136
|
-
const url = new URL(options.baseUrl);
|
|
137
|
-
switch (options.strategy) {
|
|
138
|
-
case EnqueueStrategy.SameHostname:
|
|
139
|
-
// We need to get the origin of the passed in domain in the event someone sets baseUrl
|
|
140
|
-
// to an url like https://example.com/deep/default/path and one of the found urls is an
|
|
141
|
-
// absolute relative path (/path/to/page)
|
|
142
|
-
enqueueStrategyPatterns.push({ glob: ignoreHttpSchema(`${url.origin}/**`) });
|
|
143
|
-
break;
|
|
144
|
-
case EnqueueStrategy.SameDomain: {
|
|
145
|
-
// Get the actual hostname from the base url
|
|
146
|
-
const baseUrlHostname = getDomain(url.hostname, { mixedInputs: false });
|
|
147
|
-
if (baseUrlHostname) {
|
|
148
|
-
// We have a hostname, so we can use it to match all links on the page that point to it and any subdomains of it
|
|
149
|
-
url.hostname = baseUrlHostname;
|
|
150
|
-
enqueueStrategyPatterns.push({ glob: ignoreHttpSchema(`${url.origin.replace(baseUrlHostname, `*.${baseUrlHostname}`)}/**`) }, { glob: ignoreHttpSchema(`${url.origin}/**`) });
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
// We don't have a hostname (can happen for ips for instance), so reproduce the same behavior
|
|
154
|
-
// as SameDomainAndSubdomain
|
|
155
|
-
enqueueStrategyPatterns.push({ glob: ignoreHttpSchema(`${url.origin}/**`) });
|
|
156
|
-
}
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
case EnqueueStrategy.SameOrigin: {
|
|
160
|
-
// The same behavior as SameHostname, but respecting the protocol of the URL
|
|
161
|
-
enqueueStrategyPatterns.push({ glob: `${url.origin}/**` });
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
case EnqueueStrategy.All:
|
|
165
|
-
default:
|
|
166
|
-
enqueueStrategyPatterns.push({ glob: `http{s,}://**` });
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
async function reportSkippedRequests(skippedRequests, reason) {
|
|
171
|
-
if (onSkippedRequest && skippedRequests.length > 0) {
|
|
172
|
-
await Promise.all(skippedRequests.map((request) => {
|
|
173
|
-
return onSkippedRequest({
|
|
174
|
-
url: request.url,
|
|
175
|
-
reason: request.skippedReason ?? reason,
|
|
176
|
-
});
|
|
177
|
-
}));
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
let requestOptions = createRequestOptions(urls, options);
|
|
181
|
-
if (robotsTxtFile) {
|
|
182
|
-
const skippedRequests = [];
|
|
183
|
-
requestOptions = requestOptions.filter((request) => {
|
|
184
|
-
if (robotsTxtFile.isAllowed(request.url)) {
|
|
185
|
-
return true;
|
|
186
|
-
}
|
|
187
|
-
skippedRequests.push(request);
|
|
188
|
-
return false;
|
|
189
|
-
});
|
|
190
|
-
await reportSkippedRequests(skippedRequests, 'robotsTxt');
|
|
191
|
-
}
|
|
192
|
-
if (transformRequestFunction) {
|
|
193
|
-
const skippedRequests = [];
|
|
194
|
-
requestOptions = requestOptions
|
|
195
|
-
.map((request) => {
|
|
196
|
-
const transformedRequest = transformRequestFunction(request);
|
|
197
|
-
if (!transformedRequest) {
|
|
198
|
-
skippedRequests.push(request);
|
|
199
|
-
}
|
|
200
|
-
return transformedRequest;
|
|
201
|
-
})
|
|
202
|
-
.filter((r) => Boolean(r));
|
|
203
|
-
await reportSkippedRequests(skippedRequests, 'filters');
|
|
204
|
-
}
|
|
205
|
-
async function createFilteredRequests() {
|
|
206
|
-
const skippedRequests = [];
|
|
207
|
-
// No user provided patterns means we can skip an extra filtering step
|
|
208
|
-
if (urlPatternObjects.length === 0) {
|
|
209
|
-
return createRequests(requestOptions, enqueueStrategyPatterns, urlExcludePatternObjects, options.strategy, (url) => skippedRequests.push(url));
|
|
210
|
-
}
|
|
211
|
-
// Generate requests based on the user patterns first
|
|
212
|
-
const generatedRequestsFromUserFilters = createRequests(requestOptions, urlPatternObjects, urlExcludePatternObjects, options.strategy, (url) => skippedRequests.push(url));
|
|
213
|
-
// ...then filter them by the enqueue links strategy (making this an AND check)
|
|
214
|
-
const filtered = filterRequestsByPatterns(generatedRequestsFromUserFilters, enqueueStrategyPatterns, (url) => skippedRequests.push(url));
|
|
215
|
-
await reportSkippedRequests(skippedRequests.map((url) => ({ url })), 'filters');
|
|
216
|
-
return filtered;
|
|
217
|
-
}
|
|
218
|
-
let requests = await createFilteredRequests();
|
|
219
|
-
if (typeof limit === 'number' && limit < requests.length) {
|
|
220
|
-
await reportSkippedRequests(requests.slice(limit), 'enqueueLimit');
|
|
221
|
-
requests = requests.slice(0, limit);
|
|
222
|
-
}
|
|
223
|
-
const { addedRequests } = await requestQueue.addRequestsBatched(requests, {
|
|
224
|
-
forefront,
|
|
225
|
-
waitForAllRequestsToBeAdded,
|
|
226
|
-
});
|
|
227
|
-
return { processedRequests: addedRequests, unprocessedRequests: [] };
|
|
228
|
-
}
|
|
3
|
+
export { EnqueueStrategy };
|
|
229
4
|
/**
|
|
230
5
|
* @internal
|
|
231
6
|
* This method helps resolve the baseUrl that will be used for filtering in {@link enqueueLinks}.
|
|
@@ -246,24 +21,57 @@ export function resolveBaseUrlForEnqueueLinksFiltering({ enqueueStrategy, finalR
|
|
|
246
21
|
return finalUrlOrigin;
|
|
247
22
|
}
|
|
248
23
|
// If the user wants to ensure the same domain is accessed, regardless of subdomains, we check to ensure the domains match
|
|
249
|
-
//
|
|
250
|
-
// which is the intended behavior (since we went off domain)
|
|
24
|
+
// If they don't (we went off domain via a redirect), we keep filtering against the original domain - returning undefined would disable the filtering entirely
|
|
251
25
|
if (enqueueStrategy === EnqueueStrategy.SameDomain) {
|
|
252
26
|
const originalHostname = getDomain(originalUrlOrigin, { mixedInputs: false });
|
|
253
27
|
const finalHostname = getDomain(finalUrlOrigin, { mixedInputs: false });
|
|
254
28
|
if (originalHostname === finalHostname) {
|
|
255
29
|
return finalUrlOrigin;
|
|
256
30
|
}
|
|
257
|
-
return
|
|
31
|
+
return originalUrlOrigin;
|
|
258
32
|
}
|
|
259
33
|
// Always enqueue urls that are from the same origin in all other cases, as the filtering happens on the original request url, even if there was a redirect
|
|
260
34
|
// before actually finding the urls
|
|
261
35
|
return originalUrlOrigin;
|
|
262
36
|
}
|
|
263
37
|
/**
|
|
264
|
-
*
|
|
38
|
+
* @internal
|
|
39
|
+
* Builds the glob patterns a URL must match to satisfy the given enqueue `strategy`, anchored at `baseUrl`.
|
|
40
|
+
*/
|
|
41
|
+
export function buildEnqueueStrategyPatterns(baseUrl, strategy) {
|
|
42
|
+
const url = new URL(baseUrl);
|
|
43
|
+
switch (strategy) {
|
|
44
|
+
case EnqueueStrategy.SameHostname:
|
|
45
|
+
// We need to get the origin of the passed in domain in the event someone sets baseUrl
|
|
46
|
+
// to an url like https://example.com/deep/default/path and one of the found urls is an
|
|
47
|
+
// absolute relative path (/path/to/page)
|
|
48
|
+
return [{ glob: ignoreHttpSchema(`${url.origin}/**`) }];
|
|
49
|
+
case EnqueueStrategy.SameDomain: {
|
|
50
|
+
// Get the actual hostname from the base url
|
|
51
|
+
const baseUrlHostname = getDomain(url.hostname, { mixedInputs: false });
|
|
52
|
+
if (baseUrlHostname) {
|
|
53
|
+
// We have a hostname, so we can use it to match all links on the page that point to it and any subdomains of it
|
|
54
|
+
url.hostname = baseUrlHostname;
|
|
55
|
+
return [
|
|
56
|
+
{ glob: ignoreHttpSchema(`${url.origin.replace(baseUrlHostname, `*.${baseUrlHostname}`)}/**`) },
|
|
57
|
+
{ glob: ignoreHttpSchema(`${url.origin}/**`) },
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
// We don't have a hostname (can happen for ips for instance), so reproduce the same behavior
|
|
61
|
+
// as SameDomainAndSubdomain
|
|
62
|
+
return [{ glob: ignoreHttpSchema(`${url.origin}/**`) }];
|
|
63
|
+
}
|
|
64
|
+
case EnqueueStrategy.SameOrigin:
|
|
65
|
+
// The same behavior as SameHostname, but respecting the protocol of the URL
|
|
66
|
+
return [{ glob: `${url.origin}/**` }];
|
|
67
|
+
case EnqueueStrategy.All:
|
|
68
|
+
default:
|
|
69
|
+
return [{ glob: `http{s,}://**` }];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Internal function that changes the enqueue glob patterns to match both http and https
|
|
265
74
|
*/
|
|
266
75
|
function ignoreHttpSchema(pattern) {
|
|
267
76
|
return pattern.replace(/^(https?):\/\//, 'http{s,}://');
|
|
268
77
|
}
|
|
269
|
-
//# sourceMappingURL=enqueue_links.js.map
|
package/enqueue_links/index.d.ts
CHANGED
package/enqueue_links/index.js
CHANGED
|
@@ -1,39 +1,43 @@
|
|
|
1
|
-
import type { Awaitable } from '@crawlee/types';
|
|
2
|
-
import
|
|
1
|
+
import type { Awaitable, Dictionary } from '@crawlee/types';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import type { RequestOptions, Source } from '../request.js';
|
|
3
4
|
import { Request } from '../request.js';
|
|
4
|
-
import type {
|
|
5
|
-
export
|
|
6
|
-
export type UrlPatternObject = {
|
|
5
|
+
import type { EnqueueStrategyOption } from './enqueue_links.js';
|
|
6
|
+
export interface UrlPatternObject {
|
|
7
7
|
glob?: string;
|
|
8
8
|
regexp?: RegExp;
|
|
9
|
-
}
|
|
10
|
-
export
|
|
11
|
-
purl: string;
|
|
12
|
-
} & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
|
|
13
|
-
export type PseudoUrlInput = string | PseudoUrlObject;
|
|
14
|
-
export type GlobObject = {
|
|
9
|
+
}
|
|
10
|
+
export interface GlobObject {
|
|
15
11
|
glob: string;
|
|
16
|
-
}
|
|
12
|
+
}
|
|
17
13
|
export type GlobInput = string | GlobObject;
|
|
18
|
-
export
|
|
14
|
+
export interface RegExpObject {
|
|
19
15
|
regexp: RegExp;
|
|
20
|
-
}
|
|
16
|
+
}
|
|
21
17
|
export type RegExpInput = RegExp | RegExpObject;
|
|
22
|
-
|
|
18
|
+
/** Unified URL pattern input — accepts glob strings, glob objects, RegExp instances, or regexp objects. */
|
|
19
|
+
export type UrlPatternInput = GlobInput | RegExpInput;
|
|
20
|
+
/**
|
|
21
|
+
* Accepts one {@link UrlPatternInput} — a glob string, a RegExp instance, or a `{ glob }` / `{ regexp }` object.
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export declare const urlPatternSchema: z.ZodType<UrlPatternInput>;
|
|
25
|
+
export type SkippedRequestReason = 'robotsTxt' | 'limit' | 'enqueueLimit' | 'filters' | 'transform' | 'redirect' | 'depth';
|
|
23
26
|
export type SkippedRequestCallback = (args: {
|
|
24
|
-
|
|
27
|
+
request: Request;
|
|
25
28
|
reason: SkippedRequestReason;
|
|
26
29
|
}) => Awaitable<void>;
|
|
27
30
|
/**
|
|
31
|
+
* Builds the `{ request, reason }` argument passed to a {@link SkippedRequestCallback}, constructing the
|
|
32
|
+
* `Request` lazily on first access to `request` (and caching it) since most skips are never observed by a
|
|
33
|
+
* real callback and building a `Request` isn't free.
|
|
28
34
|
* @ignore
|
|
29
35
|
*/
|
|
30
|
-
export declare function
|
|
36
|
+
export declare function createSkippedRequestArgs(source: string | Source, reason: SkippedRequestReason): Parameters<SkippedRequestCallback>[0];
|
|
31
37
|
/**
|
|
32
|
-
* Helper factory used in the `enqueueLinks()` and enqueueLinksByClickingElements() function
|
|
33
|
-
* to construct RegExps from PseudoUrl strings.
|
|
34
38
|
* @ignore
|
|
35
39
|
*/
|
|
36
|
-
export declare function
|
|
40
|
+
export declare function updateEnqueueLinksPatternCache(item: GlobInput | RegExpInput, pattern: RegExpObject | GlobObject): void;
|
|
37
41
|
/**
|
|
38
42
|
* Helper factory used in the `enqueueLinks()` and enqueueLinksByClickingElements() function
|
|
39
43
|
* to construct Glob objects from Glob pattern strings.
|
|
@@ -51,23 +55,46 @@ export declare function validateGlobPattern(glob: string): string;
|
|
|
51
55
|
*/
|
|
52
56
|
export declare function constructRegExpObjectsFromRegExps(regexps: readonly RegExpInput[]): RegExpObject[];
|
|
53
57
|
/**
|
|
58
|
+
* Helper factory used in the `enqueueLinks()` function to construct UrlPatternObjects
|
|
59
|
+
* from a mixed array of glob strings, glob objects, RegExp instances, and regexp objects.
|
|
54
60
|
* @ignore
|
|
55
61
|
*/
|
|
56
|
-
export declare function
|
|
57
|
-
export declare function filterRequestsByPatterns(requests: Request[], patterns?: UrlPatternObject[], onSkippedUrl?: (url: string) => void): Request[];
|
|
62
|
+
export declare function constructUrlPatternObjects(patterns: readonly UrlPatternInput[]): UrlPatternObject[];
|
|
58
63
|
/**
|
|
64
|
+
* Filters request options by URL patterns.
|
|
65
|
+
*
|
|
66
|
+
* When `includePatterns` is empty/undefined, all options pass through (only exclude filtering applies).
|
|
59
67
|
* @ignore
|
|
60
68
|
*/
|
|
61
|
-
export declare function
|
|
69
|
+
export declare function filterRequestOptionsByPatterns(requestOptions: RequestOptions[], includePatterns: UrlPatternObject[] | undefined, excludePatterns?: UrlPatternObject[], strategy?: EnqueueStrategyOption, onSkippedRequestOptions?: (options: RequestOptions) => void): RequestOptions[];
|
|
62
70
|
/**
|
|
63
|
-
*
|
|
64
|
-
|
|
71
|
+
* @ignore
|
|
72
|
+
*/
|
|
73
|
+
export declare function createRequestOptions(sources: readonly (string | Record<string, unknown>)[], options?: {
|
|
74
|
+
label?: string;
|
|
75
|
+
userData?: Dictionary;
|
|
76
|
+
baseUrl?: string;
|
|
77
|
+
skipNavigation?: boolean;
|
|
78
|
+
sessionId?: string;
|
|
79
|
+
strategy?: EnqueueStrategyOption;
|
|
80
|
+
}): RequestOptions[];
|
|
81
|
+
/**
|
|
82
|
+
* Takes a {@link RequestOptions} object and changes its attributes in a desired way. This user-function is used
|
|
83
|
+
* by {@link enqueueLinks} to modify request options before they are converted to {@link Request} instances.
|
|
65
84
|
*/
|
|
66
85
|
export interface RequestTransform {
|
|
67
86
|
/**
|
|
68
87
|
* @param original Request options to be modified.
|
|
69
|
-
* @returns The modified request options to enqueue
|
|
88
|
+
* @returns The modified request options to enqueue, `'unchanged'` to keep the original options as-is,
|
|
89
|
+
* or a falsy value / `'skip'` to exclude the request from the queue.
|
|
70
90
|
*/
|
|
71
|
-
(original: RequestOptions): RequestOptions | false | undefined | null;
|
|
91
|
+
(original: RequestOptions): RequestOptions | false | undefined | null | 'skip' | 'unchanged';
|
|
72
92
|
}
|
|
73
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Applies a {@link RequestTransform} function to a list of request options.
|
|
95
|
+
* Options for which the transform returns a falsy value are removed from the list.
|
|
96
|
+
* @param onSkipped Called with the original request options when the transform returns a falsy value (i.e. the request is skipped).
|
|
97
|
+
* @ignore
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
export declare function applyRequestTransform(requestOptions: RequestOptions[], transformFn: RequestTransform, onSkipped?: (requestOptions: RequestOptions) => void): RequestOptions[];
|