From d4ab7f43d1e3d744733dc8479ef89f5acf3ca2b2 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 19 Mar 2024 11:35:05 +0100 Subject: [PATCH] Update to @fastify/websocket v10 (#1089) * Update to @fastify/websocket v10 Signed-off-by: Matteo Collina * drop node 16 Signed-off-by: Matteo Collina * fixup Signed-off-by: Matteo Collina * Update index.d.ts Co-authored-by: Jonny Green * double test timeout Signed-off-by: Matteo Collina --------- Signed-off-by: Matteo Collina Co-authored-by: Jonny Green --- .taprc | 2 +- index.d.ts | 4 ++-- lib/subscription.js | 10 ++++------ package.json | 2 +- test/subscription.js | 6 +++--- test/types/index.ts | 8 ++++---- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/.taprc b/.taprc index 6782c41f..611421b0 100644 --- a/.taprc +++ b/.taprc @@ -2,4 +2,4 @@ ts: false jsx: false flow: false coverage: true -timeout: 60 +timeout: 120 diff --git a/index.d.ts b/index.d.ts index 2610c694..55ded720 100644 --- a/index.d.ts +++ b/index.d.ts @@ -16,7 +16,7 @@ import { FormattedExecutionResult, ParseOptions, } from "graphql"; -import { SocketStream } from "@fastify/websocket" +import type { WebSocket } from "ws"; import { IncomingMessage, OutgoingHttpHeaders } from "http"; import { Readable } from "stream"; @@ -458,7 +458,7 @@ declare namespace mercurius { ) => void ) => void; context?: ( - connection: SocketStream, + socket: WebSocket, request: FastifyRequest ) => Record | Promise>; onConnect?: (data: { diff --git a/lib/subscription.js b/lib/subscription.js index c35cb590..4b8ebd38 100644 --- a/lib/subscription.js +++ b/lib/subscription.js @@ -7,9 +7,7 @@ const SubscriptionConnection = require('./subscription-connection') const { getProtocolByName } = require('./subscription-protocol') function createConnectionHandler ({ subscriber, fastify, onConnect, onDisconnect, entityResolversFactory, subscriptionContextFn, keepAlive, fullWsTransport, errorFormatter }) { - return async (connection, request) => { - const { socket } = connection - + return async (socket, request) => { if (socket.protocol === undefined || getProtocolByName(socket.protocol) === null) { request.log.warn('wrong websocket protocol') // Close the connection with an error code, ws v2 ensures that the @@ -35,7 +33,7 @@ function createConnectionHandler ({ subscriber, fastify, onConnect, onDisconnect let resolveContext if (subscriptionContextFn) { - resolveContext = () => subscriptionContextFn(connection, request) + resolveContext = () => subscriptionContextFn(socket, request) } const subscriptionConnection = new SubscriptionConnection(socket, { @@ -52,10 +50,10 @@ function createConnectionHandler ({ subscriber, fastify, onConnect, onDisconnect }) /* istanbul ignore next */ - connection.socket.on('error', () => { + socket.on('error', () => { subscriptionConnection.close() }) - connection.socket.on('close', () => { + socket.on('close', () => { subscriptionConnection.close() }) } diff --git a/package.json b/package.json index 1fbcb913..8d0a2f48 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "dependencies": { "@fastify/error": "^3.0.0", "@fastify/static": "^7.0.0", - "@fastify/websocket": "^8.0.0", + "@fastify/websocket": "^10.0.0", "fastify-plugin": "^4.2.0", "graphql-jit": "0.8.4", "mqemitter": "^5.0.0", diff --git a/test/subscription.js b/test/subscription.js index 99dd7ccc..12d1c290 100644 --- a/test/subscription.js +++ b/test/subscription.js @@ -1968,9 +1968,9 @@ test('subscription server works with fastify websocket', t => { }) app.register(async function (app) { - app.get('/fastify-websocket', { websocket: true }, (connection, req) => { - connection.socket.on('message', message => { - connection.socket.send('hi from server') + app.get('/fastify-websocket', { websocket: true }, (socket, req) => { + socket.on('message', message => { + socket.send('hi from server') }) }) }) diff --git a/test/types/index.ts b/test/types/index.ts index 3c86cde9..b941f5ba 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -293,8 +293,8 @@ app.register(mercurius, { next(true) next(false) }, - context: (connection, request) => { - connection.socket + context: (socket, request) => { + socket.send('hello') request.headers return {} }, @@ -319,8 +319,8 @@ app.register(mercurius, { schema, resolvers, subscription: { - context: async (connection, request) => { - connection.socket + context: async (socket, request) => { + socket.send('hello') request.headers return {} },