Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to @fastify/websocket v10 #1089

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
"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.0",
"graphql-jit": "0.8.4",
"mqemitter": "^5.0.0",
"p-map": "^4.0.0",
"readable-stream": "^4.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
Loading