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: unhandled error thrown by busboy #711

Merged
merged 3 commits into from
Oct 25, 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
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
Loading