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
feat: allow passing a stream to `KeyValueStore.setRecord` (#1325)
Gabin Desserprit31ebd5c0b69a
diff --git a/src/storages/key_value_store.js b/src/storages/key_value_store.jsindex 60968587f..93880b1a0 100644--- a/src/storages/key_value_store.js+++ b/src/storages/key_value_store.js@@ -223,8 +223,8 @@ export class KeyValueStore {validator: ow.isValid(k, ow.string.matches(KEY_VALUE_STORE_KEY_REGEX)),message: 'The "key" argument must be at most 256 characters long and only contain the following characters: a-zA-Z0-9!-_.\'()',})));- if (options.contentType && !ow.isValid(value, ow.any(ow.string, ow.buffer))) {- throw new ArgumentError('The "value" parameter must be a String or Buffer when "options.contentType" is specified.', this.setValue);+ if (options.contentType && !(ow.isValid(value, ow.any(ow.string, ow.buffer)) || (ow.isValid(value, ow.object) && typeof value?.pipe === 'function'))) {+ throw new ArgumentError('The "value" parameter must be a String, Buffer or Stream when "options.contentType" is specified.', this.setValue);}ow(options, ow.object.exactShape({contentType: ow.optional.string.nonEmpty,diff --git a/test/storages/key_value_store.test.js b/test/storages/key_value_store.test.jsindex 8c6d9a859..a31cfbcfe 100644--- a/test/storages/key_value_store.test.js+++ b/test/storages/key_value_store.test.js@@ -1,6 +1,7 @@import {ENV_VARS,} from '@apify/consts';+import { PassThrough } from 'stream';import { apifyClient } from '../../build/utils';import {KeyValueStore,@@ -125,10 +126,10 @@ describe('KeyValueStore remote', () => {await expect(async () => store.setValue(123, 'some value')).rejects.toThrow('Expected argument to be of type `string` but received type `number`');- const valueErrMsg = 'The "value" parameter must be a String or Buffer when "options.contentType" is specified';+ const valueErrMsg = 'The "value" parameter must be a String, Buffer or Stream when "options.contentType" is specified';await expect(async () => store.setValue('key', {}, { contentType: 'image/png' })).rejects.toThrow(valueErrMsg);await expect(async () => store.setValue('key', 12345, { contentType: 'image/png' })).rejects.toThrow(valueErrMsg);- await expect(async () => store.setValue('key', () => {}, { contentType: 'image/png' })).rejects.toThrow(valueErrMsg);+ await expect(async () => store.setValue('key', () => { }, { contentType: 'image/png' })).rejects.toThrow(valueErrMsg);await expect(async () => store.setValue('key', {}, 123)).rejects.toThrow('Expected argument to be of type `object` but received type `number`');@@ -148,10 +149,10 @@ describe('KeyValueStore remote', () => {const contTypeRedundantErrMsg = 'Expected property string `contentType` to not be empty in object';await expect(async () => store.setValue('key', null, { contentType: 'image/png' }))- .rejects.toThrow('The "value" parameter must be a String or Buffer when "options.contentType" is specified.');+ .rejects.toThrow('The "value" parameter must be a String, Buffer or Stream when "options.contentType" is specified.');await expect(async () => store.setValue('key', null, { contentType: '' })).rejects.toThrow(contTypeRedundantErrMsg);await expect(async () => store.setValue('key', null, { contentType: {} }))- .rejects.toThrow('The "value" parameter must be a String or Buffer when "options.contentType" is specified.');+ .rejects.toThrow('The "value" parameter must be a String, Buffer or Stream when "options.contentType" is specified.');await expect(async () => store.setValue('key', 'value', { contentType: 123 })).rejects.toThrow('Expected property `contentType` to be of type `string` but received type `number` in object');@@ -273,6 +274,30 @@ describe('KeyValueStore remote', () => {contentType: 'image/jpeg; charset=something',});});++ test('correctly passes a stream', async () => {+ const store = new KeyValueStore({+ id: 'my-store-id-1',+ client: apifyClient,+ });++ const mockSetRecord = jest+ .spyOn(store.client, 'setRecord')+ .mockResolvedValueOnce(null);++ const value = new PassThrough();+ await store.setValue('key-1', value, { contentType: 'plain/text' });+ value.emit('data', 'hello world');+ value.end();+ value.destroy();++ expect(mockSetRecord).toHaveBeenCalledTimes(1);+ expect(mockSetRecord).toHaveBeenCalledWith({+ key: 'key-1',+ value,+ contentType: 'plain/text',+ });+ });});describe('getPublicUrl', () => {