Skip to content

Instantly share code, notes, and snippets.

@thekarel
Created March 2, 2020 12:48
Show Gist options
  • Save thekarel/d66fd8df86e3bf72aa8df66a11ea6efd to your computer and use it in GitHub Desktop.
Save thekarel/d66fd8df86e3bf72aa8df66a11ea6efd to your computer and use it in GitHub Desktop.
fetchOnceAndSaveToDiskWithBuffer
async function fetchOnceAndSaveToDiskWithBuffer(url, filename) {
return new Promise(resolve => {
if (fs.existsSync(filename)) {
resolve(readFile(filename));
return;
}
const file = fs.createWriteStream(filename);
console.log(` * Downloading from: ${url}`);
https.get(url, (response) => {
const unzip = zlib.createGunzip();
response.pipe(unzip).pipe(file);
unzip.on('end', () => {
resolve(readFile(filename));
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment