-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(web): do not rely on ReadableStream.from or new ReadableStream
- Loading branch information
1 parent
9b942be
commit 25f5e2b
Showing
1 changed file
with
19 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,27 @@ import d from "https://cdn.skypack.dev/[email protected]"; | |
export { d as debug }; | ||
|
||
// === Export system-specific operations | ||
async function writeAsyncItrToStream<T>( | ||
itr: AsyncIterable<T>, | ||
writer: WritableStreamDefaultWriter<T>, | ||
) { | ||
for await (const chunk of itr) { | ||
await writer.write(chunk); | ||
} | ||
await writer.close(); | ||
} | ||
|
||
// Turn an AsyncIterable<Uint8Array> into a stream | ||
export const itrToStream = (itr: AsyncIterable<Uint8Array>) => { | ||
// do not assume ReadableStream.from to exist yet | ||
const it = itr[Symbol.asyncIterator](); | ||
return new ReadableStream({ | ||
async pull(controller) { | ||
const chunk = await it.next(); | ||
if (chunk.done) controller.close(); | ||
else controller.enqueue(chunk.value); | ||
}, | ||
}); | ||
// do not assume ReadableStream.from or new ReadableStream to exist yet | ||
const { readable, writable } = new TransformStream(); | ||
const writer = writable.getWriter(); | ||
|
||
writeAsyncItrToStream(itr, writer).catch((err) => | ||
console.error("Something went wrong with writing itrToStream") | ||
); | ||
|
||
return readable; | ||
}; | ||
|
||
// === Base configuration for `fetch` calls | ||
|