Skip to content

Commit fd910f1

Browse files
hansonwlpinca
authored andcommitted
[feature] Accept hostname lookup family option (#962)
1 parent 19ce183 commit fd910f1

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/ws.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ This class represents a WebSocket. It extends the `EventEmitter`.
174174
depending on the `protocolVersion`.
175175
- `agent` {http.Agent|https.Agent} Use the specified Agent,
176176
- `host` {String} Value of the `Host` header.
177+
- `family` {Number} IP address family to use during hostname lookup (4 or 6).
177178
- `checkServerIdentity` {Function} A function to validate the server hostname.
178179
- `rejectUnauthorized` {Boolean} Verify or not the server certificate.
179180
- `passphrase` {String} The passphrase for the private key or pfx.

lib/WebSocket.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,15 @@ function initAsServerClient (req, socket, head, options) {
472472
* @param {String} address The URL to which to connect
473473
* @param {String[]} protocols The list of subprotocols
474474
* @param {Object} options Connection options
475-
* @param {String} option.protocol Value of the `Sec-WebSocket-Protocol` header
475+
* @param {String} options.protocol Value of the `Sec-WebSocket-Protocol` header
476476
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable permessage-deflate
477477
* @param {String} options.localAddress Local interface to bind for network connections
478478
* @param {Number} options.protocolVersion Value of the `Sec-WebSocket-Version` header
479479
* @param {Object} options.headers An object containing request headers
480480
* @param {String} options.origin Value of the `Origin` or `Sec-WebSocket-Origin` header
481481
* @param {http.Agent} options.agent Use the specified Agent
482482
* @param {String} options.host Value of the `Host` header
483+
* @param {Number} options.family IP address family to use during hostname lookup (4 or 6).
483484
* @param {Function} options.checkServerIdentity A function to validate the server hostname
484485
* @param {Boolean} options.rejectUnauthorized Verify or not the server certificate
485486
* @param {String} options.passphrase The passphrase for the private key or pfx
@@ -500,6 +501,7 @@ function initAsClient (address, protocols, options) {
500501
origin: null,
501502
agent: null,
502503
host: null,
504+
family: null,
503505

504506
//
505507
// SSL options.
@@ -572,6 +574,7 @@ function initAsClient (address, protocols, options) {
572574
}
573575
}
574576
if (options.host) requestOptions.headers.Host = options.host;
577+
if (options.family) requestOptions.family = options.family;
575578

576579
if (options.localAddress) requestOptions.localAddress = options.localAddress;
577580
if (isUnixSocket) requestOptions.socketPath = serverUrl.pathname;

test/WebSocket.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ describe('WebSocket', function () {
8080
/must be a valid IP: 123.456.789.428/
8181
);
8282
});
83+
84+
it('should accept the family option', function (done) {
85+
const wss = new WebSocketServer({ host: '::1', port: ++port }, () => {
86+
const ws = new WebSocket(`ws://localhost:${port}`, { family: 6 });
87+
});
88+
89+
wss.on('connection', (ws) => {
90+
assert.strictEqual(ws.upgradeReq.connection.remoteAddress, '::1');
91+
wss.close(done);
92+
});
93+
});
8394
});
8495

8596
describe('properties', function () {

0 commit comments

Comments
 (0)