continuation-local-storage
Advanced tools
Comparing version 2.3.1 to 2.3.2
@@ -90,4 +90,8 @@ 'use strict'; | ||
/** | ||
* Attach a context to a listener, and make sure that this hook stays | ||
* attached to the emitter forevermore. | ||
*/ | ||
function capturer(on) { | ||
return function (event, listener) { | ||
return function captured(event, listener) { | ||
listener[contextName] = namespace.active; | ||
@@ -97,5 +101,5 @@ | ||
// FIXME: ReadableStreams overwrite on / addListener; find out why | ||
wrap(this, 'on', capturer); | ||
wrap(this, 'addListener', capturer); | ||
// somebody's using an old-style stream, which overwrites .on | ||
if (this.on !== captured) wrap(this, 'on', capturer); | ||
if (this.addListener !== captured) wrap(this, 'addListener', capturer); | ||
@@ -106,21 +110,24 @@ return returned; | ||
function prepare(handlers) { | ||
var replacements = []; | ||
for (var i = 0; i < handlers.length; i++) { | ||
var handler = handlers[i]; | ||
if (handler[contextName]) { | ||
replacements.push(namespace.bind(handler, handler[contextName])); | ||
/** | ||
* Evaluate listeners within the CLS contexts in which they were originally | ||
* captured. | ||
*/ | ||
function puncher(emit) { | ||
// find all the handlers with attached contexts | ||
function prepare(handlers) { | ||
var replacements = []; | ||
for (var i = 0; i < handlers.length; i++) { | ||
var handler = handlers[i]; | ||
if (handler[contextName]) { | ||
handler = namespace.bind(handler, handler[contextName]); | ||
} | ||
replacements[i] = handler; | ||
} | ||
else { | ||
replacements.push(handler); | ||
} | ||
return replacements; | ||
} | ||
return replacements; | ||
} | ||
return function punched(event) { | ||
if (!this._events || !this._events[event]) return emit.apply(this, arguments); | ||
function puncher(emit) { | ||
return function (event) { | ||
if (!this._events[event]) return emit.apply(this, arguments); | ||
// setup | ||
@@ -127,0 +134,0 @@ var events = this._events[event]; |
{ | ||
"name": "continuation-local-storage", | ||
"version": "2.3.1", | ||
"version": "2.3.2", | ||
"description": "userland implementation of https://github.com/joyent/node/issues/5243", | ||
@@ -5,0 +5,0 @@ "main": "context.js", |
@@ -10,3 +10,3 @@ var test = require('tap').test | ||
// when the flaw was in the patch, commenting out this line would fix things: | ||
setImmediate(function () { console.log('!'); }); | ||
process.nextTick(function () { console.log('!'); }); | ||
@@ -20,3 +20,3 @@ var n = cls.createNamespace("test"); | ||
setImmediate(function () { | ||
process.nextTick(function () { | ||
t.ok(n.get('state'), "state should be visible"); | ||
@@ -23,0 +23,0 @@ }); |
@@ -16,3 +16,3 @@ 'use strict'; | ||
test("event emitters bound to CLS context", function (t) { | ||
t.plan(3); | ||
t.plan(6); | ||
@@ -37,2 +37,20 @@ t.test("handler registered in context", function (t) { | ||
t.test("once handler registered in context", function (t) { | ||
t.plan(1); | ||
var n = fresh('inOnce', this) | ||
, ee = new EventEmitter() | ||
; | ||
n.run(function () { | ||
n.set('value', 'hello'); | ||
n.bindEmitter(ee); | ||
ee.once('event', function () { | ||
t.equal(n.get('value'), 'hello', "value still set in EE."); | ||
}); | ||
}); | ||
ee.emit('event'); | ||
}); | ||
t.test("handler registered out of context", function (t) { | ||
@@ -57,11 +75,11 @@ t.plan(1); | ||
t.test("handler added but used entirely out of context", function (t) { | ||
t.test("once handler registered out of context", function (t) { | ||
t.plan(1); | ||
var n = fresh('none', this) | ||
var n = fresh('outOnce', this) | ||
, ee = new EventEmitter() | ||
; | ||
ee.on('event', function () { | ||
t.notOk(n.get('value'), "value shouldn't be visible"); | ||
ee.once('event', function () { | ||
t.equal(n.get('value'), 'hello', "value still set in EE."); | ||
}); | ||
@@ -72,6 +90,58 @@ | ||
n.bindEmitter(ee); | ||
ee.emit('event'); | ||
}); | ||
}); | ||
t.test("handler added but used entirely out of context", function (t) { | ||
t.plan(2); | ||
var n = fresh('none', this) | ||
, ee = new EventEmitter() | ||
; | ||
n.run(function () { | ||
n.set('value', 'hello'); | ||
n.bindEmitter(ee); | ||
}); | ||
ee.on('event', function () { | ||
t.ok(n, "n is set"); | ||
t.notOk(n.get('value'), "value shouldn't be visible"); | ||
}); | ||
ee.emit('event'); | ||
}); | ||
t.test("handler added but no listeners registered", function (t) { | ||
t.plan(3); | ||
var http = require('http') | ||
, n = fresh('no_listener', this) | ||
; | ||
// only fails on Node < 0.10 | ||
var server = http.createServer(function (req, res) { | ||
n.bindEmitter(req); | ||
t.doesNotThrow(function () { | ||
req.emit('event'); | ||
}); | ||
res.writeHead(200, {"Content-Length" : 4}); | ||
res.end('WORD'); | ||
}); | ||
server.listen(8080); | ||
http.get('http://localhost:8080/', function (res) { | ||
t.equal(res.statusCode, 200, "request came back OK"); | ||
res.setEncoding('ascii'); | ||
res.on('data', function (body) { | ||
t.equal(body, 'WORD'); | ||
server.close(); | ||
}); | ||
}); | ||
}); | ||
}); |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
74049
1754
4