Persistent file storage
Persistent file storage is the default upload path for Formidable. It turns each accepted file part into a file record with a generated or chosen name, writes the bytes to a destination, and reports the file only after the write is complete. The shared upload lifecycle is described in File lifecycle and storage; Volatile file handling covers the non-disk stream path, and Multipart upload handling covers how multipart parts enter this flow.
Storage model
Disk-backed file records
The PersistentFile in home/…/PersistentFile.js represents an upload whose bytes are written to the host file system. It keeps the public file metadata with the destination path and tracks size, last-write time, progress, and an optional content hash. The parser exposes this record through fileBegin before writing and file after finalization.
References: src/PersistentFile.js• L7-34 src/PersistentFile.js• L43-60 src/Formidable.js• L399-416
Upload destinations
Formidable resolves uploadDir to an absolute base directory. The default is the operating system temporary directory. A normal upload uses this base directory plus the selected newFilename, so the original client name is metadata and is not the storage path by default.
References: src/Formidable.js• L33-53 src/Formidable.js• L103-108 src/Formidable.js• L640-655
Path and filename selection
Generated names
When no filename function is supplied, Formidable creates a unique identifier for the stored name. keepExtensions can add the source file's filtered extension, including a composed extension such as .tar.gz; otherwise the generated name has no source extension.
References: src/Formidable.js• L25-31 src/Formidable.js• L607-637 src/Formidable.js• L657-684
Custom names
The filename option controls newFilename. Formidable passes the source name, the selected extension, the part metadata, and the form to this function. The returned string is joined to uploadDir, then checked so that a name cannot escape the upload directory. An unsafe result uses defaultInvalidName instead of writing outside the base.
References: src/Formidable.js• L48-52 src/Formidable.js• L640-671
Upload directories
A custom name can contain directory separators when folder uploads are intended. With createDirsFromUploads enabled, Formidable creates the parent directories before it creates the persistent file. Without that option, the configured directory must already exist for the file stream to open successfully.
References: src/Formidable.js• L558-588 src/PersistentFile.js• L36-41
Write stream lifecycle
Disk write streams
Each persistent file opens a Node file write stream at filepath. Incoming data is passed to that stream in chunks. Formidable pauses request input until each write callback runs, which limits the amount of upload data that can wait in memory. Each completed chunk updates the file size and emits file progress.
References: src/PersistentFile.js• L36-41 src/PersistentFile.js• L63-79 src/Formidable.js• L418-439
Custom write streams
fileWriteStreamHandler replaces the host file-system stream with an application-supplied writable stream. Formidable still creates the normal file record and metadata, but the record uses the handler's stream and the actual destination is controlled by the application. This supports object stores and private storage through a stream boundary; the default disk write is not also performed. See Volatile file handling for the shared non-disk record behavior.
The handler receives the file record when it opens and must return a writable stream. The S3 example in home/…/store-files-on-s3.js returns a PassThrough connected to an object-store upload. The upload caller must still use Formidable's completion result as the point at which all supplied stream writes have finished.
References: src/Formidable.js• L558-567 src/VolatileFile.js• L6-25 src/VolatileFile.js• L37-41 examples/store-files-on-s3.js• L15-29 test/…/file-write-stream-handler-option.test.js• L40-60
Upload write flow
The following flow shows how a file part becomes a stored file record and how completion moves back to the form result.
flowchart TD
A[Accepted file part] --> B[Choose newFilename]
B --> C[Resolve safe filepath]
C --> D{Custom stream handler?}
D -- No --> E[Open disk write stream]
D -- Yes --> F[Open supplied writable stream]
E --> G[Write chunks and update metadata]
F --> G
G --> H[Finalize stream and hash]
H --> I[Emit file]
I --> J[Allow form end when no writes remain]
References: src/Formidable.js• L395-477 src/Formidable.js• L558-588 src/…/octetstream.js• L32-80 src/PersistentFile.js• L63-89
Hash and metadata
Content hashes
hashAlgorithm enables an incremental crypto hash for each file. Formidable updates the hash with every data chunk and converts it to a hexadecimal digest when the stream ends. With the option disabled, the file record has no calculated hash. The digest is available in the file's serialized data after completion.
References: src/PersistentFile.js• L26-33 src/PersistentFile.js• L63-66 src/PersistentFile.js• L81-89 src/PersistentFile.js• L43-56
File metadata
The completed record describes both the client-facing identity and the stored result. Its key values are the original filename, generated or custom filename, MIME type, resolved path for disk storage, byte size, modification time, and optional hash. toJSON() is the stable shape for passing this information to application code.
References: src/PersistentFile.js• L17-24 src/PersistentFile.js• L43-56
Completion and cleanup
File completion
Formidable emits file only after the file stream's end callback runs. Multipart uploads pause around writes and use the same file finalization path. The octet-stream path also waits for all outstanding chunk writes before it ends the file, so a fast parser end cannot expose a partially written file.
References: src/Formidable.js• L435-477 src/…/octetstream.js• L48-80 src/PersistentFile.js• L81-89
Form completion
The form emits end only when the request parser has ended and no file flush remains. A caller can therefore use the parse callback, promise, or form end event as the upload lifecycle boundary. A stream or parser error takes the error path instead and prevents normal completion.
References: src/Formidable.js• L192-240 src/Formidable.js• L527-537 src/Formidable.js• L723-729
Failed-upload cleanup
When Formidable records an upload error, it destroys every opened file. A persistent file also schedules removal of its path after its write stream is destroyed. Applications that use a custom stream must give that stream useful error behavior and must manage cleanup for storage outside the local file system.
References: src/Formidable.js• L527-537 src/PersistentFile.js• L91-97 src/VolatileFile.js• L44-46
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the filename callback contract aligned with extension filtering and the path-safety check. A callback that changes its returned path shape can affect directory creation and storage location together.
- Keep custom stream completion tied to the writable stream's end callback. Returning a stream that reports completion too early can expose incomplete remote objects through the
fileor form completion events. - Keep hash updates and digest finalization on the same write and end paths for both disk and custom streams. Otherwise file metadata can report a digest for only part of the upload.
- Keep error cleanup aligned with the storage target. Local files are unlinked by the persistent record, while custom destinations need an application-level delete or abort action.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
uploadDir or a generated parent directory does not exist | The disk stream can fail to open and the form enters its error path. | Create the directory before parsing or enable createDirsFromUploads for callback-generated subdirectories. |
filename returns a path outside uploadDir | Formidable replaces it with defaultInvalidName rather than using the unsafe path. | Return a relative storage name and treat the resolved path as the enforced boundary. |
| A custom writable stream emits an error | The file error reaches the form and normal completion is stopped. | Attach storage-provider error handling and make the provider abort or remove partial data. |
| A file violates empty, minimum, maximum, or total-size rules | The form reports an upload error and destroys opened file streams. | Set limits for the storage policy and handle the parse error before using file records. |
A caller uses file before stream finalization | It can observe incomplete size, hash, or stored content. | Use file, parse completion, or the form end event as the completion boundary. |
Key files
| File | Why to read it |
|---|---|
| src/Formidable.js | Shows how names and paths are selected, how file records are created, and how upload completion and errors are coordinated. |
| src/PersistentFile.js | Defines the disk-backed file record, its write stream, hash state, metadata, and cleanup behavior. |
| src/VolatileFile.js | Shows the record used when an application supplies a custom writable stream instead of local disk storage. |
| src/…/multipart.js | Shows how multipart file parts enter the common file-writing lifecycle. |
| src/…/octetstream.js | Shows the completion barrier for raw octet-stream uploads and outstanding writes. |
| examples/store-files-on-s3.js | Illustrates the custom stream boundary for a remote object store. |
| test/…/file-write-stream-handler-option.test.js | Confirms that a custom stream receives the bytes and that the default upload directory stays unused. |
| test/…/store-files-option.test.js | Demonstrates the configured upload directory and custom file target in a complete request. |