I only recently noticed that the following code is not doing what it was doing earlier… actually writing the file to the disk.
const download = async (archive) => {
const d = await new Promise((resolve) => {
const opts = { method: 'GET' };
const req = https.request(url, opts, (res) => {
const file = fs.createWriteStream(`path/to/${archive}`);
res.on('data', (chunk) => {
file.write(chunk);
//process.stdout.write(chunk);
})
.on('end', () => file.end());
resolve(archive);
});
req.on('error', (error) => console.error(error));
req.end();
});
}
the file is getting created on disk, but is only 0B. If I uncomment process.stdout.write(chunk), it happily spits out gobbledegook of the binary file to the console. But nothing is actually written to the disk.
Where am I goofing up? or has something changed in node recently that is silently screwing up my code? Very happy for any guidance.
update: I spoke to soon. See comment below
update2: this helped me. The full zip file is downloading and unzipping properly now
thanks to the comment from @jfriend00, the following edit fixed the problem
I could have sworn the original code was working fine and then stopped working, but I am glad I didn't swear.
update:
The edits above do not help. While now the file does get downloaded, it is not downloaded properly. It is a zip file, but when I try to unzip it, I get an error that
But when I download the file directly, it unzips just fine. So my code is not downloading the file properly.