I am using axios in Node JS to automatically download some video clip files.
To do so, I simply pipe the ReadStream from the axios response into a WriteStream.
This is my approach :
const writer = fs.createWriteStream(destination);
axios({
url : url
method : 'GET',
responseType : 'stream'
})
.then((response) => {
response.data.pipe(writer);
const size = Number(response.headers['content-length']);
let progress = 0;
writer.on('finish', () => {
console.log("WRITE STREAM FINISH");
// DO SOMETHING HERE WITH THE DOWNLOADED FILE
});
writer.on('error', (error) => {
console.error(error);
});
// READ STREAM
response.data.on('data', (data) => {
progress += Buffer.byteLength(data);
process.stdout.write("\r DOWNLOAD : " + Math.floor((progress / size * 100)) + "%");
});
response.data.on('end', () => {
console.log("READ STREAM END");
});
response.data.on('close', () => {
console.log("READ STREAM CLOSE");
});
response.data.on('error', (error) => {
console.error("READ STREAM ERROR", error);
});
})
.catch((error) => {
console.error("AXIOS ERROR", error);
});
Problem
Most of the time it works perfectly, all events are emitted as expected, and the console output would be something like this :
DOWNLOAD : 100%
READ STREAM END
READ STREAM CLOSE
WRITE STREAM FINISH
But on a very random basis, it happens that the ReadStream is closed before it ends, as a result the finish event from the WriteStream is never emitted. On retrying a couple of seconds later, it works again.
DOWNLOAD : 77%
READ STREAM CLOSE
I checked the downloaded file after this happend. Looks like the download did not finish ( file size too low). Surprisingly enough, there are no errors raised .
Questions
- What could cause the
ReadStreamfrom axios to close before it ends ? - Could there be a problem at the requested server / file source ?
- Any suggestions how I could troubleshoot this further ?