forked from othiym23/node-continuation-local-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.js
270 lines (222 loc) · 6.75 KB
/
context.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict';
const EE = require('events').EventEmitter;
const assert = require('assert');
const wrapEmitter = require('emitter-listener');
const shimmer = require('shimmer');
/*
*
* CONSTANTS
*
*/
const CONTEXTS_SYMBOL = 'cls@contexts';
const ERROR_SYMBOL = 'error@context';
if (!process.env.NO_CLS_FOR_EMMITERS && !EE.usingCLS) {
EE.usingCLS = true;
shimmer.wrap(EE, 'init', function (original) {
return function () {
original.apply(this, arguments);
filteredEEBind(this, Namespace._active);
};
});
}
// load polyfill if native support is unavailable
if (!process.addAsyncListener) require('async-listener');
function Namespace(name) {
this.name = name;
// changed in 2.7: no default context
this.active = null;
this._set = [];
this.id = null;
}
Namespace.prototype.set = function (key, value) {
if (!this.active) {
throw new Error("No context available. ns.run() or ns.bind() must be called first.");
}
this.active[key] = value;
return value;
};
Namespace.prototype.get = function (key) {
if (!this.active) return undefined;
return this.active[key];
};
Namespace.prototype.createContext = function () {
return Object.create(this.active);
};
Namespace.prototype.run = function (fn) {
var context = this.createContext();
this.enter(context);
try {
fn(context);
return context;
}
catch (exception) {
if (exception) {
exception[ERROR_SYMBOL] = context;
}
throw exception;
}
finally {
this.exit(context);
}
};
Namespace.prototype.runAndReturn = function (fn) {
var value;
this.run(function (context) {
value = fn(context);
});
return value;
};
Namespace.prototype.bind = function (fn, context) {
if (!context) {
if (!this.active) {
context = Object.create(this.active);
}
else {
context = this.active;
}
}
const self = this;
return function () {
self.enter(context);
try {
return fn.apply(this, arguments);
}
catch (exception) {
if (exception) {
exception[ERROR_SYMBOL] = context;
}
throw exception;
}
finally {
self.exit(context);
}
};
};
Namespace.prototype.enter = function (context) {
assert.ok(context, "context must be provided for entering");
this._set.push(this.active);
this.active = context;
Namespace._active = this;
};
Namespace.prototype.exit = function (context) {
assert.ok(context, "context must be provided for exiting");
// Fast path for most exits that are at the top of the stack
if (this.active === context) {
assert.ok(this._set.length, "can't remove top context");
this.active = this._set.pop();
Namespace._active = (this.active) ? this : null;
return;
}
// Fast search in the stack using lastIndexOf
var index = this._set.lastIndexOf(context);
assert.ok(index >= 0, "context not currently entered; can't exit");
assert.ok(index, "can't remove top context");
this._set.splice(index, 1);
};
Namespace.prototype.bindEmitter = function (emitter) {
assert.ok(emitter.on && emitter.addListener && emitter.emit, "can only bind real EEs");
var namespace = this;
var thisSymbol = 'context@' + this.name;
// Capture the context active at the time the emitter is bound.
function attach(listener) {
if (!listener) return;
if (!listener[CONTEXTS_SYMBOL]) listener[CONTEXTS_SYMBOL] = Object.create(null);
listener[CONTEXTS_SYMBOL][thisSymbol] = {
namespace: namespace,
context: namespace.active
};
}
// At emit time, bind the listener within the correct context.
function bind(unwrapped) {
if (!(unwrapped && unwrapped[CONTEXTS_SYMBOL])) return unwrapped;
var wrapped = unwrapped;
var contexts = unwrapped[CONTEXTS_SYMBOL];
var thunk;
Object.keys(contexts).forEach(function (name) {
thunk = contexts[name];
wrapped = thunk.namespace.bind(wrapped, thunk.context);
});
const patched = function () {
for (let i = 0; i < arguments.length; ++i) {
const arg = arguments[0];
filteredEEBind(arg, thunk.context);
}
wrapped.apply(this, arguments);
};
return patched;
}
wrapEmitter(emitter, attach, bind);
};
function filteredEEBind(target, ctx) {
if (!(target instanceof EE)) return;
if (!ctx) return;
if ('wrap@before' in target) return;
const name = Object.getPrototypeOf(target).constructor.name;
switch (name) {
case 'ClientRequest':
case 'IncomingMessage':
case 'Server':
Namespace._active.bindEmitter(target);
break;
case 'Socket':
const server = target.server;
if (server && 'wrap@before' in server)
Namespace._active.bindEmitter(target);
break;
}
}
/**
* If an error comes out of a namespace, it will have a context attached to it.
* This function knows how to find it.
*
* @param {Error} exception Possibly annotated error.
*/
Namespace.prototype.fromException = function (exception) {
return exception[ERROR_SYMBOL];
};
function get(name) {
return process.namespaces[name];
}
function create(name) {
assert.ok(name, "namespace must be given a name!");
var namespace = new Namespace(name);
namespace.id = process.addAsyncListener({
create: function () {
return namespace.active;
},
before: function (context, storage) {
if (storage) namespace.enter(storage);
},
after: function (context, storage) {
if (storage) namespace.exit(storage);
},
error: function (storage) {
if (storage) namespace.exit(storage);
}
});
process.namespaces[name] = namespace;
return namespace;
}
function destroy(name) {
var namespace = get(name);
assert.ok(namespace, "can't delete nonexistent namespace!");
assert.ok(namespace.id, "don't assign to process.namespaces directly!");
process.removeAsyncListener(namespace.id);
process.namespaces[name] = null;
}
function reset() {
// must unregister async listeners
if (process.namespaces) {
Object.keys(process.namespaces).forEach(function (name) {
destroy(name);
});
}
process.namespaces = Object.create(null);
}
if (!process.namespaces) reset(); // call immediately to set up
module.exports = {
getNamespace: get,
createNamespace: create,
destroyNamespace: destroy,
reset: reset
};