Callback is called when destination file is still open if source stream emits error
sekogan opened this issue · 0 comments
sekogan commented
Consider the following code:
const fs = require('fs')
const stream = require('stream')
const pump = require('pump')
fs.mkdirSync('foo')
const source = new stream.Readable({
read() { this.emit('error', new Error('some error')) }
})
const dest = fs.createWriteStream('foo/dest')
pump(source, dest, err => {
if (err) {
fs.unlinkSync('foo/dest')
fs.rmdirSync('foo') // Throws ENOTEMPTY
}
})
Looks like the callback is called when the destination file is still open, which prevents the directory "foo" from being deleted.
As a workaround I can move the cleanup code a bit:
const fs = require('fs')
const stream = require('stream')
const pump = require('pump')
fs.mkdirSync('foo')
const source = new stream.Readable({
read() { this.emit('error', new Error('some error')) }
})
const dest = fs.createWriteStream('foo/dest')
dest.on('close', () => {
fs.unlinkSync('foo/dest')
fs.rmdirSync('foo') // OK
})
pump(source, dest, () => {})
This works but the code gets really messy.
Any suggestions would be highly appreciated!
Tested on Windows 10 with node 9.1.0 and 9.5.0.