Skip to content

Instantly share code, notes, and snippets.

@ppulwey
Created September 22, 2022 09:08
Show Gist options
  • Save ppulwey/5cfb50726b3e3d8641b677d3e4967982 to your computer and use it in GitHub Desktop.
Save ppulwey/5cfb50726b3e3d8641b677d3e4967982 to your computer and use it in GitHub Desktop.
import * as fs from 'node:fs';
import * as https from 'node:https';
const download = async (url: string, targetPath: string): Promise<void> => {
return new Promise<void>((resolve, reject) => {
if (!url.startsWith('https')) {
const err = new Error('URL must start with https');
reject(err);
}
https.get(url, res => {
const writeStream = fs.createWriteStream(targetPath);
res.pipe(writeStream);
writeStream.on('finish', () => {
writeStream.close();
resolve();
});
});
});
};
export default download;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment