MCPcopy Create free account
hub / github.com/erpc/erpc / downloadFile

Function downloadFile

typescript/cli/src/install.ts:34–63  ·  view source on GitHub ↗

* Helper function to download a file from a URL

({ url, dest }: DownloadOptions)

Source from the content-addressed store, hash-verified

32 * Helper function to download a file from a URL
33 */
34async function downloadFile({ url, dest }: DownloadOptions): Promise<void> {
35 await mkdir(path.dirname(dest), { recursive: true });
36
37 return new Promise((resolve, reject) => {
38 const request = https.get(url, (response) => {
39 if (response.statusCode === 302 || response.statusCode === 301) {
40 // Handle redirects
41 downloadFile({ url: response.headers.location!, dest })
42 .then(resolve)
43 .catch(reject);
44 return;
45 }
46
47 if (!response.statusCode || response.statusCode !== 200) {
48 reject(
49 new DownloadError("Failed to download binary", response.statusCode),
50 );
51 return;
52 }
53
54 const fileStream = fs.createWriteStream(dest);
55
56 pipelineAsync(response, fileStream).then(resolve).catch(reject);
57 });
58
59 request.on("error", (error) => {
60 reject(new DownloadError(`Network error: ${error.message}`));
61 });
62 });
63}
64
65/**
66 * Trigger the erpc cli installation (downloading the right binary and checking it's checksum)

Callers 1

installFunction · 0.85

Calls 1

getMethod · 0.80

Tested by

no test coverage detected