How to Parse a Multipart Boundary
Parse a multipart form body by boundary so each part is streamed without buffering the whole body, and file and text fields are separated cleanly.
On this page
Introduction
The hardest part of stream multipart parts is not writing the code — it is deciding what the code is allowed to reject. A multipart parsing setup that accepts everything is easy to ship and expensive to operate, because every malformed input becomes a production surprise. The discipline is to enumerate the shapes the system actually receives, encode them in multipart parsing, and let the library reject the rest loudly. This keeps the happy path fast and the failure path visible.
Most teams adopt multipart parsing for stream multipart parts after an incident teaches them that unvalidated input is load-bearing. The incident is the expensive way to learn the lesson; a quarterly review of the input shapes is the cheap way. Pin the shapes with multipart parsing, refuse the deploy that breaks the pin, and treat the pin as a contract the rest of the system can rely on. A contract that is not enforced is a wish.
The sections below walk through the concrete decisions: where to put the multipart parsing check, how to keep it fast under load, what to do when the contract changes, and how to test the failure paths so they degrade on purpose instead of by accident.
Extract the boundary
Extract the boundary is the first decision because it determines what the rest of the multipart parsing pipeline can assume. A wrong choice here turns into stream multipart parts under load, and the invariant you thought multipart parsing guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Extract the boundary is the one most teams skip: drive multipart parsing to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
import busboy from 'busboy';
const bb = busboy({ headers: req.headers, limits: { fileSize: 5e6 } });
bb.on('file', (name, stream, info) => stream.pipe(fs.createWriteStream(`/tmp/${info.filename}`)));
bb.on('field', (name, val) => fields[name] = val);
req.pipe(bb);
The javascript above is the shape to ship for stream multipart parts. It is small enough to audit and specific enough that the failure behavior is obvious. Adapt the constants to your traffic and latency budget, and resist the urge to handle cases the system has not seen yet.
Stream with busboy
The mechanism behind Stream with busboy is straightforward in multipart parsing, but the consequences ripple. A wrong choice here turns into stream multipart parts under load, and the invariant you thought multipart parsing guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Stream with busboy is the one most teams skip: drive multipart parsing to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Handle file and field parts
Handle file and field parts is where most multipart parsing implementations leak performance. A wrong choice here turns into stream multipart parts under load, and the invariant you thought multipart parsing guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Handle file and field parts is the one most teams skip: drive multipart parsing to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
test('separates file and field', () => {
req.pipe(bb);
expect(fields.title).toBe('hello');
});
Pin the behavior with this test. A multipart parsing setup that passes this test degrades on purpose instead of by accident, which is the difference between a managed failure and an incident.
Enforce per-part limits
The hard part of Enforce per-part limits is not the happy path — it is what happens when multipart parsing fails halfway. A wrong choice here turns into stream multipart parts under load, and the invariant you thought multipart parsing guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Enforce per-part limits is the one most teams skip: drive multipart parsing to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Release on finish
Release on finish should be observable without a debugger. A wrong choice here turns into stream multipart parts under load, and the invariant you thought multipart parsing guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Release on finish is the one most teams skip: drive multipart parsing to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Test parsing
Test parsing is a trade, not a winner. A wrong choice here turns into stream multipart parts under load, and the invariant you thought multipart parsing guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Test parsing is the one most teams skip: drive multipart parsing to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Common pitfalls
The common pitfalls in stream multipart parts all reduce to one mistake: treating multipart parsing as a formality instead of a contract. The contract is the thing the rest of the system relies on, and a weak contract propagates weakness everywhere.
- Skipping the multipart parsing boundary so stream multipart parts enters unvalidated.
- Assuming multipart parsing is fast without measuring the tail latency under load.
- Letting stream multipart parts fail silently instead of failing loudly with a metric.
- Testing only the happy path so the multipart parsing failure surfaces in production.
- Sharing mutable state across requests so one bad input poisons the next.
Conclusion
Do not conflate the conclusion with the implementation. They look similar from the outside but the failure modes differ: multipart parsing fails fast and the caller knows, while an unchecked path fails slow and the caller guesses. Choose the one whose failure mode your system can absorb, and document the choice at the call site.
Self-check: write a test that drives multipart parsing to the failing condition and assert the system degrades gracefully. Then stream multipart parts is a property you maintain, not a feature you hope for.
Sources: busboy, Multipart forms
FAQ
- Why busboy?
- It is streaming and fast. formidable buffers; busboy does not.
- How prevent path traversal?
- Use a UUID for the stored name. Never trust info.filename for the path.
Report a problem with this articleOpens a pre-filled issue on GitHub.