feat: add proxy support (set environment variable HTTPS_PROXY)

This commit is contained in:
calebboyd
2017-11-29 15:49:33 -06:00
parent 931af404ae
commit c1896e13a3
6 changed files with 46 additions and 24 deletions
+20 -19
View File
@@ -180,14 +180,15 @@ export class NexeCompiler<T extends NexeOptions = NexeOptions> {
}
private async _fetchPrebuiltBinaryAsync(target: NexeTarget) {
const downloadOptions: any = {
headers: {
'User-Agent': 'nexe (https://www.npmjs.com/package/nexe)'
}
}
let downloadOptions = this.options.downloadOptions
if (this.options.ghToken) {
downloadOptions.headers.Authorization = 'token ' + this.options.ghToken
downloadOptions = Object.assign({}, downloadOptions)
downloadOptions.headers = Object.assign({}, downloadOptions.headers, {
Authorization: 'token ' + this.options.ghToken
})
}
const githubRelease = await getLatestGitRelease(downloadOptions)
const assetName = target.toString()
const asset = githubRelease.assets.find(x => x.name === assetName)
@@ -196,19 +197,19 @@ export class NexeCompiler<T extends NexeOptions = NexeOptions> {
throw new Error(`${assetName} not available, create it using the --build flag`)
}
const filename = this.getNodeExecutableLocation(target)
// Remove the authorization, as the download is done via amazon S3 and does not need the token
delete downloadOptions.headers.Authorization
await download(asset.browser_download_url, dirname(filename), downloadOptions).on(
'response',
(res: IncomingMessage) => {
const total = +res.headers['content-length']!
let current = 0
res.on('data', data => {
current += data.length
this.compileStep.modify(`Downloading...${(current / total * 100).toFixed()}%`)
})
}
)
await download(
asset.browser_download_url,
dirname(filename),
this.options.downloadOptions
).on('response', (res: IncomingMessage) => {
const total = +res.headers['content-length']!
let current = 0
res.on('data', data => {
current += data.length
this.compileStep.modify(`Downloading...${(current / total * 100).toFixed()}%`)
})
})
return createReadStream(filename)
}
+12 -1
View File
@@ -10,6 +10,7 @@ import artifacts from './steps/artifacts'
import patches from './patches'
import { rimrafAsync } from './util'
import { NexeTarget } from './target'
import { EOL } from 'os'
async function compile(
compilerOptions?: Partial<NexeOptions>,
@@ -39,11 +40,21 @@ async function compile(
? void nexe(compiler).then(
() => callback && callback(null),
(e: Error) => {
if (compiler.options.loglevel !== 'silent') {
process.stderr.write(EOL + e.stack + EOL)
}
compiler.quit()
if (callback) callback(e)
else throw e
}
)
: nexe(compiler)
: nexe(compiler).catch((e: Error) => {
if (compiler.options.loglevel !== 'silent') {
process.stderr.write(EOL + e.stack + EOL)
}
compiler.quit()
throw e
})
}
export { argv, compile, version, NexeCompiler, NexeOptions, help }
+11
View File
@@ -6,6 +6,8 @@ import { getTarget, NexeTarget } from './target'
import { EOL, homedir } from 'os'
import * as c from 'chalk'
const caw = require('caw')
export const version = '{{replace:0}}'
export interface NexePatch {
@@ -227,6 +229,15 @@ function normalizeOptions(input?: Partial<NexeOptions>): NexeOptions {
options.make = flatten(options.vcBuild, options.make)
options.configure = flatten(options.configure)
options.resources = flatten(opts.resource, options.resources)
options.downloadOptions = options.downloadOptions || {}
options.downloadOptions.headers = options.downloadOptions.headers || {}
options.downloadOptions.headers['User-Agent'] = 'nexe (https://www.npmjs.com/package/nexe)'
options.downloadOptions.agent = process.env.HTTPS_PROXY
? caw(process.env.HTTPS_PROXY, { protocol: 'https' })
: options.downloadOptions.agent || require('https').globalAgent
options.downloadOptions.rejectUnauthorized = process.env.HTTPS_PROXY ? false : true
options.rc = options.rc || extractCliMap(/^rc-.*/, options)
options.output =
(options.targets[0] as NexeTarget).platform === 'windows'