Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve response body normalization #42

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/node_modules/@internal/micro-frame-component/node.marko
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "path";
import https from "https";
import fetch from "make-fetch-happen";
import consumeResponseBody from "../../../util/consume-body";
static const { ca } = https.globalAgent.options;
static const cachePath = path.resolve("node_modules/.cache/fetch");
static const strictSSL = process.env.NODE_TLS_REJECT_UNAUTHORIZED !== "0";
Expand Down Expand Up @@ -81,11 +82,7 @@ static async function fetchBody(input, out, buffer) {

if (buffer) return res.text();

if (!res.body || !res.body[Symbol.asyncIterator]) {
throw new Error("Response body must be a stream.");
}

return res.body[Symbol.asyncIterator]();
return consumeResponseBody(res);
}

<div id=component.id class=input.class style=input.style data-src=input.src>
Expand Down
6 changes: 3 additions & 3 deletions src/node_modules/@internal/stream-source-component/node.marko
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fetch from "make-fetch-happen";
import https from "https";
import path from "path";
import { getSource } from "../../../util/stream";
import consumeResponseBody from "../../../util/consume-body";
static const { ca } = https.globalAgent.options;
static const cachePath = path.resolve("node_modules/.cache/fetch");
static const strictSSL = process.env.NODE_TLS_REJECT_UNAUTHORIZED !== "0";
Expand Down Expand Up @@ -85,9 +86,8 @@ $ const streamSource = getSource(input.name, out);
<div id=component.id data-src=input.src>
$ out.bf("@_", component, true);
<await(request()) client-reorder timeout=input.timeout>
<@then|{ body }|>
$ const iter = input.parser(body[Symbol.asyncIterator]());
<await(streamSource.run(iter)) client-reorder>
<@then|res|>
<await(streamSource.run(input.parser(consumeResponseBody(res)))) client-reorder>
<@catch|err|>
$ streamSource.close(err);
</@catch>
Expand Down
26 changes: 26 additions & 0 deletions src/util/consume-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const decoder = new TextDecoder();
export default function consumeResponseBody(
res: Response
): AsyncIterator<string, void> | undefined {
if (res.body) {
if ((res.body as any).getReader) {
return consumeBodyReader(res.body.getReader());
}

if ((res.body as any)[Symbol.asyncIterator]) {
return (res.body as any)[Symbol.asyncIterator]();
}
}

throw new Error("Response body must be a stream.");
}

async function* consumeBodyReader(
reader: ReadableStreamDefaultReader<Uint8Array>
) {
do {
const next = await reader.read();
if (next.done) break;
yield decoder.decode(next.value);
} while (true);
}
Loading