Skip to content

Commit

Permalink
fix: resolve duplex error when fetching with stream body (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
banyudu authored Aug 27, 2023
1 parent bc5524b commit 1540593
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/patch/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,12 @@ const Xhook: typeof fetch = function (input, init = { headers: {} }) {
}
};

const send = () => {
const send = async () => {
const { url, isFetch, acceptedRequest, ...restInit } = options;
Native(url, restInit)
if (input instanceof Request && restInit.body instanceof ReadableStream) {
restInit.body = await new Response(restInit.body).text();
}
return Native(url, restInit)
.then(response => processAfter(response))
.catch(function (err) {
fullfiled = reject;
Expand Down
21 changes: 21 additions & 0 deletions tests/fetch-stream-body.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test, expect } from "@playwright/test";

test("fetch with stream body should not throw errors", async ({ page }) => {
await page.goto("http://127.0.0.1:8080/example/common.html");
const res = await page.evaluate(async () => {
return new Promise(resolve => {
const req = new Request("example1.txt", {
method: "POST",
body: "{}",
});
fetch(req)
.then(res => {
resolve(true);
})
.catch(err => {
resolve(false);
});
});
});
expect(res).toEqual(true);
});

0 comments on commit 1540593

Please sign in to comment.