-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(internal): minor restructuring (#143)
- Loading branch information
1 parent
ff0f7bd
commit 8c0c78d
Showing
3 changed files
with
36 additions
and
36 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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Most browsers don't yet have async iterable support for ReadableStream, | ||
* and Node has a very different way of reading bytes from its "ReadableStream". | ||
* | ||
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490 | ||
*/ | ||
export function ReadableStreamToAsyncIterable<T>(stream: any): AsyncIterableIterator<T> { | ||
if (stream[Symbol.asyncIterator]) return stream; | ||
|
||
const reader = stream.getReader(); | ||
return { | ||
async next() { | ||
try { | ||
const result = await reader.read(); | ||
if (result?.done) reader.releaseLock(); // release lock when stream becomes closed | ||
return result; | ||
} catch (e) { | ||
reader.releaseLock(); // release lock when stream becomes errored | ||
throw e; | ||
} | ||
}, | ||
async return() { | ||
const cancelPromise = reader.cancel(); | ||
reader.releaseLock(); | ||
await cancelPromise; | ||
return { done: true, value: undefined }; | ||
}, | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
}; | ||
} |
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