Skip to content

Commit

Permalink
fix: unhandled error thrown by busboy (#711)
Browse files Browse the repository at this point in the history
* fix: unhandled error thrown by busboy

As per, https://fetch.spec.whatwg.org/#dom-body-formdata, TypeErrors are
expected to be thrown. Busboy errors were being unhandled.

* chore: apply suggestions from mrbbot's code review

Co-authored-by: MrBBot <me@mrbbot.dev>

* fix: test for formData unsupported Content-Type header

The expectations weren't included in the `throwAsync()`.

---------

Co-authored-by: Bruno Nascimento <bruno.nascimento@csghq.com>
Co-authored-by: MrBBot <me@mrbbot.dev>
  • Loading branch information
3 people authored Oct 25, 2023
1 parent f919a2e commit 1837937
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
66 changes: 35 additions & 31 deletions packages/core/src/standards/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,40 +311,44 @@ export class Body<Inner extends BaseRequest | BaseResponse> {
);
}
const formData = new FormData();
await new Promise<void>(async (resolve) => {
const Busboy: typeof import("busboy") = require("busboy");
const busboy = Busboy({
headers: headers as http.IncomingHttpHeaders,
preservePath: true,
});
busboy.on("field", (name, value) => {
formData.append(name, value);
});
busboy.on("file", (name, value, info) => {
const { filename, encoding, mimeType } = info;
const base64 = encoding.toLowerCase() === "base64";
const chunks: Buffer[] = [];
let totalLength = 0;
value.on("data", (chunk: Buffer) => {
if (base64) chunk = Buffer.from(chunk.toString(), "base64");
chunks.push(chunk);
totalLength += chunk.byteLength;
await new Promise<void>(async (resolve, reject) => {
try {
const Busboy: typeof import("busboy") = require("busboy");
const busboy = Busboy({
headers: headers as http.IncomingHttpHeaders,
preservePath: true,
});
value.on("end", () => {
if (this[kFormDataFiles]) {
const file = new File(chunks, filename, { type: mimeType });
formData.append(name, file);
} else {
const text = Buffer.concat(chunks, totalLength).toString();
formData.append(name, text);
}
busboy.on("field", (name, value) => {
formData.append(name, value);
});
});
busboy.on("finish", resolve);
busboy.on("file", (name, value, info) => {
const { filename, encoding, mimeType } = info;
const base64 = encoding.toLowerCase() === "base64";
const chunks: Buffer[] = [];
let totalLength = 0;
value.on("data", (chunk: Buffer) => {
if (base64) chunk = Buffer.from(chunk.toString(), "base64");
chunks.push(chunk);
totalLength += chunk.byteLength;
});
value.on("end", () => {
if (this[kFormDataFiles]) {
const file = new File(chunks, filename, { type: mimeType });
formData.append(name, file);
} else {
const text = Buffer.concat(chunks, totalLength).toString();
formData.append(name, text);
}
});
});
busboy.on("finish", resolve);

const body = this[_kInner].body;
if (body !== null) for await (const chunk of body) busboy.write(chunk);
busboy.end();
const body = this[_kInner].body;
if (body !== null) for await (const chunk of body) busboy.write(chunk);
busboy.end();
} catch (e) {
reject(new TypeError(e instanceof Error ? e.message : String(e)));
}
});
if (this[kInputGated]) await waitForOpenInputGate();
return formData;
Expand Down
25 changes: 24 additions & 1 deletion packages/core/test/standards/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,30 @@ test("Body: formData: respects Content-Transfer-Encoding header for base64 encod
const formData = await body.formData();
t.is(formData.get("key"), "test");
});

test("Body: formData: throw error on missing boundary in Content-Type header", async (t) => {
// Check with multipart/form-data Content-Type
const body = new Body(
new BaseResponse(["second value2", "--boundary--"].join("\r\n"), {
headers: { "content-type": "multipart/form-data" },
})
);
await t.throwsAsync(body.formData(), {
instanceOf: TypeError,
message: "Multipart: Boundary not found",
});
});
test("Body: formData: throw error on unsupported Content-Type header", async (t) => {
// Check with application/json Content-Type
const body = new Body(
new BaseResponse(["second value2", "--boundary--"].join("\r\n"), {
headers: { "content-type": "application/json" },
})
);
await t.throwsAsync(body.formData(), {
instanceOf: TypeError,
message: "Unsupported content type: application/json",
});
});
test("Request: constructing from BaseRequest doesn't create new BaseRequest unless required", (t) => {
// Check properties of Request are same as BaseRequest if not RequestInit passed
const controller = new AbortController();
Expand Down

0 comments on commit 1837937

Please sign in to comment.