Skip to content

Commit 830bc99

Browse files
committed
Adding connectionOptions as a plugin option to enable readableObjectMode and other Duplex options
1 parent 1705777 commit 830bc99

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IncomingMessage, ServerResponse, Server } from 'http';
33
import { FastifyRequest, FastifyPluginCallback, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RequestGenericInterface, ContextConfigDefault, FastifyInstance } from 'fastify';
44
import * as fastify from 'fastify';
55
import * as WebSocket from 'ws';
6-
import { Duplex } from 'stream';
6+
import { Duplex, DuplexOptions } from 'stream';
77
import { FastifyReply } from 'fastify/types/reply';
88
import { RouteGenericInterface } from 'fastify/types/route';
99

@@ -59,6 +59,7 @@ export interface SocketStream extends Duplex {
5959
export interface WebsocketPluginOptions {
6060
errorHandler?: (this: FastifyInstance, error: Error, connection: SocketStream, request: FastifyRequest, reply: FastifyReply) => void;
6161
options?: WebSocketServerOptions;
62+
connectionOptions?: DuplexOptions;
6263
}
6364

6465
export interface RouteOptions<RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = fastify.FastifySchema> extends fastify.RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler>, WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric> {}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function fastifyWebsocket (fastify, opts, next) {
5858
wss.handleUpgrade(rawRequest, rawRequest[kWs], rawRequest[kWsHead], (socket) => {
5959
wss.emit('connection', socket, rawRequest)
6060

61-
const connection = WebSocket.createWebSocketStream(socket)
61+
const connection = WebSocket.createWebSocketStream(socket, opts.connectionOptions)
6262
connection.socket = socket
6363

6464
connection.socket.on('newListener', event => {

test/base.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,41 @@ test('Should be able to pass clientTracking option in false to websocket-stream'
304304
})
305305
})
306306

307+
test('Should be able to pass custom connectionOptions to createWebSocketStream', (t) => {
308+
t.plan(3)
309+
310+
const fastify = Fastify()
311+
t.teardown(() => fastify.close())
312+
313+
const connectionOptions = {
314+
readableObjectMode: true
315+
}
316+
317+
fastify.register(fastifyWebsocket, { connectionOptions })
318+
319+
fastify.get('/', { websocket: true }, (connection, request) => {
320+
t.equal(connection.readableObjectMode, true)
321+
connection.socket.binaryType = 'arraybuffer'
322+
323+
connection.once('data', (chunk) => {
324+
const message = new TextDecoder().decode(chunk)
325+
t.equal(message, 'Hello')
326+
})
327+
t.teardown(() => connection.destroy())
328+
})
329+
330+
fastify.listen(0, (err) => {
331+
t.error(err)
332+
333+
const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
334+
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
335+
t.teardown(() => client.destroy())
336+
337+
client.setEncoding('utf8')
338+
client.write('Hello')
339+
})
340+
})
341+
307342
test('Should gracefully close with a connected client', (t) => {
308343
t.plan(6)
309344

0 commit comments

Comments
 (0)