Skip to content

Commit

Permalink
fix(proxy): Fix proxy once and for all (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaledgarbaya committed Jun 29, 2017
1 parent 8dd94eb commit 04094a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/parseOptions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import fs from 'fs'
import { resolve } from 'path'
import { version } from '../package'
import { proxyStringToObject } from './utils/proxy'
import HttpsProxyAgent from 'https-proxy-agent'
import HttpProxyAgent from 'http-proxy-agent'
import { proxyStringToObject, agentFromProxy } from './utils/proxy'

export default function parseOptions (params) {
const defaultOptions = {
Expand Down Expand Up @@ -67,11 +65,8 @@ export default function parseOptions (params) {
options.proxy = proxyStringToObject(options.proxy)
}

if (options.proxy && options.proxy.isHttps) {
options.httpsAgent = new HttpsProxyAgent(options.proxy)
delete options.proxy
} else if (options.proxy && !options.proxy.isHttps) {
options.httpAgent = new HttpProxyAgent(options.proxy)
if (options.proxy) {
options.httpsAgent = agentFromProxy(options.proxy)
delete options.proxy
}

Expand Down
14 changes: 14 additions & 0 deletions lib/utils/proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parse, format } from 'url'
import { toInteger } from 'lodash'
import HttpsProxyAgent from 'https-proxy-agent'

function serializeAuth ({ username, password } = {}) {
if (!username) {
Expand Down Expand Up @@ -56,3 +57,16 @@ export function proxyObjectToString (proxyObject) {
// Ugly fix for Node 6 vs Node 8 behavior
return formatted.replace(/^\/\//, '')
}

export function agentFromProxy (proxy) {
if (!proxy) {
return {}
}
['http_proxy', 'https_proxy'].forEach((envStr) => {
delete process.env[envStr]
delete process.env[envStr.toUpperCase()]
})
const { host, port } = proxy
const agent = new HttpsProxyAgent({ host, port })
return { httpsAgent: agent }
}

0 comments on commit 04094a1

Please sign in to comment.