Skip to content

Commit

Permalink
Add fromNodeServerOrNull(), and use it.
Browse files Browse the repository at this point in the history
It replaces `networkInterfaceString()`.
  • Loading branch information
danfuzz committed Dec 5, 2024
1 parent 17dee34 commit 38f2c82
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
8 changes: 3 additions & 5 deletions src/net-protocol/private/AsyncServerSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ export class AsyncServerSocket {
* address and current-listening info.
*/
get infoForLog() {
const address = this.#serverSocket?.address();
const address = InterfaceAddress.fromNodeServerOrNull(this.#serverSocket);
const iface = this.#interface.toString();

return {
protocol: this.#protocol,
interface: iface,
...(address
? { listening: EndpointAddress.networkInterfaceString(address) }
: {})
interface: this.#interface,
...(address ? { listening: address } : {})
};
}

Expand Down
14 changes: 0 additions & 14 deletions src/net-util/export/EndpointAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,6 @@ export class EndpointAddress extends IntfDeconstructable {
return `${addressStr}${portStr}`;
}

/**
* Makes a human-friendly network interface specification string. The given
* object is expected to either bind `address` and `port` (with `port`
* possibly being `null` but _not_ `undefined`), _or_ bind `fd`.
*
* @param {object} iface The interface specification to convert.
* @returns {string} The friendly form.
*/
static networkInterfaceString(iface) {
return (Object.hasOwn(iface, 'fd'))
? `/dev/fd/${iface.fd}`
: this.endpointString(iface.address, iface.port);
}

/**
* Canonicalizes an IPv4 address, returning `null` if it turns out not to be a
* valid address.
Expand Down
25 changes: 25 additions & 0 deletions src/net-util/export/InterfaceAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@ export class InterfaceAddress extends IntfDeconstructable {
// Static members
//

/**
* Gets an instance of this class corresponding to `server.address()` on a
* standard Node `Server` instance.
*
* @param {?Server} server Server object to look at, or `null` to just return
* `null`.
* @returns {?InterfaceAddress} Instance of this class representing the
* server's interface, or `null` if `server` is not currently listening.
*/
static fromNodeServerOrNull(server) {
const nodeAddress = server?.address();

if (!nodeAddress) {
return null;
}

const { address: origAddress, port: portNumber } = nodeAddress;

const address = ((origAddress === '::') || (origAddress === '0.0.0.0'))
? '*'
: origAddress;

return new InterfaceAddress({ address, portNumber });
}

/**
* Parses a network interface spec into its components. Accepts the two forms
* `<address>:<port>` or `/dev/fd/<fd-num>:<port>` (with the port optional in
Expand Down
16 changes: 0 additions & 16 deletions src/net-util/tests/EndpointAddress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,3 @@ describe('endpointString()', () => {
expect(EndpointAddress.endpointString(address, port)).toBe(expected);
});
});

describe('networkInteraceString()', () => {
test.each`
iface | expected
${{ fd: 3 }} | ${'/dev/fd/3'}
${{ fd: 987 }} | ${'/dev/fd/987'}
${{ address: 'florp.biz', port: 123 }} | ${'florp.biz:123'}
${{ address: '10.28.18.0', port: 80 }} | ${'10.28.18.0:80'}
${{ address: 'a0:b::c:d9', port: 443 }} | ${'[a0:b::c:d9]:443'}
${{ address: 'like.cat', port: null }} | ${'like.cat'}
${{ address: '199.2.3.4', port: null }} | ${'199.2.3.4'}
${{ address: '123a::456:f', port: null }} | ${'[123a::456:f]'}
`('with ($iface)', ({ iface, expected }) => {
expect(EndpointAddress.networkInterfaceString(iface)).toBe(expected);
});
});

0 comments on commit 38f2c82

Please sign in to comment.