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 information | Purpose |
|---|---|
originalFilename | Name supplied by the client |
newFilename | Name selected for the stored object |
mimetype | Media type supplied for the file part or request |
filepath | Destination path associated with the file record |
size | Number of bytes accepted by the file writer |
hash | Optional 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:
- Select a generated name and safe destination record.
- Create the persistent or volatile file record.
- Emit
fileBeginand open the write stream. - Write incoming buffers while updating size, progress, and an optional hash.
- Check file size limits when the part ends.
- End the writer and emit
filefor the finished record. - Emit the form-level
endevent 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.
| Condition | File action | Form result |
|---|---|---|
| Part and writer finish | End the stream and publish file | Continue until all files finish |
| Request or parser error | Destroy opened writers | Emit the parse error and skip normal completion |
| Invalid file size | Enter error handling before normal file publication | Reject the parse |
| Aborted request | Destroy opened writers | Emit 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
PersistentFileandVolatileFileso callers can use either storage mode. - Keep writer completion tied to
fileand 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
| Trigger | Consequence | Guard |
|---|---|---|
| File writer emits an error | Parsing fails and all opened files are destroyed | Attach the writer error to the form error path |
| Request ends before a valid multipart boundary | The parser reports a malformed upload and normal completion does not occur | Send a complete multipart body with its boundary |
| File exceeds a configured size limit | The form rejects the upload and cleans up opened files | Set limits that match the accepted upload policy |
| Custom stream stores data outside the local upload directory | Local cleanup cannot remove the external object | Make the custom stream owner handle failed-upload cleanup |
Key files
| File | Why to read it |
|---|---|
| src/Formidable.js | Defines file creation, write coordination, completion, limits, and error cleanup |
| src/PersistentFile.js | Shows the disk-backed file contract and local cleanup behavior |
| src/VolatileFile.js | Shows the custom-stream file contract and volatile cleanup behavior |
| src/…/multipart.js | Connects multipart part metadata and byte events to the file lifecycle |
| src/…/octetstream.js | Shows the single-file raw-stream path |
| examples/store-files-on-s3.js | Shows how an application can route file bytes to an external stream |