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