Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

refactor: replace service locator forwarding object with Proxy (#3515)

Martin Adámek1508ab91c89a
diff --git a/packages/core/src/service_locator.ts b/packages/core/src/service_locator.ts
index 72d00fc72..cbd1db611 100644
--- a/packages/core/src/service_locator.ts
+++ b/packages/core/src/service_locator.ts
@@ -383,57 +383,18 @@ export function bindMethodsToServiceLocator(
};
}
-export const serviceLocator: ServiceLocatorInterface = {
- getConfiguration(): Configuration {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- return currentServiceLocator.getConfiguration();
- },
- setConfiguration(configuration: Configuration): void {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- currentServiceLocator.setConfiguration(configuration);
- },
- getEventManager(): EventManager {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- return currentServiceLocator.getEventManager();
- },
- setEventManager(eventManager: EventManager): void {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- currentServiceLocator.setEventManager(eventManager);
- },
- getStorageClient(): StorageClient {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- return currentServiceLocator.getStorageClient();
- },
- setStorageClient(storageClient: StorageClient): void {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- currentServiceLocator.setStorageClient(storageClient);
- },
- getLogger(): CrawleeLogger {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- return currentServiceLocator.getLogger();
- },
- setLogger(logger: CrawleeLogger): void {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- currentServiceLocator.setLogger(logger);
- },
- getChildLog(prefix: string): CrawleeLogger {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- return currentServiceLocator.getChildLog(prefix);
- },
- getStorageManager(constructor: Constructor<IStorage>): StorageManager | undefined {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- return currentServiceLocator.getStorageManager(constructor);
- },
- setStorageManager(constructor: Constructor<IStorage>, storageManager: StorageManager): void {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- currentServiceLocator.setStorageManager(constructor, storageManager);
- },
- clearStorageManagerCache(): void {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- currentServiceLocator.clearStorageManagerCache();
+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;
},
- reset(): void {
- const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
- currentServiceLocator.reset();
+ set(_target, prop) {
+ throw new TypeError(
+ `Cannot set property '${String(prop)}' on serviceLocator directly. Use the setter methods (e.g. setConfiguration(), setStorageClient()) instead.`,
+ );
},
-};
+});