@fastify/websocket
Advanced tools
+5
-1
@@ -54,3 +54,4 @@ 'use strict' | ||
| async function injectWS (path = '/', upgradeContext = {}) { | ||
| // TODO: place upgrade context as options | ||
| async function injectWS (path = '/', upgradeContext = {}, options = {}) { | ||
| const server2Client = new PassThrough() | ||
@@ -68,3 +69,6 @@ const client2Server = new PassThrough() | ||
| typeof options.onInit === 'function' && options.onInit(ws) | ||
| ws.on('open', () => { | ||
| typeof options.onOpen === 'function' && options.onOpen(ws) | ||
| clientStream.removeListener('data', onData) | ||
@@ -71,0 +75,0 @@ resolve(ws) |
+3
-1
| MIT License | ||
| Copyright (c) 2017 Fastify | ||
| Copyright (c) 2017-present The Fastify team | ||
| The Fastify team members are listed at https://github.com/fastify/fastify#team. | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
@@ -6,0 +8,0 @@ of this software and associated documentation files (the "Software"), to deal |
+2
-2
| { | ||
| "name": "@fastify/websocket", | ||
| "version": "11.1.0", | ||
| "version": "11.2.0", | ||
| "description": "basic websocket support for fastify", | ||
@@ -61,3 +61,3 @@ "main": "index.js", | ||
| "@fastify/type-provider-typebox": "^5.0.0", | ||
| "@types/node": "^22.0.0", | ||
| "@types/node": "^24.0.9", | ||
| "@types/ws": "^8.5.10", | ||
@@ -64,0 +64,0 @@ "c8": "^10.1.3", |
+40
-0
@@ -135,1 +135,41 @@ 'use strict' | ||
| }) | ||
| test('inject hooks', async (t) => { | ||
| const fastify = buildFastify(t) | ||
| const message = 'hi from client' | ||
| let _resolve | ||
| const promise = new Promise((resolve) => { _resolve = resolve }) | ||
| fastify.register( | ||
| async function (instance) { | ||
| instance.get('/ws', { websocket: true }, function (socket) { | ||
| socket.once('message', chunk => { | ||
| _resolve(chunk.toString()) | ||
| }) | ||
| }) | ||
| }) | ||
| await fastify.ready() | ||
| let order = 0 | ||
| let initWS, openWS | ||
| const ws = await fastify.injectWS('/ws', {}, { | ||
| onInit (ws) { | ||
| t.assert.strictEqual(order, 0) | ||
| order++ | ||
| initWS = ws | ||
| }, | ||
| onOpen (ws) { | ||
| t.assert.strictEqual(order, 1) | ||
| order++ | ||
| openWS = ws | ||
| } | ||
| }) | ||
| ws.send(message) | ||
| t.assert.strictEqual(order, 2) | ||
| t.assert.deepStrictEqual(ws, initWS) | ||
| t.assert.deepStrictEqual(ws, openWS) | ||
| t.assert.deepStrictEqual(await promise, message) | ||
| ws.terminate() | ||
| }) |
+10
-5
| /// <reference types="node" /> | ||
| import { IncomingMessage, ServerResponse, Server } from 'node:http' | ||
| import { FastifyRequest, FastifyPluginCallback, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RequestGenericInterface, ContextConfigDefault, FastifyInstance, FastifySchema, FastifyTypeProvider, FastifyTypeProviderDefault, FastifyBaseLogger } from 'fastify' | ||
| import * as fastify from 'fastify' | ||
| import * as WebSocket from 'ws' | ||
| import { ContextConfigDefault, FastifyBaseLogger, FastifyInstance, FastifyPluginCallback, FastifyRequest, FastifySchema, FastifyTypeProvider, FastifyTypeProviderDefault, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, RequestGenericInterface } from 'fastify' | ||
| import { preCloseAsyncHookHandler, preCloseHookHandler } from 'fastify/types/hooks' | ||
| import { FastifyReply } from 'fastify/types/reply' | ||
| import { preCloseHookHandler, preCloseAsyncHookHandler } from 'fastify/types/hooks' | ||
| import { RouteGenericInterface } from 'fastify/types/route' | ||
| import { IncomingMessage, Server, ServerResponse } from 'node:http' | ||
| import * as WebSocket from 'ws' | ||
@@ -30,4 +30,9 @@ interface WebsocketRouteOptions< | ||
| interface InjectWSOption { | ||
| onInit?: (ws: WebSocket.WebSocket) => void | ||
| onOpen?: (ws: WebSocket.WebSocket) => void | ||
| } | ||
| type InjectWSFn<RawRequest> = | ||
| ((path?: string, upgradeContext?: Partial<RawRequest>) => Promise<WebSocket>) | ||
| ((path?: string, upgradeContext?: Partial<RawRequest>, options?: InjectWSOption) => Promise<WebSocket>) | ||
@@ -34,0 +39,0 @@ // eslint-disable-next-line @typescript-eslint/no-unused-vars |
@@ -1,10 +0,10 @@ | ||
| // eslint-disable-next-line import-x/no-named-default -- Testing default export | ||
| import fastifyWebsocket, { WebsocketHandler, fastifyWebsocket as namedFastifyWebsocket, default as defaultFastifyWebsocket, WebSocket } from '..' | ||
| import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' | ||
| import { Type } from '@sinclair/typebox' | ||
| import fastify, { FastifyBaseLogger, FastifyInstance, FastifyReply, FastifyRequest, FastifySchema, RawRequestDefaultExpression, RawServerDefault, RequestGenericInterface, RouteOptions } from 'fastify' | ||
| import { RouteGenericInterface } from 'fastify/types/route' | ||
| import type { IncomingMessage } from 'node:http' | ||
| import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface, FastifyBaseLogger, RawServerDefault, FastifySchema, RawRequestDefaultExpression } from 'fastify' | ||
| import { expectType } from 'tsd' | ||
| import { Server } from 'ws' | ||
| import { RouteGenericInterface } from 'fastify/types/route' | ||
| import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' | ||
| import { Type } from '@sinclair/typebox' | ||
| // eslint-disable-next-line import-x/no-named-default -- Test default export | ||
| import fastifyWebsocket, { default as defaultFastifyWebsocket, fastifyWebsocket as namedFastifyWebsocket, WebSocket, WebsocketHandler } from '..' | ||
@@ -25,4 +25,4 @@ const app: FastifyInstance = fastify() | ||
| app.register(fastifyWebsocket, { options: { perMessageDeflate: true } }) | ||
| app.register(fastifyWebsocket, { preClose: function syncPreclose () {} }) | ||
| app.register(fastifyWebsocket, { preClose: async function asyncPreclose () {} }) | ||
| app.register(fastifyWebsocket, { preClose: function syncPreclose () { } }) | ||
| app.register(fastifyWebsocket, { preClose: async function asyncPreclose () { } }) | ||
@@ -93,3 +93,3 @@ app.get('/websockets-via-inferrence', { websocket: true }, async function (socket, request) { | ||
| expectType<{ search: string }>(request.query) | ||
| expectType< IncomingMessage['headers'] & { auth: string }>(request.headers) | ||
| expectType<IncomingMessage['headers'] & { auth: string }>(request.headers) | ||
| }) | ||
@@ -167,1 +167,13 @@ | ||
| expectType<typeof fastifyWebsocket>(defaultFastifyWebsocket) | ||
| app.injectWS('/', {}, {}) | ||
| app.injectWS('/', {}, { | ||
| onInit (ws) { | ||
| expectType<WebSocket>(ws) | ||
| }, | ||
| }) | ||
| app.injectWS('/', {}, { | ||
| onOpen (ws) { | ||
| expectType<WebSocket>(ws) | ||
| }, | ||
| }) |
Network access
Supply chain riskThis module accesses the network.
Network access
Supply chain riskThis module accesses the network.
93071
1.68%2114
2.57%