Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ function fastifyWebsocket (fastify, opts, next) {
}

// we always override the route handler so we can close websocket connections to routes to handlers that don't support websocket connections
routeOptions.handler = (request, reply) => {
// This is not an arrow function to fetch the encapsulated this
routeOptions.handler = function (request, reply) {
// within the route handler, we check if there has been a connection upgrade by looking at request.raw[kWs]. we need to dispatch the normal HTTP handler if not, and hijack to dispatch the websocket handler if so
if (request.raw[kWs]) {
reply.hijack()
handleUpgrade(request.raw, connection => {
let result

try {
if (isWebsocketRoute) {
result = wsHandler.call(fastify, connection, request)
result = wsHandler.call(this, connection, request)
} else {
result = noHandle.call(fastify, connection, request)
result = noHandle.call(this, connection, request)
}
} catch (err) {
return errorHandler.call(this, err, connection, request, reply)
Expand All @@ -127,7 +127,7 @@ function fastifyWebsocket (fastify, opts, next) {
}
})
} else {
return handler.call(fastify, request, reply)
return handler.call(this, request, reply)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooof, my bad! 👍 Thanks!

}
}
})
Expand Down
21 changes: 21 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,24 @@ test('Should error server if the noServer option is set', (t) => {
fastify.register(fastifyWebsocket, { options: { noServer: true } })
t.rejects(fastify.ready())
})

test('Should preserve the prefix in non-websocket routes', (t) => {
t.plan(3)

const fastify = Fastify()
t.teardown(() => fastify.close())

fastify.register(fastifyWebsocket)

fastify.register(async function (fastify) {
t.equal(fastify.prefix, '/hello')
fastify.get('/', function (request, reply) {
t.equal(this.prefix, '/hello')
reply.send('hello')
})
}, { prefix: '/hello' })

fastify.inject('/hello', function (err) {
t.error(err)
})
})
9 changes: 6 additions & 3 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ const WebSocket = require('ws')
const get = require('http').get

test('Should expose a websocket on prefixed route', t => {
t.plan(3)
t.plan(4)
const fastify = Fastify()

t.teardown(() => fastify.close())

fastify.register(fastifyWebsocket)
fastify.register(
function (instance, opts, next) {
instance.get('/echo', { websocket: true }, (conn, request) => {
instance.get('/echo', { websocket: true }, function (conn, request) {
t.equal(this.prefix, '/baz')
conn.setEncoding('utf8')
conn.write('hello client')
t.teardown(conn.destroy.bind(conn))
Expand Down Expand Up @@ -274,6 +275,7 @@ test('Should call wildcard route handler on unregistered path', t => {
})

test('Should invoke the correct handler depending on the headers', t => {
t.plan(4)
const fastify = Fastify()

t.teardown(() => fastify.close())
Expand All @@ -300,14 +302,15 @@ test('Should invoke the correct handler depending on the headers', t => {
httpClient.write('GET / HTTP/1.1\r\n\r\n')
httpClient.once('data', data => {
t.match(data.toString(), /hi from handler/i)
httpClient.end()
})
})

const wsClient = net.createConnection({ port: port }, () => {
wsClient.write('GET / HTTP/1.1\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n')
wsClient.once('data', data => {
t.match(data.toString(), /hi from wsHandler/i)
wsClient.end(() => { t.end() })
wsClient.end(() => { t.pass() })
})
})
})
Expand Down