(url: string, targetPath: string)
| 77 | }); |
| 78 | |
| 79 | async function downloadFile(url: string, targetPath: string): Promise<string> { |
| 80 | const u = new URL(url); |
| 81 | const c = new Client({ urlPrefix: u.origin, auth: false }); |
| 82 | |
| 83 | const writeStream = fs.createWriteStream(targetPath); |
| 84 | |
| 85 | const res = await c.request<void, NodeJS.ReadableStream>({ |
| 86 | method: "GET", |
| 87 | path: u.pathname, |
| 88 | queryParams: u.searchParams, |
| 89 | responseType: "stream", |
| 90 | resolveOnHTTPError: true, |
| 91 | }); |
| 92 | |
| 93 | if (res.status !== 200) { |
| 94 | throw new Error( |
| 95 | `Download failed, file "${url}" does not exist. status ${ |
| 96 | res.status |
| 97 | }: ${await res.response.text()}`, |
| 98 | { |
| 99 | cause: new Error( |
| 100 | `Object returned by getDownloadDetails() from src${path.sep}emulator${path.sep}downloadableEmulators.ts contains invalid URL: ${url}`, |
| 101 | ), |
| 102 | }, |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | return new Promise<string>((resolve, reject) => { |
| 107 | writeStream.on("finish", () => { |
| 108 | resolve(targetPath); |
| 109 | }); |
| 110 | writeStream.on("error", (err) => { |
| 111 | reject(err); |
| 112 | }); |
| 113 | res.body.pipe(writeStream); |
| 114 | }); |
| 115 | } |
no test coverage detected
searching dependent graphs…