@@ -324,23 +324,14 @@ function onStreamRead(nread, buf, handle) {
324324
325325// Called when the remote peer settings have been updated.
326326// Resets the cached settings.
327- function onSettings ( ack ) {
327+ function onSettings ( ) {
328328 const session = this [ kOwner ] ;
329329 if ( session . destroyed )
330330 return ;
331331 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 ) ;
344335}
345336
346337// If the stream exists, an attempt will be made to emit an event
@@ -540,15 +531,32 @@ function onSessionInternalError(code) {
540531 this [ kOwner ] . destroy ( new NghttpError ( code ) ) ;
541532}
542533
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+
543550// Submits a SETTINGS frame to be sent to the remote peer.
544- function submitSettings ( settings ) {
551+ function submitSettings ( settings , callback ) {
545552 if ( this . destroyed )
546553 return ;
547554 debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : submitting settings` ) ;
548555 this [ kUpdateTimer ] ( ) ;
549- this [ kLocalSettings ] = undefined ;
550556 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+ }
552560}
553561
554562// Submits a PRIORITY frame to be sent to the remote peer
@@ -783,7 +791,6 @@ class Http2Session extends EventEmitter {
783791 streams : new Map ( ) ,
784792 pendingStreams : new Set ( ) ,
785793 pendingAck : 0 ,
786- maxPendingAck : Math . max ( 1 , ( options . maxPendingAck | 0 ) || 10 ) ,
787794 writeQueueSize : 0
788795 } ;
789796
@@ -951,21 +958,19 @@ class Http2Session extends EventEmitter {
951958 }
952959
953960 // Submits a SETTINGS frame to be sent to the remote peer.
954- settings ( settings ) {
961+ settings ( settings , callback ) {
955962 if ( this . destroyed )
956963 throw new errors . Error ( 'ERR_HTTP2_INVALID_SESSION' ) ;
957-
958964 assertIsObject ( settings , 'settings' ) ;
959965 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' ) ;
965969 debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : sending settings` ) ;
966970
967- state . pendingAck ++ ;
968- const settingsFn = submitSettings . bind ( this , settings ) ;
971+ this [ kState ] . pendingAck ++ ;
972+
973+ const settingsFn = submitSettings . bind ( this , settings , callback ) ;
969974 if ( this . connecting ) {
970975 this . once ( 'connect' , settingsFn ) ;
971976 return ;
0 commit comments