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(core): give each request its own copy of userData (#4319)
Nikita Chapovskii270c713d74c4
diff --git a/packages/core/src/request.ts b/packages/core/src/request.tsindex 5a95bb371..6f1586beb 100644--- a/packages/core/src/request.ts+++ b/packages/core/src/request.ts@@ -9,6 +9,7 @@ import { z } from 'zod';import { cryptoRandomObjectId, normalizeUrl } from '@apify/utilities';import { serviceLocator } from './service-locator.js';+import { snapshotValue } from './storages/transaction.js';import { parseArgument, schemas } from './validators.js';/** The `strategy` option accepted by {@apilink ExtractLinksOptions} and {@apilink EnqueueUrlsOptions}. */@@ -231,7 +232,10 @@ class CrawleeRequest<UserData extends Dictionary = Dictionary> {// Read `__crawlee` explicitly - on a `userData` coming from another Request instance the// bag is non-enumerable, so the spread alone would silently drop the internal state// (e.g. `skipNavigation`) when a request is re-wrapped after a storage round trip.- this.#userData = { __crawlee: (userData as Dictionary).__crawlee ?? {}, ...userData };+ this.#userData = {+ ...snapshotValue(userData),+ __crawlee: snapshotValue((userData as Dictionary).__crawlee ?? {}),+ };// `userData` must stay an enumerable own accessor — serialization in the storages relies on itObject.defineProperties(this, {@@ -265,8 +269,8 @@ class CrawleeRequest<UserData extends Dictionary = Dictionary> {},});- // reassign userData to ensure internal `__crawlee` object is non-enumerable- this.userData = userData;+ // reassign the copy, not the possibly shared `userData`, so that internal `__crawlee` object is non-enumerable+ this.userData = this.#userData as UserData;const crawlee: CrawleeRequestData = ((this.userData as Dictionary).__crawlee ??= {});if (skipNavigation != null) crawlee.skipNavigation = skipNavigation;diff --git a/test/core/storages/request-queue.test.ts b/test/core/storages/request-queue.test.tsindex 6ad7202ae..cd9f3fb78 100644--- a/test/core/storages/request-queue.test.ts+++ b/test/core/storages/request-queue.test.ts@@ -376,6 +376,20 @@ describe('RequestQueue remote', () => {expect(r3.userData.__crawlee).toEqual({ maxRetries: 2 });});+ test('requests created from the same userData object do not share it', () => {+ // With a `__crawlee` bag, like a `userData` taken from another request.+ const userData: Record<string, unknown> = { label: 'DETAIL', tags: ['docs'], __crawlee: {} };+ const first = new CrawlingRequest({ url: 'http://example.com/1', userData });+ const second = new CrawlingRequest({ url: 'http://example.com/2', userData });++ first.userData.canonicalUrl = 'http://example.com/canonical';+ (first.userData.tags as string[]).push('first');+ first.sessionId = 'first-session';++ expect(second.userData).toEqual({ label: 'DETAIL', tags: ['docs'] });+ expect(second.sessionId).toBeUndefined();+ });+describe('setExpectedRequestProcessingTimeSecs', () => {test('forwards the value to the client, but only ever raises it', async () => {const queue = await RequestQueue.open();