Created
September 22, 2022 09:08
-
-
Save ppulwey/5cfb50726b3e3d8641b677d3e4967982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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