-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
391 additions
and
6 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,66 @@ | ||
import {isReadableStream} from 'is-stream'; | ||
|
||
export const getAsyncIterable = stream => { | ||
if (isReadableStream(stream, {checkOpen: false})) { | ||
return getStreamIterable(stream); | ||
} | ||
|
||
if (typeof stream?.[Symbol.asyncIterator] !== 'function') { | ||
throw new TypeError('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) { | ||
if (nodeImports === undefined) { | ||
await loadNodeImports(); | ||
} | ||
|
||
const controller = new AbortController(); | ||
handleStreamEnd(stream, controller); | ||
|
||
try { | ||
for await (const [chunk] of nodeImports.events.on(stream, 'data', { | ||
signal: controller.signal, | ||
highWatermark: stream.readableHighWaterMark, | ||
})) { | ||
yield chunk; | ||
} | ||
} catch (error) { | ||
// `error` event directly emitted on stream | ||
if (!controller.signal.aborted || error.cause === undefined) { | ||
throw error; | ||
// Stream failure via `controller.abort(error)` below, for example due to `stream.destroy(error)` | ||
} else if (error.cause !== endError) { | ||
throw error.cause; | ||
} | ||
// Otherwise, this returns successfully via `controller.abort(endError)`, for example due `stream` having completed. | ||
// The `finally` block also runs when the caller throws, for example due to the `maxBuffer` option. | ||
} finally { | ||
stream.destroy(); | ||
} | ||
}; | ||
|
||
const handleStreamEnd = async (stream, controller) => { | ||
try { | ||
await nodeImports.streamPromises.finished(stream, {cleanup: true, readable: true, writable: false, error: false}); | ||
controller.abort(endError); | ||
} catch (error) { | ||
controller.abort(error); | ||
} | ||
}; | ||
|
||
const endError = new Error('Internal error'); | ||
|
||
// Use dynamic imports to support browsers | ||
const loadNodeImports = async () => { | ||
const [events, streamPromises] = await Promise.all([ | ||
import('node:events'), | ||
import('node:stream/promises'), | ||
]); | ||
nodeImports = {events, streamPromises}; | ||
}; | ||
|
||
let nodeImports; |
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.