HTHou commented on code in PR #21:
URL: 
https://github.com/apache/iotdb-client-nodejs/pull/21#discussion_r3643445906


##########
src/utils/Config.ts:
##########
@@ -32,23 +32,48 @@ export interface InternalConfig extends Config {
   sqlDialect?: string;
 }
 
+/**
+ * Parse a single "host:port" node URL into an EndPoint. Accepts the bracketed
+ * IPv6 form "[::1]:6667" (consistent with the [ipv6]:port endpoint format
+ * standardized in apache/iotdb#18162) as well as IPv4 and hostname URLs. A 
bare
+ * (unbracketed) IPv6 address with a port is ambiguous and rejected; the
+ * "[ipv6]:port" form must be used.
+ */
+function parseNodeUrl(url: string): EndPoint {
+  const trimmed = url.trim();
+  let host: string;
+  let portStr: string;
+  if (trimmed.startsWith('[')) {
+    // Bracketed IPv6: [host]:port
+    const close = trimmed.indexOf(']');
+    if (close === -1 || trimmed[close + 1] !== ':') {
+      throw new Error(`Invalid nodeUrl format: ${url}. Expected format: 
"[ipv6]:port"`);
+    }
+    host = trimmed.slice(1, close).trim();
+    portStr = trimmed.slice(close + 2).trim();
+  } else {
+    const idx = trimmed.indexOf(':');
+    // A non-bracketed URL with more than one colon is a bare IPv6 address,
+    // which is ambiguous with a trailing port; it must be written as 
[ipv6]:port.
+    if (idx === -1 || idx !== trimmed.lastIndexOf(':')) {
+      throw new Error(`Invalid nodeUrl format: ${url}. Expected format: 
"host:port" (use "[ipv6]:port" for IPv6 addresses)`);
+    }
+    host = trimmed.slice(0, idx).trim();
+    portStr = trimmed.slice(idx + 1).trim();
+  }
+  const port = parseInt(portStr, 10);

Review Comment:
   `parseInt` accepts a numeric prefix rather than requiring the entire string 
to be numeric, so malformed endpoints such as `[::1]:6667junk` and 
`[::1]:6667:9999` are silently accepted as port `6667`. Could we validate the 
complete port string (for example, with a digits-only check before conversion) 
and add regression tests for these cases? IoTDB's Java and Python endpoint 
parsers reject both inputs because their integer conversions require the full 
value to be valid.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to