1
- import type { ConnectionId , ConnectionIdString , Crypto , Host , Hostname , Port } from './types' ;
2
- import type { Header , Config , Connection } from './native/types' ;
1
+ import type { Crypto , Host , Hostname , Port } from './types' ;
2
+ import type { Config } from './native/types' ;
3
3
import type { QUICConfig } from './config' ;
4
+ import type QUICConnectionMap from './QUICConnectionMap' ;
4
5
import Logger from '@matrixai/logger' ;
5
- import {
6
- CreateDestroy ,
7
- ready ,
8
- status
9
- } from '@matrixai/async-init/dist/CreateDestroy' ;
6
+ import { CreateDestroy , ready } from '@matrixai/async-init/dist/CreateDestroy' ;
10
7
import { running } from '@matrixai/async-init' ;
11
- import { Quiche , quiche , Type } from './native' ;
8
+ import { quiche } from './native' ;
12
9
import * as utils from './utils' ;
13
10
import * as errors from './errors' ;
14
11
import * as events from './events' ;
15
12
import { clientDefault } from './config' ;
16
13
import QUICSocket from './QUICSocket' ;
17
14
import QUICConnection from './QUICConnection' ;
18
- import QUICConnectionMap from './QUICConnectionMap' ;
19
15
import QUICConnectionId from './QUICConnectionId' ;
20
16
21
17
/**
@@ -29,7 +25,6 @@ import QUICConnectionId from './QUICConnectionId';
29
25
interface QUICClient extends CreateDestroy { }
30
26
@CreateDestroy ( )
31
27
class QUICClient extends EventTarget {
32
-
33
28
public readonly isSocketShared : boolean ;
34
29
protected socket : QUICSocket ;
35
30
protected logger : Logger ;
@@ -60,22 +55,22 @@ class QUICClient extends EventTarget {
60
55
logger = new Logger ( `${ this . name } ` ) ,
61
56
config = { } ,
62
57
} : {
63
- host : Host | Hostname ,
64
- port : Port ,
65
- localHost ?: Host | Hostname ,
66
- localPort ?: Port ,
58
+ host : Host | Hostname ;
59
+ port : Port ;
60
+ localHost ?: Host | Hostname ;
61
+ localPort ?: Port ;
67
62
crypto : {
68
63
key : ArrayBuffer ;
69
64
ops : Crypto ;
70
- } ,
65
+ } ;
71
66
socket ?: QUICSocket ;
72
67
resolveHostname ?: ( hostname : Hostname ) => Host | PromiseLike < Host > ;
73
68
logger ?: Logger ;
74
69
config ?: Partial < QUICConfig > ;
75
70
} ) {
76
71
const quicConfig = {
77
72
...clientDefault ,
78
- ...config
73
+ ...config ,
79
74
} ;
80
75
const scidBuffer = new ArrayBuffer ( quiche . MAX_CONN_ID_LEN ) ;
81
76
await crypto . ops . randomBytes ( scidBuffer ) ;
@@ -88,10 +83,7 @@ class QUICClient extends EventTarget {
88
83
// in this case, 0.0.0.0 is resolved to 127.0.0.1 and :: and ::0 is
89
84
// resolved to ::1
90
85
host_ = utils . resolvesZeroIP ( host_ ) ;
91
- const {
92
- p : errorP ,
93
- rejectP : rejectErrorP
94
- } = utils . promise < never > ( ) ;
86
+ const { p : errorP , rejectP : rejectErrorP } = utils . promise < never > ( ) ;
95
87
const handleQUICSocketError = ( e : events . QUICSocketErrorEvent ) => {
96
88
rejectErrorP ( e . detail ) ;
97
89
} ;
@@ -103,13 +95,9 @@ class QUICClient extends EventTarget {
103
95
socket = new QUICSocket ( {
104
96
crypto,
105
97
resolveHostname,
106
- logger : logger . getChild ( QUICSocket . name )
98
+ logger : logger . getChild ( QUICSocket . name ) ,
107
99
} ) ;
108
- socket . addEventListener (
109
- 'error' ,
110
- handleQUICSocketError ,
111
- { once : true }
112
- ) ;
100
+ socket . addEventListener ( 'error' , handleQUICSocketError , { once : true } ) ;
113
101
isSocketShared = false ;
114
102
await socket . start ( {
115
103
host : localHost ,
@@ -124,7 +112,8 @@ class QUICClient extends EventTarget {
124
112
// Check that the target `host` is compatible with the bound socket host
125
113
if (
126
114
socket . type === 'ipv4' &&
127
- ( ! utils . isIPv4 ( host_ ) && ! utils . isIPv4MappedIPv6 ( host_ ) )
115
+ ! utils . isIPv4 ( host_ ) &&
116
+ ! utils . isIPv4MappedIPv6 ( host_ )
128
117
) {
129
118
throw new errors . ErrorQUICClientInvalidHost (
130
119
`Cannot connect to ${ host_ } on an IPv4 QUICClient` ,
@@ -136,10 +125,7 @@ class QUICClient extends EventTarget {
136
125
throw new errors . ErrorQUICClientInvalidHost (
137
126
`Cannot connect to ${ host_ } on an IPv6 QUICClient` ,
138
127
) ;
139
- } else if (
140
- socket . type === 'ipv4&ipv6' &&
141
- ! utils . isIPv6 ( host_ )
142
- ) {
128
+ } else if ( socket . type === 'ipv4&ipv6' && ! utils . isIPv6 ( host_ ) ) {
143
129
throw new errors . ErrorQUICClientInvalidHost (
144
130
`Cannot send to ${ host_ } on a dual stack QUICClient` ,
145
131
) ;
@@ -157,16 +143,14 @@ class QUICClient extends EventTarget {
157
143
socket,
158
144
remoteInfo : {
159
145
host : host_ ,
160
- port
146
+ port,
161
147
} ,
162
148
config : quicConfig ,
163
- logger : logger . getChild ( `${ QUICConnection . name } ${ scid . toString ( ) . slice ( 32 ) } ` )
149
+ logger : logger . getChild (
150
+ `${ QUICConnection . name } ${ scid . toString ( ) . slice ( 32 ) } ` ,
151
+ ) ,
164
152
} ) ;
165
- connection . addEventListener (
166
- 'error' ,
167
- handleConnectionError ,
168
- { once : true }
169
- ) ;
153
+ connection . addEventListener ( 'error' , handleConnectionError , { once : true } ) ;
170
154
logger . debug ( 'CLIENT TRIGGER SEND' ) ;
171
155
// This will not raise an error
172
156
await connection . send ( ) ;
@@ -175,17 +159,19 @@ class QUICClient extends EventTarget {
175
159
await Promise . race ( [ connection . establishedP , errorP ] ) ;
176
160
} catch ( e ) {
177
161
logger . error ( e . toString ( ) ) ;
178
- // console .error(e);
162
+ // Console .error(e);
179
163
logger . debug ( `Is shared?: ${ isSocketShared } ` ) ;
180
164
// Waiting for connection to destroy
181
165
const destroyedProm = utils . promise < void > ( ) ;
182
- connection . addEventListener ( 'destroy' , ( ) => {
166
+ connection . addEventListener (
167
+ 'destroy' ,
168
+ ( ) => {
183
169
destroyedProm . resolveP ( ) ;
184
170
} ,
185
171
{
186
172
once : true ,
187
173
} ,
188
- )
174
+ ) ;
189
175
await destroyedProm . p ;
190
176
if ( ! isSocketShared ) {
191
177
// Stop our own socket
@@ -207,7 +193,7 @@ class QUICClient extends EventTarget {
207
193
socket,
208
194
connection,
209
195
isSocketShared,
210
- logger
196
+ logger,
211
197
} ) ;
212
198
address = utils . buildAddress ( host_ , port ) ;
213
199
logger . info ( `Created ${ this . name } to ${ address } ` ) ;
@@ -224,8 +210,8 @@ class QUICClient extends EventTarget {
224
210
protected handleQUICSocketError = ( e : events . QUICSocketErrorEvent ) => {
225
211
this . dispatchEvent (
226
212
new events . QUICClientErrorEvent ( {
227
- detail : e
228
- } )
213
+ detail : e ,
214
+ } ) ,
229
215
) ;
230
216
} ;
231
217
@@ -234,11 +220,13 @@ class QUICClient extends EventTarget {
234
220
* This is always used because QUICClient is
235
221
* one to one with QUICConnection
236
222
*/
237
- protected handleQUICConnectionError = ( e : events . QUICConnectionErrorEvent ) => {
223
+ protected handleQUICConnectionError = (
224
+ e : events . QUICConnectionErrorEvent ,
225
+ ) => {
238
226
this . dispatchEvent (
239
227
new events . QUICClientErrorEvent ( {
240
- detail : e
241
- } )
228
+ detail : e ,
229
+ } ) ,
242
230
) ;
243
231
} ;
244
232
@@ -266,16 +254,10 @@ class QUICClient extends EventTarget {
266
254
// Registers itself to the socket
267
255
this . socket . registerClient ( this ) ;
268
256
if ( ! isSocketShared ) {
269
- this . socket . addEventListener (
270
- 'error' ,
271
- this . handleQUICSocketError
272
- ) ;
257
+ this . socket . addEventListener ( 'error' , this . handleQUICSocketError ) ;
273
258
}
274
259
this . _connection = connection ;
275
- this . _connection . addEventListener (
276
- 'error' ,
277
- this . handleQUICConnectionError
278
- ) ;
260
+ this . _connection . addEventListener ( 'error' , this . handleQUICConnectionError ) ;
279
261
}
280
262
281
263
@ready ( new errors . ErrorQUICClientDestroyed ( ) )
@@ -313,7 +295,6 @@ class QUICClient extends EventTarget {
313
295
// Unlike the server
314
296
// upon a connection failing/destroying
315
297
// it should result in the CLIENT also being destroyed
316
-
317
298
}
318
299
319
300
export default QUICClient ;
0 commit comments