From 873b4a813cee51d7e644dcb4dfe3bc55e68bd5cd Mon Sep 17 00:00:00 2001 From: Adishwar Rishi Date: Fri, 15 Mar 2024 19:11:19 +1100 Subject: [PATCH] Use Response object to convert text to ReadableStream --- src/storage/storage-manager.ts | 3 +++ tests/storage/kv-storage.test.ts | 7 +------ tests/storage/r2-storage.test.ts | 7 +------ 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/storage/storage-manager.ts b/src/storage/storage-manager.ts index db81716..f604b3b 100644 --- a/src/storage/storage-manager.ts +++ b/src/storage/storage-manager.ts @@ -37,6 +37,9 @@ export class StorageManager { public static async readableStreamToBuffer(stream: ReadableStream): Promise { return await new Response(stream).arrayBuffer(); } + public static textToReadableStream(text: string): ReadableStream { + return new Response(text).body!; + } } export class InvalidStorageError extends Error { diff --git a/tests/storage/kv-storage.test.ts b/tests/storage/kv-storage.test.ts index c420ac9..0c79bd4 100644 --- a/tests/storage/kv-storage.test.ts +++ b/tests/storage/kv-storage.test.ts @@ -110,12 +110,7 @@ describe('kv-storage', () => { }); test('can write stream value', async () => { - const stream = new ReadableStream({ - start: (controller) => { - controller.enqueue('value1'); - controller.close(); - }, - }); + const stream = StorageManager.textToReadableStream('value1'); await storage.write('key1', stream); const result = await storage.read('key1'); const dataAsText = await StorageManager.readableStreamToText(result!); diff --git a/tests/storage/r2-storage.test.ts b/tests/storage/r2-storage.test.ts index 64aecf6..be52f8b 100644 --- a/tests/storage/r2-storage.test.ts +++ b/tests/storage/r2-storage.test.ts @@ -109,12 +109,7 @@ describe('r2-storage', () => { }); test('can write stream value', async () => { - const stream = new ReadableStream({ - start: (controller) => { - controller.enqueue('value1'); - controller.close(); - }, - }); + const stream = StorageManager.textToReadableStream('value1'); await storage.write('key1', stream); const result = await storage.read('key1'); const dataAsText = await StorageManager.readableStreamToText(result!);