Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(MemoryStorage): handling of readable streams for key-value stores when setting records #1852

Merged
merged 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/memory-storage/src/resource-clients/key-value-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,16 @@ export class KeyValueStoreClient extends BaseClient {
async setRecord(record: storage.KeyValueStoreRecord): Promise<void> {
s.object({
key: s.string.lengthGreaterThan(0),
value: s.union(s.null, s.string, s.number, s.instance(Buffer), s.object({}).passthrough),
value: s.union(
s.null,
s.string,
s.number,
s.instance(Buffer),
s.instance(ArrayBuffer),
s.typedArray(),
// disabling validation will make shapeshift only check the object given is an actual object, not null, nor array
s.object({}).setValidationEnabled(false),
),
contentType: s.string.lengthGreaterThan(0).optional,
}).parse(record);

Expand Down
17 changes: 4 additions & 13 deletions packages/memory-storage/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export function uniqueKeyToRequestId(uniqueKey: string): string {
export function isBuffer(value: unknown): boolean {
try {
s.union(
s.typedArray(),
s.instance(ArrayBuffer),
s.instance(Buffer),
s.instance(ArrayBuffer),
s.typedArray(),
).parse(value);

return true;
Expand All @@ -44,17 +44,8 @@ export function isBuffer(value: unknown): boolean {
}
}

export function isStream(value: unknown): boolean {
try {
s.object({
on: s.any,
pipe: s.any,
}).passthrough.parse(value);

return true;
} catch {
return false;
}
export function isStream(value: any): boolean {
return typeof value === 'object' && value && ['on', 'pipe'].every((key) => key in value && typeof value[key] === 'function');
}

export const memoryStorageLog = defaultLog.child({ prefix: 'MemoryStorage' });
Expand Down
22 changes: 22 additions & 0 deletions packages/memory-storage/test/key-value-store-stream.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MemoryStorage } from '@crawlee/memory-storage';
import { Readable } from 'node:stream';

describe('KeyValueStore should drain streams when setting records', () => {
const storage = new MemoryStorage({
persistStorage: false,
});

const fsStream = Readable.from([Buffer.from('hello'), Buffer.from('world')]);

test('should drain stream', async () => {
const defaultStoreInfo = await storage.keyValueStores().getOrCreate('default');
const defaultStore = storage.keyValueStore(defaultStoreInfo.id);

await defaultStore.setRecord({ key: 'streamz', value: fsStream, contentType: 'text/plain' });

expect(fsStream.destroyed).toBeTruthy();

const record = await defaultStore.getRecord('streamz');
expect(record!.value.toString('utf8')).toEqual('helloworld');
});
});