mirror of https://github.com/jkjoy/sunpeiwen.git
23 lines
675 B
JavaScript
23 lines
675 B
JavaScript
import isFunction from "./isFunction.js";
|
|
const isAsyncIterable = (value) => (isFunction(value[Symbol.asyncIterator]));
|
|
async function* readStream(readable) {
|
|
const reader = readable.getReader();
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) {
|
|
break;
|
|
}
|
|
yield value;
|
|
}
|
|
}
|
|
const getStreamIterator = (source) => {
|
|
if (isAsyncIterable(source)) {
|
|
return source;
|
|
}
|
|
if (isFunction(source.getReader)) {
|
|
return readStream(source);
|
|
}
|
|
throw new TypeError("Unsupported data source: Expected either ReadableStream or async iterable.");
|
|
};
|
|
export default getStreamIterator;
|