@@ -19,9 +19,8 @@ exports.Socket = class TCPSocket extends Duplex {
1919
2020 this . _allowHalfOpen = allowHalfOpen
2121
22- this . _remotePort = - 1
23- this . _remoteHost = null
24- this . _remoteFamily = 0
22+ this . _localAddress = null
23+ this . _remoteAddress = null
2524
2625 this . _pendingOpen = null
2726 this . _pendingWrite = null
@@ -69,6 +68,30 @@ exports.Socket = class TCPSocket extends Duplex {
6968 return 'opening'
7069 }
7170
71+ get localAddress ( ) {
72+ if ( this . _state & constants . state . CONNECTED ) return this . _localAddress . address
73+ }
74+
75+ get localFamily ( ) {
76+ if ( this . _state & constants . state . CONNECTED ) return `IPv${ this . _localAddress . family } `
77+ }
78+
79+ get localPort ( ) {
80+ if ( this . _state & constants . state . CONNECTED ) return this . _localAddress . port
81+ }
82+
83+ get remoteAddress ( ) {
84+ if ( this . _state & constants . state . CONNECTED ) return this . _remoteAddress . address
85+ }
86+
87+ get remoteFamily ( ) {
88+ if ( this . _state & constants . state . CONNECTED ) return `IPv${ this . _remoteAddress . family } `
89+ }
90+
91+ get remotePort ( ) {
92+ if ( this . _state & constants . state . CONNECTED ) return this . _remoteAddress . port
93+ }
94+
7295 connect ( port , host = 'localhost' , opts = { } , onconnect ) {
7396 if ( this . _state & constants . state . CONNECTING || this . _state & constants . state . CONNECTED ) {
7497 throw errors . SOCKET_ALREADY_CONNECTED ( 'Socket is already connected' )
@@ -152,10 +175,6 @@ exports.Socket = class TCPSocket extends Duplex {
152175 if ( noDelay ) this . setNoDelay ( )
153176 if ( timeout ) this . setTimeout ( timeout )
154177
155- this . _remotePort = port
156- this . _remoteHost = host
157- this . _remoteFamily = family
158-
159178 if ( onconnect ) this . once ( 'connect' , onconnect )
160179 } catch ( err ) {
161180 queueMicrotask ( ( ) => {
@@ -293,6 +312,9 @@ exports.Socket = class TCPSocket extends Duplex {
293312 _reset ( ) {
294313 this . _state = 0
295314
315+ this . _localAddress = null
316+ this . _remoteAddress = null
317+
296318 binding . reset ( this . _handle )
297319 }
298320
@@ -315,6 +337,9 @@ exports.Socket = class TCPSocket extends Duplex {
315337 this . _state &= ~ constants . state . CONNECTING
316338 this . _continueOpen ( )
317339
340+ this . _localAddress = binding . address ( this . _handle , true )
341+ this . _remoteAddress = binding . address ( this . _handle , false )
342+
318343 this . emit ( 'connect' )
319344 }
320345
@@ -396,9 +421,7 @@ exports.Server = class TCPServer extends EventEmitter {
396421 this . _noDelay = noDelay
397422 this . _pauseOnConnect = pauseOnConnect
398423
399- this . _port = - 1
400- this . _host = null
401- this . _family = 0
424+ this . _address = null
402425 this . _connections = new Set ( )
403426
404427 this . _error = null
@@ -420,15 +443,11 @@ exports.Server = class TCPServer extends EventEmitter {
420443 }
421444
422445 address ( ) {
423- if ( ( this . _state & constants . state . BOUND ) === 0 ) {
424- return null
425- }
446+ if ( ( this . _state & constants . state . BOUND ) === 0 ) return null
426447
427- return {
428- address : this . _host ,
429- family : `IPv${ this . _family } ` ,
430- port : this . _port
431- }
448+ const { address, family, port } = this . _address
449+
450+ return { address, family : `IPv${ family } ` , port }
432451 }
433452
434453 listen ( port = 0 , host = 'localhost' , backlog = 511 , opts = { } , onlistening ) {
@@ -506,9 +525,10 @@ exports.Server = class TCPServer extends EventEmitter {
506525 if ( this . _state & constants . state . UNREFED ) binding . unref ( this . _handle )
507526
508527 try {
509- this . _port = binding . bind ( this . _handle , port , host , backlog , family )
510- this . _host = host
511- this . _family = family
528+ binding . bind ( this . _handle , port , host , backlog , family )
529+
530+ this . _address = binding . address ( this . _handle , true )
531+
512532 this . _state |= constants . state . BOUND
513533 this . _state &= ~ constants . state . BINDING
514534
@@ -577,6 +597,9 @@ exports.Server = class TCPServer extends EventEmitter {
577597
578598 socket . _state |= constants . state . CONNECTED
579599
600+ socket . _localAddress = binding . address ( socket . _handle , true )
601+ socket . _remoteAddress = binding . address ( socket . _handle , false )
602+
580603 this . _connections . add ( socket )
581604
582605 if ( this . _keepAlive ) socket . setKeepAlive ( this . _keepAlive , this . _keepAliveInitialDelay )
@@ -601,6 +624,7 @@ exports.Server = class TCPServer extends EventEmitter {
601624 this . _state &= ~ constants . state . BINDING
602625 this . _error = null
603626 this . _handle = null
627+ this . _address = null
604628
605629 if ( err ) this . emit ( 'error' , err )
606630 else this . emit ( 'close' )
0 commit comments