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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
feat: instance-scoped "global" configuration (#1315)
Jindřich Bäreab4e7faa123
diff --git a/src/apify.js b/src/apify.jsindex 711acf07c..7f0b46ba0 100644--- a/src/apify.js+++ b/src/apify.js@@ -140,7 +140,7 @@ export class Apify {initializeEvents(this.config);return (async () => {try {- await userFunc();+ await Configuration.storage.run(this.config, userFunc);process.exit(EXIT_CODES.SUCCESS);} catch (err) {if (!exited) {diff --git a/src/configuration.js b/src/configuration.jsindex 6a673ff51..73d8cf4a2 100644--- a/src/configuration.js+++ b/src/configuration.js@@ -2,6 +2,7 @@ import { ENV_VARS, LOCAL_ENV_VARS } from '@apify/consts';import { join } from 'path';import { ApifyStorageLocal } from '@apify/storage-local';import { ApifyClient } from 'apify-client';+import { AsyncLocalStorage } from 'async_hooks';import log from './utils_log';/**@@ -118,6 +119,12 @@ export class Configuration {localStorageEnableWalMode: true,};+ /**+ * Provides access to the current-instance-scoped Configuration without passing it around in parameters.+ * @internal+ */+ static storage = new AsyncLocalStorage();+/*** Creates new `Configuration` instance with provided options. Env vars will have precedence over those.*@@ -297,15 +304,14 @@ export class Configuration {/*** Returns the global configuration instance. It will respect the environment variables.- * As opposed to this method, we can also get the SDK instance configuration via `sdk.config` property.+ * When used inside SDK instance (`sdk.main()`), the return value is equal to `sdk.config`.** @return {Configuration}*/static getGlobalConfig() {- if (!Configuration.globalConfig) {- Configuration.globalConfig = new Configuration();- }-- return Configuration.globalConfig;+ return Configuration.storage.getStore() ?? (() => {+ Configuration.globalConfig ??= new Configuration();+ return Configuration.globalConfig;+ })();}}