Skip to content

Commit e2f7bb2

Browse files
frimuchkovfrimuchkov
and
frimuchkov
authoredDec 2, 2020
feat: Set bytesin log as optional (#2013)
#1912 Co-authored-by: frimuchkov <frimuchkov@yandex-team.ru>

File tree

3 files changed

+91
-85
lines changed

3 files changed

+91
-85
lines changed
 

‎conf/default.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ logs:
8080
# token: false
8181
# # support for the new v1 search endpoint, functional by incomplete read more on ticket 1732
8282
# search: false
83+
# # disable writing body size to logs, read more on ticket 1912
84+
# bytesin_off: false
8385

8486
# This affect the web and api (not developed yet)
8587
#i18n:

‎src/api/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const defineAPI = function(config: IConfig, storage: IStorageHandler): any {
2727
app.use(cors());
2828

2929
// Router setup
30-
app.use(log);
30+
app.use(log(config));
3131
app.use(errorReportingMiddleware);
3232
app.use(function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
3333
res.setHeader('X-Powered-By', config.user_agent);

‎src/api/middleware.ts

+88-84
Original file line numberDiff line numberDiff line change
@@ -174,102 +174,106 @@ export const LOG_STATUS_MESSAGE = "@{status}, user: @{user}(@{remoteIP}), req: '
174174
export const LOG_VERDACCIO_ERROR = `${LOG_STATUS_MESSAGE}, error: @{!error}`;
175175
export const LOG_VERDACCIO_BYTES = `${LOG_STATUS_MESSAGE}, bytes: @{bytes.in}/@{bytes.out}`;
176176

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+
}
180186

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+
}
185191

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;
190195

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+
}
194199

195-
if (_.isNil(_auth) === false) {
196-
req.headers.authorization = _auth;
197-
}
200+
if (_.isNil(_cookie) === false) {
201+
req.headers.cookie = _cookie;
202+
}
198203

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+
}
202210

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
215214
// @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+
};
218221

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+
}
235238

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+
},
242255
},
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+
};
257260

258-
req.on('close', function(): void {
259-
log();
260-
});
261+
req.on('close', function(): void {
262+
log();
263+
});
261264

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+
}
273277
}
274278

275279
// Middleware

0 commit comments

Comments
 (0)
Please sign in to comment.