Multipart upload handling
Multipart handling lets Formidable accept one request stream that carries both text fields and file uploads. It identifies the multipart format from the request headers, separates the stream into parts, and sends each part into the normal form lifecycle without loading the full request into memory. See the content-type plugin system for how this handler is selected and file lifecycle and storage for what happens after a file part is accepted.
Request recognition
The multipart plugin in home/…/multipart.js recognizes multipart request types before body data is processed. It marks the form as multipart, extracts the boundary that frames the body, and creates the parser that consumes the request stream.
Content-type detection
The handler accepts a Content-Type whose media type starts with multipart. This currently covers both multipart/form-data and multipart/related, so the same part flow can serve browser forms and related multipart messages.
The boundary is a required parameter. A missing boundary stops the form with a bad-request error instead of allowing the body to be parsed as an unknown stream.
References: src/…/multipart.js• L8-35 src/FormidableError.js• L12-17
Request stream input
After header processing, the request body reaches the multipart parser as a stream of buffers. A request can use normal length tracking or transfer-framed input; multipart parsing uses the bytes it receives and does not require the whole body to be available first.
Request-level Content-Encoding is handled before this stage. The form first removes supported compression from the request stream, then passes the resulting bytes to the selected parser. This is separate from the per-part transfer encoding described below.
References: src/Formidable.js• L242-294 src/Formidable.js• L541-551
Boundary stream processing
The multipart parser turns a continuous byte stream into part events. It tracks the opening boundary, part headers, part data, each closing boundary, and the final end signal while preserving data that may be split across input chunks.
Boundary framing
The parser treats the boundary as a line marker with the multipart framing prefix. It recognizes a normal boundary as the start of another part and a final boundary as the end of the message. It keeps a small look-behind area while it checks a possible boundary, so bytes are not lost when a boundary-like sequence crosses a chunk or proves to be part data.
The parser requires the expected line endings and header separators. An incomplete or malformed message produces a malformed-multipart error when the stream ends or when the parser finds invalid framing.
References: src/…/Multipart.js• L48-94 src/…/Multipart.js• L139-180 src/…/Multipart.js• L235-321
Part event sequence
Each part follows the same event sequence. The plugin uses this sequence to build a part stream and to delay body handling until its headers are complete.
flowchart LR
A[Opening boundary] --> B[partBegin]
B --> C[Part headers]
C --> D[headersEnd]
D --> E[partData chunks]
E --> F[partEnd]
F --> G{More parts?}
G -->|Yes| B
G -->|No| H[end]
The parser emits header and data ranges rather than complete strings or files. This lets the multipart plugin forward each data chunk to the correct field or file consumer as soon as it is safe to do so.
References: src/…/Multipart.js• L97-102 src/…/Multipart.js• L158-169 src/…/Multipart.js• L241-295 src/…/multipart.js• L54-75
Part classification
Multipart headers describe the meaning of each part. Formidable uses the part name for the result key, the filename to identify an upload, and the content type to decide whether the part is treated as a file.
Part metadata
The plugin collects part headers without depending on their letter case. It reads name and filename from Content-Disposition, and it records Content-Type as the part MIME type. The original filename is normalized through the form's filename reader before it is given to the upload path.
The presence of a MIME type is the decisive file test in the form handler. A part without a MIME type enters the field path even if other metadata is present.
References: src/…/multipart.js• L56-91 src/Formidable.js• L347-393 src/Formidable.js• L591-605
Field lifecycle
A field part is decoded as text and accumulated until its part ends. The form then emits a field event with the part name and value. Multipart fields keep repeated values as arrays in the parsed fields result, which preserves multiple controls with the same name.
Field data counts toward the field-size limit while it arrives. If the limit is exceeded, the form enters its error path and does not complete normally.
References: src/Formidable.js• L213-227 src/Formidable.js• L368-391 src/Formidable.js• L687-703
Upload lifecycle
A file part enters onPart, which is the handoff point for the form's upload policy. The default policy can reject the part through filter, create a file target, stream the part data into that target, and emit file only after the target is complete.
The form pauses request consumption while each file write completes. This keeps the incoming stream aligned with the file sink and prevents a fast request from outrunning storage. File parts are returned under their field name, with repeated uploads represented as arrays.
Applications can replace onPart when they need a different destination or inspection policy. The replacement must still account for the part stream and the form's completion and error behavior.
References: src/Formidable.js• L228-240 src/Formidable.js• L342-347 src/Formidable.js• L395-479
Transfer encoding
Multipart transfer encoding applies to an individual part. The plugin selects a data path after it reads the part's Content-Transfer-Encoding, so decoded bytes enter the same field or file lifecycle as ordinary part data.
Supported part encodings
The default part encoding is the form's configured text encoding. The handler passes binary, 7-bit, 8-bit, and UTF-8 data through as bytes. For a base64 part, it buffers only enough encoded characters to decode complete groups, then emits the decoded bytes. It emits the final partial group when the part ends.
This behavior keeps transfer decoding separate from field text decoding and file writes. Fields use a string decoder after transfer handling, while uploads receive the resulting byte buffers.
References: src/…/multipart.js• L63-65 src/…/multipart.js• L89-91 src/…/multipart.js• L96-151 src/Formidable.js• L368-387
Unsupported part encodings
An unknown Content-Transfer-Encoding is rejected with an unsupported-operation error. The form does not emit a normal completion event after this failure, and opened upload targets are cleaned up by the form error path.
References: src/…/multipart.js• L152-164 src/Formidable.js• L527-538 src/FormidableError.js• L13-18
Form lifecycle integration
Multipart parsing is one stage in the larger form lifecycle. Header selection creates the parser, parser events create part streams, and part completion produces the public fields and files result.
Parser handoff
When writeHeaders runs, enabled plugins inspect the request headers. The multipart plugin sets the form type and installs its boundary-aware parser. During body intake, the form writes each decoded request chunk to that parser and ends it when the request stream ends.
At headersEnd, the multipart plugin pauses the parser while it calls onPart. It resumes only after the part handler is ready. This makes part processing an ordered handoff rather than a second, independent read of the request body.
References: src/Formidable.js• L302-321 src/Formidable.js• L323-339 src/Formidable.js• L481-524 src/…/multipart.js• L95-165
Result and completion
The form collects each field event into fields and each file event into files. Multipart and URL-encoded forms use arrays for repeated field names, while repeated files always remain arrays. The final end event waits until the parser has ended and all active file writes have finished.
The same lifecycle also carries progress and error signals. Field and file limits can stop the request during streaming, and a request abort or parser error prevents normal completion.
References: src/Formidable.js• L213-240 src/Formidable.js• L334-339 src/Formidable.js• L441-477 src/Formidable.js• L723-729
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the accepted multipart media types and boundary error contract aligned with the plugin selection rules in src/Formidable.js.
- Preserve the parser event order when changing boundary handling, because
headersEndis the point where part backpressure and classification begin. - Keep
Content-Transfer-Encodingdecoding compatible with both field text decoding and file byte writes. - Keep the
fieldandfileevent shapes aligned with the array behavior used to build the finalfieldsandfilesresults. - Keep parser errors and file-write errors on the same form error path so normal
enddoes not follow a failed upload.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
Multipart Content-Type has no boundary | The form reports a bad request and does not install a usable multipart parser. | Send a valid boundary parameter with the multipart media type. |
| Boundary framing or header line is incomplete or invalid | The parser reports a malformed multipart request. | Preserve the required multipart boundaries, CRLF line endings, and header separator. |
Part uses an unknown Content-Transfer-Encoding | The form reports an unsupported transfer encoding and aborts normal completion. | Use a supported part encoding or handle the part with a custom policy before relying on default decoding. |
Field data exceeds maxFieldsSize | The form errors while the field is still streaming. | Set a suitable field limit and reject oversized input at the request boundary. |
| File data exceeds a file or total-file limit | The form errors and destroys opened file targets. | Set limits that match the upload contract and monitor the form error event. |
| Request ends before the final multipart boundary | The parser reports an unexpected stream end. | Ensure the sender completes the multipart body before closing the request. |
Key files
| File | Why to read it |
|---|---|
| src/…/multipart.js | Shows how multipart headers become part streams and how those streams enter the form handler. |
| src/…/Multipart.js | Defines the boundary and part event contract for streaming multipart bodies. |
| src/Formidable.js | Connects header selection, request streaming, field/file classification, limits, and final form results. |
| src/FormidableError.js | Defines the error categories used for missing boundaries, malformed bodies, and unsupported transfer encodings. |
| test/…/multipart.js | Provides compact multipart bodies that show field parts, file parts, boundaries, and malformed headers. |
| test/…/content-transfer-encoding.test.js | Demonstrates that an unsupported per-part transfer encoding fails the form rather than emitting normal completion. |