In my Jest test, I incorporate the functionality from request-progress.
var fs = require('fs');
var request = require('request');
var progress = require('request-progress');
async function downloader() {
return new Promise((resolve, reject) => {
progress(request('https://az412801.vo.msecnd.net/vhd/VMBuild_20141027/VirtualBox /IE11/Windows/IE11.Win8.1.For.Windows.VirtualBox.zip'), {
// throttle: 2000, // Throttle the progress event to 2000ms, defaults to 1000ms
// delay: 1000, // Only start to emit after 1000ms delay, defaults to 0ms
// lengthHeader: 'x-transfer-length' // Length header to use, defaults to content-length
})
.on('progress', function (state) {
console.log('progress', state);
})
.on('error', function (err) {
// Handle error
})
.on('end', function () {
// Do something after request finishes
})
.pipe(fs.createWriteStream('IE11.Win8.1.For.Windows.VirtualBox.zip'));
});
}
After testing, it seems that there is a flush issue with the pipe not being properly flushed at the end of the transfer. This results in the downloaded file being slightly smaller than the correct filesize. A workaround I found was running additional functions after the download, which seemed to fix the size discrepancy.
I am exploring options to force a flush on the filestream within the 'end' event or consider other methods to address this issue. Adding a wait is one option, but it feels like a hacky solution that I'm not too fond of. Placing the resolve inside the 'end' event doesn't trigger the final flush, while excluding the resolve causes the function to never return. I'm curious if it's possible to implement a flush on the pipe itself.