Skip to content

Commit

Permalink
Update for Fastify v5 (#293)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Gürgün Dayıoğlu <[email protected]>
  • Loading branch information
synapse and gurgunday authored May 10, 2024
1 parent ef9aa09 commit 486276a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 deletions .taprc
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
ts: false
jsx: false
flow: false
coverage: true
jobs: 1

files:
- test/**/*.test.js
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
53 changes: 45 additions & 8 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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())

Expand All @@ -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())

Expand All @@ -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
})
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 486276a

Please sign in to comment.