Skip to content

Commit

Permalink
[feature] Accept hostname lookup family option (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansonw authored and lpinca committed Jan 11, 2017
1 parent 19ce183 commit fd910f1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/ws.md
Expand Up @@ -174,6 +174,7 @@ This class represents a WebSocket. It extends the `EventEmitter`.
depending on the `protocolVersion`.
- `agent` {http.Agent|https.Agent} Use the specified Agent,
- `host` {String} Value of the `Host` header.
- `family` {Number} IP address family to use during hostname lookup (4 or 6).
- `checkServerIdentity` {Function} A function to validate the server hostname.
- `rejectUnauthorized` {Boolean} Verify or not the server certificate.
- `passphrase` {String} The passphrase for the private key or pfx.
Expand Down
5 changes: 4 additions & 1 deletion lib/WebSocket.js
Expand Up @@ -472,14 +472,15 @@ function initAsServerClient (req, socket, head, options) {
* @param {String} address The URL to which to connect
* @param {String[]} protocols The list of subprotocols
* @param {Object} options Connection options
* @param {String} option.protocol Value of the `Sec-WebSocket-Protocol` header
* @param {String} options.protocol Value of the `Sec-WebSocket-Protocol` header
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable permessage-deflate
* @param {String} options.localAddress Local interface to bind for network connections
* @param {Number} options.protocolVersion Value of the `Sec-WebSocket-Version` header
* @param {Object} options.headers An object containing request headers
* @param {String} options.origin Value of the `Origin` or `Sec-WebSocket-Origin` header
* @param {http.Agent} options.agent Use the specified Agent
* @param {String} options.host Value of the `Host` header
* @param {Number} options.family IP address family to use during hostname lookup (4 or 6).
* @param {Function} options.checkServerIdentity A function to validate the server hostname
* @param {Boolean} options.rejectUnauthorized Verify or not the server certificate
* @param {String} options.passphrase The passphrase for the private key or pfx
Expand All @@ -500,6 +501,7 @@ function initAsClient (address, protocols, options) {
origin: null,
agent: null,
host: null,
family: null,

//
// SSL options.
Expand Down Expand Up @@ -572,6 +574,7 @@ function initAsClient (address, protocols, options) {
}
}
if (options.host) requestOptions.headers.Host = options.host;
if (options.family) requestOptions.family = options.family;

if (options.localAddress) requestOptions.localAddress = options.localAddress;
if (isUnixSocket) requestOptions.socketPath = serverUrl.pathname;
Expand Down
11 changes: 11 additions & 0 deletions test/WebSocket.test.js
Expand Up @@ -80,6 +80,17 @@ describe('WebSocket', function () {
/must be a valid IP: 123.456.789.428/
);
});

it('should accept the family option', function (done) {
const wss = new WebSocketServer({ host: '::1', port: ++port }, () => {
const ws = new WebSocket(`ws://localhost:${port}`, { family: 6 });
});

wss.on('connection', (ws) => {
assert.strictEqual(ws.upgradeReq.connection.remoteAddress, '::1');
wss.close(done);
});
});
});

describe('properties', function () {
Expand Down

0 comments on commit fd910f1

Please sign in to comment.