Skip to content
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
9 changes: 7 additions & 2 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,13 @@ class WebSocket extends EventEmitter {
receiver.on('ping', receiverOnPing);
receiver.on('pong', receiverOnPong);

socket.setTimeout(0);
socket.setNoDelay();
// These methods may not be available if `socket` is actually just a stream:
if (socket.setTimeout) {
socket.setTimeout(0);
}
if (socket.setNoDelay) {
socket.setNoDelay();
}

if (head.length > 0) socket.unshift(head);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"mocha": "^8.4.0",
"native-duplexpair": "^1.0.0",
"nyc": "^15.0.0",
"prettier": "^3.0.0",
"utf-8-validate": "^6.0.0"
Expand Down
30 changes: 30 additions & 0 deletions test/websocket-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path');
const net = require('net');
const fs = require('fs');
const os = require('os');
const DuplexPair = require('native-duplexpair');

const Sender = require('../lib/sender');
const WebSocket = require('..');
Expand Down Expand Up @@ -514,6 +515,35 @@ describe('WebSocketServer', () => {
});
});
});

it('can complete a WebSocket upgrade over any duplex stream', (done) => {
const server = http.createServer();

server.listen(0, () => {
const wss = new WebSocket.Server({ noServer: true });

server.on('upgrade', (req, socket, head) => {
// Put a stream between the raw socket and our websocket processing:
const { socket1: stream1, socket2: stream2 } = new DuplexPair();
socket.pipe(stream1);
stream1.pipe(socket);

// Pass the other side of the stream as the socket to upgrade:
wss.handleUpgrade(req, stream2, head, (ws) => {
ws.send('hello');
ws.close();
});
});

const ws = new WebSocket(`ws://localhost:${server.address().port}`);

ws.on('message', (message, isBinary) => {
assert.deepStrictEqual(message, Buffer.from('hello'));
assert.ok(!isBinary);
server.close(done);
});
});
});
});

describe('#completeUpgrade', () => {
Expand Down