feat(disk-storage): support opt-in flush for durable uploads (#1381)#1391
Open
SAY-5 wants to merge 1 commit intoexpressjs:mainfrom
Open
feat(disk-storage): support opt-in flush for durable uploads (#1381)#1391SAY-5 wants to merge 1 commit intoexpressjs:mainfrom
SAY-5 wants to merge 1 commit intoexpressjs:mainfrom
Conversation
…js#1381) Disk storage currently pipes the uploaded file into a vanilla `fs.createWriteStream` and reports success as soon as the stream finishes. On Linux, and any OS with a write-back page cache, this means the bytes can still be sitting in kernel buffers when the request handler returns — a power loss or crash at the wrong moment leaves the file either truncated or zero-length, but the HTTP response already claimed success. Node.js 20.10 / 21.0 added a `flush` option to `fs.createWriteStream` that calls `fdatasync()` on the underlying file descriptor before the stream is closed. This commit exposes it as an opt-in DiskStorage option: multer.diskStorage({ destination: dir, flush: true }) Callers who need durability can turn it on; everything else keeps the current, faster buffered-write behavior so there's no perf regression for existing users. On runtimes older than Node 20.10 the option is silently ignored by `createWriteStream`, which matches the documented upstream behavior. Added two regression tests in `test/disk-storage.js`: 1. With `flush: true`, `createWriteStream` receives `{ flush: true }` and the upload still succeeds. 2. Without `flush`, `createWriteStream` is still called with no options so the default behavior is unchanged. Full `npm test` passes: 74/74.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1381.
Why
As reported in #1381, the disk storage pipes the uploaded file into a vanilla `fs.createWriteStream` and reports the upload as successful the moment the stream finishes. On Linux (and any OS with a write-back page cache) the bytes can still be sitting in kernel buffers when the request handler returns — a power loss or crash at the wrong moment leaves the file truncated or zero-length, but the HTTP response already claimed success.
Node.js 20.10 / 21.0 added a `flush` option to `fs.createWriteStream` that calls `fdatasync()` on the file descriptor before the stream closes, which is exactly what this workflow needs.
Change
Expose `flush` as an opt-in `DiskStorage` option:
```js
multer.diskStorage({
destination: './uploads',
flush: true
})
```
Tests
Added two regression tests in `test/disk-storage.js`:
Full `npm test` suite: 74/74 passing.