Skip to content

Commit d4423d6

Browse files
authored
net: add local family
PR-URL: #43975 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 60da0a1 commit d4423d6

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/api/net.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ TCP server, the argument is as follows, otherwise the argument is `undefined`.
294294
* `data` {Object} The argument passed to event listener.
295295
* `localAddress` {string} Local address.
296296
* `localPort` {number} Local port.
297+
* `localFamily` {string} Local family.
297298
* `remoteAddress` {string} Remote address.
298299
* `remotePort` {number} Remote port.
299300
* `remoteFamily` {string} Remote IP family. `'IPv4'` or `'IPv6'`.
@@ -1047,6 +1048,16 @@ added: v0.9.6
10471048

10481049
The numeric representation of the local port. For example, `80` or `21`.
10491050

1051+
### `socket.localFamily`
1052+
1053+
<!-- YAML
1054+
added: REPLACEME
1055+
-->
1056+
1057+
* {string}
1058+
1059+
The string representation of the local IP family. `'IPv4'` or `'IPv6'`.
1060+
10501061
### `socket.pause()`
10511062

10521063
* Returns: {net.Socket} The socket itself.

lib/net.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,9 @@ protoGetter('localPort', function localPort() {
835835
return this._getsockname().port;
836836
});
837837

838+
protoGetter('localFamily', function localFamily() {
839+
return this._getsockname().family;
840+
});
838841

839842
Socket.prototype[kAfterAsyncWrite] = function() {
840843
this[kLastWriteQueueSize] = 0;
@@ -1669,6 +1672,7 @@ function onconnection(err, clientHandle) {
16691672
clientHandle.getsockname(localInfo);
16701673
data.localAddress = localInfo.address;
16711674
data.localPort = localInfo.port;
1675+
data.localFamily = localInfo.family;
16721676
}
16731677
if (clientHandle.getpeername) {
16741678
const remoteInfo = ObjectCreate(null);

test/parallel/test-net-local-address-port.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const net = require('net');
2727
const server = net.createServer(common.mustCall(function(socket) {
2828
assert.strictEqual(socket.localAddress, common.localhostIPv4);
2929
assert.strictEqual(socket.localPort, this.address().port);
30+
assert.strictEqual(socket.localFamily, this.address().family);
3031
socket.on('end', function() {
3132
server.close();
3233
});

0 commit comments

Comments
 (0)