diff --git a/src/utils/body.ts b/src/utils/body.ts index 8c96fe5d..ef4f6f77 100644 --- a/src/utils/body.ts +++ b/src/utils/body.ts @@ -30,8 +30,12 @@ export function readRawBody( (event.node.req as any)[RawBodySymbol] || (event.node.req as any).body; /* unjs/unenv #8 */ if (_rawBody) { - const promise = Promise.resolve(_rawBody); - return encoding ? promise.then((buff) => buff.toString(encoding)) : promise; + const promise = Promise.resolve( + Buffer.isBuffer(_rawBody) ? _rawBody : Buffer.from(_rawBody) + ); + return encoding + ? promise.then((buff) => buff.toString(encoding)) + : (promise as Promise); } if (!Number.parseInt(event.node.req.headers["content-length"] || "")) { diff --git a/test/body.test.ts b/test/body.test.ts index 2a674ff0..53069d97 100644 --- a/test/body.test.ts +++ b/test/body.test.ts @@ -174,7 +174,7 @@ describe("", () => { expect(result.text).toBe("200"); }); - it("handle raw body with buffer type (unenv)", async () => { + it("handle readBody with buffer type (unenv)", async () => { app.use( "/", eventHandler(async (event) => { @@ -194,6 +194,23 @@ describe("", () => { expect(result.text).toBe("200"); }); + it("handle readRawBody with array buffer type (unenv)", async () => { + app.use( + "/", + eventHandler(async (event) => { + // Emulate unenv + // @ts-ignore + event.node.req.body = new Uint8Array([1, 2, 3]); + const body = await readRawBody(event, false); + expect(body).toBeInstanceOf(Buffer); + expect(body).toMatchObject(Buffer.from([1, 2, 3])); + return "200"; + }) + ); + const result = await request.post("/api/test").send(); + expect(result.text).toBe("200"); + }); + it("parses multipart form data", async () => { app.use( "/",