1 |
|
2 |
|
3 |
|
4 |
|
5 | import { canUseAsyncIteratorSymbol } from "../../utilities/index.js";
|
6 | import asyncIterator from "./iterators/async.js";
|
7 | import nodeStreamIterator from "./iterators/nodeStream.js";
|
8 | import promiseIterator from "./iterators/promise.js";
|
9 | import readerIterator from "./iterators/reader.js";
|
10 | function isNodeResponse(value) {
|
11 | return !!value.body;
|
12 | }
|
13 | function isReadableStream(value) {
|
14 | return !!value.getReader;
|
15 | }
|
16 | function isAsyncIterableIterator(value) {
|
17 | return !!(canUseAsyncIteratorSymbol &&
|
18 | value[Symbol.asyncIterator]);
|
19 | }
|
20 | function isStreamableBlob(value) {
|
21 | return !!value.stream;
|
22 | }
|
23 | function isBlob(value) {
|
24 | return !!value.arrayBuffer;
|
25 | }
|
26 | function isNodeReadableStream(value) {
|
27 | return !!value.pipe;
|
28 | }
|
29 | export 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 |
|
38 |
|
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 |
|
\ | No newline at end of file |