diff --git a/doc/api/net.md b/doc/api/net.md index 5731af523d0151..9e74b2a5a79b9c 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -294,6 +294,7 @@ TCP server, the argument is as follows, otherwise the argument is `undefined`. * `data` {Object} The argument passed to event listener. * `localAddress` {string} Local address. * `localPort` {number} Local port. + * `localFamily` {string} Local family. * `remoteAddress` {string} Remote address. * `remotePort` {number} Remote port. * `remoteFamily` {string} Remote IP family. `'IPv4'` or `'IPv6'`. @@ -1045,6 +1046,16 @@ added: v0.9.6 The numeric representation of the local port. For example, `80` or `21`. +### `socket.localFamily` + + + +* {string} + +The string representation of the local IP family. `'IPv4'` or `'IPv6'`. + ### `socket.pause()` * Returns: {net.Socket} The socket itself. diff --git a/lib/net.js b/lib/net.js index cf48edb0274cc7..02eb9c0d4fe5a5 100644 --- a/lib/net.js +++ b/lib/net.js @@ -840,6 +840,9 @@ protoGetter('localPort', function localPort() { return this._getsockname().port; }); +protoGetter('localFamily', function localFamily() { + return this._getsockname().family; +}); Socket.prototype[kAfterAsyncWrite] = function() { this[kLastWriteQueueSize] = 0; @@ -1674,6 +1677,7 @@ function onconnection(err, clientHandle) { clientHandle.getsockname(localInfo); data.localAddress = localInfo.address; data.localPort = localInfo.port; + data.localFamily = localInfo.family; } if (clientHandle.getpeername) { const remoteInfo = ObjectCreate(null); diff --git a/test/parallel/test-net-local-address-port.js b/test/parallel/test-net-local-address-port.js index dfd7ef359b71d2..cfc6f61ef35ad8 100644 --- a/test/parallel/test-net-local-address-port.js +++ b/test/parallel/test-net-local-address-port.js @@ -27,6 +27,7 @@ const net = require('net'); const server = net.createServer(common.mustCall(function(socket) { assert.strictEqual(socket.localAddress, common.localhostIPv4); assert.strictEqual(socket.localPort, this.address().port); + assert.strictEqual(socket.localFamily, this.address().family); socket.on('end', function() { server.close(); });