Skip to content
Merged
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
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const kWs = Symbol('ws-socket')
const kWsHead = Symbol('ws-head')

function fastifyWebsocket (fastify, opts, next) {
fastify.decorateRequest('ws', null)

let errorHandler = defaultErrorHandler
if (opts.errorHandler) {
if (typeof opts.errorHandler !== 'function') {
Expand Down Expand Up @@ -68,6 +70,15 @@ function fastifyWebsocket (fastify, opts, next) {
})
}

fastify.addHook('onRequest', (request, reply, done) => { // this adds req.ws to the Request object
if (request.raw[kWs]) {
request.ws = true
} else {
request.ws = false
}
done()
})
Copy link
Member

Choose a reason for hiding this comment

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

Please add a test so future committers don't break this functionality!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What test?

Copy link
Member

Choose a reason for hiding this comment

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

I think it'd make sense to add unit tests that check the value of this property is true for a websocket route and false for a non websocket route.

Copy link
Member

Choose a reason for hiding this comment

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

/@a-a-GiTHuB-a-a are you able to copy one of the existing unit tests to get this in?


fastify.addHook('onError', (request, reply, error, done) => {
if (request.raw[kWs]) {
// Hijack reply to prevent fastify from sending the error after onError hooks are done running
Expand Down