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
fix: add `waitForAllRequestsToBeAdded` option to `enqueueLinks` helper
Martin Adámek925546b31130
diff --git a/packages/core/src/enqueue_links/enqueue_links.ts b/packages/core/src/enqueue_links/enqueue_links.tsindex 3020f332f..ecf1a14c4 100644--- a/packages/core/src/enqueue_links/enqueue_links.ts+++ b/packages/core/src/enqueue_links/enqueue_links.ts@@ -151,6 +151,12 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {* @default EnqueueStrategy.SameHostname*/strategy?: EnqueueStrategy | 'all' | 'same-domain' | 'same-hostname' | 'same-origin';++ /**+ * By default, only the first batch (1000) of found requests will be added to the queue before resolving the call.+ * You can use this option to wait for adding all of them.+ */+ waitForAllRequestsToBeAdded?: boolean;}/**@@ -264,11 +270,22 @@ export async function enqueueLinks(regexps: ow.optional.array.ofType(ow.any(ow.regExp, ow.object.hasKeys('regexp'))),transformRequestFunction: ow.optional.function,strategy: ow.optional.string.oneOf(Object.values(EnqueueStrategy)),+ waitForAllRequestsToBeAdded: ow.optional.boolean,}),);- const { requestQueue, limit, urls, pseudoUrls, exclude, globs, regexps, transformRequestFunction, forefront } =- options;+ const {+ requestQueue,+ limit,+ urls,+ pseudoUrls,+ exclude,+ globs,+ regexps,+ transformRequestFunction,+ forefront,+ waitForAllRequestsToBeAdded,+ } = options;const urlExcludePatternObjects: UrlPatternObject[] = [];const urlPatternObjects: UrlPatternObject[] = [];@@ -371,7 +388,10 @@ export async function enqueueLinks(let requests = createFilteredRequests();if (limit) requests = requests.slice(0, limit);- const { addedRequests } = await requestQueue.addRequestsBatched(requests, { forefront });+ const { addedRequests } = await requestQueue.addRequestsBatched(requests, {+ forefront,+ waitForAllRequestsToBeAdded,+ });return { processedRequests: addedRequests, unprocessedRequests: [] };}diff --git a/test/core/enqueue_links/enqueue_links.test.ts b/test/core/enqueue_links/enqueue_links.test.tsindex 420889a6d..cd072b2f1 100644--- a/test/core/enqueue_links/enqueue_links.test.ts+++ b/test/core/enqueue_links/enqueue_links.test.ts@@ -1,5 +1,5 @@import log from '@apify/log';-import { cheerioCrawlerEnqueueLinks } from '@crawlee/cheerio';+import { type AddRequestsBatchedOptions, cheerioCrawlerEnqueueLinks } from '@crawlee/cheerio';import { launchPlaywright } from '@crawlee/playwright';import type { RequestQueueOperationOptions, Source } from '@crawlee/puppeteer';import {@@ -986,5 +986,32 @@ describe('enqueueLinks()', () => {expect(enqueued[i].options.forefront).toBe(true);}});++ test('accepts waitForAllRequestsToBeAdded option', async () => {+ const enqueued: { request: string | Source; options: AddRequestsBatchedOptions }[] = [];+ const requestQueue = new RequestQueue({ id: 'xxx', client: apifyClient });++ requestQueue.addRequestsBatched = async (requests, options) => {+ // copy the requests to the enqueued list, along with options that were passed to addRequests,+ // so that it doesn't matter how many calls were made+ enqueued.push(...requests.map((request) => ({ request, options })));+ return { addedRequests: [], waitForAllRequestsToBeAdded: Promise.resolve([]) };+ };++ await cheerioCrawlerEnqueueLinks({+ options: {+ waitForAllRequestsToBeAdded: true,+ },+ $,+ requestQueue,+ originalRequestUrl: 'https://example.com',+ });++ expect(enqueued).toHaveLength(5);++ for (let i = 0; i < 5; i++) {+ expect(enqueued[i].options.waitForAllRequestsToBeAdded).toBe(true);+ }+ });});});