1 var crypto = require('crypto')
2 var HttpAgent = require('http').Agent
3 var HttpsAgent = require('https').Agent
5 var pkg = require('../package.json')
7 module.exports = initialize
9 function initialize (uri, method, accept, headers) {
10 if (!this.config.sessionToken) {
11 this.config.sessionToken = crypto.randomBytes(8).toString('hex')
12 this.log.verbose('request id', this.config.sessionToken)
19 localAddress: this.config.proxy.localAddress,
20 strictSSL: this.config.ssl.strict,
21 cert: this.config.ssl.certificate,
22 key: this.config.ssl.key,
23 ca: this.config.ssl.ca,
24 agent: getAgent.call(this, uri.protocol)
27 // allow explicit disabling of proxy in environment via CLI
29 // how false gets here is the CLI's problem (it's gross)
30 if (this.config.proxy.http === false) {
33 // request will not pay attention to the NOPROXY environment variable if a
34 // config value named proxy is passed in, even if it's set to null.
36 if (uri.protocol === 'https:') {
37 proxy = this.config.proxy.https
39 proxy = this.config.proxy.http
41 if (typeof proxy === 'string') opts.proxy = proxy
44 headers.version = this.version || pkg.version
45 headers.accept = accept
47 if (this.refer) headers.referer = this.refer
49 headers['npm-session'] = this.config.sessionToken
50 headers['user-agent'] = this.config.userAgent
55 function getAgent (protocol) {
56 if (protocol === 'https:') {
57 if (!this.httpsAgent) {
58 this.httpsAgent = new HttpsAgent({
60 maxSockets: this.config.maxSockets,
61 localAddress: this.config.proxy.localAddress,
62 rejectUnauthorized: this.config.ssl.strict,
63 ca: this.config.ssl.ca,
64 cert: this.config.ssl.certificate,
65 key: this.config.ssl.key
69 return this.httpsAgent
71 if (!this.httpAgent) {
72 this.httpAgent = new HttpAgent({
74 maxSockets: this.config.maxSockets,
75 localAddress: this.config.proxy.localAddress