Skip to content

Commit e57481a

Browse files
committed
feat: forward this on onReq & onRes
1 parent 43cecdc commit e57481a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = {
3434

3535
const kReq = Symbol('req')
3636
const kRes = Symbol('res')
37+
const kSelf = Symbol('self')
3738
const kProxyCallback = Symbol('callback')
3839
const kProxyReq = Symbol('proxyReq')
3940
const kProxyRes = Symbol('proxyRes')
@@ -70,6 +71,7 @@ function proxy (req, res, head, {
7071
req[kRes] = res
7172
}
7273

74+
res[kSelf] = this
7375
res[kReq] = req
7476
res[kRes] = res
7577
res[kProxyCallback] = callback
@@ -138,7 +140,7 @@ function proxy (req, res, head, {
138140
let proxyReq
139141

140142
if (onReq) {
141-
proxyReq = onReq(req, options)
143+
proxyReq = onReq.call(res[kSelf], req, options)
142144
}
143145

144146
if (!proxyReq) {
@@ -213,7 +215,7 @@ function onFinish (err, statusCode = 500) {
213215
}
214216

215217
if (res[kProxyCallback]) {
216-
res[kProxyCallback].call(null, err, res[kReq], res)
218+
res[kProxyCallback].call(res[kSelf], err, res[kReq], res)
217219
res[kProxyCallback] = null
218220
}
219221

@@ -265,7 +267,7 @@ function onProxyResponse (proxyRes) {
265267
proxyRes.headers[HTTP2_HEADER_STATUS] = proxyRes.statusCode || proxyRes.status
266268

267269
if (this[kOnProxyRes]) {
268-
this[kOnProxyRes](this[kReq], proxyRes.headers)
270+
this[kOnProxyRes].call(res[kSelf], this[kReq], proxyRes.headers)
269271
}
270272

271273
res.respond(proxyRes.headers, RESPOND_OPTIONS)
@@ -276,7 +278,7 @@ function onProxyResponse (proxyRes) {
276278
}
277279

278280
if (this[kOnProxyRes]) {
279-
this[kOnProxyRes](this[kReq], res)
281+
this[kOnProxyRes].call(res[kSelf], this[kReq], res)
280282
}
281283

282284
res.writeHead(res.statusCode)

0 commit comments

Comments
 (0)