Skip to content

Commit 824be55

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 61ed4b2 commit 824be55

File tree

7 files changed

+24
-42
lines changed

7 files changed

+24
-42
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ app.use(function (req, res) {
197197
const server = http.createServer(app);
198198
const wss = new WebSocket.Server({ server });
199199

200-
wss.on('connection', function connection(ws) {
201-
const location = url.parse(ws.upgradeReq.url, true);
200+
wss.on('connection', function connection(ws, req) {
201+
const location = url.parse(req.url, true);
202202
// You might use location.query.access_token to authenticate or share sessions
203-
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
203+
// or req.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
204204

205205
ws.on('message', function incoming(message) {
206206
console.log('received: %s', message);
@@ -280,17 +280,17 @@ const WebSocket = require('ws');
280280

281281
const wss = new WebSocket.Server({ port: 8080 });
282282

283-
wss.on('connection', function connection(ws) {
284-
const ip = ws.upgradeReq.connection.remoteAddress;
283+
wss.on('connection', function connection(ws, req) {
284+
const ip = req.connection.remoteAddress;
285285
});
286286
```
287287

288288
When the server runs behing a proxy like NGINX, the de-facto standard is to use
289289
the `X-Forwarded-For` header.
290290

291291
```js
292-
wss.on('connection', function connection(ws) {
293-
const ip = ws.upgradeReq.headers['x-forwarded-for'];
292+
wss.on('connection', function connection(ws, req) {
293+
const ip = req.headers['x-forwarded-for'];
294294
});
295295
```
296296

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}

examples/express-session-parse/index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ const wss = new WebSocket.Server({
6363
server
6464
});
6565

66-
wss.on('connection', (ws) => {
66+
wss.on('connection', (ws, req) => {
6767
ws.on('message', (message) => {
68-
const session = ws.upgradeReq.session;
69-
7068
//
7169
// Here we can now use session parameters.
7270
//
73-
console.log(`WS message ${message} from user ${session.userId}`);
71+
console.log(`WS message ${message} from user ${req.session.userId}`);
7472
});
7573
});
7674

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
@@ -85,7 +85,7 @@ class WebSocketServer extends EventEmitter {
8585
this._ultron.on('error', (err) => this.emit('error', err));
8686
this._ultron.on('upgrade', (req, socket, head) => {
8787
this.handleUpgrade(req, socket, head, (client) => {
88-
this.emit('connection', client);
88+
this.emit('connection', client, req);
8989
});
9090
});
9191
}
@@ -255,7 +255,7 @@ class WebSocketServer extends EventEmitter {
255255

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

258-
const client = new WebSocket([req, socket, head], null, {
258+
const client = new WebSocket([socket, head], null, {
259259
maxPayload: this.options.maxPayload,
260260
protocolVersion: version,
261261
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
}
@@ -926,17 +926,6 @@ describe('WebSocketServer', function () {
926926
wss.close(done);
927927
});
928928
});
929-
930-
it('upgradeReq is the original request object', function (done) {
931-
const wss = new WebSocketServer({ port: ++port }, () => {
932-
const ws = new WebSocket(`ws://localhost:${port}`, { protocolVersion: 8 });
933-
});
934-
935-
wss.on('connection', (client) => {
936-
assert.strictEqual(client.upgradeReq.httpVersion, '1.1');
937-
wss.close(done);
938-
});
939-
});
940929
});
941930

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

0 commit comments

Comments
 (0)