import { fromFileUrl } from "../path/mod.ts";import { ensureFile } from "../fs/ensure_file.ts";
export async function downloadFile(url: string, fileUrl: URL) { const response = await fetch(url); if (!response.ok) { throw new Error(`Request failed with status ${response.status}`); } else if (!response.body) { throw new Error( `The requested download url ${url} doesn't contain an archive to download`, ); } else if (response.status === 404) { throw new Error( `The requested url "${url}" could not be found`, ); }
await ensureFile(fromFileUrl(fileUrl)); const file = await Deno.open(fileUrl, { truncate: true, write: true }); for await (const chunk of response.body) { Deno.writeSync(file.rid, chunk); } file.close();}