Skip to content

Commit

Permalink
Use Response object to convert text to ReadableStream
Browse files Browse the repository at this point in the history
  • Loading branch information
AdiRishi committed Mar 15, 2024
1 parent 157c46e commit 873b4a8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/storage/storage-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export class StorageManager {
public static async readableStreamToBuffer(stream: ReadableStream): Promise<ArrayBuffer> {
return await new Response(stream).arrayBuffer();
}
public static textToReadableStream(text: string): ReadableStream {
return new Response(text).body!;
}
}

export class InvalidStorageError extends Error {
Expand Down
7 changes: 1 addition & 6 deletions tests/storage/kv-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!);
Expand Down
7 changes: 1 addition & 6 deletions tests/storage/r2-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!);
Expand Down

0 comments on commit 873b4a8

Please sign in to comment.