Skip to content

Commit d4ab7f4

Browse files
Update to @fastify/websocket v10 (#1089)
* Update to @fastify/websocket v10 Signed-off-by: Matteo Collina <[email protected]> * drop node 16 Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]> * Update index.d.ts Co-authored-by: Jonny Green <[email protected]> * double test timeout Signed-off-by: Matteo Collina <[email protected]> --------- Signed-off-by: Matteo Collina <[email protected]> Co-authored-by: Jonny Green <[email protected]>
1 parent 8130ba9 commit d4ab7f4

File tree

6 files changed

+15
-17
lines changed

6 files changed

+15
-17
lines changed

.taprc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ ts: false
22
jsx: false
33
flow: false
44
coverage: true
5-
timeout: 60
5+
timeout: 120

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
FormattedExecutionResult,
1717
ParseOptions,
1818
} from "graphql";
19-
import { SocketStream } from "@fastify/websocket"
19+
import type { WebSocket } from "ws";
2020
import { IncomingMessage, OutgoingHttpHeaders } from "http";
2121
import { Readable } from "stream";
2222

@@ -458,7 +458,7 @@ declare namespace mercurius {
458458
) => void
459459
) => void;
460460
context?: (
461-
connection: SocketStream,
461+
socket: WebSocket,
462462
request: FastifyRequest
463463
) => Record<string, any> | Promise<Record<string, any>>;
464464
onConnect?: (data: {

lib/subscription.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ const SubscriptionConnection = require('./subscription-connection')
77
const { getProtocolByName } = require('./subscription-protocol')
88

99
function createConnectionHandler ({ subscriber, fastify, onConnect, onDisconnect, entityResolversFactory, subscriptionContextFn, keepAlive, fullWsTransport, errorFormatter }) {
10-
return async (connection, request) => {
11-
const { socket } = connection
12-
10+
return async (socket, request) => {
1311
if (socket.protocol === undefined || getProtocolByName(socket.protocol) === null) {
1412
request.log.warn('wrong websocket protocol')
1513
// Close the connection with an error code, ws v2 ensures that the
@@ -35,7 +33,7 @@ function createConnectionHandler ({ subscriber, fastify, onConnect, onDisconnect
3533
let resolveContext
3634

3735
if (subscriptionContextFn) {
38-
resolveContext = () => subscriptionContextFn(connection, request)
36+
resolveContext = () => subscriptionContextFn(socket, request)
3937
}
4038

4139
const subscriptionConnection = new SubscriptionConnection(socket, {
@@ -52,10 +50,10 @@ function createConnectionHandler ({ subscriber, fastify, onConnect, onDisconnect
5250
})
5351

5452
/* istanbul ignore next */
55-
connection.socket.on('error', () => {
53+
socket.on('error', () => {
5654
subscriptionConnection.close()
5755
})
58-
connection.socket.on('close', () => {
56+
socket.on('close', () => {
5957
subscriptionConnection.close()
6058
})
6159
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"dependencies": {
6161
"@fastify/error": "^3.0.0",
6262
"@fastify/static": "^7.0.0",
63-
"@fastify/websocket": "^8.0.0",
63+
"@fastify/websocket": "^10.0.0",
6464
"fastify-plugin": "^4.2.0",
6565
"graphql-jit": "0.8.4",
6666
"mqemitter": "^5.0.0",

test/subscription.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,9 +1968,9 @@ test('subscription server works with fastify websocket', t => {
19681968
})
19691969

19701970
app.register(async function (app) {
1971-
app.get('/fastify-websocket', { websocket: true }, (connection, req) => {
1972-
connection.socket.on('message', message => {
1973-
connection.socket.send('hi from server')
1971+
app.get('/fastify-websocket', { websocket: true }, (socket, req) => {
1972+
socket.on('message', message => {
1973+
socket.send('hi from server')
19741974
})
19751975
})
19761976
})

test/types/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ app.register(mercurius, {
293293
next(true)
294294
next(false)
295295
},
296-
context: (connection, request) => {
297-
connection.socket
296+
context: (socket, request) => {
297+
socket.send('hello')
298298
request.headers
299299
return {}
300300
},
@@ -319,8 +319,8 @@ app.register(mercurius, {
319319
schema,
320320
resolvers,
321321
subscription: {
322-
context: async (connection, request) => {
323-
connection.socket
322+
context: async (socket, request) => {
323+
socket.send('hello')
324324
request.headers
325325
return {}
326326
},

0 commit comments

Comments
 (0)