Skip to content

Commit c060d65

Browse files
refactor: improve the constructor types
1 parent cb10d01 commit c060d65

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

lib/socket.ts

+38-35
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ export interface SocketOptions {
1515
/**
1616
* The host that we're connecting to. Set from the URI passed when connecting
1717
*/
18-
host: string;
18+
host?: string;
1919

2020
/**
2121
* The hostname for our connection. Set from the URI passed when connecting
2222
*/
23-
hostname: string;
23+
hostname?: string;
2424

2525
/**
2626
* If this is a secure connection. Set from the URI passed when connecting
2727
*/
28-
secure: boolean;
28+
secure?: boolean;
2929

3030
/**
3131
* The port for our connection. Set from the URI passed when connecting
3232
*/
33-
port: string | number;
33+
port?: string | number;
3434

3535
/**
3636
* Any query parameters in our uri. Set from the URI passed when connecting
3737
*/
38-
query: { [key: string]: any };
38+
query?: { [key: string]: any };
3939

4040
/**
4141
* `http.Agent` to use, defaults to `false` (NodeJS only)
@@ -44,35 +44,35 @@ export interface SocketOptions {
4444
*
4545
* @see https://nodejs.org/api/http.html#httprequestoptions-callback
4646
*/
47-
agent: string | boolean;
47+
agent?: string | boolean;
4848

4949
/**
5050
* Whether the client should try to upgrade the transport from
5151
* long-polling to something better.
5252
* @default true
5353
*/
54-
upgrade: boolean;
54+
upgrade?: boolean;
5555

5656
/**
5757
* Forces base 64 encoding for polling transport even when XHR2
5858
* responseType is available and WebSocket even if the used standard
5959
* supports binary.
6060
*/
61-
forceBase64: boolean;
61+
forceBase64?: boolean;
6262

6363
/**
6464
* The param name to use as our timestamp key
6565
* @default 't'
6666
*/
67-
timestampParam: string;
67+
timestampParam?: string;
6868

6969
/**
7070
* Whether to add the timestamp with each transport request. Note: this
7171
* is ignored if the browser is IE or Android, in which case requests
7272
* are always stamped
7373
* @default false
7474
*/
75-
timestampRequests: boolean;
75+
timestampRequests?: boolean;
7676

7777
/**
7878
* A list of transports to try (in order). Engine.io always attempts to
@@ -81,7 +81,7 @@ export interface SocketOptions {
8181
*
8282
* @default ['polling','websocket', 'webtransport']
8383
*/
84-
transports: string[];
84+
transports?: string[];
8585

8686
/**
8787
* If true and if the previous websocket connection to the server succeeded,
@@ -92,50 +92,50 @@ export interface SocketOptions {
9292
* not block websockets.
9393
* @default false
9494
*/
95-
rememberUpgrade: boolean;
95+
rememberUpgrade?: boolean;
9696

9797
/**
9898
* Timeout for xhr-polling requests in milliseconds (0) (only for polling transport)
9999
*/
100-
requestTimeout: number;
100+
requestTimeout?: number;
101101

102102
/**
103103
* Transport options for Node.js client (headers etc)
104104
*/
105-
transportOptions: Object;
105+
transportOptions?: Object;
106106

107107
/**
108108
* (SSL) Certificate, Private key and CA certificates to use for SSL.
109109
* Can be used in Node.js client environment to manually specify
110110
* certificate information.
111111
*/
112-
pfx: string;
112+
pfx?: string;
113113

114114
/**
115115
* (SSL) Private key to use for SSL. Can be used in Node.js client
116116
* environment to manually specify certificate information.
117117
*/
118-
key: string;
118+
key?: string;
119119

120120
/**
121121
* (SSL) A string or passphrase for the private key or pfx. Can be
122122
* used in Node.js client environment to manually specify certificate
123123
* information.
124124
*/
125-
passphrase: string;
125+
passphrase?: string;
126126

127127
/**
128128
* (SSL) Public x509 certificate to use. Can be used in Node.js client
129129
* environment to manually specify certificate information.
130130
*/
131-
cert: string;
131+
cert?: string;
132132

133133
/**
134134
* (SSL) An authority certificate or array of authority certificates to
135135
* check the remote host against.. Can be used in Node.js client
136136
* environment to manually specify certificate information.
137137
*/
138-
ca: string | string[];
138+
ca?: string | string[];
139139

140140
/**
141141
* (SSL) A string describing the ciphers to use or exclude. Consult the
@@ -144,7 +144,7 @@ export interface SocketOptions {
144144
* details on the format.. Can be used in Node.js client environment to
145145
* manually specify certificate information.
146146
*/
147-
ciphers: string;
147+
ciphers?: string;
148148

149149
/**
150150
* (SSL) If true, the server certificate is verified against the list of
@@ -153,7 +153,7 @@ export interface SocketOptions {
153153
* is sent. Can be used in Node.js client environment to manually specify
154154
* certificate information.
155155
*/
156-
rejectUnauthorized: boolean;
156+
rejectUnauthorized?: boolean;
157157

158158
/**
159159
* Headers that will be passed for each request to the server (via xhr-polling and via websockets).
@@ -166,56 +166,56 @@ export interface SocketOptions {
166166
* client certificates, etc.) with cross-origin XHR polling requests
167167
* @default false
168168
*/
169-
withCredentials: boolean;
169+
withCredentials?: boolean;
170170

171171
/**
172172
* Whether to automatically close the connection whenever the beforeunload event is received.
173173
* @default false
174174
*/
175-
closeOnBeforeunload: boolean;
175+
closeOnBeforeunload?: boolean;
176176

177177
/**
178178
* Whether to always use the native timeouts. This allows the client to
179179
* reconnect when the native timeout functions are overridden, such as when
180180
* mock clocks are installed.
181181
* @default false
182182
*/
183-
useNativeTimers: boolean;
183+
useNativeTimers?: boolean;
184184

185185
/**
186186
* Whether the heartbeat timer should be unref'ed, in order not to keep the Node.js event loop active.
187187
*
188188
* @see https://nodejs.org/api/timers.html#timeoutunref
189189
* @default false
190190
*/
191-
autoUnref: boolean;
191+
autoUnref?: boolean;
192192

193193
/**
194194
* parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable.
195195
* @default false
196196
*/
197-
perMessageDeflate: { threshold: number };
197+
perMessageDeflate?: { threshold: number };
198198

199199
/**
200200
* The path to get our client file from, in the case of the server
201201
* serving it
202202
* @default '/engine.io'
203203
*/
204-
path: string;
204+
path?: string;
205205

206206
/**
207207
* Whether we should add a trailing slash to the request path.
208208
* @default true
209209
*/
210-
addTrailingSlash: boolean;
210+
addTrailingSlash?: boolean;
211211

212212
/**
213213
* Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols,
214214
* so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to
215215
* be able to handle different types of interactions depending on the specified protocol)
216216
* @default []
217217
*/
218-
protocols: string | string[];
218+
protocols?: string | string[];
219219
}
220220

