@@ -11,16 +11,23 @@ assertCrypto();
11
11
12
12
const {
13
13
ArrayFrom,
14
+ ArrayPrototypePush,
14
15
BigInt64Array,
15
16
Boolean,
16
17
Error,
18
+ FunctionPrototypeBind,
19
+ FunctionPrototypeCall,
17
20
Map,
18
21
Number,
19
22
Promise,
20
23
PromiseAll,
24
+ PromisePrototypeThen,
25
+ PromisePrototypeCatch,
26
+ PromisePrototypeFinally,
21
27
PromiseReject,
22
28
PromiseResolve,
23
- Set,
29
+ ReflectApply,
30
+ SafeSet,
24
31
Symbol,
25
32
SymbolFor,
26
33
} = primordials ;
@@ -302,22 +309,25 @@ function onSessionClose(code, family, silent, statelessReset) {
302
309
// being requested. It is only called if the 'clientHelloHandler' option is
303
310
// specified on listen().
304
311
function onSessionClientHello ( alpn , servername , ciphers ) {
305
- this [ owner_symbol ] [ kClientHello ] ( alpn , servername , ciphers )
306
- . then ( ( context ) => {
312
+ PromisePrototypeThen (
313
+ this [ owner_symbol ] [ kClientHello ] ( alpn , servername , ciphers ) ,
314
+ ( context ) => {
307
315
if ( context !== undefined && ! context ?. context )
308
316
throw new ERR_INVALID_ARG_TYPE ( 'context' , 'SecureContext' , context ) ;
309
317
this . onClientHelloDone ( context ?. context ) ;
310
- } )
311
- . catch ( ( error ) => this [ owner_symbol ] . destroy ( error ) ) ;
318
+ } ,
319
+ ( error ) => this [ owner_symbol ] . destroy ( error )
320
+ ) ;
312
321
}
313
322
314
323
// This callback is only ever invoked for QuicServerSession instances,
315
324
// and is used to trigger OCSP request processing when needed. The
316
325
// user callback must invoke .onCertDone() in order for the
317
326
// TLS handshake to continue.
318
327
function onSessionCert ( servername ) {
319
- this [ owner_symbol ] [ kHandleOcsp ] ( servername )
320
- . then ( ( data ) => {
328
+ PromisePrototypeThen (
329
+ this [ owner_symbol ] [ kHandleOcsp ] ( servername ) ,
330
+ ( data ) => {
321
331
if ( data !== undefined ) {
322
332
if ( typeof data === 'string' )
323
333
data = Buffer . from ( data ) ;
@@ -329,17 +339,20 @@ function onSessionCert(servername) {
329
339
}
330
340
}
331
341
this . onCertDone ( data ) ;
332
- } )
333
- . catch ( ( error ) => this [ owner_symbol ] . destroy ( error ) ) ;
342
+ } ,
343
+ ( error ) => this [ owner_symbol ] . destroy ( error )
344
+ ) ;
334
345
}
335
346
336
347
// This callback is only ever invoked for QuicClientSession instances,
337
348
// and is used to deliver the OCSP response as provided by the server.
338
349
// If the requestOCSP configuration option is false, this will never
339
350
// be called.
340
351
function onSessionStatus ( data ) {
341
- this [ owner_symbol ] [ kHandleOcsp ] ( data )
342
- . catch ( ( error ) => this [ owner_symbol ] . destroy ( error ) ) ;
352
+ PromisePrototypeCatch (
353
+ this [ owner_symbol ] [ kHandleOcsp ] ( data ) ,
354
+ ( error ) => this [ owner_symbol ] . destroy ( error )
355
+ ) ;
343
356
}
344
357
345
358
// Called by the C++ internals when the TLS handshake is completed.
@@ -369,12 +382,13 @@ function onSessionHandshake(
369
382
// resumption and 0RTT.
370
383
function onSessionTicket ( sessionTicket , transportParams ) {
371
384
if ( this [ owner_symbol ] ) {
372
- process . nextTick (
373
- emit . bind (
374
- this [ owner_symbol ] ,
375
- 'sessionTicket' ,
376
- sessionTicket ,
377
- transportParams ) ) ;
385
+ process . nextTick ( FunctionPrototypeBind (
386
+ emit ,
387
+ this [ owner_symbol ] ,
388
+ 'sessionTicket' ,
389
+ sessionTicket ,
390
+ transportParams
391
+ ) ) ;
378
392
}
379
393
}
380
394
@@ -384,13 +398,14 @@ function onSessionTicket(sessionTicket, transportParams) {
384
398
function onSessionPathValidation ( res , local , remote ) {
385
399
const session = this [ owner_symbol ] ;
386
400
if ( session ) {
387
- process . nextTick (
388
- emit . bind (
389
- session ,
390
- 'pathValidation' ,
391
- res === NGTCP2_PATH_VALIDATION_RESULT_FAILURE ? 'failure' : 'success' ,
392
- local ,
393
- remote ) ) ;
401
+ process . nextTick ( FunctionPrototypeBind (
402
+ emit ,
403
+ session ,
404
+ 'pathValidation' ,
405
+ res === NGTCP2_PATH_VALIDATION_RESULT_FAILURE ? 'failure' : 'success' ,
406
+ local ,
407
+ remote
408
+ ) ) ;
394
409
}
395
410
}
396
411
@@ -486,7 +501,7 @@ function onStreamHeaders(id, headers, kind, push_id) {
486
501
// When a stream is flow control blocked, causes a blocked event
487
502
// to be emitted. This is a purely informational event.
488
503
function onStreamBlocked ( ) {
489
- process . nextTick ( emit . bind ( this [ owner_symbol ] , 'blocked' ) ) ;
504
+ process . nextTick ( FunctionPrototypeBind ( emit , this [ owner_symbol ] , 'blocked' ) ) ;
490
505
}
491
506
492
507
// Register the callbacks with the QUIC internal binding.
@@ -543,14 +558,17 @@ function addressOrLocalhost(address, type) {
543
558
}
544
559
545
560
function deferredClosePromise ( state ) {
546
- return state . closePromise = new Promise ( ( resolve , reject ) => {
547
- state . closePromiseResolve = resolve ;
548
- state . closePromiseReject = reject ;
549
- } ) . finally ( ( ) => {
550
- state . closePromise = undefined ;
551
- state . closePromiseResolve = undefined ;
552
- state . closePromiseReject = undefined ;
553
- } ) ;
561
+ return state . closePromise = PromisePrototypeFinally (
562
+ new Promise ( ( resolve , reject ) => {
563
+ state . closePromiseResolve = resolve ;
564
+ state . closePromiseReject = reject ;
565
+ } ) ,
566
+ ( ) => {
567
+ state . closePromise = undefined ;
568
+ state . closePromiseResolve = undefined ;
569
+ state . closePromiseReject = undefined ;
570
+ }
571
+ ) ;
554
572
}
555
573
556
574
async function resolvePreferredAddress ( lookup , preferredAddress ) {
@@ -640,7 +658,7 @@ class QuicEndpoint {
640
658
if ( state . bindPromise !== undefined )
641
659
return state . bindPromise ;
642
660
643
- return state . bindPromise = this [ kBind ] ( ) . finally ( ( ) => {
661
+ return state . bindPromise = PromisePrototypeFinally ( this [ kBind ] ( ) , ( ) => {
644
662
state . bindPromise = undefined ;
645
663
} ) ;
646
664
}
@@ -899,7 +917,7 @@ class QuicSocket extends EventEmitter {
899
917
closePromiseResolve : undefined ,
900
918
closePromiseReject : undefined ,
901
919
defaultEncoding : undefined ,
902
- endpoints : new Set ( ) ,
920
+ endpoints : new SafeSet ( ) ,
903
921
highWaterMark : undefined ,
904
922
listenPending : false ,
905
923
listenPromise : undefined ,
@@ -908,7 +926,7 @@ class QuicSocket extends EventEmitter {
908
926
clientHelloHandler : undefined ,
909
927
server : undefined ,
910
928
serverSecureContext : undefined ,
911
- sessions : new Set ( ) ,
929
+ sessions : new SafeSet ( ) ,
912
930
state : kSocketUnbound ,
913
931
sharedState : undefined ,
914
932
stats : undefined ,
@@ -1048,9 +1066,12 @@ class QuicSocket extends EventEmitter {
1048
1066
if ( state . bindPromise !== undefined )
1049
1067
return state . bindPromise ;
1050
1068
1051
- return state . bindPromise = this [ kBind ] ( options ) . finally ( ( ) => {
1052
- state . bindPromise = undefined ;
1053
- } ) ;
1069
+ return state . bindPromise = PromisePrototypeFinally (
1070
+ this [ kBind ] ( options ) ,
1071
+ ( ) => {
1072
+ state . bindPromise = undefined ;
1073
+ }
1074
+ ) ;
1054
1075
}
1055
1076
1056
1077
async [ kBind ] ( options ) {
@@ -1074,7 +1095,7 @@ class QuicSocket extends EventEmitter {
1074
1095
1075
1096
const binds = [ ] ;
1076
1097
for ( const endpoint of state . endpoints )
1077
- binds . push ( endpoint . bind ( { signal } ) ) ;
1098
+ ArrayPrototypePush ( binds , endpoint . bind ( { signal } ) ) ;
1078
1099
1079
1100
await PromiseAll ( binds ) ;
1080
1101
@@ -1169,9 +1190,12 @@ class QuicSocket extends EventEmitter {
1169
1190
if ( state . listenPromise !== undefined )
1170
1191
return state . listenPromise ;
1171
1192
1172
- return state . listenPromise = this [ kListen ] ( options ) . finally ( ( ) => {
1173
- state . listenPromise = undefined ;
1174
- } ) ;
1193
+ return state . listenPromise = PromisePrototypeFinally (
1194
+ this [ kListen ] ( options ) ,
1195
+ ( ) => {
1196
+ state . listenPromise = undefined ;
1197
+ }
1198
+ ) ;
1175
1199
}
1176
1200
1177
1201
async [ kListen ] ( options ) {
@@ -1388,8 +1412,9 @@ class QuicSocket extends EventEmitter {
1388
1412
// Otherwise, loop through each of the known sessions and close them.
1389
1413
const reqs = [ promise ] ;
1390
1414
for ( const session of state . sessions ) {
1391
- reqs . push ( session . close ( )
1392
- . catch ( ( error ) => this . destroy ( error ) ) ) ;
1415
+ ArrayPrototypePush ( reqs ,
1416
+ PromisePrototypeCatch ( session . close ( ) ,
1417
+ ( error ) => this . destroy ( error ) ) ) ;
1393
1418
}
1394
1419
return PromiseAll ( reqs ) ;
1395
1420
}
@@ -1441,11 +1466,11 @@ class QuicSocket extends EventEmitter {
1441
1466
if ( error ) {
1442
1467
if ( typeof state . closePromiseReject === 'function' )
1443
1468
state . closePromiseReject ( error ) ;
1444
- process . nextTick ( emit . bind ( this , 'error' , error ) ) ;
1469
+ process . nextTick ( FunctionPrototypeBind ( emit , this , 'error' , error ) ) ;
1445
1470
} else if ( typeof state . closePromiseResolve === 'function' ) {
1446
1471
state . closePromiseResolve ( ) ;
1447
1472
}
1448
- process . nextTick ( emit . bind ( this , 'close' ) ) ;
1473
+ process . nextTick ( FunctionPrototypeBind ( emit , this , 'close' ) ) ;
1449
1474
}
1450
1475
1451
1476
ref ( ) {
@@ -1714,14 +1739,17 @@ class QuicSession extends EventEmitter {
1714
1739
if ( state . handshakeCompletePromise !== undefined )
1715
1740
return state . handshakeCompletePromise ;
1716
1741
1717
- state . handshakeCompletePromise = new Promise ( ( resolve , reject ) => {
1718
- state . handshakeCompletePromiseResolve = resolve ;
1719
- state . handshakeCompletePromiseReject = reject ;
1720
- } ) . finally ( ( ) => {
1721
- state . handshakeCompletePromise = undefined ;
1722
- state . handshakeCompletePromiseReject = undefined ;
1723
- state . handshakeCompletePromiseResolve = undefined ;
1724
- } ) ;
1742
+ state . handshakeCompletePromise = PromisePrototypeFinally (
1743
+ new Promise ( ( resolve , reject ) => {
1744
+ state . handshakeCompletePromiseResolve = resolve ;
1745
+ state . handshakeCompletePromiseReject = reject ;
1746
+ } ) ,
1747
+ ( ) => {
1748
+ state . handshakeCompletePromise = undefined ;
1749
+ state . handshakeCompletePromiseReject = undefined ;
1750
+ state . handshakeCompletePromiseResolve = undefined ;
1751
+ }
1752
+ ) ;
1725
1753
1726
1754
return state . handshakeCompletePromise ;
1727
1755
}
@@ -1985,7 +2013,7 @@ class QuicSession extends EventEmitter {
1985
2013
if ( error ) {
1986
2014
if ( typeof state . closePromiseReject === 'function' )
1987
2015
state . closePromiseReject ( error ) ;
1988
- process . nextTick ( emit . bind ( this , 'error' , error ) ) ;
2016
+ process . nextTick ( FunctionPrototypeBind ( emit , this , 'error' , error ) ) ;
1989
2017
} else if ( typeof state . closePromiseResolve === 'function' )
1990
2018
state . closePromiseResolve ( ) ;
1991
2019
@@ -1994,7 +2022,7 @@ class QuicSession extends EventEmitter {
1994
2022
new ERR_OPERATION_FAILED ( 'Handshake failed' ) ) ;
1995
2023
}
1996
2024
1997
- process . nextTick ( emit . bind ( this , 'close' ) ) ;
2025
+ process . nextTick ( FunctionPrototypeBind ( emit , this , 'close' ) ) ;
1998
2026
}
1999
2027
2000
2028
// For server QuicSession instances, true if earlyData is
@@ -2698,7 +2726,7 @@ class QuicStream extends Duplex {
2698
2726
default :
2699
2727
assert . fail ( 'Invalid headers kind' ) ;
2700
2728
}
2701
- process . nextTick ( emit . bind ( this , name , headers , push_id ) ) ;
2729
+ process . nextTick ( FunctionPrototypeBind ( emit , this , name , headers , push_id ) ) ;
2702
2730
}
2703
2731
2704
2732
[ kAfterAsyncWrite ] ( { bytes } ) {
@@ -2809,7 +2837,7 @@ class QuicStream extends Duplex {
2809
2837
if ( ! this . destroyed ) {
2810
2838
if ( ! this . detached )
2811
2839
this [ kInternalState ] . sharedState . writeEnded = true ;
2812
- super . end . apply ( this , args ) ;
2840
+ ReflectApply ( super . end , this , args ) ;
2813
2841
}
2814
2842
return this ;
2815
2843
}
@@ -2825,13 +2853,14 @@ class QuicStream extends Duplex {
2825
2853
state . didRead = true ;
2826
2854
}
2827
2855
2828
- streamOnResume . call ( this ) ;
2856
+ FunctionPrototypeCall ( streamOnResume , this ) ;
2829
2857
}
2830
2858
2831
2859
sendFile ( path , options = { } ) {
2832
2860
if ( this . detached )
2833
2861
throw new ERR_INVALID_STATE ( 'Unable to send file' ) ;
2834
- fs . open ( path , 'r' , QuicStream [ kOnFileOpened ] . bind ( this , options ) ) ;
2862
+ fs . open ( path , 'r' ,
2863
+ FunctionPrototypeBind ( QuicStream [ kOnFileOpened ] , this , options ) ) ;
2835
2864
}
2836
2865
2837
2866
static [ kOnFileOpened ] ( options , err , fd ) {
@@ -2847,7 +2876,7 @@ class QuicStream extends Duplex {
2847
2876
}
2848
2877
2849
2878
if ( this . destroyed || this . closed ) {
2850
- fs . close ( fd , ( err ) => { if ( err ) throw err ; } ) ;
2879
+ fs . close ( fd , assert . ifError ) ;
2851
2880
return ;
2852
2881
}
2853
2882
@@ -2895,7 +2924,8 @@ class QuicStream extends Duplex {
2895
2924
static [ kOnFileUnpipe ] ( ) { // Called on the StreamPipe instance.
2896
2925
const stream = this . sink [ owner_symbol ] ;
2897
2926
if ( stream . ownsFd )
2898
- this . source . close ( ) . catch ( stream . destroy . bind ( stream ) ) ;
2927
+ PromisePrototypeCatch ( this . source . close ( ) ,
2928
+ FunctionPrototypeBind ( stream . destroy , stream ) ) ;
2899
2929
else
2900
2930
this . source . releaseFD ( ) ;
2901
2931
stream . end ( ) ;
0 commit comments