Skip to content

Commit 092239a

Browse files
theanarkhjuanarbol
authored andcommitted
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 579e066 commit 092239a

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/api/net.md

+11
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ TCP server, the argument is as follows, otherwise the argument is `undefined`.
270270
* `data` {Object} The argument passed to event listener.
271271
* `localAddress` {string} Local address.
272272
* `localPort` {number} Local port.
273+
* `localFamily` {string} Local family.
273274
* `remoteAddress` {string} Remote address.
274275
* `remotePort` {number} Remote port.
275276
* `remoteFamily` {string} Remote IP family. `'IPv4'` or `'IPv6'`.
@@ -1007,6 +1008,16 @@ added: v0.9.6
10071008

10081009
The numeric representation of the local port. For example, `80` or `21`.
10091010

1011+
### `socket.localFamily`
1012+
1013+
<!-- YAML
1014+
added: REPLACEME
1015+
-->
1016+
1017+
* {string}
1018+
1019+
The string representation of the local IP family. `'IPv4'` or `'IPv6'`.
1020+
10101021
### `socket.pause()`
10111022

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

lib/net.js

+4
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,9 @@ protoGetter('localPort', function localPort() {
849849
return this._getsockname().port;
850850
});
851851

852+
protoGetter('localFamily', function localFamily() {
853+
return this._getsockname().family;
854+
});
852855

853856
Socket.prototype[kAfterAsyncWrite] = function() {
854857
this[kLastWriteQueueSize] = 0;
@@ -1683,6 +1686,7 @@ function onconnection(err, clientHandle) {
16831686
clientHandle.getsockname(localInfo);
16841687
data.localAddress = localInfo.address;
16851688
data.localPort = localInfo.port;
1689+
data.localFamily = localInfo.family;
16861690
}
16871691
if (clientHandle.getpeername) {
16881692
const remoteInfo = ObjectCreate(null);

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

+1
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)