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 1 commit
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
80 changes: 45 additions & 35 deletions packages/core/src/standards/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,41 +311,51 @@ 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;
});
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 busboyPromiseResult = await new Promise<void | Error>(
async (resolve, reject) => {
b-marques marked this conversation as resolved.
Show resolved Hide resolved
try {
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;
});
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();
} catch (e) {
reject(e);
b-marques marked this conversation as resolved.
Show resolved Hide resolved
}
}
);
if (busboyPromiseResult instanceof Error) {
throw new TypeError(busboyPromiseResult.message);
}
if (this[kInputGated]) await waitForOpenInputGate();
return formData;
}
Expand Down
23 changes: 22 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,28 @@ 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
let body = new Body(
b-marques marked this conversation as resolved.
Show resolved Hide resolved
new BaseResponse(["second value2", "--boundary--"].join("\r\n"), {
headers: { "content-type": "multipart/form-data" },
})
);
let formData = await body.formData().catch((e) => e);
t.is(formData instanceof Error, true);
t.is(formData.message, "Multipart: Boundary not found");
b-marques marked this conversation as resolved.
Show resolved Hide resolved
});
test("Body: formData: throw error on unsupported Content-Type header", async (t) => {
// Check with multipart/form-data Content-Type
b-marques marked this conversation as resolved.
Show resolved Hide resolved
let body = new Body(
new BaseResponse(["second value2", "--boundary--"].join("\r\n"), {
headers: { "content-type": "application/json" },
})
);
let formData = await body.formData().catch((e) => e);
t.is(formData instanceof Error, true);
t.is(formData.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