@@ -324,23 +324,14 @@ function onStreamRead(nread, buf, handle) {
324
324
325
325
// Called when the remote peer settings have been updated.
326
326
// Resets the cached settings.
327
- function onSettings ( ack ) {
327
+ function onSettings ( ) {
328
328
const session = this [ kOwner ] ;
329
329
if ( session . destroyed )
330
330
return ;
331
331
session [ kUpdateTimer ] ( ) ;
332
- let event = 'remoteSettings' ;
333
- if ( ack ) {
334
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : settings acknowledged` ) ;
335
- if ( session [ kState ] . pendingAck > 0 )
336
- session [ kState ] . pendingAck -- ;
337
- session [ kLocalSettings ] = undefined ;
338
- event = 'localSettings' ;
339
- } else {
340
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : new settings received` ) ;
341
- session [ kRemoteSettings ] = undefined ;
342
- }
343
- process . nextTick ( emit , session , event , session [ event ] ) ;
332
+ debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : new settings received` ) ;
333
+ session [ kRemoteSettings ] = undefined ;
334
+ process . nextTick ( emit , session , 'remoteSettings' , session . remoteSettings ) ;
344
335
}
345
336
346
337
// If the stream exists, an attempt will be made to emit an event
@@ -540,15 +531,32 @@ function onSessionInternalError(code) {
540
531
this [ kOwner ] . destroy ( new NghttpError ( code ) ) ;
541
532
}
542
533
534
+ function settingsCallback ( cb , ack , duration ) {
535
+ this [ kState ] . pendingAck -- ;
536
+ this [ kLocalSettings ] = undefined ;
537
+ if ( ack ) {
538
+ debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : settings received` ) ;
539
+ const settings = this . localSettings ;
540
+ if ( typeof cb === 'function' )
541
+ cb ( null , settings , duration ) ;
542
+ this . emit ( 'localSettings' , settings ) ;
543
+ } else {
544
+ debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : settings canceled` ) ;
545
+ if ( typeof cb === 'function' )
546
+ cb ( new errors . Error ( 'ERR_HTTP2_SETTINGS_CANCEL' ) ) ;
547
+ }
548
+ }
549
+
543
550
// Submits a SETTINGS frame to be sent to the remote peer.
544
- function submitSettings ( settings ) {
551
+ function submitSettings ( settings , callback ) {
545
552
if ( this . destroyed )
546
553
return ;
547
554
debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : submitting settings` ) ;
548
555
this [ kUpdateTimer ] ( ) ;
549
- this [ kLocalSettings ] = undefined ;
550
556
updateSettingsBuffer ( settings ) ;
551
- this [ kHandle ] . settings ( ) ;
557
+ if ( ! this [ kHandle ] . settings ( settingsCallback . bind ( this , callback ) ) ) {
558
+ this . destroy ( new errors . Error ( 'ERR_HTTP2_MAX_PENDING_SETTINGS_ACK' ) ) ;
559
+ }
552
560
}
553
561
554
562
// Submits a PRIORITY frame to be sent to the remote peer
@@ -783,7 +791,6 @@ class Http2Session extends EventEmitter {
783
791
streams : new Map ( ) ,
784
792
pendingStreams : new Set ( ) ,
785
793
pendingAck : 0 ,
786
- maxPendingAck : Math . max ( 1 , ( options . maxPendingAck | 0 ) || 10 ) ,
787
794
writeQueueSize : 0
788
795
} ;
789
796
@@ -951,21 +958,19 @@ class Http2Session extends EventEmitter {
951
958
}
952
959
953
960
// Submits a SETTINGS frame to be sent to the remote peer.
954
- settings ( settings ) {
961
+ settings ( settings , callback ) {
955
962
if ( this . destroyed )
956
963
throw new errors . Error ( 'ERR_HTTP2_INVALID_SESSION' ) ;
957
-
958
964
assertIsObject ( settings , 'settings' ) ;
959
965
settings = validateSettings ( settings ) ;
960
- const state = this [ kState ] ;
961
- if ( state . pendingAck === state . maxPendingAck ) {
962
- throw new errors . Error ( 'ERR_HTTP2_MAX_PENDING_SETTINGS_ACK' ,
963
- this [ kState ] . pendingAck ) ;
964
- }
966
+
967
+ if ( callback && typeof callback !== 'function' )
968
+ throw new errors . TypeError ( 'ERR_INVALID_CALLBACK' ) ;
965
969
debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : sending settings` ) ;
966
970
967
- state . pendingAck ++ ;
968
- const settingsFn = submitSettings . bind ( this , settings ) ;
971
+ this [ kState ] . pendingAck ++ ;
972
+
973
+ const settingsFn = submitSettings . bind ( this , settings , callback ) ;
969
974
if ( this . connecting ) {
970
975
this . once ( 'connect' , settingsFn ) ;
971
976
return ;
0 commit comments