Content-type plugin system
Formidable uses a plugin layer to connect an HTTP request's Content-Type with the parser that can consume its body. The enabled plugins inspect the request headers, install the matching stream handler, and turn parser output into the fields and files returned by parse().
Request routing model
Header-based matching
The request headers provide the first routing signal. For a non-empty request, Formidable checks that a content type exists, then calls every enabled plugin; each built-in plugin claims only the media type that it understands. A matching plugin records a simple internal type and installs the parser for the request. This is composition rather than a single central switch, so the enabled plugin set defines the supported request encodings.
The default set contains handlers for raw octet streams, URL-encoded forms, multipart bodies, and JSON bodies. Applications can remove a handler when they do not want to accept that encoding. The order of the set can also affect composition, so changes to enabledPlugins should be deliberate.
The following flow shows how header checks lead to one request stream and one final form result.
flowchart LR
A[Request headers] --> B{Non-empty body?}
B -- No --> C[Empty-body handler]
B -- Yes --> D[Enabled plugins inspect Content-Type]
D --> E{Matching handler}
E -- None --> F[Unsupported media type error]
E -- Found --> G[Parser attached to form]
C --> H[Request bytes]
G --> H
H --> I[Fields and files]
I --> J[parse callback or promise]
References: src/Formidable.js• L302-321 src/Formidable.js• L482-525 src/Formidable.js• L33-47
Stream handoff
Plugin routing happens before request bytes reach the selected parser. Formidable first reads the headers and creates the handler. It then places a decompression stream in front of that handler when Content-Encoding is gzip, deflate, br, or compress; otherwise it passes the bytes through unchanged.
The selected parser receives each decoded chunk through the form's write path. The parser signals completion, and the form waits for any pending file work before it emits the final end event. This keeps content-type choice separate from transport compression and from final result delivery.
References: src/Formidable.js• L192-300 src/Formidable.js• L323-340 src/Formidable.js• L723-729
Built-in handler set
Multipart handling
The multipart plugin recognizes multipart media types and requires a boundary from the header. It connects that boundary to the multipart parser, which exposes each part to Formidable as a field or file stream. Multipart behavior has its own child page: Multipart upload handling.
References: src/…/multipart.js• L8-36 src/…/multipart.js• L42-54 src/…/multipart.js• L95-168
Structured body handling
The other built-in plugins adapt three body representations to the same form result. The URL-encoded handler emits field pairs, the JSON handler supplies an object of fields, and the octet-stream handler treats the raw body as a file upload. Their child page explains these handlers and their parser behavior in more detail: Structured request body parsing.
| Request representation | Formidable result | Built-in handler |
|---|---|---|
application/x-www-form-urlencoded | Field events and field values | querystring |
| JSON media type | Object fields | json |
application/octet-stream | One raw file stream | octetstream |
References: src/…/querystring.js• L5-17 src/…/querystring.js• L23-37 src/…/json.js• L5-18 src/…/json.js• L24-38 src/…/octetstream.js• L5-17 src/…/octetstream.js• L23-47
Parser replacement
Each matching plugin replaces the form's temporary empty-body handler with a parser that understands its encoding. The parser is an adapter boundary: it reads request bytes and emits parser-specific events, while the form converts those events into shared field, file, progress, error, and end behavior. This lets callers use the same parse() API across supported encodings.
The parser classes are also exported as public named exports. They are useful when an application needs parser-level access, but normal request processing should use the content-type plugins so that parser setup and form lifecycle state stay aligned.
References: src/Formidable.js• L214-240 src/Formidable.js• L499-524 src/…/index.js• L1-15
Custom plugin composition
Plugin contract
A plugin is a function that receives the Formidable instance and its options. Formidable binds the function to that instance, so the same object is available as the first argument and as this. A plugin can inspect headers, attach a parser, set the request type, add event handling, or leave the request for another enabled plugin.
Custom plugins can be added with form.use(plugin) after the form is created. They can also be supplied through options.enabledPlugins, which is useful when an application wants to define the complete handler set. Every function in that set runs during header parsing, and each function must avoid claiming media types that another handler should process.
References: src/Formidable.js• L131-160 src/Formidable.js• L482-524 README.md• L522-558
Composition boundaries
The plugin layer is the right place to add a new content-type route or to wrap an existing parser. A custom plugin must finish its header-time setup before the request stream starts. If it changes the parser or form type, it also changes which shared result rules apply downstream.
The built-in plugin functions are exported from the package entry point. A custom route can therefore compose with selected built-ins by importing the needed handlers and passing an explicit enabledPlugins list. The list must contain at least one function; an empty list is rejected during form creation.
References: src/index.js• L21-27 src/…/index.js• L1-6 src/Formidable.js• L33-47 src/Formidable.js• L131-145
Lifecycle signals
Completion and result delivery
The form emits a plugin event after each plugin finishes. Once the selected parser reaches the end of the request, Formidable collects field and file events and delivers one result through the callback or promise returned by parse(). A request with no body uses the empty-body handler and can complete without a content-type route.
Field aggregation depends on the selected type. Multipart and URL-encoded fields keep repeated names as arrays, while JSON fields retain the values produced by the JSON body. File events are collected separately from fields, which gives callers one stable result shape even when the input representation changes.
References: src/Formidable.js• L197-240 src/Formidable.js• L482-525 src/Formidable.js• L723-729
Failure signaling
Routing failures stop normal parsing. A missing content type produces a client error for a non-empty request, no matching parser produces an unsupported-media-type error, and a malformed multipart header produces a multipart boundary error. If a plugin throws while Formidable is building the route, parse() rejects with a pluginFailed error that identifies the plugin index.
References: src/Formidable.js• L302-320 src/Formidable.js• L488-517 src/…/multipart.js• L17-35 src/FormidableError.js• L1-18
Change checklist
When these files or sections are changed, remember to consider these:
- A new or changed content-type matcher must have a compatible parser and must be exported if applications are expected to enable it directly.
- A change to the selected form
typecan change repeated-field aggregation inparse(), even when the parser still emits field events. - A change to the default plugin list changes the media types accepted by every new form. An explicit
enabledPluginslist changes that contract again. - A custom plugin that attaches a parser must do so during header processing. Request data can arrive as soon as
parse()starts the stream. - Changes to plugin error codes or HTTP status values affect callers that distinguish unsupported media types, malformed requests, and plugin failures.
- Multipart matcher changes must stay aligned with boundary validation and the multipart child page.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A non-empty request has no Content-Type header | Formidable emits missingContentType with HTTP status 400 | Send a supported media type for every body that needs parsing |
| No enabled plugin matches the request content type | Formidable emits noParser with HTTP status 415 | Enable the built-in handler or add a custom handler for the media type |
enabledPlugins is empty | Form creation throws missingPlugin before parsing starts | Keep at least one plugin in the enabled set |
| A multipart content type has no boundary | Formidable emits missingMultipartBoundary with HTTP status 400 | Include a valid boundary parameter in the multipart header |
| A plugin throws during route setup | parse() rejects with pluginFailed and HTTP status 500 | Keep header-time plugin setup small and handle failures in the caller |
A value passed to use() is not a function | Form creation or plugin registration throws pluginFunction | Pass a callable plugin function |
Key files
| File | Why to read it |
|---|---|
| src/Formidable.js | Defines the form state, header routing, request stream handoff, plugin execution, and shared result lifecycle. |
| src/…/index.js | Shows the built-in plugin set that the package exposes. |
| src/…/multipart.js | Shows the multipart route and its boundary requirement before part handling begins. |
| src/…/querystring.js | Shows how URL-encoded input enters the shared field event path. |
| src/…/json.js | Shows how a JSON body becomes form fields. |
| src/…/octetstream.js | Shows how a raw body becomes a file stream. |
| src/…/index.js | Shows the parser boundary and the parser classes available to callers. |
| src/FormidableError.js | Defines the error codes used when routing or plugin setup fails. |