Skip to content

Instantly share code, notes, and snippets.

@thewh1teagle
Created September 21, 2024 13:07
Show Gist options
  • Save thewh1teagle/3e3aee9fe55ed01eedf6d50481f92880 to your computer and use it in GitHub Desktop.
Save thewh1teagle/3e3aee9fe55ed01eedf6d50481f92880 to your computer and use it in GitHub Desktop.
Wget in nodejs
import {randomBytes} from "node:crypto";
import fs from 'fs'
function randomString(length = 10) {
return randomBytes(length).toString('hex');
}
interface wgetOpts {
out_path?: string | null
quiet?: boolean
existsOk?: boolean
}
export async function wget(url: string, options?: wgetOpts) {
var resp = await fetch(url)
// Get size
var size = resp.headers.get('content-length')
var out_path = randomString(10) as string | null
// Get filename
const contentDisposition = resp.headers.get('content-disposition')
if (contentDisposition) {
const filenamePattern = /^.*filename[^;=\n]*=(([\'"]).*?\2|[^;\n]*)[\n;]?$/
const matches = filenamePattern.exec(contentDisposition);
if (matches && matches[1]) {
var out_path = matches[1].replace("\"", "") as string | null; // Remove quotes if present
}
}
// Check response
if (!resp.ok) {
throw `Status Error(${resp.status}): ${await resp.text()}`
}
// Start stream download
const fileStream = fs.createWriteStream(out_path!)
var totalSize = size ? parseInt(size, 10) : null
var downloaded = 0
const reader = resp.body?.getReader()
while (true) {
const result = await reader?.read()
if (result?.done) {
break
}
fileStream.write(result?.value)
downloaded += result?.value?.length ?? 0
if (totalSize) {
const percentage = downloaded / totalSize * 100
console.log(`${percentage.toFixed(0)}%`)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment