Skip to content

Commit cd9c5ba

Browse files
committed
refactor: onError => errorHandler
1 parent 9e72b74 commit cd9c5ba

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

index.js

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ function impl (req, resOrSocket, headOrNil, {
4545
onReq,
4646
onRes
4747
}, callback) {
48-
const onError = ErrorHandler.create(req, resOrSocket, callback)
48+
const errorHandler = ErrorHandler.create(req, resOrSocket, callback)
4949

50-
resOrSocket.on('error', onError)
50+
resOrSocket.on('error', errorHandler)
5151

5252
const incoming = req.stream || req
53-
incoming.on('error', onError)
53+
incoming.on('error', errorHandler)
5454

5555
try {
5656
if (resOrSocket instanceof net.Socket) {
@@ -77,7 +77,7 @@ function impl (req, resOrSocket, headOrNil, {
7777
}
7878

7979
if (timeout) {
80-
req.setTimeout(timeout, onError.requestTimeout)
80+
req.setTimeout(timeout, errorHandler.requestTimeout)
8181
}
8282

8383
if (resOrSocket instanceof net.Socket) {
@@ -111,28 +111,27 @@ function impl (req, resOrSocket, headOrNil, {
111111
onReq(req, options)
112112
}
113113

114-
return proxy(req, resOrSocket, options, onRes, onError)
114+
return proxy(req, resOrSocket, options, onRes, errorHandler)
115115
} catch (err) {
116-
return onError(err)
116+
return errorHandler(err)
117117
}
118118
}
119119

120-
function proxy (req, resOrSocket, options, onRes, onError) {
120+
function proxy (req, resOrSocket, options, onRes, errorHandler) {
121121
const proxyReq = http.request(options)
122-
123-
const onProxyError = ProxyErrorHandler.create(req, proxyReq, onError)
122+
const proxyErrorHandler = ProxyErrorHandler.create(req, proxyReq, errorHandler)
124123

125124
req
126125
.pipe(proxyReq)
127-
.on('error', onProxyError)
126+
.on('error', proxyErrorHandler)
128127
// NOTE http.ClientRequest emits "socket hang up" error when aborted
129128
// before having received a response, i.e. there is no need to listen for
130129
// proxyReq.on('aborted', ...).
131-
.on('timeout', onProxyError.gatewayTimeout)
132-
.on('response', ProxyResponseHandler.create(req, resOrSocket, onRes, onProxyError))
130+
.on('timeout', proxyErrorHandler.gatewayTimeout)
131+
.on('response', ProxyResponseHandler.create(req, resOrSocket, onRes, proxyErrorHandler))
133132

134133
if (resOrSocket instanceof net.Socket) {
135-
proxyReq.on('upgrade', ProxyUpgradeHandler.create(req, resOrSocket, onProxyError))
134+
proxyReq.on('upgrade', ProxyUpgradeHandler.create(req, resOrSocket, proxyErrorHandler))
136135
}
137136
}
138137

@@ -277,7 +276,7 @@ class ProxyErrorHandler {
277276
this.hasError = null
278277
this.req = null
279278
this.proxyReq = null
280-
this.onError = null
279+
this.errorHandler = null
281280

282281
this._release = this._release.bind(this)
283282
this._handle = this._handle.bind(this)
@@ -305,7 +304,7 @@ class ProxyErrorHandler {
305304
}
306305

307306
this._abort()
308-
this.onError(err)
307+
this.errorHandler(err)
309308
}
310309

311310
_gatewayTimeout () {
@@ -328,15 +327,15 @@ class ProxyErrorHandler {
328327
this.hasError = null
329328
this.req = null
330329
this.proxyReq = null
331-
this.onError = null
330+
this.errorHandler = null
332331
ProxyErrorHandler.pool.push(this)
333332
}
334333

335-
static create (req, proxyReq, onError) {
334+
static create (req, proxyReq, errorHandler) {
336335
const handler = ProxyErrorHandler.pool.pop() || new ProxyErrorHandler()
337336
handler.req = req
338337
handler.proxyReq = proxyReq
339-
handler.onError = onError
338+
handler.errorHandler = errorHandler
340339
handler.req.on('close', handler._release)
341340
return handler._handle
342341
}
@@ -352,7 +351,7 @@ class ProxyResponseHandler {
352351
this.req = null
353352
this.resOrSocket = null
354353
this.onRes = null
355-
this.onProxyError = null
354+
this.proxyErrorHandler = null
356355
this.proxyRes = null
357356

358357
this._handle = this._handle.bind(this)
@@ -368,7 +367,7 @@ class ProxyResponseHandler {
368367
this.proxyRes = proxyRes
369368

370369
try {
371-
proxyRes.on('aborted', this.onProxyError.socketHangup)
370+
proxyRes.on('aborted', this.proxyErrorHandler.socketHangup)
372371

373372
if (this.resOrSocket instanceof net.Socket) {
374373
if (this.onRes) {
@@ -393,29 +392,29 @@ class ProxyResponseHandler {
393392
this.resOrSocket.writeHead(this.resOrSocket.statusCode)
394393
proxyRes.on('end', this._addTrailers)
395394
proxyRes
396-
.on('error', this.onProxyError)
395+
.on('error', this.proxyErrorHandler)
397396
.pipe(this.resOrSocket)
398397
}
399398
} catch (err) {
400-
this.onProxyError(err)
399+
this.proxyErrorHandler(err)
401400
}
402401
}
403402

404403
_release () {
405404
this.req = null
406405
this.resOrSocket = null
407406
this.onRes = null
408-
this.onProxyError = null
407+
this.proxyErrorHandler = null
409408
this.proxyRes = null
410409
ProxyResponseHandler.pool.push(this)
411410
}
412411

413-
static create (req, resOrSocket, onRes, onProxyError) {
412+
static create (req, resOrSocket, onRes, proxyErrorHandler) {
414413
const handler = ProxyResponseHandler.pool.pop() || new ProxyResponseHandler()
415414
handler.req = req
416415
handler.resOrSocket = resOrSocket
417416
handler.onRes = onRes
418-
handler.onProxyError = onProxyError
417+
handler.proxyErrorHandler = proxyErrorHandler
419418
handler.proxyRes = null
420419
handler.req.on('close', handler._release)
421420
return handler._handle
@@ -427,7 +426,7 @@ class ProxyUpgradeHandler {
427426
constructor () {
428427
this.req = null
429428
this.resOrSocket = null
430-
this.onProxyError = null
429+
this.proxyErrorHandler = null
431430
this.proxyRes = null
432431
this.proxySocket = null
433432

@@ -464,14 +463,14 @@ class ProxyUpgradeHandler {
464463

465464
this.resOrSocket.write(head)
466465

467-
proxyRes.on('error', this.onProxyError)
466+
proxyRes.on('error', this.proxyErrorHandler)
468467

469468
proxySocket
470-
.on('error', this.onProxyError)
469+
.on('error', this.proxyErrorHandler)
471470
.pipe(this.resOrSocket)
472471
.pipe(proxySocket)
473472
} catch (err) {
474-
this.onProxyError(err)
473+
this.proxyErrorHandler(err)
475474
}
476475
}
477476

@@ -481,17 +480,17 @@ class ProxyUpgradeHandler {
481480

482481
this.req = null
483482
this.resOrSocket = null
484-
this.onProxyError = null
483+
this.proxyErrorHandler = null
485484
this.proxyRes = null
486485
this.proxySocket = null
487486
ProxyUpgradeHandler.pool.push(this)
488487
}
489488

490-
static create (req, resOrSocket, onProxyError) {
489+
static create (req, resOrSocket, proxyErrorHandler) {
491490
const handler = ProxyUpgradeHandler.pool.pop() || new ProxyUpgradeHandler()
492491
handler.req = req
493492
handler.resOrSocket = resOrSocket
494-
handler.onProxyError = onProxyError
493+
handler.proxyErrorHandler = proxyErrorHandler
495494
handler.req.on('close', handler._release)
496495
return handler._handle
497496
}

0 commit comments

Comments
 (0)