Skip to content

Commit

Permalink
Flush sync does fsync sync (#184)
Browse files Browse the repository at this point in the history
* Have flushSync do a fsyncSync

Signed-off-by: Matteo Collina <[email protected]>

* flushSync should do fsyncSync

Signed-off-by: Matteo Collina <[email protected]>

---------

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored Oct 9, 2023
1 parent 8e981d9 commit 7ea4cf0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,12 @@ function flushSync () {
sleep(BUSY_WRITE_TIMEOUT)
}
}

try {
fs.fsyncSync(this.fd)
} catch {
// Skip the error. The fd might not support fsync.
}
}

function flushBufferSync () {
Expand Down
8 changes: 7 additions & 1 deletion test/flush-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ test('retry in flushSync on EAGAIN', (t) => {
})

test('throw error in flushSync on EAGAIN', (t) => {
t.plan(11)
t.plan(12)

const fakeFs = Object.create(fs)
const SonicBoom = proxyquire('../', {
Expand Down Expand Up @@ -114,6 +114,12 @@ test('throw error in flushSync on EAGAIN', (t) => {
throw err
}

fakeFs.fsyncSync = function (...args) {
t.pass('fake fs.fsyncSync called')
fakeFs.fsyncSync = fs.fsyncSync
return fs.fsyncSync.apply(null, args)
}

t.ok(stream.write('hello world\n'))
t.throws(stream.flushSync.bind(stream), err, 'EAGAIN')

Expand Down

0 comments on commit 7ea4cf0

Please sign in to comment.