@@ -13,6 +13,7 @@ import type {
13
13
import {
14
14
App ,
15
15
us_listen_socket_close ,
16
+ us_socket_local_port ,
16
17
_cfg as setUwsGlobalConfig
17
18
} from "@trufflesuite/uws-js-unofficial" ;
18
19
@@ -33,6 +34,13 @@ import WebsocketServer, { WebSocketCapableFlavor } from "./servers/ws-server";
33
34
import HttpServer from "./servers/http-server" ;
34
35
import Emittery from "emittery" ;
35
36
37
+ // not using the "net" node package in order to avoid having to polyfill this
38
+ // for the browser build.
39
+ // isIPv4 taken from https://github.com/nodejs/node/blob/01323d50c4b24cf730a651d06ba20633905ecbed/lib/internal/net.js#L31
40
+ const v4Seg = "(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" ;
41
+ const IPv4Reg = new RegExp ( `^(${ v4Seg } [.]){3}${ v4Seg } $` ) ;
42
+ const isIPv4 = ( s : string ) => IPv4Reg . test ( s ) ;
43
+
36
44
export type Provider = Connector [ "provider" ] ;
37
45
38
46
const DEFAULT_HOST = "127.0.0.1" ;
@@ -43,13 +51,13 @@ export type Callback = (err: Error | null) => void;
43
51
* Server ready state constants.
44
52
*
45
53
* These are bit flags. This means that you can check if the status is:
46
- * * ready: `status === Status .ready` or `status & Status .ready !== 0`
47
- * * opening: `status === Status .opening` or `status & Status .opening !== 0`
48
- * * open: `status === Status .open` or `status & Status .open !== 0`
49
- * * opening || open: `status & Status .openingOrOpen !== 0` or `status & (Status .opening | Status .open) !== 0`
50
- * * closing: `status === Status .closing` or `status & Status .closing !== 0`
51
- * * closed: `status === Status .closed` or `status & Status .closed !== 0`
52
- * * closing || closed: `status & Status .closingOrClosed !== 0` or `status & (Status .closing | Status .closed) !== 0`
54
+ * * ready: `status === ServerStatus .ready` or `status & ServerStatus .ready !== 0`
55
+ * * opening: `status === ServerStatus .opening` or `status & ServerStatus .opening !== 0`
56
+ * * open: `status === ServerStatus .open` or `status & ServerStatus .open !== 0`
57
+ * * opening || open: `status & ServerStatus .openingOrOpen !== 0` or `status & (ServerStatus .opening | ServerStatus .open) !== 0`
58
+ * * closing: `status === ServerStatus .closing` or `status & ServerStatus .closing !== 0`
59
+ * * closed: `status === ServerStatus .closed` or `status & ServerStatus .closed !== 0`
60
+ * * closing || closed: `status & ServerStatus .closingOrClosed !== 0` or `status & (ServerStatus .closing | ServerStatus .closed) !== 0`
53
61
*/
54
62
export enum ServerStatus {
55
63
/**
@@ -106,6 +114,7 @@ export class Server<
106
114
#app: TemplatedApp | null = null ;
107
115
#httpServer: HttpServer | null = null ;
108
116
#listenSocket: us_listen_socket | null = null ;
117
+ #host: string | null = null ;
109
118
#connector: ConnectorsByName [ Flavor ] ;
110
119
#websocketServer: WebsocketServer | null = null ;
111
120
@@ -183,8 +192,7 @@ export class Server<
183
192
( typeof port !== "number" && typeof port !== "string" ) ||
184
193
( typeof port === "string" && ( < string > port ) . trim ( ) . length === 0 ) ||
185
194
+ port !== + port >>> 0 ||
186
- port > 0xffff ||
187
- port === 0
195
+ port > 0xffff
188
196
) {
189
197
const err = new Error (
190
198
`Port should be >= 0 and < 65536. Received ${ port } .`
@@ -239,6 +247,7 @@ export class Server<
239
247
if ( listenSocket ) {
240
248
this . #status = ServerStatus . open ;
241
249
this . #listenSocket = listenSocket ;
250
+ this . #host = ( host as string ) || DEFAULT_HOST ;
242
251
} else {
243
252
this . #status = ServerStatus . closed ;
244
253
const err = new Error (
@@ -285,6 +294,19 @@ export class Server<
285
294
}
286
295
}
287
296
297
+ public address ( ) {
298
+ if ( this . #listenSocket) {
299
+ const address = this . #host;
300
+ return {
301
+ address,
302
+ family : isIPv4 ( address ) ? "IPv4" : "IPv6" ,
303
+ port : us_socket_local_port ( this . #listenSocket)
304
+ } ;
305
+ } else {
306
+ return null ;
307
+ }
308
+ }
309
+
288
310
public async close ( ) {
289
311
if ( this . #status === ServerStatus . opening ) {
290
312
// if opening
0 commit comments