Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 332703e

Browse files
author
Michal Vlasák
committed
Refactor express handlers, update Changelog
1 parent cb9f71f commit 332703e

2 files changed

Lines changed: 45 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
### Fixed
44
- lint issue due to changes in tslint 5.12.0
55
- stream write function type
6+
- pretty streams created only when needed
67

78
### Added
89
- coveralls integration
10+
11+
## Changed
12+
- refactoring of express handlers
913

1014
## [1.0.3] - 2018-11-08
1115

src/express.ts

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,59 @@ import { AckeeLogger } from '.';
55

66
const errorSymbol = Symbol.for('error');
77

8+
type AckeeRequest = Request & { _startAt?: [number, number]; ackId?: string };
9+
type AckeeResponse = Response & { _startAt?: [number, number]; time?: string; out?: object; [errorSymbol]?: any };
10+
811
export type AckeeLoggerExpressMiddleware = (
912
this: AckeeLogger,
10-
req: Request & { _startAt?: [number, number]; ackId?: string },
11-
response: Response & { _startAt?: [number, number]; time?: string; out?: object; [errorSymbol]?: any },
13+
req: AckeeRequest,
14+
response: AckeeResponse,
1215
next: any
1316
) => void;
1417

18+
const expressOnHeaders = (req: AckeeRequest, res: AckeeResponse) => () => {
19+
res._startAt = process.hrtime();
20+
const diffFromSeconds = (res._startAt[0] - req._startAt![0]) * 1e3;
21+
const diffFromNanoseconds = (res._startAt[1] - req._startAt![1]) * 1e-6;
22+
const ms = diffFromSeconds + diffFromNanoseconds;
23+
res.time = ms.toFixed(3);
24+
};
25+
26+
const expressOnFinished = (logger: AckeeLogger, req: AckeeRequest) => (_err: Error | null, res: AckeeResponse) => {
27+
const error = res[errorSymbol];
28+
const reqOut = `${res.statusCode} ${req.method} ${req.originalUrl} ${res.time} ms ${req.headers['user-agent']}`;
29+
if (logger.options.ignoredHttpMethods && logger.options.ignoredHttpMethods.includes(req.method)) {
30+
return;
31+
}
32+
const standardOutput = {
33+
data: {
34+
req,
35+
res,
36+
ackId: req.ackId,
37+
},
38+
message: `${reqOut} - Standard output`,
39+
};
40+
41+
if (error) {
42+
logger.error({ error, req, res, ackId: req.ackId }, `${reqOut} - Error handler at the end of app`);
43+
} else if (res.out) {
44+
logger.debug(standardOutput.data, standardOutput.message);
45+
} else {
46+
logger.info(standardOutput.data, standardOutput.message);
47+
}
48+
};
49+
1550
const expressMiddleware: RequestHandler = function(
1651
this: AckeeLogger,
17-
req: Request & { _startAt?: [number, number]; ackId?: string },
18-
response: Response & { _startAt?: [number, number]; time?: string; out?: object; [errorSymbol]?: any },
52+
req: AckeeRequest,
53+
response: AckeeResponse,
1954
next: any
2055
) {
2156
const reqIn = `--- ${req.method} ${req.originalUrl} ${req.headers['user-agent']}`;
2257
this.debug({ req, ackId: req.ackId }, `${reqIn} - Request accepted`);
2358
req._startAt = process.hrtime();
24-
onHeaders(response, () => {
25-
response._startAt = process.hrtime();
26-
const diffFromSeconds = (response._startAt[0] - req._startAt![0]) * 1e3;
27-
const diffFromNanoseconds = (response._startAt[1] - req._startAt![1]) * 1e-6;
28-
const ms = diffFromSeconds + diffFromNanoseconds;
29-
response.time = ms.toFixed(3);
30-
});
31-
onFinished(response, (_err, res) => {
32-
const error = res[errorSymbol];
33-
const reqOut = `${res.statusCode} ${req.method} ${req.originalUrl} ${res.time} ms ${req.headers['user-agent']}`;
34-
if (this.options.ignoredHttpMethods && this.options.ignoredHttpMethods.includes(req.method)) {
35-
return;
36-
}
37-
const standardOutput = {
38-
data: {
39-
req,
40-
res,
41-
ackId: req.ackId,
42-
},
43-
message: `${reqOut} - Standard output`,
44-
};
45-
46-
if (error) {
47-
this.error({ error, req, res, ackId: req.ackId }, `${reqOut} - Error handler at the end of app`);
48-
} else if (res.out) {
49-
this.debug(standardOutput.data, standardOutput.message);
50-
} else {
51-
this.info(standardOutput.data, standardOutput.message);
52-
}
53-
});
59+
onHeaders(response, expressOnHeaders(req, response));
60+
onFinished(response, expressOnFinished(this, req));
5461
next();
5562
};
5663

0 commit comments

Comments
 (0)