Introducing a new way to review your PRs.

Read the announcement

node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.

View on GitHub

13 documents · Updated Nov 9, 2026

docs / parser-and-stream-utilities

Parser and stream utilities

Parser and stream utilities turn request bodies into a common set of field and file events. They also provide the small boundary helpers that keep completion callbacks safe, keep asynchronous work in order, and convert common form field shapes into values that application code can use.

Parser family

The parser family accepts body data through Node.js streams and emits either structured records or raw bytes. Content-type plugins select the parser that matches the request, while the main request flow forwards decoded body data into it and waits for parser completion.

ParserRequest data it representsOutput role
JSONParserJSON bodyEmits the parsed object as fields.
QueryStringParserURL-encoded form bodyEmits one { key, value } record for each field.
MultipartParserMultipart bodyEmits part and boundary events for field and file handling.
OctetStreamParserRaw binary bodyPasses bytes through for one uploaded file.
DummyParserEmpty or unsupported parser stateEnds the form flow when there is no body to parse.

The public parser exports come from the fully qualified path src/…/index.js and are re-exported by src/index.js. The export layer also keeps the historical OctetstreamParser and QuerystringParser spellings available beside the preferred names. StreamingQuerystring.js describes a chunk-aware query-string parser, but it is not part of the active request path.

References: src/…/index.js• L1-15 src/index.js• L25-26 src/…/StreamingQuerystring.js• L1-10

Parser selection

Parser selection is part of request setup rather than a choice made by application code for each incoming chunk. The form coordinator checks the request headers, lets enabled content-type plugins claim the body, and reports an unsupported-media error when no parser claims it.

When a plugin selects a parser, it sets the request type and connects parser output to the form’s field or file events. The request stream then follows one input path: optional content decoding, parser writes, parser end, and final form completion.

This diagram shows how request bytes become normalized form output.

flowchart LR
  A[Request headers and body] --> B[Content decoding]
  B --> C{Content type}
  C -->|JSON| D[JSON parser]
  C -->|URL encoded| E[Query-string parser]
  C -->|Multipart| F[Multipart parser]
  C -->|Octet stream| G[Pass-through parser]
  C -->|Empty body| H[Dummy parser]
  D --> I[Field events]
  E --> I
  F --> J[Field and file events]
  G --> J
  H --> K[Form end]
  I --> K
  J --> K

References: src/Formidable.js• L193-299 src/Formidable.js• L302-340 src/Formidable.js• L481-525 src/…/json.js• L14-38 src/…/querystring.js• L14-39 src/…/multipart.js• L17-36 src/…/octetstream.js• L14-17

Parser output

Parser output uses events so the form coordinator can apply one field and file contract across several body formats. JSON produces a complete field object, URL-encoded data produces field pairs, multipart data produces part events, and raw binary data remains a byte stream for file storage.

The coordinator keeps repeated URL-encoded and multipart field names as arrays. JSON and raw binary requests use direct field values or file output instead. This difference is the reason field-shape helpers must know the selected request type.

References: src/…/JSON.js• L5-27 src/…/Querystring.js• L5-30 src/…/OctetStream.js• L1-10 src/…/json.js• L25-35 src/…/querystring.js• L24-35 src/Formidable.js• L213-239

Stream coordination

Stream coordination protects the request lifecycle from timing and backpressure problems. The shared helpers make callback completion stable across synchronous and asynchronous code, while parser plugins pause input when downstream work must finish before more data is handled.

Completion guards

once creates a wrapper that runs a callback only on its first call and returns the first result on later calls. Form parsing uses it around the final callback so an error and an end event cannot complete the same parse operation twice.

dezalgo keeps a callback from running synchronously when it is invoked during the current call stack. It defers an early call to a microtask but calls later invocations directly. Together, the two helpers give callers one completion event with a predictable timing boundary.

References: src/utils.js• L1-26 src/utils.js• L28-52 src/Formidable.js• L192-214

Backpressure coordination

Backpressure means slowing input while a downstream operation is still busy. Multipart handling pauses the parser while application part handling runs, and file handling pauses the request while a file write is pending. The octet-stream path also tracks outstanding writes before it emits the completed file.

This coordination matters because parser end is not always the same as durable output. The form ends only after the parser has ended, all pending file work has flushed, and no error has stopped the request.

