@verdaccio/middleware 6.0.0-6-next.35 → 6.0.0-6-next.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @verdaccio/middleware
2
2
 
3
+ ## 6.0.0-6-next.36
4
+
5
+ ### Patch Changes
6
+
7
+ - 9943e2b1: fix: extract logger from middleware
8
+ - @verdaccio/core@6.0.0-6-next.57
9
+ - @verdaccio/utils@6.0.0-6-next.25
10
+
3
11
  ## 6.0.0-6-next.35
4
12
 
5
13
  ### Minor Changes
@@ -1,4 +1,4 @@
1
1
  import { HttpError } from 'http-errors';
2
2
  import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
3
- export declare function handleError(err: HttpError, req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): any;
4
- export declare function errorReportingMiddleware(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void;
3
+ export declare const handleError: (logger: any) => (err: HttpError, req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) => any;
4
+ export declare const errorReportingMiddleware: (logger: any) => (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) => void;
@@ -3,15 +3,13 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.errorReportingMiddleware = errorReportingMiddleware;
7
- exports.handleError = handleError;
6
+ exports.handleError = exports.errorReportingMiddleware = void 0;
8
7
  var _debug = _interopRequireDefault(require("debug"));
9
8
  var _lodash = _interopRequireDefault(require("lodash"));
10
9
  var _core = require("@verdaccio/core");
11
- var _logger = require("@verdaccio/logger");
12
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
11
  const debug = (0, _debug.default)('verdaccio:middleware:error');
14
- function handleError(err, req, res, next) {
12
+ const handleError = logger => function handleError(err, req, res, next) {
15
13
  debug('error handler init');
16
14
  if (_lodash.default.isError(err)) {
17
15
  debug('is native error');
@@ -22,7 +20,7 @@ function handleError(err, req, res, next) {
22
20
  debug('is locals error report ref');
23
21
  // in case of very early error this middleware may not be loaded before error is generated
24
22
  // fixing that
25
- errorReportingMiddleware(req, res, _lodash.default.noop);
23
+ errorReportingMiddleware(logger)(req, res, _lodash.default.noop);
26
24
  }
27
25
  debug('set locals error report ref');
28
26
  res.locals.report_error(err);
@@ -31,10 +29,11 @@ function handleError(err, req, res, next) {
31
29
  debug('no error to report, jump next layer');
32
30
  return next(err);
33
31
  }
34
- }
32
+ };
35
33
 
36
34
  // Middleware
37
- function errorReportingMiddleware(req, res, next) {
35
+ exports.handleError = handleError;
36
+ const errorReportingMiddleware = logger => function errorReportingMiddleware(req, res, next) {
38
37
  debug('error report middleware');
39
38
  res.locals.report_error = res.locals.report_error || function (err) {
40
39
  if (err.status && err.status >= _core.HTTP_STATUS.BAD_REQUEST && err.status < 600) {
@@ -49,12 +48,12 @@ function errorReportingMiddleware(req, res, next) {
49
48
  }
50
49
  } else {
51
50
  debug('is error < 409 %o', err === null || err === void 0 ? void 0 : err.status);
52
- _logger.logger.error({
51
+ logger.error({
53
52
  err: err
54
53
  }, 'unexpected error: @{!err.message}\n@{err.stack}');
55
54
  if (!res.status || !res.send) {
56
55
  // TODO: decide which debug keep
57
- _logger.logger.error('this is an error in express.js, please report this');
56
+ logger.error('this is an error in express.js, please report this');
58
57
  debug('this is an error in express.js, please report this, destroy response %o', err);
59
58
  res.destroy();
60
59
  } else if (!res.headersSent) {
@@ -71,5 +70,6 @@ function errorReportingMiddleware(req, res, next) {
71
70
  };
72
71
  debug('error report middleware next()');
73
72
  next();
74
- }
73
+ };
74
+ exports.errorReportingMiddleware = errorReportingMiddleware;
75
75
  //# sourceMappingURL=error.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"error.js","names":["debug","buildDebug","handleError","err","req","res","next","_","isError","code","statusCode","HTTP_STATUS","NOT_MODIFIED","isFunction","locals","report_error","errorReportingMiddleware","noop","status","BAD_REQUEST","isNil","headersSent","message","error","API_ERROR","UNKNOWN_ERROR","logger","send","destroy","INTERNAL_ERROR","INTERNAL_SERVER_ERROR"],"sources":["../../src/middlewares/error.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport { HttpError } from 'http-errors';\nimport _ from 'lodash';\n\nimport { API_ERROR, HTTP_STATUS, VerdaccioError } from '@verdaccio/core';\nimport { logger } from '@verdaccio/logger';\n\nimport { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';\n\nconst debug = buildDebug('verdaccio:middleware:error');\n\nexport function handleError(\n err: HttpError,\n req: $RequestExtend,\n res: $ResponseExtend,\n next: $NextFunctionVer\n) {\n debug('error handler init');\n if (_.isError(err)) {\n debug('is native error');\n if (err.code === 'ECONNABORT' && res.statusCode === HTTP_STATUS.NOT_MODIFIED) {\n return next();\n }\n if (_.isFunction(res.locals.report_error) === false) {\n debug('is locals error report ref');\n // in case of very early error this middleware may not be loaded before error is generated\n // fixing that\n errorReportingMiddleware(req, res, _.noop);\n }\n debug('set locals error report ref');\n res.locals.report_error(err);\n } else {\n // Fall to Middleware.final\n debug('no error to report, jump next layer');\n return next(err);\n }\n}\n\n// Middleware\nexport function errorReportingMiddleware(\n req: $RequestExtend,\n res: $ResponseExtend,\n next: $NextFunctionVer\n): void {\n debug('error report middleware');\n res.locals.report_error =\n res.locals.report_error ||\n function (err: VerdaccioError): void {\n if (err.status && err.status >= HTTP_STATUS.BAD_REQUEST && err.status < 600) {\n debug('is error > 409 %o', err?.status);\n if (_.isNil(res.headersSent) === false) {\n debug('send status %o', err?.status);\n res.status(err.status);\n debug('next layer %o', err?.message);\n next({ error: err.message || API_ERROR.UNKNOWN_ERROR });\n }\n } else {\n debug('is error < 409 %o', err?.status);\n logger.error({ err: err }, 'unexpected error: @{!err.message}\\n@{err.stack}');\n if (!res.status || !res.send) {\n // TODO: decide which debug keep\n logger.error('this is an error in express.js, please report this');\n debug('this is an error in express.js, please report this, destroy response %o', err);\n res.destroy();\n } else if (!res.headersSent) {\n debug('report internal error %o', err);\n res.status(HTTP_STATUS.INTERNAL_ERROR);\n next({ error: API_ERROR.INTERNAL_SERVER_ERROR });\n } else {\n // socket should be already closed\n debug('this should not happen, otherwise report %o', err);\n }\n }\n };\n\n debug('error report middleware next()');\n next();\n}\n"],"mappings":";;;;;;;AAAA;AAEA;AAEA;AACA;AAA2C;AAI3C,MAAMA,KAAK,GAAG,IAAAC,cAAU,EAAC,4BAA4B,CAAC;AAE/C,SAASC,WAAW,CACzBC,GAAc,EACdC,GAAmB,EACnBC,GAAoB,EACpBC,IAAsB,EACtB;EACAN,KAAK,CAAC,oBAAoB,CAAC;EAC3B,IAAIO,eAAC,CAACC,OAAO,CAACL,GAAG,CAAC,EAAE;IAClBH,KAAK,CAAC,iBAAiB,CAAC;IACxB,IAAIG,GAAG,CAACM,IAAI,KAAK,YAAY,IAAIJ,GAAG,CAACK,UAAU,KAAKC,iBAAW,CAACC,YAAY,EAAE;MAC5E,OAAON,IAAI,EAAE;IACf;IACA,IAAIC,eAAC,CAACM,UAAU,CAACR,GAAG,CAACS,MAAM,CAACC,YAAY,CAAC,KAAK,KAAK,EAAE;MACnDf,KAAK,CAAC,4BAA4B,CAAC;MACnC;MACA;MACAgB,wBAAwB,CAACZ,GAAG,EAAEC,GAAG,EAAEE,eAAC,CAACU,IAAI,CAAC;IAC5C;IACAjB,KAAK,CAAC,6BAA6B,CAAC;IACpCK,GAAG,CAACS,MAAM,CAACC,YAAY,CAACZ,GAAG,CAAC;EAC9B,CAAC,MAAM;IACL;IACAH,KAAK,CAAC,qCAAqC,CAAC;IAC5C,OAAOM,IAAI,CAACH,GAAG,CAAC;EAClB;AACF;;AAEA;AACO,SAASa,wBAAwB,CACtCZ,GAAmB,EACnBC,GAAoB,EACpBC,IAAsB,EAChB;EACNN,KAAK,CAAC,yBAAyB,CAAC;EAChCK,GAAG,CAACS,MAAM,CAACC,YAAY,GACrBV,GAAG,CAACS,MAAM,CAACC,YAAY,IACvB,UAAUZ,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UAAEiB,KAAK,EAAEC,eAAS,CAACM;QAAsB,CAAC,CAAC;MAClD,CAAC,MAAM;QACL;QACA9B,KAAK,CAAC,6CAA6C,EAAEG,GAAG,CAAC;MAC3D;IACF;EACF,CAAC;EAEHH,KAAK,CAAC,gCAAgC,CAAC;EACvCM,IAAI,EAAE;AACR"}
1
+ {"version":3,"file":"error.js","names":["debug","buildDebug","handleError","logger","err","req","res","next","_","isError","code","statusCode","HTTP_STATUS","NOT_MODIFIED","isFunction","locals","report_error","errorReportingMiddleware","noop","status","BAD_REQUEST","isNil","headersSent","message","error","API_ERROR","UNKNOWN_ERROR","send","destroy","INTERNAL_ERROR","INTERNAL_SERVER_ERROR"],"sources":["../../src/middlewares/error.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport { HttpError } from 'http-errors';\nimport _ from 'lodash';\n\nimport { API_ERROR, HTTP_STATUS, VerdaccioError } from '@verdaccio/core';\n\nimport { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';\n\nconst debug = buildDebug('verdaccio:middleware:error');\n\nexport const handleError = (logger) =>\n function handleError(\n err: HttpError,\n req: $RequestExtend,\n res: $ResponseExtend,\n next: $NextFunctionVer\n ) {\n debug('error handler init');\n if (_.isError(err)) {\n debug('is native error');\n if (err.code === 'ECONNABORT' && res.statusCode === HTTP_STATUS.NOT_MODIFIED) {\n return next();\n }\n if (_.isFunction(res.locals.report_error) === false) {\n debug('is locals error report ref');\n // in case of very early error this middleware may not be loaded before error is generated\n // fixing that\n errorReportingMiddleware(logger)(req, res, _.noop);\n }\n debug('set locals error report ref');\n res.locals.report_error(err);\n } else {\n // Fall to Middleware.final\n debug('no error to report, jump next layer');\n return next(err);\n }\n };\n\n// Middleware\nexport const errorReportingMiddleware = (logger) =>\n function errorReportingMiddleware(\n req: $RequestExtend,\n res: $ResponseExtend,\n next: $NextFunctionVer\n ): void {\n debug('error report middleware');\n res.locals.report_error =\n res.locals.report_error ||\n function (err: VerdaccioError): void {\n if (err.status && err.status >= HTTP_STATUS.BAD_REQUEST && err.status < 600) {\n debug('is error > 409 %o', err?.status);\n if (_.isNil(res.headersSent) === false) {\n debug('send status %o', err?.status);\n res.status(err.status);\n debug('next layer %o', err?.message);\n next({ error: err.message || API_ERROR.UNKNOWN_ERROR });\n }\n } else {\n debug('is error < 409 %o', err?.status);\n logger.error({ err: err }, 'unexpected error: @{!err.message}\\n@{err.stack}');\n if (!res.status || !res.send) {\n // TODO: decide which debug keep\n logger.error('this is an error in express.js, please report this');\n debug('this is an error in express.js, please report this, destroy response %o', err);\n res.destroy();\n } else if (!res.headersSent) {\n debug('report internal error %o', err);\n res.status(HTTP_STATUS.INTERNAL_ERROR);\n next({ error: API_ERROR.INTERNAL_SERVER_ERROR });\n } else {\n // socket should be already closed\n debug('this should not happen, otherwise report %o', err);\n }\n }\n };\n\n debug('error report middleware next()');\n next();\n };\n"],"mappings":";;;;;;AAAA;AAEA;AAEA;AAAyE;AAIzE,MAAMA,KAAK,GAAG,IAAAC,cAAU,EAAC,4BAA4B,CAAC;AAE/C,MAAMC,WAAW,GAAIC,MAAM,IAChC,SAASD,WAAW,CAClBE,GAAc,EACdC,GAAmB,EACnBC,GAAoB,EACpBC,IAAsB,EACtB;EACAP,KAAK,CAAC,oBAAoB,CAAC;EAC3B,IAAIQ,eAAC,CAACC,OAAO,CAACL,GAAG,CAAC,EAAE;IAClBJ,KAAK,CAAC,iBAAiB,CAAC;IACxB,IAAII,GAAG,CAACM,IAAI,KAAK,YAAY,IAAIJ,GAAG,CAACK,UAAU,KAAKC,iBAAW,CAACC,YAAY,EAAE;MAC5E,OAAON,IAAI,EAAE;IACf;IACA,IAAIC,eAAC,CAACM,UAAU,CAACR,GAAG,CAACS,MAAM,CAACC,YAAY,CAAC,KAAK,KAAK,EAAE;MACnDhB,KAAK,CAAC,4BAA4B,CAAC;MACnC;MACA;MACAiB,wBAAwB,CAACd,MAAM,CAAC,CAACE,GAAG,EAAEC,GAAG,EAAEE,eAAC,CAACU,IAAI,CAAC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,CAAC;QAAEpB,GAAG,EAAEA;MAAI,CAAC,EAAE,iDAAiD,CAAC;MAC7E,IAAI,CAACE,GAAG,CAACa,MAAM,IAAI,CAACb,GAAG,CAACqB,IAAI,EAAE;QAC5B;QACAxB,MAAM,CAACqB,KAAK,CAAC,oDAAoD,CAAC;QAClExB,KAAK,CAAC,yEAAyE,EAAEI,GAAG,CAAC;QACrFE,GAAG,CAACsB,OAAO,EAAE;MACf,CAAC,MAAM,IAAI,CAACtB,GAAG,CAACgB,WAAW,EAAE;QAC3BtB,KAAK,CAAC,0BAA0B,EAAEI,GAAG,CAAC;QACtCE,GAAG,CAACa,MAAM,CAACP,iBAAW,CAACiB,cAAc,CAAC;QACtCtB,IAAI,CAAC;UAAEiB,KAAK,EAAEC,eAAS,CAACK;QAAsB,CAAC,CAAC;MAClD,CAAC,MAAM;QACL;QACA9B,KAAK,CAAC,6CAA6C,EAAEI,GAAG,CAAC;MAC3D;IACF;EACF,CAAC;EAEHJ,KAAK,CAAC,gCAAgC,CAAC;EACvCO,IAAI,EAAE;AACR,CAAC;AAAC"}
@@ -2,4 +2,4 @@ import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
2
2
  export declare const LOG_STATUS_MESSAGE = "@{status}, user: @{user}(@{remoteIP}), req: '@{request.method} @{request.url}'";
3
3
  export declare const LOG_VERDACCIO_ERROR: string;
4
4
  export declare const LOG_VERDACCIO_BYTES: string;
5
- export declare function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void;
5
+ export declare const log: (logger: any) => (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) => void;
@@ -3,10 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.LOG_VERDACCIO_ERROR = exports.LOG_VERDACCIO_BYTES = exports.LOG_STATUS_MESSAGE = void 0;
7
- exports.log = log;
6
+ exports.log = exports.LOG_VERDACCIO_ERROR = exports.LOG_VERDACCIO_BYTES = exports.LOG_STATUS_MESSAGE = void 0;
8
7
  var _lodash = _interopRequireDefault(require("lodash"));
9
- var _logger = require("@verdaccio/logger");
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
9
  // FIXME: deprecated, moved to @verdaccio/dev-commons
12
10
  const LOG_STATUS_MESSAGE = "@{status}, user: @{user}(@{remoteIP}), req: '@{request.method} @{request.url}'";
@@ -15,87 +13,90 @@ const LOG_VERDACCIO_ERROR = `${LOG_STATUS_MESSAGE}, error: @{!error}`;
15
13
  exports.LOG_VERDACCIO_ERROR = LOG_VERDACCIO_ERROR;
16
14
  const LOG_VERDACCIO_BYTES = `${LOG_STATUS_MESSAGE}, bytes: @{bytes.in}/@{bytes.out}`;
17
15
  exports.LOG_VERDACCIO_BYTES = LOG_VERDACCIO_BYTES;
18
- function log(req, res, next) {
19
- // logger
20
- req.log = _logger.logger.child({
21
- sub: 'in'
22
- });
23
- const _auth = req.headers.authorization;
24
- if (_lodash.default.isNil(_auth) === false) {
25
- req.headers.authorization = '<Classified>';
26
- }
27
- const _cookie = req.get('cookie');
28
- if (_lodash.default.isNil(_cookie) === false) {
29
- req.headers.cookie = '<Classified>';
30
- }
31
- req.url = req.originalUrl;
32
- req.log.info({
33
- req: req,
34
- ip: req.ip
35
- }, "@{ip} requested '@{req.method} @{req.url}'");
36
- req.originalUrl = req.url;
37
- if (_lodash.default.isNil(_auth) === false) {
38
- req.headers.authorization = _auth;
39
- }
40
- if (_lodash.default.isNil(_cookie) === false) {
41
- req.headers.cookie = _cookie;
42
- }
43
- let bytesin = 0;
44
- req.on('data', function (chunk) {
45
- bytesin += chunk.length;
46
- });
47
- let bytesout = 0;
48
- const _write = res.write;
49
- // FIXME: res.write should return boolean
50
- // @ts-ignore
51
- res.write = function (buf) {
52
- bytesout += buf.length;
53
- /* eslint prefer-rest-params: "off" */
54
- // @ts-ignore
55
- _write.apply(res, arguments);
56
- };
57
- const log = function () {
58
- var _req$remote_user;
59
- const forwardedFor = req.get('x-forwarded-for');
60
- const remoteAddress = req.connection.remoteAddress;
61
- const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;
62
- let message;
63
- if (res.locals._verdaccio_error) {
64
- message = LOG_VERDACCIO_ERROR;
65
- } else {
66
- message = LOG_VERDACCIO_BYTES;
16
+ const log = logger => {
17
+ return function log(req, res, next) {
18
+ // logger
19
+ req.log = logger.child({
20
+ sub: 'in'
21
+ });
22
+ const _auth = req.headers.authorization;
23
+ if (_lodash.default.isNil(_auth) === false) {
24
+ req.headers.authorization = '<Classified>';
25
+ }
26
+ const _cookie = req.get('cookie');
27
+ if (_lodash.default.isNil(_cookie) === false) {
28
+ req.headers.cookie = '<Classified>';
67
29
  }
68
30
  req.url = req.originalUrl;
69
- req.log.http({
70
- request: {
71
- method: req.method,
72
- url: req.url
73
- },
74
- user: ((_req$remote_user = req.remote_user) === null || _req$remote_user === void 0 ? void 0 : _req$remote_user.name) || null,
75
- remoteIP,
76
- status: res.statusCode,
77
- error: res.locals._verdaccio_error,
78
- bytes: {
79
- in: bytesin,
80
- out: bytesout
81
- }
82
- }, message);
31
+ req.log.info({
32
+ req: req,
33
+ ip: req.ip
34
+ }, "@{ip} requested '@{req.method} @{req.url}'");
83
35
  req.originalUrl = req.url;
84
- };
85
- req.on('close', function () {
86
- log();
87
- });
88
- const _end = res.end;
89
- // @ts-ignore
90
- res.end = function (buf) {
91
- if (buf) {
92
- bytesout += buf.length;
36
+ if (_lodash.default.isNil(_auth) === false) {
37
+ req.headers.authorization = _auth;
93
38
  }
94
- /* eslint prefer-rest-params: "off" */
39
+ if (_lodash.default.isNil(_cookie) === false) {
40
+ req.headers.cookie = _cookie;
41
+ }
42
+ let bytesin = 0;
43
+ req.on('data', function (chunk) {
44
+ bytesin += chunk.length;
45
+ });
46
+ let bytesout = 0;
47
+ const _write = res.write;
48
+ // FIXME: res.write should return boolean
49
+ // @ts-ignore
50
+ res.write = function (buf) {
51
+ bytesout += buf.length;
52
+ /* eslint prefer-rest-params: "off" */
53
+ // @ts-ignore
54
+ _write.apply(res, arguments);
55
+ };
56
+ const log = function () {
57
+ var _req$remote_user;
58
+ const forwardedFor = req.get('x-forwarded-for');
59
+ const remoteAddress = req.connection.remoteAddress;
60
+ const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;
61
+ let message;
62
+ if (res.locals._verdaccio_error) {
63
+ message = LOG_VERDACCIO_ERROR;
64
+ } else {
65
+ message = LOG_VERDACCIO_BYTES;
66
+ }
67
+ req.url = req.originalUrl;
68
+ req.log.http({
69
+ request: {
70
+ method: req.method,
71
+ url: req.url
72
+ },
73
+ user: ((_req$remote_user = req.remote_user) === null || _req$remote_user === void 0 ? void 0 : _req$remote_user.name) || null,
74
+ remoteIP,
75
+ status: res.statusCode,
76
+ error: res.locals._verdaccio_error,
77
+ bytes: {
78
+ in: bytesin,
79
+ out: bytesout
80
+ }
81
+ }, message);
82
+ req.originalUrl = req.url;
83
+ };
84
+ req.on('close', function () {
85
+ log();
86
+ });
87
+ const _end = res.end;
95
88
  // @ts-ignore
96
- _end.apply(res, arguments);
97
- log();
89
+ res.end = function (buf) {
90
+ if (buf) {
91
+ bytesout += buf.length;
92
+ }
93
+ /* eslint prefer-rest-params: "off" */
94
+ // @ts-ignore
95
+ _end.apply(res, arguments);
96
+ log();
97
+ };
98
+ next();
98
99
  };
99
- next();
100
- }
100
+ };
101
+ exports.log = log;
101
102
  //# sourceMappingURL=log.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"log.js","names":["LOG_STATUS_MESSAGE","LOG_VERDACCIO_ERROR","LOG_VERDACCIO_BYTES","log","req","res","next","logger","child","sub","_auth","headers","authorization","_","isNil","_cookie","get","cookie","url","originalUrl","info","ip","bytesin","on","chunk","length","bytesout","_write","write","buf","apply","arguments","forwardedFor","remoteAddress","connection","remoteIP","message","locals","_verdaccio_error","http","request","method","user","remote_user","name","status","statusCode","error","bytes","in","out","_end","end"],"sources":["../../src/middlewares/log.ts"],"sourcesContent":["import _ from 'lodash';\n\nimport { logger } from '@verdaccio/logger';\n\nimport { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';\n\n// FIXME: deprecated, moved to @verdaccio/dev-commons\nexport const LOG_STATUS_MESSAGE =\n \"@{status}, user: @{user}(@{remoteIP}), req: '@{request.method} @{request.url}'\";\nexport const LOG_VERDACCIO_ERROR = `${LOG_STATUS_MESSAGE}, error: @{!error}`;\nexport const LOG_VERDACCIO_BYTES = `${LOG_STATUS_MESSAGE}, bytes: @{bytes.in}/@{bytes.out}`;\n\nexport function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {\n // logger\n req.log = logger.child({ sub: 'in' });\n\n const _auth = req.headers.authorization;\n if (_.isNil(_auth) === false) {\n req.headers.authorization = '<Classified>';\n }\n\n const _cookie = req.get('cookie');\n if (_.isNil(_cookie) === false) {\n req.headers.cookie = '<Classified>';\n }\n\n req.url = req.originalUrl;\n req.log.info({ req: req, ip: req.ip }, \"@{ip} requested '@{req.method} @{req.url}'\");\n req.originalUrl = req.url;\n\n if (_.isNil(_auth) === false) {\n req.headers.authorization = _auth;\n }\n\n if (_.isNil(_cookie) === false) {\n req.headers.cookie = _cookie;\n }\n\n let bytesin = 0;\n req.on('data', function (chunk): void {\n bytesin += chunk.length;\n });\n\n let bytesout = 0;\n const _write = res.write;\n // FIXME: res.write should return boolean\n // @ts-ignore\n res.write = function (buf): boolean {\n bytesout += buf.length;\n /* eslint prefer-rest-params: \"off\" */\n // @ts-ignore\n _write.apply(res, arguments);\n };\n\n const log = function (): void {\n const forwardedFor = req.get('x-forwarded-for');\n const remoteAddress = req.connection.remoteAddress;\n const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;\n let message;\n if (res.locals._verdaccio_error) {\n message = LOG_VERDACCIO_ERROR;\n } else {\n message = LOG_VERDACCIO_BYTES;\n }\n\n req.url = req.originalUrl;\n req.log.http(\n {\n request: {\n method: req.method,\n url: req.url,\n },\n user: req.remote_user?.name || null,\n remoteIP,\n status: res.statusCode,\n error: res.locals._verdaccio_error,\n bytes: {\n in: bytesin,\n out: bytesout,\n },\n },\n message\n );\n req.originalUrl = req.url;\n };\n\n req.on('close', function (): void {\n log();\n });\n\n const _end = res.end;\n // @ts-ignore\n res.end = function (buf): void {\n if (buf) {\n bytesout += buf.length;\n }\n /* eslint prefer-rest-params: \"off\" */\n // @ts-ignore\n _end.apply(res, arguments);\n log();\n };\n next();\n}\n"],"mappings":";;;;;;;AAAA;AAEA;AAA2C;AAI3C;AACO,MAAMA,kBAAkB,GAC7B,gFAAgF;AAAC;AAC5E,MAAMC,mBAAmB,GAAI,GAAED,kBAAmB,oBAAmB;AAAC;AACtE,MAAME,mBAAmB,GAAI,GAAEF,kBAAmB,mCAAkC;AAAC;AAErF,SAASG,GAAG,CAACC,GAAmB,EAAEC,GAAoB,EAAEC,IAAsB,EAAQ;EAC3F;EACAF,GAAG,CAACD,GAAG,GAAGI,cAAM,CAACC,KAAK,CAAC;IAAEC,GAAG,EAAE;EAAK,CAAC,CAAC;EAErC,MAAMC,KAAK,GAAGN,GAAG,CAACO,OAAO,CAACC,aAAa;EACvC,IAAIC,eAAC,CAACC,KAAK,CAACJ,KAAK,CAAC,KAAK,KAAK,EAAE;IAC5BN,GAAG,CAACO,OAAO,CAACC,aAAa,GAAG,cAAc;EAC5C;EAEA,MAAMG,OAAO,GAAGX,GAAG,CAACY,GAAG,CAAC,QAAQ,CAAC;EACjC,IAAIH,eAAC,CAACC,KAAK,CAACC,OAAO,CAAC,KAAK,KAAK,EAAE;IAC9BX,GAAG,CAACO,OAAO,CAACM,MAAM,GAAG,cAAc;EACrC;EAEAb,GAAG,CAACc,GAAG,GAAGd,GAAG,CAACe,WAAW;EACzBf,GAAG,CAACD,GAAG,CAACiB,IAAI,CAAC;IAAEhB,GAAG,EAAEA,GAAG;IAAEiB,EAAE,EAAEjB,GAAG,CAACiB;EAAG,CAAC,EAAE,4CAA4C,CAAC;EACpFj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AO,EAAE;QACPC,MAAM,EAAErC,GAAG,CAACqC,MAAM;QAClBvB,GAAG,EAAEd,GAAG,CAACc;MACX,CAAC;MACDwB,IAAI,EAAE,qBAAAtC,GAAG,CAACuC,WAAW,qDAAf,iBAAiBC,IAAI,KAAI,IAAI;MACnCT,QAAQ;MACRU,MAAM,EAAExC,GAAG,CAACyC,UAAU;MACtBC,KAAK,EAAE1C,GAAG,CAACgC,MAAM,CAACC,gBAAgB;MAClCU,KAAK,EAAE;QACLC,EAAE,EAAE3B,OAAO;QACX4B,GAAG,EAAExB;MACP;IACF,CAAC,EACDU,OAAO,CACR;IACDhC,GAAG,CAACe,WAAW,GAAGf,GAAG,CAACc,GAAG;EAC3B,CAAC;EAEDd,GAAG,CAACmB,EAAE,CAAC,OAAO,EAAE,YAAkB;IAChCpB,GAAG,EAAE;EACP,CAAC,CAAC;EAEF,MAAMgD,IAAI,GAAG9C,GAAG,CAAC+C,GAAG;EACpB;EACA/C,GAAG,CAAC+C,GAAG,GAAG,UAAUvB,GAAG,EAAQ;IAC7B,IAAIA,GAAG,EAAE;MACPH,QAAQ,IAAIG,GAAG,CAACJ,MAAM;IACxB;IACA;IACA;IACA0B,IAAI,CAACrB,KAAK,CAACzB,GAAG,EAAE0B,SAAS,CAAC;IAC1B5B,GAAG,EAAE;EACP,CAAC;EACDG,IAAI,EAAE;AACR"}
1
+ {"version":3,"file":"log.js","names":["LOG_STATUS_MESSAGE","LOG_VERDACCIO_ERROR","LOG_VERDACCIO_BYTES","log","logger","req","res","next","child","sub","_auth","headers","authorization","_","isNil","_cookie","get","cookie","url","originalUrl","info","ip","bytesin","on","chunk","length","bytesout","_write","write","buf","apply","arguments","forwardedFor","remoteAddress","connection","remoteIP","message","locals","_verdaccio_error","http","request","method","user","remote_user","name","status","statusCode","error","bytes","in","out","_end","end"],"sources":["../../src/middlewares/log.ts"],"sourcesContent":["import _ from 'lodash';\n\nimport { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';\n\n// FIXME: deprecated, moved to @verdaccio/dev-commons\nexport const LOG_STATUS_MESSAGE =\n \"@{status}, user: @{user}(@{remoteIP}), req: '@{request.method} @{request.url}'\";\nexport const LOG_VERDACCIO_ERROR = `${LOG_STATUS_MESSAGE}, error: @{!error}`;\nexport const LOG_VERDACCIO_BYTES = `${LOG_STATUS_MESSAGE}, bytes: @{bytes.in}/@{bytes.out}`;\n\nexport const log = (logger) => {\n return function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {\n // logger\n req.log = logger.child({ sub: 'in' });\n\n const _auth = req.headers.authorization;\n if (_.isNil(_auth) === false) {\n req.headers.authorization = '<Classified>';\n }\n\n const _cookie = req.get('cookie');\n if (_.isNil(_cookie) === false) {\n req.headers.cookie = '<Classified>';\n }\n\n req.url = req.originalUrl;\n req.log.info({ req: req, ip: req.ip }, \"@{ip} requested '@{req.method} @{req.url}'\");\n req.originalUrl = req.url;\n\n if (_.isNil(_auth) === false) {\n req.headers.authorization = _auth;\n }\n\n if (_.isNil(_cookie) === false) {\n req.headers.cookie = _cookie;\n }\n\n let bytesin = 0;\n req.on('data', function (chunk): void {\n bytesin += chunk.length;\n });\n\n let bytesout = 0;\n const _write = res.write;\n // FIXME: res.write should return boolean\n // @ts-ignore\n res.write = function (buf): boolean {\n bytesout += buf.length;\n /* eslint prefer-rest-params: \"off\" */\n // @ts-ignore\n _write.apply(res, arguments);\n };\n\n const log = function (): void {\n const forwardedFor = req.get('x-forwarded-for');\n const remoteAddress = req.connection.remoteAddress;\n const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;\n let message;\n if (res.locals._verdaccio_error) {\n message = LOG_VERDACCIO_ERROR;\n } else {\n message = LOG_VERDACCIO_BYTES;\n }\n\n req.url = req.originalUrl;\n req.log.http(\n {\n request: {\n method: req.method,\n url: req.url,\n },\n user: req.remote_user?.name || null,\n remoteIP,\n status: res.statusCode,\n error: res.locals._verdaccio_error,\n bytes: {\n in: bytesin,\n out: bytesout,\n },\n },\n message\n );\n req.originalUrl = req.url;\n };\n\n req.on('close', function (): void {\n log();\n });\n\n const _end = res.end;\n // @ts-ignore\n res.end = function (buf): void {\n if (buf) {\n bytesout += buf.length;\n }\n /* eslint prefer-rest-params: \"off\" */\n // @ts-ignore\n _end.apply(res, arguments);\n log();\n };\n next();\n };\n};\n"],"mappings":";;;;;;AAAA;AAAuB;AAIvB;AACO,MAAMA,kBAAkB,GAC7B,gFAAgF;AAAC;AAC5E,MAAMC,mBAAmB,GAAI,GAAED,kBAAmB,oBAAmB;AAAC;AACtE,MAAME,mBAAmB,GAAI,GAAEF,kBAAmB,mCAAkC;AAAC;AAErF,MAAMG,GAAG,GAAIC,MAAM,IAAK;EAC7B,OAAO,SAASD,GAAG,CAACE,GAAmB,EAAEC,GAAoB,EAAEC,IAAsB,EAAQ;IAC3F;IACAF,GAAG,CAACF,GAAG,GAAGC,MAAM,CAACI,KAAK,CAAC;MAAEC,GAAG,EAAE;IAAK,CAAC,CAAC;IAErC,MAAMC,KAAK,GAAGL,GAAG,CAACM,OAAO,CAACC,aAAa;IACvC,IAAIC,eAAC,CAACC,KAAK,CAACJ,KAAK,CAAC,KAAK,KAAK,EAAE;MAC5BL,GAAG,CAACM,OAAO,CAACC,aAAa,GAAG,cAAc;IAC5C;IAEA,MAAMG,OAAO,GAAGV,GAAG,CAACW,GAAG,CAAC,QAAQ,CAAC;IACjC,IAAIH,eAAC,CAACC,KAAK,CAACC,OAAO,CAAC,KAAK,KAAK,EAAE;MAC9BV,GAAG,CAACM,OAAO,CAACM,MAAM,GAAG,cAAc;IACrC;IAEAZ,GAAG,CAACa,GAAG,GAAGb,GAAG,CAACc,WAAW;IACzBd,GAAG,CAACF,GAAG,CAACiB,IAAI,CAAC;MAAEf,GAAG,EAAEA,GAAG;MAAEgB,EAAE,EAAEhB,GAAG,CAACgB;IAAG,CAAC,EAAE,4CAA4C,CAAC;IACpFhB,GAAG,CAACc,WAAW,GAAGd,GAAG,CAACa,GAAG;IA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AM;UAClBvB,GAAG,EAAEb,GAAG,CAACa;QACX,CAAC;QACDwB,IAAI,EAAE,qBAAArC,GAAG,CAACsC,WAAW,qDAAf,iBAAiBC,IAAI,KAAI,IAAI;QACnCT,QAAQ;QACRU,MAAM,EAAEvC,GAAG,CAACwC,UAAU;QACtBC,KAAK,EAAEzC,GAAG,CAAC+B,MAAM,CAACC,gBAAgB;QAClCU,KAAK,EAAE;UACLC,EAAE,EAAE3B,OAAO;UACX4B,GAAG,EAAExB;QACP;MACF,CAAC,EACDU,OAAO,CACR;MACD/B,GAAG,CAACc,WAAW,GAAGd,GAAG,CAACa,GAAG;IAC3B,CAAC;IAEDb,GAAG,CAACkB,EAAE,CAAC,OAAO,EAAE,YAAkB;MAChCpB,GAAG,EAAE;IACP,CAAC,CAAC;IAEF,MAAMgD,IAAI,GAAG7C,GAAG,CAAC8C,GAAG;IACpB;IACA9C,GAAG,CAAC8C,GAAG,GAAG,UAAUvB,GAAG,EAAQ;MAC7B,IAAIA,GAAG,EAAE;QACPH,QAAQ,IAAIG,GAAG,CAACJ,MAAM;MACxB;MACA;MACA;MACA0B,IAAI,CAACrB,KAAK,CAACxB,GAAG,EAAEyB,SAAS,CAAC;MAC1B5B,GAAG,EAAE;IACP,CAAC;IACDI,IAAI,EAAE;EACR,CAAC;AACH,CAAC;AAAC"}
package/jest.config.js CHANGED
@@ -4,7 +4,7 @@ module.exports = Object.assign({}, config, {
4
4
  coverageThreshold: {
5
5
  global: {
6
6
  lines: 67,
7
- functions: 80,
7
+ functions: 75,
8
8
  branches: 56,
9
9
  statements: 67,
10
10
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verdaccio/middleware",
3
- "version": "6.0.0-6-next.35",
3
+ "version": "6.0.0-6-next.36",
4
4
  "description": "loaders logic",
5
5
  "main": "./build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -26,13 +26,11 @@
26
26
  "verdaccio"
27
27
  ],
28
28
  "engines": {
29
- "node": ">=14",
30
- "npm": ">=6"
29
+ "node": ">=12"
31
30
  },
32
31
  "dependencies": {
33
- "@verdaccio/core": "6.0.0-6-next.56",
34
- "@verdaccio/logger": "6.0.0-6-next.24",
35
- "@verdaccio/utils": "6.0.0-6-next.24",
32
+ "@verdaccio/core": "6.0.0-6-next.57",
33
+ "@verdaccio/utils": "6.0.0-6-next.25",
36
34
  "debug": "4.3.4",
37
35
  "lodash": "4.17.21",
38
36
  "mime": "2.6.0"
@@ -42,6 +40,7 @@
42
40
  "url": "https://opencollective.com/verdaccio"
43
41
  },
44
42
  "devDependencies": {
43
+ "@verdaccio/logger": "6.0.0-6-next.25",
45
44
  "body-parser": "1.20.1",
46
45
  "supertest": "6.3.3"
47
46
  },
@@ -3,76 +3,77 @@ import { HttpError } from 'http-errors';
3
3
  import _ from 'lodash';
4
4
 
5
5
  import { API_ERROR, HTTP_STATUS, VerdaccioError } from '@verdaccio/core';
6
- import { logger } from '@verdaccio/logger';
7
6
 
8
7
  import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
9
8
 
10
9
  const debug = buildDebug('verdaccio:middleware:error');
11
10
 
12
- export function handleError(
13
- err: HttpError,
14
- req: $RequestExtend,
15
- res: $ResponseExtend,
16
- next: $NextFunctionVer
17
- ) {
18
- debug('error handler init');
19
- if (_.isError(err)) {
20
- debug('is native error');
21
- if (err.code === 'ECONNABORT' && res.statusCode === HTTP_STATUS.NOT_MODIFIED) {
22
- return next();
23
- }
24
- if (_.isFunction(res.locals.report_error) === false) {
25
- debug('is locals error report ref');
26
- // in case of very early error this middleware may not be loaded before error is generated
27
- // fixing that
28
- errorReportingMiddleware(req, res, _.noop);
11
+ export const handleError = (logger) =>
12
+ function handleError(
13
+ err: HttpError,
14
+ req: $RequestExtend,
15
+ res: $ResponseExtend,
16
+ next: $NextFunctionVer
17
+ ) {
18
+ debug('error handler init');
19
+ if (_.isError(err)) {
20
+ debug('is native error');
21
+ if (err.code === 'ECONNABORT' && res.statusCode === HTTP_STATUS.NOT_MODIFIED) {
22
+ return next();
23
+ }
24
+ if (_.isFunction(res.locals.report_error) === false) {
25
+ debug('is locals error report ref');
26
+ // in case of very early error this middleware may not be loaded before error is generated
27
+ // fixing that
28
+ errorReportingMiddleware(logger)(req, res, _.noop);
29
+ }
30
+ debug('set locals error report ref');
31
+ res.locals.report_error(err);
32
+ } else {
33
+ // Fall to Middleware.final
34
+ debug('no error to report, jump next layer');
35
+ return next(err);
29
36
  }
30
- debug('set locals error report ref');
31
- res.locals.report_error(err);
32
- } else {
33
- // Fall to Middleware.final
34
- debug('no error to report, jump next layer');
35
- return next(err);
36
- }
37
- }
37
+ };
38
38
 
39
39
  // Middleware
40
- export function errorReportingMiddleware(
41
- req: $RequestExtend,
42
- res: $ResponseExtend,
43
- next: $NextFunctionVer
44
- ): void {
45
- debug('error report middleware');
46
- res.locals.report_error =
47
- res.locals.report_error ||
48
- function (err: VerdaccioError): void {
49
- if (err.status && err.status >= HTTP_STATUS.BAD_REQUEST && err.status < 600) {
50
- debug('is error > 409 %o', err?.status);
51
- if (_.isNil(res.headersSent) === false) {
52
- debug('send status %o', err?.status);
53
- res.status(err.status);
54
- debug('next layer %o', err?.message);
55
- next({ error: err.message || API_ERROR.UNKNOWN_ERROR });
56
- }
57
- } else {
58
- debug('is error < 409 %o', err?.status);
59
- logger.error({ err: err }, 'unexpected error: @{!err.message}\n@{err.stack}');
60
- if (!res.status || !res.send) {
61
- // TODO: decide which debug keep
62
- logger.error('this is an error in express.js, please report this');
63
- debug('this is an error in express.js, please report this, destroy response %o', err);
64
- res.destroy();
65
- } else if (!res.headersSent) {
66
- debug('report internal error %o', err);
67
- res.status(HTTP_STATUS.INTERNAL_ERROR);
68
- next({ error: API_ERROR.INTERNAL_SERVER_ERROR });
40
+ export const errorReportingMiddleware = (logger) =>
41
+ function errorReportingMiddleware(
42
+ req: $RequestExtend,
43
+ res: $ResponseExtend,
44
+ next: $NextFunctionVer
45
+ ): void {
46
+ debug('error report middleware');
47
+ res.locals.report_error =
48
+ res.locals.report_error ||
49
+ function (err: VerdaccioError): void {
50
+ if (err.status && err.status >= HTTP_STATUS.BAD_REQUEST && err.status < 600) {
51
+ debug('is error > 409 %o', err?.status);
52
+ if (_.isNil(res.headersSent) === false) {
53
+ debug('send status %o', err?.status);
54
+ res.status(err.status);
55
+ debug('next layer %o', err?.message);
56
+ next({ error: err.message || API_ERROR.UNKNOWN_ERROR });
57
+ }
69
58
  } else {
70
- // socket should be already closed
71
- debug('this should not happen, otherwise report %o', err);
59
+ debug('is error < 409 %o', err?.status);
60
+ logger.error({ err: err }, 'unexpected error: @{!err.message}\n@{err.stack}');
61
+ if (!res.status || !res.send) {
62
+ // TODO: decide which debug keep
63
+ logger.error('this is an error in express.js, please report this');
64
+ debug('this is an error in express.js, please report this, destroy response %o', err);
65
+ res.destroy();
66
+ } else if (!res.headersSent) {
67
+ debug('report internal error %o', err);
68
+ res.status(HTTP_STATUS.INTERNAL_ERROR);
69
+ next({ error: API_ERROR.INTERNAL_SERVER_ERROR });
70
+ } else {
71
+ // socket should be already closed
72
+ debug('this should not happen, otherwise report %o', err);
73
+ }
72
74
  }
73
- }
74
- };
75
+ };
75
76
 
76
- debug('error report middleware next()');
77
- next();
78
- }
77
+ debug('error report middleware next()');
78
+ next();
79
+ };
@@ -1,7 +1,5 @@
1
1
  import _ from 'lodash';
2
2
 
3
- import { logger } from '@verdaccio/logger';
4
-
5
3
  import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
6
4
 
7
5
  // FIXME: deprecated, moved to @verdaccio/dev-commons
@@ -10,94 +8,96 @@ export const LOG_STATUS_MESSAGE =
10
8
  export const LOG_VERDACCIO_ERROR = `${LOG_STATUS_MESSAGE}, error: @{!error}`;
11
9
  export const LOG_VERDACCIO_BYTES = `${LOG_STATUS_MESSAGE}, bytes: @{bytes.in}/@{bytes.out}`;
12
10
 
13
- export function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
14
- // logger
15
- req.log = logger.child({ sub: 'in' });
11
+ export const log = (logger) => {
12
+ return function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
13
+ // logger
14
+ req.log = logger.child({ sub: 'in' });
16
15
 
17
- const _auth = req.headers.authorization;
18
- if (_.isNil(_auth) === false) {
19
- req.headers.authorization = '<Classified>';
20
- }
16
+ const _auth = req.headers.authorization;
17
+ if (_.isNil(_auth) === false) {
18
+ req.headers.authorization = '<Classified>';
19
+ }
21
20
 
22
- const _cookie = req.get('cookie');
23
- if (_.isNil(_cookie) === false) {
24
- req.headers.cookie = '<Classified>';
25
- }
21
+ const _cookie = req.get('cookie');
22
+ if (_.isNil(_cookie) === false) {
23
+ req.headers.cookie = '<Classified>';
24
+ }
26
25
 
27
- req.url = req.originalUrl;
28
- req.log.info({ req: req, ip: req.ip }, "@{ip} requested '@{req.method} @{req.url}'");
29
- req.originalUrl = req.url;
26
+ req.url = req.originalUrl;
27
+ req.log.info({ req: req, ip: req.ip }, "@{ip} requested '@{req.method} @{req.url}'");
28
+ req.originalUrl = req.url;
30
29
 
31
- if (_.isNil(_auth) === false) {
32
- req.headers.authorization = _auth;
33
- }
30
+ if (_.isNil(_auth) === false) {
31
+ req.headers.authorization = _auth;
32
+ }
34
33
 
35
- if (_.isNil(_cookie) === false) {
36
- req.headers.cookie = _cookie;
37
- }
34
+ if (_.isNil(_cookie) === false) {
35
+ req.headers.cookie = _cookie;
36
+ }
38
37
 
39
- let bytesin = 0;
40
- req.on('data', function (chunk): void {
41
- bytesin += chunk.length;
42
- });
38
+ let bytesin = 0;
39
+ req.on('data', function (chunk): void {
40
+ bytesin += chunk.length;
41
+ });
43
42
 
44
- let bytesout = 0;
45
- const _write = res.write;
46
- // FIXME: res.write should return boolean
47
- // @ts-ignore
48
- res.write = function (buf): boolean {
49
- bytesout += buf.length;
50
- /* eslint prefer-rest-params: "off" */
43
+ let bytesout = 0;
44
+ const _write = res.write;
45
+ // FIXME: res.write should return boolean
51
46
  // @ts-ignore
52
- _write.apply(res, arguments);
53
- };
47
+ res.write = function (buf): boolean {
48
+ bytesout += buf.length;
49
+ /* eslint prefer-rest-params: "off" */
50
+ // @ts-ignore
51
+ _write.apply(res, arguments);
52
+ };
54
53
 
55
- const log = function (): void {
56
- const forwardedFor = req.get('x-forwarded-for');
57
- const remoteAddress = req.connection.remoteAddress;
58
- const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;
59
- let message;
60
- if (res.locals._verdaccio_error) {
61
- message = LOG_VERDACCIO_ERROR;
62
- } else {
63
- message = LOG_VERDACCIO_BYTES;
64
- }
54
+ const log = function (): void {
55
+ const forwardedFor = req.get('x-forwarded-for');
56
+ const remoteAddress = req.connection.remoteAddress;
57
+ const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;
58
+ let message;
59
+ if (res.locals._verdaccio_error) {
60
+ message = LOG_VERDACCIO_ERROR;
61
+ } else {
62
+ message = LOG_VERDACCIO_BYTES;
63
+ }
65
64
 
66
- req.url = req.originalUrl;
67
- req.log.http(
68
- {
69
- request: {
70
- method: req.method,
71
- url: req.url,
72
- },
73
- user: req.remote_user?.name || null,
74
- remoteIP,
75
- status: res.statusCode,
76
- error: res.locals._verdaccio_error,
77
- bytes: {
78
- in: bytesin,
79
- out: bytesout,
65
+ req.url = req.originalUrl;
66
+ req.log.http(
67
+ {
68
+ request: {
69
+ method: req.method,
70
+ url: req.url,
71
+ },
72
+ user: req.remote_user?.name || null,
73
+ remoteIP,
74
+ status: res.statusCode,
75
+ error: res.locals._verdaccio_error,
76
+ bytes: {
77
+ in: bytesin,
78
+ out: bytesout,
79
+ },
80
80
  },
81
- },
82
- message
83
- );
84
- req.originalUrl = req.url;
85
- };
81
+ message
82
+ );
83
+ req.originalUrl = req.url;
84
+ };
86
85
 
87
- req.on('close', function (): void {
88
- log();
89
- });
86
+ req.on('close', function (): void {
87
+ log();
88
+ });
90
89
 
91
- const _end = res.end;
92
- // @ts-ignore
93
- res.end = function (buf): void {
94
- if (buf) {
95
- bytesout += buf.length;
96
- }
97
- /* eslint prefer-rest-params: "off" */
90
+ const _end = res.end;
98
91
  // @ts-ignore
99
- _end.apply(res, arguments);
100
- log();
92
+ res.end = function (buf): void {
93
+ if (buf) {
94
+ bytesout += buf.length;
95
+ }
96
+ /* eslint prefer-rest-params: "off" */
97
+ // @ts-ignore
98
+ _end.apply(res, arguments);
99
+ log();
100
+ };
101
+ next();
101
102
  };
102
- next();
103
- }
103
+ };
package/test/log.spec.ts CHANGED
@@ -2,7 +2,7 @@ import path from 'path';
2
2
  import request from 'supertest';
3
3
 
4
4
  import { HTTP_STATUS } from '@verdaccio/core';
5
- import { setup } from '@verdaccio/logger';
5
+ import { logger, setup } from '@verdaccio/logger';
6
6
 
7
7
  import { log } from '../src';
8
8
  import { getApp } from './helper';
@@ -17,7 +17,7 @@ setup({
17
17
  test('should log request', async () => {
18
18
  const app = getApp([]);
19
19
  // @ts-ignore
20
- app.use(log);
20
+ app.use(log(logger));
21
21
  // @ts-ignore
22
22
  app.get('/:package', (req, res) => {
23
23
  res.status(HTTP_STATUS.OK).json({});