@@ -8,90 +8,96 @@ var makeId = require('./id');
88
99var sp = ' ' ;
1010
11- module . exports = new Volleyball ( ) ;
11+ module . exports = Volleyball ( ) ; // eslint-disable-line new-cap
1212
1313function Volleyball ( ) {
14- var config = arguments . length <= 0 || arguments [ 0 ] === undefined ? { } : arguments [ 0 ] ;
14+ var config = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { } ;
1515
16- var logger = getLogger ( config . debug ) ;
16+
17+ // items shared across multiple req-res cycles, for a given volleyball
18+ var log = getLogger ( config . debug ) ;
1719
1820 function volleyball ( req , res , next ) {
19- var shared = {
20- logger : logger ,
21+ // items shared between the request and response of just one cycle
22+ var cycle = {
23+ log : log ,
2124 id : makeId ( ) ,
2225 time : process . hrtime ( )
2326 } ;
2427
25- logReq ( req , shared ) ;
26-
28+ logReq ( req , cycle ) ;
2729 res . on ( 'finish' , function ( ) {
28- return logRes ( res , shared ) ;
30+ return logRes ( res , cycle ) ;
2931 } ) ;
3032 res . on ( 'close' , function ( ) {
31- return logClose ( res , shared ) ;
33+ return logClose ( res , cycle ) ;
3234 } ) ;
3335
3436 next ( ) ;
3537 }
36- volleyball . custom = volleyballFactory ;
3738
38- return volleyball ;
39- }
39+ // factory method which does not expose any of the library internals
40+ volleyball . custom = function ( ) {
41+ return Volleyball . apply ( undefined , arguments ) ;
42+ } ; // eslint-disable-line new-cap
4043
41- function volleyballFactory ( ) {
42- for ( var _len = arguments . length , args = Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
43- args [ _key ] = arguments [ _key ] ;
44- }
45-
46- return new ( Function . prototype . bind . apply ( Volleyball , [ null ] . concat ( args ) ) ) ( ) ;
44+ return volleyball ;
4745}
4846
4947function getLogger ( debugChoice ) {
5048 if ( ! debugChoice ) return defaultLogger ;
5149 if ( debugChoice === true ) return debug ( 'http' ) ;
5250 if ( typeof debugChoice === 'string' ) return debug ( debugChoice ) ;
5351 if ( typeof debugChoice === 'function' ) return debugChoice ;
52+ throw Error ( 'Invalid option for debug' ) ;
5453}
5554
5655function defaultLogger ( str ) {
5756 process . stdout . write ( str + '\n' ) ;
5857}
5958
60- function logReq ( req , shared ) {
59+ function logReq ( req , cycle ) {
6160 var bytes = + req . headers [ 'content-length' ] ;
6261 var type = req . headers [ 'content-type' ] ;
6362
64- var reqLine = shared . id + ' ' + chalk . dim ( '——>' ) + ' ' ;
63+ var reqLine = cycle . id + ' ' + chalk . dim ( '——>' ) + ' ' ;
6564 reqLine += chalk . bold . underline ( req . method ) + ' ' + req . url + ' ' ;
6665 if ( bytes ) reqLine += chalk . blue ( filesize ( bytes ) ) + sp ;
6766 if ( type ) reqLine += chalk . blue . dim ( type ) ;
6867
69- shared . logger ( reqLine ) ;
68+ cycle . log ( reqLine ) ;
7069}
7170
72- function logRes ( res , shared ) {
71+ function logRes ( res , cycle ) {
7372 var status = res . statusCode ;
7473 var meaning = http . STATUS_CODES [ status ] ;
7574 var bytes = + res . getHeader ( 'content-length' ) ;
7675 var type = res . getHeader ( 'content-type' ) ;
7776
78- var statusColor = void 0 ;
79- if ( status >= 500 ) statusColor = 'red' ; else if ( status >= 400 ) statusColor = 'yellow' ; else if ( status >= 300 ) statusColor = 'cyan' ; else if ( status >= 200 ) statusColor = 'green' ; else statusColor = 'reset' ;
77+ var statusColor = colorForStatus ( status ) ;
8078
81- var resLine = shared . id + ' ' + chalk . dim ( '<——' ) + ' ' ;
79+ var resLine = cycle . id + ' ' + chalk . dim ( '<——' ) + ' ' ;
8280 resLine += chalk [ statusColor ] ( status + ' ' + meaning ) + sp ;
8381 if ( bytes ) resLine += chalk . blue ( filesize ( bytes ) ) + sp ;
8482 if ( type ) resLine += chalk . blue . dim ( type ) + sp ;
85- resLine += chalk . dim ( '(<— > ' + msDiff ( shared . time ) + ' ms)' ) ;
83+ resLine += chalk . dim ( '(<\u2014 > ' + msDiff ( cycle . time ) + ' ms)' ) ;
8684
87- shared . logger ( resLine ) ;
85+ cycle . log ( resLine ) ;
8886}
8987
90- function logClose ( res , shared ) {
91- var closeLine = shared . id + ' ' + chalk . dim ( '—X—' ) + ' ' ;
88+ function logClose ( res , cycle ) {
89+ var closeLine = cycle . id + ' ' + chalk . dim ( '—X—' ) + ' ' ;
9290 closeLine += chalk . red ( 'connection closed before res end/flush' ) ;
9391
94- shared . logger ( closeLine ) ;
92+ cycle . log ( closeLine ) ;
93+ }
94+
95+ function colorForStatus ( status ) {
96+ if ( status >= 500 ) return 'red' ;
97+ if ( status >= 400 ) return 'yellow' ;
98+ if ( status >= 300 ) return 'cyan' ;
99+ if ( status >= 200 ) return 'green' ;
100+ return 'reset' ;
95101}
96102
97103function msDiff ( time ) {
0 commit comments