@@ -174,102 +174,106 @@ export const LOG_STATUS_MESSAGE = "@{status}, user: @{user}(@{remoteIP}), req: '
174
174
export const LOG_VERDACCIO_ERROR = `${ LOG_STATUS_MESSAGE } , error: @{!error}` ;
175
175
export const LOG_VERDACCIO_BYTES = `${ LOG_STATUS_MESSAGE } , bytes: @{bytes.in}/@{bytes.out}` ;
176
176
177
- export function log ( req : $RequestExtend , res : $ResponseExtend , next : $NextFunctionVer ) : void {
178
- // logger
179
- req . log = logger . child ( { sub : 'in' } ) ;
177
+ export function log ( config : Config ) {
178
+ return function ( req : $RequestExtend , res : $ResponseExtend , next : $NextFunctionVer ) : void {
179
+ // logger
180
+ req . log = logger . child ( { sub : 'in' } ) ;
181
+
182
+ const _auth = req . headers . authorization ;
183
+ if ( _ . isNil ( _auth ) === false ) {
184
+ req . headers . authorization = '<Classified>' ;
185
+ }
180
186
181
- const _auth = req . headers . authorization ;
182
- if ( _ . isNil ( _auth ) === false ) {
183
- req . headers . authorization = '<Classified>' ;
184
- }
187
+ const _cookie = req . headers . cookie ;
188
+ if ( _ . isNil ( _cookie ) === false ) {
189
+ req . headers . cookie = '<Classified>' ;
190
+ }
185
191
186
- const _cookie = req . headers . cookie ;
187
- if ( _ . isNil ( _cookie ) === false ) {
188
- req . headers . cookie = '<Classified>' ;
189
- }
192
+ req . url = req . originalUrl ;
193
+ req . log . info ( { req : req , ip : req . ip } , "@{ip} requested '@{req.method} @{req.url}'" ) ;
194
+ req . originalUrl = req . url ;
190
195
191
- req . url = req . originalUrl ;
192
- req . log . info ( { req : req , ip : req . ip } , "@{ip} requested '@{req.method} @{req.url}'" ) ;
193
- req . originalUrl = req . url ;
196
+ if ( _ . isNil ( _auth ) === false ) {
197
+ req . headers . authorization = _auth ;
198
+ }
194
199
195
- if ( _ . isNil ( _auth ) === false ) {
196
- req . headers . authorization = _auth ;
197
- }
200
+ if ( _ . isNil ( _cookie ) === false ) {
201
+ req . headers . cookie = _cookie ;
202
+ }
198
203
199
- if ( _ . isNil ( _cookie ) === false ) {
200
- req . headers . cookie = _cookie ;
201
- }
204
+ let bytesin = 0 ;
205
+ if ( config ?. experiments ?. bytesin_off !== true ) {
206
+ req . on ( 'data' , function ( chunk ) : void {
207
+ bytesin += chunk . length ;
208
+ } ) ;
209
+ }
202
210
203
- let bytesin = 0 ;
204
- req . on ( 'data' , function ( chunk ) : void {
205
- bytesin += chunk . length ;
206
- } ) ;
207
-
208
- let bytesout = 0 ;
209
- const _write = res . write ;
210
- // FIXME: res.write should return boolean
211
- // @ts -ignore
212
- res . write = function ( buf ) : boolean {
213
- bytesout += buf . length ;
214
- /* eslint prefer-rest-params: "off" */
211
+ let bytesout = 0 ;
212
+ const _write = res . write ;
213
+ // FIXME: res.write should return boolean
215
214
// @ts -ignore
216
- _write . apply ( res , arguments ) ;
217
- } ;
215
+ res . write = function ( buf ) : boolean {
216
+ bytesout += buf . length ;
217
+ /* eslint prefer-rest-params: "off" */
218
+ // @ts -ignore
219
+ _write . apply ( res , arguments ) ;
220
+ } ;
218
221
219
- let logHasBeenCalled = false ;
220
- const log = function ( ) : void {
221
- if ( logHasBeenCalled ) {
222
- return ;
223
- }
224
- logHasBeenCalled = true ;
225
-
226
- const forwardedFor = req . headers [ 'x-forwarded-for' ] ;
227
- const remoteAddress = req . connection . remoteAddress ;
228
- const remoteIP = forwardedFor ? `${ forwardedFor } via ${ remoteAddress } ` : remoteAddress ;
229
- let message ;
230
- if ( res . _verdaccio_error ) {
231
- message = LOG_VERDACCIO_ERROR ;
232
- } else {
233
- message = LOG_VERDACCIO_BYTES ;
234
- }
222
+ let logHasBeenCalled = false ;
223
+ const log = function ( ) : void {
224
+ if ( logHasBeenCalled ) {
225
+ return ;
226
+ }
227
+ logHasBeenCalled = true ;
228
+
229
+ const forwardedFor = req . headers [ 'x-forwarded-for' ] ;
230
+ const remoteAddress = req . connection . remoteAddress ;
231
+ const remoteIP = forwardedFor ? `${ forwardedFor } via ${ remoteAddress } ` : remoteAddress ;
232
+ let message ;
233
+ if ( res . _verdaccio_error ) {
234
+ message = LOG_VERDACCIO_ERROR ;
235
+ } else {
236
+ message = LOG_VERDACCIO_BYTES ;
237
+ }
235
238
236
- req . url = req . originalUrl ;
237
- req . log . warn (
238
- {
239
- request : {
240
- method : req . method ,
241
- url : req . url ,
239
+ req . url = req . originalUrl ;
240
+ req . log . warn (
241
+ {
242
+ request : {
243
+ method : req . method ,
244
+ url : req . url ,
245
+ } ,
246
+ level : 35 , // http
247
+ user : ( req . remote_user && req . remote_user . name ) || null ,
248
+ remoteIP,
249
+ status : res . statusCode ,
250
+ error : res . _verdaccio_error ,
251
+ bytes : {
252
+ in : bytesin ,
253
+ out : bytesout ,
254
+ } ,
242
255
} ,
243
- level : 35 , // http
244
- user : ( req . remote_user && req . remote_user . name ) || null ,
245
- remoteIP,
246
- status : res . statusCode ,
247
- error : res . _verdaccio_error ,
248
- bytes : {
249
- in : bytesin ,
250
- out : bytesout ,
251
- } ,
252
- } ,
253
- message
254
- ) ;
255
- req . originalUrl = req . url ;
256
- } ;
256
+ message
257
+ ) ;
258
+ req . originalUrl = req . url ;
259
+ } ;
257
260
258
- req . on ( 'close' , function ( ) : void {
259
- log ( ) ;
260
- } ) ;
261
+ req . on ( 'close' , function ( ) : void {
262
+ log ( ) ;
263
+ } ) ;
261
264
262
- const _end = res . end ;
263
- res . end = function ( buf ) : void {
264
- if ( buf ) {
265
- bytesout += buf . length ;
266
- }
267
- /* eslint prefer-rest-params: "off" */
268
- // @ts -ignore
269
- _end . apply ( res , arguments ) ;
270
- log ( ) ;
271
- } ;
272
- next ( ) ;
265
+ const _end = res . end ;
266
+ res . end = function ( buf ) : void {
267
+ if ( buf ) {
268
+ bytesout += buf . length ;
269
+ }
270
+ /* eslint prefer-rest-params: "off" */
271
+ // @ts -ignore
272
+ _end . apply ( res , arguments ) ;
273
+ log ( ) ;
274
+ } ;
275
+ next ( ) ;
276
+ }
273
277
}
274
278
275
279
// Middleware
0 commit comments