@crawlee/core 4.0.0-beta.12 → 4.0.0-beta.121
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -13
- package/autoscaling/autoscaled_pool.d.ts +67 -172
- package/autoscaling/autoscaled_pool.js +165 -320
- package/autoscaling/client_load_signal.d.ts +55 -0
- package/autoscaling/client_load_signal.js +73 -0
- package/autoscaling/concurrency_system.d.ts +268 -0
- package/autoscaling/concurrency_system.js +351 -0
- package/autoscaling/cpu_load_signal.d.ts +43 -0
- package/autoscaling/cpu_load_signal.js +47 -0
- package/autoscaling/event_loop_load_signal.d.ts +51 -0
- package/autoscaling/event_loop_load_signal.js +60 -0
- package/autoscaling/index.d.ts +6 -1
- package/autoscaling/index.js +6 -1
- package/autoscaling/load_signal.d.ts +99 -0
- package/autoscaling/load_signal.js +104 -0
- package/autoscaling/memory_load_signal.d.ts +47 -0
- package/autoscaling/memory_load_signal.js +106 -0
- package/autoscaling/snapshotter.d.ts +58 -163
- package/autoscaling/snapshotter.js +45 -263
- package/autoscaling/system_status.d.ts +62 -84
- package/autoscaling/system_status.js +92 -122
- package/autoscaling/weighted_avg.d.ts +5 -0
- package/autoscaling/weighted_avg.js +14 -0
- package/byte_utils.d.ts +17 -0
- package/byte_utils.js +42 -0
- package/configuration.d.ts +96 -223
- package/configuration.js +170 -222
- package/cookie_utils.d.ts +4 -3
- package/cookie_utils.js +22 -13
- package/crawlers/context_pipeline.d.ts +10 -1
- package/crawlers/context_pipeline.js +31 -8
- package/crawlers/crawler_commons.d.ts +90 -83
- package/crawlers/crawler_commons.js +1 -116
- package/crawlers/error_snapshotter.d.ts +2 -5
- package/crawlers/error_snapshotter.js +7 -8
- package/crawlers/error_tracker.d.ts +0 -1
- package/crawlers/error_tracker.js +0 -1
- package/crawlers/index.d.ts +1 -3
- package/crawlers/index.js +0 -3
- package/crawlers/internals/types.d.ts +0 -1
- package/crawlers/internals/types.js +0 -1
- package/crawlers/statistics.d.ts +143 -59
- package/crawlers/statistics.js +243 -153
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/enqueue_links/enqueue_links.d.ts +59 -68
- package/enqueue_links/enqueue_links.js +57 -62
- package/enqueue_links/index.d.ts +0 -1
- package/enqueue_links/index.js +0 -1
- package/enqueue_links/shared.d.ts +40 -27
- package/enqueue_links/shared.js +90 -68
- package/errors.d.ts +72 -4
- package/errors.js +89 -5
- package/events/event_manager.d.ts +35 -9
- package/events/event_manager.js +10 -12
- package/events/index.d.ts +0 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -3
- package/events/local_event_manager.js +39 -13
- package/http.d.ts +9 -0
- package/http.js +28 -0
- package/index.d.ts +7 -4
- package/index.js +6 -3
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +82 -3
- package/log.js +106 -1
- package/memory-storage/consts.d.ts +4 -0
- package/memory-storage/consts.js +4 -0
- package/memory-storage/index.d.ts +1 -0
- package/memory-storage/index.js +1 -0
- package/memory-storage/memory-storage.d.ts +38 -0
- package/memory-storage/memory-storage.js +130 -0
- package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
- package/memory-storage/resource-clients/common/base-client.js +6 -0
- package/memory-storage/resource-clients/dataset.d.ts +40 -0
- package/memory-storage/resource-clients/dataset.js +114 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +204 -0
- package/memory-storage/resource-clients/request-queue.d.ts +77 -0
- package/memory-storage/resource-clients/request-queue.js +422 -0
- package/memory-storage/utils.d.ts +16 -0
- package/memory-storage/utils.js +41 -0
- package/owned_or_injected.d.ts +58 -0
- package/owned_or_injected.js +98 -0
- package/package.json +13 -12
- package/proxy_configuration.d.ts +24 -132
- package/proxy_configuration.js +24 -143
- package/recoverable_state.d.ts +140 -0
- package/recoverable_state.js +212 -0
- package/request.d.ts +86 -17
- package/request.js +120 -41
- package/router.d.ts +193 -21
- package/router.js +188 -43
- package/serialization.d.ts +0 -1
- package/serialization.js +9 -11
- package/service_locator.d.ts +165 -0
- package/service_locator.js +253 -0
- package/session_pool/consts.d.ts +1 -2
- package/session_pool/consts.js +1 -2
- package/session_pool/errors.d.ts +0 -1
- package/session_pool/errors.js +0 -1
- package/session_pool/fingerprint.d.ts +9 -0
- package/session_pool/fingerprint.js +30 -0
- package/session_pool/index.d.ts +0 -2
- package/session_pool/index.js +0 -2
- package/session_pool/session.d.ts +35 -89
- package/session_pool/session.js +82 -142
- package/session_pool/session_pool.d.ts +69 -90
- package/session_pool/session_pool.js +151 -150
- package/storages/batched_adds.d.ts +37 -0
- package/storages/batched_adds.js +73 -0
- package/storages/dataset.d.ts +114 -54
- package/storages/dataset.js +285 -144
- package/storages/index.d.ts +10 -8
- package/storages/index.js +8 -8
- package/storages/key_value_store.d.ts +185 -42
- package/storages/key_value_store.js +424 -151
- package/storages/key_value_store_codec.d.ts +32 -0
- package/storages/key_value_store_codec.js +113 -0
- package/storages/request_dedup_cache.d.ts +22 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +52 -116
- package/storages/request_list.js +159 -133
- package/storages/request_loader.d.ts +101 -0
- package/storages/request_loader.js +1 -0
- package/storages/request_manager.d.ts +33 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +97 -0
- package/storages/request_manager_tandem.js +197 -0
- package/storages/request_queue.d.ts +290 -47
- package/storages/request_queue.js +757 -216
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +37 -88
- package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +137 -143
- package/storages/storage_instance_manager.d.ts +87 -0
- package/storages/storage_instance_manager.js +256 -0
- package/storages/storage_stats.d.ts +48 -0
- package/storages/storage_stats.js +29 -0
- package/storages/throttling_request_manager.d.ts +216 -0
- package/storages/throttling_request_manager.js +453 -0
- package/storages/transaction.d.ts +252 -0
- package/storages/transaction.js +251 -0
- package/storages/utils.d.ts +58 -11
- package/storages/utils.js +64 -13
- package/system-info/cpu-info.d.ts +67 -0
- package/system-info/cpu-info.js +216 -0
- package/system-info/memory-info.d.ts +31 -0
- package/system-info/memory-info.js +115 -0
- package/system-info/ps-tree.d.ts +17 -0
- package/system-info/ps-tree.js +144 -0
- package/system-info/runtime.d.ts +14 -0
- package/system-info/runtime.js +80 -0
- package/typedefs.d.ts +0 -6
- package/typedefs.js +0 -1
- package/url.d.ts +9 -0
- package/url.js +11 -0
- package/validators.d.ts +8 -1
- package/validators.js +10 -3
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js.map +0 -1
- package/configuration.d.ts.map +0 -1
- package/configuration.js.map +0 -1
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js.map +0 -1
- package/crawlers/context_pipeline.d.ts.map +0 -1
- package/crawlers/context_pipeline.js.map +0 -1
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_utils.d.ts +0 -10
- package/crawlers/crawler_utils.d.ts.map +0 -1
- package/crawlers/crawler_utils.js +0 -12
- package/crawlers/crawler_utils.js.map +0 -1
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js.map +0 -1
- package/crawlers/internals/types.d.ts.map +0 -1
- package/crawlers/internals/types.js.map +0 -1
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js.map +0 -1
- package/errors.d.ts.map +0 -1
- package/errors.js.map +0 -1
- package/events/event_manager.d.ts.map +0 -1
- package/events/event_manager.js.map +0 -1
- package/events/index.d.ts.map +0 -1
- package/events/index.js.map +0 -1
- package/events/local_event_manager.d.ts.map +0 -1
- package/events/local_event_manager.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -134
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -33
- package/http_clients/base-http-client.js.map +0 -1
- package/http_clients/form-data-like.d.ts +0 -67
- package/http_clients/form-data-like.d.ts.map +0 -1
- package/http_clients/form-data-like.js +0 -5
- package/http_clients/form-data-like.js.map +0 -1
- package/http_clients/got-scraping-http-client.d.ts +0 -15
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -69
- package/http_clients/got-scraping-http-client.js.map +0 -1
- package/http_clients/index.d.ts +0 -3
- package/http_clients/index.d.ts.map +0 -1
- package/http_clients/index.js +0 -3
- package/http_clients/index.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/log.d.ts.map +0 -1
- package/log.js.map +0 -1
- package/proxy_configuration.d.ts.map +0 -1
- package/proxy_configuration.js.map +0 -1
- package/request.d.ts.map +0 -1
- package/request.js.map +0 -1
- package/router.d.ts.map +0 -1
- package/router.js.map +0 -1
- package/serialization.d.ts.map +0 -1
- package/serialization.js.map +0 -1
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js.map +0 -1
- package/session_pool/events.d.ts +0 -3
- package/session_pool/events.d.ts.map +0 -1
- package/session_pool/events.js +0 -3
- package/session_pool/events.js.map +0 -1
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js.map +0 -1
- package/storages/access_checking.d.ts +0 -13
- package/storages/access_checking.d.ts.map +0 -1
- package/storages/access_checking.js +0 -14
- package/storages/access_checking.js.map +0 -1
- package/storages/dataset.d.ts.map +0 -1
- package/storages/dataset.js.map +0 -1
- package/storages/index.d.ts.map +0 -1
- package/storages/index.js.map +0 -1
- package/storages/key_value_store.d.ts.map +0 -1
- package/storages/key_value_store.js.map +0 -1
- package/storages/request_list.d.ts.map +0 -1
- package/storages/request_list.js.map +0 -1
- package/storages/request_provider.d.ts +0 -308
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -555
- package/storages/request_provider.js.map +0 -1
- package/storages/request_queue.d.ts.map +0 -1
- package/storages/request_queue.js.map +0 -1
- package/storages/request_queue_v2.d.ts +0 -87
- package/storages/request_queue_v2.d.ts.map +0 -1
- package/storages/request_queue_v2.js +0 -438
- package/storages/request_queue_v2.js.map +0 -1
- package/storages/sitemap_request_list.d.ts.map +0 -1
- package/storages/sitemap_request_list.js.map +0 -1
- package/storages/storage_manager.d.ts +0 -58
- package/storages/storage_manager.d.ts.map +0 -1
- package/storages/storage_manager.js +0 -105
- package/storages/storage_manager.js.map +0 -1
- package/storages/utils.d.ts.map +0 -1
- package/storages/utils.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
- package/typedefs.d.ts.map +0 -1
- package/typedefs.js.map +0 -1
- package/validators.d.ts.map +0 -1
- package/validators.js.map +0 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { addTimeoutToPromise, storage as timeoutStorage } from '@apify/timeout';
|
|
2
|
+
import { EventType, KeyValueStore, serviceLocator, StateValidationError } from '@crawlee/core';
|
|
3
|
+
const DEFAULT_PERSISTENCE_TIMEOUT_MILLIS = 60_000;
|
|
4
|
+
/**
|
|
5
|
+
* A class for managing persistent recoverable state using a plain JavaScript object.
|
|
6
|
+
*
|
|
7
|
+
* This class facilitates state persistence to a `KeyValueStore`, allowing data to be saved and retrieved
|
|
8
|
+
* across migrations or restarts. It manages the loading, saving, and resetting of state data,
|
|
9
|
+
* with optional persistence capabilities.
|
|
10
|
+
*
|
|
11
|
+
* The state is represented by a plain JavaScript object that can be serialized to and deserialized from JSON.
|
|
12
|
+
* The class automatically hooks into the event system to persist state when needed.
|
|
13
|
+
*/
|
|
14
|
+
export class RecoverableState {
|
|
15
|
+
#defaultState;
|
|
16
|
+
#state = null;
|
|
17
|
+
#initialized = false;
|
|
18
|
+
#listening = false;
|
|
19
|
+
#persistenceEnabled;
|
|
20
|
+
#persistStateKey;
|
|
21
|
+
#persistenceTimeoutMillis;
|
|
22
|
+
#configuration;
|
|
23
|
+
#keyValueStore;
|
|
24
|
+
#log;
|
|
25
|
+
#serialize;
|
|
26
|
+
#deserialize;
|
|
27
|
+
#persistStateQuietly;
|
|
28
|
+
/**
|
|
29
|
+
* Initialize a new recoverable state object.
|
|
30
|
+
*
|
|
31
|
+
* @param options Configuration options for the recoverable state
|
|
32
|
+
*/
|
|
33
|
+
constructor(options) {
|
|
34
|
+
const { defaultState } = options;
|
|
35
|
+
this.#defaultState =
|
|
36
|
+
typeof defaultState === 'function'
|
|
37
|
+
? defaultState
|
|
38
|
+
: () => structuredClone(defaultState);
|
|
39
|
+
this.#persistStateKey = options.persistStateKey;
|
|
40
|
+
this.#persistenceEnabled = options.persistenceEnabled ?? false;
|
|
41
|
+
this.#persistenceTimeoutMillis = options.persistenceTimeoutMillis ?? DEFAULT_PERSISTENCE_TIMEOUT_MILLIS;
|
|
42
|
+
this.#configuration = options.configuration;
|
|
43
|
+
this.#keyValueStore = options.keyValueStore ?? null;
|
|
44
|
+
this.#log = options.logger ?? serviceLocator.getLogger().child({ prefix: 'RecoverableState' });
|
|
45
|
+
this.#serialize = this.#toConversion(options.serialize);
|
|
46
|
+
this.#deserialize = this.#toConversion(options.deserialize);
|
|
47
|
+
// The automatic persists, where a rejection has nowhere useful to go - the event manager does not catch
|
|
48
|
+
// listener errors, and throwing from teardown would bury the outcome of the work it cleans up after.
|
|
49
|
+
this.#persistStateQuietly = async (eventData) => this.persistState(eventData).catch((error) => this.#log.warning(`Failed to persist the state under key '${this.#persistStateKey}'.`, { error }));
|
|
50
|
+
}
|
|
51
|
+
/** Normalizes a conversion option into a function. Absent conversions pass the value through unchanged. */
|
|
52
|
+
#toConversion(conversion) {
|
|
53
|
+
if (conversion === undefined) {
|
|
54
|
+
return async (value) => value;
|
|
55
|
+
}
|
|
56
|
+
if (typeof conversion === 'function') {
|
|
57
|
+
return async (value) => conversion(value);
|
|
58
|
+
}
|
|
59
|
+
return async (value) => {
|
|
60
|
+
const result = await conversion['~standard'].validate(value);
|
|
61
|
+
if (result.issues) {
|
|
62
|
+
throw new StateValidationError(this.#persistStateKey, result.issues);
|
|
63
|
+
}
|
|
64
|
+
return result.value;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Initialize the recoverable state.
|
|
69
|
+
*
|
|
70
|
+
* If persistence is enabled, this method loads the saved state and registers the object to listen for
|
|
71
|
+
* PERSIST_STATE events. A state established beforehand by {@link RecoverableState.reset} survives if there
|
|
72
|
+
* is no record to restore.
|
|
73
|
+
*
|
|
74
|
+
* Calling this again after a {@link RecoverableState.teardown} starts a new persistence window - the
|
|
75
|
+
* listener is registered again and the record reloaded.
|
|
76
|
+
*
|
|
77
|
+
* @returns The loaded state object
|
|
78
|
+
*/
|
|
79
|
+
async initialize() {
|
|
80
|
+
if (this.#initialized) {
|
|
81
|
+
return this.currentValue;
|
|
82
|
+
}
|
|
83
|
+
if (this.#persistenceEnabled) {
|
|
84
|
+
this.#keyValueStore ??= KeyValueStore.open(null, {
|
|
85
|
+
configuration: this.#configuration ?? serviceLocator.getConfiguration(),
|
|
86
|
+
});
|
|
87
|
+
await this.#resolveKeyValueStore();
|
|
88
|
+
serviceLocator.getEventManager().on(EventType.PERSIST_STATE, this.#persistStateQuietly);
|
|
89
|
+
this.#listening = true;
|
|
90
|
+
}
|
|
91
|
+
// Flipped before the record is loaded, so that a caller catching a `StateValidationError` is left with a
|
|
92
|
+
// fully wired object running on the default state rather than a half-initialized one.
|
|
93
|
+
this.#initialized = true;
|
|
94
|
+
this.#state ??= this.#defaultState();
|
|
95
|
+
await this.#loadSavedState();
|
|
96
|
+
return this.currentValue;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Clean up resources used by the recoverable state.
|
|
100
|
+
*
|
|
101
|
+
* If persistence is enabled, this method deregisters the object from PERSIST_STATE events
|
|
102
|
+
* and persists the current state one last time, warning rather than throwing if that write fails - cleanup
|
|
103
|
+
* runs when the work is already done, and failing it would bury whatever the caller was doing. The in-memory
|
|
104
|
+
* state is left alone, and {@link RecoverableState.initialize} can be called again to open a new
|
|
105
|
+
* persistence window.
|
|
106
|
+
*/
|
|
107
|
+
async teardown() {
|
|
108
|
+
this.#initialized = false;
|
|
109
|
+
if (!this.#persistenceEnabled) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
serviceLocator.getEventManager().off(EventType.PERSIST_STATE, this.#persistStateQuietly);
|
|
113
|
+
this.#listening = false;
|
|
114
|
+
await this.#persistStateQuietly();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get the current state.
|
|
118
|
+
*
|
|
119
|
+
* Throws until the state has been established, by either {@link RecoverableState.initialize} or the
|
|
120
|
+
* synchronous {@link RecoverableState.reset} - the latter being how a caller that cannot await in its
|
|
121
|
+
* constructor gets a usable state right away.
|
|
122
|
+
*/
|
|
123
|
+
get currentValue() {
|
|
124
|
+
if (this.#state === null) {
|
|
125
|
+
throw new Error('Recoverable state has not yet been loaded - call initialize() or reset() first');
|
|
126
|
+
}
|
|
127
|
+
return this.#state;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Reset the in-memory state to the default values, leaving any persisted record alone.
|
|
131
|
+
*
|
|
132
|
+
* Use {@link RecoverableState.resetStore} to clear the persisted record as well.
|
|
133
|
+
*/
|
|
134
|
+
reset() {
|
|
135
|
+
this.#state = this.#defaultState();
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Clear the persisted state record, leaving the in-memory state alone.
|
|
139
|
+
*
|
|
140
|
+
* This is a between-lifecycles operation - its point is to stop the next {@link RecoverableState.initialize}
|
|
141
|
+
* from restoring the record, so it throws while PERSIST_STATE events are still being handled, where the next
|
|
142
|
+
* one would write the record straight back. Use {@link RecoverableState.reset} to reset the state itself,
|
|
143
|
+
* or {@link RecoverableState.teardown} before clearing the record.
|
|
144
|
+
*
|
|
145
|
+
* A no-op if persistence is disabled or no KeyValueStore is available yet.
|
|
146
|
+
*/
|
|
147
|
+
async resetStore() {
|
|
148
|
+
if (this.#listening) {
|
|
149
|
+
throw new Error(`Cannot clear the state persisted under key '${this.#persistStateKey}' while it is still being persisted periodically - the next PERSIST_STATE event would write it straight back. Use reset() to reset the state itself, or teardown() before clearing the record.`);
|
|
150
|
+
}
|
|
151
|
+
if (!this.#persistenceEnabled) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const keyValueStore = await this.#resolveKeyValueStore();
|
|
155
|
+
if (keyValueStore === null) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
await this.#withTimeout(async () => keyValueStore.setValue(this.#persistStateKey, null), 'Clearing the persisted state');
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Persist the current state to the KeyValueStore.
|
|
162
|
+
*
|
|
163
|
+
* This method is typically called in response to a PERSIST_STATE event, but can also be called
|
|
164
|
+
* directly when needed. It is a no-op if persistence is disabled, if no KeyValueStore is available yet, or if
|
|
165
|
+
* there is no state to write. A failed write only rejects here - the periodic and teardown ones warn instead.
|
|
166
|
+
*
|
|
167
|
+
* @param eventData Optional data associated with a PERSIST_STATE event
|
|
168
|
+
*/
|
|
169
|
+
async persistState(eventData) {
|
|
170
|
+
if (!this.#persistenceEnabled || this.#state === null) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const keyValueStore = await this.#resolveKeyValueStore();
|
|
174
|
+
if (keyValueStore === null) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
this.#log.debug(`Persisting state of the RecoverableState (eventData=${JSON.stringify(eventData)}).`);
|
|
178
|
+
const serializedState = await this.#serialize(this.currentValue);
|
|
179
|
+
await this.#withTimeout(async () => keyValueStore.setValue(this.#persistStateKey, serializedState), 'Persisting the state');
|
|
180
|
+
}
|
|
181
|
+
/** Awaits a store handed over as a pending `open()`, keeping the resolved instance for later calls. */
|
|
182
|
+
async #resolveKeyValueStore() {
|
|
183
|
+
if (this.#keyValueStore === null) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
this.#keyValueStore = await this.#keyValueStore;
|
|
187
|
+
return this.#keyValueStore;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Load the saved state from the KeyValueStore. Leaves the current state alone if there is no record to load.
|
|
191
|
+
*/
|
|
192
|
+
async #loadSavedState() {
|
|
193
|
+
if (!this.#persistenceEnabled) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const keyValueStore = await this.#resolveKeyValueStore();
|
|
197
|
+
if (keyValueStore === null) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const storedState = await this.#withTimeout(async () => keyValueStore.getValue(this.#persistStateKey), 'Loading the persisted state');
|
|
201
|
+
if (storedState === null || storedState === undefined) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
this.#state = await this.#deserialize(storedState);
|
|
205
|
+
}
|
|
206
|
+
async #withTimeout(operation, description) {
|
|
207
|
+
// `@apify/timeout` shares one `AbortController` across nested frames and `KeyValueStore` checks it on
|
|
208
|
+
// every operation, so a teardown-time persist running inside an already-expired request handler timeout
|
|
209
|
+
// would be aborted before it started. Hence a fresh timeout context.
|
|
210
|
+
return timeoutStorage.exit(async () => addTimeoutToPromise(operation, this.#persistenceTimeoutMillis, `${description} under key '${this.#persistStateKey}' timed out after ${this.#persistenceTimeoutMillis / 1000} seconds.`));
|
|
211
|
+
}
|
|
212
|
+
}
|
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,8 @@ export declare enum RequestState {
|
|
|
43
43
|
* ```
|
|
44
44
|
* @category Sources
|
|
45
45
|
*/
|
|
46
|
-
|
|
46
|
+
declare class CrawleeRequest<UserData extends Dictionary = Dictionary> {
|
|
47
|
+
#private;
|
|
47
48
|
/** Request ID */
|
|
48
49
|
id?: string;
|
|
49
50
|
/** URL of the web page to crawl. */
|
|
@@ -74,9 +75,12 @@ export declare class Request<UserData extends Dictionary = Dictionary> {
|
|
|
74
75
|
errorMessages: string[];
|
|
75
76
|
/** Object with HTTP headers. Key is header name, value is the value. */
|
|
76
77
|
headers?: Record<string, string>;
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Custom user data assigned to the request.
|
|
80
|
+
*
|
|
81
|
+
* All data stored in `userData` must be JSON-serializable.
|
|
82
|
+
* Storing non-serializable values (e.g. functions, symbols) may result in unexpected results.
|
|
83
|
+
*/
|
|
80
84
|
userData: UserData;
|
|
81
85
|
/**
|
|
82
86
|
* ISO datetime string that indicates the time when the request has been processed.
|
|
@@ -87,14 +91,40 @@ export declare class Request<UserData extends Dictionary = Dictionary> {
|
|
|
87
91
|
* `Request` parameters including the URL, HTTP method and headers, and others.
|
|
88
92
|
*/
|
|
89
93
|
constructor(options: RequestOptions<UserData>);
|
|
90
|
-
/**
|
|
94
|
+
/**
|
|
95
|
+
* Converts the Crawlee Request object to a `fetch` API Request object.
|
|
96
|
+
* @returns The native `fetch` API Request object.
|
|
97
|
+
*/
|
|
98
|
+
intoFetchAPIRequest(): Request;
|
|
99
|
+
/**
|
|
100
|
+
* Tells the crawler processing this request to skip the navigation and process the request directly.
|
|
101
|
+
*
|
|
102
|
+
* When this is set to `true`, the crawling context will not contain the results of the navigation
|
|
103
|
+
* (e.g. `response`, `body`, `contentType`, `$` or `request.loadedUrl`).
|
|
104
|
+
* Accessing these properties will throw a {@link NavigationSkippedError} at runtime.
|
|
105
|
+
*/
|
|
91
106
|
get skipNavigation(): boolean;
|
|
92
|
-
/**
|
|
107
|
+
/**
|
|
108
|
+
* Tells the crawler processing this request to skip the navigation and process the request directly.
|
|
109
|
+
*
|
|
110
|
+
* When this is set to `true`, the crawling context will not contain the results of the navigation
|
|
111
|
+
* (e.g. `response`, `body`, `contentType`, `$` or `request.loadedUrl`).
|
|
112
|
+
* Accessing these properties will throw a {@link NavigationSkippedError} at runtime.
|
|
113
|
+
*/
|
|
93
114
|
set skipNavigation(value: boolean);
|
|
94
|
-
/**
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Depth of the request in the current crawl tree.
|
|
117
|
+
* Note that this is dependent on the crawler setup and might produce unexpected results when used with multiple crawlers.
|
|
118
|
+
*/
|
|
119
|
+
get crawlDepth(): number;
|
|
120
|
+
/**
|
|
121
|
+
* Depth of the request in the current crawl tree.
|
|
122
|
+
* Note that this is dependent on the crawler setup and might produce unexpected results when used with multiple crawlers.
|
|
123
|
+
*/
|
|
124
|
+
set crawlDepth(value: number);
|
|
125
|
+
/** 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. */
|
|
126
|
+
get sessionId(): string | undefined;
|
|
127
|
+
set sessionId(value: string | undefined);
|
|
98
128
|
/** shortcut for getting `request.userData.label` */
|
|
99
129
|
get label(): string | undefined;
|
|
100
130
|
/** shortcut for setting `request.userData.label` */
|
|
@@ -107,6 +137,14 @@ export declare class Request<UserData extends Dictionary = Dictionary> {
|
|
|
107
137
|
get state(): RequestState;
|
|
108
138
|
/** Describes the request's current lifecycle state. */
|
|
109
139
|
set state(value: RequestState);
|
|
140
|
+
/**
|
|
141
|
+
* Reason for skipping this request.
|
|
142
|
+
*/
|
|
143
|
+
get skippedReason(): SkippedRequestReason | undefined;
|
|
144
|
+
/**
|
|
145
|
+
* Reason for skipping this request.
|
|
146
|
+
*/
|
|
147
|
+
set skippedReason(value: SkippedRequestReason | undefined);
|
|
110
148
|
private get enqueueStrategy();
|
|
111
149
|
private set enqueueStrategy(value);
|
|
112
150
|
/**
|
|
@@ -124,7 +162,7 @@ export declare class Request<UserData extends Dictionary = Dictionary> {
|
|
|
124
162
|
*/
|
|
125
163
|
pushErrorMessage(errorOrMessage: unknown, options?: PushErrorMessageOptions): void;
|
|
126
164
|
/** @internal */
|
|
127
|
-
static computeUniqueKey({ url, method, payload, keepUrlFragment, useExtendedUniqueKey, }: ComputeUniqueKeyOptions): string;
|
|
165
|
+
static computeUniqueKey({ url, method, payload, keepUrlFragment, useExtendedUniqueKey, alwaysEnqueue, }: ComputeUniqueKeyOptions): string;
|
|
128
166
|
/** @internal */
|
|
129
167
|
static hashPayload(payload: BinaryLike): string;
|
|
130
168
|
}
|
|
@@ -145,7 +183,7 @@ export interface RequestOptions<UserData extends Dictionary = Dictionary> {
|
|
|
145
183
|
* The `keepUrlFragment` option determines whether URL hash fragment is included in the `uniqueKey` or not.
|
|
146
184
|
*
|
|
147
185
|
* The `useExtendedUniqueKey` options determines whether method and payload are included in the `uniqueKey`,
|
|
148
|
-
* producing a `uniqueKey` in the following format: `METHOD
|
|
186
|
+
* producing a `uniqueKey` in the following format: `METHOD|payloadHash|normalizedUrl`. This is useful
|
|
149
187
|
* when requests point to the same URL, but with different methods and payloads. For example: form submits.
|
|
150
188
|
*
|
|
151
189
|
* Pass an arbitrary non-empty text value to the `uniqueKey` property
|
|
@@ -169,6 +207,9 @@ export interface RequestOptions<UserData extends Dictionary = Dictionary> {
|
|
|
169
207
|
/**
|
|
170
208
|
* Custom user data assigned to the request. Use this to save any request related data to the
|
|
171
209
|
* request's scope, keeping them accessible on retries, failures etc.
|
|
210
|
+
*
|
|
211
|
+
* All data stored in `userData` must be JSON-serializable.
|
|
212
|
+
* Storing non-serializable values (e.g. functions, symbols) may result in unexpected results.
|
|
172
213
|
*/
|
|
173
214
|
userData?: UserData;
|
|
174
215
|
/**
|
|
@@ -190,17 +231,45 @@ export interface RequestOptions<UserData extends Dictionary = Dictionary> {
|
|
|
190
231
|
* @default false
|
|
191
232
|
*/
|
|
192
233
|
useExtendedUniqueKey?: boolean;
|
|
234
|
+
/**
|
|
235
|
+
* If `true` then a random value is included in the `uniqueKey` computation, ensuring the request
|
|
236
|
+
* is always enqueued even if a request with the same URL (and method/payload) is already present
|
|
237
|
+
* in the queue. Cannot be used together with a custom `uniqueKey`.
|
|
238
|
+
* @default false
|
|
239
|
+
*/
|
|
240
|
+
alwaysEnqueue?: boolean;
|
|
193
241
|
/**
|
|
194
242
|
* The `true` value indicates that the request will not be automatically retried on error.
|
|
195
243
|
* @default false
|
|
196
244
|
*/
|
|
197
245
|
noRetry?: boolean;
|
|
246
|
+
/**
|
|
247
|
+
* ID of a session from the crawler's `SessionPool` to use for this request.
|
|
248
|
+
* When set, the crawler will fetch this session from the pool instead of creating a new one.
|
|
249
|
+
*/
|
|
250
|
+
sessionId?: string;
|
|
198
251
|
/**
|
|
199
252
|
* If set to `true` then the crawler processing this request evaluates
|
|
200
253
|
* the `requestHandler` immediately without prior browser navigation.
|
|
254
|
+
*
|
|
255
|
+
* When enabled, the crawling context will not contain the results of the navigation
|
|
256
|
+
* (e.g. `response`, `body`, `contentType`, `$` or `request.loadedUrl`).
|
|
257
|
+
* Accessing these properties will throw a {@link NavigationSkippedError} at runtime.
|
|
201
258
|
* @default false
|
|
202
259
|
*/
|
|
203
260
|
skipNavigation?: boolean;
|
|
261
|
+
/**
|
|
262
|
+
* Depth of the request in the current crawl tree.
|
|
263
|
+
* Note that this is dependent on the crawler setup and might produce unexpected results when used with multiple crawlers.
|
|
264
|
+
* @default 0
|
|
265
|
+
*/
|
|
266
|
+
crawlDepth?: number;
|
|
267
|
+
/**
|
|
268
|
+
* Reason for skipping this request.
|
|
269
|
+
* This is used to provide more information about why the request was skipped.
|
|
270
|
+
* @internal
|
|
271
|
+
*/
|
|
272
|
+
skippedReason?: SkippedRequestReason;
|
|
204
273
|
/**
|
|
205
274
|
* Maximum number of retries for this request. Allows to override the global `maxRequestRetries` option of `BasicCrawler`.
|
|
206
275
|
*/
|
|
@@ -227,15 +296,15 @@ interface ComputeUniqueKeyOptions {
|
|
|
227
296
|
payload?: string | Buffer;
|
|
228
297
|
keepUrlFragment?: boolean;
|
|
229
298
|
useExtendedUniqueKey?: boolean;
|
|
299
|
+
alwaysEnqueue?: boolean;
|
|
230
300
|
}
|
|
231
301
|
export type Source = (Partial<RequestOptions> & {
|
|
232
302
|
requestsFromUrl?: string;
|
|
233
303
|
regex?: RegExp;
|
|
234
|
-
}) |
|
|
304
|
+
}) | CrawleeRequest;
|
|
235
305
|
/** @internal */
|
|
236
306
|
export interface InternalSource {
|
|
237
307
|
requestsFromUrl: string;
|
|
238
308
|
regex?: RegExp;
|
|
239
309
|
}
|
|
240
|
-
export {};
|
|
241
|
-
//# sourceMappingURL=request.d.ts.map
|
|
310
|
+
export { CrawleeRequest as Request };
|