@crawlee/core 3.0.0-beta.5 → 3.0.0-beta.52
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 +67 -71
- package/autoscaling/autoscaled_pool.d.ts +3 -3
- package/autoscaling/autoscaled_pool.d.ts.map +1 -1
- package/autoscaling/autoscaled_pool.js +1 -1
- package/autoscaling/autoscaled_pool.js.map +1 -1
- package/autoscaling/snapshotter.d.ts +5 -5
- package/autoscaling/snapshotter.d.ts.map +1 -1
- package/autoscaling/snapshotter.js +2 -2
- package/autoscaling/snapshotter.js.map +1 -1
- package/autoscaling/system_status.d.ts +1 -1
- package/autoscaling/system_status.d.ts.map +1 -1
- package/configuration.d.ts +12 -11
- package/configuration.d.ts.map +1 -1
- package/configuration.js +10 -8
- package/configuration.js.map +1 -1
- package/crawlers/crawler_commons.d.ts +12 -7
- package/crawlers/crawler_commons.d.ts.map +1 -1
- package/crawlers/crawler_utils.d.ts +1 -1
- package/crawlers/crawler_utils.d.ts.map +1 -1
- package/crawlers/statistics.d.ts.map +1 -1
- package/crawlers/statistics.js +1 -1
- package/crawlers/statistics.js.map +1 -1
- package/enqueue_links/enqueue_links.d.ts +20 -18
- package/enqueue_links/enqueue_links.d.ts.map +1 -1
- package/enqueue_links/enqueue_links.js +13 -13
- package/enqueue_links/enqueue_links.js.map +1 -1
- package/enqueue_links/shared.d.ts +11 -6
- package/enqueue_links/shared.d.ts.map +1 -1
- package/enqueue_links/shared.js +7 -2
- package/enqueue_links/shared.js.map +1 -1
- package/errors.d.ts +16 -0
- package/errors.d.ts.map +1 -0
- package/errors.js +22 -0
- package/errors.js.map +1 -0
- package/events/event_manager.d.ts +6 -5
- package/events/event_manager.d.ts.map +1 -1
- package/events/event_manager.js +7 -4
- package/events/event_manager.js.map +1 -1
- package/events/local_event_manager.d.ts +3 -5
- package/events/local_event_manager.d.ts.map +1 -1
- package/events/local_event_manager.js +3 -5
- package/events/local_event_manager.js.map +1 -1
- package/index.d.ts +3 -0
- package/index.d.ts.map +1 -1
- package/index.js +2 -0
- package/index.js.map +1 -1
- package/index.mjs +5 -0
- package/package.json +21 -20
- package/proxy_configuration.d.ts +7 -14
- package/proxy_configuration.d.ts.map +1 -1
- package/proxy_configuration.js +6 -15
- package/proxy_configuration.js.map +1 -1
- package/request.d.ts +11 -6
- package/request.d.ts.map +1 -1
- package/request.js +5 -1
- package/request.js.map +1 -1
- package/router.d.ts +93 -0
- package/router.d.ts.map +1 -0
- package/router.js +137 -0
- package/router.js.map +1 -0
- package/serialization.js +1 -1
- package/serialization.js.map +1 -1
- package/session_pool/session.d.ts +5 -5
- package/session_pool/session.d.ts.map +1 -1
- package/session_pool/session.js.map +1 -1
- package/session_pool/session_pool.d.ts +3 -2
- package/session_pool/session_pool.d.ts.map +1 -1
- package/session_pool/session_pool.js +4 -5
- package/session_pool/session_pool.js.map +1 -1
- package/session_pool/session_utils.d.ts +6 -3
- package/session_pool/session_utils.d.ts.map +1 -1
- package/session_pool/session_utils.js +3 -0
- package/session_pool/session_utils.js.map +1 -1
- package/storages/dataset.d.ts +34 -12
- package/storages/dataset.d.ts.map +1 -1
- package/storages/dataset.js +44 -12
- package/storages/dataset.js.map +1 -1
- package/storages/index.d.ts +1 -0
- package/storages/index.d.ts.map +1 -1
- package/storages/index.js +1 -0
- package/storages/index.js.map +1 -1
- package/storages/key_value_store.d.ts +120 -22
- package/storages/key_value_store.d.ts.map +1 -1
- package/storages/key_value_store.js +178 -24
- package/storages/key_value_store.js.map +1 -1
- package/storages/request_list.d.ts +8 -7
- package/storages/request_list.d.ts.map +1 -1
- package/storages/request_list.js +5 -3
- package/storages/request_list.js.map +1 -1
- package/storages/request_queue.d.ts +12 -8
- package/storages/request_queue.d.ts.map +1 -1
- package/storages/request_queue.js +18 -6
- package/storages/request_queue.js.map +1 -1
- package/storages/storage_manager.d.ts +9 -5
- package/storages/storage_manager.d.ts.map +1 -1
- package/storages/storage_manager.js +28 -9
- package/storages/storage_manager.js.map +1 -1
- package/storages/utils.d.ts +16 -0
- package/storages/utils.d.ts.map +1 -0
- package/storages/utils.js +26 -0
- package/storages/utils.js.map +1 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/typedefs.d.ts +0 -2
- package/typedefs.d.ts.map +1 -1
- package/typedefs.js.map +1 -1
- package/validators.d.ts +5 -5
- package/validators.d.ts.map +1 -1
- package/validators.js.map +1 -1
package/storages/index.d.ts
CHANGED
package/storages/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/storages/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/storages/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,SAAS,CAAC"}
|
package/storages/index.js
CHANGED
|
@@ -6,4 +6,5 @@ tslib_1.__exportStar(require("./key_value_store"), exports);
|
|
|
6
6
|
tslib_1.__exportStar(require("./request_list"), exports);
|
|
7
7
|
tslib_1.__exportStar(require("./request_queue"), exports);
|
|
8
8
|
tslib_1.__exportStar(require("./storage_manager"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./utils"), exports);
|
|
9
10
|
//# sourceMappingURL=index.js.map
|
package/storages/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/storages/index.ts"],"names":[],"mappings":";;;AAAA,oDAA0B;AAC1B,4DAAkC;AAClC,yDAA+B;AAC/B,0DAAgC;AAChC,4DAAkC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/storages/index.ts"],"names":[],"mappings":";;;AAAA,oDAA0B;AAC1B,4DAAkC;AAClC,yDAA+B;AAC/B,0DAAgC;AAChC,4DAAkC;AAClC,kDAAwB"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Dictionary, StorageClient } from '@crawlee/types';
|
|
2
3
|
import { Configuration } from '../configuration';
|
|
3
|
-
import { Awaitable } from '../typedefs';
|
|
4
|
-
import { StorageManagerOptions } from './storage_manager';
|
|
4
|
+
import type { Awaitable } from '../typedefs';
|
|
5
|
+
import type { StorageManagerOptions } from './storage_manager';
|
|
5
6
|
/**
|
|
6
7
|
* Helper function to possibly stringify value if options.contentType is not set.
|
|
7
8
|
*
|
|
@@ -14,19 +15,19 @@ export declare const maybeStringify: <T>(value: T, options: {
|
|
|
14
15
|
* The `KeyValueStore` class represents a key-value store, a simple data storage that is used
|
|
15
16
|
* for saving and reading data records or files. Each data record is
|
|
16
17
|
* represented by a unique key and associated with a MIME content type. Key-value stores are ideal
|
|
17
|
-
* for saving screenshots,
|
|
18
|
+
* for saving screenshots, crawler inputs and outputs, web pages, PDFs or to persist the state of crawlers.
|
|
18
19
|
*
|
|
19
20
|
* Do not instantiate this class directly, use the
|
|
20
21
|
* {@link KeyValueStore.open} function instead.
|
|
21
22
|
*
|
|
22
|
-
* Each
|
|
23
|
-
* for the run. By convention, the
|
|
23
|
+
* Each crawler run is associated with a default key-value store, which is created exclusively
|
|
24
|
+
* for the run. By convention, the crawler input and output are stored into the
|
|
24
25
|
* default key-value store under the `INPUT` and `OUTPUT` key, respectively.
|
|
25
26
|
* Typically, input and output are JSON files, although it can be any other format.
|
|
26
27
|
* To access the default key-value store directly, you can use the
|
|
27
|
-
* {@link
|
|
28
|
+
* {@link KeyValueStore.getValue} and {@link KeyValueStore.setValue} convenience functions.
|
|
28
29
|
*
|
|
29
|
-
* To access the input, you can also use the {@link
|
|
30
|
+
* To access the input, you can also use the {@link KeyValueStore.getInput} convenience function.
|
|
30
31
|
*
|
|
31
32
|
* `KeyValueStore` stores its data either on local disk or in the Apify cloud,
|
|
32
33
|
* depending on whether the [`APIFY_IS_AT_HOME`](/docs/guides/environment-variables#apify_is_at_home)
|
|
@@ -51,13 +52,13 @@ export declare const maybeStringify: <T>(value: T, options: {
|
|
|
51
52
|
* **Example usage:**
|
|
52
53
|
*
|
|
53
54
|
* ```javascript
|
|
54
|
-
* // Get
|
|
55
|
-
* const input = await
|
|
55
|
+
* // Get crawler input from the default key-value store.
|
|
56
|
+
* const input = await KeyValueStore.getInput();
|
|
56
57
|
* // Get some value from the default key-value store.
|
|
57
|
-
* const otherValue = await
|
|
58
|
+
* const otherValue = await KeyValueStore.getValue('my-key');
|
|
58
59
|
*
|
|
59
|
-
* // Write
|
|
60
|
-
* await
|
|
60
|
+
* // Write crawler output to the default key-value store.
|
|
61
|
+
* await KeyValueStore.setValue('OUTPUT', { myResult: 123 });
|
|
61
62
|
*
|
|
62
63
|
* // Open a named key-value store
|
|
63
64
|
* const store = await KeyValueStore.open('some-name');
|
|
@@ -79,7 +80,10 @@ export declare class KeyValueStore {
|
|
|
79
80
|
readonly config: Configuration;
|
|
80
81
|
readonly id: string;
|
|
81
82
|
readonly name?: string;
|
|
82
|
-
private client;
|
|
83
|
+
private readonly client;
|
|
84
|
+
private persistStateEventStarted;
|
|
85
|
+
/** Cache for persistent (auto-saved) values. When we try to set such value, the cache will be updated automatically. */
|
|
86
|
+
private readonly cache;
|
|
83
87
|
/**
|
|
84
88
|
* @internal
|
|
85
89
|
*/
|
|
@@ -109,12 +113,16 @@ export declare class KeyValueStore {
|
|
|
109
113
|
* @param key
|
|
110
114
|
* Unique key of the record. It can be at most 256 characters long and only consist
|
|
111
115
|
* of the following characters: `a`-`z`, `A`-`Z`, `0`-`9` and `!-_.'()`
|
|
116
|
+
* @param defaultValue
|
|
117
|
+
* Fallback that will be returned if no value if present in the storage.
|
|
112
118
|
* @returns
|
|
113
119
|
* Returns a promise that resolves to an object, string
|
|
114
120
|
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
115
121
|
* on the MIME content type of the record.
|
|
116
122
|
*/
|
|
117
|
-
getValue<T = unknown>(key: string): Promise<T | null>;
|
|
123
|
+
getValue<T = unknown>(key: string, defaultValue?: T): Promise<T | null>;
|
|
124
|
+
getAutoSavedValue<T extends Dictionary = Dictionary>(key: string, defaultValue?: T): Promise<T>;
|
|
125
|
+
private ensurePersistStateEvent;
|
|
118
126
|
/**
|
|
119
127
|
* Saves or deletes a record in the key-value store.
|
|
120
128
|
* The function returns a promise that resolves once the record has been saved or deleted.
|
|
@@ -145,7 +153,7 @@ export declare class KeyValueStore {
|
|
|
145
153
|
* {@link KeyValueStore.getValue} function.
|
|
146
154
|
*
|
|
147
155
|
* **IMPORTANT:** Always make sure to use the `await` keyword when calling `setValue()`,
|
|
148
|
-
* otherwise the
|
|
156
|
+
* otherwise the crawler process might finish before the value is stored!
|
|
149
157
|
*
|
|
150
158
|
* @param key
|
|
151
159
|
* Unique key of the record. It can be at most 256 characters long and only consist
|
|
@@ -164,11 +172,8 @@ export declare class KeyValueStore {
|
|
|
164
172
|
* depending on the mode of operation.
|
|
165
173
|
*/
|
|
166
174
|
drop(): Promise<void>;
|
|
167
|
-
/**
|
|
168
|
-
|
|
169
|
-
* access the value in the remote key-value store.
|
|
170
|
-
*/
|
|
171
|
-
getPublicUrl(key: string): string;
|
|
175
|
+
/** @internal */
|
|
176
|
+
clearCache(): void;
|
|
172
177
|
/**
|
|
173
178
|
* Iterates over key-value store keys, yielding each in turn to an `iteratee` function.
|
|
174
179
|
* Each invocation of `iteratee` is called with three arguments: `(key, index, info)`, where `key`
|
|
@@ -203,10 +208,103 @@ export declare class KeyValueStore {
|
|
|
203
208
|
*
|
|
204
209
|
* @param [storeIdOrName]
|
|
205
210
|
* ID or name of the key-value store to be opened. If `null` or `undefined`,
|
|
206
|
-
* the function returns the default key-value store associated with the
|
|
211
|
+
* the function returns the default key-value store associated with the crawler run.
|
|
207
212
|
* @param [options] Storage manager options.
|
|
208
213
|
*/
|
|
209
214
|
static open(storeIdOrName?: string | null, options?: StorageManagerOptions): Promise<KeyValueStore>;
|
|
215
|
+
/**
|
|
216
|
+
* Gets a value from the default {@link KeyValueStore} associated with the current crawler run.
|
|
217
|
+
*
|
|
218
|
+
* This is just a convenient shortcut for {@link KeyValueStore.getValue}.
|
|
219
|
+
* For example, calling the following code:
|
|
220
|
+
* ```javascript
|
|
221
|
+
* const value = await KeyValueStore.getValue('my-key');
|
|
222
|
+
* ```
|
|
223
|
+
*
|
|
224
|
+
* is equivalent to:
|
|
225
|
+
* ```javascript
|
|
226
|
+
* const store = await KeyValueStore.open();
|
|
227
|
+
* const value = await store.getValue('my-key');
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* To store the value to the default key-value store, you can use the {@link KeyValueStore.setValue} function.
|
|
231
|
+
*
|
|
232
|
+
* For more information, see {@link KeyValueStore.open}
|
|
233
|
+
* and {@link KeyValueStore.getValue}.
|
|
234
|
+
*
|
|
235
|
+
* @param key Unique record key.
|
|
236
|
+
* @param defaultValue Fallback that will be returned if no value if present in the storage.
|
|
237
|
+
* @returns
|
|
238
|
+
* Returns a promise that resolves to an object, string
|
|
239
|
+
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
240
|
+
* on the MIME content type of the record, or `null`
|
|
241
|
+
* if the record is missing.
|
|
242
|
+
* @ignore
|
|
243
|
+
*/
|
|
244
|
+
static getValue<T = unknown>(key: string, defaultValue?: T): Promise<T | null>;
|
|
245
|
+
static getAutoSavedValue<T extends Dictionary = Dictionary>(key: string, defaultValue?: T): Promise<T>;
|
|
246
|
+
/**
|
|
247
|
+
* Stores or deletes a value in the default {@link KeyValueStore} associated with the current crawler run.
|
|
248
|
+
*
|
|
249
|
+
* This is just a convenient shortcut for {@link KeyValueStore.setValue}.
|
|
250
|
+
* For example, calling the following code:
|
|
251
|
+
* ```javascript
|
|
252
|
+
* await KeyValueStore.setValue('OUTPUT', { foo: "bar" });
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* is equivalent to:
|
|
256
|
+
* ```javascript
|
|
257
|
+
* const store = await KeyValueStore.open();
|
|
258
|
+
* await store.setValue('OUTPUT', { foo: "bar" });
|
|
259
|
+
* ```
|
|
260
|
+
*
|
|
261
|
+
* To get a value from the default key-value store, you can use the {@link KeyValueStore.getValue} function.
|
|
262
|
+
*
|
|
263
|
+
* For more information, see {@link KeyValueStore.open}
|
|
264
|
+
* and {@link KeyValueStore.getValue}.
|
|
265
|
+
*
|
|
266
|
+
* @param key
|
|
267
|
+
* Unique record key.
|
|
268
|
+
* @param value
|
|
269
|
+
* Record data, which can be one of the following values:
|
|
270
|
+
* - If `null`, the record in the key-value store is deleted.
|
|
271
|
+
* - If no `options.contentType` is specified, `value` can be any JavaScript object, and it will be stringified to JSON.
|
|
272
|
+
* - If `options.contentType` is set, `value` is taken as is, and it must be a `String` or [`Buffer`](https://nodejs.org/api/buffer.html).
|
|
273
|
+
* For any other value an error will be thrown.
|
|
274
|
+
* @param [options]
|
|
275
|
+
* @ignore
|
|
276
|
+
*/
|
|
277
|
+
static setValue<T>(key: string, value: T | null, options?: RecordOptions): Promise<void>;
|
|
278
|
+
/**
|
|
279
|
+
* Gets the crawler input value from the default {@link KeyValueStore} associated with the current crawler run.
|
|
280
|
+
*
|
|
281
|
+
* This is just a convenient shortcut for [`keyValueStore.getValue('INPUT')`](core/class/KeyValueStore#getValue).
|
|
282
|
+
* For example, calling the following code:
|
|
283
|
+
* ```javascript
|
|
284
|
+
* const input = await KeyValueStore.getInput();
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* is equivalent to:
|
|
288
|
+
* ```javascript
|
|
289
|
+
* const store = await KeyValueStore.open();
|
|
290
|
+
* await store.getValue('INPUT');
|
|
291
|
+
* ```
|
|
292
|
+
*
|
|
293
|
+
* Note that the `getInput()` function does not cache the value read from the key-value store.
|
|
294
|
+
* If you need to use the input multiple times in your crawler,
|
|
295
|
+
* it is far more efficient to read it once and store it locally.
|
|
296
|
+
*
|
|
297
|
+
* For more information, see {@link KeyValueStore.open}
|
|
298
|
+
* and {@link KeyValueStore.getValue}.
|
|
299
|
+
*
|
|
300
|
+
* @returns
|
|
301
|
+
* Returns a promise that resolves to an object, string
|
|
302
|
+
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
303
|
+
* on the MIME content type of the record, or `null`
|
|
304
|
+
* if the record is missing.
|
|
305
|
+
* @ignore
|
|
306
|
+
*/
|
|
307
|
+
static getInput<T = Dictionary | string | Buffer>(): Promise<T | null>;
|
|
210
308
|
}
|
|
211
309
|
/**
|
|
212
310
|
* User-function used in the {@link KeyValueStore.forEachKey} method.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key_value_store.d.ts","sourceRoot":"","sources":["../../src/storages/key_value_store.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"key_value_store.d.ts","sourceRoot":"","sources":["../../src/storages/key_value_store.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,UAAU,EAAuB,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACrF,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAI/D;;;;GAIG;AACH,eAAO,MAAM,cAAc,yBAA0B;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,MAwB5E,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgEG;AACH,qBAAa,aAAa;IAYqB,QAAQ,CAAC,MAAM;IAX1D,QAAQ,CAAC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A4BG;WACU,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;WAKvE,iBAAiB,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,IAAU,GAAG,OAAO,CAAC,CAAC,CAAC;IAKlH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;WACU,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,GAAE,aAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;IAKlG;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;WACU,QAAQ,CAAC,CAAC,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,KAAK,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;CAI/E;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IACxB;;;;;OAKG;IACH,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;CACzE;AAED,MAAM,WAAW,oBAAoB;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,aAAa,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC1B;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,4BAA4B;IACzC;;OAEG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC9B"}
|
|
@@ -7,6 +7,7 @@ const utilities_1 = require("@apify/utilities");
|
|
|
7
7
|
const ow_1 = tslib_1.__importStar(require("ow"));
|
|
8
8
|
const configuration_1 = require("../configuration");
|
|
9
9
|
const storage_manager_1 = require("./storage_manager");
|
|
10
|
+
const utils_1 = require("./utils");
|
|
10
11
|
/**
|
|
11
12
|
* Helper function to possibly stringify value if options.contentType is not set.
|
|
12
13
|
*
|
|
@@ -40,19 +41,19 @@ exports.maybeStringify = maybeStringify;
|
|
|
40
41
|
* The `KeyValueStore` class represents a key-value store, a simple data storage that is used
|
|
41
42
|
* for saving and reading data records or files. Each data record is
|
|
42
43
|
* represented by a unique key and associated with a MIME content type. Key-value stores are ideal
|
|
43
|
-
* for saving screenshots,
|
|
44
|
+
* for saving screenshots, crawler inputs and outputs, web pages, PDFs or to persist the state of crawlers.
|
|
44
45
|
*
|
|
45
46
|
* Do not instantiate this class directly, use the
|
|
46
47
|
* {@link KeyValueStore.open} function instead.
|
|
47
48
|
*
|
|
48
|
-
* Each
|
|
49
|
-
* for the run. By convention, the
|
|
49
|
+
* Each crawler run is associated with a default key-value store, which is created exclusively
|
|
50
|
+
* for the run. By convention, the crawler input and output are stored into the
|
|
50
51
|
* default key-value store under the `INPUT` and `OUTPUT` key, respectively.
|
|
51
52
|
* Typically, input and output are JSON files, although it can be any other format.
|
|
52
53
|
* To access the default key-value store directly, you can use the
|
|
53
|
-
* {@link
|
|
54
|
+
* {@link KeyValueStore.getValue} and {@link KeyValueStore.setValue} convenience functions.
|
|
54
55
|
*
|
|
55
|
-
* To access the input, you can also use the {@link
|
|
56
|
+
* To access the input, you can also use the {@link KeyValueStore.getInput} convenience function.
|
|
56
57
|
*
|
|
57
58
|
* `KeyValueStore` stores its data either on local disk or in the Apify cloud,
|
|
58
59
|
* depending on whether the [`APIFY_IS_AT_HOME`](/docs/guides/environment-variables#apify_is_at_home)
|
|
@@ -77,13 +78,13 @@ exports.maybeStringify = maybeStringify;
|
|
|
77
78
|
* **Example usage:**
|
|
78
79
|
*
|
|
79
80
|
* ```javascript
|
|
80
|
-
* // Get
|
|
81
|
-
* const input = await
|
|
81
|
+
* // Get crawler input from the default key-value store.
|
|
82
|
+
* const input = await KeyValueStore.getInput();
|
|
82
83
|
* // Get some value from the default key-value store.
|
|
83
|
-
* const otherValue = await
|
|
84
|
+
* const otherValue = await KeyValueStore.getValue('my-key');
|
|
84
85
|
*
|
|
85
|
-
* // Write
|
|
86
|
-
* await
|
|
86
|
+
* // Write crawler output to the default key-value store.
|
|
87
|
+
* await KeyValueStore.setValue('OUTPUT', { myResult: 123 });
|
|
87
88
|
*
|
|
88
89
|
* // Open a named key-value store
|
|
89
90
|
* const store = await KeyValueStore.open('some-name');
|
|
@@ -130,6 +131,19 @@ class KeyValueStore {
|
|
|
130
131
|
writable: true,
|
|
131
132
|
value: void 0
|
|
132
133
|
});
|
|
134
|
+
Object.defineProperty(this, "persistStateEventStarted", {
|
|
135
|
+
enumerable: true,
|
|
136
|
+
configurable: true,
|
|
137
|
+
writable: true,
|
|
138
|
+
value: false
|
|
139
|
+
});
|
|
140
|
+
/** Cache for persistent (auto-saved) values. When we try to set such value, the cache will be updated automatically. */
|
|
141
|
+
Object.defineProperty(this, "cache", {
|
|
142
|
+
enumerable: true,
|
|
143
|
+
configurable: true,
|
|
144
|
+
writable: true,
|
|
145
|
+
value: new Map()
|
|
146
|
+
});
|
|
133
147
|
this.id = options.id;
|
|
134
148
|
this.name = options.name;
|
|
135
149
|
this.client = options.client.keyValueStore(this.id);
|
|
@@ -159,15 +173,37 @@ class KeyValueStore {
|
|
|
159
173
|
* @param key
|
|
160
174
|
* Unique key of the record. It can be at most 256 characters long and only consist
|
|
161
175
|
* of the following characters: `a`-`z`, `A`-`Z`, `0`-`9` and `!-_.'()`
|
|
176
|
+
* @param defaultValue
|
|
177
|
+
* Fallback that will be returned if no value if present in the storage.
|
|
162
178
|
* @returns
|
|
163
179
|
* Returns a promise that resolves to an object, string
|
|
164
180
|
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
165
181
|
* on the MIME content type of the record.
|
|
166
182
|
*/
|
|
167
|
-
async getValue(key) {
|
|
183
|
+
async getValue(key, defaultValue) {
|
|
168
184
|
(0, ow_1.default)(key, ow_1.default.string.nonEmpty);
|
|
169
185
|
const record = await this.client.getRecord(key);
|
|
170
|
-
return record?.value ?? null;
|
|
186
|
+
return record?.value ?? defaultValue ?? null;
|
|
187
|
+
}
|
|
188
|
+
async getAutoSavedValue(key, defaultValue = {}) {
|
|
189
|
+
if (this.cache.has(key)) {
|
|
190
|
+
return this.cache.get(key);
|
|
191
|
+
}
|
|
192
|
+
const value = await this.getValue(key, defaultValue);
|
|
193
|
+
this.cache.set(key, value);
|
|
194
|
+
this.ensurePersistStateEvent();
|
|
195
|
+
return value;
|
|
196
|
+
}
|
|
197
|
+
ensurePersistStateEvent() {
|
|
198
|
+
if (this.persistStateEventStarted) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
this.config.getEventManager().on('persistState', async () => {
|
|
202
|
+
for (const [key, value] of this.cache) {
|
|
203
|
+
await this.setValue(key, value);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
this.persistStateEventStarted = true;
|
|
171
207
|
}
|
|
172
208
|
/**
|
|
173
209
|
* Saves or deletes a record in the key-value store.
|
|
@@ -199,7 +235,7 @@ class KeyValueStore {
|
|
|
199
235
|
* {@link KeyValueStore.getValue} function.
|
|
200
236
|
*
|
|
201
237
|
* **IMPORTANT:** Always make sure to use the `await` keyword when calling `setValue()`,
|
|
202
|
-
* otherwise the
|
|
238
|
+
* otherwise the crawler process might finish before the value is stored!
|
|
203
239
|
*
|
|
204
240
|
* @param key
|
|
205
241
|
* Unique key of the record. It can be at most 256 characters long and only consist
|
|
@@ -227,6 +263,22 @@ class KeyValueStore {
|
|
|
227
263
|
}));
|
|
228
264
|
// Make copy of options, don't update what user passed.
|
|
229
265
|
const optionsCopy = { ...options };
|
|
266
|
+
// If we try to set the value of a cached state to a different reference, we need to update the cache accordingly.
|
|
267
|
+
const cachedValue = this.cache.get(key);
|
|
268
|
+
if (this.cache.has(key) && cachedValue !== value) {
|
|
269
|
+
if (value === null) {
|
|
270
|
+
// Cached state can be only object, so a propagation of `null` means removing all its properties.
|
|
271
|
+
Object.keys(cachedValue).forEach((k) => this.cache.delete(k));
|
|
272
|
+
}
|
|
273
|
+
else if (typeof value === 'object') {
|
|
274
|
+
// We need to remove the keys that are no longer present in the new value.
|
|
275
|
+
Object.keys(cachedValue)
|
|
276
|
+
.filter((k) => !(k in value))
|
|
277
|
+
.forEach((k) => this.cache.delete(k));
|
|
278
|
+
// And update the existing ones + add new ones.
|
|
279
|
+
Object.assign(cachedValue, value);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
230
282
|
// In this case delete the record.
|
|
231
283
|
if (value === null)
|
|
232
284
|
return this.client.deleteRecord(key);
|
|
@@ -243,16 +295,12 @@ class KeyValueStore {
|
|
|
243
295
|
*/
|
|
244
296
|
async drop() {
|
|
245
297
|
await this.client.delete();
|
|
246
|
-
const manager =
|
|
298
|
+
const manager = storage_manager_1.StorageManager.getManager(KeyValueStore, this.config);
|
|
247
299
|
manager.closeStorage(this);
|
|
248
300
|
}
|
|
249
|
-
/**
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
*/
|
|
253
|
-
getPublicUrl(key) {
|
|
254
|
-
// FIXME how should this work? should we remove this method or provide a way to configure the base url?
|
|
255
|
-
return `https://api.apify.com/v2/key-value-stores/${this.id}/records/${key}`;
|
|
301
|
+
/** @internal */
|
|
302
|
+
clearCache() {
|
|
303
|
+
this.cache.clear();
|
|
256
304
|
}
|
|
257
305
|
/**
|
|
258
306
|
* Iterates over key-value store keys, yielding each in turn to an `iteratee` function.
|
|
@@ -304,17 +352,123 @@ class KeyValueStore {
|
|
|
304
352
|
*
|
|
305
353
|
* @param [storeIdOrName]
|
|
306
354
|
* ID or name of the key-value store to be opened. If `null` or `undefined`,
|
|
307
|
-
* the function returns the default key-value store associated with the
|
|
355
|
+
* the function returns the default key-value store associated with the crawler run.
|
|
308
356
|
* @param [options] Storage manager options.
|
|
309
357
|
*/
|
|
310
358
|
static async open(storeIdOrName, options = {}) {
|
|
311
|
-
(0, ow_1.default)(storeIdOrName, ow_1.default.optional.string);
|
|
359
|
+
(0, ow_1.default)(storeIdOrName, ow_1.default.optional.any(ow_1.default.string, ow_1.default.null));
|
|
312
360
|
(0, ow_1.default)(options, ow_1.default.object.exactShape({
|
|
313
361
|
config: ow_1.default.optional.object.instanceOf(configuration_1.Configuration),
|
|
314
362
|
}));
|
|
315
|
-
|
|
363
|
+
await (0, utils_1.purgeDefaultStorages)();
|
|
364
|
+
const manager = storage_manager_1.StorageManager.getManager(KeyValueStore, options.config);
|
|
316
365
|
return manager.openStorage(storeIdOrName);
|
|
317
366
|
}
|
|
367
|
+
/**
|
|
368
|
+
* Gets a value from the default {@link KeyValueStore} associated with the current crawler run.
|
|
369
|
+
*
|
|
370
|
+
* This is just a convenient shortcut for {@link KeyValueStore.getValue}.
|
|
371
|
+
* For example, calling the following code:
|
|
372
|
+
* ```javascript
|
|
373
|
+
* const value = await KeyValueStore.getValue('my-key');
|
|
374
|
+
* ```
|
|
375
|
+
*
|
|
376
|
+
* is equivalent to:
|
|
377
|
+
* ```javascript
|
|
378
|
+
* const store = await KeyValueStore.open();
|
|
379
|
+
* const value = await store.getValue('my-key');
|
|
380
|
+
* ```
|
|
381
|
+
*
|
|
382
|
+
* To store the value to the default key-value store, you can use the {@link KeyValueStore.setValue} function.
|
|
383
|
+
*
|
|
384
|
+
* For more information, see {@link KeyValueStore.open}
|
|
385
|
+
* and {@link KeyValueStore.getValue}.
|
|
386
|
+
*
|
|
387
|
+
* @param key Unique record key.
|
|
388
|
+
* @param defaultValue Fallback that will be returned if no value if present in the storage.
|
|
389
|
+
* @returns
|
|
390
|
+
* Returns a promise that resolves to an object, string
|
|
391
|
+
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
392
|
+
* on the MIME content type of the record, or `null`
|
|
393
|
+
* if the record is missing.
|
|
394
|
+
* @ignore
|
|
395
|
+
*/
|
|
396
|
+
static async getValue(key, defaultValue) {
|
|
397
|
+
const store = await this.open();
|
|
398
|
+
return store.getValue(key, defaultValue);
|
|
399
|
+
}
|
|
400
|
+
static async getAutoSavedValue(key, defaultValue = {}) {
|
|
401
|
+
const store = await this.open();
|
|
402
|
+
return store.getAutoSavedValue(key, defaultValue);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Stores or deletes a value in the default {@link KeyValueStore} associated with the current crawler run.
|
|
406
|
+
*
|
|
407
|
+
* This is just a convenient shortcut for {@link KeyValueStore.setValue}.
|
|
408
|
+
* For example, calling the following code:
|
|
409
|
+
* ```javascript
|
|
410
|
+
* await KeyValueStore.setValue('OUTPUT', { foo: "bar" });
|
|
411
|
+
* ```
|
|
412
|
+
*
|
|
413
|
+
* is equivalent to:
|
|
414
|
+
* ```javascript
|
|
415
|
+
* const store = await KeyValueStore.open();
|
|
416
|
+
* await store.setValue('OUTPUT', { foo: "bar" });
|
|
417
|
+
* ```
|
|
418
|
+
*
|
|
419
|
+
* To get a value from the default key-value store, you can use the {@link KeyValueStore.getValue} function.
|
|
420
|
+
*
|
|
421
|
+
* For more information, see {@link KeyValueStore.open}
|
|
422
|
+
* and {@link KeyValueStore.getValue}.
|
|
423
|
+
*
|
|
424
|
+
* @param key
|
|
425
|
+
* Unique record key.
|
|
426
|
+
* @param value
|
|
427
|
+
* Record data, which can be one of the following values:
|
|
428
|
+
* - If `null`, the record in the key-value store is deleted.
|
|
429
|
+
* - If no `options.contentType` is specified, `value` can be any JavaScript object, and it will be stringified to JSON.
|
|
430
|
+
* - If `options.contentType` is set, `value` is taken as is, and it must be a `String` or [`Buffer`](https://nodejs.org/api/buffer.html).
|
|
431
|
+
* For any other value an error will be thrown.
|
|
432
|
+
* @param [options]
|
|
433
|
+
* @ignore
|
|
434
|
+
*/
|
|
435
|
+
static async setValue(key, value, options = {}) {
|
|
436
|
+
const store = await this.open();
|
|
437
|
+
return store.setValue(key, value, options);
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Gets the crawler input value from the default {@link KeyValueStore} associated with the current crawler run.
|
|
441
|
+
*
|
|
442
|
+
* This is just a convenient shortcut for [`keyValueStore.getValue('INPUT')`](core/class/KeyValueStore#getValue).
|
|
443
|
+
* For example, calling the following code:
|
|
444
|
+
* ```javascript
|
|
445
|
+
* const input = await KeyValueStore.getInput();
|
|
446
|
+
* ```
|
|
447
|
+
*
|
|
448
|
+
* is equivalent to:
|
|
449
|
+
* ```javascript
|
|
450
|
+
* const store = await KeyValueStore.open();
|
|
451
|
+
* await store.getValue('INPUT');
|
|
452
|
+
* ```
|
|
453
|
+
*
|
|
454
|
+
* Note that the `getInput()` function does not cache the value read from the key-value store.
|
|
455
|
+
* If you need to use the input multiple times in your crawler,
|
|
456
|
+
* it is far more efficient to read it once and store it locally.
|
|
457
|
+
*
|
|
458
|
+
* For more information, see {@link KeyValueStore.open}
|
|
459
|
+
* and {@link KeyValueStore.getValue}.
|
|
460
|
+
*
|
|
461
|
+
* @returns
|
|
462
|
+
* Returns a promise that resolves to an object, string
|
|
463
|
+
* or [`Buffer`](https://nodejs.org/api/buffer.html), depending
|
|
464
|
+
* on the MIME content type of the record, or `null`
|
|
465
|
+
* if the record is missing.
|
|
466
|
+
* @ignore
|
|
467
|
+
*/
|
|
468
|
+
static async getInput() {
|
|
469
|
+
const store = await this.open();
|
|
470
|
+
return store.getValue(store.config.get('inputKey'));
|
|
471
|
+
}
|
|
318
472
|
}
|
|
319
473
|
exports.KeyValueStore = KeyValueStore;
|
|
320
474
|
//# sourceMappingURL=key_value_store.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key_value_store.js","sourceRoot":"","sources":["../../src/storages/key_value_store.ts"],"names":[],"mappings":";;;;AAAA,0CAA0D;AAC1D,gDAAyD;AACzD,iDAAuC;AAEvC,oDAAiD;
|
|
1
|
+
{"version":3,"file":"key_value_store.js","sourceRoot":"","sources":["../../src/storages/key_value_store.ts"],"names":[],"mappings":";;;;AAAA,0CAA0D;AAC1D,gDAAyD;AACzD,iDAAuC;AAEvC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,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,MAAM,CAAC,KAAK,CAAC,QAAQ;QACjB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC,QAAQ,CAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;IAC3D,CAAC;CACJ;AA1WD,sCA0WC"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Configuration } from '../configuration';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import type { RequestOptions } from '../request';
|
|
4
|
+
import { Request } from '../request';
|
|
5
|
+
import type { ProxyConfiguration } from '../proxy_configuration';
|
|
5
6
|
/** @internal */
|
|
6
7
|
export declare const STATE_PERSISTENCE_KEY = "REQUEST_LIST_STATE";
|
|
7
8
|
/** @internal */
|
|
@@ -49,7 +50,7 @@ export interface RequestListOptions {
|
|
|
49
50
|
* This is very useful in a scenario when getting the sources is a resource intensive or time consuming
|
|
50
51
|
* task, such as fetching URLs from multiple sitemaps or parsing URLs from large datasets. Using the
|
|
51
52
|
* `sourcesFunction` in combination with `persistStateKey` and `persistRequestsKey` will allow you to
|
|
52
|
-
* fetch and parse those URLs only once, saving valuable time when your
|
|
53
|
+
* fetch and parse those URLs only once, saving valuable time when your crawler migrates or restarts.
|
|
53
54
|
*
|
|
54
55
|
* If both {@link RequestListOptions.sources} and {@link RequestListOptions.sourcesFunction} are provided,
|
|
55
56
|
* the sources returned by the function will be added after the `sources`.
|
|
@@ -67,7 +68,7 @@ export interface RequestListOptions {
|
|
|
67
68
|
*
|
|
68
69
|
* // Sitemaps can change in real-time, so it's important to persist
|
|
69
70
|
* // the URLs we collected. Otherwise we might lose our scraping
|
|
70
|
-
* // state in case of an
|
|
71
|
+
* // state in case of an crawler migration / failure / time-out.
|
|
71
72
|
* const requestList = new RequestList({
|
|
72
73
|
* sourcesFunction,
|
|
73
74
|
* persistStateKey: 'state-key',
|
|
@@ -91,7 +92,7 @@ export interface RequestListOptions {
|
|
|
91
92
|
/**
|
|
92
93
|
* Identifies the key in the default key-value store under which `RequestList` periodically stores its
|
|
93
94
|
* state (i.e. which URLs were crawled and which not).
|
|
94
|
-
* If the
|
|
95
|
+
* If the crawler is restarted, `RequestList` will read the state
|
|
95
96
|
* and continue where it left off.
|
|
96
97
|
*
|
|
97
98
|
* If `persistStateKey` is not set, `RequestList` will always start from the beginning,
|
|
@@ -174,9 +175,9 @@ export interface RequestListOptions {
|
|
|
174
175
|
* which are in progress and which were reclaimed. The state may be automatically persisted to the default
|
|
175
176
|
* {@link KeyValueStore} by setting the `persistStateKey` option so that if the Node.js process is restarted,
|
|
176
177
|
* the crawling can continue where it left off. The automated persisting is launched upon receiving the `persistState`
|
|
177
|
-
* event that is periodically emitted by {@link
|
|
178
|
+
* event that is periodically emitted by {@link EventManager}.
|
|
178
179
|
*
|
|
179
|
-
* The internal state is closely tied to the provided sources (URLs). If the sources change on
|
|
180
|
+
* The internal state is closely tied to the provided sources (URLs). If the sources change on crawler restart, the state will become corrupted and
|
|
180
181
|
* `RequestList` will raise an exception. This typically happens when the sources is a list of URLs downloaded from the web.
|
|
181
182
|
* In such case, use the `persistRequestsKey` option in conjunction with `persistStateKey`,
|
|
182
183
|
* to make the `RequestList` store the initial sources to the default key-value store and load them after restart,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request_list.d.ts","sourceRoot":"","sources":["../../src/storages/request_list.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"request_list.d.ts","sourceRoot":"","sources":["../../src/storages/request_list.ts"],"names":[],"mappings":";AAKA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,K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