From 486276a13a0e4759eb8a96857325e8807c0630a6 Mon Sep 17 00:00:00 2001 From: Cristian Barlutiu Date: Fri, 10 May 2024 10:03:01 +0200 Subject: [PATCH] Update for Fastify v5 (#293) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update for Fastify v5 * reverted back snippet to original code * recreate taprc * remove console.log * added check to ensure websocket was still active before closing --------- Signed-off-by: Gürgün Dayıoğlu Co-authored-by: Gürgün Dayıoğlu --- .github/workflows/ci.yml | 2 +- .taprc | 5 ---- package.json | 20 +++++++-------- test/base.test.js | 53 ++++++++++++++++++++++++++++++++++------ 4 files changed, 56 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0610c55..582175c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ on: jobs: test: - uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3 + uses: fastify/workflows/.github/workflows/plugins-ci.yml@v4.1.0 with: lint: true license-check: true diff --git a/.taprc b/.taprc index fec4f69..f22a32e 100644 --- a/.taprc +++ b/.taprc @@ -1,8 +1,3 @@ -ts: false -jsx: false -flow: false -coverage: true jobs: 1 - files: - test/**/*.test.js diff --git a/package.json b/package.json index dfe93e0..24a8035 100644 --- a/package.json +++ b/package.json @@ -26,21 +26,21 @@ }, "homepage": "https://github.com/fastify/fastify-websocket#readme", "devDependencies": { - "@fastify/pre-commit": "^2.0.2", + "@fastify/pre-commit": "^2.1.0", "@fastify/type-provider-typebox": "^4.0.0", - "@types/node": "^20.1.0", - "@types/ws": "^8.2.2", - "fastify": "^4.25.0", + "@types/node": "^20.11.28", + "@types/ws": "^8.5.10", + "fastify": "^4.26.2", "fastify-tsconfig": "^2.0.0", - "split2": "^4.1.0", - "standard": "^17.0.0", - "tap": "^16.0.0", + "split2": "^4.2.0", + "standard": "^17.1.0", + "tap": "^18.7.1", "tsd": "^0.31.0" }, "dependencies": { - "duplexify": "^4.1.2", - "fastify-plugin": "^4.0.0", - "ws": "^8.0.0" + "duplexify": "^4.1.3", + "fastify-plugin": "^4.5.1", + "ws": "^8.16.0" }, "publishConfig": { "access": "public" diff --git a/test/base.test.js b/test/base.test.js index be43118..978c131 100644 --- a/test/base.test.js +++ b/test/base.test.js @@ -33,7 +33,11 @@ test('Should expose a websocket', async (t) => { await fastify.listen({ port: 0 }) const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) - t.teardown(() => ws.close()) + t.teardown(() => { + if (ws.readyState) { + ws.close() + } + }) const chunkPromise = once(ws, 'message') await once(ws, 'open') @@ -94,12 +98,18 @@ test('Should run custom errorHandler on wildcard route handler error', async (t) await fastify.listen({ port: 0 }) const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) - t.teardown(() => ws.close()) + t.teardown(() => { + if (ws.readyState) { + ws.close() + } + }) await p }) test('Should run custom errorHandler on error inside websocket handler', async (t) => { + t.plan(1) + const fastify = Fastify() t.teardown(() => fastify.close()) @@ -125,12 +135,19 @@ test('Should run custom errorHandler on error inside websocket handler', async ( await fastify.listen({ port: 0 }) const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) - t.teardown(() => ws.close()) + + t.teardown(() => { + if (ws.readyState) { + ws.close() + } + }) await p }) test('Should run custom errorHandler on error inside async websocket handler', async (t) => { + t.plan(1) + const fastify = Fastify() t.teardown(() => fastify.close()) @@ -156,7 +173,11 @@ test('Should run custom errorHandler on error inside async websocket handler', a await fastify.listen({ port: 0 }) const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) - t.teardown(() => ws.close()) + t.teardown(() => { + if (ws.readyState) { + ws.close() + } + }) await p }) @@ -188,7 +209,11 @@ test('Should be able to pass custom options to ws', async (t) => { const ws = new WebSocket('ws://localhost:' + fastify.server.address().port, clientOptions) const chunkPromise = once(ws, 'message') await once(ws, 'open') - t.teardown(() => ws.close()) + t.teardown(() => { + if (ws.readyState) { + ws.close() + } + }) ws.send('hello') @@ -228,7 +253,11 @@ test('Should warn if path option is provided to ws', async (t) => { const ws = new WebSocket('ws://localhost:' + fastify.server.address().port, clientOptions) const chunkPromise = once(ws, 'message') await once(ws, 'open') - t.teardown(() => ws.close()) + t.teardown(() => { + if (ws.readyState) { + ws.close() + } + }) ws.send('hello') @@ -269,7 +298,11 @@ test('Should be able to pass a custom server option to ws', async (t) => { const ws = new WebSocket('ws://localhost:' + externalServerPort) const chunkPromise = once(ws, 'message') await once(ws, 'open') - t.teardown(() => ws.close()) + t.teardown(() => { + if (ws.readyState) { + ws.close() + } + }) ws.send('hello') @@ -330,7 +363,11 @@ test('Should be able to pass preClose option to override default', async (t) => await fastify.listen({ port: 0 }) const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) - t.teardown(() => ws.close()) + t.teardown(() => { + if (ws.readyState) { + ws.close() + } + }) const chunkPromise = once(ws, 'message') await once(ws, 'open')