Created
February 2, 2024 15:04
-
-
Save coryasilva/6f5f5636e71448e8c5539ed31e9b5e14 to your computer and use it in GitHub Desktop.
NodeJS Download File; streaming read; streaming write; async; got
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 https from 'node:https' | |
import { join } from 'node:path' | |
import { createWriteStream } from 'node:fs' | |
import { pipeline } from 'node:stream/promises' | |
import got from 'got' | |
/* Config */ | |
// Enable keep alive for better performance between calls. | |
https.globalAgent = new https.Agent({ keepAlive: true }) | |
const outDir = './out' | |
/** | |
* Downloads an file using streams | |
* @param {string} url | |
* @param {string} filename | |
*/ | |
async function downloadFile(url, filename) { | |
const downloadStream = got.stream(url) | |
const writeStream = createWriteStream(join(outDir, filename)) | |
downloadStream.on('downloadProgress', ({ transferred, total, percent }) => { | |
const percentage = Math.round(percent * 100) | |
console.log(`downloadImage: Progress: ${transferred}/${total} (${percentage}%)`) | |
}) | |
try { | |
await pipeline(downloadStream, writeStream) | |
console.log(`downloadImage: Info: File downloaded to ${filename}`) | |
} catch (error) { | |
console.log(`downloadImage: Error: ${error.message}`) | |
throw new Error(`downloadImage: Error: Failed to download ${url} and write to ${filename}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment