Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 1837937

Browse files
b-marquesBruno Nascimentomrbbot
authored
fix: unhandled error thrown by busboy (#711)
* 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 <[email protected]> * fix: test for formData unsupported Content-Type header The expectations weren't included in the `throwAsync()`. --------- Co-authored-by: Bruno Nascimento <[email protected]> Co-authored-by: MrBBot <[email protected]>
1 parent f919a2e commit 1837937

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

packages/core/src/standards/http.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -311,40 +311,44 @@ export class Body<Inner extends BaseRequest | BaseResponse> {
311311
);
312312
}
313313
const formData = new FormData();
314-
await new Promise<void>(async (resolve) => {
315-
const Busboy: typeof import("busboy") = require("busboy");
316-
const busboy = Busboy({
317-
headers: headers as http.IncomingHttpHeaders,
318-
preservePath: true,
319-
});
320-
busboy.on("field", (name, value) => {
321-
formData.append(name, value);
322-
});
323-
busboy.on("file", (name, value, info) => {
324-
const { filename, encoding, mimeType } = info;
325-
const base64 = encoding.toLowerCase() === "base64";
326-
const chunks: Buffer[] = [];
327-
let totalLength = 0;
328-
value.on("data", (chunk: Buffer) => {
329-
if (base64) chunk = Buffer.from(chunk.toString(), "base64");
330-
chunks.push(chunk);
331-
totalLength += chunk.byteLength;
314+
await new Promise<void>(async (resolve, reject) => {
315+
try {
316+
const Busboy: typeof import("busboy") = require("busboy");
317+
const busboy = Busboy({
318+
headers: headers as http.IncomingHttpHeaders,
319+
preservePath: true,
332320
});
333-
value.on("end", () => {
334-
if (this[kFormDataFiles]) {
335-
const file = new File(chunks, filename, { type: mimeType });
336-
formData.append(name, file);
337-
} else {
338-
const text = Buffer.concat(chunks, totalLength).toString();
339-
formData.append(name, text);
340-
}
321+
busboy.on("field", (name, value) => {
322+
formData.append(name, value);
341323
});
342-
});
343-
busboy.on("finish", resolve);
324+
busboy.on("file", (name, value, info) => {
325+
const { filename, encoding, mimeType } = info;
326+
const base64 = encoding.toLowerCase() === "base64";
327+
const chunks: Buffer[] = [];
328+
let totalLength = 0;
329+
value.on("data", (chunk: Buffer) => {
330+
if (base64) chunk = Buffer.from(chunk.toString(), "base64");
331+
chunks.push(chunk);
332+
totalLength += chunk.byteLength;
333+
});
334+
value.on("end", () => {
335+
if (this[kFormDataFiles]) {
336+
const file = new File(chunks, filename, { type: mimeType });
337+
formData.append(name, file);
338+
} else {
339+
const text = Buffer.concat(chunks, totalLength).toString();
340+
formData.append(name, text);
341+
}
342+
});
343+
});
344+
busboy.on("finish", resolve);
344345

345-
const body = this[_kInner].body;
346-
if (body !== null) for await (const chunk of body) busboy.write(chunk);
347-
busboy.end();
346+
const body = this[_kInner].body;
347+
if (body !== null) for await (const chunk of body) busboy.write(chunk);
348+
busboy.end();
349+
} catch (e) {
350+
reject(new TypeError(e instanceof Error ? e.message : String(e)));
351+
}
348352
});
349353
if (this[kInputGated]) await waitForOpenInputGate();
350354
return formData;

packages/core/test/standards/http.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,30 @@ test("Body: formData: respects Content-Transfer-Encoding header for base64 encod
382382
const formData = await body.formData();
383383
t.is(formData.get("key"), "test");
384384
});
385-
385+
test("Body: formData: throw error on missing boundary in Content-Type header", async (t) => {
386+
// Check with multipart/form-data Content-Type
387+
const body = new Body(
388+
new BaseResponse(["second value2", "--boundary--"].join("\r\n"), {
389+
headers: { "content-type": "multipart/form-data" },
390+
})
391+
);
392+
await t.throwsAsync(body.formData(), {
393+
instanceOf: TypeError,
394+
message: "Multipart: Boundary not found",
395+
});
396+
});
397+
test("Body: formData: throw error on unsupported Content-Type header", async (t) => {
398+
// Check with application/json Content-Type
399+
const body = new Body(
400+
new BaseResponse(["second value2", "--boundary--"].join("\r\n"), {
401+
headers: { "content-type": "application/json" },
402+
})
403+
);
404+
await t.throwsAsync(body.formData(), {
405+
instanceOf: TypeError,
406+
message: "Unsupported content type: application/json",
407+
});
408+
});
386409
test("Request: constructing from BaseRequest doesn't create new BaseRequest unless required", (t) => {
387410
// Check properties of Request are same as BaseRequest if not RequestInit passed
388411
const controller = new AbortController();

0 commit comments

Comments
 (0)