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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function fastifyWebsocket (fastify, opts, next) {
})
}

fastify.addHook('onRequest', (request, reply, done) => { // this adds req.ws to the Request object
fastify.addHook('onRequest', (request, _reply, done) => { // this adds req.ws to the Request object
if (request.raw[kWs]) {
request.ws = true
} else {
Expand All @@ -139,7 +139,7 @@ function fastifyWebsocket (fastify, opts, next) {
done()
})

fastify.addHook('onResponse', (request, reply, done) => {
fastify.addHook('onResponse', (request, _reply, done) => {
if (request.ws) {
request.raw[kWs].destroy()
}
Expand Down
4 changes: 2 additions & 2 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ test('clashing upgrade handler', async (t) => {
const fastify = Fastify()
t.teardown(() => fastify.close())

fastify.server.on('upgrade', (req, socket, head) => {
fastify.server.on('upgrade', (req, socket) => {
const res = new http.ServerResponse(req)
res.assignSocket(socket)
res.end()
Expand All @@ -660,7 +660,7 @@ test('clashing upgrade handler', async (t) => {

await fastify.register(fastifyWebsocket)

fastify.get('/', { websocket: true }, (connection) => {
fastify.get('/', { websocket: true }, () => {
t.fail('this should never be invoked')
})

Expand Down
58 changes: 29 additions & 29 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ test('Should run onRequest, preValidation, preHandler hooks', t => {
fastify.register(fastifyWebsocket)

fastify.register(async function (fastify) {
fastify.addHook('onRequest', async (request, reply) => t.ok('called', 'onRequest'))
fastify.addHook('preParsing', async (request, reply, payload) => t.ok('called', 'preParsing'))
fastify.addHook('preValidation', async (request, reply) => t.ok('called', 'preValidation'))
fastify.addHook('preHandler', async (request, reply) => t.ok('called', 'preHandler'))
fastify.addHook('onRequest', async () => t.ok('called', 'onRequest'))
fastify.addHook('preParsing', async () => t.ok('called', 'preParsing'))
fastify.addHook('preValidation', async () => t.ok('called', 'preValidation'))
fastify.addHook('preHandler', async () => t.ok('called', 'preHandler'))

fastify.get('/echo', { websocket: true }, (socket, request) => {
fastify.get('/echo', { websocket: true }, (socket) => {
socket.send('hello client')
t.teardown(() => socket.terminate())

Expand Down Expand Up @@ -56,7 +56,7 @@ test('Should not run onTimeout hook', t => {
fastify.register(fastifyWebsocket)

fastify.register(async function () {
fastify.addHook('onTimeout', async (request, reply) => t.fail('called', 'onTimeout'))
fastify.addHook('onTimeout', async () => t.fail('called', 'onTimeout'))

fastify.get('/echo', { websocket: true }, (socket, request) => {
socket.send('hello client')
Expand Down Expand Up @@ -86,10 +86,10 @@ test('Should run onError hook before handler is executed (error thrown in onRequ
fastify.register(fastifyWebsocket)

fastify.register(async function (fastify) {
fastify.addHook('onRequest', async (request, reply) => { throw new Error('Fail') })
fastify.addHook('onError', async (request, reply) => t.ok('called', 'onError'))
fastify.addHook('onRequest', async () => { throw new Error('Fail') })
fastify.addHook('onError', async () => t.ok('called', 'onError'))

fastify.get('/echo', { websocket: true }, (conn, request) => {
fastify.get('/echo', { websocket: true }, () => {
t.fail()
})
})
Expand All @@ -110,14 +110,14 @@ test('Should run onError hook before handler is executed (error thrown in preVal
fastify.register(fastifyWebsocket)

fastify.register(async function (fastify) {
fastify.addHook('preValidation', async (request, reply) => {
fastify.addHook('preValidation', async () => {
await Promise.resolve()
throw new Error('Fail')
})

fastify.addHook('onError', async (request, reply) => t.ok('called', 'onError'))
fastify.addHook('onError', async () => t.ok('called', 'onError'))

fastify.get('/echo', { websocket: true }, (conn, request) => {
fastify.get('/echo', { websocket: true }, () => {
t.fail()
})
})
Expand All @@ -138,17 +138,17 @@ test('onError hooks can send a reply and prevent hijacking', t => {
fastify.register(fastifyWebsocket)

fastify.register(async function (fastify) {
fastify.addHook('preValidation', async (request, reply) => {
fastify.addHook('preValidation', async () => {
await Promise.resolve()
throw new Error('Fail')
})

fastify.addHook('onError', async (request, reply) => {
fastify.addHook('onError', async (_request, reply) => {
t.ok('called', 'onError')
await reply.code(501).send('there was an error')
})

fastify.get('/echo', { websocket: true }, (conn, request) => {
fastify.get('/echo', { websocket: true }, () => {
t.fail()
})
})
Expand All @@ -169,18 +169,18 @@ test('setErrorHandler functions can send a reply and prevent hijacking', t => {
fastify.register(fastifyWebsocket)

fastify.register(async function (fastify) {
fastify.addHook('preValidation', async (request, reply) => {
fastify.addHook('preValidation', async () => {
await Promise.resolve()
throw new Error('Fail')
})

fastify.setErrorHandler(async (error, request, reply) => {
fastify.setErrorHandler(async (error, _request, reply) => {
t.ok('called', 'onError')
t.ok(error)
await reply.code(501).send('there was an error')
})

fastify.get('/echo', { websocket: true }, (conn, request) => {
fastify.get('/echo', { websocket: true }, () => {
t.fail()
})
})
Expand All @@ -201,9 +201,9 @@ test('Should not run onError hook if reply was already hijacked (error thrown in
fastify.register(fastifyWebsocket)

fastify.register(async function (fastify) {
fastify.addHook('onError', async (request, reply) => t.fail('called', 'onError'))
fastify.addHook('onError', async () => t.fail('called', 'onError'))

fastify.get('/echo', { websocket: true }, async (socket, request) => {
fastify.get('/echo', { websocket: true }, async (socket) => {
t.teardown(() => socket.terminate())
throw new Error('Fail')
})
Expand All @@ -227,10 +227,10 @@ test('Should not run preSerialization/onSend hooks', t => {
fastify.register(fastifyWebsocket)

fastify.register(async function (fastify) {
fastify.addHook('onSend', async (request, reply) => t.fail('called', 'onSend'))
fastify.addHook('preSerialization', async (request, reply) => t.fail('called', 'preSerialization'))
fastify.addHook('onSend', async () => t.fail('called', 'onSend'))
fastify.addHook('preSerialization', async () => t.fail('called', 'preSerialization'))

fastify.get('/echo', { websocket: true }, async (socket, request) => {
fastify.get('/echo', { websocket: true }, async (socket) => {
socket.send('hello client')
t.teardown(() => socket.terminate())
})
Expand Down Expand Up @@ -258,7 +258,7 @@ test('Should not hijack reply for a normal http request in the internal onError
fastify.register(fastifyWebsocket)

fastify.register(async function (fastify) {
fastify.get('/', async (request, reply) => {
fastify.get('/', async () => {
throw new Error('Fail')
})
})
Expand Down Expand Up @@ -294,7 +294,7 @@ test('Should run async hooks and still deliver quickly sent messages', (t) => {
async () => await new Promise((resolve) => setTimeout(resolve, 25))
)

fastify.get('/echo', { websocket: true }, (socket, request) => {
fastify.get('/echo', { websocket: true }, (socket) => {
socket.send('hello client')
t.teardown(() => socket.terminate())

Expand Down Expand Up @@ -329,11 +329,11 @@ test('Should not hijack reply for an normal request to a websocket route that is

fastify.register(fastifyWebsocket)
fastify.register(async function (fastify) {
fastify.addHook('preValidation', async (request, reply) => {
fastify.addHook('preValidation', async (_request, reply) => {
await Promise.resolve()
await reply.code(404).send('not found')
})
fastify.get('/echo', { websocket: true }, (conn, request) => {
fastify.get('/echo', { websocket: true }, () => {
t.fail()
})
})
Expand Down Expand Up @@ -361,10 +361,10 @@ test('Should not hijack reply for an WS request to a WS route that gets sent a n

fastify.register(fastifyWebsocket)
fastify.register(async function (fastify) {
fastify.addHook('preValidation', async (request, reply) => {
fastify.addHook('preValidation', async (_request, reply) => {
await reply.code(404).send('not found')
})
fastify.get('/echo', { websocket: true }, (conn, request) => {
fastify.get('/echo', { websocket: true }, () => {
t.fail()
})
})
Expand Down
6 changes: 3 additions & 3 deletions test/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test('routes correctly the message between two routes', async (t) => {
fastify.register(
async function (instance) {
instance.get('/ws', { websocket: true }, function (socket) {
socket.once('message', chunk => {
socket.once('message', () => {
_reject('wrong-route')
})
})
Expand Down Expand Up @@ -121,11 +121,11 @@ test('rejects if the websocket is not upgraded', async (t) => {

fastify.register(
async function (instance) {
instance.addHook('preValidation', async (request, reply) => {
instance.addHook('preValidation', async (_request, reply) => {
return reply.code(401).send()
})

instance.get('/', { websocket: true }, function (conn) {
instance.get('/', { websocket: true }, function () {
})
})

Expand Down
Loading
Loading