@crawlee/core 4.0.0-beta.13 → 4.0.0-beta.131
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -13
- package/autoscaling/autoscaled_pool.d.ts +67 -172
- package/autoscaling/autoscaled_pool.js +182 -329
- package/autoscaling/concurrency_system.d.ts +268 -0
- package/autoscaling/concurrency_system.js +362 -0
- package/autoscaling/cpu_load_signal.d.ts +43 -0
- package/autoscaling/cpu_load_signal.js +47 -0
- package/autoscaling/event_loop_load_signal.d.ts +51 -0
- package/autoscaling/event_loop_load_signal.js +60 -0
- package/autoscaling/index.d.ts +6 -1
- package/autoscaling/index.js +6 -1
- package/autoscaling/load_signal.d.ts +100 -0
- package/autoscaling/load_signal.js +105 -0
- package/autoscaling/memory_load_signal.d.ts +47 -0
- package/autoscaling/memory_load_signal.js +106 -0
- package/autoscaling/snapshotter.d.ts +58 -163
- package/autoscaling/snapshotter.js +45 -263
- package/autoscaling/storage_backend_load_signal.d.ts +56 -0
- package/autoscaling/storage_backend_load_signal.js +73 -0
- package/autoscaling/system_status.d.ts +67 -89
- package/autoscaling/system_status.js +92 -122
- package/autoscaling/weighted_avg.d.ts +5 -0
- package/autoscaling/weighted_avg.js +14 -0
- package/byte_utils.d.ts +17 -0
- package/byte_utils.js +42 -0
- package/configuration.d.ts +96 -223
- package/configuration.js +170 -222
- package/cookie_utils.d.ts +3 -2
- package/cookie_utils.js +18 -7
- package/crawlers/context_pipeline.d.ts +10 -1
- package/crawlers/context_pipeline.js +31 -8
- package/crawlers/crawler_commons.d.ts +90 -129
- package/crawlers/crawler_commons.js +1 -116
- package/crawlers/error_snapshotter.d.ts +2 -5
- package/crawlers/error_snapshotter.js +7 -8
- package/crawlers/error_tracker.d.ts +0 -1
- package/crawlers/error_tracker.js +0 -1
- package/crawlers/index.d.ts +1 -3
- package/crawlers/index.js +0 -3
- package/crawlers/internals/types.d.ts +0 -1
- package/crawlers/internals/types.js +0 -1
- package/crawlers/statistics.d.ts +187 -63
- package/crawlers/statistics.js +354 -164
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/enqueue_links/enqueue_links.d.ts +68 -154
- package/enqueue_links/enqueue_links.js +38 -210
- package/enqueue_links/index.d.ts +0 -1
- package/enqueue_links/index.js +0 -1
- package/enqueue_links/shared.d.ts +52 -26
- package/enqueue_links/shared.js +115 -66
- package/errors.d.ts +72 -4
- package/errors.js +89 -5
- package/events/event_manager.d.ts +35 -9
- package/events/event_manager.js +12 -13
- package/events/index.d.ts +0 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -3
- package/events/local_event_manager.js +39 -13
- package/http.d.ts +9 -0
- package/http.js +28 -0
- package/index.d.ts +7 -4
- package/index.js +6 -3
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +82 -3
- package/log.js +106 -1
- package/memory-storage/consts.d.ts +4 -0
- package/memory-storage/consts.js +4 -0
- package/memory-storage/index.d.ts +1 -0
- package/memory-storage/index.js +1 -0
- package/memory-storage/memory-storage.d.ts +38 -0
- package/memory-storage/memory-storage.js +130 -0
- package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
- package/memory-storage/resource-clients/common/base-client.js +6 -0
- package/memory-storage/resource-clients/dataset.d.ts +40 -0
- package/memory-storage/resource-clients/dataset.js +108 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +201 -0
- package/memory-storage/resource-clients/request-queue.d.ts +77 -0
- package/memory-storage/resource-clients/request-queue.js +409 -0
- package/memory-storage/utils.d.ts +16 -0
- package/memory-storage/utils.js +41 -0
- package/owned_or_injected.d.ts +58 -0
- package/owned_or_injected.js +98 -0
- package/package.json +14 -13
- package/proxy_configuration.d.ts +24 -132
- package/proxy_configuration.js +32 -147
- package/recoverable_state.d.ts +155 -0
- package/recoverable_state.js +231 -0
- package/request.d.ts +80 -16
- package/request.js +147 -68
- package/router.d.ts +193 -21
- package/router.js +188 -43
- package/serialization.d.ts +0 -1
- package/serialization.js +15 -15
- package/service_locator.d.ts +165 -0
- package/service_locator.js +253 -0
- package/session_pool/consts.d.ts +1 -2
- package/session_pool/consts.js +1 -2
- package/session_pool/errors.d.ts +0 -1
- package/session_pool/errors.js +0 -1
- package/session_pool/fingerprint.d.ts +9 -0
- package/session_pool/fingerprint.js +30 -0
- package/session_pool/index.d.ts +0 -2
- package/session_pool/index.js +0 -2
- package/session_pool/session.d.ts +35 -88
- package/session_pool/session.js +101 -159
- package/session_pool/session_pool.d.ts +73 -91
- package/session_pool/session_pool.js +172 -165
- package/storages/batched_adds.d.ts +37 -0
- package/storages/batched_adds.js +73 -0
- package/storages/dataset.d.ts +114 -54
- package/storages/dataset.js +291 -148
- package/storages/index.d.ts +10 -8
- package/storages/index.js +8 -8
- package/storages/key_value_store.d.ts +185 -42
- package/storages/key_value_store.js +445 -169
- package/storages/key_value_store_codec.d.ts +32 -0
- package/storages/key_value_store_codec.js +113 -0
- package/storages/request_dedup_cache.d.ts +22 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +52 -115
- package/storages/request_list.js +183 -152
- package/storages/request_loader.d.ts +101 -0
- package/storages/request_loader.js +1 -0
- package/storages/request_manager.d.ts +33 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +97 -0
- package/storages/request_manager_tandem.js +197 -0
- package/storages/request_queue.d.ts +290 -47
- package/storages/request_queue.js +762 -216
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +45 -89
- package/storages/sitemap_request_loader.js +438 -0
- package/storages/storage_instance_manager.d.ts +87 -0
- package/storages/storage_instance_manager.js +256 -0
- package/storages/storage_stats.d.ts +48 -0
- package/storages/storage_stats.js +29 -0
- package/storages/throttling_request_manager.d.ts +239 -0
- package/storages/throttling_request_manager.js +646 -0
- package/storages/transaction.d.ts +252 -0
- package/storages/transaction.js +251 -0
- package/storages/utils.d.ts +59 -11
- package/storages/utils.js +75 -15
- package/system-info/cpu-info.d.ts +67 -0
- package/system-info/cpu-info.js +216 -0
- package/system-info/memory-info.d.ts +31 -0
- package/system-info/memory-info.js +115 -0
- package/system-info/ps-tree.d.ts +17 -0
- package/system-info/ps-tree.js +144 -0
- package/system-info/runtime.d.ts +14 -0
- package/system-info/runtime.js +80 -0
- package/typedefs.d.ts +0 -6
- package/typedefs.js +0 -1
- package/url.d.ts +9 -0
- package/url.js +11 -0
- package/validators.d.ts +22 -18
- package/validators.js +13 -18
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js.map +0 -1
- package/configuration.d.ts.map +0 -1
- package/configuration.js.map +0 -1
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js.map +0 -1
- package/crawlers/context_pipeline.d.ts.map +0 -1
- package/crawlers/context_pipeline.js.map +0 -1
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_utils.d.ts +0 -10
- package/crawlers/crawler_utils.d.ts.map +0 -1
- package/crawlers/crawler_utils.js +0 -12
- package/crawlers/crawler_utils.js.map +0 -1
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js.map +0 -1
- package/crawlers/internals/types.d.ts.map +0 -1
- package/crawlers/internals/types.js.map +0 -1
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js.map +0 -1
- package/errors.d.ts.map +0 -1
- package/errors.js.map +0 -1
- package/events/event_manager.d.ts.map +0 -1
- package/events/event_manager.js.map +0 -1
- package/events/index.d.ts.map +0 -1
- package/events/index.js.map +0 -1
- package/events/local_event_manager.d.ts.map +0 -1
- package/events/local_event_manager.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -140
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -40
- 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 -20
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -82
- package/http_clients/got-scraping-http-client.js.map +0 -1
- package/http_clients/index.d.ts +0 -3
- package/http_clients/index.d.ts.map +0 -1
- package/http_clients/index.js +0 -3
- package/http_clients/index.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/log.d.ts.map +0 -1
- package/log.js.map +0 -1
- package/proxy_configuration.d.ts.map +0 -1
- package/proxy_configuration.js.map +0 -1
- package/request.d.ts.map +0 -1
- package/request.js.map +0 -1
- package/router.d.ts.map +0 -1
- package/router.js.map +0 -1
- package/serialization.d.ts.map +0 -1
- package/serialization.js.map +0 -1
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js.map +0 -1
- package/session_pool/events.d.ts +0 -3
- package/session_pool/events.d.ts.map +0 -1
- package/session_pool/events.js +0 -3
- package/session_pool/events.js.map +0 -1
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js.map +0 -1
- package/storages/access_checking.d.ts +0 -13
- package/storages/access_checking.d.ts.map +0 -1
- package/storages/access_checking.js +0 -14
- package/storages/access_checking.js.map +0 -1
- package/storages/dataset.d.ts.map +0 -1
- package/storages/dataset.js.map +0 -1
- package/storages/index.d.ts.map +0 -1
- package/storages/index.js.map +0 -1
- package/storages/key_value_store.d.ts.map +0 -1
- package/storages/key_value_store.js.map +0 -1
- package/storages/request_list.d.ts.map +0 -1
- package/storages/request_list.js.map +0 -1
- package/storages/request_provider.d.ts +0 -308
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -555
- package/storages/request_provider.js.map +0 -1
- package/storages/request_queue.d.ts.map +0 -1
- package/storages/request_queue.js.map +0 -1
- package/storages/request_queue_v2.d.ts +0 -87
- package/storages/request_queue_v2.d.ts.map +0 -1
- package/storages/request_queue_v2.js +0 -438
- package/storages/request_queue_v2.js.map +0 -1
- package/storages/sitemap_request_list.d.ts.map +0 -1
- package/storages/sitemap_request_list.js +0 -430
- 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,165 @@
|
|
|
1
|
+
import type { StorageBackend } from '@crawlee/types';
|
|
2
|
+
import { Configuration } from './configuration.js';
|
|
3
|
+
import type { EventManager } from './events/event_manager.js';
|
|
4
|
+
import type { CrawleeLogger } from './log.js';
|
|
5
|
+
import { StorageInstanceManager } from './storages/storage_instance_manager.js';
|
|
6
|
+
interface ServiceLocatorInterface {
|
|
7
|
+
/**
|
|
8
|
+
* Get the configuration.
|
|
9
|
+
* Creates a default Configuration instance if none has been set.
|
|
10
|
+
*/
|
|
11
|
+
getConfiguration(): Configuration;
|
|
12
|
+
/**
|
|
13
|
+
* Set the configuration.
|
|
14
|
+
*
|
|
15
|
+
* @param configuration The configuration to set
|
|
16
|
+
* @throws {ServiceConflictError} If a different configuration has already been retrieved
|
|
17
|
+
*/
|
|
18
|
+
setConfiguration(configuration: Configuration): void;
|
|
19
|
+
/**
|
|
20
|
+
* Get the event manager.
|
|
21
|
+
* Creates a default LocalEventManager instance if none has been set.
|
|
22
|
+
*/
|
|
23
|
+
getEventManager(): EventManager;
|
|
24
|
+
/**
|
|
25
|
+
* Set the event manager.
|
|
26
|
+
*
|
|
27
|
+
* @param eventManager The event manager to set
|
|
28
|
+
* @throws {ServiceConflictError} If a different event manager has already been retrieved
|
|
29
|
+
*/
|
|
30
|
+
setEventManager(eventManager: EventManager): void;
|
|
31
|
+
/**
|
|
32
|
+
* Get the storage backend.
|
|
33
|
+
* Creates a default storage backend if none has been set — `FileSystemStorageBackend` when
|
|
34
|
+
* `persistStorage` is enabled (the default), `MemoryStorageBackend` otherwise.
|
|
35
|
+
*/
|
|
36
|
+
getStorageBackend(): StorageBackend;
|
|
37
|
+
/**
|
|
38
|
+
* Set the storage backend.
|
|
39
|
+
*
|
|
40
|
+
* @param storageBackend The storage backend to set
|
|
41
|
+
* @throws {ServiceConflictError} If a different storage backend has already been retrieved
|
|
42
|
+
*/
|
|
43
|
+
setStorageBackend(storageBackend: StorageBackend): void;
|
|
44
|
+
/**
|
|
45
|
+
* Get the logger.
|
|
46
|
+
* Returns the default `@apify/log` logger if none has been set.
|
|
47
|
+
*/
|
|
48
|
+
getLogger(): CrawleeLogger;
|
|
49
|
+
/**
|
|
50
|
+
* Set the logger.
|
|
51
|
+
*
|
|
52
|
+
* @param logger The logger to set
|
|
53
|
+
* @throws {ServiceConflictError} If a different logger has already been retrieved
|
|
54
|
+
*/
|
|
55
|
+
setLogger(logger: CrawleeLogger): void;
|
|
56
|
+
/**
|
|
57
|
+
* Get a child logger with the given prefix.
|
|
58
|
+
* Equivalent to `getLogger().child({ prefix })`.
|
|
59
|
+
*/
|
|
60
|
+
getChildLog(prefix: string): CrawleeLogger;
|
|
61
|
+
/**
|
|
62
|
+
* Get the storage instance manager (shared across all storage types).
|
|
63
|
+
*/
|
|
64
|
+
getStorageInstanceManager(): StorageInstanceManager;
|
|
65
|
+
/**
|
|
66
|
+
* Returns the currently set services without triggering the implicit creation of defaults.
|
|
67
|
+
* Used to inherit already-materialized services into crawler-scoped service locators.
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
getServicesIfSet(): {
|
|
71
|
+
configuration?: Configuration;
|
|
72
|
+
eventManager?: EventManager;
|
|
73
|
+
storageBackend?: StorageBackend;
|
|
74
|
+
logger?: CrawleeLogger;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Resets the service locator to its initial state.
|
|
78
|
+
* Used mainly for testing purposes.
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
reset(): void;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Service locator for managing the services used by Crawlee.
|
|
85
|
+
*
|
|
86
|
+
* All services are initialized to their default value lazily.
|
|
87
|
+
*
|
|
88
|
+
* There are two primary usage patterns:
|
|
89
|
+
*
|
|
90
|
+
* **1. Global service locator (for default services):**
|
|
91
|
+
* ```typescript
|
|
92
|
+
* import { serviceLocator, BasicCrawler } from 'crawlee';
|
|
93
|
+
*
|
|
94
|
+
* // Optionally configure global services before creating crawlers
|
|
95
|
+
* serviceLocator.setStorageBackend(myCustomClient);
|
|
96
|
+
*
|
|
97
|
+
* // Crawler uses global services
|
|
98
|
+
* const crawler = new BasicCrawler({ ... });
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* **2. Per-crawler services (recommended for isolation):**
|
|
102
|
+
* ```typescript
|
|
103
|
+
* import { BasicCrawler, Configuration, LocalEventManager, MemoryStorageBackend } from 'crawlee';
|
|
104
|
+
*
|
|
105
|
+
* const crawler = new BasicCrawler({
|
|
106
|
+
* requestHandler: async ({ request }) => { ... },
|
|
107
|
+
* configuration: new Configuration({ ... }), // custom configuration
|
|
108
|
+
* storageBackend: new MemoryStorageBackend(), // custom storage
|
|
109
|
+
* eventManager: LocalEventManager.fromConfiguration(), // custom events
|
|
110
|
+
* });
|
|
111
|
+
* // Crawler has its own isolated ServiceLocator instance
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export declare class ServiceLocator implements ServiceLocatorInterface {
|
|
115
|
+
#private;
|
|
116
|
+
/**
|
|
117
|
+
* Creates a new ServiceLocator instance.
|
|
118
|
+
*
|
|
119
|
+
* @param configuration Optional configuration instance to use
|
|
120
|
+
* @param eventManager Optional event manager instance to use
|
|
121
|
+
* @param storageBackend Optional storage backend instance to use
|
|
122
|
+
* @param logger Optional logger instance to use
|
|
123
|
+
*/
|
|
124
|
+
constructor(configuration?: Configuration, eventManager?: EventManager, storageBackend?: StorageBackend, logger?: CrawleeLogger);
|
|
125
|
+
/** @internal */
|
|
126
|
+
getServicesIfSet(): {
|
|
127
|
+
configuration?: Configuration;
|
|
128
|
+
eventManager?: EventManager;
|
|
129
|
+
storageBackend?: StorageBackend;
|
|
130
|
+
logger?: CrawleeLogger;
|
|
131
|
+
};
|
|
132
|
+
getConfiguration(): Configuration;
|
|
133
|
+
setConfiguration(configuration: Configuration): void;
|
|
134
|
+
getEventManager(): EventManager;
|
|
135
|
+
setEventManager(eventManager: EventManager): void;
|
|
136
|
+
getStorageBackend(): StorageBackend;
|
|
137
|
+
setStorageBackend(storageBackend: StorageBackend): void;
|
|
138
|
+
getLogger(): CrawleeLogger;
|
|
139
|
+
setLogger(logger: CrawleeLogger): void;
|
|
140
|
+
getChildLog(prefix: string): CrawleeLogger;
|
|
141
|
+
getStorageInstanceManager(): StorageInstanceManager;
|
|
142
|
+
reset(): void;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Wraps all methods on `target` so that any code they invoke will see the given
|
|
146
|
+
* `serviceLocator` via `AsyncLocalStorage`, rather than the global one.
|
|
147
|
+
*
|
|
148
|
+
* Walks the prototype chain and replaces each method on the *instance* (not the prototype)
|
|
149
|
+
* with a wrapper that calls `serviceLocatorStorage.run(serviceLocator, originalMethod)`.
|
|
150
|
+
*
|
|
151
|
+
* The `AsyncLocalStorage` context propagates through the entire sync/async call tree of each
|
|
152
|
+
* wrapped method — including `super` calls, since the prototype methods execute within the
|
|
153
|
+
* context established by the instance-level wrapper.
|
|
154
|
+
*
|
|
155
|
+
* @internal
|
|
156
|
+
* @returns Scope control functions: `run` executes a callback within the scoped context,
|
|
157
|
+
* `enterScope`/`exitScope` allow entering/leaving the scope imperatively (e.g., for constructor bodies).
|
|
158
|
+
*/
|
|
159
|
+
export declare function bindMethodsToServiceLocator(serviceLocator: ServiceLocator, target: {}): {
|
|
160
|
+
run: <T>(fn: () => T) => T;
|
|
161
|
+
enterScope: () => void;
|
|
162
|
+
exitScope: () => void;
|
|
163
|
+
};
|
|
164
|
+
export declare const serviceLocator: ServiceLocatorInterface;
|
|
165
|
+
export {};
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { FileSystemStorageBackend } from '@crawlee/fs-storage';
|
|
3
|
+
import log from '@apify/log';
|
|
4
|
+
import { Configuration } from './configuration.js';
|
|
5
|
+
import { ServiceConflictError } from './errors.js';
|
|
6
|
+
import { LocalEventManager } from './events/local_event_manager.js';
|
|
7
|
+
import { ApifyLogAdapter } from './log.js';
|
|
8
|
+
import { MemoryStorageBackend } from './memory-storage/index.js';
|
|
9
|
+
import { StorageInstanceManager } from './storages/storage_instance_manager.js';
|
|
10
|
+
/**
|
|
11
|
+
* Service locator for managing the services used by Crawlee.
|
|
12
|
+
*
|
|
13
|
+
* All services are initialized to their default value lazily.
|
|
14
|
+
*
|
|
15
|
+
* There are two primary usage patterns:
|
|
16
|
+
*
|
|
17
|
+
* **1. Global service locator (for default services):**
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { serviceLocator, BasicCrawler } from 'crawlee';
|
|
20
|
+
*
|
|
21
|
+
* // Optionally configure global services before creating crawlers
|
|
22
|
+
* serviceLocator.setStorageBackend(myCustomClient);
|
|
23
|
+
*
|
|
24
|
+
* // Crawler uses global services
|
|
25
|
+
* const crawler = new BasicCrawler({ ... });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* **2. Per-crawler services (recommended for isolation):**
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { BasicCrawler, Configuration, LocalEventManager, MemoryStorageBackend } from 'crawlee';
|
|
31
|
+
*
|
|
32
|
+
* const crawler = new BasicCrawler({
|
|
33
|
+
* requestHandler: async ({ request }) => { ... },
|
|
34
|
+
* configuration: new Configuration({ ... }), // custom configuration
|
|
35
|
+
* storageBackend: new MemoryStorageBackend(), // custom storage
|
|
36
|
+
* eventManager: LocalEventManager.fromConfiguration(), // custom events
|
|
37
|
+
* });
|
|
38
|
+
* // Crawler has its own isolated ServiceLocator instance
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export class ServiceLocator {
|
|
42
|
+
#configuration;
|
|
43
|
+
#eventManager;
|
|
44
|
+
#storageBackend;
|
|
45
|
+
#logger;
|
|
46
|
+
/**
|
|
47
|
+
* Unified storage instance manager for Dataset, KeyValueStore, and RequestQueue.
|
|
48
|
+
* Shared across all ServiceLocator instances (global singleton), matching crawlee-python.
|
|
49
|
+
* Per-crawler isolation is achieved via `clientCacheKey`, not separate manager instances.
|
|
50
|
+
*/
|
|
51
|
+
static #storageInstanceManager;
|
|
52
|
+
/**
|
|
53
|
+
* Creates a new ServiceLocator instance.
|
|
54
|
+
*
|
|
55
|
+
* @param configuration Optional configuration instance to use
|
|
56
|
+
* @param eventManager Optional event manager instance to use
|
|
57
|
+
* @param storageBackend Optional storage backend instance to use
|
|
58
|
+
* @param logger Optional logger instance to use
|
|
59
|
+
*/
|
|
60
|
+
constructor(configuration, eventManager, storageBackend, logger) {
|
|
61
|
+
this.#configuration = configuration;
|
|
62
|
+
this.#eventManager = eventManager;
|
|
63
|
+
this.#storageBackend = storageBackend;
|
|
64
|
+
this.#logger = logger;
|
|
65
|
+
}
|
|
66
|
+
/** @internal */
|
|
67
|
+
getServicesIfSet() {
|
|
68
|
+
return {
|
|
69
|
+
configuration: this.#configuration,
|
|
70
|
+
eventManager: this.#eventManager,
|
|
71
|
+
storageBackend: this.#storageBackend,
|
|
72
|
+
logger: this.#logger,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
getConfiguration() {
|
|
76
|
+
if (!this.#configuration) {
|
|
77
|
+
this.getLogger().debug('No configuration set, implicitly creating and using default Configuration.');
|
|
78
|
+
this.#configuration = new Configuration();
|
|
79
|
+
}
|
|
80
|
+
return this.#configuration;
|
|
81
|
+
}
|
|
82
|
+
setConfiguration(configuration) {
|
|
83
|
+
// Same instance, no need to do anything
|
|
84
|
+
if (this.#configuration === configuration) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// Already have a different configuration that was retrieved
|
|
88
|
+
if (this.#configuration) {
|
|
89
|
+
throw new ServiceConflictError('Configuration', configuration, this.#configuration);
|
|
90
|
+
}
|
|
91
|
+
this.#configuration = configuration;
|
|
92
|
+
}
|
|
93
|
+
getEventManager() {
|
|
94
|
+
if (!this.#eventManager) {
|
|
95
|
+
this.getLogger().debug('No event manager set, implicitly creating and using default LocalEventManager.');
|
|
96
|
+
if (!this.#configuration) {
|
|
97
|
+
this.getLogger().warning('Implicit creation of event manager will implicitly set configuration as side effect. ' +
|
|
98
|
+
'It is advised to explicitly first set the configuration instead.');
|
|
99
|
+
}
|
|
100
|
+
this.#eventManager = LocalEventManager.fromConfiguration(this.getConfiguration());
|
|
101
|
+
}
|
|
102
|
+
return this.#eventManager;
|
|
103
|
+
}
|
|
104
|
+
setEventManager(eventManager) {
|
|
105
|
+
// Same instance, no need to do anything
|
|
106
|
+
if (this.#eventManager === eventManager) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
// Already have a different event manager that was retrieved
|
|
110
|
+
if (this.#eventManager) {
|
|
111
|
+
throw new ServiceConflictError('EventManager', eventManager, this.#eventManager);
|
|
112
|
+
}
|
|
113
|
+
this.#eventManager = eventManager;
|
|
114
|
+
}
|
|
115
|
+
getStorageBackend() {
|
|
116
|
+
if (!this.#storageBackend) {
|
|
117
|
+
this.getLogger().debug('No storage backend set, implicitly creating and using the default storage backend ' +
|
|
118
|
+
'(FileSystemStorageBackend when persistStorage is enabled, MemoryStorageBackend otherwise).');
|
|
119
|
+
if (!this.#configuration) {
|
|
120
|
+
this.getLogger().warning('Implicit creation of storage backend will implicitly set configuration as side effect. ' +
|
|
121
|
+
'It is advised to explicitly first set the configuration instead.');
|
|
122
|
+
}
|
|
123
|
+
const configuration = this.getConfiguration();
|
|
124
|
+
this.#storageBackend = configuration.persistStorage
|
|
125
|
+
? new FileSystemStorageBackend({
|
|
126
|
+
localDataDirectory: configuration.storageDir,
|
|
127
|
+
logger: this.getLogger().child({ prefix: 'FileSystemStorageBackend' }),
|
|
128
|
+
})
|
|
129
|
+
: new MemoryStorageBackend({
|
|
130
|
+
logger: this.getLogger().child({ prefix: 'MemoryStorageBackend' }),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return this.#storageBackend;
|
|
134
|
+
}
|
|
135
|
+
setStorageBackend(storageBackend) {
|
|
136
|
+
// Same instance, no need to do anything
|
|
137
|
+
if (this.#storageBackend === storageBackend) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
// Already have a different storage backend that was retrieved
|
|
141
|
+
if (this.#storageBackend) {
|
|
142
|
+
throw new ServiceConflictError('StorageBackend', storageBackend, this.#storageBackend);
|
|
143
|
+
}
|
|
144
|
+
this.#storageBackend = storageBackend;
|
|
145
|
+
}
|
|
146
|
+
getLogger() {
|
|
147
|
+
if (!this.#logger) {
|
|
148
|
+
this.#logger = new ApifyLogAdapter(log);
|
|
149
|
+
}
|
|
150
|
+
return this.#logger;
|
|
151
|
+
}
|
|
152
|
+
setLogger(logger) {
|
|
153
|
+
if (this.#logger === logger) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (this.#logger) {
|
|
157
|
+
throw new ServiceConflictError('Logger', logger, this.#logger);
|
|
158
|
+
}
|
|
159
|
+
this.#logger = logger;
|
|
160
|
+
}
|
|
161
|
+
getChildLog(prefix) {
|
|
162
|
+
return this.getLogger().child({ prefix });
|
|
163
|
+
}
|
|
164
|
+
getStorageInstanceManager() {
|
|
165
|
+
if (!ServiceLocator.#storageInstanceManager) {
|
|
166
|
+
ServiceLocator.#storageInstanceManager = new StorageInstanceManager();
|
|
167
|
+
}
|
|
168
|
+
return ServiceLocator.#storageInstanceManager;
|
|
169
|
+
}
|
|
170
|
+
reset() {
|
|
171
|
+
this.#configuration = undefined;
|
|
172
|
+
this.#eventManager = undefined;
|
|
173
|
+
this.#storageBackend = undefined;
|
|
174
|
+
this.#logger = undefined;
|
|
175
|
+
ServiceLocator.#storageInstanceManager?.clearCache();
|
|
176
|
+
ServiceLocator.#storageInstanceManager = undefined;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Used as the default service provider when crawlers don't specify custom services.
|
|
181
|
+
*/
|
|
182
|
+
const globalServiceLocator = new ServiceLocator();
|
|
183
|
+
const serviceLocatorStorage = new AsyncLocalStorage();
|
|
184
|
+
/**
|
|
185
|
+
* Wraps all methods on `target` so that any code they invoke will see the given
|
|
186
|
+
* `serviceLocator` via `AsyncLocalStorage`, rather than the global one.
|
|
187
|
+
*
|
|
188
|
+
* Walks the prototype chain and replaces each method on the *instance* (not the prototype)
|
|
189
|
+
* with a wrapper that calls `serviceLocatorStorage.run(serviceLocator, originalMethod)`.
|
|
190
|
+
*
|
|
191
|
+
* The `AsyncLocalStorage` context propagates through the entire sync/async call tree of each
|
|
192
|
+
* wrapped method — including `super` calls, since the prototype methods execute within the
|
|
193
|
+
* context established by the instance-level wrapper.
|
|
194
|
+
*
|
|
195
|
+
* @internal
|
|
196
|
+
* @returns Scope control functions: `run` executes a callback within the scoped context,
|
|
197
|
+
* `enterScope`/`exitScope` allow entering/leaving the scope imperatively (e.g., for constructor bodies).
|
|
198
|
+
*/
|
|
199
|
+
export function bindMethodsToServiceLocator(serviceLocator, target) {
|
|
200
|
+
let proto = Object.getPrototypeOf(target);
|
|
201
|
+
const seenKeys = new Set();
|
|
202
|
+
while (proto !== null && proto !== Object.prototype) {
|
|
203
|
+
const propertyKeys = [...Object.getOwnPropertyNames(proto), ...Object.getOwnPropertySymbols(proto)];
|
|
204
|
+
for (const propertyKey of propertyKeys) {
|
|
205
|
+
// The chain is walked derived-first, so the first occurrence of a key is the one dynamic
|
|
206
|
+
// dispatch would pick — a subclass override must not be clobbered by its base version.
|
|
207
|
+
if (seenKeys.has(propertyKey))
|
|
208
|
+
continue;
|
|
209
|
+
seenKeys.add(propertyKey);
|
|
210
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, propertyKey);
|
|
211
|
+
// We use property descriptors rather than accessing target[propertyKey] directly,
|
|
212
|
+
// because that would trigger getters and cause unwanted side effects.
|
|
213
|
+
// Skip getters, setters, and constructors — only wrap regular methods.
|
|
214
|
+
if (propertyKey === 'constructor' ||
|
|
215
|
+
!descriptor ||
|
|
216
|
+
descriptor.get ||
|
|
217
|
+
descriptor.set ||
|
|
218
|
+
typeof descriptor.value !== 'function')
|
|
219
|
+
continue;
|
|
220
|
+
const original = descriptor.value;
|
|
221
|
+
target[propertyKey] = (...args) => {
|
|
222
|
+
return serviceLocatorStorage.run(serviceLocator, () => {
|
|
223
|
+
return original.apply(target, args);
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
proto = Object.getPrototypeOf(proto);
|
|
228
|
+
}
|
|
229
|
+
let previousStore;
|
|
230
|
+
return {
|
|
231
|
+
run: (fn) => serviceLocatorStorage.run(serviceLocator, fn),
|
|
232
|
+
enterScope: () => {
|
|
233
|
+
previousStore = serviceLocatorStorage.getStore();
|
|
234
|
+
serviceLocatorStorage.enterWith(serviceLocator);
|
|
235
|
+
},
|
|
236
|
+
exitScope: () => {
|
|
237
|
+
serviceLocatorStorage.enterWith(previousStore); // casting to any so that `undefined` is accepted - this "unsets" the AsyncLocalStorage
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
export const serviceLocator = new Proxy({}, {
|
|
242
|
+
get(_target, prop) {
|
|
243
|
+
const active = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
244
|
+
const value = Reflect.get(active, prop, active);
|
|
245
|
+
if (typeof value === 'function') {
|
|
246
|
+
return value.bind(active);
|
|
247
|
+
}
|
|
248
|
+
return value;
|
|
249
|
+
},
|
|
250
|
+
set(_target, prop) {
|
|
251
|
+
throw new TypeError(`Cannot set property '${String(prop)}' on serviceLocator directly. Use the setter methods (e.g. setConfiguration(), setStorageBackend()) instead.`);
|
|
252
|
+
},
|
|
253
|
+
});
|
package/session_pool/consts.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
export declare const BLOCKED_STATUS_CODES: number[];
|
|
2
|
-
export declare const PERSIST_STATE_KEY = "
|
|
2
|
+
export declare const PERSIST_STATE_KEY = "CRAWLEE_SESSION_POOL_STATE";
|
|
3
3
|
export declare const MAX_POOL_SIZE = 1000;
|
|
4
|
-
//# sourceMappingURL=consts.d.ts.map
|
package/session_pool/consts.js
CHANGED
package/session_pool/errors.d.ts
CHANGED
package/session_pool/errors.js
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { SessionFingerprint } from '@crawlee/types';
|
|
2
|
+
/**
|
|
3
|
+
* Build a {@link SessionFingerprint} whose `platform` matches the host OS
|
|
4
|
+
* and whose `browser`/`device` are randomized within the realistic profiles for
|
|
5
|
+
* that platform. Used by {@link SessionPool} as the default fingerprint for
|
|
6
|
+
* freshly created sessions; callers can override by passing their own
|
|
7
|
+
* `fingerprint` in `sessionOptions`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createDefaultSessionFingerprint(): SessionFingerprint;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* (browser, platform, device) combinations that correspond to setups people
|
|
3
|
+
* actually run. Anything not listed here (e.g. `edge` on android, `safari` on
|
|
4
|
+
* windows, `desktop` mobile platforms) is left out so a randomized default
|
|
5
|
+
* never produces a fingerprint that would itself be a giveaway.
|
|
6
|
+
*/
|
|
7
|
+
const PROFILES_BY_PLATFORM = [
|
|
8
|
+
{ browser: 'chrome', platform: 'windows', device: 'desktop' },
|
|
9
|
+
{ browser: 'firefox', platform: 'windows', device: 'desktop' },
|
|
10
|
+
{ browser: 'edge', platform: 'windows', device: 'desktop' },
|
|
11
|
+
{ browser: 'chrome', platform: 'macos', device: 'desktop' },
|
|
12
|
+
{ browser: 'firefox', platform: 'macos', device: 'desktop' },
|
|
13
|
+
{ browser: 'safari', platform: 'macos', device: 'desktop' },
|
|
14
|
+
{ browser: 'edge', platform: 'macos', device: 'desktop' },
|
|
15
|
+
{ browser: 'chrome', platform: 'linux', device: 'desktop' },
|
|
16
|
+
{ browser: 'firefox', platform: 'linux', device: 'desktop' },
|
|
17
|
+
{ browser: 'chrome', platform: 'android', device: 'mobile' },
|
|
18
|
+
{ browser: 'firefox', platform: 'android', device: 'mobile' },
|
|
19
|
+
{ browser: 'safari', platform: 'ios', device: 'mobile' },
|
|
20
|
+
];
|
|
21
|
+
/**
|
|
22
|
+
* Build a {@link SessionFingerprint} whose `platform` matches the host OS
|
|
23
|
+
* and whose `browser`/`device` are randomized within the realistic profiles for
|
|
24
|
+
* that platform. Used by {@link SessionPool} as the default fingerprint for
|
|
25
|
+
* freshly created sessions; callers can override by passing their own
|
|
26
|
+
* `fingerprint` in `sessionOptions`.
|
|
27
|
+
*/
|
|
28
|
+
export function createDefaultSessionFingerprint() {
|
|
29
|
+
return { ...PROFILES_BY_PLATFORM[Math.floor(Math.random() * PROFILES_BY_PLATFORM.length)] };
|
|
30
|
+
}
|
package/session_pool/index.d.ts
CHANGED