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
Feature/at home (#81)
Marek Trunkát44ef507e8a29
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4fecb112d..f4aacaeb5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1,5 +1,6 @@xxxxxxxxxxxxxxxxxxx-===================+- Added `Apify.isAtHome()` function that returns `true` when code is running at Apify platform and `false` otherwise+ (for example locally).- Added `ignoreMainProcess` parameter to `AutoscaledPool`. Check documentation for more info.0.5.26 / 2018-04-27diff --git a/src/index.js b/src/index.jsindex 97f318f06..86c07e197 100644--- a/src/index.js+++ b/src/index.js@@ -13,7 +13,7 @@ import Request from './request';import RequestList from './request_list';import { openRequestQueue } from './request_queue';import SettingsRotator from './settings_rotator';-import { apifyClient, getMemoryInfo, isProduction } from './utils';+import { apifyClient, getMemoryInfo, isProduction, isAtHome } from './utils';import { browse, launchWebDriver } from './webdriver';/* globals module */@@ -44,6 +44,8 @@ module.exports = {readyFreddy,getMemoryInfo,getApifyProxyUrl,+ isAtHome,+ client: apifyClient,// Autoscaled poolAutoscaledPool,@@ -78,9 +80,6 @@ module.exports = {// Settings rotatorSettingsRotator,- // Utils- client: apifyClient,-// Webdriverbrowse,launchWebDriver,diff --git a/src/utils.js b/src/utils.jsindex 82c062e89..30992f7b7 100644--- a/src/utils.js+++ b/src/utils.js@@ -278,3 +278,15 @@ export const getTypicalChromeExecutablePath = () => {default: return 'google-chrome';}};++/**+ * Returns `true` when code is running at Apify platform and `false` otherwise (for example locally).+ *+ * @returns {Boolean}+ *+ * @memberof module:Apify+ * @name isAtHome+ * @instance+ * @function+ */+export const isAtHome = () => !!process.env[ENV_VARS.IS_AT_HOME];diff --git a/test/utils.js b/test/utils.jsindex 0a37ce484..e710c0b92 100644--- a/test/utils.js+++ b/test/utils.js@@ -15,9 +15,9 @@ chai.use(chaiAsPromised);describe('utils.newClient()', () => {it('reads environment variables correctly', () => {- process.env.APIFY_API_BASE_URL = 'http://www.example.com:1234/path/';- process.env.APIFY_USER_ID = 'userId';- process.env.APIFY_TOKEN = 'token';+ process.env[ENV_VARS.API_BASE_URL] = 'http://www.example.com:1234/path/';+ process.env[ENV_VARS.USER_ID] = 'userId';+ process.env[ENV_VARS.TOKEN] = 'token';const client = utils.newClient();expect(client.constructor.name).to.eql('ApifyClient');@@ -29,9 +29,9 @@ describe('utils.newClient()', () => {});it('uses correct default if APIFY_API_BASE_URL is not defined', () => {- delete process.env.APIFY_API_BASE_URL;- process.env.APIFY_USER_ID = 'userId';- process.env.APIFY_TOKEN = 'token';+ delete process.env[ENV_VARS.API_BASE_URL];+ process.env[ENV_VARS.USER_ID] = 'userId';+ process.env[ENV_VARS.TOKEN] = 'token';const client = utils.newClient();const opts = client.getOptions();@@ -267,6 +267,16 @@ describe('utils.newPromise()', () => {});});+describe('utils.isAtHome()', () => {+ it('works', () => {+ expect(utils.isAtHome()).to.be.eql(false);+ process.env[ENV_VARS.IS_AT_HOME] = 1;+ expect(utils.isAtHome()).to.be.eql(true);+ delete process.env[ENV_VARS.IS_AT_HOME];+ expect(utils.isAtHome()).to.be.eql(false);+ });+});+describe('pidusage NPM package', () => {it('throws correct error message when process not found', () => {const NONEXISTING_PID = 9999;@@ -276,7 +286,7 @@ describe('pidusage NPM package', () => {});});-describe('utils.sum', () => {+describe('utils.sum()', () => {it('works', () => {expect(utils.sum([1, 2, 3, 1.2])).to.be.eql(7.2);expect(utils.sum([])).to.be.eql(0);@@ -284,7 +294,7 @@ describe('utils.sum', () => {});});-describe('utils.avg', () => {+describe('utils.avg()', () => {it('works', () => {expect(utils.avg([1, 2, 3, 1.2])).to.be.eql(7.2 / 4);expect(utils.avg([])).to.be.eql(NaN);