astro-ghostcms/.pnpm-store/v3/files/94/2e16339aa049a501a5fd1de9e7e...

22 lines
652 B
Plaintext
Raw Permalink Normal View History

2024-02-14 14:10:47 +00:00
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;
}
}
export 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.");
};