@crawlee/core 3.0.0-beta.9 → 3.0.0
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 +75 -133
- package/autoscaling/autoscaled_pool.d.ts +5 -5
- package/autoscaling/autoscaled_pool.d.ts.map +1 -1
- package/autoscaling/autoscaled_pool.js +16 -16
- package/autoscaling/autoscaled_pool.js.map +1 -1
- package/autoscaling/snapshotter.d.ts +6 -6
- package/autoscaling/snapshotter.d.ts.map +1 -1
- package/autoscaling/snapshotter.js +12 -8
- 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 +77 -62
- package/configuration.d.ts.map +1 -1
- package/configuration.js +85 -87
- package/configuration.js.map +1 -1
- package/crawlers/crawler_commons.d.ts +7 -12
- 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 +25 -23
- package/enqueue_links/enqueue_links.d.ts.map +1 -1
- package/enqueue_links/enqueue_links.js +23 -23
- 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 +12 -8
- package/events/event_manager.d.ts.map +1 -1
- package/events/event_manager.js +17 -6
- 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 +4 -0
- package/index.d.ts.map +1 -1
- package/index.js +5 -0
- package/index.js.map +1 -1
- package/index.mjs +6 -0
- package/package.json +22 -27
- 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.d.ts.map +1 -1
- package/serialization.js +1 -2
- package/serialization.js.map +1 -1
- package/session_pool/session.d.ts +10 -15
- package/session_pool/session.d.ts.map +1 -1
- package/session_pool/session.js +18 -18
- 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 -5
- 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 +37 -15
- 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 +121 -23
- 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 +32 -35
- package/storages/request_list.d.ts.map +1 -1
- package/storages/request_list.js +32 -24
- 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 -10
- 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 -18
- 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
|
@@ -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,oDAAiD;AAGjD,uDAAmD;AACnD,mCAA+C;AAE/C;;;;GAIG;AACI,MAAM,cAAc,GAAG,CAAI,KAAQ,EAAE,OAAiC,EAAE,EAAE;IAC7E,+DAA+D;IAC/D,IAAI,OAAO,CAAC,WAAW,KAAK,IAAI,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,EAAE;QACnE,OAAO,CAAC,WAAW,GAAG,iCAAiC,CAAC;QAExD,IAAI;YACA,kFAAkF;YAClF,KAAK,GAAG,IAAA,iCAAqB,EAAC,KAAmB,EAAE,IAAI,EAAE,CAAC,CAAiB,CAAC;SAC/E;QAAC,OAAO,CAAC,EAAE;YACR,MAAM,KAAK,GAAG,CAAU,CAAC;YACzB,qCAAqC;YACrC,IAAI,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,uBAAuB,CAAC,IAAI,CAAC,EAAE;gBACtD,KAAK,CAAC,OAAO,GAAG,qBAAqB,CAAC;aACzC;YACD,MAAM,IAAI,KAAK,CAAC,wDAAwD,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;SAC5F;QAED,IAAI,KAAK,KAAK,SAAS,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,wEAAwE;kBAClF,+DAA+D,CAAC,CAAC;SAC1E;KACJ;IAED,OAAO,KAAK,CAAC;AACj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GAAG,EAAE,YAAY,CAAC,CAAC;IAChD,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,iBAAiB,CAAoC,GAAW,EAAE,eAAe,EAAO;QACjG,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC,iBAAiB,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;IACtD,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAI,GAAW,EAAE,KAAe,EAAE,UAAyB,EAAE;QAC9E,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,IAAI,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`.
|
|
@@ -63,22 +64,20 @@ export interface RequestListOptions {
|
|
|
63
64
|
* // and big websites typically have multiple sitemaps.
|
|
64
65
|
* const sitemaps = await downloadHugeSitemaps();
|
|
65
66
|
* return parseUrlsFromSitemaps(sitemaps);
|
|
66
|
-
* }
|
|
67
|
+
* };
|
|
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
|
-
* const requestList =
|
|
71
|
+
* // state in case of an crawler migration / failure / time-out.
|
|
72
|
+
* const requestList = await RequestList.open(null, [], {
|
|
73
|
+
* // The sourcesFunction is called now and the Requests are persisted.
|
|
74
|
+
* // If something goes wrong and we need to start again, RequestList
|
|
75
|
+
* // will load the persisted Requests from storage and will NOT
|
|
76
|
+
* // call the sourcesFunction again, saving time and resources.
|
|
72
77
|
* sourcesFunction,
|
|
73
78
|
* persistStateKey: 'state-key',
|
|
74
79
|
* persistRequestsKey: 'requests-key',
|
|
75
80
|
* })
|
|
76
|
-
*
|
|
77
|
-
* // The sourcesFunction is called now and the Requests are persisted.
|
|
78
|
-
* // If something goes wrong and we need to start again, RequestList
|
|
79
|
-
* // will load the persisted Requests from storage and will NOT
|
|
80
|
-
* // call the sourcesFunction again, saving time and resources.
|
|
81
|
-
* await requestList.initialize();
|
|
82
81
|
* ```
|
|
83
82
|
*/
|
|
84
83
|
sourcesFunction?: RequestListSourcesFunction;
|
|
@@ -91,7 +90,7 @@ export interface RequestListOptions {
|
|
|
91
90
|
/**
|
|
92
91
|
* Identifies the key in the default key-value store under which `RequestList` periodically stores its
|
|
93
92
|
* state (i.e. which URLs were crawled and which not).
|
|
94
|
-
* If the
|
|
93
|
+
* If the crawler is restarted, `RequestList` will read the state
|
|
95
94
|
* and continue where it left off.
|
|
96
95
|
*
|
|
97
96
|
* If `persistStateKey` is not set, `RequestList` will always start from the beginning,
|
|
@@ -174,9 +173,9 @@ export interface RequestListOptions {
|
|
|
174
173
|
* which are in progress and which were reclaimed. The state may be automatically persisted to the default
|
|
175
174
|
* {@link KeyValueStore} by setting the `persistStateKey` option so that if the Node.js process is restarted,
|
|
176
175
|
* 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
|
|
176
|
+
* event that is periodically emitted by {@link EventManager}.
|
|
178
177
|
*
|
|
179
|
-
* The internal state is closely tied to the provided sources (URLs). If the sources change on
|
|
178
|
+
* 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
179
|
* `RequestList` will raise an exception. This typically happens when the sources is a list of URLs downloaded from the web.
|
|
181
180
|
* In such case, use the `persistRequestsKey` option in conjunction with `persistStateKey`,
|
|
182
181
|
* to make the `RequestList` store the initial sources to the default key-value store and load them after restart,
|
|
@@ -196,23 +195,19 @@ export interface RequestListOptions {
|
|
|
196
195
|
* **Advanced usage:**
|
|
197
196
|
* ```javascript
|
|
198
197
|
* // Use the constructor to get more control over the initialization.
|
|
199
|
-
* const requestList =
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* { url: 'http://www.example.com/page-2', userData: { foo: 'bar' }},
|
|
204
|
-
*
|
|
205
|
-
* // Bulk load of URLs from file `http://www.example.com/my-url-list.txt`
|
|
206
|
-
* // Note that all URLs must start with http:// or https://
|
|
207
|
-
* { requestsFromUrl: 'http://www.example.com/my-url-list.txt', userData: { isFromUrl: true } },
|
|
208
|
-
* ],
|
|
198
|
+
* const requestList = await RequestList.open(null, [
|
|
199
|
+
* // Separate requests
|
|
200
|
+
* { url: 'http://www.example.com/page-1', method: 'GET', headers: { ... } },
|
|
201
|
+
* { url: 'http://www.example.com/page-2', userData: { foo: 'bar' }},
|
|
209
202
|
*
|
|
203
|
+
* // Bulk load of URLs from file `http://www.example.com/my-url-list.txt`
|
|
204
|
+
* // Note that all URLs must start with http:// or https://
|
|
205
|
+
* { requestsFromUrl: 'http://www.example.com/my-url-list.txt', userData: { isFromUrl: true } },
|
|
206
|
+
* ], {
|
|
210
207
|
* // Persist the state to avoid re-crawling which can lead to data duplications.
|
|
211
208
|
* // Keep in mind that the sources have to be immutable or this will throw an error.
|
|
212
209
|
* persistStateKey: 'my-state',
|
|
213
210
|
* });
|
|
214
|
-
*
|
|
215
|
-
* await requestList.initialize();
|
|
216
211
|
* ```
|
|
217
212
|
* @category Sources
|
|
218
213
|
*/
|
|
@@ -260,14 +255,16 @@ export declare class RequestList {
|
|
|
260
255
|
private proxyConfiguration?;
|
|
261
256
|
private events;
|
|
262
257
|
/**
|
|
258
|
+
* To create new instance of `RequestList` we need to use `RequestList.open()` factory method.
|
|
263
259
|
* @param options All `RequestList` configuration options
|
|
260
|
+
* @internal
|
|
264
261
|
*/
|
|
265
|
-
constructor(
|
|
262
|
+
private constructor();
|
|
266
263
|
/**
|
|
267
264
|
* Loads all remote sources of URLs and potentially starts periodic state persistence.
|
|
268
265
|
* This function must be called before you can start using the instance in a meaningful way.
|
|
269
266
|
*/
|
|
270
|
-
initialize
|
|
267
|
+
private initialize;
|
|
271
268
|
/**
|
|
272
269
|
* Adds previously persisted Requests, as retrieved from the key-value store.
|
|
273
270
|
* This needs to be done in a memory efficient way. We should update the input
|
|
@@ -396,10 +393,10 @@ export declare class RequestList {
|
|
|
396
393
|
* const requestList = await RequestList.open('my-name', sources);
|
|
397
394
|
* ```
|
|
398
395
|
*
|
|
399
|
-
* @param
|
|
400
|
-
* Name of the request list to be opened. Setting a name enables the `RequestList`'s
|
|
401
|
-
* in the key-value store. This is useful in case of a restart or migration. Since `RequestList`
|
|
402
|
-
* stored in memory, a restart or migration wipes it clean. Setting a name will enable the `RequestList`'s
|
|
396
|
+
* @param listNameOrOptions
|
|
397
|
+
* Name of the request list to be opened, or the options object. Setting a name enables the `RequestList`'s
|
|
398
|
+
* state to be persisted in the key-value store. This is useful in case of a restart or migration. Since `RequestList`
|
|
399
|
+
* is only stored in memory, a restart or migration wipes it clean. Setting a name will enable the `RequestList`'s
|
|
403
400
|
* state to survive those situations and continue where it left off.
|
|
404
401
|
*
|
|
405
402
|
* The name will be used as a prefix in key-value store, producing keys such as `NAME-REQUEST_LIST_STATE`
|
|
@@ -407,7 +404,7 @@ export declare class RequestList {
|
|
|
407
404
|
*
|
|
408
405
|
* If `null`, the list will not be persisted and will only be stored in memory. Process restart
|
|
409
406
|
* will then cause the list to be crawled again from the beginning. We suggest always using a name.
|
|
410
|
-
* @param sources
|
|
407
|
+
* @param [sources]
|
|
411
408
|
* An array of sources of URLs for the {@link RequestList}. It can be either an array of strings,
|
|
412
409
|
* plain objects that define at least the `url` property, or an array of {@link Request} instances.
|
|
413
410
|
*
|
|
@@ -426,7 +423,7 @@ export declare class RequestList {
|
|
|
426
423
|
* the {@link RequestListOptions.persistStateKey} and {@link RequestListOptions.persistRequestsKey}
|
|
427
424
|
* options and the `sources` parameter supersedes the {@link RequestListOptions.sources} option.
|
|
428
425
|
*/
|
|
429
|
-
static open(
|
|
426
|
+
static open(listNameOrOptions: string | null | RequestListOptions, sources?: Source[], options?: RequestListOptions): Promise<RequestList>;
|
|
430
427
|
/**
|
|
431
428
|
* @internal wraps public utility for mocking purposes
|
|
432
429
|
*/
|
|
@@ -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":[],"m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oBAAY,MAAM,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,GAAG;IAAE,eAAe,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC;AACjH,aAAK,cAAc,GAAG;IAAE,eAAe,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAClE,oBAAY,0BAA0B,GAAG,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC"}
|
package/storages/request_list.js
CHANGED
|
@@ -8,7 +8,8 @@ const configuration_1 = require("../configuration");
|
|
|
8
8
|
const log_1 = require("../log");
|
|
9
9
|
const request_1 = require("../request");
|
|
10
10
|
const serialization_1 = require("../serialization");
|
|
11
|
-
const key_value_store_1 = require("
|
|
11
|
+
const key_value_store_1 = require("./key_value_store");
|
|
12
|
+
const utils_2 = require("./utils");
|
|
12
13
|
/** @internal */
|
|
13
14
|
exports.STATE_PERSISTENCE_KEY = 'REQUEST_LIST_STATE';
|
|
14
15
|
/** @internal */
|
|
@@ -40,9 +41,9 @@ const CONTENT_TYPE_BINARY = 'application/octet-stream';
|
|
|
40
41
|
* which are in progress and which were reclaimed. The state may be automatically persisted to the default
|
|
41
42
|
* {@link KeyValueStore} by setting the `persistStateKey` option so that if the Node.js process is restarted,
|
|
42
43
|
* the crawling can continue where it left off. The automated persisting is launched upon receiving the `persistState`
|
|
43
|
-
* event that is periodically emitted by {@link
|
|
44
|
+
* event that is periodically emitted by {@link EventManager}.
|
|
44
45
|
*
|
|
45
|
-
* The internal state is closely tied to the provided sources (URLs). If the sources change on
|
|
46
|
+
* The internal state is closely tied to the provided sources (URLs). If the sources change on crawler restart, the state will become corrupted and
|
|
46
47
|
* `RequestList` will raise an exception. This typically happens when the sources is a list of URLs downloaded from the web.
|
|
47
48
|
* In such case, use the `persistRequestsKey` option in conjunction with `persistStateKey`,
|
|
48
49
|
* to make the `RequestList` store the initial sources to the default key-value store and load them after restart,
|
|
@@ -62,29 +63,27 @@ const CONTENT_TYPE_BINARY = 'application/octet-stream';
|
|
|
62
63
|
* **Advanced usage:**
|
|
63
64
|
* ```javascript
|
|
64
65
|
* // Use the constructor to get more control over the initialization.
|
|
65
|
-
* const requestList =
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* { url: 'http://www.example.com/page-2', userData: { foo: 'bar' }},
|
|
70
|
-
*
|
|
71
|
-
* // Bulk load of URLs from file `http://www.example.com/my-url-list.txt`
|
|
72
|
-
* // Note that all URLs must start with http:// or https://
|
|
73
|
-
* { requestsFromUrl: 'http://www.example.com/my-url-list.txt', userData: { isFromUrl: true } },
|
|
74
|
-
* ],
|
|
66
|
+
* const requestList = await RequestList.open(null, [
|
|
67
|
+
* // Separate requests
|
|
68
|
+
* { url: 'http://www.example.com/page-1', method: 'GET', headers: { ... } },
|
|
69
|
+
* { url: 'http://www.example.com/page-2', userData: { foo: 'bar' }},
|
|
75
70
|
*
|
|
71
|
+
* // Bulk load of URLs from file `http://www.example.com/my-url-list.txt`
|
|
72
|
+
* // Note that all URLs must start with http:// or https://
|
|
73
|
+
* { requestsFromUrl: 'http://www.example.com/my-url-list.txt', userData: { isFromUrl: true } },
|
|
74
|
+
* ], {
|
|
76
75
|
* // Persist the state to avoid re-crawling which can lead to data duplications.
|
|
77
76
|
* // Keep in mind that the sources have to be immutable or this will throw an error.
|
|
78
77
|
* persistStateKey: 'my-state',
|
|
79
78
|
* });
|
|
80
|
-
*
|
|
81
|
-
* await requestList.initialize();
|
|
82
79
|
* ```
|
|
83
80
|
* @category Sources
|
|
84
81
|
*/
|
|
85
82
|
class RequestList {
|
|
86
83
|
/**
|
|
84
|
+
* To create new instance of `RequestList` we need to use `RequestList.open()` factory method.
|
|
87
85
|
* @param options All `RequestList` configuration options
|
|
86
|
+
* @internal
|
|
88
87
|
*/
|
|
89
88
|
constructor(options = {}) {
|
|
90
89
|
Object.defineProperty(this, "log", {
|
|
@@ -248,7 +247,7 @@ class RequestList {
|
|
|
248
247
|
// If this option is set then all requests will get a pre-generated unique ID and duplicate URLs will be kept in the list.
|
|
249
248
|
this.keepDuplicateUrls = keepDuplicateUrls;
|
|
250
249
|
// Will be empty after initialization to save memory.
|
|
251
|
-
this.sources = sources
|
|
250
|
+
this.sources = sources ? [...sources] : [];
|
|
252
251
|
this.sourcesFunction = sourcesFunction;
|
|
253
252
|
// The proxy configuration used for `requestsFromUrls` requests.
|
|
254
253
|
this.proxyConfiguration = proxyConfiguration;
|
|
@@ -262,6 +261,7 @@ class RequestList {
|
|
|
262
261
|
throw new Error('RequestList sources are already loading or were loaded.');
|
|
263
262
|
}
|
|
264
263
|
this.isLoading = true;
|
|
264
|
+
await (0, utils_2.purgeDefaultStorages)();
|
|
265
265
|
const [state, persistedRequests] = await this._loadStateAndPersistedRequests();
|
|
266
266
|
// Add persisted requests / new sources in a memory efficient way because with very
|
|
267
267
|
// large lists, we were running out of memory.
|
|
@@ -278,6 +278,7 @@ class RequestList {
|
|
|
278
278
|
if (this.persistStateKey) {
|
|
279
279
|
this.events.on("persistState" /* EventType.PERSIST_STATE */, this.persistState.bind(this));
|
|
280
280
|
}
|
|
281
|
+
return this;
|
|
281
282
|
}
|
|
282
283
|
/**
|
|
283
284
|
* Adds previously persisted Requests, as retrieved from the key-value store.
|
|
@@ -382,7 +383,7 @@ class RequestList {
|
|
|
382
383
|
throw new Error('The state object is invalid: nextIndex must be a non-negative number.');
|
|
383
384
|
}
|
|
384
385
|
if (state.nextIndex > this.requests.length) {
|
|
385
|
-
throw new Error('The state object is not consistent with RequestList too few requests loaded.');
|
|
386
|
+
throw new Error('The state object is not consistent with RequestList, too few requests loaded.');
|
|
386
387
|
}
|
|
387
388
|
if (state.nextIndex < this.requests.length
|
|
388
389
|
&& this.requests[state.nextIndex].uniqueKey !== state.nextUniqueKey) {
|
|
@@ -670,10 +671,10 @@ class RequestList {
|
|
|
670
671
|
* const requestList = await RequestList.open('my-name', sources);
|
|
671
672
|
* ```
|
|
672
673
|
*
|
|
673
|
-
* @param
|
|
674
|
-
* Name of the request list to be opened. Setting a name enables the `RequestList`'s
|
|
675
|
-
* in the key-value store. This is useful in case of a restart or migration. Since `RequestList`
|
|
676
|
-
* stored in memory, a restart or migration wipes it clean. Setting a name will enable the `RequestList`'s
|
|
674
|
+
* @param listNameOrOptions
|
|
675
|
+
* Name of the request list to be opened, or the options object. Setting a name enables the `RequestList`'s
|
|
676
|
+
* state to be persisted in the key-value store. This is useful in case of a restart or migration. Since `RequestList`
|
|
677
|
+
* is only stored in memory, a restart or migration wipes it clean. Setting a name will enable the `RequestList`'s
|
|
677
678
|
* state to survive those situations and continue where it left off.
|
|
678
679
|
*
|
|
679
680
|
* The name will be used as a prefix in key-value store, producing keys such as `NAME-REQUEST_LIST_STATE`
|
|
@@ -681,7 +682,7 @@ class RequestList {
|
|
|
681
682
|
*
|
|
682
683
|
* If `null`, the list will not be persisted and will only be stored in memory. Process restart
|
|
683
684
|
* will then cause the list to be crawled again from the beginning. We suggest always using a name.
|
|
684
|
-
* @param sources
|
|
685
|
+
* @param [sources]
|
|
685
686
|
* An array of sources of URLs for the {@link RequestList}. It can be either an array of strings,
|
|
686
687
|
* plain objects that define at least the `url` property, or an array of {@link Request} instances.
|
|
687
688
|
*
|
|
@@ -700,8 +701,15 @@ class RequestList {
|
|
|
700
701
|
* the {@link RequestListOptions.persistStateKey} and {@link RequestListOptions.persistRequestsKey}
|
|
701
702
|
* options and the `sources` parameter supersedes the {@link RequestListOptions.sources} option.
|
|
702
703
|
*/
|
|
703
|
-
static async open(
|
|
704
|
-
|
|
704
|
+
static async open(listNameOrOptions, sources, options = {}) {
|
|
705
|
+
if (listNameOrOptions != null && typeof listNameOrOptions === 'object') {
|
|
706
|
+
options = { ...listNameOrOptions, ...options };
|
|
707
|
+
const rl = new RequestList(options);
|
|
708
|
+
await rl.initialize();
|
|
709
|
+
return rl;
|
|
710
|
+
}
|
|
711
|
+
const listName = listNameOrOptions;
|
|
712
|
+
(0, ow_1.default)(listName, ow_1.default.optional.any(ow_1.default.string, ow_1.default.null));
|
|
705
713
|
(0, ow_1.default)(sources, ow_1.default.array);
|
|
706
714
|
(0, ow_1.default)(options, ow_1.default.object.is((v) => !Array.isArray(v)));
|
|
707
715
|
const rl = new RequestList({
|