Skip to content

Commit

Permalink
[major] Remove the upgradeReq property
Browse files Browse the repository at this point in the history
The `http.IncomingMessage` object, instead of being attached to the
`WebSocket` object, is passes as the second argument to the
`connection` event.
  • Loading branch information
lpinca committed May 11, 2017
1 parent 9062bb0 commit 12658ed
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 42 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -197,10 +197,10 @@ app.use(function (req, res) {
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

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

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

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

wss.on('connection', function connection(ws) {
const ip = ws.upgradeReq.connection.remoteAddress;
wss.on('connection', function connection(ws, req) {
const ip = req.connection.remoteAddress;
});
```

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

```js
wss.on('connection', function connection(ws) {
const ip = ws.upgradeReq.headers['x-forwarded-for'];
wss.on('connection', function connection(ws, req) {
const ip = req.headers['x-forwarded-for'];
});
```

Expand Down
12 changes: 4 additions & 8 deletions doc/ws.md
Expand Up @@ -89,8 +89,11 @@ provided.
### Event: 'connection'

- `socket` {WebSocket}
- `request` {http.IncomingMessage}

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

### Event: 'error'

Expand Down Expand Up @@ -423,13 +426,6 @@ Send `data` through the connection.

Forcibly close the connection.

### websocket.upgradeReq

- {http.IncomingMessage}

The http GET request sent by the client. Useful for parsing authority headers,
cookie headers, and other information. This is only available for server clients.

### websocket.url

- {String}
Expand Down
6 changes: 2 additions & 4 deletions examples/express-session-parse/index.js
Expand Up @@ -63,14 +63,12 @@ const wss = new WebSocket.Server({
server
});

wss.on('connection', (ws) => {
wss.on('connection', (ws, req) => {
ws.on('message', (message) => {
const session = ws.upgradeReq.session;

//
// Here we can now use session parameters.
//
console.log(`WS message ${message} from user ${session.userId}`);
console.log(`WS message ${message} from user ${req.session.userId}`);
});
});

Expand Down
5 changes: 2 additions & 3 deletions lib/WebSocket.js
Expand Up @@ -65,7 +65,7 @@ class WebSocket extends EventEmitter {
this._ultron = null;

if (Array.isArray(address)) {
initAsServerClient.call(this, address[0], address[1], address[2], options);
initAsServerClient.call(this, address[0], address[1], options);
} else {
initAsClient.call(this, address, protocols, options);
}
Expand Down Expand Up @@ -455,13 +455,12 @@ module.exports = WebSocket;
* @param {String} options.protocol The chosen subprotocol
* @private
*/
function initAsServerClient (req, socket, head, options) {
function initAsServerClient (socket, head, options) {
this.protocolVersion = options.protocolVersion;
this.extensions = options.extensions;
this.maxPayload = options.maxPayload;
this.protocol = options.protocol;

this.upgradeReq = req;
this._isServer = true;

this.setSocket(socket, head);
Expand Down
4 changes: 2 additions & 2 deletions lib/WebSocketServer.js
Expand Up @@ -85,7 +85,7 @@ class WebSocketServer extends EventEmitter {
this._ultron.on('error', (err) => this.emit('error', err));
this._ultron.on('upgrade', (req, socket, head) => {
this.handleUpgrade(req, socket, head, (client) => {
this.emit('connection', client);
this.emit('connection', client, req);
});
});
}
Expand Down Expand Up @@ -255,7 +255,7 @@ class WebSocketServer extends EventEmitter {

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

const client = new WebSocket([req, socket, head], null, {
const client = new WebSocket([socket, head], null, {
maxPayload: this.options.maxPayload,
protocolVersion: version,
extensions,
Expand Down
8 changes: 4 additions & 4 deletions test/WebSocket.test.js
Expand Up @@ -77,8 +77,8 @@ describe('WebSocket', function () {
});
});

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

wss.on('connection', (ws) => {
assert.strictEqual(ws.upgradeReq.connection.remoteAddress, '::1');
wss.on('connection', (ws, req) => {
assert.strictEqual(req.connection.remoteAddress, '::1');
wss.close(done);
});
});
Expand Down
17 changes: 3 additions & 14 deletions test/WebSocketServer.test.js
Expand Up @@ -90,11 +90,11 @@ describe('WebSocketServer', function () {
server.listen(sockPath, () => {
const wss = new WebSocketServer({ server });

wss.on('connection', (ws) => {
wss.on('connection', (ws, req) => {
if (wss.clients.size === 1) {
assert.strictEqual(ws.upgradeReq.url, '/foo?bar=bar');
assert.strictEqual(req.url, '/foo?bar=bar');
} else {
assert.strictEqual(ws.upgradeReq.url, '/');
assert.strictEqual(req.url, '/');
wss.close();
server.close(done);
}
Expand Down Expand Up @@ -926,17 +926,6 @@ describe('WebSocketServer', function () {
wss.close(done);
});
});

it('upgradeReq is the original request object', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const ws = new WebSocket(`ws://localhost:${port}`, { protocolVersion: 8 });
});

wss.on('connection', (client) => {
assert.strictEqual(client.upgradeReq.httpVersion, '1.1');
wss.close(done);
});
});
});

describe('permessage-deflate', function () {
Expand Down

0 comments on commit 12658ed

Please sign in to comment.