From e2411d5292779e5d26619fedec44ef820c0b6e6f Mon Sep 17 00:00:00 2001 From: Cristian Barlutiu Date: Mon, 18 Mar 2024 17:57:59 +0100 Subject: [PATCH 1/5] Update for Fastify v5 --- .github/workflows/ci.yml | 2 +- .taprc | 8 -------- package.json | 28 ++++++++++++++++------------ test/base.test.js | 22 +++++++++++++--------- 4 files changed, 30 insertions(+), 30 deletions(-) delete mode 100644 .taprc 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 deleted file mode 100644 index fec4f69..0000000 --- a/.taprc +++ /dev/null @@ -1,8 +0,0 @@ -ts: false -jsx: false -flow: false -coverage: true -jobs: 1 - -files: - - test/**/*.test.js diff --git a/package.json b/package.json index 587f0b1..53173dc 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", - "tsd": "^0.30.1" + "split2": "^4.2.0", + "standard": "^17.1.0", + "tap": "^18.7.1", + "tsd": "^0.30.7" }, "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" @@ -48,5 +48,9 @@ "pre-commit": [ "lint", "test" - ] + ], + "tap": { + "jobs": 1, + "files": ["test/**/*.test.js"] + } } diff --git a/test/base.test.js b/test/base.test.js index e03a14a..492006c 100644 --- a/test/base.test.js +++ b/test/base.test.js @@ -90,7 +90,6 @@ test('Should run custom errorHandler on wildcard route handler error', async (t) fastify.get('/*', { websocket: true }, (conn) => { conn.pipe(conn) - t.teardown(() => conn.destroy()) return Promise.reject(new Error('Fail')) }) @@ -98,11 +97,14 @@ test('Should run custom errorHandler on wildcard route handler error', async (t) const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' }) - t.teardown(() => client.destroy()) await p + + t.teardown(() => client.destroy()) }) test('Should run custom errorHandler on error inside websocket handler', async (t) => { + t.plan(1) + const fastify = Fastify() t.teardown(() => fastify.close()) @@ -122,29 +124,30 @@ test('Should run custom errorHandler on error inside websocket handler', async ( fastify.get('/', { websocket: true }, function wsHandler (conn) { conn.pipe(conn) - t.teardown(() => conn.destroy()) throw new Error('Fail') }) 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()) await p + + t.teardown(() => client.destroy()) }) test('Should run custom errorHandler on error inside async websocket handler', async (t) => { + t.plan(1) + const fastify = Fastify() t.teardown(() => fastify.close()) let _resolve - const p = new Promise((resolve) => { - _resolve = resolve - }) + const promise = new Promise((resolve) => { _resolve = resolve }) const options = { errorHandler: function (error) { + console.log('Error received') t.equal(error.message, 'Fail') _resolve() } @@ -154,15 +157,16 @@ test('Should run custom errorHandler on error inside async websocket handler', a fastify.get('/', { websocket: true }, async function wsHandler (conn) { conn.pipe(conn) - t.teardown(() => conn.destroy()) throw new Error('Fail') }) await fastify.listen({ port: 0 }) const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' }) + + await promise + t.teardown(() => client.destroy()) - await p }) test('Should be able to pass custom options to websocket-stream', async (t) => { From 3b39d88c5c1d5a37adbe4a572bba0403dfc423dc Mon Sep 17 00:00:00 2001 From: Cristian Barlutiu Date: Mon, 18 Mar 2024 18:04:42 +0100 Subject: [PATCH 2/5] reverted back snippet to original code --- test/base.test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/base.test.js b/test/base.test.js index 492006c..ad01e1b 100644 --- a/test/base.test.js +++ b/test/base.test.js @@ -143,7 +143,9 @@ test('Should run custom errorHandler on error inside async websocket handler', a t.teardown(() => fastify.close()) let _resolve - const promise = new Promise((resolve) => { _resolve = resolve }) + const p = new Promise((resolve) => { + _resolve = resolve + }) const options = { errorHandler: function (error) { @@ -164,7 +166,7 @@ test('Should run custom errorHandler on error inside async websocket handler', a const ws = new WebSocket('ws://localhost:' + fastify.server.address().port) const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' }) - await promise + await p t.teardown(() => client.destroy()) }) From f46d1bf93db61aae0a263661242cf4c134f03942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Mon, 15 Apr 2024 20:23:13 +0200 Subject: [PATCH 3/5] recreate taprc --- .taprc | 3 +++ package.json | 6 +----- 2 files changed, 4 insertions(+), 5 deletions(-) create mode 100644 .taprc diff --git a/.taprc b/.taprc new file mode 100644 index 0000000..f22a32e --- /dev/null +++ b/.taprc @@ -0,0 +1,3 @@ +jobs: 1 +files: + - test/**/*.test.js diff --git a/package.json b/package.json index 53173dc..d1edbcb 100644 --- a/package.json +++ b/package.json @@ -48,9 +48,5 @@ "pre-commit": [ "lint", "test" - ], - "tap": { - "jobs": 1, - "files": ["test/**/*.test.js"] - } + ] } From 0752b26ac40627d14460513415886ff67ca2a48a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Tue, 16 Apr 2024 15:36:46 +0200 Subject: [PATCH 4/5] remove console.log --- test/base.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/base.test.js b/test/base.test.js index 7790e73..b3df057 100644 --- a/test/base.test.js +++ b/test/base.test.js @@ -146,7 +146,6 @@ test('Should run custom errorHandler on error inside async websocket handler', a const options = { errorHandler: function (error) { - console.log('Error received') t.equal(error.message, 'Fail') _resolve() } From 01b1cb649ad91603ee48b1351a19130332ef0c3f Mon Sep 17 00:00:00 2001 From: Cristian Barlutiu Date: Thu, 9 May 2024 12:20:22 +0200 Subject: [PATCH 5/5] added check to ensure websocket was still active before closing --- test/base.test.js | 48 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/test/base.test.js b/test/base.test.js index b3df057..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,7 +98,11 @@ 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 }) @@ -128,7 +136,11 @@ 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 }) @@ -161,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 }) @@ -193,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') @@ -233,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') @@ -274,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') @@ -335,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')