@@ -139,6 +139,26 @@ const { _connectionListener: httpConnectionListener } = http;
139
139
const debug = require ( 'internal/util/debuglog' ) . debuglog ( 'http2' ) ;
140
140
const { getOptionValue } = require ( 'internal/options' ) ;
141
141
142
+ // TODO(addaleax): See if this can be made more efficient by figuring out
143
+ // whether debugging is enabled before we perform any further steps. Currently,
144
+ // this seems pretty fast, though.
145
+ function debugStream ( id , sessionType , message , ...args ) {
146
+ debug ( 'Http2Stream %s [Http2Session %s]: ' + message ,
147
+ id , sessionName ( sessionType ) , ...args ) ;
148
+ }
149
+
150
+ function debugStreamObj ( stream , message , ...args ) {
151
+ debugStream ( stream [ kID ] , stream [ kSession ] [ kType ] , ...args ) ;
152
+ }
153
+
154
+ function debugSession ( sessionType , message , ...args ) {
155
+ debug ( 'Http2Session %s: ' + message , sessionName ( sessionType ) , ...args ) ;
156
+ }
157
+
158
+ function debugSessionObj ( session , message , ...args ) {
159
+ debugSession ( session [ kType ] , message , ...args ) ;
160
+ }
161
+
142
162
const kMaxFrameSize = ( 2 ** 24 ) - 1 ;
143
163
const kMaxInt = ( 2 ** 32 ) - 1 ;
144
164
const kMaxStreams = ( 2 ** 31 ) - 1 ;
@@ -260,8 +280,7 @@ function onSessionHeaders(handle, id, cat, flags, headers) {
260
280
261
281
const type = session [ kType ] ;
262
282
session [ kUpdateTimer ] ( ) ;
263
- debug ( `Http2Stream ${ id } [Http2Session ` +
264
- `${ sessionName ( type ) } ]: headers received` ) ;
283
+ debugStream ( id , type , 'headers received' ) ;
265
284
const streams = session [ kState ] . streams ;
266
285
267
286
const endOfStream = ! ! ( flags & NGHTTP2_FLAG_END_STREAM ) ;
@@ -321,8 +340,7 @@ function onSessionHeaders(handle, id, cat, flags, headers) {
321
340
const originSet = session [ kState ] . originSet = initOriginSet ( session ) ;
322
341
originSet . delete ( stream [ kOrigin ] ) ;
323
342
}
324
- debug ( `Http2Stream ${ id } [Http2Session ` +
325
- `${ sessionName ( type ) } ]: emitting stream '${ event } ' event` ) ;
343
+ debugStream ( id , type , "emitting stream '%s' event" , event ) ;
326
344
process . nextTick ( emit , stream , event , obj , flags , headers ) ;
327
345
}
328
346
if ( endOfStream ) {
@@ -363,7 +381,7 @@ function onPing(payload) {
363
381
if ( session . destroyed )
364
382
return ;
365
383
session [ kUpdateTimer ] ( ) ;
366
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : new ping received` ) ;
384
+ debugSessionObj ( session , ' new ping received' ) ;
367
385
session . emit ( 'ping' , payload ) ;
368
386
}
369
387
@@ -378,8 +396,7 @@ function onStreamClose(code) {
378
396
if ( ! stream || stream . destroyed )
379
397
return false ;
380
398
381
- debug ( `Http2Stream ${ stream [ kID ] } [Http2Session ` +
382
- `${ sessionName ( stream [ kSession ] [ kType ] ) } ]: closed with code ${ code } ` ) ;
399
+ debugStreamObj ( stream , 'closed with code %d' , code ) ;
383
400
384
401
if ( ! stream . closed )
385
402
closeStream ( stream , code , kNoRstStream ) ;
@@ -416,7 +433,7 @@ function onSettings() {
416
433
if ( session . destroyed )
417
434
return ;
418
435
session [ kUpdateTimer ] ( ) ;
419
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : new settings received` ) ;
436
+ debugSessionObj ( session , ' new settings received' ) ;
420
437
session [ kRemoteSettings ] = undefined ;
421
438
session . emit ( 'remoteSettings' , session . remoteSettings ) ;
422
439
}
@@ -428,9 +445,9 @@ function onPriority(id, parent, weight, exclusive) {
428
445
const session = this [ kOwner ] ;
429
446
if ( session . destroyed )
430
447
return ;
431
- debug ( `Http2Stream ${ id } [Http2Session ` +
432
- ` ${ sessionName ( session [ kType ] ) } ]: priority [parent: ${ parent } , ` +
433
- `weight: ${ weight } , exclusive: ${ exclusive } ]` ) ;
448
+ debugStream ( id , session [ kType ] ,
449
+ ' priority [parent: %d, weight: %d, exclusive: %s]' ,
450
+ parent , weight , exclusive ) ;
434
451
const emitter = session [ kState ] . streams . get ( id ) || session ;
435
452
if ( ! emitter . destroyed ) {
436
453
emitter [ kUpdateTimer ] ( ) ;
@@ -444,8 +461,8 @@ function onFrameError(id, type, code) {
444
461
const session = this [ kOwner ] ;
445
462
if ( session . destroyed )
446
463
return ;
447
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : error sending frame ` +
448
- `type ${ type } on stream ${ id } , code: ${ code } ` ) ;
464
+ debugSessionObj ( session , ' error sending frame type %d on stream %d, code: %d' ,
465
+ type , id , code ) ;
449
466
const emitter = session [ kState ] . streams . get ( id ) || session ;
450
467
emitter [ kUpdateTimer ] ( ) ;
451
468
emitter . emit ( 'frameError' , type , code , id ) ;
@@ -455,8 +472,8 @@ function onAltSvc(stream, origin, alt) {
455
472
const session = this [ kOwner ] ;
456
473
if ( session . destroyed )
457
474
return ;
458
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : altsvc received: ` +
459
- `stream: ${ stream } , origin: ${ origin } , alt: ${ alt } ` ) ;
475
+ debugSessionObj ( session , ' altsvc received: stream: %d, origin: %s, alt: %s' ,
476
+ stream , origin , alt ) ;
460
477
session [ kUpdateTimer ] ( ) ;
461
478
session . emit ( 'altsvc' , alt , origin , stream ) ;
462
479
}
@@ -483,8 +500,7 @@ function onOrigin(origins) {
483
500
const session = this [ kOwner ] ;
484
501
if ( session . destroyed )
485
502
return ;
486
- debug ( 'Http2Session %s: origin received: %j' ,
487
- sessionName ( session [ kType ] ) , origins ) ;
503
+ debugSessionObj ( session , 'origin received: %j' , origins ) ;
488
504
session [ kUpdateTimer ] ( ) ;
489
505
if ( ! session . encrypted || session . destroyed )
490
506
return undefined ;
@@ -504,8 +520,8 @@ function onGoawayData(code, lastStreamID, buf) {
504
520
const session = this [ kOwner ] ;
505
521
if ( session . destroyed )
506
522
return ;
507
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : goaway ${ code } ` +
508
- `received [last stream id: ${ lastStreamID } ]` ) ;
523
+ debugSessionObj ( session , ' goaway %d received [last stream id: %d]' ,
524
+ code , lastStreamID ) ;
509
525
510
526
const state = session [ kState ] ;
511
527
state . goawayCode = code ;
@@ -560,8 +576,7 @@ function requestOnConnect(headers, options) {
560
576
return ;
561
577
}
562
578
563
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : connected, ` +
564
- 'initializing request' ) ;
579
+ debugSessionObj ( session , 'connected, initializing request' ) ;
565
580
566
581
let streamOptions = 0 ;
567
582
if ( options . endStream )
@@ -650,13 +665,13 @@ function settingsCallback(cb, ack, duration) {
650
665
this [ kState ] . pendingAck -- ;
651
666
this [ kLocalSettings ] = undefined ;
652
667
if ( ack ) {
653
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : settings received` ) ;
668
+ debugSessionObj ( this , ' settings received' ) ;
654
669
const settings = this . localSettings ;
655
670
if ( typeof cb === 'function' )
656
671
cb ( null , settings , duration ) ;
657
672
this . emit ( 'localSettings' , settings ) ;
658
673
} else {
659
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : settings canceled` ) ;
674
+ debugSessionObj ( this , ' settings canceled' ) ;
660
675
if ( typeof cb === 'function' )
661
676
cb ( new ERR_HTTP2_SETTINGS_CANCEL ( ) ) ;
662
677
}
@@ -666,7 +681,7 @@ function settingsCallback(cb, ack, duration) {
666
681
function submitSettings ( settings , callback ) {
667
682
if ( this . destroyed )
668
683
return ;
669
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : submitting settings` ) ;
684
+ debugSessionObj ( this , ' submitting settings' ) ;
670
685
this [ kUpdateTimer ] ( ) ;
671
686
updateSettingsBuffer ( settings ) ;
672
687
if ( ! this [ kHandle ] . settings ( settingsCallback . bind ( this , callback ) ) ) {
@@ -700,7 +715,7 @@ function submitPriority(options) {
700
715
function submitGoaway ( code , lastStreamID , opaqueData ) {
701
716
if ( this . destroyed )
702
717
return ;
703
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : submitting goaway` ) ;
718
+ debugSessionObj ( this , ' submitting goaway' ) ;
704
719
this [ kUpdateTimer ] ( ) ;
705
720
this [ kHandle ] . goaway ( code , lastStreamID , opaqueData ) ;
706
721
}
@@ -831,7 +846,7 @@ function setupHandle(socket, type, options) {
831
846
'Internal HTTP/2 Failure. The socket is not connected. Please ' +
832
847
'report this as a bug in Node.js' ) ;
833
848
834
- debug ( `Http2Session ${ sessionName ( type ) } : setting up session handle` ) ;
849
+ debugSession ( type , ' setting up session handle' ) ;
835
850
this [ kState ] . flags |= SESSION_FLAGS_READY ;
836
851
837
852
updateOptionsBuffer ( options ) ;
@@ -987,7 +1002,7 @@ class Http2Session extends EventEmitter {
987
1002
setupFn ( ) ;
988
1003
}
989
1004
990
- debug ( `Http2Session ${ sessionName ( type ) } : created` ) ;
1005
+ debugSession ( type , ' created' ) ;
991
1006
}
992
1007
993
1008
// Returns undefined if the socket is not yet connected, true if the
@@ -1163,7 +1178,7 @@ class Http2Session extends EventEmitter {
1163
1178
1164
1179
if ( callback && typeof callback !== 'function' )
1165
1180
throw new ERR_INVALID_CALLBACK ( callback ) ;
1166
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : sending settings` ) ;
1181
+ debugSessionObj ( this , ' sending settings' ) ;
1167
1182
1168
1183
this [ kState ] . pendingAck ++ ;
1169
1184
@@ -1204,7 +1219,7 @@ class Http2Session extends EventEmitter {
1204
1219
destroy ( error = NGHTTP2_NO_ERROR , code ) {
1205
1220
if ( this . destroyed )
1206
1221
return ;
1207
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : destroying` ) ;
1222
+ debugSessionObj ( this , ' destroying' ) ;
1208
1223
1209
1224
if ( typeof error === 'number' ) {
1210
1225
code = error ;
@@ -1261,7 +1276,7 @@ class Http2Session extends EventEmitter {
1261
1276
close ( callback ) {
1262
1277
if ( this . closed || this . destroyed )
1263
1278
return ;
1264
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : marking session closed` ) ;
1279
+ debugSessionObj ( this , ' marking session closed' ) ;
1265
1280
this [ kState ] . flags |= SESSION_FLAGS_CLOSED ;
1266
1281
if ( typeof callback === 'function' )
1267
1282
this . once ( 'close' , callback ) ;
@@ -1432,7 +1447,7 @@ class ClientHttp2Session extends Http2Session {
1432
1447
// Submits a new HTTP2 request to the connected peer. Returns the
1433
1448
// associated Http2Stream instance.
1434
1449
request ( headers , options ) {
1435
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : initiating request` ) ;
1450
+ debugSessionObj ( this , ' initiating request' ) ;
1436
1451
1437
1452
if ( this . destroyed )
1438
1453
throw new ERR_HTTP2_INVALID_SESSION ( ) ;
@@ -1832,8 +1847,7 @@ class Http2Stream extends Duplex {
1832
1847
if ( this . pending ) {
1833
1848
this . once ( 'ready' , ( ) => this . _final ( cb ) ) ;
1834
1849
} else if ( handle !== undefined ) {
1835
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
1836
- `${ sessionName ( this [ kSession ] [ kType ] ) } ]: _final shutting down` ) ;
1850
+ debugStreamObj ( this , '_final shutting down' ) ;
1837
1851
const req = new ShutdownWrap ( ) ;
1838
1852
req . oncomplete = afterShutdown ;
1839
1853
req . callback = cb ;
@@ -1892,9 +1906,7 @@ class Http2Stream extends Duplex {
1892
1906
assertIsObject ( headers , 'headers' ) ;
1893
1907
headers = Object . assign ( Object . create ( null ) , headers ) ;
1894
1908
1895
- const session = this [ kSession ] ;
1896
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
1897
- `${ sessionName ( session [ kType ] ) } ]: sending trailers` ) ;
1909
+ debugStreamObj ( this , 'sending trailers' ) ;
1898
1910
1899
1911
this [ kUpdateTimer ] ( ) ;
1900
1912
@@ -1947,8 +1959,7 @@ class Http2Stream extends Duplex {
1947
1959
const handle = this [ kHandle ] ;
1948
1960
const id = this [ kID ] ;
1949
1961
1950
- debug ( `Http2Stream ${ this [ kID ] || '<pending>' } [Http2Session ` +
1951
- `${ sessionName ( session [ kType ] ) } ]: destroying stream` ) ;
1962
+ debugStream ( this [ kID ] || 'pending' , session [ kType ] , 'destroying stream' ) ;
1952
1963
1953
1964
const state = this [ kState ] ;
1954
1965
const sessionCode = session [ kState ] . goawayCode ||
@@ -2274,8 +2285,7 @@ class ServerHttp2Stream extends Http2Stream {
2274
2285
2275
2286
const session = this [ kSession ] ;
2276
2287
2277
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2278
- `${ sessionName ( session [ kType ] ) } ]: initiating push stream` ) ;
2288
+ debugStreamObj ( this , 'initiating push stream' ) ;
2279
2289
2280
2290
this [ kUpdateTimer ] ( ) ;
2281
2291
@@ -2355,9 +2365,7 @@ class ServerHttp2Stream extends Http2Stream {
2355
2365
assertIsObject ( options , 'options' ) ;
2356
2366
options = { ...options } ;
2357
2367
2358
- const session = this [ kSession ] ;
2359
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2360
- `${ sessionName ( session [ kType ] ) } ]: initiating response` ) ;
2368
+ debugStreamObj ( this , 'initiating response' ) ;
2361
2369
this [ kUpdateTimer ] ( ) ;
2362
2370
2363
2371
options . endStream = ! ! options . endStream ;
@@ -2430,8 +2438,7 @@ class ServerHttp2Stream extends Http2Stream {
2430
2438
2431
2439
validateNumber ( fd , 'fd' ) ;
2432
2440
2433
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2434
- `${ sessionName ( session [ kType ] ) } ]: initiating response from fd` ) ;
2441
+ debugStreamObj ( this , 'initiating response from fd' ) ;
2435
2442
this [ kUpdateTimer ] ( ) ;
2436
2443
this . ownsFd = false ;
2437
2444
@@ -2492,8 +2499,7 @@ class ServerHttp2Stream extends Http2Stream {
2492
2499
}
2493
2500
2494
2501
const session = this [ kSession ] ;
2495
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2496
- `${ sessionName ( session [ kType ] ) } ]: initiating response from file` ) ;
2502
+ debugStreamObj ( this , 'initiating response from file' ) ;
2497
2503
this [ kUpdateTimer ] ( ) ;
2498
2504
this . ownsFd = true ;
2499
2505
@@ -2527,9 +2533,7 @@ class ServerHttp2Stream extends Http2Stream {
2527
2533
assertIsObject ( headers , 'headers' ) ;
2528
2534
headers = Object . assign ( Object . create ( null ) , headers ) ;
2529
2535
2530
- const session = this [ kSession ] ;
2531
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2532
- `${ sessionName ( session [ kType ] ) } ]: sending additional headers` ) ;
2536
+ debugStreamObj ( this , 'sending additional headers' ) ;
2533
2537
2534
2538
if ( headers [ HTTP2_HEADER_STATUS ] != null ) {
2535
2539
const statusCode = headers [ HTTP2_HEADER_STATUS ] |= 0 ;
@@ -2590,8 +2594,7 @@ function socketOnError(error) {
2590
2594
// we can do and the other side is fully within its rights to do so.
2591
2595
if ( error . code === 'ECONNRESET' && session [ kState ] . goawayCode !== null )
2592
2596
return session . destroy ( ) ;
2593
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : socket error [` +
2594
- `${ error . message } ]` ) ;
2597
+ debugSessionObj ( this , 'socket error [%s]' , error . message ) ;
2595
2598
session . destroy ( error ) ;
2596
2599
}
2597
2600
}
@@ -2636,7 +2639,8 @@ function connectionListener(socket) {
2636
2639
return httpConnectionListener . call ( this , socket ) ;
2637
2640
}
2638
2641
// Let event handler deal with the socket
2639
- debug ( `Unknown protocol from ${ socket . remoteAddress } :${ socket . remotePort } ` ) ;
2642
+ debug ( 'Unknown protocol from %s:%s' ,
2643
+ socket . remoteAddress , socket . remotePort ) ;
2640
2644
if ( ! this . emit ( 'unknownProtocol' , socket ) ) {
2641
2645
// We don't know what to do, so let's just tell the other side what's
2642
2646
// going on in a format that they *might* understand.
@@ -2761,7 +2765,7 @@ function setupCompat(ev) {
2761
2765
function socketOnClose ( ) {
2762
2766
const session = this [ kSession ] ;
2763
2767
if ( session !== undefined ) {
2764
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : socket closed` ) ;
2768
+ debugSessionObj ( session , ' socket closed' ) ;
2765
2769
const err = session . connecting ? new ERR_SOCKET_CLOSED ( ) : null ;
2766
2770
const state = session [ kState ] ;
2767
2771
state . streams . forEach ( ( stream ) => stream . close ( NGHTTP2_CANCEL ) ) ;
0 commit comments