Volatile file handling
Volatile file handling represents an upload without making Formidable responsible for a file on the host file system. It sends each upload chunk to a writable stream chosen by the caller. This lets an application keep data in memory or send it to another store while still receiving the normal file metadata and upload events.
Storage boundary
Volatile upload representation
VolatileFile is the upload record used when fileWriteStreamHandler is set. The record describes the upload and tracks its progress, but the handler owns the destination. The destination can be a memory buffer, a cloud upload stream, or another writable target. It is therefore not correct to treat file.filepath as proof that a local file exists.
This mode changes the storage boundary rather than the upload contract. Formidable still applies file filters and size limits, emits fileBegin before data flows, and emits file after the destination stream ends. The parent page, File lifecycle and storage, describes how this boundary fits with the other file paths.
References: src/Formidable.js• L395-417 src/Formidable.js• L558-588 src/VolatileFile.js• L6-34
Persistent storage contrast
The default path uses PersistentFile and writes to the configured upload directory. Volatile handling takes precedence when a write-stream handler exists, so the upload directory is not the destination for that file. Use Persistent file storage when the application needs a Formidable-owned local temporary file that it can move later.
References: src/Formidable.js• L558-588 src/PersistentFile.js• L36-41 README.md• L317-352
File metadata
Upload identity
The volatile record keeps the identity supplied by the client and the name selected for the upload. originalFilename and mimetype describe the incoming part. newFilename is the generated or caller-selected name used to identify the upload. The record also carries a logical filepath value for compatibility with the common file shape, but the custom stream decides where bytes go.
References: src/VolatileFile.js• L7-27 src/Formidable.js• L401-409 src/Formidable.js• L657-683
Upload progress and digest
The record starts with a size of zero and increases the size after each successful stream write. It emits progress with the current byte count. When hashing is enabled, it updates the digest for each chunk and replaces the in-progress hash with its hexadecimal digest when the upload ends.
The JSON form is suited to logs or responses. It includes the size and identifying fields. It includes the hash when one is configured, but it does not expose the local path used by persistent files.
References: src/VolatileFile.js• L27-34 src/VolatileFile.js• L48-59 src/VolatileFile.js• L66-90 README.md• L645-649
Upload lifecycle
Stream destination
The write-stream handler receives the volatile file record and must return a writable stream. Formidable writes upload chunks to that stream in sequence. The handler can connect the stream to an external store or collect the chunks for later use. The upload remains stream-based unless the handler deliberately collects all data.
The following flow shows the relationship between the request parser, the volatile record, and the caller-owned destination.
flowchart LR
A[Incoming upload] --> B[Formidable creates VolatileFile]
B --> C[fileBegin event]
B --> D[Caller write-stream handler]
D --> E[Writable destination]
A --> F[Upload chunks]
F --> D
D --> G[Stream end]
G --> H[file event with metadata]
References: src/Formidable.js• L404-417 src/Formidable.js• L418-477 src/VolatileFile.js• L37-42 README.md• L341-350
Completion and failure cleanup
Formidable waits for file writes before it reports the file as complete. On completion, the volatile record ends the writable stream and then emits its end event. The form emits the file result after that step and can then finish the request when no other work remains.
If the request or stream fails, Formidable destroys every open file destination. Destroying a volatile file destroys the writable stream only. The application or the external storage client must define any remote cleanup needed after a partial write.
References: src/Formidable.js• L435-477 src/Formidable.js• L527-539 src/Formidable.js• L83-90 src/VolatileFile.js• L44-46
Usage choices
External stream storage
Use volatile handling when the upload should flow directly to a destination outside the default local directory. The repository example connects the returned writable stream to an S3 upload. This avoids a host-file-system staging step, while the external client remains responsible for the final storage result and its errors.
References: examples/store-files-on-s3.js• L15-29 examples/store-files-on-s3.js• L31-41 README.md• L341-350
In-memory buffering
Use a custom writable that collects chunks only when the expected upload size is safe for the process memory limit. The buffer example warns that this choice removes the main benefit of streaming and can cause memory overflow. File size and total file size limits still protect the parsing flow, but they do not make an unbounded memory design safe.
References: examples/forceBuffer.js• L1 examples/forceBuffer.js• L11-30 src/Formidable.js• L418-438
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the
fileWriteStreamHandlerbranch aligned with the file lifecycle events. A change in when the stream opens or ends can alter the meaning offileBeginandfilefor every storage target. - Preserve the shared metadata shape when changing
VolatileFile. Callers can use the same parsed-file result for local files and custom destinations. - Review error cleanup with each new destination type. Stream destruction is local to the returned writable and does not automatically undo a remote or application-managed write.
- Keep upload size limits in view when adding buffering handlers. A handler that stores chunks in memory changes the resource cost without changing the parser contract.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The write-stream handler returns a destination that does not support the writable stream contract. | Chunk writes or stream completion fail and the form reports an error. | Return a working writable stream and handle its error event. |
| A request or destination stream fails after bytes reach an external store. | Formidable destroys the open stream, but a partial remote object can remain. | Make the destination cleanup-aware and remove or mark incomplete remote data. |
| A handler collects every chunk in memory for a large upload. | Process memory can grow until the request fails or the process becomes unstable. | Prefer a streaming destination and set suitable file and total-size limits. |
The caller assumes file.filepath names a local file in volatile mode. | Reads or local file operations target a path that Formidable did not write. | Treat the handler's destination as the source of file bytes. |
Key files
| File | Why to read it |
|---|---|
| src/VolatileFile.js | Defines the metadata, write, hash, completion, and destroy behavior of the volatile upload record. |
| src/Formidable.js | Shows when Formidable selects volatile handling and how it connects the record to the request lifecycle. |
| src/PersistentFile.js | Provides the local-storage counterpart needed to understand the storage boundary. |
| examples/store-files-on-s3.js | Shows the direct-stream use case for an external storage service. |
| examples/forceBuffer.js | Shows the in-memory use case and its memory risk. |
| test/…/volatile-file.test.js | Summarizes the guarantees for metadata, stream writes, completion, errors, and destruction. |