forked from nxtedition/node-http2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat.js
158 lines (146 loc) · 3.76 KB
/
compat.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
const http = require('http')
const https = require('https')
const tlsOptions = [
'ca',
'cert',
'ciphers',
'clientCertEngine',
'crl',
'dhparam',
'ecdhCurve',
'honorCipherOrder',
'key',
'passphrase',
'pfx',
'rejectUnauthorized',
'secureOptions',
'secureProtocol',
'servername',
'sessionIdContext',
'highWaterMark',
'checkServerIdentity',
];
module.exports = function (proxy) {
proxy.ws = function ws (req, socket, head, options, callback) {
const promise = compat({ req, socket, head }, options)
if (!callback) {
return promise
}
// Legacy compat...
promise
.then(() => callback(null, req, socket, head))
.catch(err => callback(err, req, socket, head))
}
proxy.web = function web (req, res, options, callback) {
const promise = compat({ req, res }, options)
if (!callback) {
return promise
}
// Legacy compat...
promise
.then(() => callback(null, req, res))
.catch(err => callback(err, req, res))
}
async function compat (ctx, options) {
const { req, res } = ctx
const {
hostname,
port,
path,
socketPath,
protocol,
timeout,
proxyTimeout,
proxyName,
onReq,
onRes
} = options
// Legacy compat...
if (timeout != null) {
req.setTimeout(timeout)
}
await proxy(
{ ...ctx, proxyName },
async ureq => {
for (const key of tlsOptions) {
if (Reflect.has(options, key)) {
const value = Reflect.get(options, key);
Reflect.set(ureq, key, value);
}
}
if (hostname !== undefined) {
ureq.hostname = hostname
}
if (port !== undefined) {
ureq.port = port
}
if (path !== undefined) {
ureq.path = path
}
if (proxyTimeout !== undefined) {
ureq.timeout = proxyTimeout
}
if (socketPath !== undefined) {
ureq.socketPath = socketPath
}
let ret
if (onReq) {
if (onReq.length <= 2) {
ret = await onReq(req, ureq)
} else {
// Legacy compat...
ret = await new Promise((resolve, reject) => {
const promiseOrReq = onReq(req, ureq, (err, val) =>
err ? reject(err) : resolve(val)
)
if (promiseOrReq) {
if (promiseOrReq.then) {
promiseOrReq.then(resolve).catch(reject)
} else if (promiseOrReq.abort) {
resolve(promiseOrReq)
} else {
throw new Error(
'onReq must return a promise or a request object'
)
}
} else {
resolve()
}
})
}
}
if (!ret) {
let agent
if (protocol == null || /^(http|ws):?$/.test(protocol)) {
agent = http
} else if (/^(http|ws)s:?$/.test(protocol)) {
agent = https
} else {
throw new Error('invalid protocol')
}
ret = agent.request(ureq)
}
return ret
},
onRes
? async (proxyRes, headers) => {
proxyRes.headers = headers
if (onRes.length <= 3) {
return onRes(req, res, proxyRes)
} else {
// Legacy compat...
return new Promise((resolve, reject) => {
const promise = onRes(req, res, proxyRes, (err, val) =>
err ? reject(err) : resolve(val)
)
if (promise && promise.then) {
promise.then(resolve).catch(reject)
}
})
}
}
: null
)
}
return proxy
}