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(http-crawler): avoid crashing when gotOptions.cache is on (#2686)
Kaffi Y.1106d3aeccd9
diff --git a/packages/http-crawler/src/internals/http-crawler.ts b/packages/http-crawler/src/internals/http-crawler.tsindex 79bd77a8f..883fb3bec 100644--- a/packages/http-crawler/src/internals/http-crawler.ts+++ b/packages/http-crawler/src/internals/http-crawler.ts@@ -977,9 +977,9 @@ function addResponsePropertiesToStream(stream: GotRequest) {response.on('end', () => {// @ts-expect-error- Object.assign(stream.rawTrailers, response.rawTrailers);+ if (stream.rawTrailers) Object.assign(stream.rawTrailers, response.rawTrailers);// @ts-expect-error- Object.assign(stream.trailers, response.trailers);+ if (stream.trailers) Object.assign(stream.trailers, response.trailers);// @ts-expect-errorstream.complete = response.complete;diff --git a/test/core/crawlers/http_crawler.test.ts b/test/core/crawlers/http_crawler.test.tsindex a639722fa..296235181 100644--- a/test/core/crawlers/http_crawler.test.ts+++ b/test/core/crawlers/http_crawler.test.ts@@ -374,3 +374,29 @@ test('should retry on 403 even with disallowed content-type', async () => {expect(succeeded).toHaveLength(1);expect(succeeded[0].retryCount).toBe(1);});++test('should work with cacheable-request', async () => {+ const isFromCache: Record<string, boolean> = {};+ const cache = new Map();+ const crawler = new HttpCrawler({+ maxConcurrency: 1,+ preNavigationHooks: [+ async (_, gotOptions) => {+ gotOptions.cache = cache;+ gotOptions.headers = {+ ...gotOptions.headers,+ // to force cache+ 'cache-control': 'max-stale',+ };+ },+ ],+ requestHandler: async ({ request, response }) => {+ isFromCache[request.uniqueKey] = response.isFromCache;+ },+ });+ await crawler.run([+ { url, uniqueKey: 'first' },+ { url, uniqueKey: 'second' },+ ]);+ expect(isFromCache).toEqual({ first: false, second: true });+});