diff --git a/doc/api/net.md b/doc/api/net.md index 01e567e93f8ea8..b5310b10665302 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'`. @@ -1047,6 +1048,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 a67dda1309c722..47e263f3dbd069 100644 --- a/lib/net.js +++ b/lib/net.js @@ -835,6 +835,9 @@ protoGetter('localPort', function localPort() { return this._getsockname().port; }); +protoGetter('localFamily', function localFamily() { + return this._getsockname().family; +}); Socket.prototype[kAfterAsyncWrite] = function() { this[kLastWriteQueueSize] = 0; @@ -1669,6 +1672,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(); });