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

[fix] ensure handleUpgrade doesn't break if socket is just a stream #2165

Merged
merged 2 commits into from
Aug 31, 2023
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.
Jump to
Jump to file
Failed to load files.
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