Skip to main content

@chiba/wget@1.0.0
Built and signed on GitHub Actions

wget. file download.

This package works with Deno
This package works with Deno
JSR Score
100%
Published
2 months ago (1.0.0)
import * as fs from "jsr:@std/fs@^1.0.0"; /** * Options */ export interface Options { /** write documents to FILE */ outputDocument?: string; /** Proxy, Certificate, etc */ client?: Deno.CreateHttpClientOptions; } /** * Result */ export interface Result { response?: Response; outputDocument?: string; } /** * wget * * @param url * @param options * @returns result */ export async function wget( url: string, options?: Options, ): Promise<Result> { const init = options?.client ? { client: Deno.createHttpClient(options.client) } : undefined; const response = await fetch(url, init); let outputDocument = options?.outputDocument?.trim(); if (!outputDocument) { // response.headers.get("Content-Disposition") // TODO const array = new URL(url).pathname.split("/"); outputDocument = array[array.length - 1].trim(); if (!outputDocument) { outputDocument = "index.html"; } } const writable = getWritable(outputDocument); await response.body?.pipeTo(writable); return { response, outputDocument }; } function getWritable(outputDocument: string) { if (outputDocument === "-") { return Deno.stdout.writable; } else { const original = outputDocument; for (let i = 1; i < Number.MAX_SAFE_INTEGER; i++) { if (!fs.existsSync(outputDocument)) { break; } outputDocument = `${original}.${i}`; } return Deno.createSync(outputDocument).writable; } }