File tree Expand file tree Collapse file tree 5 files changed +54
-12
lines changed Expand file tree Collapse file tree 5 files changed +54
-12
lines changed Original file line number Diff line number Diff line change 11import type { PromiseCancellable } from '@matrixai/async-cancellable' ;
22import type { ContextTimed } from '@matrixai/contexts' ;
3- import type { ClientCrypto , Host , Port , VerifyCallback } from './types' ;
3+ import type { ClientCrypto , Host , VerifyCallback } from './types' ;
44import type { Config } from './native/types' ;
55import type QUICConnectionMap from './QUICConnectionMap' ;
66import type {
@@ -136,6 +136,8 @@ class QUICClient extends EventTarget {
136136 // Validating host and port types
137137 let [ host_ ] = await utils . resolveHost ( host , resolveHostname ) ;
138138 const [ localHost_ ] = await utils . resolveHost ( localHost , resolveHostname ) ;
139+ const port_ = utils . toPort ( port ) ;
140+ const localPort_ = utils . toPort ( localPort ) ;
139141 // If the target host is in fact a zero IP, it cannot be used
140142 // as a target host, so we need to resolve it to a non-zero IP
141143 // in this case, 0.0.0.0 is resolved to 127.0.0.1 and :: and ::0 is
@@ -156,7 +158,7 @@ class QUICClient extends EventTarget {
156158 isSocketShared = false ;
157159 await socket . start ( {
158160 host : localHost_ ,
159- port : localPort ,
161+ port : localPort_ ,
160162 } ) ;
161163 } else {
162164 if ( ! socket [ running ] ) {
@@ -208,7 +210,7 @@ class QUICClient extends EventTarget {
208210 socket,
209211 remoteInfo : {
210212 host : host_ ,
211- port : port as Port ,
213+ port : port_ ,
212214 } ,
213215 config : quicConfig ,
214216 reasonToCode,
Original file line number Diff line number Diff line change @@ -454,11 +454,11 @@ class QUICConnection extends EventTarget {
454454 }
455455
456456 public get remoteHost ( ) : string {
457- return this . _remoteHost ;
457+ return utils . fromHost ( this . _remoteHost ) ;
458458 }
459459
460460 public get remotePort ( ) : number {
461- return this . _remotePort ;
461+ return utils . fromPort ( this . _remotePort ) ;
462462 }
463463
464464 public get localHost ( ) : string {
Original file line number Diff line number Diff line change @@ -154,8 +154,8 @@ class QUICSocket extends EventTarget {
154154 * Whereas `0.0.0.0` means only all IPv4.
155155 */
156156 @ready ( new errors . ErrorQUICSocketNotRunning ( ) )
157- public get host ( ) {
158- return this . _host ;
157+ public get host ( ) : string {
158+ return utils . fromHost ( this . _host ) ;
159159 }
160160
161161 /**
@@ -164,8 +164,8 @@ class QUICSocket extends EventTarget {
164164 * Because `0` is always resolved to a specific port.
165165 */
166166 @ready ( new errors . ErrorQUICSocketNotRunning ( ) )
167- public get port ( ) {
168- return this . _port ;
167+ public get port ( ) : number {
168+ return utils . fromPort ( this . _port ) ;
169169 }
170170
171171 /**
@@ -202,6 +202,7 @@ class QUICSocket extends EventTarget {
202202 host ,
203203 this . resolveHostname ,
204204 ) ;
205+ const port_ = utils . toPort ( port ) ;
205206 this . socket = dgram . createSocket ( {
206207 type : udpType ,
207208 reuseAddr,
@@ -215,7 +216,7 @@ class QUICSocket extends EventTarget {
215216 // This resolves DNS via `getaddrinfo` under the hood.
216217 // It which respects the hosts file.
217218 // This makes it equivalent to `dns.lookup`.
218- const socketBindP = this . socketBind ( port , host_ ) ;
219+ const socketBindP = this . socketBind ( port_ , host_ ) ;
219220 try {
220221 await Promise . race ( [ socketBindP , errorP ] ) ;
221222 } catch ( e ) {
Original file line number Diff line number Diff line change @@ -5,6 +5,14 @@ class ErrorQUIC<T> extends AbstractError<T> {
55 static description = 'QUIC error' ;
66}
77
8+ class ErrorQUICHostInvalid < T > extends AbstractError < T > {
9+ static description = 'Host provided was not valid' ;
10+ }
11+
12+ class ErrorQUICPortInvalid < T > extends AbstractError < T > {
13+ static description = 'Port provided was not valid' ;
14+ }
15+
816class ErrorQUICConfig < T > extends ErrorQUIC < T > {
917 static description = 'QUIC config error' ;
1018}
@@ -141,6 +149,8 @@ class ErrorQUICUndefinedBehaviour<T> extends ErrorQUIC<T> {
141149
142150export {
143151 ErrorQUIC ,
152+ ErrorQUICHostInvalid ,
153+ ErrorQUICPortInvalid ,
144154 ErrorQUICConfig ,
145155 ErrorQUICSocket ,
146156 ErrorQUICSocketNotRunning ,
Original file line number Diff line number Diff line change @@ -168,11 +168,22 @@ async function resolveHost(
168168 } else if ( isIPv6 ( host ) ) {
169169 return [ host as Host , 'udp6' ] ;
170170 } else {
171- host = await resolveHostname ( host ) ;
172- return resolveHost ( host , resolveHostname ) ;
171+ try {
172+ host = await resolveHostname ( host ) ;
173+ return resolveHost ( host , resolveHostname ) ;
174+ } catch {
175+ throw new errors . ErrorQUICHostInvalid ( ) ;
176+ }
173177 }
174178}
175179
180+ /**
181+ * Converts a Host back to a string.
182+ */
183+ function fromHost ( host : Host ) : string {
184+ return host ;
185+ }
186+
176187/**
177188 * Is it a valid Port?
178189 */
@@ -181,6 +192,21 @@ function isPort(port: any): port is Port {
181192 return port >= 0 && port <= 65535 ;
182193}
183194
195+ /**
196+ * Throws if port is invalid, otherwise returns port as Port.
197+ */
198+ function toPort ( port : any ) : Port {
199+ if ( ! isPort ( port ) ) throw new errors . ErrorQUICPortInvalid ( ) ;
200+ return port ;
201+ }
202+
203+ /**
204+ * Converts a Port back to a number.
205+ */
206+ function fromPort ( port : Port ) : number {
207+ return port ;
208+ }
209+
184210/**
185211 * Convert callback-style to promise-style
186212 * If this is applied to overloaded function
@@ -463,7 +489,10 @@ export {
463489 toCanonicalIp ,
464490 resolveHostname ,
465491 resolveHost ,
492+ fromHost ,
466493 isPort ,
494+ toPort ,
495+ fromPort ,
467496 promisify ,
468497 promise ,
469498 bufferWrap ,
You can’t perform that action at this time.
0 commit comments