Skip to content

Commit 41413cf

Browse files
committed
feat(context): remove namespace destroy from middleware and enable setting values on context
1 parent c6fad5c commit 41413cf

File tree

7 files changed

+107
-82
lines changed

7 files changed

+107
-82
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ logFactory.Timer = Timer;
2121
logFactory.getNamespaces = function() {
2222
return process.env.DEBUG || '';
2323
};
24-
logFactory.getMiddleware = contextMiddlewareFactory.getMiddleware;
24+
logFactory.getMiddleware = contextMiddlewareFactory.getMiddleware.bind(contextMiddlewareFactory);
25+
logFactory.setOnContext = contextMiddlewareFactory.setOnContext.bind(contextMiddlewareFactory);
2526

2627
module.exports = logFactory;

koa-example.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
'use strict';
22

33
const Koa = require('koa');
4+
const uuid = require('uuid');
45
const logFactory = require('./index');
56
const logger = logFactory('example');
67
const port = 3000;
78

89
const app = new Koa();
910

1011
app.use(async (ctx, next) => {
11-
ctx.request.header['x-request-id'] = 'uuid';
12+
ctx.request.header['x-request-id'] = uuid.v4();
1213
await next();
1314
});
15+
1416
app.use(logFactory.getMiddleware());
17+
1518
app.use(async (ctx) => {
16-
logger.info('works');
19+
logger.info('before');
20+
21+
logFactory.setOnContext('customer_id', Math.round(Math.random() * 1000));
22+
23+
logger.info('after');
1724
ctx.body = 'It works';
1825
});
1926

2027
app.listen(port);
21-
console.log('listening on port: ' + port);
28+
console.log('listening on port: ' + port);

package-lock.json

Lines changed: 56 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
"json"
3838
],
3939
"dependencies": {
40-
"chalk": "2.1.0",
40+
"chalk": "2.3.0",
4141
"cls-hooked": "4.2.2",
42-
"event-stream": "3.3.2"
42+
"event-stream": "3.3.4",
43+
"lodash": "4.17.4",
44+
"uuid": "3.1.0"
4345
},
4446
"devDependencies": {
4547
"@bubltechnology/customizable-commit-analyzer": "1.0.2-0",
Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
'use strict';
22

33
const continuationLocalStorage = require('cls-hooked');
4+
const uuid = require('uuid');
45

56
class ContextMiddlewareFactory {
67
static getMiddleware() {
8+
const namespace = this._createNamespace();
9+
710
return async function(ctx, next) {
8-
const namespace = continuationLocalStorage.createNamespace('session');
9-
10-
try{
11-
await new Promise(namespace.bind(function(resolve, reject) {
12-
namespace.set('request_id', ctx.request.header['x-request-id']);
13-
14-
next().then(resolve).catch(reject);
15-
}));
16-
} catch(error) {
17-
throw error;
18-
} finally {
19-
continuationLocalStorage.destroyNamespace('session');
20-
}
11+
await new Promise(namespace.bind(function(resolve, reject) {
12+
namespace.set(
13+
'request_id',
14+
ctx.request.header['x-request-id'] || uuid.v4()
15+
);
16+
17+
next().then(resolve).catch(reject);
18+
}));
2119
};
2220
}
21+
22+
static setOnContext(key, value) {
23+
const namespace = this._createNamespace();
24+
namespace.set(key, value);
25+
}
26+
27+
static destroyNamespace() {
28+
if (this._namespace) {
29+
continuationLocalStorage.destroyNamespace('session');
30+
}
31+
}
32+
33+
static _createNamespace() {
34+
if (!this._namespace) {
35+
this._namespace = continuationLocalStorage.createNamespace('session');
36+
}
37+
return this._namespace;
38+
}
2339
}
2440

2541
module.exports = ContextMiddlewareFactory;

0 commit comments

Comments
 (0)