Skip to content

Commit

Permalink
fix: improve repsonse body normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Dec 10, 2024
1 parent 443a947 commit 86b1ae8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
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);
}

0 comments on commit 86b1ae8

Please sign in to comment.