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
Implement local support
Ondra Urbanc67354c3af68
diff --git a/src/basic_crawler.js b/src/basic_crawler.jsindex 8c4c2d67e..049d4eba5 100644--- a/src/basic_crawler.js+++ b/src/basic_crawler.js@@ -361,8 +361,7 @@ class BasicCrawler {*/async _loadHandledRequestCount() {if (this.requestQueue) {- const queueInfo = await apifyClient.requestQueues.getQueue({ queueId: this.requestQueue.queueId });- this.handledRequestsCount = queueInfo.handledRequestCount;+ this.handledRequestsCount = await this.requestQueue.handledCount();} else if (this.requestList) {this.handledRequestsCount = this.requestList.handledCount();}diff --git a/src/request_queue.js b/src/request_queue.jsindex f5f13ef1e..18f820c1d 100644--- a/src/request_queue.js+++ b/src/request_queue.js@@ -550,6 +550,16 @@ export class RequestQueue {if (this.queueName) queuesCache.remove(this.queueName);});}++ /**+ * Returns the number of handled requests.+ *+ * @return {Promise<number>}+ */+ async handledCount() {+ const queueInfo = await requestQueues.getQueue({ queueId: this.queueId });+ return queueInfo.handledRequestCount;+ }}/**@@ -582,6 +592,7 @@ export class RequestQueueLocal {this.queueOrderNoCounter = 0; // Counter used in _getQueueOrderNo to ensure there won't be a collision.this.pendingCount = 0;+ this._handledCount = 0;this.inProgressCount = 0;this.requestIdToQueueOrderNo = {};this.queueOrderNoInProgress = {};@@ -600,6 +611,7 @@ export class RequestQueueLocal {]);this.pendingCount = pending.length;+ this._handledCount = handled.length;const handledPaths = handled.map(filename => path.join(this.localHandledEmulationPath, filename));const pendingPaths = pending.map(filename => path.join(this.localPendingEmulationPath, filename));@@ -762,6 +774,7 @@ export class RequestQueueLocal {.then(() => renamePromised(source, dest)).then(() => {this.pendingCount--;+ this._handledCount++;this.inProgressCount--;delete this.queueOrderNoInProgress[queueOrderNo];@@ -825,6 +838,11 @@ export class RequestQueueLocal {queuesCache.remove(this.queueId);});}++ async handledCount() {+ await this.initializationPromise;+ return this._handledCount;+ }}/**diff --git a/test/basic_crawler.js b/test/basic_crawler.jsindex 45967e953..e4ca74b10 100644--- a/test/basic_crawler.js+++ b/test/basic_crawler.js@@ -287,13 +287,11 @@ describe('BasicCrawler', () => {handleRequestFunction,});- sinon.stub(Apify.client.requestQueues, 'getQueue')- .returns(Promise.resolve({- handledRequestCount: 0,- }));-// It enqueues all requests from RequestList to RequestQueue.const mock = sinon.mock(requestQueue);+ mock.expects('handledCount')+ .once()+ .returns(Promise.resolve(0));mock.expects('addRequest').once().withArgs(new Apify.Request(sources[0]), { forefront: true })@@ -383,7 +381,6 @@ describe('BasicCrawler', () => {expect(await requestList.isEmpty()).to.be.eql(true);mock.verify();- sinon.restore();});it('should say that task is not ready requestList is not set and requestQueue is empty', async () => {@@ -404,11 +401,6 @@ describe('BasicCrawler', () => {const queue = [];let isFinished = false;- sinon.stub(Apify.client.requestQueues, 'getQueue')- .returns(Promise.resolve({- handledRequestCount: 0,- }));-const basicCrawler = new Apify.BasicCrawler({requestQueue,autoscaledPoolOptions: {@@ -431,6 +423,7 @@ describe('BasicCrawler', () => {const request1 = new Apify.Request({ url: 'http://example.com/1' });const mock = sinon.mock(requestQueue);+ mock.expects('handledCount').once().returns(Promise.resolve());mock.expects('markRequestHandled').once().withArgs(request0).returns(Promise.resolve());mock.expects('markRequestHandled').once().withArgs(request1).returns(Promise.resolve());mock.expects('isFinished').never();@@ -508,10 +501,8 @@ describe('BasicCrawler', () => {requestQueue.fetchNextRequest = async () => (new Apify.Request({ id: 'id', url: 'http://example.com' }));requestQueue.markRequestHandled = async () => {};let stub = sinon- .stub(Apify.client.requestQueues, 'getQueue')- .returns(Promise.resolve({- handledRequestCount: 33,- }));+ .stub(requestQueue, 'handledCount')+ .returns(33);let count = 0;let crawler = new Apify.BasicCrawler({@@ -525,7 +516,7 @@ describe('BasicCrawler', () => {});await crawler.run();- sinon.assert.calledWith(stub, sinon.match({ queueId: 'id' }));+ sinon.assert.called(stub);expect(count).to.be.eql(7);sinon.restore();@@ -559,10 +550,8 @@ describe('BasicCrawler', () => {.returns(20);const queueStub = sinon- .stub(Apify.client.requestQueues, 'getQueue')- .returns(Promise.resolve({- handledRequestCount: 33,- }));+ .stub(requestQueue, 'handledCount')+ .returns(33);const addRequestStub = sinon.stub(requestQueue, 'addRequest')@@ -581,7 +570,7 @@ describe('BasicCrawler', () => {});await crawler.run();- sinon.assert.calledWith(queueStub, sinon.match({ queueId: 'id' }));+ sinon.assert.called(queueStub);sinon.assert.notCalled(listStub);sinon.assert.callCount(addRequestStub, 7);expect(count).to.be.eql(7);diff --git a/test/request_queue.js b/test/request_queue.jsindex ae6870f68..9a44b15ec 100644--- a/test/request_queue.js+++ b/test/request_queue.js@@ -191,13 +191,41 @@ describe('RequestQueue', () => {expect(await anotherQueue.isFinished()).to.be.eql(true);});- it('should accept Request constructor object in addRequest()', async () => {+ it('should accept plain object in addRequest()', async () => {expectNotUsingLocalStorage();const queue = new RequestQueue('some-id');expect(() => {queue.addRequest({ url: 'http://example.com/a' });}).to.not.throw();});++ it('should return correct handledCount', async () => {+ const queue = new RequestQueueLocal('id', LOCAL_STORAGE_DIR);+ let count = await queue.handledCount();+ expect(count).to.be.eql(0);+ const r1 = new Apify.Request({ url: 'http://example.com/1' });+ const r2 = new Apify.Request({ url: 'http://example.com/2' });+ const r3 = new Apify.Request({ url: 'http://example.com/3' });+ await queue.addRequest(r1);+ await queue.addRequest(r2);+ count = await queue.handledCount();+ expect(count).to.be.eql(0);+ const rf1 = await queue.fetchNextRequest();+ await queue.markRequestHandled(rf1);+ count = await queue.handledCount();+ expect(count).to.be.eql(1);+ await queue.addRequest(r3);+ const rf2 = await queue.fetchNextRequest();+ await queue.markRequestHandled(rf2);+ const rf3 = await queue.fetchNextRequest();+ await queue.markRequestHandled(rf3);+ count = await queue.handledCount();+ expect(count).to.be.eql(3);++ const newQueue = new RequestQueueLocal('id', LOCAL_STORAGE_DIR);+ count = await newQueue.handledCount();+ expect(count).to.be.eql(3);+ });});describe('remote', async () => {@@ -439,7 +467,7 @@ describe('RequestQueue', () => {mock.restore();});- it('shoud handle situation when newly created request is not available yet', async () => {+ it('should handle situation when newly created request is not available yet', async () => {expectNotUsingLocalStorage();const { Request } = Apify;@@ -530,13 +558,26 @@ describe('RequestQueue', () => {mock.restore();});- it('should accept Request constructor object in addRequest()', async () => {+ it('should accept plain object in addRequest()', async () => {expectNotUsingLocalStorage();const queue = new RequestQueue('some-id');expect(() => {queue.addRequest({ url: 'http://example.com/a' });}).to.not.throw();});++ it('should return correct handledCount', async () => {+ const stub = sinon+ .stub(apifyClient.requestQueues, 'getQueue')+ .returns(Promise.resolve({+ handledRequestCount: 33,+ }));+ const queue = new RequestQueue('id');+ const count = await queue.handledCount();+ expect(count).to.be.eql(33);+ sinon.assert.callCount(stub, 1);+ sinon.restore();+ });});describe('Apify.openRequestQueue', async () => {