Skip to content

Commit 5885f09

Browse files
committed
fix: improve repsonse body normalization
1 parent 443a947 commit 5885f09

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

src/node_modules/@internal/micro-frame-component/node.marko

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22
import https from "https";
33
import fetch from "make-fetch-happen";
4+
import consumeResponseBody from "../../../util/consume-body";
45
static const { ca } = https.globalAgent.options;
56
static const cachePath = path.resolve("node_modules/.cache/fetch");
67
static const strictSSL = process.env.NODE_TLS_REJECT_UNAUTHORIZED !== "0";
@@ -81,11 +82,7 @@ static async function fetchBody(input, out, buffer) {
8182
8283
if (buffer) return res.text();
8384
84-
if (!res.body || !res.body[Symbol.asyncIterator]) {
85-
throw new Error("Response body must be a stream.");
86-
}
87-
88-
return res.body[Symbol.asyncIterator]();
85+
return consumeResponseBody(res);
8986
}
9087

9188
<div id=component.id class=input.class style=input.style data-src=input.src>

src/node_modules/@internal/stream-source-component/node.marko

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fetch from "make-fetch-happen";
22
import https from "https";
33
import path from "path";
44
import { getSource } from "../../../util/stream";
5+
import consumeResponseBody from "../../../util/consume-body";
56
static const { ca } = https.globalAgent.options;
67
static const cachePath = path.resolve("node_modules/.cache/fetch");
78
static const strictSSL = process.env.NODE_TLS_REJECT_UNAUTHORIZED !== "0";
@@ -85,9 +86,8 @@ $ const streamSource = getSource(input.name, out);
8586
<div id=component.id data-src=input.src>
8687
$ out.bf("@_", component, true);
8788
<await(request()) client-reorder timeout=input.timeout>
88-
<@then|{ body }|>
89-
$ const iter = input.parser(body[Symbol.asyncIterator]());
90-
<await(streamSource.run(iter)) client-reorder>
89+
<@then|res|>
90+
<await(streamSource.run(input.parser(consumeResponseBody(res)))) client-reorder>
9191
<@catch|err|>
9292
$ streamSource.close(err);
9393
</@catch>

src/util/consume-body.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const decoder = new TextDecoder();
2+
export default function consumeResponseBody(
3+
res: Response
4+
): AsyncIterator<string, void> | undefined {
5+
if (res.body) {
6+
if ((res.body as any).getReader) {
7+
return consumeBodyReader(res.body.getReader());
8+
}
9+
10+
if ((res.body as any)[Symbol.asyncIterator]) {
11+
return (res.body as any)[Symbol.asyncIterator]();
12+
}
13+
}
14+
15+
throw new Error("Response body must be a stream.");
16+
}
17+
18+
async function* consumeBodyReader(
19+
reader: ReadableStreamDefaultReader<Uint8Array>
20+
) {
21+
do {
22+
const next = await reader.read();
23+
if (next.done) break;
24+
yield decoder.decode(next.value);
25+
} while (true);
26+
}

0 commit comments

Comments
 (0)