Skip to content

Commit

Permalink
fix: proxy config from npm electron-userland#585
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Sep 11, 2016
1 parent dd61408 commit fe8b98f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/develar.xml

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

4 changes: 3 additions & 1 deletion nsis-auto-updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"dependencies": {
"bluebird": "^3.4.6",
"fs-extra-p": "^1.1.8",
"semver": "^5.3.0"
"semver": "^5.3.0",
"ini": "^1.3.4",
"tunnel-agent": "^0.4.3"
},
"bundledDependencies": [
"fs-extra-p",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"fs-extra-p": "^1.1.8",
"hosted-git-info": "^2.1.5",
"image-size": "^0.5.0",
"ini": "^1.3.4",
"isbinaryfile": "^3.0.1",
"lodash.template": "^4.4.0",
"mime": "^1.3.4",
Expand Down Expand Up @@ -122,7 +123,7 @@
"json8": "^0.9.2",
"path-sort": "^0.1.0",
"pre-git": "^3.10.0",
"ts-babel": "^1.0.7",
"ts-babel": "^1.0.9",
"tslint": "^3.15.1",
"typescript": "^2.0.2",
"whitespace": "^2.1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/util/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { copy, Stats } from "fs-extra-p"
import { Minimatch } from "minimatch"
import * as path from "path"
import { Promise as BluebirdPromise } from "bluebird"
const readInstalled = require("read-installed")

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")
const readInstalled = require("read-installed")

// we use relative path to avoid canonical path issue - e.g. /tmp vs /private/tmp
export function copyFiltered(src: string, destination: string, filter: Filter, dereference: boolean): Promise<any> {
Expand Down
60 changes: 48 additions & 12 deletions src/util/httpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Socket } from "net"
import { IncomingMessage, ClientRequest } from "http"
import { IncomingMessage, ClientRequest, Agent } from "http"
import * as https from "https"
import { createWriteStream, ensureDir } from "fs-extra-p"
import { createWriteStream, ensureDir, readFile } from "fs-extra-p"
import { parse as parseUrl } from "url"
import { Promise as BluebirdPromise } from "bluebird"
import * as path from "path"
import { createHash } from "crypto"
import { Transform } from "stream"
import { homedir } from "os"

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")

const maxRedirects = 10

Expand All @@ -22,7 +26,10 @@ function _download(url: string, destination: string, options: DownloadOptions |
callback = <any>options
options = null
}
doDownload(url, destination, 0, options || {}, callback)

createAgent("https")
.then(it => doDownload(url, destination, 0, options || {}, it, callback))
.catch(callback)
}

export function addTimeOutHandler(request: ClientRequest, callback: (error: Error) => void) {
Expand All @@ -34,7 +41,7 @@ export function addTimeOutHandler(request: ClientRequest, callback: (error: Erro
})
}

function doDownload(url: string, destination: string, redirectCount: number, options: DownloadOptions, callback: (error: Error | null) => void) {
function doDownload(url: string, destination: string, redirectCount: number, options: DownloadOptions, agent: Agent, callback: (error: Error | null) => void) {
const ensureDirPromise = options.skipDirCreation ? BluebirdPromise.resolve() : ensureDir(path.dirname(destination))

const parsedUrl = parseUrl(url)
Expand All @@ -45,7 +52,7 @@ function doDownload(url: string, destination: string, redirectCount: number, opt
headers: {
"User-Agent": "electron-builder"
},
agent: createAgent("https"),
agent: agent,
}, (response: IncomingMessage) => {
if (response.statusCode >= 400) {
callback(new Error(`Cannot download "${url}", status ${response.statusCode}: ${response.statusMessage}`))
Expand All @@ -55,7 +62,7 @@ function doDownload(url: string, destination: string, redirectCount: number, opt
const redirectUrl = response.headers.location
if (redirectUrl != null) {
if (redirectCount < maxRedirects) {
doDownload(redirectUrl, destination, redirectCount++, options, callback)
doDownload(redirectUrl, destination, redirectCount++, options, agent, callback)
}
else {
callback(new Error("Too many redirects (> " + maxRedirects + ")"))
Expand Down Expand Up @@ -124,15 +131,44 @@ class DigestTransform extends Transform {
}
}

function createAgent(uriProtocol: string) {
const proxyString: string = process.env.HTTPS_PROXY ||
process.env.https_proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy
async function proxyFromNpm() {
let data = ""
try {
data = await readFile(path.join(homedir(), ".npmrc"), "utf-8")
}
catch (ignored) {
return null
}

if (!proxyString) {
if (!data) {
return null
}

try {
const config = require("ini").parse(data)
return config["https-proxy"] || config["http-proxy"] || config.proxy
}
catch (e) {
// used in nsis auto-updater, do not use .util.warn here
console.warn(e)
return null
}
}

async function createAgent(uriProtocol: string) {
let proxyString: string =
process.env.npm_config_https_proxy ||
process.env.HTTPS_PROXY || process.env.https_proxy ||
process.env.HTTP_PROXY || process.env.http_proxy ||
process.env.npm_config_http_proxy ||
process.env.npm_config_proxy

if (!proxyString) {
proxyString = await proxyFromNpm()
if (!proxyString) {
return null
}
}

const proxy = parseUrl(proxyString)

Expand Down

0 comments on commit fe8b98f

Please sign in to comment.