import type { Readable } from 'node:stream' /** * Convert a Node.js [`Readable`](https://nodejs.org/dist/latest/docs/api/stream.html#class-streamreadable) * stream or a browser [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) * to an [iterable source](https://achingbrain.github.io/it-stream-types/types/Source.html). */ export function source (readable: Readable | ReadableStream): AsyncGenerator { // Browser ReadableStream if (isReadableStream(readable)) { return (async function * () { const reader = readable.getReader() try { while (true) { const { done, value } = await reader.read() if (done) { return } yield value } } finally { reader.releaseLock() } })() } if (isNodeStream(readable)) { return readable } throw new Error('unknown stream') } function isNodeStream (obj?: any): obj is AsyncGenerator { return obj[Symbol.asyncIterator] != null } function isReadableStream (obj?: any): obj is ReadableStream { return typeof obj?.getReader === 'function' }