@crawlee/core 4.0.0-beta.1 → 4.0.0-beta.100
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 +69 -156
- package/autoscaling/autoscaled_pool.js +97 -252
- package/autoscaling/client_load_signal.d.ts +59 -0
- package/autoscaling/client_load_signal.js +73 -0
- package/autoscaling/concurrency_system.d.ts +283 -0
- package/autoscaling/concurrency_system.js +350 -0
- package/autoscaling/cpu_load_signal.d.ts +44 -0
- package/autoscaling/cpu_load_signal.js +46 -0
- package/autoscaling/event_loop_load_signal.d.ts +54 -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 +103 -0
- package/autoscaling/memory_load_signal.d.ts +56 -0
- package/autoscaling/memory_load_signal.js +106 -0
- package/autoscaling/snapshotter.d.ts +61 -163
- package/autoscaling/snapshotter.js +45 -270
- package/autoscaling/system_status.d.ts +63 -83
- package/autoscaling/system_status.js +90 -120
- 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 -230
- package/configuration.js +170 -226
- package/cookie_utils.d.ts +4 -3
- package/cookie_utils.js +22 -13
- package/crawlers/context_pipeline.d.ts +70 -0
- package/crawlers/context_pipeline.js +122 -0
- package/crawlers/crawler_commons.d.ts +96 -40
- package/crawlers/crawler_commons.js +15 -24
- package/crawlers/error_snapshotter.d.ts +3 -3
- package/crawlers/error_snapshotter.js +2 -3
- package/crawlers/error_tracker.d.ts +2 -2
- package/crawlers/error_tracker.js +0 -1
- package/crawlers/index.d.ts +1 -3
- package/crawlers/index.js +1 -3
- package/crawlers/internals/types.d.ts +7 -0
- package/crawlers/internals/types.js +1 -0
- package/crawlers/statistics.d.ts +28 -23
- package/crawlers/statistics.js +38 -34
- 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 +39 -26
- package/enqueue_links/shared.js +89 -67
- package/errors.d.ts +53 -4
- package/errors.js +70 -5
- package/events/event_manager.d.ts +34 -8
- package/events/event_manager.js +8 -10
- package/events/index.d.ts +0 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -5
- package/events/local_event_manager.js +37 -40
- package/index.d.ts +6 -4
- package/index.js +5 -3
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +82 -3
- package/log.js +102 -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 +46 -0
- package/memory-storage/memory-storage.js +136 -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 +113 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +203 -0
- package/memory-storage/resource-clients/request-queue.d.ts +96 -0
- package/memory-storage/resource-clients/request-queue.js +421 -0
- package/memory-storage/utils.d.ts +16 -0
- package/memory-storage/utils.js +41 -0
- package/owned_or_injected.d.ts +60 -0
- package/owned_or_injected.js +98 -0
- package/package.json +13 -12
- package/proxy_configuration.d.ts +29 -152
- package/proxy_configuration.js +21 -173
- package/recoverable_state.d.ts +120 -0
- package/recoverable_state.js +143 -0
- package/request.d.ts +85 -17
- package/request.js +107 -36
- package/router.d.ts +194 -19
- package/router.js +177 -32
- package/serialization.d.ts +0 -1
- package/serialization.js +1 -2
- package/service_locator.d.ts +156 -0
- package/service_locator.js +244 -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 +37 -75
- package/session_pool/session.js +49 -102
- package/session_pool/session_pool.d.ts +85 -90
- package/session_pool/session_pool.js +131 -120
- package/storages/access_checking.d.ts +1 -2
- package/storages/access_checking.js +5 -2
- package/storages/dataset.d.ts +103 -54
- package/storages/dataset.js +174 -132
- package/storages/index.d.ts +8 -7
- package/storages/index.js +6 -7
- package/storages/key_value_store.d.ts +167 -39
- package/storages/key_value_store.js +274 -127
- 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 +23 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +54 -98
- package/storages/request_list.js +99 -75
- package/storages/request_loader.d.ts +96 -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 +106 -0
- package/storages/request_manager_tandem.js +197 -0
- package/storages/request_queue.d.ts +287 -47
- package/storages/request_queue.js +620 -215
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +38 -46
- package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +58 -65
- package/storages/storage_instance_manager.d.ts +88 -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/utils.d.ts +54 -9
- 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/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/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_extension.d.ts +0 -12
- package/crawlers/crawler_extension.d.ts.map +0 -1
- package/crawlers/crawler_extension.js +0 -14
- package/crawlers/crawler_extension.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/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.map +0 -1
- 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 -307
- 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
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { KeyValueStore, serviceLocator } from '@crawlee/core';
|
|
2
|
+
/**
|
|
3
|
+
* A class for managing persistent recoverable state using a plain JavaScript object.
|
|
4
|
+
*
|
|
5
|
+
* This class facilitates state persistence to a `KeyValueStore`, allowing data to be saved and retrieved
|
|
6
|
+
* across migrations or restarts. It manages the loading, saving, and resetting of state data,
|
|
7
|
+
* with optional persistence capabilities.
|
|
8
|
+
*
|
|
9
|
+
* The state is represented by a plain JavaScript object that can be serialized to and deserialized from JSON.
|
|
10
|
+
* The class automatically hooks into the event system to persist state when needed.
|
|
11
|
+
*/
|
|
12
|
+
export class RecoverableState {
|
|
13
|
+
defaultState;
|
|
14
|
+
state = null;
|
|
15
|
+
persistenceEnabled;
|
|
16
|
+
persistStateKey;
|
|
17
|
+
persistStateKvsName;
|
|
18
|
+
persistStateKvsId;
|
|
19
|
+
keyValueStore = null;
|
|
20
|
+
log;
|
|
21
|
+
serialize;
|
|
22
|
+
deserialize;
|
|
23
|
+
/**
|
|
24
|
+
* Initialize a new recoverable state object.
|
|
25
|
+
*
|
|
26
|
+
* @param options Configuration options for the recoverable state
|
|
27
|
+
*/
|
|
28
|
+
constructor(options) {
|
|
29
|
+
this.defaultState = options.defaultState;
|
|
30
|
+
this.persistStateKey = options.persistStateKey;
|
|
31
|
+
this.persistenceEnabled = options.persistenceEnabled ?? false;
|
|
32
|
+
this.persistStateKvsName = options.persistStateKvsName;
|
|
33
|
+
this.persistStateKvsId = options.persistStateKvsId;
|
|
34
|
+
this.log = options.logger ?? serviceLocator.getLogger().child({ prefix: 'RecoverableState' });
|
|
35
|
+
this.serialize = options.serialize ?? JSON.stringify;
|
|
36
|
+
this.deserialize = options.deserialize ?? JSON.parse;
|
|
37
|
+
this.persistState = this.persistState.bind(this);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Initialize the recoverable state.
|
|
41
|
+
*
|
|
42
|
+
* This method must be called before using the recoverable state. It loads the saved state
|
|
43
|
+
* if persistence is enabled and registers the object to listen for PERSIST_STATE events.
|
|
44
|
+
*
|
|
45
|
+
* @returns The loaded state object
|
|
46
|
+
*/
|
|
47
|
+
async initialize() {
|
|
48
|
+
if (this.state !== null && this.state !== undefined) {
|
|
49
|
+
return this.currentValue;
|
|
50
|
+
}
|
|
51
|
+
if (!this.persistenceEnabled) {
|
|
52
|
+
this.state = this.deserialize(this.serialize(this.defaultState));
|
|
53
|
+
return this.currentValue;
|
|
54
|
+
}
|
|
55
|
+
let kvsIdentifier = null;
|
|
56
|
+
if (this.persistStateKvsName) {
|
|
57
|
+
kvsIdentifier = { name: this.persistStateKvsName };
|
|
58
|
+
}
|
|
59
|
+
else if (this.persistStateKvsId) {
|
|
60
|
+
kvsIdentifier = { id: this.persistStateKvsId };
|
|
61
|
+
}
|
|
62
|
+
this.keyValueStore = await KeyValueStore.open(kvsIdentifier, {
|
|
63
|
+
configuration: serviceLocator.getConfiguration(),
|
|
64
|
+
});
|
|
65
|
+
await this.loadSavedState();
|
|
66
|
+
// Register for persist state events
|
|
67
|
+
const eventManager = serviceLocator.getEventManager();
|
|
68
|
+
eventManager.on("persistState" /* EventType.PERSIST_STATE */, this.persistState);
|
|
69
|
+
return this.currentValue;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Clean up resources used by the recoverable state.
|
|
73
|
+
*
|
|
74
|
+
* If persistence is enabled, this method deregisters the object from PERSIST_STATE events
|
|
75
|
+
* and persists the current state one last time.
|
|
76
|
+
*/
|
|
77
|
+
async teardown() {
|
|
78
|
+
if (!this.persistenceEnabled || !this.persistState) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const eventManager = serviceLocator.getEventManager();
|
|
82
|
+
eventManager.off("persistState" /* EventType.PERSIST_STATE */, this.persistState);
|
|
83
|
+
await this.persistState();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Get the current state.
|
|
87
|
+
*/
|
|
88
|
+
get currentValue() {
|
|
89
|
+
if (this.state === null) {
|
|
90
|
+
throw new Error('Recoverable state has not yet been loaded');
|
|
91
|
+
}
|
|
92
|
+
return this.state;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Reset the state to the default values and clear any persisted state.
|
|
96
|
+
*
|
|
97
|
+
* Resets the current state to the default state and, if persistence is enabled,
|
|
98
|
+
* clears the persisted state from the KeyValueStore.
|
|
99
|
+
*/
|
|
100
|
+
async reset() {
|
|
101
|
+
this.state = this.deserialize(this.serialize(this.defaultState));
|
|
102
|
+
if (this.persistenceEnabled) {
|
|
103
|
+
if (this.keyValueStore === null) {
|
|
104
|
+
throw new Error('Recoverable state has not yet been initialized');
|
|
105
|
+
}
|
|
106
|
+
await this.keyValueStore.setValue(this.persistStateKey, null);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Persist the current state to the KeyValueStore.
|
|
111
|
+
*
|
|
112
|
+
* This method is typically called in response to a PERSIST_STATE event, but can also be called
|
|
113
|
+
* directly when needed.
|
|
114
|
+
*
|
|
115
|
+
* @param eventData Optional data associated with a PERSIST_STATE event
|
|
116
|
+
*/
|
|
117
|
+
async persistState(eventData) {
|
|
118
|
+
this.log.debug(`Persisting state of the RecoverableState (eventData=${JSON.stringify(eventData)}).`);
|
|
119
|
+
if (this.keyValueStore === null || this.state === null) {
|
|
120
|
+
throw new Error('Recoverable state has not yet been initialized');
|
|
121
|
+
}
|
|
122
|
+
if (this.persistenceEnabled) {
|
|
123
|
+
await this.keyValueStore.setValue(this.persistStateKey, this.serialize(this.state), {
|
|
124
|
+
contentType: 'text/plain', // HACK - the result is expected to be JSON, but we do this to avoid the implicit JSON.parse in `KeyValueStore.getValue`
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Load the saved state from the KeyValueStore
|
|
130
|
+
*/
|
|
131
|
+
async loadSavedState() {
|
|
132
|
+
if (this.keyValueStore === null) {
|
|
133
|
+
throw new Error('Recoverable state has not yet been initialized');
|
|
134
|
+
}
|
|
135
|
+
const storedState = await this.keyValueStore.getValue(this.persistStateKey);
|
|
136
|
+
if (storedState === null || storedState === undefined) {
|
|
137
|
+
this.state = this.deserialize(this.serialize(this.defaultState));
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
this.state = this.deserialize(storedState);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
package/request.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { BinaryLike } from 'node:crypto';
|
|
2
|
-
import type { Dictionary } from '@crawlee/types';
|
|
2
|
+
import type { AllowedHttpMethods, Dictionary } from '@crawlee/types';
|
|
3
3
|
import type { EnqueueLinksOptions } from './enqueue_links/enqueue_links.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { SkippedRequestReason } from './enqueue_links/shared.js';
|
|
5
5
|
export declare enum RequestState {
|
|
6
6
|
UNPROCESSED = 0,
|
|
7
7
|
BEFORE_NAV = 1,
|
|
@@ -43,7 +43,7 @@ export declare enum RequestState {
|
|
|
43
43
|
* ```
|
|
44
44
|
* @category Sources
|
|
45
45
|
*/
|
|
46
|
-
|
|
46
|
+
declare class CrawleeRequest<UserData extends Dictionary = Dictionary> {
|
|
47
47
|
/** Request ID */
|
|
48
48
|
id?: string;
|
|
49
49
|
/** URL of the web page to crawl. */
|
|
@@ -76,7 +76,12 @@ export declare class Request<UserData extends Dictionary = Dictionary> {
|
|
|
76
76
|
headers?: Record<string, string>;
|
|
77
77
|
/** Private store for the custom user data assigned to the request. */
|
|
78
78
|
private _userData;
|
|
79
|
-
/**
|
|
79
|
+
/**
|
|
80
|
+
* Custom user data assigned to the request.
|
|
81
|
+
*
|
|
82
|
+
* All data stored in `userData` must be JSON-serializable.
|
|
83
|
+
* Storing non-serializable values (e.g. functions, symbols) may result in unexpected results.
|
|
84
|
+
*/
|
|
80
85
|
userData: UserData;
|
|
81
86
|
/**
|
|
82
87
|
* ISO datetime string that indicates the time when the request has been processed.
|
|
@@ -87,14 +92,40 @@ export declare class Request<UserData extends Dictionary = Dictionary> {
|
|
|
87
92
|
* `Request` parameters including the URL, HTTP method and headers, and others.
|
|
88
93
|
*/
|
|
89
94
|
constructor(options: RequestOptions<UserData>);
|
|
90
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* Converts the Crawlee Request object to a `fetch` API Request object.
|
|
97
|
+
* @returns The native `fetch` API Request object.
|
|
98
|
+
*/
|
|
99
|
+
intoFetchAPIRequest(): Request;
|
|
100
|
+
/**
|
|
101
|
+
* Tells the crawler processing this request to skip the navigation and process the request directly.
|
|
102
|
+
*
|
|
103
|
+
* When this is set to `true`, the crawling context will not contain the results of the navigation
|
|
104
|
+
* (e.g. `response`, `body`, `contentType`, `$` or `request.loadedUrl`).
|
|
105
|
+
* Accessing these properties will throw a {@link NavigationSkippedError} at runtime.
|
|
106
|
+
*/
|
|
91
107
|
get skipNavigation(): boolean;
|
|
92
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* Tells the crawler processing this request to skip the navigation and process the request directly.
|
|
110
|
+
*
|
|
111
|
+
* When this is set to `true`, the crawling context will not contain the results of the navigation
|
|
112
|
+
* (e.g. `response`, `body`, `contentType`, `$` or `request.loadedUrl`).
|
|
113
|
+
* Accessing these properties will throw a {@link NavigationSkippedError} at runtime.
|
|
114
|
+
*/
|
|
93
115
|
set skipNavigation(value: boolean);
|
|
94
|
-
/**
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Depth of the request in the current crawl tree.
|
|
118
|
+
* Note that this is dependent on the crawler setup and might produce unexpected results when used with multiple crawlers.
|
|
119
|
+
*/
|
|
120
|
+
get crawlDepth(): number;
|
|
121
|
+
/**
|
|
122
|
+
* Depth of the request in the current crawl tree.
|
|
123
|
+
* Note that this is dependent on the crawler setup and might produce unexpected results when used with multiple crawlers.
|
|
124
|
+
*/
|
|
125
|
+
set crawlDepth(value: number);
|
|
126
|
+
/** ID of a session to use for this request. When set, the crawler will fetch this session from the session pool instead of creating a new one. */
|
|
127
|
+
get sessionId(): string | undefined;
|
|
128
|
+
set sessionId(value: string | undefined);
|
|
98
129
|
/** shortcut for getting `request.userData.label` */
|
|
99
130
|
get label(): string | undefined;
|
|
100
131
|
/** shortcut for setting `request.userData.label` */
|
|
@@ -107,6 +138,14 @@ export declare class Request<UserData extends Dictionary = Dictionary> {
|
|
|
107
138
|
get state(): RequestState;
|
|
108
139
|
/** Describes the request's current lifecycle state. */
|
|
109
140
|
set state(value: RequestState);
|
|
141
|
+
/**
|
|
142
|
+
* Reason for skipping this request.
|
|
143
|
+
*/
|
|
144
|
+
get skippedReason(): SkippedRequestReason | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* Reason for skipping this request.
|
|
147
|
+
*/
|
|
148
|
+
set skippedReason(value: SkippedRequestReason | undefined);
|
|
110
149
|
private get enqueueStrategy();
|
|
111
150
|
private set enqueueStrategy(value);
|
|
112
151
|
/**
|
|
@@ -123,10 +162,8 @@ export declare class Request<UserData extends Dictionary = Dictionary> {
|
|
|
123
162
|
* @param [options]
|
|
124
163
|
*/
|
|
125
164
|
pushErrorMessage(errorOrMessage: unknown, options?: PushErrorMessageOptions): void;
|
|
126
|
-
protected _computeUniqueKey(options: ComputeUniqueKeyOptions): string;
|
|
127
|
-
protected _hashPayload(payload: BinaryLike): string;
|
|
128
165
|
/** @internal */
|
|
129
|
-
static computeUniqueKey({ url, method, payload, keepUrlFragment, useExtendedUniqueKey, }: ComputeUniqueKeyOptions): string;
|
|
166
|
+
static computeUniqueKey({ url, method, payload, keepUrlFragment, useExtendedUniqueKey, alwaysEnqueue, }: ComputeUniqueKeyOptions): string;
|
|
130
167
|
/** @internal */
|
|
131
168
|
static hashPayload(payload: BinaryLike): string;
|
|
132
169
|
}
|
|
@@ -147,7 +184,7 @@ export interface RequestOptions<UserData extends Dictionary = Dictionary> {
|
|
|
147
184
|
* The `keepUrlFragment` option determines whether URL hash fragment is included in the `uniqueKey` or not.
|
|
148
185
|
*
|
|
149
186
|
* The `useExtendedUniqueKey` options determines whether method and payload are included in the `uniqueKey`,
|
|
150
|
-
* producing a `uniqueKey` in the following format: `METHOD
|
|
187
|
+
* producing a `uniqueKey` in the following format: `METHOD|payloadHash|normalizedUrl`. This is useful
|
|
151
188
|
* when requests point to the same URL, but with different methods and payloads. For example: form submits.
|
|
152
189
|
*
|
|
153
190
|
* Pass an arbitrary non-empty text value to the `uniqueKey` property
|
|
@@ -171,6 +208,9 @@ export interface RequestOptions<UserData extends Dictionary = Dictionary> {
|
|
|
171
208
|
/**
|
|
172
209
|
* Custom user data assigned to the request. Use this to save any request related data to the
|
|
173
210
|
* request's scope, keeping them accessible on retries, failures etc.
|
|
211
|
+
*
|
|
212
|
+
* All data stored in `userData` must be JSON-serializable.
|
|
213
|
+
* Storing non-serializable values (e.g. functions, symbols) may result in unexpected results.
|
|
174
214
|
*/
|
|
175
215
|
userData?: UserData;
|
|
176
216
|
/**
|
|
@@ -192,17 +232,45 @@ export interface RequestOptions<UserData extends Dictionary = Dictionary> {
|
|
|
192
232
|
* @default false
|
|
193
233
|
*/
|
|
194
234
|
useExtendedUniqueKey?: boolean;
|
|
235
|
+
/**
|
|
236
|
+
* If `true` then a random value is included in the `uniqueKey` computation, ensuring the request
|
|
237
|
+
* is always enqueued even if a request with the same URL (and method/payload) is already present
|
|
238
|
+
* in the queue. Cannot be used together with a custom `uniqueKey`.
|
|
239
|
+
* @default false
|
|
240
|
+
*/
|
|
241
|
+
alwaysEnqueue?: boolean;
|
|
195
242
|
/**
|
|
196
243
|
* The `true` value indicates that the request will not be automatically retried on error.
|
|
197
244
|
* @default false
|
|
198
245
|
*/
|
|
199
246
|
noRetry?: boolean;
|
|
247
|
+
/**
|
|
248
|
+
* ID of a session from the crawler's `SessionPool` to use for this request.
|
|
249
|
+
* When set, the crawler will fetch this session from the pool instead of creating a new one.
|
|
250
|
+
*/
|
|
251
|
+
sessionId?: string;
|
|
200
252
|
/**
|
|
201
253
|
* If set to `true` then the crawler processing this request evaluates
|
|
202
254
|
* the `requestHandler` immediately without prior browser navigation.
|
|
255
|
+
*
|
|
256
|
+
* When enabled, the crawling context will not contain the results of the navigation
|
|
257
|
+
* (e.g. `response`, `body`, `contentType`, `$` or `request.loadedUrl`).
|
|
258
|
+
* Accessing these properties will throw a {@link NavigationSkippedError} at runtime.
|
|
203
259
|
* @default false
|
|
204
260
|
*/
|
|
205
261
|
skipNavigation?: boolean;
|
|
262
|
+
/**
|
|
263
|
+
* Depth of the request in the current crawl tree.
|
|
264
|
+
* Note that this is dependent on the crawler setup and might produce unexpected results when used with multiple crawlers.
|
|
265
|
+
* @default 0
|
|
266
|
+
*/
|
|
267
|
+
crawlDepth?: number;
|
|
268
|
+
/**
|
|
269
|
+
* Reason for skipping this request.
|
|
270
|
+
* This is used to provide more information about why the request was skipped.
|
|
271
|
+
* @internal
|
|
272
|
+
*/
|
|
273
|
+
skippedReason?: SkippedRequestReason;
|
|
206
274
|
/**
|
|
207
275
|
* Maximum number of retries for this request. Allows to override the global `maxRequestRetries` option of `BasicCrawler`.
|
|
208
276
|
*/
|
|
@@ -229,15 +297,15 @@ interface ComputeUniqueKeyOptions {
|
|
|
229
297
|
payload?: string | Buffer;
|
|
230
298
|
keepUrlFragment?: boolean;
|
|
231
299
|
useExtendedUniqueKey?: boolean;
|
|
300
|
+
alwaysEnqueue?: boolean;
|
|
232
301
|
}
|
|
233
302
|
export type Source = (Partial<RequestOptions> & {
|
|
234
303
|
requestsFromUrl?: string;
|
|
235
304
|
regex?: RegExp;
|
|
236
|
-
}) |
|
|
305
|
+
}) | CrawleeRequest;
|
|
237
306
|
/** @internal */
|
|
238
307
|
export interface InternalSource {
|
|
239
308
|
requestsFromUrl: string;
|
|
240
309
|
regex?: RegExp;
|
|
241
310
|
}
|
|
242
|
-
export {};
|
|
243
|
-
//# sourceMappingURL=request.d.ts.map
|
|
311
|
+
export { CrawleeRequest as Request };
|
package/request.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import crypto from 'node:crypto';
|
|
2
2
|
import util from 'node:util';
|
|
3
3
|
import ow from 'ow';
|
|
4
|
-
import { normalizeUrl } from '@apify/utilities';
|
|
5
|
-
import {
|
|
4
|
+
import { cryptoRandomObjectId, normalizeUrl } from '@apify/utilities';
|
|
5
|
+
import { serviceLocator } from './service_locator.js';
|
|
6
6
|
import { keys } from './typedefs.js';
|
|
7
7
|
// new properties on the Request object breaks serialization
|
|
8
|
-
const log = defaultLog.child({ prefix: 'Request' });
|
|
9
8
|
const requestOptionalPredicates = {
|
|
10
9
|
id: ow.optional.string,
|
|
11
10
|
loadedUrl: ow.optional.string.url,
|
|
@@ -14,7 +13,7 @@ const requestOptionalPredicates = {
|
|
|
14
13
|
payload: ow.optional.any(ow.string, ow.uint8Array),
|
|
15
14
|
noRetry: ow.optional.boolean,
|
|
16
15
|
retryCount: ow.optional.number,
|
|
17
|
-
|
|
16
|
+
sessionId: ow.optional.string,
|
|
18
17
|
maxRetries: ow.optional.number,
|
|
19
18
|
errorMessages: ow.optional.array.ofType(ow.string),
|
|
20
19
|
headers: ow.optional.object,
|
|
@@ -23,7 +22,9 @@ const requestOptionalPredicates = {
|
|
|
23
22
|
handledAt: ow.optional.any(ow.string.date, ow.date),
|
|
24
23
|
keepUrlFragment: ow.optional.boolean,
|
|
25
24
|
useExtendedUniqueKey: ow.optional.boolean,
|
|
25
|
+
alwaysEnqueue: ow.optional.boolean,
|
|
26
26
|
skipNavigation: ow.optional.boolean,
|
|
27
|
+
crawlDepth: ow.optional.number.greaterThanOrEqual(0),
|
|
27
28
|
state: ow.optional.number.greaterThanOrEqual(0).lessThanOrEqual(6),
|
|
28
29
|
};
|
|
29
30
|
export var RequestState;
|
|
@@ -68,7 +69,7 @@ export var RequestState;
|
|
|
68
69
|
* ```
|
|
69
70
|
* @category Sources
|
|
70
71
|
*/
|
|
71
|
-
|
|
72
|
+
class CrawleeRequest {
|
|
72
73
|
/** Request ID */
|
|
73
74
|
id;
|
|
74
75
|
/** URL of the web page to crawl. */
|
|
@@ -101,7 +102,12 @@ export class Request {
|
|
|
101
102
|
headers;
|
|
102
103
|
/** Private store for the custom user data assigned to the request. */
|
|
103
104
|
_userData = {};
|
|
104
|
-
/**
|
|
105
|
+
/**
|
|
106
|
+
* Custom user data assigned to the request.
|
|
107
|
+
*
|
|
108
|
+
* All data stored in `userData` must be JSON-serializable.
|
|
109
|
+
* Storing non-serializable values (e.g. functions, symbols) may result in unexpected results.
|
|
110
|
+
*/
|
|
105
111
|
userData = {};
|
|
106
112
|
/**
|
|
107
113
|
* ISO datetime string that indicates the time when the request has been processed.
|
|
@@ -130,21 +136,31 @@ export class Request {
|
|
|
130
136
|
ow(value, `RequestOptions.${prop}`, predicate);
|
|
131
137
|
}
|
|
132
138
|
});
|
|
133
|
-
const { id, url, loadedUrl, uniqueKey, payload, noRetry = false, retryCount = 0,
|
|
139
|
+
const { id, url, loadedUrl, uniqueKey, payload, noRetry = false, retryCount = 0, sessionId, maxRetries, errorMessages = [], headers = {}, userData = {}, label, handledAt, keepUrlFragment = false, useExtendedUniqueKey = false, alwaysEnqueue = false, skipNavigation, enqueueStrategy, crawlDepth, } = options;
|
|
134
140
|
let { method = 'GET' } = options;
|
|
135
141
|
method = method.toUpperCase();
|
|
136
142
|
if (method === 'GET' && payload)
|
|
137
143
|
throw new Error('Request with GET method cannot have a payload.');
|
|
144
|
+
if (uniqueKey && alwaysEnqueue) {
|
|
145
|
+
throw new Error('`alwaysEnqueue` cannot be used together with a custom `uniqueKey`.');
|
|
146
|
+
}
|
|
138
147
|
this.id = id;
|
|
139
148
|
this.url = url;
|
|
140
149
|
this.loadedUrl = loadedUrl;
|
|
141
150
|
this.uniqueKey =
|
|
142
|
-
uniqueKey ||
|
|
151
|
+
uniqueKey ||
|
|
152
|
+
CrawleeRequest.computeUniqueKey({
|
|
153
|
+
url,
|
|
154
|
+
method,
|
|
155
|
+
payload,
|
|
156
|
+
keepUrlFragment,
|
|
157
|
+
useExtendedUniqueKey,
|
|
158
|
+
alwaysEnqueue,
|
|
159
|
+
});
|
|
143
160
|
this.method = method;
|
|
144
161
|
this.payload = payload;
|
|
145
162
|
this.noRetry = noRetry;
|
|
146
163
|
this.retryCount = retryCount;
|
|
147
|
-
this.sessionRotationCount = sessionRotationCount;
|
|
148
164
|
this.errorMessages = [...errorMessages];
|
|
149
165
|
this.headers = { ...headers };
|
|
150
166
|
this.handledAt = handledAt instanceof Date ? handledAt.toISOString() : handledAt;
|
|
@@ -191,16 +207,43 @@ export class Request {
|
|
|
191
207
|
this.skipNavigation = skipNavigation;
|
|
192
208
|
if (maxRetries != null)
|
|
193
209
|
this.maxRetries = maxRetries;
|
|
210
|
+
if (crawlDepth != null)
|
|
211
|
+
this.userData.__crawlee.crawlDepth ??= crawlDepth;
|
|
212
|
+
if (sessionId)
|
|
213
|
+
this.sessionId = sessionId;
|
|
194
214
|
// If it's already set, don't override it (for instance when fetching from storage)
|
|
195
215
|
if (enqueueStrategy) {
|
|
196
216
|
this.enqueueStrategy ??= enqueueStrategy;
|
|
197
217
|
}
|
|
198
218
|
}
|
|
199
|
-
/**
|
|
219
|
+
/**
|
|
220
|
+
* Converts the Crawlee Request object to a `fetch` API Request object.
|
|
221
|
+
* @returns The native `fetch` API Request object.
|
|
222
|
+
*/
|
|
223
|
+
intoFetchAPIRequest() {
|
|
224
|
+
return new Request(this.url, {
|
|
225
|
+
method: this.method,
|
|
226
|
+
headers: this.headers,
|
|
227
|
+
body: this.payload,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Tells the crawler processing this request to skip the navigation and process the request directly.
|
|
232
|
+
*
|
|
233
|
+
* When this is set to `true`, the crawling context will not contain the results of the navigation
|
|
234
|
+
* (e.g. `response`, `body`, `contentType`, `$` or `request.loadedUrl`).
|
|
235
|
+
* Accessing these properties will throw a {@link NavigationSkippedError} at runtime.
|
|
236
|
+
*/
|
|
200
237
|
get skipNavigation() {
|
|
201
238
|
return this.userData.__crawlee?.skipNavigation ?? false;
|
|
202
239
|
}
|
|
203
|
-
/**
|
|
240
|
+
/**
|
|
241
|
+
* Tells the crawler processing this request to skip the navigation and process the request directly.
|
|
242
|
+
*
|
|
243
|
+
* When this is set to `true`, the crawling context will not contain the results of the navigation
|
|
244
|
+
* (e.g. `response`, `body`, `contentType`, `$` or `request.loadedUrl`).
|
|
245
|
+
* Accessing these properties will throw a {@link NavigationSkippedError} at runtime.
|
|
246
|
+
*/
|
|
204
247
|
set skipNavigation(value) {
|
|
205
248
|
if (!this.userData.__crawlee) {
|
|
206
249
|
this.userData.__crawlee = { skipNavigation: value };
|
|
@@ -209,18 +252,28 @@ export class Request {
|
|
|
209
252
|
this.userData.__crawlee.skipNavigation = value;
|
|
210
253
|
}
|
|
211
254
|
}
|
|
212
|
-
/**
|
|
213
|
-
|
|
214
|
-
|
|
255
|
+
/**
|
|
256
|
+
* Depth of the request in the current crawl tree.
|
|
257
|
+
* Note that this is dependent on the crawler setup and might produce unexpected results when used with multiple crawlers.
|
|
258
|
+
*/
|
|
259
|
+
get crawlDepth() {
|
|
260
|
+
return this.userData.__crawlee?.crawlDepth ?? 0;
|
|
215
261
|
}
|
|
216
|
-
/**
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
262
|
+
/**
|
|
263
|
+
* Depth of the request in the current crawl tree.
|
|
264
|
+
* Note that this is dependent on the crawler setup and might produce unexpected results when used with multiple crawlers.
|
|
265
|
+
*/
|
|
266
|
+
set crawlDepth(value) {
|
|
267
|
+
this.userData.__crawlee ??= {};
|
|
268
|
+
this.userData.__crawlee.crawlDepth = value;
|
|
269
|
+
}
|
|
270
|
+
/** ID of a session to use for this request. When set, the crawler will fetch this session from the session pool instead of creating a new one. */
|
|
271
|
+
get sessionId() {
|
|
272
|
+
return this.userData.__crawlee?.sessionId;
|
|
273
|
+
}
|
|
274
|
+
set sessionId(value) {
|
|
275
|
+
this.userData.__crawlee ??= {};
|
|
276
|
+
this.userData.__crawlee.sessionId = value;
|
|
224
277
|
}
|
|
225
278
|
/** shortcut for getting `request.userData.label` */
|
|
226
279
|
get label() {
|
|
@@ -256,6 +309,23 @@ export class Request {
|
|
|
256
309
|
this.userData.__crawlee.state = value;
|
|
257
310
|
}
|
|
258
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* Reason for skipping this request.
|
|
314
|
+
*/
|
|
315
|
+
get skippedReason() {
|
|
316
|
+
return this.userData.__crawlee?.skippedReason;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Reason for skipping this request.
|
|
320
|
+
*/
|
|
321
|
+
set skippedReason(value) {
|
|
322
|
+
if (!this.userData.__crawlee) {
|
|
323
|
+
this.userData.__crawlee = { skippedReason: value };
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
this.userData.__crawlee.skippedReason = value;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
259
329
|
get enqueueStrategy() {
|
|
260
330
|
return this.userData.__crawlee?.enqueueStrategy;
|
|
261
331
|
}
|
|
@@ -317,32 +387,33 @@ export class Request {
|
|
|
317
387
|
}
|
|
318
388
|
this.errorMessages.push(message);
|
|
319
389
|
}
|
|
320
|
-
// TODO: only for better BC, remove in v4
|
|
321
|
-
_computeUniqueKey(options) {
|
|
322
|
-
return Request.computeUniqueKey(options);
|
|
323
|
-
}
|
|
324
|
-
// TODO: only for better BC, remove in v4
|
|
325
|
-
_hashPayload(payload) {
|
|
326
|
-
return Request.hashPayload(payload);
|
|
327
|
-
}
|
|
328
390
|
/** @internal */
|
|
329
|
-
static computeUniqueKey({ url, method = 'GET', payload, keepUrlFragment = false, useExtendedUniqueKey = false, }) {
|
|
391
|
+
static computeUniqueKey({ url, method = 'GET', payload, keepUrlFragment = false, useExtendedUniqueKey = false, alwaysEnqueue = false, }) {
|
|
330
392
|
const normalizedMethod = method.toUpperCase();
|
|
331
393
|
const normalizedUrl = normalizeUrl(url, keepUrlFragment) || url; // It returns null when url is invalid, causing weird errors.
|
|
394
|
+
let uniqueKey;
|
|
332
395
|
if (!useExtendedUniqueKey) {
|
|
333
396
|
if (normalizedMethod !== 'GET' && payload) {
|
|
334
|
-
|
|
397
|
+
serviceLocator
|
|
398
|
+
.getLogger()
|
|
399
|
+
.warningOnce(`We've encountered a ${normalizedMethod} Request with a payload. ` +
|
|
335
400
|
'This is fine. Just letting you know that if your requests point to the same URL ' +
|
|
336
401
|
'and differ only in method and payload, you should see the "useExtendedUniqueKey" option of Request constructor.');
|
|
337
402
|
}
|
|
338
|
-
|
|
403
|
+
uniqueKey = normalizedUrl;
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
const payloadHash = payload ? CrawleeRequest.hashPayload(payload) : '';
|
|
407
|
+
uniqueKey = `${normalizedMethod}|${payloadHash}|${normalizedUrl}`;
|
|
408
|
+
}
|
|
409
|
+
if (alwaysEnqueue) {
|
|
410
|
+
uniqueKey = `${cryptoRandomObjectId(17)}|${uniqueKey}`;
|
|
339
411
|
}
|
|
340
|
-
|
|
341
|
-
return `${normalizedMethod}(${payloadHash}):${normalizedUrl}`;
|
|
412
|
+
return uniqueKey;
|
|
342
413
|
}
|
|
343
414
|
/** @internal */
|
|
344
415
|
static hashPayload(payload) {
|
|
345
416
|
return crypto.createHash('sha256').update(payload).digest('base64').replace(/[+/=]/g, '').substring(0, 8);
|
|
346
417
|
}
|
|
347
418
|
}
|
|
348
|
-
|
|
419
|
+
export { CrawleeRequest as Request };
|