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: resolve duplex error when fetching with stream body #167

Merged
merged 1 commit into from
Aug 27, 2023
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: 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);
});
Loading