-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
394 additions
and
14 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
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,46 @@ | ||
import {isReadableStream} from 'is-stream'; | ||
|
||
export const getAsyncIterable = stream => { | ||
if (isReadableStream(stream, {checkOpen: false})) { | ||
return getStreamIterable(stream); | ||
} | ||
|
||
if (stream?.[Symbol.asyncIterator] === undefined) { | ||
throw new Error('The first argument must be a Readable, a ReadableStream, or an async iterable.'); | ||
} | ||
|
||
return stream; | ||
}; | ||
|
||
// The default iterable for Node.js streams does not allow for multiple readers at once, so we re-implement it | ||
const getStreamIterable = async function * (stream) { | ||
const {on, finished} = await getNodeImports(); | ||
const onStreamData = on(stream, 'data', {highWatermark: stream.readableHighWaterMark}); | ||
handleStreamEnd(stream, onStreamData, finished); | ||
try { | ||
for await (const [chunk] of onStreamData) { | ||
yield chunk; | ||
} | ||
} finally { | ||
stream.destroy(); | ||
} | ||
}; | ||
|
||
const handleStreamEnd = async (stream, onStreamData, finished) => { | ||
try { | ||
await finished(stream, {cleanup: true, readable: true, writable: false}); | ||
await onStreamData.return(); | ||
} catch (error) { | ||
const normalizedError = error instanceof Error ? error : new Error(String(error)); | ||
await onStreamData.throw(normalizedError); | ||
} | ||
}; | ||
|
||
// Use dynamic imports to support browsers | ||
const getNodeImports = async () => { | ||
const [{on}, {finished}] = await Promise.all([ | ||
import('node:events'), | ||
import('node:stream/promises'), | ||
]); | ||
return {on, finished}; | ||
}; |
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
Oops, something went wrong.