-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathservice_locator.ts
More file actions
364 lines (317 loc) · 12.6 KB
/
Copy pathservice_locator.ts
File metadata and controls
364 lines (317 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import { AsyncLocalStorage } from 'node:async_hooks';
import { MemoryStorage } from '@crawlee/memory-storage';
import type { StorageClient } from '@crawlee/types';
import log from '@apify/log';
import { Configuration } from './configuration.js';
import { ServiceConflictError } from './errors.js';
import type { EventManager } from './events/event_manager.js';
import { LocalEventManager } from './events/local_event_manager.js';
import type { CrawleeLogger } from './log.js';
import { ApifyLogAdapter } from './log.js';
import { StorageInstanceManager } from './storages/storage_instance_manager.js';
interface ServiceLocatorInterface {
/**
* Get the configuration.
* Creates a default Configuration instance if none has been set.
*/
getConfiguration(): Configuration;
/**
* Set the configuration.
*
* @param configuration The configuration to set
* @throws {ServiceConflictError} If a different configuration has already been retrieved
*/
setConfiguration(configuration: Configuration): void;
/**
* Get the event manager.
* Creates a default LocalEventManager instance if none has been set.
*/
getEventManager(): EventManager;
/**
* Set the event manager.
*
* @param eventManager The event manager to set
* @throws {ServiceConflictError} If a different event manager has already been retrieved
*/
setEventManager(eventManager: EventManager): void;
/**
* Get the storage client.
* Creates a default MemoryStorage instance if none has been set.
*/
getStorageClient(): StorageClient;
/**
* Set the storage client.
*
* @param storageClient The storage client to set
* @throws {ServiceConflictError} If a different storage client has already been retrieved
*/
setStorageClient(storageClient: StorageClient): void;
/**
* Get the logger.
* Returns the default `@apify/log` logger if none has been set.
*/
getLogger(): CrawleeLogger;
/**
* Set the logger.
*
* @param logger The logger to set
* @throws {ServiceConflictError} If a different logger has already been retrieved
*/
setLogger(logger: CrawleeLogger): void;
/**
* Get a child logger with the given prefix.
* Equivalent to `getLogger().child({ prefix })`.
*/
getChildLog(prefix: string): CrawleeLogger;
/**
* Get the storage instance manager (shared across all storage types).
*/
getStorageInstanceManager(): StorageInstanceManager;
/**
* Resets the service locator to its initial state.
* Used mainly for testing purposes.
* @internal
*/
reset(): void;
}
/**
* Service locator for managing the services used by Crawlee.
*
* All services are initialized to their default value lazily.
*
* There are two primary usage patterns:
*
* **1. Global service locator (for default services):**
* ```typescript
* import { serviceLocator, BasicCrawler } from 'crawlee';
*
* // Optionally configure global services before creating crawlers
* serviceLocator.setStorageClient(myCustomClient);
*
* // Crawler uses global services
* const crawler = new BasicCrawler({ ... });
* ```
*
* **2. Per-crawler services (recommended for isolation):**
* ```typescript
* import { BasicCrawler, Configuration, LocalEventManager } from 'crawlee';
* import { MemoryStorage } from '@crawlee/memory-storage';
*
* const crawler = new BasicCrawler({
* requestHandler: async ({ request }) => { ... },
* configuration: new Configuration({ ... }), // custom config
* storageClient: new MemoryStorage(), // custom storage
* eventManager: LocalEventManager.fromConfig(), // custom events
* });
* // Crawler has its own isolated ServiceLocator instance
* ```
*/
export class ServiceLocator implements ServiceLocatorInterface {
private configuration?: Configuration;
private eventManager?: EventManager;
private storageClient?: StorageClient;
private logger?: CrawleeLogger;
/**
* Unified storage instance manager for Dataset, KeyValueStore, and RequestQueue.
* Shared across all ServiceLocator instances (global singleton), matching crawlee-python.
* Per-crawler isolation is achieved via `clientCacheKey`, not separate manager instances.
*/
private static storageInstanceManager?: StorageInstanceManager;
/**
* Creates a new ServiceLocator instance.
*
* @param configuration Optional configuration instance to use
* @param eventManager Optional event manager instance to use
* @param storageClient Optional storage client instance to use
* @param logger Optional logger instance to use
*/
constructor(
configuration?: Configuration,
eventManager?: EventManager,
storageClient?: StorageClient,
logger?: CrawleeLogger,
) {
this.configuration = configuration;
this.eventManager = eventManager;
this.storageClient = storageClient;
this.logger = logger;
}
getConfiguration(): Configuration {
if (!this.configuration) {
this.getLogger().debug('No configuration set, implicitly creating and using default Configuration.');
this.configuration = new Configuration();
}
return this.configuration;
}
setConfiguration(configuration: Configuration): void {
// Same instance, no need to do anything
if (this.configuration === configuration) {
return;
}
// Already have a different configuration that was retrieved
if (this.configuration) {
throw new ServiceConflictError('Configuration', configuration, this.configuration);
}
this.configuration = configuration;
}
getEventManager(): EventManager {
if (!this.eventManager) {
this.getLogger().debug('No event manager set, implicitly creating and using default LocalEventManager.');
if (!this.configuration) {
this.getLogger().warning(
'Implicit creation of event manager will implicitly set configuration as side effect. ' +
'It is advised to explicitly first set the configuration instead.',
);
}
this.eventManager = LocalEventManager.fromConfig(this.getConfiguration());
}
return this.eventManager;
}
setEventManager(eventManager: EventManager): void {
// Same instance, no need to do anything
if (this.eventManager === eventManager) {
return;
}
// Already have a different event manager that was retrieved
if (this.eventManager) {
throw new ServiceConflictError('EventManager', eventManager, this.eventManager);
}
this.eventManager = eventManager;
}
getStorageClient(): StorageClient {
if (!this.storageClient) {
this.getLogger().debug('No storage client set, implicitly creating and using default MemoryStorage.');
if (!this.configuration) {
this.getLogger().warning(
'Implicit creation of storage client will implicitly set configuration as side effect. ' +
'It is advised to explicitly first set the configuration instead.',
);
}
const config = this.getConfiguration();
this.storageClient = new MemoryStorage({
persistStorage: config.persistStorage,
logger: this.getLogger().child({ prefix: 'MemoryStorage' }),
});
}
return this.storageClient;
}
setStorageClient(storageClient: StorageClient): void {
// Same instance, no need to do anything
if (this.storageClient === storageClient) {
return;
}
// Already have a different storage client that was retrieved
if (this.storageClient) {
throw new ServiceConflictError('StorageClient', storageClient, this.storageClient);
}
this.storageClient = storageClient;
}
getLogger(): CrawleeLogger {
if (!this.logger) {
this.logger = new ApifyLogAdapter(log);
}
return this.logger;
}
setLogger(logger: CrawleeLogger): void {
if (this.logger === logger) {
return;
}
if (this.logger) {
throw new ServiceConflictError('Logger', logger, this.logger);
}
this.logger = logger;
}
getChildLog(prefix: string): CrawleeLogger {
return this.getLogger().child({ prefix });
}
getStorageInstanceManager(): StorageInstanceManager {
if (!ServiceLocator.storageInstanceManager) {
ServiceLocator.storageInstanceManager = new StorageInstanceManager();
}
return ServiceLocator.storageInstanceManager;
}
reset(): void {
this.configuration = undefined;
this.eventManager = undefined;
this.storageClient = undefined;
this.logger = undefined;
ServiceLocator.storageInstanceManager?.clearCache();
ServiceLocator.storageInstanceManager = undefined;
}
}
/**
* Used as the default service provider when crawlers don't specify custom services.
*/
const globalServiceLocator = new ServiceLocator();
const serviceLocatorStorage = new AsyncLocalStorage<ServiceLocatorInterface>();
/**
* Wraps all methods on `target` so that any code they invoke will see the given
* `serviceLocator` via `AsyncLocalStorage`, rather than the global one.
*
* Walks the prototype chain and replaces each method on the *instance* (not the prototype)
* with a wrapper that calls `serviceLocatorStorage.run(serviceLocator, originalMethod)`.
*
* The `AsyncLocalStorage` context propagates through the entire sync/async call tree of each
* wrapped method — including `super` calls, since the prototype methods execute within the
* context established by the instance-level wrapper.
*
* @internal
* @returns Scope control functions: `run` executes a callback within the scoped context,
* `enterScope`/`exitScope` allow entering/leaving the scope imperatively (e.g., for constructor bodies).
*/
export function bindMethodsToServiceLocator(
serviceLocator: ServiceLocator,
target: {},
): { run: <T>(fn: () => T) => T; enterScope: () => void; exitScope: () => void } {
let proto = Object.getPrototypeOf(target);
while (proto !== null && proto !== Object.prototype) {
const propertyKeys = [...Object.getOwnPropertyNames(proto), ...Object.getOwnPropertySymbols(proto)];
for (const propertyKey of propertyKeys) {
const descriptor = Object.getOwnPropertyDescriptor(proto, propertyKey);
// We use property descriptors rather than accessing target[propertyKey] directly,
// because that would trigger getters and cause unwanted side effects.
// Skip getters, setters, and constructors — only wrap regular methods.
if (
propertyKey === 'constructor' ||
!descriptor ||
descriptor.get ||
descriptor.set ||
typeof descriptor.value !== 'function'
)
continue;
const original = descriptor.value;
(target as Record<string | symbol, unknown>)[propertyKey] = (...args: any[]) => {
return serviceLocatorStorage.run(serviceLocator, () => {
return original.apply(target, args);
});
};
}
proto = Object.getPrototypeOf(proto);
}
let previousStore: ServiceLocatorInterface | undefined;
return {
run: <T>(fn: () => T): T => serviceLocatorStorage.run(serviceLocator, fn),
enterScope: () => {
previousStore = serviceLocatorStorage.getStore();
serviceLocatorStorage.enterWith(serviceLocator);
},
exitScope: () => {
serviceLocatorStorage.enterWith(previousStore as any); // casting to any so that `undefined` is accepted - this "unsets" the AsyncLocalStorage
},
};
}
export const serviceLocator = new Proxy({} as ServiceLocatorInterface, {
get(_target, prop) {
const active = serviceLocatorStorage.getStore() ?? globalServiceLocator;
const value = Reflect.get(active, prop, active);
if (typeof value === 'function') {
return value.bind(active);
}
return value;
},
set(_target, prop) {
throw new TypeError(
`Cannot set property '${String(prop)}' on serviceLocator directly. Use the setter methods (e.g. setConfiguration(), setStorageClient()) instead.`,
);
},
});