@crawlee/core 4.0.0-beta.19 → 4.0.0-beta.191
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/byte_utils.d.ts +17 -0
- package/byte_utils.js +42 -0
- package/configuration.d.ts +90 -226
- package/configuration.js +164 -225
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/errors.d.ts +40 -19
- package/errors.js +53 -28
- package/events/event_manager.d.ts +35 -9
- package/events/event_manager.js +12 -13
- package/events/index.d.ts +1 -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/events/system_info.d.ts +38 -0
- package/events/system_info.js +1 -0
- package/index.d.ts +7 -11
- package/index.js +8 -10
- package/internal.d.ts +8 -0
- package/internal.js +9 -0
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +77 -1
- package/log.js +134 -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 +414 -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 +18 -19
- package/proxy_configuration.d.ts +22 -134
- package/proxy_configuration.js +34 -146
- package/recoverable_state.d.ts +106 -52
- package/recoverable_state.js +200 -72
- package/request.d.ts +48 -93
- package/request.js +116 -217
- package/serialization.d.ts +0 -1
- package/serialization.js +15 -15
- package/service_locator.d.ts +165 -0
- package/service_locator.js +254 -0
- package/storages/batched_adds.d.ts +37 -0
- package/storages/batched_adds.js +73 -0
- package/storages/dataset.d.ts +114 -56
- package/storages/dataset.js +292 -149
- package/storages/index.d.ts +7 -9
- package/storages/index.js +5 -9
- package/storages/key_value_store.d.ts +187 -50
- package/storages/key_value_store.js +453 -172
- 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 +54 -129
- package/storages/request_list.js +272 -245
- package/storages/request_loader.d.ts +127 -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 +50 -23
- package/storages/request_manager_tandem.js +132 -57
- package/storages/request_queue.d.ts +286 -48
- package/storages/request_queue.js +756 -218
- 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/transaction.d.ts +260 -0
- package/storages/transaction.js +287 -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 +23 -18
- package/validators.js +14 -18
- package/autoscaling/autoscaled_pool.d.ts +0 -300
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js +0 -533
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts +0 -4
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js +0 -4
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts +0 -189
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js +0 -285
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts +0 -181
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js +0 -169
- 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 +0 -43
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js +0 -111
- package/cookie_utils.js.map +0 -1
- package/crawlers/context_pipeline.d.ts +0 -61
- package/crawlers/context_pipeline.d.ts.map +0 -1
- package/crawlers/context_pipeline.js +0 -99
- package/crawlers/context_pipeline.js.map +0 -1
- package/crawlers/crawler_commons.d.ts +0 -195
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js +0 -108
- 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 +0 -60
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js +0 -118
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts +0 -55
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js +0 -309
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts +0 -7
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js +0 -7
- package/crawlers/index.js.map +0 -1
- package/crawlers/internals/types.d.ts +0 -8
- package/crawlers/internals/types.d.ts.map +0 -1
- package/crawlers/internals/types.js +0 -2
- package/crawlers/internals/types.js.map +0 -1
- package/crawlers/statistics.d.ts +0 -204
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js +0 -346
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts +0 -249
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js +0 -269
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts +0 -3
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js +0 -3
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts +0 -73
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js +0 -212
- 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 -91
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -44
- 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 -102
- 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 +0 -134
- package/router.d.ts.map +0 -1
- package/router.js +0 -164
- 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 +0 -4
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js +0 -4
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts +0 -8
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js +0 -12
- 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 +0 -6
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js +0 -6
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts +0 -203
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js +0 -278
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts +0 -258
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js +0 -387
- 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 +0 -245
- 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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/core",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.191",
|
|
4
4
|
"description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"type": "module",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": "./index.js",
|
|
11
|
+
"./internal": "./internal.js",
|
|
11
12
|
"./package.json": "./package.json"
|
|
12
13
|
},
|
|
13
14
|
"keywords": [
|
|
@@ -38,7 +39,7 @@
|
|
|
38
39
|
},
|
|
39
40
|
"homepage": "https://crawlee.dev",
|
|
40
41
|
"scripts": {
|
|
41
|
-
"build": "
|
|
42
|
+
"build": "pnpm clean && pnpm compile && pnpm copy",
|
|
42
43
|
"clean": "rimraf ./dist",
|
|
43
44
|
"compile": "tsc -p tsconfig.build.json",
|
|
44
45
|
"copy": "tsx ../../scripts/copy.ts"
|
|
@@ -47,28 +48,26 @@
|
|
|
47
48
|
"access": "public"
|
|
48
49
|
},
|
|
49
50
|
"dependencies": {
|
|
50
|
-
"@apify/consts": "^
|
|
51
|
-
"@apify/datastructures": "^
|
|
52
|
-
"@apify/log": "^
|
|
53
|
-
"@apify/
|
|
54
|
-
"@apify/
|
|
55
|
-
"@
|
|
56
|
-
"@crawlee/
|
|
57
|
-
"@crawlee/types": "4.0.0-beta.
|
|
58
|
-
"@crawlee/utils": "4.0.0-beta.
|
|
51
|
+
"@apify/consts": "^3.0.1",
|
|
52
|
+
"@apify/datastructures": "^3.0.1",
|
|
53
|
+
"@apify/log": "^3.0.1",
|
|
54
|
+
"@apify/timeout": "^1.0.1",
|
|
55
|
+
"@apify/utilities": "^3.0.1",
|
|
56
|
+
"@crawlee/fs-storage": "4.0.0-beta.191",
|
|
57
|
+
"@crawlee/http-client": "4.0.0-beta.191",
|
|
58
|
+
"@crawlee/types": "4.0.0-beta.191",
|
|
59
|
+
"@crawlee/utils": "4.0.0-beta.191",
|
|
59
60
|
"@sapphire/async-queue": "^1.5.5",
|
|
61
|
+
"@standard-schema/spec": "^1.0.0",
|
|
60
62
|
"@vladfrangu/async_event_emitter": "^2.4.6",
|
|
63
|
+
"content-type": "^1.0.5",
|
|
61
64
|
"csv-stringify": "^6.5.2",
|
|
62
|
-
"fs-extra": "^11.3.0",
|
|
63
|
-
"got-scraping": "^4.1.1",
|
|
64
65
|
"json5": "^2.2.3",
|
|
65
|
-
"
|
|
66
|
-
"ow": "^2.0.0",
|
|
66
|
+
"mime-types": "^3.0.1",
|
|
67
67
|
"stream-json": "^1.9.1",
|
|
68
|
-
"tldts": "^7.0.6",
|
|
69
|
-
"tough-cookie": "^6.0.0",
|
|
70
68
|
"tslib": "^2.8.1",
|
|
71
|
-
"type-fest": "^4.41.0"
|
|
69
|
+
"type-fest": "^4.41.0",
|
|
70
|
+
"zod": "^4.5.4"
|
|
72
71
|
},
|
|
73
72
|
"lerna": {
|
|
74
73
|
"command": {
|
|
@@ -77,5 +76,5 @@
|
|
|
77
76
|
}
|
|
78
77
|
}
|
|
79
78
|
},
|
|
80
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "efc4b5504e9aa6d529cf0eb218152a3bfad6ebf1"
|
|
81
80
|
}
|
package/proxy_configuration.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ProxyInfo } from '@crawlee/types';
|
|
2
2
|
export interface ProxyConfigurationFunction {
|
|
3
|
-
(
|
|
4
|
-
request?: Request;
|
|
5
|
-
}): string | null | Promise<string | null>;
|
|
3
|
+
(): string | null | Promise<string | null>;
|
|
6
4
|
}
|
|
7
5
|
type UrlList = (string | null)[];
|
|
8
6
|
export interface ProxyConfigurationOptions {
|
|
@@ -13,117 +11,29 @@ export interface ProxyConfigurationOptions {
|
|
|
13
11
|
*/
|
|
14
12
|
proxyUrls?: UrlList;
|
|
15
13
|
/**
|
|
16
|
-
* Custom function that allows you to generate the new proxy URL dynamically.
|
|
14
|
+
* Custom function that allows you to generate the new proxy URL dynamically.
|
|
17
15
|
* Can return either stringified proxy URL or `null` if the proxy should not be used. Can be asynchronous.
|
|
18
16
|
*
|
|
19
17
|
* This function is used to generate the URL when {@link ProxyConfiguration.newUrl} or {@link ProxyConfiguration.newProxyInfo} is called.
|
|
20
18
|
*/
|
|
21
19
|
newUrlFunction?: ProxyConfigurationFunction;
|
|
22
|
-
/**
|
|
23
|
-
* An array of custom proxy URLs to be rotated stratified in tiers.
|
|
24
|
-
* This is a more advanced version of `proxyUrls` that allows you to define a hierarchy of proxy URLs
|
|
25
|
-
* If everything goes well, all the requests will be sent through the first proxy URL in the list.
|
|
26
|
-
* Whenever the crawler encounters a problem with the current proxy on the given domain, it will switch to the higher tier for this domain.
|
|
27
|
-
* The crawler probes lower-level proxies at intervals to check if it can make the tier downshift.
|
|
28
|
-
*
|
|
29
|
-
* This feature is useful when you have a set of proxies with different performance characteristics (speed, price, antibot performance etc.) and you want to use the best one for each domain.
|
|
30
|
-
*
|
|
31
|
-
* Use `null` as a proxy URL to disable the proxy for the given tier.
|
|
32
|
-
*/
|
|
33
|
-
tieredProxyUrls?: UrlList[];
|
|
34
|
-
}
|
|
35
|
-
export interface TieredProxy {
|
|
36
|
-
proxyUrl: string | null;
|
|
37
|
-
proxyTier?: number;
|
|
38
20
|
}
|
|
39
21
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* Outside of crawlers, you can get this object by calling {@link ProxyConfiguration.newProxyInfo}.
|
|
43
|
-
*
|
|
44
|
-
* **Example usage:**
|
|
22
|
+
* Minimal contract that any object passed to a crawler as its `proxyConfiguration`
|
|
23
|
+
* option must satisfy.
|
|
45
24
|
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* });
|
|
50
|
-
*
|
|
51
|
-
* // Getting proxyInfo object by calling class method directly
|
|
52
|
-
* const proxyInfo = await proxyConfiguration.newProxyInfo();
|
|
53
|
-
*
|
|
54
|
-
* // In crawler
|
|
55
|
-
* const crawler = new CheerioCrawler({
|
|
56
|
-
* // ...
|
|
57
|
-
* proxyConfiguration,
|
|
58
|
-
* requestHandler({ proxyInfo }) {
|
|
59
|
-
* // Getting used proxy URL
|
|
60
|
-
* const proxyUrl = proxyInfo.url;
|
|
61
|
-
* }
|
|
62
|
-
* })
|
|
63
|
-
*
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
export interface ProxyInfo {
|
|
67
|
-
/**
|
|
68
|
-
* The URL of the proxy.
|
|
69
|
-
*/
|
|
70
|
-
url: string;
|
|
71
|
-
/**
|
|
72
|
-
* Username for the proxy.
|
|
73
|
-
*/
|
|
74
|
-
username?: string;
|
|
75
|
-
/**
|
|
76
|
-
* User's password for the proxy.
|
|
77
|
-
*/
|
|
78
|
-
password: string;
|
|
79
|
-
/**
|
|
80
|
-
* Hostname of your proxy.
|
|
81
|
-
*/
|
|
82
|
-
hostname: string;
|
|
83
|
-
/**
|
|
84
|
-
* Proxy port.
|
|
85
|
-
*/
|
|
86
|
-
port: number | string;
|
|
87
|
-
/**
|
|
88
|
-
* Proxy tier for the current proxy, if applicable (only for `tieredProxyUrls`).
|
|
89
|
-
*/
|
|
90
|
-
proxyTier?: number;
|
|
91
|
-
/**
|
|
92
|
-
* When `true`, the proxy is likely intercepting HTTPS traffic and is able to view and modify its content.
|
|
93
|
-
*
|
|
94
|
-
* @default false
|
|
95
|
-
*/
|
|
96
|
-
ignoreTlsErrors?: boolean;
|
|
97
|
-
}
|
|
98
|
-
interface TieredProxyOptions {
|
|
99
|
-
request?: Request;
|
|
100
|
-
proxyTier?: number;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Internal class for tracking the proxy tier history for a specific domain.
|
|
25
|
+
* Implement this interface to plug a custom proxy-provisioning strategy into any Crawlee
|
|
26
|
+
* crawler — for example a remote proxy service or a thin wrapper around the built-in
|
|
27
|
+
* `ProxyConfiguration` with different rotation rules. *
|
|
104
28
|
*
|
|
105
|
-
*
|
|
29
|
+
* @category Scaling
|
|
106
30
|
*/
|
|
107
|
-
|
|
108
|
-
private histogram;
|
|
109
|
-
private currentTier;
|
|
110
|
-
constructor(tieredProxyUrls: (string | null)[][]);
|
|
111
|
-
/**
|
|
112
|
-
* Processes a single step of the algorithm and updates the current tier prediction based on the error history.
|
|
113
|
-
*/
|
|
114
|
-
private processStep;
|
|
115
|
-
/**
|
|
116
|
-
* Increases the error score for the given proxy tier. This raises the chance of picking a different proxy tier for the subsequent requests.
|
|
117
|
-
*
|
|
118
|
-
* The error score is increased by 10 for the given tier. This means that this tier will be disadvantaged for the next 10 requests (every new request prediction decreases the error score by 1).
|
|
119
|
-
* @param tier The proxy tier to mark as problematic.
|
|
120
|
-
*/
|
|
121
|
-
addError(tier: number): void;
|
|
31
|
+
export interface IProxyConfiguration {
|
|
122
32
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
33
|
+
* Creates a new {@link ProxyInfo} object describing the proxy to use.
|
|
34
|
+
* Returns `undefined` when no proxy should be used.
|
|
125
35
|
*/
|
|
126
|
-
|
|
36
|
+
newProxyInfo(): Promise<ProxyInfo | undefined>;
|
|
127
37
|
}
|
|
128
38
|
/**
|
|
129
39
|
* Configures connection to a proxy server with the provided options. Proxy servers are used to prevent target websites from blocking
|
|
@@ -153,16 +63,9 @@ declare class ProxyTierTracker {
|
|
|
153
63
|
* ```
|
|
154
64
|
* @category Scaling
|
|
155
65
|
*/
|
|
156
|
-
export declare class ProxyConfiguration {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
protected proxyUrls?: UrlList;
|
|
160
|
-
protected tieredProxyUrls?: UrlList[];
|
|
161
|
-
protected usedProxyUrls: Map<string, string | null>;
|
|
162
|
-
protected newUrlFunction?: ProxyConfigurationFunction;
|
|
163
|
-
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
164
|
-
protected log: import("@apify/log").Log;
|
|
165
|
-
protected domainTiers: Map<string, ProxyTierTracker>;
|
|
66
|
+
export declare class ProxyConfiguration implements IProxyConfiguration {
|
|
67
|
+
#private;
|
|
68
|
+
readonly isManInTheMiddle = false;
|
|
166
69
|
/**
|
|
167
70
|
* Creates a {@link ProxyConfiguration} instance based on the provided options. Proxy servers are used to prevent target websites from
|
|
168
71
|
* blocking your crawlers based on IP address rate limits or blacklists. Setting proxy configuration in your crawlers automatically configures
|
|
@@ -193,35 +96,20 @@ export declare class ProxyConfiguration {
|
|
|
193
96
|
*
|
|
194
97
|
* @return Represents information about used proxy and its configuration.
|
|
195
98
|
*/
|
|
196
|
-
newProxyInfo(
|
|
197
|
-
/**
|
|
198
|
-
* Given a request / proxy tier, this function returns a new proxy URL based on the provided configuration options.
|
|
199
|
-
* @param options Options for the tiered proxy rotation
|
|
200
|
-
* @returns An object with the proxy URL and the proxy tier used.
|
|
201
|
-
*/
|
|
202
|
-
protected _handleTieredUrl(options?: TieredProxyOptions): TieredProxy;
|
|
203
|
-
/**
|
|
204
|
-
* Given a `Request` object, this function returns the tier of the proxy that should be used for the request.
|
|
205
|
-
*
|
|
206
|
-
* This returns `null` if `tieredProxyUrls` option is not set.
|
|
207
|
-
*/
|
|
208
|
-
protected predictProxyTier(request: Request): number | null;
|
|
99
|
+
newProxyInfo(): Promise<ProxyInfo | undefined>;
|
|
209
100
|
/**
|
|
210
101
|
* Returns a new proxy URL based on provided configuration options.
|
|
211
102
|
*
|
|
212
103
|
* @return A string with a proxy URL, including authentication credentials and port number.
|
|
213
104
|
* For example, `http://bob:password123@proxy.example.com:8000`
|
|
214
105
|
*/
|
|
215
|
-
newUrl(
|
|
216
|
-
|
|
106
|
+
newUrl(): Promise<string | undefined>;
|
|
107
|
+
private handleProxyUrlsList;
|
|
217
108
|
/**
|
|
218
109
|
* Calls the custom newUrlFunction and checks format of its return value
|
|
219
110
|
*/
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
protected _throwCannotCombineCustomMethods(): never;
|
|
224
|
-
protected _throwNoOptionsProvided(): never;
|
|
111
|
+
private callNewUrlFunction;
|
|
112
|
+
private throwCannotCombineCustomMethods;
|
|
113
|
+
private throwNoOptionsProvided;
|
|
225
114
|
}
|
|
226
115
|
export {};
|
|
227
|
-
//# sourceMappingURL=proxy_configuration.d.ts.map
|
package/proxy_configuration.js
CHANGED
|
@@ -1,54 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
currentTier;
|
|
11
|
-
constructor(tieredProxyUrls) {
|
|
12
|
-
this.histogram = tieredProxyUrls.map(() => 0);
|
|
13
|
-
this.currentTier = 0;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Processes a single step of the algorithm and updates the current tier prediction based on the error history.
|
|
17
|
-
*/
|
|
18
|
-
processStep() {
|
|
19
|
-
this.histogram.forEach((x, i) => {
|
|
20
|
-
if (this.currentTier === i)
|
|
21
|
-
return;
|
|
22
|
-
if (x > 0)
|
|
23
|
-
this.histogram[i]--;
|
|
24
|
-
});
|
|
25
|
-
const left = this.currentTier > 0 ? this.histogram[this.currentTier - 1] : Infinity;
|
|
26
|
-
const right = this.currentTier < this.histogram.length - 1 ? this.histogram[this.currentTier + 1] : Infinity;
|
|
27
|
-
if (this.histogram[this.currentTier] > Math.min(left, right)) {
|
|
28
|
-
this.currentTier = left <= right ? this.currentTier - 1 : this.currentTier + 1;
|
|
29
|
-
}
|
|
30
|
-
else if (this.histogram[this.currentTier] === left) {
|
|
31
|
-
this.currentTier--;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Increases the error score for the given proxy tier. This raises the chance of picking a different proxy tier for the subsequent requests.
|
|
36
|
-
*
|
|
37
|
-
* The error score is increased by 10 for the given tier. This means that this tier will be disadvantaged for the next 10 requests (every new request prediction decreases the error score by 1).
|
|
38
|
-
* @param tier The proxy tier to mark as problematic.
|
|
39
|
-
*/
|
|
40
|
-
addError(tier) {
|
|
41
|
-
this.histogram[tier] += 10;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Returns the best proxy tier for the next request based on the error history for different proxy tiers.
|
|
45
|
-
* @returns The proxy tier prediction
|
|
46
|
-
*/
|
|
47
|
-
predictTier() {
|
|
48
|
-
this.processStep();
|
|
49
|
-
return this.currentTier;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { parseArgument, schemas } from './validators.js';
|
|
3
|
+
const proxyConfigurationOptionsSchema = z.strictObject({
|
|
4
|
+
proxyUrls: z
|
|
5
|
+
.array(z.union([z.url(), z.null()]))
|
|
6
|
+
.nonempty()
|
|
7
|
+
.optional(),
|
|
8
|
+
newUrlFunction: schemas.anyFunction.optional(),
|
|
9
|
+
});
|
|
52
10
|
/**
|
|
53
11
|
* Configures connection to a proxy server with the provided options. Proxy servers are used to prevent target websites from blocking
|
|
54
12
|
* your crawlers based on IP address rate limits or blacklists. Setting proxy configuration in your crawlers automatically configures
|
|
@@ -79,13 +37,9 @@ class ProxyTierTracker {
|
|
|
79
37
|
*/
|
|
80
38
|
export class ProxyConfiguration {
|
|
81
39
|
isManInTheMiddle = false;
|
|
82
|
-
nextCustomUrlIndex = 0;
|
|
83
|
-
proxyUrls;
|
|
84
|
-
|
|
85
|
-
usedProxyUrls = new Map();
|
|
86
|
-
newUrlFunction;
|
|
87
|
-
log = log.child({ prefix: 'ProxyConfiguration' });
|
|
88
|
-
domainTiers = new Map();
|
|
40
|
+
#nextCustomUrlIndex = 0;
|
|
41
|
+
#proxyUrls;
|
|
42
|
+
#newUrlFunction;
|
|
89
43
|
/**
|
|
90
44
|
* Creates a {@link ProxyConfiguration} instance based on the provided options. Proxy servers are used to prevent target websites from
|
|
91
45
|
* blocking your crawlers based on IP address rate limits or blacklists. Setting proxy configuration in your crawlers automatically configures
|
|
@@ -108,19 +62,17 @@ export class ProxyConfiguration {
|
|
|
108
62
|
*/
|
|
109
63
|
constructor(options = {}) {
|
|
110
64
|
const { validateRequired, ...rest } = options;
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
this._throwCannotCombineCustomMethods();
|
|
65
|
+
if ('tieredProxyUrls' in rest) {
|
|
66
|
+
throw new Error('The `tieredProxyUrls` option has been removed in Crawlee v4. ' +
|
|
67
|
+
'See the v4 upgrading guide for the recommended migration to named sessions.');
|
|
68
|
+
}
|
|
69
|
+
const { proxyUrls, newUrlFunction } = parseArgument(rest, proxyConfigurationOptionsSchema);
|
|
70
|
+
if (proxyUrls && newUrlFunction)
|
|
71
|
+
this.throwCannotCombineCustomMethods();
|
|
119
72
|
if (!proxyUrls && !newUrlFunction && validateRequired)
|
|
120
|
-
this.
|
|
121
|
-
this
|
|
122
|
-
this
|
|
123
|
-
this.tieredProxyUrls = tieredProxyUrls;
|
|
73
|
+
this.throwNoOptionsProvided();
|
|
74
|
+
this.#proxyUrls = proxyUrls;
|
|
75
|
+
this.#newUrlFunction = newUrlFunction;
|
|
124
76
|
}
|
|
125
77
|
/**
|
|
126
78
|
* This function creates a new {@link ProxyInfo} info object.
|
|
@@ -131,17 +83,8 @@ export class ProxyConfiguration {
|
|
|
131
83
|
*
|
|
132
84
|
* @return Represents information about used proxy and its configuration.
|
|
133
85
|
*/
|
|
134
|
-
async newProxyInfo(
|
|
135
|
-
|
|
136
|
-
let tier;
|
|
137
|
-
if (this.tieredProxyUrls) {
|
|
138
|
-
const { proxyUrl, proxyTier } = this._handleTieredUrl(options);
|
|
139
|
-
url = proxyUrl ?? undefined;
|
|
140
|
-
tier = proxyTier;
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
url = await this.newUrl(options);
|
|
144
|
-
}
|
|
86
|
+
async newProxyInfo() {
|
|
87
|
+
const url = await this.newUrl();
|
|
145
88
|
if (!url)
|
|
146
89
|
return undefined;
|
|
147
90
|
const { username, password, port, hostname } = new URL(url);
|
|
@@ -151,82 +94,28 @@ export class ProxyConfiguration {
|
|
|
151
94
|
password: decodeURIComponent(password),
|
|
152
95
|
hostname,
|
|
153
96
|
port: port,
|
|
154
|
-
proxyTier: tier,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Given a request / proxy tier, this function returns a new proxy URL based on the provided configuration options.
|
|
159
|
-
* @param options Options for the tiered proxy rotation
|
|
160
|
-
* @returns An object with the proxy URL and the proxy tier used.
|
|
161
|
-
*/
|
|
162
|
-
_handleTieredUrl(options) {
|
|
163
|
-
if (!this.tieredProxyUrls)
|
|
164
|
-
throw new Error('Tiered proxy URLs are not set');
|
|
165
|
-
if (!options || (!options?.request && options?.proxyTier === undefined)) {
|
|
166
|
-
const allProxyUrls = this.tieredProxyUrls.flat();
|
|
167
|
-
return {
|
|
168
|
-
proxyUrl: allProxyUrls[this.nextCustomUrlIndex++ % allProxyUrls.length],
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
let tierPrediction = options.proxyTier;
|
|
172
|
-
if (typeof tierPrediction !== 'number') {
|
|
173
|
-
tierPrediction = this.predictProxyTier(options.request);
|
|
174
|
-
}
|
|
175
|
-
const proxyTier = this.tieredProxyUrls[tierPrediction];
|
|
176
|
-
return {
|
|
177
|
-
proxyUrl: proxyTier[this.nextCustomUrlIndex++ % proxyTier.length],
|
|
178
|
-
proxyTier: tierPrediction,
|
|
179
97
|
};
|
|
180
98
|
}
|
|
181
|
-
/**
|
|
182
|
-
* Given a `Request` object, this function returns the tier of the proxy that should be used for the request.
|
|
183
|
-
*
|
|
184
|
-
* This returns `null` if `tieredProxyUrls` option is not set.
|
|
185
|
-
*/
|
|
186
|
-
predictProxyTier(request) {
|
|
187
|
-
if (!this.tieredProxyUrls)
|
|
188
|
-
return null;
|
|
189
|
-
const domain = new URL(request.url).hostname;
|
|
190
|
-
if (!this.domainTiers.has(domain)) {
|
|
191
|
-
this.domainTiers.set(domain, new ProxyTierTracker(this.tieredProxyUrls));
|
|
192
|
-
}
|
|
193
|
-
request.userData.__crawlee ??= {};
|
|
194
|
-
const tracker = this.domainTiers.get(domain);
|
|
195
|
-
if (typeof request.userData.__crawlee.lastProxyTier === 'number') {
|
|
196
|
-
tracker.addError(request.userData.__crawlee.lastProxyTier);
|
|
197
|
-
}
|
|
198
|
-
const tierPrediction = tracker.predictTier();
|
|
199
|
-
if (typeof request.userData.__crawlee.lastProxyTier === 'number' &&
|
|
200
|
-
request.userData.__crawlee.lastProxyTier !== tierPrediction) {
|
|
201
|
-
log.debug(`Changing proxy tier for domain "${domain}" from ${request.userData.__crawlee.lastProxyTier} to ${tierPrediction}.`);
|
|
202
|
-
}
|
|
203
|
-
request.userData.__crawlee.lastProxyTier = tierPrediction;
|
|
204
|
-
request.userData.__crawlee.forefront = true;
|
|
205
|
-
return tierPrediction;
|
|
206
|
-
}
|
|
207
99
|
/**
|
|
208
100
|
* Returns a new proxy URL based on provided configuration options.
|
|
209
101
|
*
|
|
210
102
|
* @return A string with a proxy URL, including authentication credentials and port number.
|
|
211
103
|
* For example, `http://bob:password123@proxy.example.com:8000`
|
|
212
104
|
*/
|
|
213
|
-
async newUrl(
|
|
214
|
-
if (this
|
|
215
|
-
return (await this.
|
|
216
|
-
}
|
|
217
|
-
if (this.tieredProxyUrls) {
|
|
218
|
-
return this._handleTieredUrl(options).proxyUrl ?? undefined;
|
|
105
|
+
async newUrl() {
|
|
106
|
+
if (this.#newUrlFunction) {
|
|
107
|
+
return (await this.callNewUrlFunction()) ?? undefined;
|
|
219
108
|
}
|
|
220
|
-
return this.
|
|
109
|
+
return this.handleProxyUrlsList() ?? undefined;
|
|
221
110
|
}
|
|
222
|
-
|
|
223
|
-
return this
|
|
111
|
+
handleProxyUrlsList() {
|
|
112
|
+
return this.#proxyUrls[this.#nextCustomUrlIndex++ % this.#proxyUrls.length];
|
|
224
113
|
}
|
|
225
114
|
/**
|
|
226
115
|
* Calls the custom newUrlFunction and checks format of its return value
|
|
227
116
|
*/
|
|
228
|
-
async
|
|
229
|
-
const proxyUrl = await this
|
|
117
|
+
async callNewUrlFunction() {
|
|
118
|
+
const proxyUrl = await this.#newUrlFunction();
|
|
230
119
|
try {
|
|
231
120
|
if (proxyUrl) {
|
|
232
121
|
new URL(proxyUrl); // eslint-disable-line no-new
|
|
@@ -237,11 +126,10 @@ export class ProxyConfiguration {
|
|
|
237
126
|
throw new Error(`The provided newUrlFunction did not return a valid URL.\nCause: ${err.message}`);
|
|
238
127
|
}
|
|
239
128
|
}
|
|
240
|
-
|
|
129
|
+
throwCannotCombineCustomMethods() {
|
|
241
130
|
throw new Error('Cannot combine custom proxies "options.proxyUrls" with custom generating function "options.newUrlFunction".');
|
|
242
131
|
}
|
|
243
|
-
|
|
132
|
+
throwNoOptionsProvided() {
|
|
244
133
|
throw new Error('One of "options.proxyUrls" or "options.newUrlFunction" needs to be provided.');
|
|
245
134
|
}
|
|
246
135
|
}
|
|
247
|
-
//# sourceMappingURL=proxy_configuration.js.map
|