UNPKG

1.63 kBJavaScriptView Raw
1/**
2 * Original source:
3 * https://github.com/kmalakoff/response-iterator/blob/master/src/index.ts
4 */
5import { canUseAsyncIteratorSymbol } from "../../utilities/index.js";
6import asyncIterator from "./iterators/async.js";
7import nodeStreamIterator from "./iterators/nodeStream.js";
8import promiseIterator from "./iterators/promise.js";
9import readerIterator from "./iterators/reader.js";
10function isNodeResponse(value) {
11 return !!value.body;
12}
13function isReadableStream(value) {
14 return !!value.getReader;
15}
16function isAsyncIterableIterator(value) {
17 return !!(canUseAsyncIteratorSymbol &&
18 value[Symbol.asyncIterator]);
19}
20function isStreamableBlob(value) {
21 return !!value.stream;
22}
23function isBlob(value) {
24 return !!value.arrayBuffer;
25}
26function isNodeReadableStream(value) {
27 return !!value.pipe;
28}
29export function responseIterator(response) {
30 var body = response;
31 if (isNodeResponse(response))
32 body = response.body;
33 if (isAsyncIterableIterator(body))
34 return asyncIterator(body);
35 if (isReadableStream(body))
36 return readerIterator(body.getReader());
37 // this errors without casting to ReadableStream<T>
38 // because Blob.stream() returns a NodeJS ReadableStream
39 if (isStreamableBlob(body)) {
40 return readerIterator(body.stream().getReader());
41 }
42 if (isBlob(body))
43 return promiseIterator(body.arrayBuffer());
44 if (isNodeReadableStream(body))
45 return nodeStreamIterator(body);
46 throw new Error("Unknown body type for responseIterator. Please pass a streamable response.");
47}
48//# sourceMappingURL=responseIterator.js.map
\No newline at end of file