Skip to content

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.

fix(memory-storage): store JSON snapshots in `DatasetClient.pushData()` (#3831)

PilgrimStackd0c5803468a5
diff --git a/packages/core/src/memory-storage/resource-clients/dataset.ts b/packages/core/src/memory-storage/resource-clients/dataset.ts
index 89877de59..5bf2f7aca 100644
--- a/packages/core/src/memory-storage/resource-clients/dataset.ts
+++ b/packages/core/src/memory-storage/resource-clients/dataset.ts
@@ -124,7 +124,7 @@ export class DatasetClient<Data extends Dictionary = Dictionary>
async pushData(items: Data[]): Promise<void> {
for (const entry of items) {
const idx = this.generateLocalEntryName(++this.itemCount);
- this.datasetEntries.set(idx, entry);
+ this.datasetEntries.set(idx, JSON.parse(JSON.stringify(entry)) as Data);
}
this.updateTimestamps(true);
diff --git a/test/core/storages/dataset.test.ts b/test/core/storages/dataset.test.ts
index 00800a1ea..0c8fdc764 100644
--- a/test/core/storages/dataset.test.ts
+++ b/test/core/storages/dataset.test.ts
@@ -368,6 +368,30 @@ describe('dataset', () => {
const jsonErrMsg = 'Converting circular structure to JSON';
await expect(dataset.pushData(circularObj)).rejects.toThrow(jsonErrMsg);
});
+
+ test('stores independent snapshots, not object references', async () => {
+ const dataset = await Dataset.open({ name: `test-snapshots-${Date.now()}` });
+ const mutableData = { rand: 0, counter: 0 };
+
+ await dataset.pushData(mutableData);
+ mutableData.rand = Math.random();
+ mutableData.counter = 1;
+ await dataset.pushData(mutableData);
+ mutableData.rand = Math.random();
+ mutableData.counter = 2;
+ await dataset.pushData(mutableData);
+
+ const { items } = await dataset.getData();
+
+ expect(items).toHaveLength(3);
+ expect(items[0]).toEqual({ rand: 0, counter: 0 });
+ expect(items[1].counter).toBe(1);
+ expect(items[2].counter).toBe(2);
+
+ // Each push must store an independent snapshot — items should not be the same reference
+ expect(items[0]).not.toBe(items[1]);
+ expect(items[1]).not.toBe(items[2]);
+ });
});
describe('utils', () => {