-
Notifications
You must be signed in to change notification settings - Fork 5
/
agents.js
201 lines (165 loc) · 5.79 KB
/
agents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict'
const http = require('http')
const https = require('https')
const net = require('net')
const tls = require('tls')
const { once } = require('events')
const { createTimeout, abortRace, urlify, appendPort, cacheAgent } = require('./util')
const { normalizeOptions, cacheOptions } = require('./options')
const { getProxy, getProxyType, isSecureProxy, proxyCache } = require('./proxy.js')
const Errors = require('./errors.js')
const createAgent = (base, name) => {
const SECURE = base === https
const SOCKET_TYPE = SECURE ? tls : net
const agent = class extends base.Agent {
#options
#timeouts
#proxy
#socket
constructor (_options) {
const { timeouts, proxy, noProxy, ...options } = normalizeOptions(_options)
super(options)
this.#options = options
this.#timeouts = timeouts
this.#proxy = proxy ? { proxies: getProxyType(proxy), proxy: urlify(proxy), noProxy } : null
}
get proxy () {
return this.#proxy ? { url: this.#proxy.proxy } : {}
}
#getProxy (options) {
const proxy = this.#proxy
? getProxy(appendPort(`${options.protocol}//${options.host}`, options.port), this.#proxy)
: null
if (!proxy) {
return
}
const secure = isSecureProxy(proxy)
return cacheAgent({
key: cacheOptions({
...options,
...this.#options,
secure,
timeouts: this.#timeouts,
proxy,
}),
cache: proxyCache,
secure,
proxies: this.#proxy.proxies,
}, proxy, this.#options)
}
#setKeepAlive (socket) {
socket.setKeepAlive(this.keepAlive, this.keepAliveMsecs)
socket.setNoDelay(this.keepAlive)
}
#setIdleTimeout (socket, options) {
if (this.#timeouts.idle) {
socket.setTimeout(this.#timeouts.idle, () => {
socket.destroy(new Errors.IdleTimeoutError(options))
})
}
}
async #proxyConnect (proxy, request, options) {
// socks-proxy-agent accepts a dns lookup function
options.lookup ??= this.#options.lookup
// all the proxy agents use this secureEndpoint option to determine
// if the proxy should connect over tls or not. we can set it based
// on if the HttpAgent or HttpsAgent is used.
options.secureEndpoint = SECURE
const socket = await abortRace([
(ac) => createTimeout(this.#timeouts.connection, ac).catch(() => {
throw new Errors.ConnectionTimeoutError(options)
}),
(ac) => proxy.connect(request, options).then((s) => {
this.#setKeepAlive(s)
const connectEvent = SECURE ? 'secureConnect' : 'connect'
const connectingEvent = SECURE ? 'secureConnecting' : 'connecting'
if (!s[connectingEvent]) {
return s
}
return abortRace([
() => once(s, 'error', ac).then((err) => {
throw err
}),
() => once(s, connectEvent, ac).then(() => s),
], ac)
}),
])
this.#setIdleTimeout(socket, options)
return socket
}
async connect (request, options) {
const proxy = this.#getProxy(options)
if (proxy) {
return this.#proxyConnect(proxy, request, options)
}
const socket = SOCKET_TYPE.connect(options)
this.#setKeepAlive(socket)
await abortRace([
(s) => createTimeout(this.#timeouts.connection, s).catch(() => {
throw new Errors.ConnectionTimeoutError(options)
}),
(s) => once(socket, 'error', s).then((err) => {
throw err
}),
(s) => once(socket, 'connect', s),
])
this.#setIdleTimeout(socket, options)
return socket
}
addRequest (request, options) {
const proxy = this.#getProxy(options)
// it would be better to call proxy.addRequest here but this causes the
// http-proxy-agent to call its super.addRequest which causes the request
// to be added to the agent twice. since we only support 3 agents
// currently (see the required agents in proxy.js) we have manually
// checked that the only public methods we need to call are called in the
// next block. this could change in the future and presumably we would get
// failing tests until we have properly called the necessary methods on
// each of our proxy agents
if (proxy?.setRequestProps) {
proxy.setRequestProps(request, options)
}
request.setHeader('connection', this.keepAlive ? 'keep-alive' : 'close')
const responseTimeout = createTimeout(this.#timeouts.response)
if (responseTimeout) {
request.once('finish', () => {
responseTimeout.start(() => {
request.destroy(new Errors.ResponseTimeoutError(request, this.proxy?.url))
})
})
request.once('response', () => {
responseTimeout.clear()
})
}
const transferTimeout = createTimeout(this.#timeouts.transfer)
if (transferTimeout) {
request.once('response', (res) => {
transferTimeout.start(() => {
res.destroy(new Errors.TransferTimeoutError(request, this.proxy?.url))
})
res.once('close', () => {
transferTimeout.clear()
})
})
}
return super.addRequest(request, options)
}
createSocket (req, options, cb) {
return Promise.resolve()
.then(() => this.connect(req, options))
.then((socket) => {
this.#socket = socket
return super.createSocket(req, options, cb)
}, cb)
}
createConnection () {
return this.#socket
}
}
Object.defineProperty(agent, 'name', { value: name })
return agent
}
module.exports = {
HttpAgent: createAgent(http, 'HttpAgent'),
HttpsAgent: createAgent(https, 'HttpsAgent'),
}