Skip to content

Commit 7f9fa5e

Browse files
author
Danilo Resende
committed
Drop Node version < 8
After more testing the 'async-hooks' polyfill shown to not work properly for Node version prior to 8.
1 parent 44e8197 commit 7f9fa5e

File tree

7 files changed

+49
-12
lines changed

7 files changed

+49
-12
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: node_js
22
node_js:
3-
- "4"
4-
- "5"
3+
- "8"
54
- "stable"
65

76
sudo: required

lib/async-context.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const asyncHooks = require('async_hooks');
4+
5+
class AsyncContext {
6+
constructor() {
7+
this.map = new Map();
8+
asyncHooks.createHook({
9+
init: (id, _type, triggerId) => {
10+
if (this.map.has(triggerId))
11+
this.map.set(id, this.map.get(triggerId));
12+
},
13+
destroy: (id) => this.map.delete(id)
14+
}).enable();
15+
}
16+
17+
get() {
18+
const id = asyncHooks.executionAsyncId();
19+
if (this.map.has(id))
20+
return this.map.get(id);
21+
}
22+
23+
set(val) {
24+
this.map.set(asyncHooks.executionAsyncId(), val);
25+
}
26+
}
27+
28+
module.exports = new AsyncContext();

lib/middlewares/express.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
'use strict';
22

3+
const asyncContext = require('../async-context');
4+
35
module.exports = {
46
buildMiddleware: function(provider) {
57
return function(req, res, next) {
68
provider.handler(req, res, next);
79
};
810
},
9-
mainMiddleware: function(enable, authorize, handleRequest) {
11+
mainMiddleware: function(enable, authorize, handleRequest, cls) {
1012
return function(req, res, next) {
1113
handleRequest(enable, authorize, req, res).then((handled) => {
1214
res.locals.miniprofiler = req.miniprofiler;
1315

16+
asyncContext.set(req.miniprofiler);
17+
Object.defineProperty(req, 'miniprofiler', { get: () => asyncContext.get() });
18+
1419
var render = res.render;
1520
res.render = function() {
1621
var renderArguments = arguments;
@@ -24,4 +29,4 @@ module.exports = {
2429
}).catch(next);
2530
};
2631
}
27-
};
32+
};

lib/middlewares/hapi.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const asyncContext = require('../async-context');
4+
35
module.exports = {
46
buildMiddleware: function(provider) {
57
var plugin = {
@@ -25,7 +27,9 @@ module.exports = {
2527
register: (server, options, next) => {
2628
server.ext('onRequest', function(request, reply) {
2729
handleRequest(enable, authorize, request.raw.req, request.raw.res).then((handled) => {
28-
request.app.miniprofiler = request.raw.req.miniprofiler;
30+
asyncContext.set(request.raw.req.miniprofiler);
31+
Object.defineProperty(request.app, 'miniprofiler', { get: () => asyncContext.get() });
32+
Object.defineProperty(request.raw.req, 'miniprofiler', { get: () => asyncContext.get() });
2933

3034
if (!handled)
3135
reply.continue();

lib/middlewares/koa.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const asyncContext = require('../async-context');
4+
35
module.exports = {
46
buildMiddleware: function(provider) {
57
return function *(next) {
@@ -12,7 +14,10 @@ module.exports = {
1214
mainMiddleware: function(enable, authorize, handleRequest) {
1315
return function *(next) {
1416
var handled = yield handleRequest(enable, authorize, this.req, this.res);
15-
this.state.miniprofiler = this.req.miniprofiler;
17+
18+
asyncContext.set(this.req.miniprofiler);
19+
Object.defineProperty(this.state, 'miniprofiler', { get: () => asyncContext.get() });
20+
Object.defineProperty(this.req, 'miniprofiler', { get: () => asyncContext.get() });
1621

1722
if (this.render) {
1823
var render = this.render;

lib/miniprofiler.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
var url = require('url');
1616
var ui = require('./ui.js');
1717
var clientParser = require('./client-parser.js');
18-
var ContinuationLocalStorage = require('asyncctx').ContinuationLocalStorage;
1918

2019
const hostname = require('os').hostname;
2120
var ignoredPaths = [];
@@ -28,7 +27,6 @@ var ignoredPaths = [];
2827
RedisStorage: require('./storages/redis.js')
2928
};
3029

31-
var cls = new ContinuationLocalStorage();
3230
var storage = new exports.storage.InMemoryStorage({ max: 100, maxAge: 1000 * 60 * 60 });
3331

3432
exports.configure = configure;
@@ -280,8 +278,7 @@ function include(id) {
280278
return enabled && authorized ? include(currentRequestExtension.id) : '';
281279
};
282280

283-
cls.setContext(currentRequestExtension);
284-
Object.defineProperty(request, 'miniprofiler', { get: () => cls.getContext() });
281+
request.miniprofiler = currentRequestExtension;
285282

286283
return currentRequestExtension;
287284
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "miniprofiler",
3-
"version": "1.2.3",
3+
"version": "2.0.0",
44
"description": "A simple but effective mini-profiler.",
55
"main": "lib/miniprofiler.js",
66
"scripts": {
@@ -26,7 +26,6 @@
2626
"license": "Apache-2.0",
2727
"readmeFilename": "README.md",
2828
"dependencies": {
29-
"asyncctx": "^1.1.0",
3029
"lru-cache": "^4.0.1"
3130
},
3231
"tags": [

0 commit comments

Comments
 (0)