use 'close' event on file stream for reliability#787
use 'close' event on file stream for reliability#787Trott wants to merge 1 commit intoexpressjs:masterfrom
Conversation
Change listened-for event on the output stream in storage/disk.js from 'finish' to 'close' to improve reliability. With 'finish', there is a race condition such that sometimes the callback runs before the file exists. Refs: expressjs#238
|
That's interesting. In the meantime this workaround is not entirely correct either, you might end up invoking the callback twice since cb = once(cb)
var finalPath = path.join(destination, filename)
var outStream = fs.createWriteStream(finalPath)
file.stream.pipe(outStream)
file.stream.on('error', err => {
outStream.destroy()
cb(err)
})
outStream.on('error', cb)
outStream.on('close', function () {
cb(null, {
destination: destination,
filename: filename,
path: finalPath,
size: outStream.bytesWritten
})
})Also are you aware that Furthermore this code assumes that the source stream has not already completed. If it has completed this will never call the callback. But I guess that's a minor. |
|
Yes, I think this might be a bug in node. Streams can emit |
@ronag I was using Node.js 12.11.1. |
|
Awesome and thank you! I'll close this. |
Change listened-for event on the output stream in storage/disk.js from
'finish' to 'close' to improve reliability. With 'finish', there is a
race condition such that sometimes the callback runs before the file
exists.
Refs: #238