Im using ytdl-core to get the direct urls for youtube videos (the long urls that start with https://r1-- etc...), and using https.get to download them. ~70% of the time when res.end is called, it returns an empty buffer instead of the data for the video.
this is the code that im currently using to handle the http request:
function largeHttpGet(url, callback, progress) {
try{
var _current = 0;
var _buffer = [];
var _req = https.get(url, function (res) {
var _total = parseInt(res.headers['content-length'], 10);
res.on('data', function (chunk) {
_buffer.push(chunk);
_current = parseFloat(_current + chunk.length);
progress(_current / _total * 100);
})
res.on('end', function () {
callback(Buffer.concat(_buffer));
//this returns an empty buffer the majority of the time
})
})
}
catch(error){
console.log("[HTTP]: " + error);
}
}
and this is what i use to get the youtube video's info:
getVidInfo(link, function(info){
back.send("return", {"key": key, "data": info} );
})
This error only occurs with the youtube video links, all other requests work consistently.