Skip to content

Commit

Permalink
Add proxy support via HTTP_PROXY & HTTPS_PROXY env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
steprescott committed Oct 18, 2018
1 parent 2a7ed4e commit b20437f
Show file tree
Hide file tree
Showing 4 changed files with 1,344 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"dependencies": {
"@babel/polyfill": "^7.0.0",
"@octokit/rest": "^15.12.1",
"@types/http-proxy-agent": "git+ssh://git@github.com:steprescott/http-proxy-agent.git",
"@types/http-proxy-agent": "^2.0.1",
"chalk": "^2.3.0",
"commander": "^2.18.0",
"debug": "^4.0.1",
Expand Down
44 changes: 44 additions & 0 deletions source/api/_tests/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// import * as jest from 'jest';
import * as http from "http"
import * as node_fetch from "node-fetch"

import { api } from "../fetch"
import HttpProxyAgent from "http-proxy-agent"

interface ResponseMock {
body?: any
Expand Down Expand Up @@ -37,16 +39,45 @@ class TestServer {
}
}

class TestProxy {
isRunning = false
private port = 30002
private hostname = "localhost"
private router = (_req: any, res: any) => {
res.statusCode = 200
res.end(null)
}
private server = http.createServer(this.router)

start = async (): Promise<void> => {
return new Promise<void>((resolve, reject) => {
this.isRunning = true
this.server.listen(this.port, this.hostname, (err: any) => (err ? reject(err) : resolve()))
})
}
stop = async (): Promise<void> => {
return new Promise<void>((resolve, reject) => {
this.isRunning = false
this.server.close((err: any) => (err ? reject(err) : resolve()))
})
}
}

describe("fetch", () => {
let url: string
let server = new TestServer()
let proxy = new TestProxy()

beforeEach(() => {
url = "http://localhost:30001/"
})

afterEach(async () => {
await server.stop()

if (proxy.isRunning) {
await proxy.stop()
}
})

it("handles json success", async () => {
Expand Down Expand Up @@ -87,4 +118,17 @@ describe("fetch", () => {
expect(response.status).toBe(500)
expect(await response.text()).toBe(body)
})

it("sets proxy agent", async () => {
const proxyUrl = "http://localhost:30002/"
process.env["HTTP_PROXY"] = proxyUrl

await proxy.start()
await server.start({})

let options: node_fetch.RequestInit = { agent: undefined }
await api(url, options)
let agent = options.agent as HttpProxyAgent
expect(agent.proxy.href).toBe(proxyUrl)
})
})
12 changes: 12 additions & 0 deletions source/api/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { debug } from "../debug"
import * as node_fetch from "node-fetch"

import HttpProxyAgent from "http-proxy-agent"
import HttpsProxyAgent from "https-proxy-agent"

const d = debug("networking")
declare const global: any

Expand Down Expand Up @@ -57,6 +60,15 @@ export function api(

d(output.join(" "))
}

let agent = init.agent
const proxy = process.env["HTTPS_PROXY"] || process.env["HTTP_PROXY"]

if (!agent && proxy) {
let secure = url.toString().startsWith("https")
init.agent = secure ? new HttpsProxyAgent(proxy) : new HttpProxyAgent(proxy)
}

const originalFetch = node_fetch.default
return originalFetch(url, init).then(async (response: node_fetch.Response) => {
// Handle failing errors
Expand Down
Loading

0 comments on commit b20437f

Please sign in to comment.