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 / file-lifecycle-and-storage

File lifecycle and storage

The file lifecycle turns an uploaded byte stream into a file record that callers can use after parsing. The system records the file's source name and type, writes each byte to a selected destination, reports progress, finalizes the destination, and removes unfinished data when the request fails.

File representation

An uploaded file is represented by metadata plus a write target. The metadata keeps the original name and media type beside the generated name, destination path when one exists, byte count, modification time for disk-backed files, and an optional content hash. The parser returns files grouped by form field name after the write is complete.

The public file types are PersistentFile and VolatileFile. They share the same lifecycle actions and events so the upload flow does not need to know where the bytes are kept. See Persistent file storage and Volatile file handling for the two storage modes.

File informationPurpose
originalFilenameName supplied by the client
newFilenameName selected for the stored object
mimetypeMedia type supplied for the file part or request
filepathDestination path associated with the file record
sizeNumber of bytes accepted by the file writer
hashOptional digest of the accepted bytes

References: src/PersistentFile.js• L7-60 src/VolatileFile.js• L6-64 src/Formidable.js• L213-240 src/index.js• L1-23

Storage selection

The storage choice is made when the first file data is ready to be written. With the default settings, the system creates a disk-backed file below the configured upload directory. When a fileWriteStreamHandler is supplied, it creates a volatile file record and asks that handler for the destination stream instead. This lets an application send bytes to a service such as object storage without creating a local upload file.

The selected writer still receives the same byte sequence and must support the stream lifecycle expected by the file record. The configured upload directory and generated name remain part of the record even when the custom stream chooses a different physical destination.

flowchart LR
    A[Upload bytes] --> B{Custom write stream?}
    B -- No --> C[Disk-backed file]
    B -- Yes --> D[Application-provided stream]
    C --> E[File metadata]
    D --> E
    E --> F[Completed file event]

References: src/Formidable.js• L399-417 src/Formidable.js• L558-588 src/PersistentFile.js• L36-41 src/VolatileFile.js• L37-42 examples/store-files-on-s3.js• L15-41

Upload lifecycle

The upload flow creates one file record per accepted file part. It opens the writer before data arrives, pauses request processing while each write completes, and emits the completed file only after the part ends and the writer has flushed its data.

When the request contains multipart data, part headers identify the field name, original filename, and media type. A part without a media type is treated as a normal field rather than a file. Raw octet-stream requests create one file from the request itself and use the request filename header when present.

When the system processes a file, it follows this sequence:

  1. Select a generated name and safe destination record.
  2. Create the persistent or volatile file record.
  3. Emit fileBegin and open the write stream.
  4. Write incoming buffers while updating size, progress, and an optional hash.
  5. Check file size limits when the part ends.
  6. End the writer and emit file for the finished record.
  7. Emit the form-level end event after all file writers finish and no error exists.
sequenceDiagram
    participant Request
    participant Parser
    participant File as File record
    participant Store as Write destination
    participant Caller

    Request->>Parser: Upload bytes
    Parser->>File: Create metadata
    File->>Caller: fileBegin
    File->>Store: Open destination
    loop Incoming buffers
        Parser->>File: Write buffer
        File->>Store: Persist or forward buffer
        File->>Caller: progress
    end
    Parser->>File: End part
    File->>Store: Close destination
    File->>Caller: file
    Parser->>Caller: end when all files finish

References: src/Formidable.js• L347-479 src/Formidable.js• L723-729 src/…/multipart.js• L42-172 src/…/octetstream.js• L23-83

Completion and cleanup

Completion means that the file writer has ended successfully and the file record has been published through the file event. The form waits for all active file writes before it emits its final end event, so consumers can treat the returned file records as complete at parse completion.

An error stops normal completion and destroys every file that the form has opened. A persistent file also schedules removal of its path. A volatile file destroys its supplied stream; cleanup of a remote or external destination is the responsibility of the stream handler and its owning application.

ConditionFile actionForm result
Part and writer finishEnd the stream and publish fileContinue until all files finish
Request or parser errorDestroy opened writersEmit the parse error and skip normal completion
Invalid file sizeEnter error handling before normal file publicationReject the parse
Aborted requestDestroy opened writersEmit an aborted-request error

References: src/Formidable.js• L441-477 src/Formidable.js• L527-539 src/Formidable.js• L723-729 src/PersistentFile.js• L81-96 src/VolatileFile.js• L83-90

Storage child pages

The storage modes have different ownership rules after parsing. Read Persistent file storage for local disk retention and deletion. Read Volatile file handling for application-provided streams and external destinations. Read Multipart upload handling for how multipart parts become file records.

References: src/Formidable.js• L558-588 src/PersistentFile.js• L36-96 src/VolatileFile.js• L37-90 src/…/multipart.js• L54-165

Change checklist

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

  • Keep the metadata shape and event timing aligned between PersistentFile and VolatileFile so callers can use either storage mode.
  • Keep writer completion tied to file and form completion tied to all active writers. A change can expose a file before its bytes are fully written.
  • Keep error cleanup aligned with the destination owner. Local paths need removal while external streams may need provider-specific cancellation.
  • Keep filename and path selection consistent with the file writer. A custom writer can use a different destination while callers still inspect the generated file metadata.

Important failure modes

TriggerConsequenceGuard
File writer emits an errorParsing fails and all opened files are destroyedAttach the writer error to the form error path
Request ends before a valid multipart boundaryThe parser reports a malformed upload and normal completion does not occurSend a complete multipart body with its boundary
File exceeds a configured size limitThe form rejects the upload and cleans up opened filesSet limits that match the accepted upload policy
Custom stream stores data outside the local upload directoryLocal cleanup cannot remove the external objectMake the custom stream owner handle failed-upload cleanup

Key files

FileWhy to read it
src/Formidable.jsDefines file creation, write coordination, completion, limits, and error cleanup
src/PersistentFile.jsShows the disk-backed file contract and local cleanup behavior
src/VolatileFile.jsShows the custom-stream file contract and volatile cleanup behavior
src/…/multipart.jsConnects multipart part metadata and byte events to the file lifecycle
src/…/octetstream.jsShows the single-file raw-stream path
examples/store-files-on-s3.jsShows how an application can route file bytes to an external stream