Skip to content

Commit 10c30f8

Browse files
committed
perf: removed try/catch
1 parent 6353c7c commit 10c30f8

File tree

1 file changed

+86
-98
lines changed

1 file changed

+86
-98
lines changed

index.js

Lines changed: 86 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -52,69 +52,65 @@ function impl (req, resOrSocket, headOrNil, {
5252
const incoming = req.stream || req
5353
incoming.on('error', errorHandler)
5454

55-
try {
56-
if (resOrSocket instanceof net.Socket) {
57-
if (req.method !== 'GET') {
58-
throw createError('method not allowed', null, 405)
59-
}
60-
61-
if (!req.headers[HTTP2_HEADER_UPGRADE] ||
62-
req.headers[HTTP2_HEADER_UPGRADE].toLowerCase() !== 'websocket') {
63-
throw createError('bad request', null, 400)
64-
}
55+
if (resOrSocket instanceof net.Socket) {
56+
if (req.method !== 'GET') {
57+
return errorHandler(createError('method not allowed', null, 405))
6558
}
6659

67-
if (req.httpVersion !== '1.1' && req.httpVersion !== '2.0') {
68-
throw createError('http version not supported', null, 505)
60+
if (!req.headers[HTTP2_HEADER_UPGRADE] ||
61+
req.headers[HTTP2_HEADER_UPGRADE].toLowerCase() !== 'websocket') {
62+
return errorHandler(createError('bad request', null, 400))
6963
}
64+
}
7065

71-
if (proxyName && req.headers[HTTP2_HEADER_VIA]) {
72-
for (const name of req.headers[HTTP2_HEADER_VIA].split(',')) {
73-
if (sanitize(name).endsWith(proxyName.toLowerCase())) {
74-
throw createError('loop detected', null, 508)
75-
}
76-
}
77-
}
66+
if (req.httpVersion !== '1.1' && req.httpVersion !== '2.0') {
67+
return errorHandler(createError('http version not supported', null, 505))
68+
}
7869

79-
if (timeout) {
80-
req.setTimeout(timeout, errorHandler.requestTimeout)
70+
if (proxyName && req.headers[HTTP2_HEADER_VIA]) {
71+
for (const name of req.headers[HTTP2_HEADER_VIA].split(',')) {
72+
if (sanitize(name).endsWith(proxyName.toLowerCase())) {
73+
return errorHandler(createError('loop detected', null, 508))
74+
}
8175
}
76+
}
8277

83-
if (resOrSocket instanceof net.Socket) {
84-
if (headOrNil && headOrNil.length) {
85-
resOrSocket.unshift(headOrNil)
86-
}
78+
if (timeout) {
79+
req.setTimeout(timeout, errorHandler.requestTimeout)
80+
}
8781

88-
setupSocket(resOrSocket)
82+
if (resOrSocket instanceof net.Socket) {
83+
if (headOrNil && headOrNil.length) {
84+
resOrSocket.unshift(headOrNil)
8985
}
9086

91-
const headers = getRequestHeaders(req)
87+
setupSocket(resOrSocket)
88+
}
9289

93-
if (proxyName) {
94-
if (headers[HTTP2_HEADER_VIA]) {
95-
headers[HTTP2_HEADER_VIA] += `,${proxyName}`
96-
} else {
97-
headers[HTTP2_HEADER_VIA] = proxyName
98-
}
99-
}
90+
const headers = getRequestHeaders(req)
10091

101-
const options = {
102-
method: req.method,
103-
hostname,
104-
port,
105-
path: req.url,
106-
headers,
107-
timeout: proxyTimeout
92+
if (proxyName) {
93+
if (headers[HTTP2_HEADER_VIA]) {
94+
headers[HTTP2_HEADER_VIA] += `,${proxyName}`
95+
} else {
96+
headers[HTTP2_HEADER_VIA] = proxyName
10897
}
98+
}
10999

110-
if (onReq) {
111-
onReq(req, options)
112-
}
100+
const options = {
101+
method: req.method,
102+
hostname,
103+
port,
104+
path: req.url,
105+
headers,
106+
timeout: proxyTimeout
107+
}
113108

114-
return proxy(req, resOrSocket, options, onRes, errorHandler)
115-
} catch (err) {
116-
return errorHandler(err)
109+
if (onReq) {
110+
onReq(req, options)
117111
}
112+
113+
proxy(req, resOrSocket, options, onRes, errorHandler)
118114
}
119115

120116
function proxy (req, resOrSocket, options, onRes, errorHandler) {
@@ -365,37 +361,33 @@ class ProxyResponseHandler {
365361
_handle (proxyRes) {
366362
this.proxyRes = proxyRes
367363

368-
try {
369-
proxyRes.on('aborted', this.proxyErrorHandler.socketHangup)
364+
proxyRes.on('aborted', this.proxyErrorHandler.socketHangup)
370365

371-
if (this.resOrSocket instanceof net.Socket) {
372-
if (this.onRes) {
373-
this.onRes(this.req, this.resOrSocket)
374-
}
375-
376-
if (!proxyRes.upgrade) {
377-
this.resOrSocket.end()
378-
}
379-
} else {
380-
setupHeaders(proxyRes.headers)
366+
if (this.resOrSocket instanceof net.Socket) {
367+
if (this.onRes) {
368+
this.onRes(this.req, this.resOrSocket)
369+
}
381370

382-
this.resOrSocket.statusCode = proxyRes.statusCode
383-
for (const key of Object.keys(proxyRes.headers)) {
384-
this.resOrSocket.setHeader(key, proxyRes.headers[key])
385-
}
371+
if (!proxyRes.upgrade) {
372+
this.resOrSocket.end()
373+
}
374+
} else {
375+
setupHeaders(proxyRes.headers)
386376

387-
if (this.onRes) {
388-
this.onRes(this.req, this.resOrSocket)
389-
}
377+
this.resOrSocket.statusCode = proxyRes.statusCode
378+
for (const key of Object.keys(proxyRes.headers)) {
379+
this.resOrSocket.setHeader(key, proxyRes.headers[key])
380+
}
390381

391-
this.resOrSocket.writeHead(this.resOrSocket.statusCode)
392-
proxyRes.on('end', this._addTrailers)
393-
proxyRes
394-
.on('error', this.proxyErrorHandler)
395-
.pipe(this.resOrSocket)
382+
if (this.onRes) {
383+
this.onRes(this.req, this.resOrSocket)
396384
}
397-
} catch (err) {
398-
this.proxyErrorHandler(err)
385+
386+
this.resOrSocket.writeHead(this.resOrSocket.statusCode)
387+
proxyRes.on('end', this._addTrailers)
388+
proxyRes
389+
.on('error', this.proxyErrorHandler)
390+
.pipe(this.resOrSocket)
399391
}
400392
}
401393

@@ -442,40 +434,36 @@ class ProxyUpgradeHandler {
442434
this.proxyRes = proxyRes
443435
this.proxySocket = proxySocket
444436

445-
try {
446-
setupSocket(proxySocket)
437+
setupSocket(proxySocket)
447438

448-
if (proxyHead && proxyHead.length) {
449-
proxySocket.unshift(proxyHead)
450-
}
439+
if (proxyHead && proxyHead.length) {
440+
proxySocket.unshift(proxyHead)
441+
}
451442

452-
let head = 'HTTP/1.1 101 Switching Protocols'
443+
let head = 'HTTP/1.1 101 Switching Protocols'
453444

454-
for (const key of Object.keys(proxyRes.headers)) {
455-
const value = proxyRes.headers[key]
456-
457-
if (!Array.isArray(value)) {
458-
head += '\r\n' + key + ': ' + value
459-
} else {
460-
for (let i = 0; i < value.length; i++) {
461-
head += '\r\n' + key + ': ' + value[i]
462-
}
445+
for (const key of Object.keys(proxyRes.headers)) {
446+
const value = proxyRes.headers[key]
447+
448+
if (!Array.isArray(value)) {
449+
head += '\r\n' + key + ': ' + value
450+
} else {
451+
for (let i = 0; i < value.length; i++) {
452+
head += '\r\n' + key + ': ' + value[i]
463453
}
464454
}
455+
}
465456

466-
head += '\r\n\r\n'
457+
head += '\r\n\r\n'
467458

468-
this.resOrSocket.write(head)
459+
this.resOrSocket.write(head)
469460

470-
proxyRes.on('error', this.proxyErrorHandler)
461+
proxyRes.on('error', this.proxyErrorHandler)
471462

472-
proxySocket
473-
.on('error', this.proxyErrorHandler)
474-
.pipe(this.resOrSocket)
475-
.pipe(proxySocket)
476-
} catch (err) {
477-
this.proxyErrorHandler(err)
478-
}
463+
proxySocket
464+
.on('error', this.proxyErrorHandler)
465+
.pipe(this.resOrSocket)
466+
.pipe(proxySocket)
479467
}
480468

481469
_release () {

0 commit comments

Comments
 (0)