Skip to content

Commit b6b632c

Browse files
theanarkhruyadorno
authored andcommitted
net: add local family
PR-URL: #43975 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 51a0310 commit b6b632c

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
@@ -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'`.
@@ -1045,6 +1046,16 @@ added: v0.9.6
10451046

10461047
The numeric representation of the local port. For example, `80` or `21`.
10471048

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

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

lib/net.js

+4
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,9 @@ protoGetter('localPort', function localPort() {
840840
return this._getsockname().port;
841841
});
842842

843+
protoGetter('localFamily', function localFamily() {
844+
return this._getsockname().family;
845+
});
843846

844847
Socket.prototype[kAfterAsyncWrite] = function() {
845848
this[kLastWriteQueueSize] = 0;
@@ -1674,6 +1677,7 @@ function onconnection(err, clientHandle) {
16741677
clientHandle.getsockname(localInfo);
16751678
data.localAddress = localInfo.address;
16761679
data.localPort = localInfo.port;
1680+
data.localFamily = localInfo.family;
16771681
}
16781682
if (clientHandle.getpeername) {
16791683
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)