@fastify/websocket
Advanced tools
Comparing version 6.0.1 to 7.0.0
13
index.js
@@ -83,15 +83,2 @@ 'use strict' | ||
fastify.addHook('onError', (request, reply, error, done) => { | ||
/* istanbul ignore next */ | ||
if (request.raw[kWs]) { | ||
// Hijack reply to prevent fastify from sending the error after onError hooks are done running | ||
reply.hijack() | ||
handleUpgrade(request.raw, connection => { | ||
// Handle the error | ||
errorHandler.call(this, error, connection, request, reply) | ||
}) | ||
} | ||
done() | ||
}) | ||
fastify.addHook('onResponse', (request, reply, done) => { | ||
@@ -98,0 +85,0 @@ if (request.ws) { |
{ | ||
"name": "@fastify/websocket", | ||
"version": "6.0.1", | ||
"version": "7.0.0", | ||
"description": "basic websocket support for fastify", | ||
@@ -32,5 +32,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@fastify/pre-commit": "^2.0.2", | ||
"@types/ws": "^8.2.2", | ||
"fastify": "^4.0.0-rc.2", | ||
"pre-commit": "^1.2.2", | ||
"snazzy": "^9.0.0", | ||
@@ -40,6 +40,6 @@ "split2": "^4.1.0", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.20.0" | ||
"tsd": "^0.22.0" | ||
}, | ||
"dependencies": { | ||
"fastify-plugin": "^3.0.0", | ||
"fastify-plugin": "^4.0.0", | ||
"ws": "^8.0.0" | ||
@@ -46,0 +46,0 @@ }, |
@@ -5,3 +5,2 @@ # @fastify/websocket | ||
[![NPM version](https://img.shields.io/npm/v/@fastify/websocket.svg?style=flat)](https://www.npmjs.com/package/@fastify/websocket) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-websocket/badge.svg)](https://snyk.io/test/github/fastify/fastify-websocket) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) | ||
@@ -15,3 +14,3 @@ | ||
```shell | ||
npm install @fastify/websocket --save | ||
npm i @fastify/websocket | ||
# or | ||
@@ -24,3 +23,3 @@ yarn add @fastify/websocket | ||
```shell | ||
npm install @types/ws --save-dev | ||
npm i @types/ws -D | ||
# or | ||
@@ -48,3 +47,3 @@ yarn add -D @types/ws | ||
fastify.listen(3000, err => { | ||
fastify.listen({ port: 3000 }, err => { | ||
if (err) { | ||
@@ -86,3 +85,3 @@ fastify.log.error(err) | ||
fastify.listen(3000, err => { | ||
fastify.listen({ port: 3000 }, err => { | ||
if (err) { | ||
@@ -191,3 +190,3 @@ fastify.log.error(err) | ||
fastify.listen(3000, err => { | ||
fastify.listen({ port: 3000 }, err => { | ||
if (err) { | ||
@@ -202,7 +201,7 @@ fastify.log.error(err) | ||
You can optionally provide a custom errorHandler that will be used to handle any cleaning up: | ||
You can optionally provide a custom `errorHandler` that will be used to handle any cleaning up of established websocket connections. The `errorHandler` will be called if any errors are thrown by your websocket route handler after the connection has been established. Note that neither Fastify's `onError` hook or functions registered with `fastify.setErrorHandler` will be called for errors thrown during a websocket request handler. | ||
Neither the `errorHandler` passed to this plugin or fastify's `onError` hook will be called for errors encountered during message processing for your connection. If you want to handle unexpected errors within your `message` event handlers, you'll need to use your own `try { } catch {}` statements and decide what to send back over the websocket. | ||
```js | ||
'use strict' | ||
const fastify = require('fastify')() | ||
@@ -234,3 +233,3 @@ | ||
fastify.listen(3000, err => { | ||
fastify.listen({ port: 3000 }, err => { | ||
if (err) { | ||
@@ -243,2 +242,3 @@ fastify.log.error(err) | ||
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. | ||
## Options | ||
@@ -245,0 +245,0 @@ |
@@ -96,3 +96,3 @@ 'use strict' | ||
fastify.get('/echo', { websocket: true }, (conn, request) => { | ||
t.teardown(conn.destroy.bind(conn)) | ||
t.fail() | ||
}) | ||
@@ -104,5 +104,3 @@ }) | ||
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo') | ||
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' }) | ||
t.teardown(client.destroy.bind(client)) | ||
ws.on('close', code => t.equal(code, 1006)) | ||
ws.on('unexpected-response', (_request, response) => t.equal(response.statusCode, 500)) | ||
}) | ||
@@ -135,5 +133,3 @@ }) | ||
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo') | ||
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' }) | ||
t.teardown(client.destroy.bind(client)) | ||
ws.on('close', code => t.equal(code, 1006)) | ||
ws.on('unexpected-response', (_request, response) => t.equal(response.statusCode, 500)) | ||
}) | ||
@@ -158,3 +154,3 @@ }) | ||
t.ok('called', 'onError') | ||
await reply.code(404).send('there was an error') | ||
await reply.code(501).send('there was an error') | ||
}) | ||
@@ -170,8 +166,38 @@ | ||
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo') | ||
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' }) | ||
t.teardown(client.destroy.bind(client)) | ||
ws.on('close', code => t.equal(code, 1006)) | ||
ws.on('unexpected-response', (_request, response) => t.equal(response.statusCode, 501)) | ||
}) | ||
}) | ||
test('setErrorHandler functions can send a reply and prevent hijacking', t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
t.teardown(() => fastify.close()) | ||
fastify.register(fastifyWebsocket) | ||
fastify.register(async function (fastify) { | ||
fastify.addHook('preValidation', async (request, reply) => { | ||
await Promise.resolve() | ||
throw new Error('Fail') | ||
}) | ||
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) => { | ||
t.fail() | ||
}) | ||
}) | ||
fastify.listen({ port: 0 }, function (err) { | ||
t.error(err) | ||
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo') | ||
ws.on('unexpected-response', (_request, response) => t.equal(response.statusCode, 501)) | ||
}) | ||
}) | ||
test('Should not run onError hook if reply was already hijacked (error thrown in websocket handler)', t => { | ||
@@ -178,0 +204,0 @@ t.plan(2) |
Sorry, the diff of this file is not supported yet
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
75863
1716
+ Addedfastify-plugin@4.5.1(transitive)
- Removedfastify-plugin@3.0.1(transitive)
Updatedfastify-plugin@^4.0.0