Skip to content

Commit

Permalink
fix: avoid serializing buffer body (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Aug 23, 2023
1 parent b9ef395 commit cb05d8f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export function isJSONSerializable(value: any) {
if (Array.isArray(value)) {
return true;
}
if (value.buffer) {
return false;
}
return (
(value.constructor && value.constructor.name === "Object") ||
typeof value.toJSON === "function"
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ describe("ofetch", () => {
expect(body).to.deep.eq(message);
});

it("Handle buffer body", async () => {
const message = "Hallo von Pascal";
const { body } = await $fetch(getURL("echo"), {
method: "POST",
body: Buffer.from("Hallo von Pascal"),
headers: { "Content-Type": "text/plain" },
});
expect(body).to.deep.eq(message);
});

it("Bypass FormData body", async () => {
const data = new FormData();
data.append("foo", "bar");
Expand Down

0 comments on commit cb05d8f

Please sign in to comment.