Skip to content

Commit

Permalink
perf(index): cache status code regex (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Jan 10, 2025
1 parent 035a3c7 commit db6711e
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Duplexify = require('duplexify')

const kWs = Symbol('ws-socket')
const kWsHead = Symbol('ws-head')
const statusCodeReg = /HTTP\/1.1 (\d+)/u

function fastifyWebsocket (fastify, opts, next) {
fastify.decorateRequest('ws', null)
Expand Down Expand Up @@ -74,7 +75,7 @@ function fastifyWebsocket (fastify, opts, next) {
ws.setSocket(clientStream, head, { maxPayload: 0 })
} else {
clientStream.removeListener('data', onData)
const statusCode = Number(chunk.toString().match(/HTTP\/1.1 (\d+)/)[1])
const statusCode = Number(statusCodeReg.exec(chunk.toString())[1])
reject(new Error('Unexpected server response: ' + statusCode))
}
}
Expand Down

0 comments on commit db6711e

Please sign in to comment.