forked from fastify/fast-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
155 lines (139 loc) · 3.89 KB
/
request.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
const semver = require('semver')
const http = require('http')
const https = require('https')
const eos = require('end-of-stream')
const pump = require('pump')
const { stripHttp1ConnectionHeaders } = require('./utils')
const undici = require('undici')
function buildRequest (opts) {
const isHttp2 = !!opts.http2
const isUndici = !!opts.undici
const requests = {
'http:': http,
'https:': https
}
const baseUrl = opts.base
const rejectUnauthorized = opts.rejectUnauthorized
let h2client
let agents
let http2
let pool
if (isHttp2) {
if (semver.lt(process.version, '9.0.0')) {
throw new Error('Http2 support requires Node version >= 9.0.0')
}
if (!opts.base) throw new Error('Option base is required when http2 is true')
} else {
agents = {
'http:': new http.Agent(agentOption(opts)),
'https:': new https.Agent(agentOption(opts))
}
}
if (isHttp2) {
http2 = getHttp2()
return { request: handleHttp2Req, close }
} else if (isUndici) {
if (typeof opts.undici !== 'object') {
opts.undici = {}
}
pool = new undici.Pool(baseUrl, opts.undici)
return { request: handleUndici, close }
} else {
return { request: handleHttp1Req, close }
}
function close () {
if (isUndici) {
pool.destroy()
} else if (!isHttp2) {
agents['http:'].destroy()
agents['https:'].destroy()
} else if (h2client) {
h2client.destroy()
}
}
function handleUndici (opts, done) {
const req = {
path: opts.url.pathname + opts.qs,
method: opts.method,
headers: opts.headers,
body: opts.body
}
pool.request(req, function (err, res) {
if (err) {
done(err)
return
}
setImmediate(() => done(null, { statusCode: res.statusCode, headers: res.headers, stream: res.body }))
})
}
function handleHttp1Req (opts, done) {
const req = requests[opts.url.protocol].request({
method: opts.method,
port: opts.url.port,
path: opts.url.pathname + opts.qs,
hostname: opts.url.hostname,
headers: opts.headers,
agent: agents[opts.url.protocol]
})
req.on('error', done)
req.on('response', res => {
setImmediate(() => done(null, { statusCode: res.statusCode, headers: res.headers, stream: res }))
})
end(req, opts.body, done)
}
function handleHttp2Req (opts, done) {
if (!h2client || h2client.destroyed) {
h2client = http2.connect(baseUrl, { rejectUnauthorized })
h2client.once('error', done)
// we might enqueue a large number of requests in this connection
// before it's connected
h2client.setMaxListeners(0)
h2client.on('connect', () => {
// reset the max listener to 10 on connect
h2client.setMaxListeners(10)
h2client.removeListener('error', done)
})
}
const req = h2client.request({
':method': opts.method,
':path': opts.url.pathname + opts.qs,
...stripHttp1ConnectionHeaders(opts.headers)
})
const isGet = opts.method === 'GET' || opts.method === 'get'
if (!isGet) {
end(req, opts.body, done)
}
eos(req, err => {
if (err) done(err)
})
req.on('response', headers => {
setImmediate(() => {
const statusCode = headers[':status']
done(null, { statusCode, headers, stream: req })
})
})
}
}
module.exports = buildRequest
function agentOption (opts) {
return {
keepAlive: true,
keepAliveMsecs: opts.keepAliveMsecs || 60 * 1000, // 1 minute
maxSockets: opts.maxSockets || 2048,
maxFreeSockets: opts.maxFreeSockets || 2048,
rejectUnauthorized: opts.rejectUnauthorized
}
}
function end (req, body, cb) {
if (!body || typeof body === 'string') {
req.end(body)
} else {
pump(body, req, err => {
if (err) cb(err)
})
}
}
// neede to avoid the experimental warning
function getHttp2 () {
return require('http2')
}