File tree Expand file tree Collapse file tree 1 file changed +13
-6
lines changed
Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change 1+ import assert from "node:assert" ;
12import { existsSync , readFileSync } from "node:fs" ;
23import { urlToHttpOptions } from "node:url" ;
34import type * as Models from "../models" ;
45
56export function isTcpPath ( path : string ) : boolean {
6- // tcp path must be:
7- // tcp://<host>:<port>
8- const regex = / ^ (?: t c p : \/ \/ ) \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
1217export 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}
You can’t perform that action at this time.
0 commit comments