From a0a5fe88653c5a584af2beb814261bf1f3c05f51 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 29 Nov 2024 07:59:46 -0800 Subject: [PATCH] net: add SocketAddress.parse Adds a new `net.SocketAddress.parse(...)` API. ```js const addr = SocketAddress.parse('123.123.123.123:1234'); console.log(addr.address); // 123.123.123.123 console.log(addr.port); 1234 ``` PR-URL: https://github.com/nodejs/node/pull/56076 Reviewed-By: Matteo Collina Reviewed-By: Yagiz Nizipli --- doc/api/net.md | 11 ++ lib/internal/socketaddress.js | 34 +++- test/parallel/test-socketaddress.js | 237 ++++++++++++++++------------ 3 files changed, 182 insertions(+), 100 deletions(-) diff --git a/doc/api/net.md b/doc/api/net.md index 72d8d47fd4296f..0f22537a9bcc40 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -244,6 +244,17 @@ added: * Type {number} +### `SocketAddress.parse(input)` + + + +* `input` {string} An input string containing an IP address and optional port, + e.g. `123.1.2.3:1234` or `[1::1]:1234`. +* Returns: {net.SocketAddress} Returns a `SocketAddress` if parsing was successful. + Otherwise returns `undefined`. + ## Class: `net.Server`