@fastify/websocket
Advanced tools
Comparing version 8.1.0 to 8.2.0
21
index.js
@@ -22,2 +22,11 @@ 'use strict' | ||
let preClose = defaultPreClose | ||
if (opts && opts.preClose) { | ||
if (typeof opts.preClose !== 'function') { | ||
return next(new Error('invalid preClose function')) | ||
} | ||
preClose = opts.preClose | ||
} | ||
if (opts.options && opts.options.noServer) { | ||
@@ -147,8 +156,8 @@ return next(new Error("fastify-websocket doesn't support the ws noServer option. If you want to create a websocket server detatched from fastify, use the ws library directly.")) | ||
fastify.addHook('onClose', close) | ||
// Fastify is missing a pre-close event, or the ability to | ||
// add a hook before the server.close call. We need to resort | ||
// to monkeypatching for now. | ||
fastify.addHook('preClose', function (done) { | ||
fastify.addHook('preClose', preClose) | ||
function defaultPreClose (done) { | ||
const server = this.websocketServer | ||
@@ -161,8 +170,6 @@ if (server.clients) { | ||
fastify.server.removeListener('upgrade', onUpgrade) | ||
done() | ||
}) | ||
function close (fastify, done) { | ||
const server = fastify.websocketServer | ||
server.close(done) | ||
done() | ||
} | ||
@@ -169,0 +176,0 @@ |
{ | ||
"name": "@fastify/websocket", | ||
"version": "8.1.0", | ||
"version": "8.2.0", | ||
"description": "basic websocket support for fastify", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -236,2 +236,25 @@ # @fastify/websocket | ||
Note: Fastify's `onError` and error handlers registered by `setErrorHandler` will still be called for errors encountered *before* the websocket connection is established. This means errors thrown by `onRequest` hooks, `preValidation` handlers, and hooks registered by plugins will use the normal error handling mechanisms in Fastify. Once the websocket is established and your websocket route handler is called, `fastify-websocket`'s `errorHandler` takes over. | ||
### Custom preClose hook: | ||
By default, all ws connections are closed when the server closes. If you wish to modify this behaviour, you can pass your own `preClose` function. | ||
Note that `preClose` is responsible for closing all connections and closing the websocket server. | ||
```js | ||
const fastify = require('fastify')() | ||
fastify.register(require('@fastify/websocket'), { | ||
preClose: (done) => { // Note: can also use async style, without done-callback | ||
const server = this.websocketServer | ||
for (const connection of server.clients) { | ||
connection.close(1001, 'WS server is going offline in custom manner, sending a code + message') | ||
} | ||
server.close(done) | ||
} | ||
}) | ||
``` | ||
## Options | ||
@@ -238,0 +261,0 @@ |
@@ -354,2 +354,70 @@ 'use strict' | ||
test('Should be able to pass preClose option to override default', async (t) => { | ||
t.plan(3) | ||
const fastify = Fastify() | ||
const preClose = (done) => { | ||
t.pass('Custom preclose successfully called') | ||
for (const connection of fastify.websocketServer.clients) { | ||
connection.close() | ||
} | ||
done() | ||
} | ||
await fastify.register(fastifyWebsocket, { preClose }) | ||
fastify.get('/', { websocket: true }, (connection) => { | ||
connection.setEncoding('utf8') | ||
t.teardown(() => connection.destroy()) | ||
connection.once('data', (chunk) => { | ||
t.equal(chunk, 'hello server') | ||
connection.write('hello client') | ||
connection.end() | ||
}) | ||
}) | ||
await fastify.listen({ port: 0 }) | ||
const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) | ||
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' }) | ||
t.teardown(() => client.destroy()) | ||
client.setEncoding('utf8') | ||
client.write('hello server') | ||
const [chunk] = await once(client, 'data') | ||
t.equal(chunk, 'hello client') | ||
client.end() | ||
await fastify.close() | ||
}) | ||
test('Should fail if custom preClose is not a function', async (t) => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
const preClose = 'Not a function' | ||
try { | ||
await fastify.register(fastifyWebsocket, { preClose }) | ||
} catch (err) { | ||
t.equal(err.message, 'invalid preClose function') | ||
} | ||
fastify.get('/', { websocket: true }, (connection) => { | ||
t.teardown(() => connection.destroy()) | ||
}) | ||
try { | ||
await fastify.listen({ port: 0 }) | ||
} catch (err) { | ||
t.equal(err.message, 'invalid preClose function') | ||
} | ||
}) | ||
test('Should gracefully close with a connected client', async (t) => { | ||
@@ -356,0 +424,0 @@ t.plan(2) |
@@ -8,2 +8,3 @@ /// <reference types="node" /> | ||
import { FastifyReply } from 'fastify/types/reply'; | ||
import { preCloseHookHandler, preCloseAsyncHookHandler } from 'fastify/types/hooks'; | ||
import { RouteGenericInterface } from 'fastify/types/route'; | ||
@@ -92,2 +93,3 @@ | ||
connectionOptions?: DuplexOptions; | ||
preClose?: preCloseHookHandler | preCloseAsyncHookHandler; | ||
} | ||
@@ -94,0 +96,0 @@ |
@@ -25,2 +25,4 @@ import wsPlugin, { WebsocketHandler, SocketStream } from '..'; | ||
app.register(wsPlugin, { options: { perMessageDeflate: true } }); | ||
app.register(wsPlugin, { preClose: function syncPreclose() {} }); | ||
app.register(wsPlugin, { preClose: async function asyncPreclose(){} }); | ||
@@ -27,0 +29,0 @@ app.get('/websockets-via-inferrence', { websocket: true }, async function (connection, request) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84960
1907
298