221221
interface HandshakeData {
@@ -285,7 +285,9 @@ export class Socket extends Emitter<
285285
* @param {String|Object} uri - uri or options
286286
* @param {Object} opts - options
287287
*/
288-
constructor(uri, opts: Partial<SocketOptions> = {}) {
288+
constructor(uri?: string, opts?: SocketOptions);
289+
constructor(opts: SocketOptions);
290+
constructor(uri?: string | SocketOptions, opts: SocketOptions = {}) {
289291
super();
290292

291293
if (uri && "object" === typeof uri) {
@@ -294,11 +296,12 @@ export class Socket extends Emitter<
294296
}
295297

296298
if (uri) {
297-
uri = parse(uri);
298-
opts.hostname = uri.host;
299-
opts.secure = uri.protocol === "https" || uri.protocol === "wss";
300-
opts.port = uri.port;
301-
if (uri.query) opts.query = uri.query;
299+
const parsedUri = parse(uri as string);
300+
opts.hostname = parsedUri.host;
301+
opts.secure =
302+
parsedUri.protocol === "https" || parsedUri.protocol === "wss";
303+
opts.port = parsedUri.port;
304+
if (parsedUri.query) opts.query = parsedUri.query;
302305
} else if (opts.host) {
303306
opts.hostname = parse(opts.host).host;
304307
}

0 commit comments

Comments
 (0)