deno.land / std@0.166.0 / _util / download_file.ts

download_file.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { fromFileUrl } from "../path/mod.ts";import { ensureFile } from "../fs/ensure_file.ts";
/** Download the file at the given url to the given path. */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 }); await response.body.pipeTo(file.writable);}
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago