Skip to content

Commit 9909232

Browse files
committed
perf(ws): optimized upgrade
1 parent e7401c6 commit 9909232

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

index.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -231,25 +231,23 @@ function proxy (req, resOrSocket, options, onRes, onError) {
231231
proxySocket.unshift(proxyHead)
232232
}
233233

234-
resOrSocket.write(
235-
Object
236-
.keys(proxyRes.headers)
237-
.reduce((head, key) => {
238-
const value = proxyRes.headers[key]
239-
240-
if (!Array.isArray(value)) {
241-
head.push(key + ': ' + value)
242-
return head
243-
}
244-
245-
for (let i = 0; i < value.length; i++) {
246-
head.push(key + ': ' + value[i])
247-
}
248-
249-
return head
250-
}, ['HTTP/1.1 101 Switching Protocols'])
251-
.join('\r\n') + '\r\n\r\n'
252-
)
234+
let head = 'HTTP/1.1 101 Switching Protocols'
235+
236+
for (const key of Object.keys(proxyRes.headers)) {
237+
const value = proxyRes.headers[key]
238+
239+
if (!Array.isArray(value)) {
240+
head += '\r\n' + key + ': ' + value
241+
} else {
242+
for (let i = 0; i < value.length; i++) {
243+
head += '\r\n' + key + ': ' + value[i]
244+
}
245+
}
246+
}
247+
248+
head += '\r\n\r\n'
249+
250+
resOrSocket.write(head)
253251

254252
proxyRes.on('error', onProxyError)
255253

0 commit comments

Comments
 (0)