UNPKG

3.04 kBJavaScriptView Raw
1'use strict'
2
3let hush = require('./console').hush
4
5function findProxy (urlParsed) {
6 let httpProxy = process.env.HTTP_PROXY || process.env.http_proxy
7 let httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy
8
9 if (urlParsed.protocol === 'https:') {
10 return httpsProxy || httpProxy
11 } else {
12 return httpProxy
13 }
14}
15
16function findTunnel (urlParsed) {
17 let tunnel = require('tunnel-agent')
18
19 if (urlParsed.protocol === 'https:') {
20 return tunnel.httpsOverHttp
21 } else {
22 return tunnel.httpOverHttp
23 }
24}
25
26function agent (urlParsed, proxyParsed, certs) {
27 let tunnelMethod = findTunnel(urlParsed)
28 let opts = {
29 proxy: {
30 host: proxyParsed.hostname,
31 port: proxyParsed.port || '8080'
32 }
33 }
34
35 if (proxyParsed.auth) {
36 opts.proxy.proxyAuth = proxyParsed.auth
37 }
38
39 if (certs.length > 0) {
40 opts.ca = certs
41 }
42
43 let tunnelAgent = tunnelMethod(opts)
44 if (urlParsed.protocol === 'https:') {
45 tunnelAgent.defaultPort = 443
46 }
47 return tunnelAgent
48}
49
50function sslCertFile () {
51 return process.env.SSL_CERT_FILE ? [process.env.SSL_CERT_FILE] : []
52}
53
54function sslCertDir () {
55 let certDir = process.env.SSL_CERT_DIR
56 if (certDir) {
57 const fs = require('fs')
58 const path = require('path')
59 return fs.readdirSync(certDir).map(f => path.join(certDir, f))
60 } else {
61 return []
62 }
63}
64
65function getCerts () {
66 let filenames = sslCertFile().concat(sslCertDir())
67
68 if (filenames.length > 0) {
69 hush('Adding the following trusted certificate authorities')
70 }
71
72 return filenames.map(function (filename) {
73 const fs = require('fs')
74 hush(' ' + filename)
75 return fs.readFileSync(filename)
76 })
77}
78
79function addToOpts (url, opts) {
80 const urlLib = require('url')
81
82 let urlParsed = urlLib.parse(url)
83 let proxy = findProxy(urlParsed)
84
85 let certs = getCerts()
86
87 if (proxy) {
88 let proxyParsed = urlLib.parse(proxy)
89 opts = Object.assign({}, opts, {agent: agent(urlParsed, proxyParsed, certs)})
90 }
91
92 if (certs.length > 0) {
93 opts = Object.assign({}, opts, {ca: certs})
94 }
95
96 return opts
97}
98
99let loadErrors
100
101function got (url, opts) {
102 const gotDelegate = require('got')
103 loadErrors()
104 return gotDelegate(url, addToOpts(url, opts))
105}
106
107got.stream = function (url, opts) {
108 const gotDelegate = require('got')
109 loadErrors()
110 return gotDelegate.stream(url, addToOpts(url, opts))
111}
112
113const helpers = [
114 'get',
115 'post',
116 'put',
117 'patch',
118 'head',
119 'delete'
120]
121
122helpers.forEach(el => {
123 got[el] = (url, opts) => got(url, Object.assign({}, opts, {method: el}))
124})
125
126helpers.forEach(el => {
127 got.stream[el] = function (url, opts) {
128 return got.stream(url, Object.assign({}, opts, {method: el}))
129 }
130})
131
132loadErrors = () => {
133 const gotDelegate = require('got')
134
135 const errors = [
136 'CacheError',
137 'CancelError',
138 'GotError',
139 'UnsupportedProtocolError',
140 'HTTPError',
141 'MaxRedirectsError',
142 'ParseError',
143 'ReadError',
144 'RequestError'
145 ]
146
147 errors.forEach(el => {
148 got[el] = gotDelegate[el]
149 })
150}
151
152module.exports = got