Skip to content

Commit 19316d2

Browse files
committed
fixes
1 parent 8056438 commit 19316d2

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/adapter/utils.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1+
import assert from "node:assert";
12
import {existsSync, readFileSync} from "node:fs";
23
import {urlToHttpOptions} from "node:url";
34
import type * as Models from "../models";
45

56
export function isTcpPath(path: string): boolean {
6-
// tcp path must be:
7-
// tcp://<host>:<port>
8-
const regex = /^(?:tcp:\/\/)\S+?[:]\d+$/gm;
9-
return regex.test(path);
7+
try {
8+
// validation as side-effect
9+
new URL(path);
10+
11+
return true;
12+
} catch {
13+
return false;
14+
}
1015
}
1116

1217
export function parseTcpPath(path: string): {host: string; port: number} {
13-
// built-in extra validation
1418
const info = urlToHttpOptions(new URL(path));
1519

20+
// urlToHttpOptions has a weird return type, extra validation doesn't hurt
21+
assert(info.hostname && info.port);
22+
1623
return {
17-
host: String(info.hostname),
24+
host: info.hostname,
1825
port: Number(info.port),
1926
};
2027
}

0 commit comments

Comments
 (0)