-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-core.js
149 lines (130 loc) · 2.87 KB
/
index-core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
let fs = require('fs');
let ah = require('async_hooks');
let {AsyncResource} = ah;
let major = require('./major');
let stack = {};
let cls = function(ns) {
let wrapper = {
run: (fn) => {
let c = cls.create(ns);
return c.run(fn);
}
};
return wrapper;
};
cls._stack = stack;
cls.create = createContext;
cls.getContext = getContext;
cls.get = getContext;
cls.exit = exitContext;
cls.debug = false;
Object.defineProperty(cls, 'active', {
enumerable: true,
get: getContext
});
function createContext(ns) {
let c = {};
Object.defineProperty(c, 'run', {
enumerable: false,
writable: false,
value: run
});
Object.defineProperty(c, 'start', {
enumerable: false,
writable: false,
value: start
});
return c;
function run(cb) {
if (cls.debug)
log('run');
const resource = new AsyncResource('node-cls');
return resource.runInAsyncScope(() => {
let asyncId = ah.executionAsyncId();
let current = stack[asyncId];
current.contexts[ns] = c;
if (cb)
return cb();
});
}
function start() {
if (major < 12)
throw new Error('start() is not supported in nodejs < v12.0.0');
return Promise.resolve().then(() => {
stack[ah.executionAsyncId()].contexts[ns] = c;
});
}
}
function getContext(ns) {
let asyncId = ah.executionAsyncId();
while(asyncId) {
let current = stack[asyncId];
if (!current)
return;
if (current.contexts[ns])
return current.contexts[ns];
asyncId = current.parent;
}
}
async function exitContext(ns) {
if (cls.debug)
log('exit Context');
let asyncId = ah.executionAsyncId();
let context = getContext(ns);
if (!context)
return;
let node = stack[asyncId];
if (node && node.contexts[ns] === context)
delete node.contexts[ns];
exitContextUp(context, ns, asyncId);
return Promise.resolve();
}
function exitContextUp(context, ns, asyncId) {
while(stack[asyncId]) {
if (cls.debug)
log(`exit ${ asyncId}`);
asyncId = stack[asyncId].parent;
let parent = stack[asyncId];
if (parent) {
if (parent.contexts[ns] === context)
delete parent.contexts[ns];
else if (parent.contexts[ns])
return;
}
}
}
let hook = ah.createHook({
init,
destroy
});
hook.enable();
function newNode(asyncId, triggerId, contexts) {
return { contexts, id: asyncId, parent: triggerId};
}
function init(asyncId, _type, triggerId) {
let parent = stack[triggerId];
let contexts = {};
if (!parent) {
if (cls.debug)
log('no parent');
stack[asyncId] = newNode(asyncId, triggerId, contexts);
}
else {
Object.assign(contexts, parent.contexts);
stack[asyncId] = newNode(asyncId, triggerId, contexts);
}
if (cls.debug)
log('init ' + asyncId);
}
function log(str) {
fs.writeSync(1, `${str}\n`);
}
function destroy(asyncId) {
if (stack[asyncId])
delete stack[asyncId];
if (cls.debug) {
log(`destroy ${ asyncId} : ${ Object.keys(stack).length}`);
log(`keys ${ Object.keys(stack)}`);
}
}
module.exports = cls;