Skip to content

Commit 2c25d57

Browse files
committed
refactor: improve address handling in parseDiscoveredEndpoints and update related tests
1 parent 001bd4c commit 2c25d57

3 files changed

Lines changed: 38 additions & 26 deletions

File tree

nodejs/src/client/clusterRegistry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export class Cluster {
1515
return this._addresses;
1616
}
1717

18-
addAddresses(newAddresses: Address[]): void {
19-
const merged = mergeAddresses([...this._addresses], newAddresses);
18+
addAddresses(addresses: Address[]): void {
19+
const merged = mergeAddresses([...this._addresses], addresses);
2020
this._addresses = Cluster.freezeAddresses(merged);
2121
}
2222

nodejs/src/common/dsn.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -252,35 +252,36 @@ export function parseDiscoveredEndpoints(instances: string[]): Address[] {
252252

253253
const portStr = remainder.startsWith(":") ? remainder.slice(1) : "";
254254
const parsedPort = parsePortValue(portStr);
255-
if (parsedPort === null && portStr.length > 0) {
255+
if (parsedPort === null) {
256256
warnInvalidEndpoint(raw);
257257
continue;
258258
}
259259
endpoints.push(
260-
new Address(`[${ipv6Host}]`, parsedPort ?? getDefaultPortForHost(ipv6Host))
260+
new Address(`[${ipv6Host}]`, parsedPort)
261261
);
262262
continue;
263263
}
264264

265-
let host = trimmed;
266-
let port = getDefaultPortForHost(host);
267265
const lastColon = trimmed.lastIndexOf(":");
268-
if (lastColon !== -1) {
269-
host = trimmed.slice(0, lastColon);
270-
if (host.length === 0 || host.includes(":")) {
271-
warnInvalidEndpoint(raw);
272-
continue;
273-
}
274-
const portStr = trimmed.slice(lastColon + 1);
275-
const parsedPort = parsePortValue(portStr);
276-
if (parsedPort === null && portStr.length > 0) {
277-
warnInvalidEndpoint(raw);
278-
continue;
279-
}
280-
port = parsedPort ?? getDefaultPortForHost(host);
266+
if (lastColon === -1) {
267+
warnInvalidEndpoint(raw);
268+
continue;
269+
}
270+
271+
const host = trimmed.slice(0, lastColon);
272+
if (host.length === 0 || host.includes(":")) {
273+
warnInvalidEndpoint(raw);
274+
continue;
275+
}
276+
277+
const portStr = trimmed.slice(lastColon + 1);
278+
const parsedPort = parsePortValue(portStr);
279+
if (parsedPort === null) {
280+
warnInvalidEndpoint(raw);
281+
continue;
281282
}
282283

283-
endpoints.push(new Address(host, port));
284+
endpoints.push(new Address(host, parsedPort));
284285
}
285286
return endpoints;
286287
}

nodejs/test/common/dsn.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,17 +509,28 @@ describe("dsn", () => {
509509
]);
510510
});
511511

512-
test("parseDiscoveredEndpoints uses default port for endpoints without explicit port", () => {
512+
test("parseDiscoveredEndpoints skips endpoints without explicit port and warns", () => {
513+
const warnSpy = jest.spyOn(logger, "warn").mockImplementation(() => logger);
513514
const result = parseDiscoveredEndpoints([
514515
"node3",
515516
"tenant.cloud.tdengine.com",
516517
"[2001:db8::10]",
517518
]);
518-
expect(result).toEqual([
519-
{ host: "node3", port: 6041 },
520-
{ host: "tenant.cloud.tdengine.com", port: 443 },
521-
{ host: "[2001:db8::10]", port: 6041 },
522-
]);
519+
expect(result).toEqual([]);
520+
expect(warnSpy).toHaveBeenCalledTimes(3);
521+
expect(warnSpy).toHaveBeenNthCalledWith(
522+
1,
523+
"Adapter HA: ignoring invalid endpoint: node3"
524+
);
525+
expect(warnSpy).toHaveBeenNthCalledWith(
526+
2,
527+
"Adapter HA: ignoring invalid endpoint: tenant.cloud.tdengine.com"
528+
);
529+
expect(warnSpy).toHaveBeenNthCalledWith(
530+
3,
531+
"Adapter HA: ignoring invalid endpoint: [2001:db8::10]"
532+
);
533+
warnSpy.mockRestore();
523534
});
524535

525536
test("parseDiscoveredEndpoints skips invalid endpoints and warns", () => {

0 commit comments

Comments
 (0)