Skip to content

Commit 69940a2

Browse files
committed
feat: add cloud service host detection and default port handling in DSN parsing
1 parent af116ff commit 69940a2

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

nodejs/src/client/wsClient.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { w3cwebsocket } from "websocket";
2121
import { ConnectorInfo, TSDB_OPTION_CONNECTION } from "../common/constant";
2222

2323
const DEFAULT_PORT = 6041;
24+
const CLOUD_DEFAULT_PORT = 443;
2425

2526
export class WsClient {
2627
private _wsConnector?: WebSocketConnector;
@@ -67,10 +68,22 @@ export class WsClient {
6768
return normalized.length > 0 ? normalized : "ws";
6869
}
6970

71+
private isCloudServiceHost(host: string): boolean {
72+
const normalizedHost = host.toLowerCase();
73+
return (
74+
normalizedHost.includes("cloud.tdengine.com") ||
75+
normalizedHost.includes("cloud.taosdata.com")
76+
);
77+
}
78+
79+
private getDefaultPortForHost(host: string): number {
80+
return this.isCloudServiceHost(host) ? CLOUD_DEFAULT_PORT : DEFAULT_PORT;
81+
}
82+
7083
private convertUrlToDsn(url: URL): Dsn {
7184
const scheme = url.protocol.replace(":", "");
7285
const host = url.hostname;
73-
const port = url.port.length > 0 ? Number.parseInt(url.port, 10) : DEFAULT_PORT;
86+
const port = url.port.length > 0 ? Number.parseInt(url.port, 10) : this.getDefaultPortForHost(host);
7487
const params = new Map<string, string>();
7588
url.searchParams.forEach((value, key) => {
7689
params.set(key, value);

nodejs/src/common/dsn.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ export function parse(url: string): Dsn {
106106
}
107107

108108
const DEFAULT_PORT = 6041;
109+
const CLOUD_DEFAULT_PORT = 443;
110+
111+
function isCloudServiceHost(host: string): boolean {
112+
const normalizedHost = host.toLowerCase();
113+
return (
114+
normalizedHost.includes("cloud.tdengine.com") ||
115+
normalizedHost.includes("cloud.taosdata.com")
116+
);
117+
}
118+
119+
function getDefaultPortForHost(host: string): number {
120+
return isCloudServiceHost(host) ? CLOUD_DEFAULT_PORT : DEFAULT_PORT;
121+
}
109122

110123
/**
111124
* Parse comma-separated host list. Supports IPv6 in brackets.
@@ -136,14 +149,14 @@ function parseHostList(hostStr: string): Address[] {
136149
);
137150
}
138151
const ipv6Host = hostStr.slice(i + 1, closeBracket);
139-
let port = DEFAULT_PORT;
152+
let port = getDefaultPortForHost(ipv6Host);
140153
let next = closeBracket + 1;
141154
if (next < hostStr.length && hostStr[next] === ":") {
142155
const portEnd = hostStr.indexOf(",", next);
143156
const portStr = portEnd === -1
144157
? hostStr.slice(next + 1)
145158
: hostStr.slice(next + 1, portEnd);
146-
port = parsePort(portStr, hostStr);
159+
port = parsePort(portStr, hostStr, ipv6Host);
147160
i = portEnd === -1 ? hostStr.length : portEnd;
148161
} else {
149162
i = next;
@@ -159,10 +172,10 @@ function parseHostList(hostStr: string): Address[] {
159172
const lastColon = segment.lastIndexOf(":");
160173
if (lastColon !== -1) {
161174
const host = segment.slice(0, lastColon);
162-
const port = parsePort(segment.slice(lastColon + 1), hostStr);
175+
const port = parsePort(segment.slice(lastColon + 1), hostStr, host);
163176
hosts.push({ host, port });
164177
} else {
165-
hosts.push({ host: segment, port: DEFAULT_PORT });
178+
hosts.push({ host: segment, port: getDefaultPortForHost(segment) });
166179
}
167180
i = commaIndex === -1 ? hostStr.length : commaIndex;
168181
}
@@ -171,9 +184,12 @@ function parseHostList(hostStr: string): Address[] {
171184
return hosts;
172185
}
173186

174-
function parsePort(portStr: string, context: string): number {
187+
function parsePort(portStr: string, context: string, hostForDefault?: string): number {
175188
// If port string is empty, use default port
176189
if (portStr.length === 0) {
190+
if (hostForDefault) {
191+
return getDefaultPortForHost(hostForDefault);
192+
}
177193
return DEFAULT_PORT;
178194
}
179195
// Validate that port string contains only digits

0 commit comments

Comments
 (0)