forked from orta/danger-plugin-yarn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: default + scoped registries with proxy + auth support; support …
…for different renders (orta#17)
- Loading branch information
Showing
11 changed files
with
5,820 additions
and
1,619 deletions.
There are no files selected for viewing
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,12 +1,9 @@ | ||
{ | ||
"prettier.singleQuote": false, | ||
"prettier.trailingComma": "es5", | ||
"prettier.semi": false, | ||
"prettier.printWidth": 120, | ||
"files.exclude": { | ||
"**/.git": true, | ||
"**/dist": true, | ||
"**/node_modules": true | ||
}, | ||
"editor.formatOnSave": false | ||
"files.exclude": { | ||
"**/.git": true, | ||
"**/dist": true, | ||
"**/node_modules": true | ||
}, | ||
"editor.formatOnSave": true, | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Agent } from "http" | ||
import HttpsProxyAgent from "https-proxy-agent" | ||
import { URL } from "url" | ||
|
||
import { YarnConfig } from "./getYarnConfig" | ||
|
||
function formatHostname(hostname: string): string { | ||
// canonicalize the hostname, so that 'oogle.com' won't match 'google.com' | ||
return hostname.replace(/^\.*/, ".").toLowerCase() | ||
} | ||
|
||
function parseNoProxyZone(zone: string): Readonly<{ hostname: string; port: string; hasPort: boolean }> { | ||
zone = zone.trim().toLowerCase() | ||
|
||
const zoneParts = zone.split(":", 2) | ||
const zoneHost = formatHostname(zoneParts[0]) | ||
const zonePort = zoneParts[1] | ||
const hasPort = zone.indexOf(":") > -1 | ||
|
||
return { | ||
hostname: zoneHost, | ||
port: zonePort, | ||
hasPort, | ||
} | ||
} | ||
|
||
function uriInNoProxy(uri: URL, noProxy: string): boolean { | ||
const port = uri.port || (uri.protocol === "https:" ? "443" : "80") | ||
const hostname = formatHostname(uri.hostname) | ||
const noProxyList = noProxy.split(",") | ||
|
||
// iterate through the noProxyList until it finds a match. | ||
return noProxyList.map(parseNoProxyZone).some(noProxyZone => { | ||
const isMatchedAt = hostname.indexOf(noProxyZone.hostname) | ||
const hostnameMatched = isMatchedAt > -1 && isMatchedAt === hostname.length - noProxyZone.hostname.length | ||
|
||
if (noProxyZone.hasPort) { | ||
return port === noProxyZone.port && hostnameMatched | ||
} | ||
|
||
return hostnameMatched | ||
}) | ||
} | ||
|
||
function getProxyFromUri(uri: URL, config: YarnConfig): string | undefined { | ||
// Decide the proper request proxy to use based on the request URI object and the | ||
// environmental variables (NO_PROXY, HTTP_PROXY, etc.) | ||
// Respect NO_PROXY environment variables. | ||
// See: http://lynx.isc.org/current/breakout/lynx_help/keystrokes/environments.html | ||
const noProxy = process.env.NO_PROXY || process.env.no_proxy | ||
|
||
// If the noProxy is a wildcard then return undefined | ||
if (noProxy === "*") { | ||
return undefined | ||
} | ||
|
||
// If the noProxy is not empty and the uri is found return undefined | ||
if (noProxy && uriInNoProxy(uri, noProxy)) { | ||
return undefined | ||
} | ||
|
||
// Check for HTTP or HTTPS proxy in environment else default to undefined | ||
if (uri.protocol === "http:") { | ||
return process.env.HTTP_PROXY || process.env.http_proxy || config["http-proxy"] || config.proxy || undefined | ||
} | ||
|
||
if (uri.protocol === "https:") { | ||
return ( | ||
process.env.HTTPS_PROXY || | ||
process.env.https_proxy || | ||
config["https-proxy"] || | ||
process.env.HTTP_PROXY || | ||
process.env.http_proxy || | ||
config["http-proxy"] || | ||
config.proxy || | ||
undefined | ||
) | ||
} | ||
|
||
// If none of that works, return undefined | ||
// (What uri protocol are you using then?) | ||
return undefined | ||
} | ||
|
||
export function getProxyAgentFromUri(uri: URL, config: YarnConfig): Agent | undefined { | ||
const proxy = getProxyFromUri(uri, config) | ||
|
||
if (proxy) { | ||
return new HttpsProxyAgent(proxy) as Agent | ||
} | ||
|
||
return undefined | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { YarnConfig } from "./getYarnConfig" | ||
|
||
export type Registry = Readonly<{ | ||
url: string | ||
authToken?: string | ||
}> | ||
|
||
export interface Registries { | ||
readonly default: Registry | ||
[scopeName: string]: Registry | ||
} | ||
|
||
export function getRegistries(config: YarnConfig): Registries { | ||
const registries = { | ||
default: { | ||
url: config.registry, | ||
authToken: config._auth, | ||
}, | ||
} | ||
|
||
for (const [key, value] of Object.entries(config)) { | ||
if (key.endsWith(":registry")) { | ||
const [scopeName] = key.split(":registry") | ||
const url = value | ||
const authToken = config[`${url}:_auth`] | ||
registries[scopeName] = { | ||
url, | ||
authToken, | ||
} | ||
} | ||
} | ||
|
||
return registries | ||
} |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as child_process from "child_process" | ||
import { promisify } from "util" | ||
|
||
const exec = promisify(child_process.exec) | ||
|
||
export interface YarnConfig { | ||
registry: string | ||
_auth?: string | ||
"https-proxy"?: string | ||
"http-proxy"?: string | ||
proxy?: string | ||
} | ||
|
||
export interface YarnConfigListMessage { | ||
type: "info" | "inspect" | ||
data: object | ||
} | ||
|
||
export async function getYarnConfig(): Promise<YarnConfig> { | ||
const defaultConfig = { | ||
registry: "https://registry.npmjs.org/", | ||
} | ||
|
||
const { stdout } = await exec("yarn config list --json") | ||
|
||
if (stdout) { | ||
const jsonLines: YarnConfigListMessage[] = stdout.split("\n").map(line => JSON.parse(line.trim())) | ||
// The json lines are ordered from yarn to npm, but we wish to produce | ||
// a config in which npm is overridden with yarn config. | ||
return jsonLines.reduceRight((config, jsonLine) => { | ||
if (jsonLine.type === "inspect") { | ||
return { ...config, ...jsonLine.data } | ||
} | ||
|
||
return config | ||
}, defaultConfig) | ||
} | ||
|
||
return defaultConfig | ||
} |
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
Oops, something went wrong.