References: src/…/multipart.js• L95-165 src/…/octetstream.js• L46-81 src/Formidable.js• L418-477 src/Formidable.js• L723-729

Sequential asynchronous work

The request flow serializes directory creation needed by file output. Each new directory task waits for the previous task before it starts, while its own promise still reports success or failure to the caller. This avoids overlapping file-system setup for related uploads.

References: src/Formidable.js• L59-82 src/Formidable.js• L558-588

Field normalization helpers

Field normalization helpers adapt parser output to common HTML form expectations without changing the original parsed object. They are application-facing helpers: parser events establish the raw shape, then callers choose whether to collapse repeated values or interpret checkbox fields.

First-value selection

firstValues returns fields in their existing form for JSON and other non-repeating request types. For URL-encoded and multipart forms, it selects the first value for each field unless the field name appears in the exception list. Exceptions preserve arrays for controls such as multi-select inputs.

Use this helper when an application wants the pre-3.0 single-value field shape. Apply it before boolean conversion when checkbox names can be absent or repeated.

References: src/…/firstValues.js• L1-19 src/Formidable.js• L217-226 README.md• L732-749

Boolean interpretation

readBooleans maps a named set of expected checkbox fields to booleans. A field is true when its value is the HTML checkbox marker on or the boolean true; an absent or other value becomes false. The helper copies the field map before it changes the named keys.

This behavior reflects HTML form input: an unchecked checkbox sends no field at all. Callers should first reduce repeated values with firstValues or an equivalent step so each named checkbox has one value.

References: src/…/readBooleans.js• L1-10 README.md• L751-770

Public utility boundaries

The package exposes parser classes from the root entry point and exposes the field helpers through their dedicated helper paths. This split keeps parser construction available for integrations while keeping field-shape conversion as an explicit step after parsing.

The parser export names include compatibility aliases. The build also creates separate helper and parser entry points, so changes to a public name or to an entry path can affect both import users and CommonJS consumers.

References: src/index.js• L10-27 src/…/index.js• L7-15 package.json• L21-36 tool/rollup.config.js• L28-103

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep parser names, compatibility aliases, root exports, and the matching build entry points aligned.
  • Preserve the field-shape contract between URL-encoded or multipart parser events and firstValues exceptions.
  • Preserve the ordering contract between parser end, pending file writes, and the form-level end callback.
  • Keep callback guards compatible with both callback-based and promise-based parse callers.
  • Keep readBooleans input assumptions aligned with the value shape produced by field normalization.

Important failure modes

TriggerConsequenceGuard
No content type or no enabled plugin claims the requestParsing stops with a client or unsupported-media error.Validate request headers and keep the required content-type plugin enabled.
Multipart content has no boundaryThe parser cannot identify parts and the request fails.Send a valid multipart content type with its boundary parameter.
Parser input ends before a valid multipart boundary closesThe parser reports malformed multipart data.Ensure the client sends a complete multipart body.
File writes remain pending when the parser endsFile completion and form completion must wait; early completion would expose incomplete output.Keep pause/resume and outstanding-write tracking tied to write callbacks.
Checkbox fields are passed to readBooleans while still arraysArray values do not equal on or true, so expected checked fields can become false.Run firstValues first or otherwise reduce each checkbox to one value.
A completion callback is invoked from more than one lifecycle pathCallback consumers can receive duplicate completion.Wrap the final callback with dezalgo and once.

Key files

FileWhy to read it
src/Formidable.jsShows how request bytes, plugins, parser completion, field shape, and file flushing join into one lifecycle.
src/…/index.jsDefines the public parser class names and compatibility aliases.
src/…/JSON.jsIllustrates whole-body JSON parsing into one structured output.
src/…/Querystring.jsIllustrates URL-encoded field-pair output.
src/…/Multipart.jsDefines the multipart event stream consumed by field and file handling.
src/…/OctetStream.jsDefines the raw byte pass-through used for octet-stream uploads.
src/…/index.jsLists the content-type plugins that connect request types to parsers.
src/utils.jsProvides callback timing and single-completion safeguards.
src/…/firstValues.jsDefines repeated-field reduction and multi-value exceptions.
src/…/readBooleans.jsDefines HTML checkbox value conversion.
docs/content-type-plugin-system.mdExplains how content-type plugins choose and connect parser behavior.
docs/form-request-lifecycle.mdExplains the wider request lifecycle that consumes these parser and stream contracts.