-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read proxy settings from npmrc files, close RNG-56
- Loading branch information
Showing
5 changed files
with
760 additions
and
241 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; |
Oops, something went wrong.