Skip to content

Instantly share code, notes, and snippets.

@coryasilva
Created February 2, 2024 15:04
Show Gist options
  • Save coryasilva/6f5f5636e71448e8c5539ed31e9b5e14 to your computer and use it in GitHub Desktop.
Save coryasilva/6f5f5636e71448e8c5539ed31e9b5e14 to your computer and use it in GitHub Desktop.
NodeJS Download File; streaming read; streaming write; async; got
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