Skip to content

Commit

Permalink
Update to @fastify/websocket v10 (#1089)
Browse files Browse the repository at this point in the history
* Update to @fastify/websocket v10

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* drop node 16

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* fixup

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* Update index.d.ts

Co-authored-by: Jonny Green <hello@jonnydgreen.com>

* double test timeout

Signed-off-by: Matteo Collina <hello@matteocollina.com>

---------

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Co-authored-by: Jonny Green <hello@jonnydgreen.com>
  • Loading branch information
mcollina and jonnydgreen committed Mar 19, 2024
1 parent 8130ba9 commit d4ab7f4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .taprc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ ts: false
jsx: false
flow: false
coverage: true
timeout: 60
timeout: 120
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -458,7 +458,7 @@ declare namespace mercurius {
) => void
) => void;
context?: (
connection: SocketStream,
socket: WebSocket,
request: FastifyRequest
) => Record<string, any> | Promise<Record<string, any>>;
onConnect?: (data: {
Expand Down
10 changes: 4 additions & 6 deletions lib/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, {
Expand All @@ -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()
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions test/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
Expand Down
8 changes: 4 additions & 4 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
},
Expand All @@ -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 {}
},
Expand Down

0 comments on commit d4ab7f4

Please sign in to comment.