Skip to content

Commit 2236ed9

Browse files
committed
[major] Remove the upgradeReq property
The `http.IncomingMessage` object, instead of being attached to the `WebSocket` object, is passes as the second argument to the `connection` event.
1 parent abd27fe commit 2236ed9

File tree

5 files changed

+15
-31
lines changed

5 files changed

+15
-31
lines changed

doc/ws.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ provided.
8989
### Event: 'connection'
9090

9191
- `socket` {WebSocket}
92+
- `request` {http.IncomingMessage}
9293

93-
Emitted when the handshake is complete. `socket` is an instance of `WebSocket`.
94+
Emitted when the handshake is complete. `request` is the http GET request sent
95+
by the client. Useful for parsing authority headers, cookie headers, and other
96+
information.
9497

9598
### Event: 'error'
9699

@@ -423,13 +426,6 @@ Send `data` through the connection.
423426

424427
Forcibly close the connection.
425428

426-
### websocket.upgradeReq
427-
428-
- {http.IncomingMessage}
429-
430-
The http GET request sent by the client. Useful for parsing authority headers,
431-
cookie headers, and other information. This is only available for server clients.
432-
433429
### websocket.url
434430

435431
- {String}

lib/WebSocket.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class WebSocket extends EventEmitter {
6565
this._ultron = null;
6666

6767
if (Array.isArray(address)) {
68-
initAsServerClient.call(this, address[0], address[1], address[2], options);
68+
initAsServerClient.call(this, address[0], address[1], options);
6969
} else {
7070
initAsClient.call(this, address, protocols, options);
7171
}
@@ -455,13 +455,12 @@ module.exports = WebSocket;
455455
* @param {String} options.protocol The chosen subprotocol
456456
* @private
457457
*/
458-
function initAsServerClient (req, socket, head, options) {
458+
function initAsServerClient (socket, head, options) {
459459
this.protocolVersion = options.protocolVersion;
460460
this.extensions = options.extensions;
461461
this.maxPayload = options.maxPayload;
462462
this.protocol = options.protocol;
463463

464-
this.upgradeReq = req;
465464
this._isServer = true;
466465

467466
this.setSocket(socket, head);

lib/WebSocketServer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class WebSocketServer extends EventEmitter {
8686
this._ultron.on('upgrade', (req, socket, head) => {
8787
this.handleUpgrade(req, socket, head, (client) => {
8888
this.emit(`connection${req.url}`, client);
89-
this.emit('connection', client);
89+
this.emit('connection', client, req);
9090
});
9191
});
9292
}
@@ -256,7 +256,7 @@ class WebSocketServer extends EventEmitter {
256256

257257
socket.write(headers.concat('', '').join('\r\n'));
258258

259-
const client = new WebSocket([req, socket, head], null, {
259+
const client = new WebSocket([socket, head], null, {
260260
maxPayload: this.options.maxPayload,
261261
protocolVersion: version,
262262
extensions,

test/WebSocket.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ describe('WebSocket', function () {
7777
});
7878
});
7979

80-
wss.on('connection', (ws) => {
81-
assert.strictEqual(ws.upgradeReq.connection.remoteAddress, '127.0.0.2');
80+
wss.on('connection', (ws, req) => {
81+
assert.strictEqual(req.connection.remoteAddress, '127.0.0.2');
8282
wss.close(done);
8383
});
8484
});
@@ -95,8 +95,8 @@ describe('WebSocket', function () {
9595
const ws = new WebSocket(`ws://localhost:${port}`, { family: 6 });
9696
});
9797

98-
wss.on('connection', (ws) => {
99-
assert.strictEqual(ws.upgradeReq.connection.remoteAddress, '::1');
98+
wss.on('connection', (ws, req) => {
99+
assert.strictEqual(req.connection.remoteAddress, '::1');
100100
wss.close(done);
101101
});
102102
});

test/WebSocketServer.test.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ describe('WebSocketServer', function () {
9090
server.listen(sockPath, () => {
9191
const wss = new WebSocketServer({ server });
9292

93-
wss.on('connection', (ws) => {
93+
wss.on('connection', (ws, req) => {
9494
if (wss.clients.size === 1) {
95-
assert.strictEqual(ws.upgradeReq.url, '/foo?bar=bar');
95+
assert.strictEqual(req.url, '/foo?bar=bar');
9696
} else {
97-
assert.strictEqual(ws.upgradeReq.url, '/');
97+
assert.strictEqual(req.url, '/');
9898
wss.close();
9999
server.close(done);
100100
}
@@ -940,17 +940,6 @@ describe('WebSocketServer', function () {
940940
wss.close(done);
941941
});
942942
});
943-
944-
it('upgradeReq is the original request object', function (done) {
945-
const wss = new WebSocketServer({ port: ++port }, () => {
946-
const ws = new WebSocket(`ws://localhost:${port}`, { protocolVersion: 8 });
947-
});
948-
949-
wss.on('connection', (client) => {
950-
assert.strictEqual(client.upgradeReq.httpVersion, '1.1');
951-
wss.close(done);
952-
});
953-
});
954943
});
955944

956945
describe('permessage-deflate', function () {

0 commit comments

Comments
 (0)