add snarf.ts

This commit is contained in:
Atlas Cove 2022-11-14 16:45:09 +00:00
parent 447a54e516
commit 13d67c363e
1 changed files with 14 additions and 0 deletions

14
snarf.ts Normal file
View File

@ -0,0 +1,14 @@
import { readerFromStreamReader } from "https://deno.land/std/io/mod.ts";
import { basename, dirname } from "https://deno.land/std/path/mod.ts";
export async function snarf(url: Request | string, init?: RequestInit, fn?: string): Promise<void> {
var outf= fn ?? basename(typeof url === "string" ? url : url.url)
res= await fetch(url, init);
sr= res.body?.getReader();
if(sr !== null && sr !== void 0) {
var r=readerFromStreamReader(sr);
var f=Deno.open(outf, {create:true,write:true})
await Deno.copy(r,f)
f.close()
}
}