-
-
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
385 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,62 @@ | ||
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) { | ||
if (!controller.signal.aborted || error.cause === undefined) { | ||
throw error; | ||
} else if (error.cause !== abortError) { | ||
throw error.cause; | ||
} | ||
} finally { | ||
stream.destroy(); | ||
} | ||
}; | ||
|
||
const handleStreamEnd = async (stream, controller) => { | ||
try { | ||
await nodeImports.streamPromises.finished(stream, {cleanup: true, readable: true, writable: false, error: false}); | ||
controller.abort(abortError); | ||
} catch (error) { | ||
controller.abort(error); | ||
} | ||
}; | ||
|
||
const abortError = 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.