Skip to content

Commit

Permalink
feat: read proxy settings from npmrc files, close RNG-56
Browse files Browse the repository at this point in the history
  • Loading branch information
notdryft committed Dec 6, 2024
1 parent c8b7b8d commit 32a1384
Show file tree
Hide file tree
Showing 5 changed files with 760 additions and 241 deletions.
109 changes: 10 additions & 99 deletions js-simulation/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion js/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
"dependencies": {
"@jspm/core": "2.1.0",
"archiver": "7.0.1",
"axios": "1.7.7",
"commander": "12.1.0",
"decompress": "4.2.1",
"esbuild": "0.24.0",
"esbuild-plugin-tsc": "0.4.0",
"import-meta-resolve": "4.1.0",
"make-fetch-happen": "14.0.3",
"readline-sync": "1.4.10"
},
"devDependencies": {
"@types/archiver": "6.0.3",
"@types/decompress": "4.2.7",
"@types/make-fetch-happen": "10.0.4",
"@types/node": "18.19.67",
"@types/readline-sync": "1.4.8",
"prettier": "3.4.1",
Expand Down
26 changes: 22 additions & 4 deletions js/cli/src/dependencies/download.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import axios from "axios";
import fetch, { MakeFetchHappenOptions, TlsOptions } from "make-fetch-happen";
import fs from "fs";
import stream from "stream";
import util from "util";
import stream from "stream";

const pipeline = util.promisify(stream.pipeline);

const NPM_CONFIG_PROXY_KEY = "npm_config_proxy";
const NPM_CONFIG_HTTPS_PROXY_KEY = "npm_config_https_proxy";
const NPM_CONFIG_NOPROXY_KEY = "npm_config_noproxy";

const fetchOptionsFromNpmConfiguration = (): MakeFetchHappenOptions & TlsOptions => {
const proxy = process.env[NPM_CONFIG_HTTPS_PROXY_KEY] || process.env[NPM_CONFIG_PROXY_KEY] || undefined;
const noProxy = process.env[NPM_CONFIG_NOPROXY_KEY] || undefined;
return {
proxy,
noProxy
};
};

export const downloadFile = async (url: string, targetFile: string): Promise<void> => {
const request = await axios.get(url, { responseType: "stream" });
await pipeline(request.data, fs.createWriteStream(targetFile));
const options = fetchOptionsFromNpmConfiguration();
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.status} ${response.statusText}`);
}

await pipeline(response.body, fs.createWriteStream(targetFile));
};
Loading

0 comments on commit 32a1384

Please sign in to comment.