@@ -52,10 +52,10 @@ export class BareRequest {
5252 sent = false ;
5353
5454 private cache = true ;
55- private startTime : [ seconds : number , nanoseconds : number ] ;
55+ private startTime ? : [ seconds : number , nanoseconds : number ] ;
5656 private startDate = new Date ( ) ;
5757 private remoteClient = '' ;
58- private countTimeFormat : 'ms' | 's' = 's' ;
58+ private requestTimeFormat ? : 'ms' | 's' ;
5959 private headers : { [ header : string ] : string | string [ ] } = { } ;
6060 private cookies : { [ cooke : string ] : string } = { } ;
6161 private contentType ?: keyof typeof ContentType ;
@@ -64,20 +64,27 @@ export class BareRequest {
6464 constructor (
6565 public _originalRequest : IncomingMessage ,
6666 public _originalResponse : ServerResponse ,
67- logging ?: boolean ,
67+ options ?: { logging ?: boolean ; requestTimeFormat ?: 'ms' | 's' } ,
6868 ) {
6969 this . ID = { code : ( _originalRequest . headers [ 'x-request-id' ] as string ) || generateId ( ) } ;
7070 this . remoteIp = _originalRequest . socket . remoteAddress ;
7171 this . contentType = this . _originalRequest . headers [ 'content-type' ] as any ;
7272 this . requestHeaders = this . _originalRequest . headers ;
7373
74- _originalRequest [ 'flow' ] = this ; // to receive an id later on in the route handler
74+ _originalRequest [ 'flow' ] = this ; // to receive flow object later on in the route handler
7575
76- this . setHeaders ( { 'Content-Type' : 'text/plain' , 'X-Request-Id' : this . ID . code } ) ;
77- this . startTime = process . hrtime ( ) ;
76+ this . setHeaders ( {
77+ 'Content-Type' : 'text/plain; charset=utf-8' ,
78+ 'X-Request-Id' : this . ID . code ,
79+ } ) ;
80+
81+ if ( options ?. requestTimeFormat ) {
82+ this . startTime = process . hrtime ( ) ;
83+ this . requestTimeFormat = options . requestTimeFormat ;
84+ }
7885
7986 // call logging section
80- if ( logging === true ) {
87+ if ( options ?. logging === true ) {
8188 _originalResponse . on ( 'close' , ( ) =>
8289 logHttp (
8390 this . headers ,
@@ -140,22 +147,20 @@ export class BareRequest {
140147 }
141148
142149 private setRequestTime ( ) {
150+ if ( ! this . requestTimeFormat ) return ;
151+
143152 const diff = process . hrtime ( this . startTime ) ;
144153
145154 const time =
146- diff [ 0 ] * ( this . countTimeFormat === 's' ? 1 : 1e3 ) +
147- diff [ 1 ] * ( this . countTimeFormat === 's' ? 1e-9 : 1e-6 ) ;
155+ diff [ 0 ] * ( this . requestTimeFormat === 's' ? 1 : 1e3 ) +
156+ diff [ 1 ] * ( this . requestTimeFormat === 's' ? 1e-9 : 1e-6 ) ;
148157
149158 this . setHeaders ( {
150159 'X-Processing-Time' : time ,
151- 'X-Processing-Time-Mode' : this . countTimeFormat === 's' ? 'seconds' : 'milliseconds' ,
160+ 'X-Processing-Time-Mode' : this . requestTimeFormat === 's' ? 'seconds' : 'milliseconds' ,
152161 } ) ;
153162 }
154163
155- private setTimeFormat ( format : 's' | 'ms' ) {
156- this . countTimeFormat = format ;
157- }
158-
159164 private cleanHeader ( header : string ) {
160165 delete this . headers [ header ] ;
161166 }
@@ -245,7 +250,8 @@ export class BareRequest {
245250 logMe . error ( "Tying to send into closed client's stream" ) ;
246251 return ;
247252 }
248- if ( this . _originalResponse . headersSent ) {
253+
254+ if ( this . _originalResponse . headersSent || this . sent ) {
249255 logMe . error ( 'Trying to send with the headers already sent' ) ;
250256 return ;
251257 }
0 commit comments