@crawlee/core 4.0.0-beta.8 → 4.0.0-beta.80
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 +5 -6
- package/autoscaling/autoscaled_pool.js +12 -11
- package/autoscaling/client_load_signal.d.ts +25 -0
- package/autoscaling/client_load_signal.js +36 -0
- package/autoscaling/cpu_load_signal.d.ts +28 -0
- package/autoscaling/cpu_load_signal.js +24 -0
- package/autoscaling/event_loop_load_signal.d.ts +23 -0
- package/autoscaling/event_loop_load_signal.js +35 -0
- package/autoscaling/index.d.ts +5 -1
- package/autoscaling/index.js +5 -1
- package/autoscaling/load_signal.d.ts +99 -0
- package/autoscaling/load_signal.js +135 -0
- package/autoscaling/memory_load_signal.d.ts +43 -0
- package/autoscaling/memory_load_signal.js +102 -0
- package/autoscaling/snapshotter.d.ts +35 -72
- package/autoscaling/snapshotter.js +78 -181
- package/autoscaling/system_status.d.ts +21 -36
- package/autoscaling/system_status.js +46 -84
- package/configuration.d.ts +88 -228
- package/configuration.js +162 -224
- package/cookie_utils.d.ts +4 -3
- package/cookie_utils.js +20 -13
- package/crawlers/context_pipeline.d.ts +70 -0
- package/crawlers/context_pipeline.js +122 -0
- package/crawlers/crawler_commons.d.ts +20 -30
- package/crawlers/crawler_commons.js +12 -21
- package/crawlers/crawler_utils.d.ts +2 -3
- package/crawlers/crawler_utils.js +1 -2
- package/crawlers/error_snapshotter.d.ts +3 -3
- package/crawlers/error_snapshotter.js +2 -3
- package/crawlers/error_tracker.d.ts +2 -2
- package/crawlers/error_tracker.js +0 -1
- package/crawlers/index.d.ts +1 -2
- package/crawlers/index.js +1 -2
- package/crawlers/internals/types.d.ts +7 -0
- package/crawlers/internals/types.js +1 -0
- package/crawlers/statistics.d.ts +22 -17
- package/crawlers/statistics.js +30 -26
- package/enqueue_links/enqueue_links.d.ts +44 -22
- package/enqueue_links/enqueue_links.js +51 -30
- package/enqueue_links/index.d.ts +0 -1
- package/enqueue_links/index.js +0 -1
- package/enqueue_links/shared.d.ts +25 -9
- package/enqueue_links/shared.js +69 -38
- package/errors.d.ts +53 -4
- package/errors.js +70 -5
- package/events/event_manager.d.ts +34 -8
- package/events/event_manager.js +8 -10
- package/events/index.d.ts +0 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -5
- package/events/local_event_manager.js +37 -40
- package/index.d.ts +4 -3
- package/index.js +3 -2
- package/log.d.ts +82 -3
- package/log.js +102 -1
- package/memory-storage/consts.d.ts +4 -0
- package/memory-storage/consts.js +4 -0
- package/memory-storage/index.d.ts +1 -0
- package/memory-storage/index.js +1 -0
- package/memory-storage/memory-storage.d.ts +46 -0
- package/memory-storage/memory-storage.js +136 -0
- package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
- package/memory-storage/resource-clients/common/base-client.js +6 -0
- package/memory-storage/resource-clients/dataset.d.ts +40 -0
- package/memory-storage/resource-clients/dataset.js +113 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +203 -0
- package/memory-storage/resource-clients/request-queue.d.ts +96 -0
- package/memory-storage/resource-clients/request-queue.js +421 -0
- package/memory-storage/utils.d.ts +17 -0
- package/memory-storage/utils.js +42 -0
- package/package.json +12 -10
- package/proxy_configuration.d.ts +14 -149
- package/proxy_configuration.js +19 -168
- package/recoverable_state.d.ts +120 -0
- package/recoverable_state.js +141 -0
- package/request.d.ts +84 -13
- package/request.js +107 -28
- package/router.d.ts +117 -15
- package/router.js +124 -30
- package/serialization.d.ts +0 -1
- package/serialization.js +1 -2
- package/service_locator.d.ts +156 -0
- package/service_locator.js +238 -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 -73
- package/session_pool/session.js +46 -99
- package/session_pool/session_pool.d.ts +71 -72
- package/session_pool/session_pool.js +111 -100
- package/storages/access_checking.d.ts +0 -1
- package/storages/access_checking.js +5 -2
- package/storages/dataset.d.ts +101 -51
- package/storages/dataset.js +168 -126
- package/storages/index.d.ts +8 -7
- package/storages/index.js +6 -7
- package/storages/key_value_store.d.ts +165 -36
- package/storages/key_value_store.js +269 -122
- package/storages/key_value_store_codec.d.ts +32 -0
- package/storages/key_value_store_codec.js +113 -0
- package/storages/request_dedup_cache.d.ts +23 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +38 -82
- package/storages/request_list.js +65 -41
- package/storages/request_loader.d.ts +96 -0
- package/storages/request_loader.js +1 -0
- package/storages/request_manager.d.ts +33 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +106 -0
- package/storages/request_manager_tandem.js +197 -0
- package/storages/request_queue.d.ts +303 -46
- package/storages/request_queue.js +629 -214
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +28 -24
- package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +49 -46
- package/storages/storage_instance_manager.d.ts +91 -0
- package/storages/storage_instance_manager.js +258 -0
- package/storages/storage_stats.d.ts +48 -0
- package/storages/storage_stats.js +29 -0
- package/storages/utils.d.ts +51 -6
- package/storages/utils.js +60 -9
- 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 +1 -2
- package/typedefs.js +0 -1
- package/validators.d.ts +8 -1
- package/validators.js +8 -1
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js.map +0 -1
- package/configuration.d.ts.map +0 -1
- package/configuration.js.map +0 -1
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js.map +0 -1
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_extension.d.ts +0 -12
- package/crawlers/crawler_extension.d.ts.map +0 -1
- package/crawlers/crawler_extension.js +0 -14
- package/crawlers/crawler_extension.js.map +0 -1
- package/crawlers/crawler_utils.d.ts.map +0 -1
- package/crawlers/crawler_utils.js.map +0 -1
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js.map +0 -1
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js.map +0 -1
- package/errors.d.ts.map +0 -1
- package/errors.js.map +0 -1
- package/events/event_manager.d.ts.map +0 -1
- package/events/event_manager.js.map +0 -1
- package/events/index.d.ts.map +0 -1
- package/events/index.js.map +0 -1
- package/events/local_event_manager.d.ts.map +0 -1
- package/events/local_event_manager.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -134
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -33
- package/http_clients/base-http-client.js.map +0 -1
- package/http_clients/form-data-like.d.ts +0 -67
- package/http_clients/form-data-like.d.ts.map +0 -1
- package/http_clients/form-data-like.js +0 -5
- package/http_clients/form-data-like.js.map +0 -1
- package/http_clients/got-scraping-http-client.d.ts +0 -15
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -69
- package/http_clients/got-scraping-http-client.js.map +0 -1
- package/http_clients/index.d.ts +0 -3
- package/http_clients/index.d.ts.map +0 -1
- package/http_clients/index.js +0 -3
- package/http_clients/index.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/log.d.ts.map +0 -1
- package/log.js.map +0 -1
- package/proxy_configuration.d.ts.map +0 -1
- package/proxy_configuration.js.map +0 -1
- package/request.d.ts.map +0 -1
- package/request.js.map +0 -1
- package/router.d.ts.map +0 -1
- package/router.js.map +0 -1
- package/serialization.d.ts.map +0 -1
- package/serialization.js.map +0 -1
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js.map +0 -1
- package/session_pool/events.d.ts +0 -3
- package/session_pool/events.d.ts.map +0 -1
- package/session_pool/events.js +0 -3
- package/session_pool/events.js.map +0 -1
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js.map +0 -1
- package/storages/access_checking.d.ts.map +0 -1
- package/storages/access_checking.js.map +0 -1
- package/storages/dataset.d.ts.map +0 -1
- package/storages/dataset.js.map +0 -1
- package/storages/index.d.ts.map +0 -1
- package/storages/index.js.map +0 -1
- package/storages/key_value_store.d.ts.map +0 -1
- package/storages/key_value_store.js.map +0 -1
- package/storages/request_list.d.ts.map +0 -1
- package/storages/request_list.js.map +0 -1
- package/storages/request_provider.d.ts +0 -307
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -555
- package/storages/request_provider.js.map +0 -1
- package/storages/request_queue.d.ts.map +0 -1
- package/storages/request_queue.js.map +0 -1
- package/storages/request_queue_v2.d.ts +0 -87
- package/storages/request_queue_v2.d.ts.map +0 -1
- package/storages/request_queue_v2.js +0 -438
- package/storages/request_queue_v2.js.map +0 -1
- package/storages/sitemap_request_list.d.ts.map +0 -1
- package/storages/sitemap_request_list.js.map +0 -1
- package/storages/storage_manager.d.ts +0 -58
- package/storages/storage_manager.d.ts.map +0 -1
- package/storages/storage_manager.js +0 -105
- package/storages/storage_manager.js.map +0 -1
- package/storages/utils.d.ts.map +0 -1
- package/storages/utils.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
- package/typedefs.d.ts.map +0 -1
- package/typedefs.js.map +0 -1
- package/validators.d.ts.map +0 -1
- package/validators.js.map +0 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { ContextPipelineCleanupError, ContextPipelineInitializationError, ContextPipelineInterruptedError, RequestHandlerError, SessionError, } from '../errors.js';
|
|
2
|
+
import { serviceLocator } from '../service_locator.js';
|
|
3
|
+
/**
|
|
4
|
+
* Encapsulates the logic of gradually enhancing the crawling context with additional information and utilities.
|
|
5
|
+
*
|
|
6
|
+
* The enhancement is done by a chain of middlewares that are added to the pipeline after its creation.
|
|
7
|
+
* This class provides a type-safe way to build a pipeline of context transformations where each step
|
|
8
|
+
* can enhance the context with additional properties or utilities.
|
|
9
|
+
*
|
|
10
|
+
* @template TContextBase - The base context type that serves as the starting point
|
|
11
|
+
* @template TCrawlingContext - The final context type after all middleware transformations
|
|
12
|
+
*/
|
|
13
|
+
export class ContextPipeline {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new empty context pipeline.
|
|
16
|
+
*
|
|
17
|
+
* @template TContextBase - The base context type for the pipeline
|
|
18
|
+
* @returns A new ContextPipeline instance with no transformations
|
|
19
|
+
*/
|
|
20
|
+
static create() {
|
|
21
|
+
return new ContextPipelineImpl({ action: async (context) => context });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Implementation of the `ContextPipeline` logic. This hides implementation details such as the `middleware` and `parent`
|
|
26
|
+
* properties from the `ContextPipeline` interface, making type checking more reliable.
|
|
27
|
+
*/
|
|
28
|
+
class ContextPipelineImpl extends ContextPipeline {
|
|
29
|
+
middleware;
|
|
30
|
+
parent;
|
|
31
|
+
constructor(middleware, parent) {
|
|
32
|
+
super();
|
|
33
|
+
this.middleware = middleware;
|
|
34
|
+
this.parent = parent;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @inheritdoc
|
|
38
|
+
*/
|
|
39
|
+
compose(middleware) {
|
|
40
|
+
return new ContextPipelineImpl(middleware, this);
|
|
41
|
+
}
|
|
42
|
+
chain(other) {
|
|
43
|
+
const otherMiddlewares = Array.from(other.middlewareChain()).reverse();
|
|
44
|
+
let result = this;
|
|
45
|
+
for (const middleware of otherMiddlewares) {
|
|
46
|
+
result = result.compose(middleware);
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
*middlewareChain() {
|
|
51
|
+
let step = this;
|
|
52
|
+
while (step !== undefined) {
|
|
53
|
+
yield step.middleware;
|
|
54
|
+
step = step.parent;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @inheritdoc
|
|
59
|
+
*/
|
|
60
|
+
async call(crawlingContext, finalContextConsumer) {
|
|
61
|
+
const middlewares = Array.from(this.middlewareChain()).reverse();
|
|
62
|
+
const cleanupStack = [];
|
|
63
|
+
let consumerException;
|
|
64
|
+
try {
|
|
65
|
+
for (const { action, cleanup } of middlewares) {
|
|
66
|
+
try {
|
|
67
|
+
const contextExtension = await action(crawlingContext);
|
|
68
|
+
const extensionNames = [
|
|
69
|
+
...Object.getOwnPropertyNames(contextExtension),
|
|
70
|
+
...Object.getOwnPropertySymbols(contextExtension),
|
|
71
|
+
];
|
|
72
|
+
for (const key of extensionNames) {
|
|
73
|
+
try {
|
|
74
|
+
if (Object.getOwnPropertyDescriptor(crawlingContext, key)?.configurable !== false) {
|
|
75
|
+
Object.defineProperty(crawlingContext, key, Object.getOwnPropertyDescriptor(contextExtension, key));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
serviceLocator
|
|
80
|
+
.getLogger()
|
|
81
|
+
.debug(`Context pipeline failed to define property ${key.toString()}:`, error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (cleanup) {
|
|
85
|
+
cleanupStack.push(cleanup);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (exception) {
|
|
89
|
+
if (exception instanceof SessionError) {
|
|
90
|
+
throw exception; // Session errors are re-thrown as-is
|
|
91
|
+
}
|
|
92
|
+
if (exception instanceof ContextPipelineInterruptedError) {
|
|
93
|
+
throw exception;
|
|
94
|
+
}
|
|
95
|
+
throw new ContextPipelineInitializationError(exception);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
await finalContextConsumer(crawlingContext);
|
|
100
|
+
}
|
|
101
|
+
catch (exception) {
|
|
102
|
+
if (exception instanceof SessionError) {
|
|
103
|
+
consumerException = exception;
|
|
104
|
+
throw exception; // Session errors are re-thrown as-is
|
|
105
|
+
}
|
|
106
|
+
consumerException = exception;
|
|
107
|
+
throw new RequestHandlerError(exception);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
try {
|
|
112
|
+
for (const cleanup of cleanupStack.reverse()) {
|
|
113
|
+
await cleanup(crawlingContext, consumerException);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (exception) {
|
|
117
|
+
// eslint-disable-next-line no-unsafe-finally
|
|
118
|
+
throw new ContextPipelineCleanupError(exception);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
import type { OptionsInit, Response as GotResponse } from 'got-scraping';
|
|
4
|
-
import type { ReadonlyDeep } from 'type-fest';
|
|
1
|
+
import type { Dictionary, HttpRequestOptions, ISession, ProxyInfo, SendRequestOptions } from '@crawlee/types';
|
|
2
|
+
import type { ReadonlyDeep, SetRequired } from 'type-fest';
|
|
5
3
|
import type { Configuration } from '../configuration.js';
|
|
6
4
|
import type { EnqueueLinksOptions } from '../enqueue_links/enqueue_links.js';
|
|
7
|
-
import type {
|
|
8
|
-
import type { ProxyInfo } from '../proxy_configuration.js';
|
|
5
|
+
import type { CrawleeLogger } from '../log.js';
|
|
9
6
|
import type { Request, Source } from '../request.js';
|
|
10
|
-
import type { Session } from '../session_pool/session.js';
|
|
11
7
|
import type { Dataset } from '../storages/dataset.js';
|
|
12
8
|
import { KeyValueStore, type RecordOptions } from '../storages/key_value_store.js';
|
|
13
|
-
import type { RequestQueueOperationOptions } from '../storages/
|
|
9
|
+
import type { RequestQueueOperationOptions } from '../storages/request_queue.js';
|
|
10
|
+
import type { StorageIdentifier } from '../storages/storage_instance_manager.js';
|
|
14
11
|
/** @internal */
|
|
15
12
|
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
16
13
|
/** @internal */
|
|
@@ -22,9 +19,9 @@ export type LoadedRequest<R extends Request> = WithRequired<R, 'id' | 'loadedUrl
|
|
|
22
19
|
export type LoadedContext<Context extends RestrictedCrawlingContext> = IsAny<Context> extends true ? Context : {
|
|
23
20
|
request: LoadedRequest<Context['request']>;
|
|
24
21
|
} & Omit<Context, 'request'>;
|
|
25
|
-
export interface RestrictedCrawlingContext<UserData extends Dictionary = Dictionary>
|
|
22
|
+
export interface RestrictedCrawlingContext<UserData extends Dictionary = Dictionary> {
|
|
26
23
|
id: string;
|
|
27
|
-
session
|
|
24
|
+
session: ISession;
|
|
28
25
|
/**
|
|
29
26
|
* An object with information about currently used proxy by the crawler
|
|
30
27
|
* and configured by the {@link ProxyConfiguration} class.
|
|
@@ -41,7 +38,7 @@ export interface RestrictedCrawlingContext<UserData extends Dictionary = Diction
|
|
|
41
38
|
*
|
|
42
39
|
* @param [data] Data to be pushed to the default dataset.
|
|
43
40
|
*/
|
|
44
|
-
pushData(data: ReadonlyDeep<Parameters<Dataset['pushData']>[0]>,
|
|
41
|
+
pushData(data: ReadonlyDeep<Parameters<Dataset['pushData']>[0]>, datasetIdentifier?: string | StorageIdentifier): Promise<void>;
|
|
45
42
|
/**
|
|
46
43
|
* This function automatically finds and enqueues links from the current page, adding them to the {@link RequestQueue}
|
|
47
44
|
* currently used by the crawler.
|
|
@@ -66,7 +63,7 @@ export interface RestrictedCrawlingContext<UserData extends Dictionary = Diction
|
|
|
66
63
|
*
|
|
67
64
|
* @param [options] All `enqueueLinks()` parameters are passed via an options object.
|
|
68
65
|
*/
|
|
69
|
-
enqueueLinks: (options
|
|
66
|
+
enqueueLinks: (options: ReadonlyDeep<Omit<SetRequired<EnqueueLinksOptions, 'urls'>, 'requestManager' | 'robotsTxtFile'>>) => Promise<unknown>;
|
|
70
67
|
/**
|
|
71
68
|
* Add requests directly to the request queue.
|
|
72
69
|
*
|
|
@@ -81,14 +78,13 @@ export interface RestrictedCrawlingContext<UserData extends Dictionary = Diction
|
|
|
81
78
|
/**
|
|
82
79
|
* Get a key-value store with given name or id, or the default one for the crawler.
|
|
83
80
|
*/
|
|
84
|
-
getKeyValueStore: (
|
|
81
|
+
getKeyValueStore: (identifier?: string | StorageIdentifier) => Promise<Pick<KeyValueStore, 'id' | 'name' | 'getValue' | 'getAutoSavedValue' | 'setValue' | 'getPublicUrl'>>;
|
|
85
82
|
/**
|
|
86
83
|
* A preconfigured logger for the request handler.
|
|
87
84
|
*/
|
|
88
|
-
log:
|
|
85
|
+
log: CrawleeLogger;
|
|
89
86
|
}
|
|
90
|
-
export interface CrawlingContext<
|
|
91
|
-
crawler: Crawler;
|
|
87
|
+
export interface CrawlingContext<UserData extends Dictionary = Dictionary> extends RestrictedCrawlingContext<UserData> {
|
|
92
88
|
/**
|
|
93
89
|
* This function automatically finds and enqueues links from the current page, adding them to the {@link RequestQueue}
|
|
94
90
|
* currently used by the crawler.
|
|
@@ -114,14 +110,9 @@ export interface CrawlingContext<Crawler = unknown, UserData extends Dictionary
|
|
|
114
110
|
* @param [options] All `enqueueLinks()` parameters are passed via an options object.
|
|
115
111
|
* @returns Promise that resolves to {@link BatchAddRequestsResult} object.
|
|
116
112
|
*/
|
|
117
|
-
enqueueLinks(options
|
|
113
|
+
enqueueLinks(options: ReadonlyDeep<Omit<SetRequired<EnqueueLinksOptions, 'urls'>, 'requestManager' | 'robotsTxtFile'>> & Pick<EnqueueLinksOptions, 'requestManager' | 'robotsTxtFile'>): Promise<unknown>;
|
|
118
114
|
/**
|
|
119
|
-
*
|
|
120
|
-
*/
|
|
121
|
-
getKeyValueStore: (idOrName?: string) => Promise<KeyValueStore>;
|
|
122
|
-
/**
|
|
123
|
-
* Fires HTTP request via [`got-scraping`](https://crawlee.dev/js/docs/guides/got-scraping), allowing to override the request
|
|
124
|
-
* options on the fly.
|
|
115
|
+
* Fires HTTP request via the internal HTTP client, allowing to override the request options on the fly.
|
|
125
116
|
*
|
|
126
117
|
* This is handy when you work with a browser crawler but want to execute some requests outside it (e.g. API requests).
|
|
127
118
|
* Check the [Skipping navigations for certain requests](https://crawlee.dev/js/docs/examples/skip-navigation) example for
|
|
@@ -136,7 +127,11 @@ export interface CrawlingContext<Crawler = unknown, UserData extends Dictionary
|
|
|
136
127
|
* },
|
|
137
128
|
* ```
|
|
138
129
|
*/
|
|
139
|
-
sendRequest
|
|
130
|
+
sendRequest: (requestOverrides?: Partial<HttpRequestOptions>, optionsOverrides?: SendRequestOptions) => Promise<Response>;
|
|
131
|
+
/**
|
|
132
|
+
* Register a function to be called at the very end of the request handling process. This is useful for resources that should be accessible to error handlers, for instance.
|
|
133
|
+
*/
|
|
134
|
+
registerDeferredCleanup(cleanup: () => Promise<unknown>): void;
|
|
140
135
|
}
|
|
141
136
|
/**
|
|
142
137
|
* A partial implementation of {@link RestrictedCrawlingContext} that stores parameters of calls to context methods for later inspection.
|
|
@@ -149,7 +144,6 @@ export declare class RequestHandlerResult {
|
|
|
149
144
|
private _keyValueStoreChanges;
|
|
150
145
|
private pushDataCalls;
|
|
151
146
|
private addRequestsCalls;
|
|
152
|
-
private enqueueLinksCalls;
|
|
153
147
|
constructor(config: Configuration, crawleeStateKey: string);
|
|
154
148
|
/**
|
|
155
149
|
* A record of calls to {@link RestrictedCrawlingContext.pushData}, {@link RestrictedCrawlingContext.addRequests}, {@link RestrictedCrawlingContext.enqueueLinks} made by a request handler.
|
|
@@ -157,7 +151,6 @@ export declare class RequestHandlerResult {
|
|
|
157
151
|
get calls(): ReadonlyDeep<{
|
|
158
152
|
pushData: Parameters<RestrictedCrawlingContext['pushData']>[];
|
|
159
153
|
addRequests: Parameters<RestrictedCrawlingContext['addRequests']>[];
|
|
160
|
-
enqueueLinks: Parameters<RestrictedCrawlingContext['enqueueLinks']>[];
|
|
161
154
|
}>;
|
|
162
155
|
/**
|
|
163
156
|
* A record of changes made to key-value stores by a request handler.
|
|
@@ -171,7 +164,7 @@ export declare class RequestHandlerResult {
|
|
|
171
164
|
*/
|
|
172
165
|
get datasetItems(): ReadonlyDeep<{
|
|
173
166
|
item: Dictionary;
|
|
174
|
-
|
|
167
|
+
datasetIdentifier?: string | StorageIdentifier;
|
|
175
168
|
}[]>;
|
|
176
169
|
/**
|
|
177
170
|
* URLs enqueued to the request queue by a request handler, either via {@link RestrictedCrawlingContext.addRequests} or {@link RestrictedCrawlingContext.enqueueLinks}
|
|
@@ -188,12 +181,9 @@ export declare class RequestHandlerResult {
|
|
|
188
181
|
label?: string;
|
|
189
182
|
}[]>;
|
|
190
183
|
pushData: RestrictedCrawlingContext['pushData'];
|
|
191
|
-
enqueueLinks: RestrictedCrawlingContext['enqueueLinks'];
|
|
192
184
|
addRequests: RestrictedCrawlingContext['addRequests'];
|
|
193
185
|
useState: RestrictedCrawlingContext['useState'];
|
|
194
186
|
getKeyValueStore: RestrictedCrawlingContext['getKeyValueStore'];
|
|
195
|
-
private idOrDefault;
|
|
196
187
|
private getKeyValueStoreChangedValue;
|
|
197
188
|
private setKeyValueStoreChangedValue;
|
|
198
189
|
}
|
|
199
|
-
//# sourceMappingURL=crawler_commons.d.ts.map
|
|
@@ -10,7 +10,6 @@ export class RequestHandlerResult {
|
|
|
10
10
|
_keyValueStoreChanges = {};
|
|
11
11
|
pushDataCalls = [];
|
|
12
12
|
addRequestsCalls = [];
|
|
13
|
-
enqueueLinksCalls = [];
|
|
14
13
|
constructor(config, crawleeStateKey) {
|
|
15
14
|
this.config = config;
|
|
16
15
|
this.crawleeStateKey = crawleeStateKey;
|
|
@@ -22,7 +21,6 @@ export class RequestHandlerResult {
|
|
|
22
21
|
return {
|
|
23
22
|
pushData: this.pushDataCalls,
|
|
24
23
|
addRequests: this.addRequestsCalls,
|
|
25
|
-
enqueueLinks: this.enqueueLinksCalls,
|
|
26
24
|
};
|
|
27
25
|
}
|
|
28
26
|
/**
|
|
@@ -35,16 +33,13 @@ export class RequestHandlerResult {
|
|
|
35
33
|
* Items added to datasets by a request handler.
|
|
36
34
|
*/
|
|
37
35
|
get datasetItems() {
|
|
38
|
-
return this.pushDataCalls.flatMap(([data,
|
|
36
|
+
return this.pushDataCalls.flatMap(([data, datasetIdentifier]) => (Array.isArray(data) ? data : [data]).map((item) => ({ item, datasetIdentifier })));
|
|
39
37
|
}
|
|
40
38
|
/**
|
|
41
39
|
* URLs enqueued to the request queue by a request handler, either via {@link RestrictedCrawlingContext.addRequests} or {@link RestrictedCrawlingContext.enqueueLinks}
|
|
42
40
|
*/
|
|
43
41
|
get enqueuedUrls() {
|
|
44
42
|
const result = [];
|
|
45
|
-
for (const [options] of this.enqueueLinksCalls) {
|
|
46
|
-
result.push(...(options?.urls?.map((url) => ({ url, label: options?.label })) ?? []));
|
|
47
|
-
}
|
|
48
43
|
for (const [requests] of this.addRequestsCalls) {
|
|
49
44
|
for (const request of requests) {
|
|
50
45
|
if (typeof request === 'object' &&
|
|
@@ -78,9 +73,6 @@ export class RequestHandlerResult {
|
|
|
78
73
|
pushData = async (data, datasetIdOrName) => {
|
|
79
74
|
this.pushDataCalls.push([data, datasetIdOrName]);
|
|
80
75
|
};
|
|
81
|
-
enqueueLinks = async (options) => {
|
|
82
|
-
this.enqueueLinksCalls.push([options]);
|
|
83
|
-
};
|
|
84
76
|
addRequests = async (requests, options = {}) => {
|
|
85
77
|
this.addRequestsCalls.push([requests, options]);
|
|
86
78
|
};
|
|
@@ -88,29 +80,28 @@ export class RequestHandlerResult {
|
|
|
88
80
|
const store = await this.getKeyValueStore(undefined);
|
|
89
81
|
return await store.getAutoSavedValue(this.crawleeStateKey, defaultValue);
|
|
90
82
|
};
|
|
91
|
-
getKeyValueStore = async (
|
|
92
|
-
const store = await KeyValueStore.open(
|
|
83
|
+
getKeyValueStore = async (identifier) => {
|
|
84
|
+
const store = await KeyValueStore.open(identifier, { config: this.config });
|
|
85
|
+
const storeId = store.id;
|
|
93
86
|
return {
|
|
94
|
-
id: this.
|
|
95
|
-
name:
|
|
96
|
-
getValue: async (key) => this.getKeyValueStoreChangedValue(
|
|
87
|
+
id: storeId ?? this.config.defaultKeyValueStoreId,
|
|
88
|
+
name: store.name,
|
|
89
|
+
getValue: async (key) => this.getKeyValueStoreChangedValue(storeId, key) ?? (await store.getValue(key)),
|
|
97
90
|
setValue: async (key, value, options) => {
|
|
98
|
-
this.setKeyValueStoreChangedValue(
|
|
91
|
+
this.setKeyValueStoreChangedValue(storeId, key, value, options);
|
|
99
92
|
},
|
|
100
93
|
getAutoSavedValue: store.getAutoSavedValue.bind(store),
|
|
101
94
|
getPublicUrl: store.getPublicUrl.bind(store),
|
|
102
95
|
};
|
|
103
96
|
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const id = this.idOrDefault(idOrName);
|
|
97
|
+
getKeyValueStoreChangedValue = (storeKey, key) => {
|
|
98
|
+
const id = storeKey ?? this.config.defaultKeyValueStoreId;
|
|
107
99
|
this._keyValueStoreChanges[id] ??= {};
|
|
108
100
|
return this.keyValueStoreChanges[id][key]?.changedValue ?? null;
|
|
109
101
|
};
|
|
110
|
-
setKeyValueStoreChangedValue = (
|
|
111
|
-
const id = this.
|
|
102
|
+
setKeyValueStoreChangedValue = (storeKey, key, changedValue, options) => {
|
|
103
|
+
const id = storeKey ?? this.config.defaultKeyValueStoreId;
|
|
112
104
|
this._keyValueStoreChanges[id] ??= {};
|
|
113
105
|
this._keyValueStoreChanges[id][key] = { changedValue, options };
|
|
114
106
|
};
|
|
115
107
|
}
|
|
116
|
-
//# sourceMappingURL=crawler_commons.js.map
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ISession } from '@crawlee/types';
|
|
2
2
|
/**
|
|
3
3
|
* Handles timeout request
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
6
|
export declare function handleRequestTimeout({ session, errorMessage }: {
|
|
7
|
-
session?:
|
|
7
|
+
session?: ISession;
|
|
8
8
|
errorMessage: string;
|
|
9
9
|
}): void;
|
|
10
|
-
//# sourceMappingURL=crawler_utils.d.ts.map
|
|
@@ -5,8 +5,7 @@ import { TimeoutError } from '@apify/timeout';
|
|
|
5
5
|
*/
|
|
6
6
|
export function handleRequestTimeout({ session, errorMessage }) {
|
|
7
7
|
session?.markBad();
|
|
8
|
-
const timeoutMillis =
|
|
8
|
+
const timeoutMillis = /(\d+)\s?ms/.exec(errorMessage)?.[1]; // first capturing group
|
|
9
9
|
const timeoutSecs = Number(timeoutMillis) / 1000;
|
|
10
10
|
throw new TimeoutError(`Navigation timed out after ${timeoutSecs} seconds.`);
|
|
11
11
|
}
|
|
12
|
-
//# sourceMappingURL=crawler_utils.js.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { CrawlingContext } from '../crawlers/crawler_commons.js';
|
|
2
2
|
import type { KeyValueStore } from '../storages/key_value_store.js';
|
|
3
3
|
import type { ErrnoException } from './error_tracker.js';
|
|
4
|
+
import type { SnapshottableProperties } from './internals/types.js';
|
|
4
5
|
interface BrowserCrawlingContext {
|
|
5
6
|
saveSnapshot: (options: {
|
|
6
7
|
key: string;
|
|
@@ -39,7 +40,7 @@ export declare class ErrorSnapshotter {
|
|
|
39
40
|
/**
|
|
40
41
|
* Capture a snapshot of the error context.
|
|
41
42
|
*/
|
|
42
|
-
captureSnapshot(error: ErrnoException, context: CrawlingContext): Promise<ErrorSnapshot>;
|
|
43
|
+
captureSnapshot(error: ErrnoException, context: CrawlingContext & SnapshottableProperties): Promise<ErrorSnapshot>;
|
|
43
44
|
/**
|
|
44
45
|
* Captures a snapshot of the current page using the context.saveSnapshot function.
|
|
45
46
|
* This function is applicable for browser contexts only.
|
|
@@ -49,11 +50,10 @@ export declare class ErrorSnapshotter {
|
|
|
49
50
|
/**
|
|
50
51
|
* Save the HTML snapshot of the page, and return the fileName with the extension.
|
|
51
52
|
*/
|
|
52
|
-
saveHTMLSnapshot(html: string, keyValueStore: KeyValueStore, fileName: string): Promise<string | undefined>;
|
|
53
|
+
saveHTMLSnapshot(html: string, keyValueStore: Pick<KeyValueStore, 'setValue'>, fileName: string): Promise<string | undefined>;
|
|
53
54
|
/**
|
|
54
55
|
* Generate a unique fileName for each error snapshot.
|
|
55
56
|
*/
|
|
56
57
|
generateFilename(error: ErrnoException): string;
|
|
57
58
|
}
|
|
58
59
|
export {};
|
|
59
|
-
//# sourceMappingURL=error_snapshotter.d.ts.map
|
|
@@ -52,9 +52,9 @@ export class ErrorSnapshotter {
|
|
|
52
52
|
}
|
|
53
53
|
return {
|
|
54
54
|
screenshotFileName,
|
|
55
|
-
screenshotFileUrl: screenshotFileName && keyValueStore.getPublicUrl(screenshotFileName),
|
|
55
|
+
screenshotFileUrl: screenshotFileName && (await keyValueStore.getPublicUrl(screenshotFileName)),
|
|
56
56
|
htmlFileName,
|
|
57
|
-
htmlFileUrl: htmlFileName && keyValueStore.getPublicUrl(htmlFileName),
|
|
57
|
+
htmlFileUrl: htmlFileName && (await keyValueStore.getPublicUrl(htmlFileName)),
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
catch {
|
|
@@ -115,4 +115,3 @@ export class ErrorSnapshotter {
|
|
|
115
115
|
return fileName;
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
|
-
//# sourceMappingURL=error_snapshotter.js.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CrawlingContext } from '../crawlers/crawler_commons.js';
|
|
2
2
|
import { ErrorSnapshotter } from './error_snapshotter.js';
|
|
3
|
+
import type { SnapshottableProperties } from './internals/types.js';
|
|
3
4
|
/**
|
|
4
5
|
* Node.js Error interface
|
|
5
6
|
*/
|
|
@@ -48,7 +49,6 @@ export declare class ErrorTracker {
|
|
|
48
49
|
addAsync(error: ErrnoException, context?: CrawlingContext): Promise<void>;
|
|
49
50
|
getUniqueErrorCount(): number;
|
|
50
51
|
getMostPopularErrors(count: number): [number, string[]][];
|
|
51
|
-
captureSnapshot(storage: Record<string, unknown>, error: ErrnoException, context: CrawlingContext): Promise<void>;
|
|
52
|
+
captureSnapshot(storage: Record<string, unknown>, error: ErrnoException, context: CrawlingContext & SnapshottableProperties): Promise<void>;
|
|
52
53
|
reset(): void;
|
|
53
54
|
}
|
|
54
|
-
//# sourceMappingURL=error_tracker.d.ts.map
|
package/crawlers/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
+
export * from './context_pipeline.js';
|
|
1
2
|
export * from './crawler_commons.js';
|
|
2
|
-
export * from './crawler_extension.js';
|
|
3
3
|
export * from './crawler_utils.js';
|
|
4
4
|
export * from './statistics.js';
|
|
5
5
|
export * from './error_tracker.js';
|
|
6
6
|
export * from './error_snapshotter.js';
|
|
7
|
-
//# sourceMappingURL=index.d.ts.map
|
package/crawlers/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
+
export * from './context_pipeline.js';
|
|
1
2
|
export * from './crawler_commons.js';
|
|
2
|
-
export * from './crawler_extension.js';
|
|
3
3
|
export * from './crawler_utils.js';
|
|
4
4
|
export * from './statistics.js';
|
|
5
5
|
export * from './error_tracker.js';
|
|
6
6
|
export * from './error_snapshotter.js';
|
|
7
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/crawlers/statistics.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { Configuration } from '../configuration.js';
|
|
1
|
+
import type { CrawleeLogger } from '../log.js';
|
|
3
2
|
import { KeyValueStore } from '../storages/key_value_store.js';
|
|
4
3
|
import { ErrorTracker } from './error_tracker.js';
|
|
5
4
|
/**
|
|
@@ -17,7 +16,7 @@ export interface PersistenceOptions {
|
|
|
17
16
|
* statistics for requests.
|
|
18
17
|
*
|
|
19
18
|
* All statistic information is saved on key value store
|
|
20
|
-
* under the key `
|
|
19
|
+
* under the key `CRAWLEE_CRAWLER_STATISTICS_*`, persists between
|
|
21
20
|
* migrations and abort/resurrect
|
|
22
21
|
*
|
|
23
22
|
* @category Crawlers
|
|
@@ -35,7 +34,7 @@ export declare class Statistics {
|
|
|
35
34
|
/**
|
|
36
35
|
* Statistic instance id.
|
|
37
36
|
*/
|
|
38
|
-
readonly id:
|
|
37
|
+
readonly id: string;
|
|
39
38
|
/**
|
|
40
39
|
* Current statistic state used for doing calculations on {@link Statistics.calculate} calls
|
|
41
40
|
*/
|
|
@@ -44,10 +43,6 @@ export declare class Statistics {
|
|
|
44
43
|
* Contains the current retries histogram. Index 0 means 0 retries, index 2, 2 retries, and so on
|
|
45
44
|
*/
|
|
46
45
|
readonly requestRetryHistogram: number[];
|
|
47
|
-
/**
|
|
48
|
-
* Contains the associated Configuration instance
|
|
49
|
-
*/
|
|
50
|
-
private readonly config;
|
|
51
46
|
protected keyValueStore?: KeyValueStore;
|
|
52
47
|
protected persistStateKey: string;
|
|
53
48
|
private logIntervalMillis;
|
|
@@ -57,8 +52,9 @@ export declare class Statistics {
|
|
|
57
52
|
private readonly log;
|
|
58
53
|
private instanceStart;
|
|
59
54
|
private logInterval;
|
|
60
|
-
private
|
|
55
|
+
private _events?;
|
|
61
56
|
private persistenceOptions;
|
|
57
|
+
private get events();
|
|
62
58
|
/**
|
|
63
59
|
* @internal
|
|
64
60
|
*/
|
|
@@ -90,6 +86,12 @@ export declare class Statistics {
|
|
|
90
86
|
* @ignore
|
|
91
87
|
*/
|
|
92
88
|
failJob(id: number | string, retryCount: number): void;
|
|
89
|
+
/**
|
|
90
|
+
* Discards a started job without affecting the finished/failed counters, e.g. when a request
|
|
91
|
+
* turns out to be skipped (robots.txt, enqueue strategy) after `startJob` was already called for it.
|
|
92
|
+
* @ignore
|
|
93
|
+
*/
|
|
94
|
+
discardJob(id: number | string): void;
|
|
93
95
|
/**
|
|
94
96
|
* Calculate the current statistics
|
|
95
97
|
*/
|
|
@@ -146,17 +148,12 @@ export interface StatisticsOptions {
|
|
|
146
148
|
* Parent logger instance, the statistics will create a child logger from this.
|
|
147
149
|
* @default crawler.log
|
|
148
150
|
*/
|
|
149
|
-
log?:
|
|
151
|
+
log?: CrawleeLogger;
|
|
150
152
|
/**
|
|
151
153
|
* Key value store instance to persist the statistics.
|
|
152
154
|
* If not provided, the default one will be used when capturing starts
|
|
153
155
|
*/
|
|
154
156
|
keyValueStore?: KeyValueStore;
|
|
155
|
-
/**
|
|
156
|
-
* Configuration instance to use
|
|
157
|
-
* @default Configuration.getGlobalConfig()
|
|
158
|
-
*/
|
|
159
|
-
config?: Configuration;
|
|
160
157
|
/**
|
|
161
158
|
* Control how and when to persist the statistics.
|
|
162
159
|
*/
|
|
@@ -166,13 +163,22 @@ export interface StatisticsOptions {
|
|
|
166
163
|
* @default false
|
|
167
164
|
*/
|
|
168
165
|
saveErrorSnapshots?: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* A unique identifier for this statistics instance. This ID is used for persistence
|
|
168
|
+
* to the key value store, ensuring the same statistics can be loaded after script restarts.
|
|
169
|
+
*
|
|
170
|
+
* If not provided, an auto-incremented ID will be used for backward compatibility.
|
|
171
|
+
* This means statistics may not persist correctly across script restarts
|
|
172
|
+
* if crawler creation order changes.
|
|
173
|
+
*/
|
|
174
|
+
id?: string;
|
|
169
175
|
}
|
|
170
176
|
/**
|
|
171
177
|
* Format of the persisted stats
|
|
172
178
|
*/
|
|
173
179
|
export interface StatisticPersistedState extends Omit<StatisticState, 'statsPersistedAt'> {
|
|
174
180
|
requestRetryHistogram: number[];
|
|
175
|
-
statsId:
|
|
181
|
+
statsId: string;
|
|
176
182
|
requestAvgFailedDurationMillis: number;
|
|
177
183
|
requestAvgFinishedDurationMillis: number;
|
|
178
184
|
requestTotalDurationMillis: number;
|
|
@@ -201,4 +207,3 @@ export interface StatisticState {
|
|
|
201
207
|
retryErrors: Record<string, unknown>;
|
|
202
208
|
requestsWithStatusCode: Record<string, number>;
|
|
203
209
|
}
|
|
204
|
-
//# sourceMappingURL=statistics.d.ts.map
|