1
1
/**
2
+ * @typedef {object } ServerOptions
3
+ * @property {boolean } [noDelay]
4
+ * @property {boolean } [keepAlive]
5
+ * @property {number } [keepAliveInitialDelay]
6
+ * @property {boolean } [allowHalfOpen]
7
+ * @property {boolean } [pauseOnConnect]
8
+ *
2
9
* @typedef {import('./TLSSocket').default } TLSSocket
3
10
*
4
11
* @typedef {object } ServerEvents
12
19
*/
13
20
export default class Server extends EventEmitter < ServerEvents , any > {
14
21
/**
22
+ * @param {ServerOptions | ((socket: Socket) => void) } [options] Server options or connection listener
15
23
* @param {(socket: Socket) => void } [connectionCallback] Automatically set as a listener for the `'connection'` event.
16
24
*/
17
- constructor ( connectionCallback ?: ( ( socket : Socket ) => void ) | undefined ) ;
25
+ constructor ( options ?: ServerOptions | ( ( socket : Socket ) => void ) , connectionCallback ?: ( socket : Socket ) => void ) ;
26
+
18
27
/** @protected @readonly */
19
28
protected readonly _id : number ;
20
29
/** @protected @readonly */
21
30
protected readonly _eventEmitter : import ( "react-native" ) . EventEmitter ;
22
31
/** @private @type {Set<Socket> } */
23
- private _connections ;
32
+ private _connections : Set < Socket > ;
24
33
/** @private */
25
- private _localAddress ;
34
+ private _localAddress : string | undefined ;
26
35
/** @private */
27
- private _localPort ;
36
+ private _localPort : number | undefined ;
28
37
/** @private */
29
- private _localFamily ;
38
+ private _localFamily : string | undefined ;
39
+ /** @private */
40
+ private _serverOptions : ServerOptions ;
30
41
listening : boolean ;
42
+
31
43
/**
32
44
* Start a server listening for connections.
33
45
*
@@ -38,15 +50,15 @@ export default class Server extends EventEmitter<ServerEvents, any> {
38
50
* `server.listen()` call or `server.close()` has been called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN`
39
51
* error will be thrown.
40
52
*
41
- * @param {{ port: number; host: string; reuseAddress?: boolean} } options
53
+ * @param {{ port: number; host?: string; reuseAddress?: boolean} | number } options
54
+ * @param {string | (() => void) } [callback_or_host]
42
55
* @param {() => void } [callback]
43
56
* @returns {Server }
44
57
*/
45
- listen ( options : {
46
- port : number ;
47
- host : string ;
48
- reuseAddress ?: boolean ;
49
- } , callback ?: ( ( ) => void ) | undefined ) : Server ;
58
+ listen ( options : { port : number ; host ?: string ; reuseAddress ?: boolean } | number ,
59
+ callback_or_host ?: string | ( ( ) => void ) ,
60
+ callback ?: ( ) => void ) : Server ;
61
+
50
62
/**
51
63
* Asynchronously get the number of concurrent connections on the server.
52
64
*
@@ -56,6 +68,7 @@ export default class Server extends EventEmitter<ServerEvents, any> {
56
68
* @returns {Server }
57
69
*/
58
70
getConnections ( callback : ( err : Error | null , count : number ) => void ) : Server ;
71
+
59
72
/**
60
73
* Stops the server from accepting new connections and keeps existing connections.
61
74
* This function is asynchronous, the server is finally closed when all connections are ended and the server emits a `'close'` event.
@@ -65,7 +78,8 @@ export default class Server extends EventEmitter<ServerEvents, any> {
65
78
* @param {(err?: Error) => void } [callback] Called when the server is closed.
66
79
* @returns {Server }
67
80
*/
68
- close ( callback ?: ( ( err ?: Error | undefined ) => void ) | undefined ) : Server ;
81
+ close ( callback ?: ( err ?: Error ) => void ) : Server ;
82
+
69
83
/**
70
84
* Returns the bound `address`, the address `family` name, and `port` of the server as reported by the operating system if listening
71
85
* on an IP socket (useful to find which port was assigned when getting an OS-assigned address):
@@ -74,24 +88,26 @@ export default class Server extends EventEmitter<ServerEvents, any> {
74
88
* @returns {import('./Socket').AddressInfo | null }
75
89
*/
76
90
address ( ) : import ( './Socket' ) . AddressInfo | null ;
91
+
77
92
ref ( ) : Server ;
78
93
unref ( ) : Server ;
94
+
79
95
/**
80
96
* @private
81
97
*/
82
- private _registerEvents ;
83
- _listeningListener : import ( "react-native" ) . EmitterSubscription | undefined ;
84
- _errorListener : import ( "react-native" ) . EmitterSubscription | undefined ;
85
- _connectionsListener : import ( "react-native" ) . EmitterSubscription | undefined ;
98
+ private _registerEvents ( ) : void ;
99
+
86
100
/**
87
101
* @private
88
102
*/
89
- private _setDisconnected ;
103
+ private _setDisconnected ( ) : void ;
104
+
90
105
/**
91
106
* @protected
92
107
* @param {Socket } socket
93
108
*/
94
109
protected _addConnection ( socket : Socket ) : void ;
110
+
95
111
/**
96
112
* @protected
97
113
* @param {{ id: number; connection: import('./Socket').NativeConnectionInfo; } } info
@@ -101,14 +117,32 @@ export default class Server extends EventEmitter<ServerEvents, any> {
101
117
id : number ;
102
118
connection : import ( './Socket' ) . NativeConnectionInfo ;
103
119
} ) : Socket ;
120
+
121
+ /**
122
+ * Apply server socket options to a newly connected socket
123
+ * @param {Socket } socket
124
+ * @private
125
+ */
126
+ private _applySocketOptions ( socket : Socket ) : void ;
104
127
}
128
+
129
+ export type ServerOptions = {
130
+ noDelay ?: boolean ;
131
+ keepAlive ?: boolean ;
132
+ keepAliveInitialDelay ?: number ;
133
+ allowHalfOpen ?: boolean ;
134
+ pauseOnConnect ?: boolean ;
135
+ } ;
136
+
105
137
export type TLSSocket = import ( "./TLSSocket" ) . default ;
138
+
106
139
export type ServerEvents = {
107
140
close : ( ) => void ;
108
141
connection : ( socket : Socket ) => void ;
109
142
listening : ( ) => void ;
110
143
error : ( err : Error ) => void ;
111
144
secureConnection : ( tlsSocket : TLSSocket ) => void ;
112
145
} ;
146
+
113
147
import EventEmitter from "eventemitter3" ;
114
- import Socket from "./Socket" ;
148
+ import Socket from "./Socket" ;
0 commit comments