Skip to content

Commit 8e6cd35

Browse files
authored
Merge pull request #186 from aelnaiem/master
Adding connectionOptions as a plugin option to enable Duplex options
2 parents 1705777 + 7cbc00b commit 8e6cd35

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ fastify.listen(3000, err => {
238238

239239
`fastify-websocket` accept these options for [`ws`](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback) :
240240

241-
- `objectMode` - Send each chunk on its own, and do not try to pack them in a single websocket frame.
242241
- `host` - The hostname where to bind the server.
243242
- `port` - The port where to bind the server.
244243
- `backlog` - The maximum length of the queue of pending connections.
@@ -257,6 +256,16 @@ _**NB** The `path` option from `ws` should not be provided since the routing is
257256

258257
_**NB** The `noServer` option from `ws` should not be provided since the point of fastify-websocket is to listen on the fastify server. If you want a custom server, you can use the `server` option, and if you want more control, you can use the `ws` library directly_
259258

259+
You can also pass the following as `connectionOptions` for [createWebSocketStream](https://github.com/websockets/ws/blob/master/doc/ws.md#createwebsocketstreamwebsocket-options).
260+
261+
- `allowHalfOpen` <boolean> If set to false, then the stream will automatically end the writable side when the readable side ends. Default: true.
262+
- `readable` <boolean> Sets whether the Duplex should be readable. Default: true.
263+
- `writable` <boolean> Sets whether the Duplex should be writable. Default: true.
264+
- `readableObjectMode` <boolean> Sets objectMode for readable side of the stream. Has no effect if objectMode is true. Default: false.
265+
- `readableHighWaterMark` <number> Sets highWaterMark for the readable side of the stream.
266+
- `writableHighWaterMark` <number> Sets highWaterMark for the writable side of the stream.
267+
268+
[ws](https://github.com/websockets/ws) does not allow you to set `objectMode` or `writableObjectMode` to true
260269
## Acknowledgements
261270

262271
This project is kindly sponsored by [nearForm](https://nearform.com).

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const http = require('http')
4+
const util = require('util')
45
const split = require('split2')
56
const test = require('tap').test
67
const Fastify = require('fastify')
@@ -304,6 +305,46 @@ test('Should be able to pass clientTracking option in false to websocket-stream'
304305
})
305306
})
306307

308+
test('Should be able to pass custom connectionOptions to createWebSocketStream', (t) => {
309+
t.plan(3)
310+
311+
const fastify = Fastify()
312+
t.teardown(() => fastify.close())
313+
314+
const connectionOptions = {
315+
readableObjectMode: true
316+
}
317+
318+
fastify.register(fastifyWebsocket, { connectionOptions })
319+
320+
fastify.get('/', { websocket: true }, (connection, request) => {
321+
// readableObjectMode was added in Node v12.3.0 so for earlier versions
322+
// we check the encapsulated readable state directly
323+
const mode = (typeof connection.readableObjectMode === 'undefined')
324+
? connection._readableState.objectMode
325+
: connection.readableObjectMode
326+
t.equal(mode, true)
327+
connection.socket.binaryType = 'arraybuffer'
328+
329+
connection.once('data', (chunk) => {
330+
const message = new util.TextDecoder().decode(chunk)
331+
t.equal(message, 'Hello')
332+
})
333+
t.teardown(() => connection.destroy())
334+
})
335+
336+
fastify.listen(0, (err) => {
337+
t.error(err)
338+
339+
const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
340+
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
341+
t.teardown(() => client.destroy())
342+
343+
client.setEncoding('utf8')
344+
client.write('Hello')
345+
})
346+
})
347+
307348
test('Should gracefully close with a connected client', (t) => {
308349
t.plan(6)
309350

0 commit comments

Comments
 (0)