Skip to content

Commit

Permalink
release: v4.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-Lewis committed Aug 8, 2016
2 parents 6b01c0f + 6572640 commit f9bd0d2
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 184 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.idea/
!.idea/runConfigurations
npm-debug.log
2 changes: 1 addition & 1 deletion .idea/runConfigurations/bind_emitter_tap_js.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions .idea/runConfigurations/namespace_test.xml

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/namespaces_tap_js.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/promises_tap_js.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/run_tap_tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
script:
- "npm test"

language: node_js

node_js:
- "6"

sudo: false
25 changes: 17 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@

### v4.1.0
* Feature: add `runAndReturn` method to get return value of `func` (from ).


### v4.0.1
* Same API but major change to implementation. Uses **unofficial** [AsyncWrap](https://github.com/nodejs/node-eps/blob/async-wrap-ep/XXX-asyncwrap-api.md) instead of [async-listener](https://github.com/othiym23/async-listener).


### v3.1.0 (2014-07-28):

* Updated to use `[email protected]` to pick up bug fixes.

### v3.0.0 (2013-12-14):

* Removed the notion of a "default" or "global" context per namespace. It only
existed to create a simpler interface for developing and testing the module,
and created the potential for nasty information disclosure bugs (see [issue
#14](https://github.com/othiym23/node-continuation-local-storage/issues/14)
for details). This is potentially a breaking change, if you're depending on
the global context, so semver says we have to bump the major version.
* Removed the notion of a "default" or "global" context per namespace.
It only existed to create a simpler interface for developing and testing the module,
and created the potential for nasty information disclosure bugs
(see [issue #14](https://github.com/othiym23/node-continuation-local-storage/issues/14)
for details). This is potentially a breaking change, if you're depending on the global context,
so semver says we have to bump the major version.
* Added this changelog.

### v2.6.2 (2013-12-07):
Expand All @@ -18,8 +27,8 @@

### v2.6.1 (2013-11-29):

* `emitter-listener` has been extracted from `shimmer` into a standalone
module for `namespace.bindEmitter()`.
* `emitter-listener` has been extracted from `shimmer` into a standalone module
for `namespace.bindEmitter()`.

### v2.6.0 (2013-11-27):

Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
[![NPM](https://nodei.co/npm/cls-hooked.png?downloads=true&stars=true)](https://nodei.co/npm/cls-hooked/)
[![NPM](https://nodei.co/npm/cls-hooked.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/cls-hooked/)

[![Build Status](https://travis-ci.org/Jeff-Lewis/cls-hooked.svg?branch=master)](https://travis-ci.org/Jeff-Lewis/cls-hooked)

# Continuation-Local Storage ( Hooked )

### This is a fork of [CLS](https://github.com/othiym23/node-continuation-local-storage) using [AsyncWrap](https://github.com/nodejs/node-eps/blob/async-wrap-ep/XXX-asyncwrap-api.md) instead of [async-listener](https://github.com/othiym23/async-listener). ###

#### Thanks to [@trevnorris](https://github.com/trevnorris) for [AsyncWrap](https://github.com/nodejs/node-eps/blob/async-wrap-ep/XXX-asyncwrap-api.md) and all the async work in Node and [@AndreasMadsen](https://github.com/AndreasMadsen) for [async-hook](https://github.com/AndreasMadsen/async-hook). ###

Continuation-local storage works like thread-local storage in threaded
programming, but is based on chains of Node-style callbacks instead of threads.
The standard Node convention of functions calling functions is very similar to
Expand Down Expand Up @@ -186,6 +192,19 @@ that take callbacks themselves) from the provided callback within the scope of
that namespace. The new context is passed as an argument to the callback
when it's called.

### namespace.runAndReturn(callback)

* return: the return value of the callback

Create a new context on which values can be set or read. Run all the functions
that are called (either directly, or indirectly through asynchronous functions
that take callbacks themselves) from the provided callback within the scope of
that namespace. The new context is passed as an argument to the callback
when it's called.

Same as `namespace.run()` but returns the return value of the callback rather
than the context.

### namespace.bind(callback, [context])

* return: a callback wrapped up in a context closure
Expand Down
61 changes: 31 additions & 30 deletions context.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ const DEBUG_CLS_HOOKED = process.env.DEBUG_CLS_HOOKED;

let currentUid = -1;

module.exports = {
getNamespace: getNamespace,
createNamespace: createNamespace,
destroyNamespace: destroyNamespace,
reset: reset,
//trace: trace,
ERROR_SYMBOL: ERROR_SYMBOL
};

function Namespace(name) {
this.name = name;
// changed in 2.7: no default context
Expand Down Expand Up @@ -93,7 +102,15 @@ Namespace.prototype.run = function run(fn) {
}
};

Namespace.prototype.bind = function bind(fn, context) {
Namespace.prototype.runAndReturn = function (fn) {
var value;
this.run(function (context) {
value = fn(context);
});
return value;
};

Namespace.prototype.bind = function bindFactory(fn, context) {
if (!context) {
if (!this.active) {
context = this.createContext();
Expand All @@ -104,7 +121,7 @@ Namespace.prototype.bind = function bind(fn, context) {
}

let self = this;
return function () {
return function clsBind() {
self.enter(context);
try {
return fn.apply(this, arguments);
Expand Down Expand Up @@ -152,10 +169,6 @@ Namespace.prototype.exit = function exit(context) {
debug2('??ERROR?? context exiting but not entered - ignoring: ' + util.inspect(context));
}
assert.ok(index >= 0, 'context not currently entered; can\'t exit. \n' + util.inspect(this) + '\n' + util.inspect(context));
/*let len = trace.length;
for (let i = 0; i < len; i++) {
console.log(trace[i]);
}*/
} else {
assert.ok(index, 'can\'t remove top context');
this._set.splice(index, 1);
Expand Down Expand Up @@ -191,7 +204,7 @@ Namespace.prototype.bindEmitter = function bindEmitter(emitter) {

let wrapped = unwrapped;
let unwrappedContexts = unwrapped[CONTEXTS_SYMBOL];
Object.keys(unwrappedContexts).forEach(function (name) {
Object.keys(unwrappedContexts).forEach(function(name) {
let thunk = unwrappedContexts[name];
wrapped = thunk.namespace.bind(wrapped, thunk.context);
});
Expand Down Expand Up @@ -226,12 +239,15 @@ function createNamespace(name) {

asyncHook.addHooks({
init(uid, handle, provider, parentUid, parentHandle) {
//currentUid = parentUid || uid; // Suggested usage but appears to work better for tracing modules.
//parentUid = parentUid || currentUid; // Suggested usage but appears to work better for tracing modules.
currentUid = uid;

//CHAIN Parent's Context onto child if none exists. This is needed to pass net-events.spec
if (parentUid) {
namespace._contexts.set(uid, namespace._contexts.get(parentUid));
if (DEBUG_CLS_HOOKED) {
debug2('PARENTID: ' + name + ' uid:' + uid + ' parent:' + parentUid + ' provider:' + provider);
}
} else {
namespace._contexts.set(currentUid, namespace.active);
}
Expand All @@ -241,12 +257,6 @@ function createNamespace(name) {
+ ' active:' + util.inspect(namespace.active, true));
}

if (parentUid) {
if (DEBUG_CLS_HOOKED) {
debug2('PARENTID: ' + name + ' uid:' + uid + ' parent:' + parentUid + ' provider:' + provider);
}
}

},
pre(uid, handle) {
currentUid = uid;
Expand Down Expand Up @@ -306,7 +316,7 @@ function destroyNamespace(name) {
function reset() {
// must unregister async listeners
if (process.namespaces) {
Object.keys(process.namespaces).forEach(function (name) {
Object.keys(process.namespaces).forEach(function(name) {
destroyNamespace(name);
});
}
Expand All @@ -327,23 +337,14 @@ function debug2(msg) {


/*function debug(from, ns) {
process._rawDebug('DEBUG: ' + util.inspect({
from: from,
currentUid: currentUid,
context: ns ? ns._contexts.get(currentUid) : 'no ns'
}, true, 2, true));
}*/
process._rawDebug('DEBUG: ' + util.inspect({
from: from,
currentUid: currentUid,
context: ns ? ns._contexts.get(currentUid) : 'no ns'
}, true, 2, true));
}*/


module.exports = {
getNamespace: getNamespace,
createNamespace: createNamespace,
destroyNamespace: destroyNamespace,
reset: reset,
//trace: trace,
ERROR_SYMBOL: ERROR_SYMBOL
};

function getFunctionName(fn) {
if (!fn) {
return fn;
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"context.js"
],
"scripts": {
"test": "mocha test/*.js",
"test": "mocha test/*.js & tap test/tap/*.tap.js",
"test-tap": "tap test/tap/*.tap.js"
},
"repository": {
Expand All @@ -28,17 +28,17 @@
"license": "BSD-2-Clause",
"engineStrict": true,
"engines": {
"node": "^6.2.2"
"node": "^6.3.1"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.5.3",
"mocha": "^3.0.2",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0",
"stack-chain": "^1.3.7",
"superagent": "^2.0.0",
"tap": "^6.2.0",
"tap-mocha-reporter": "0.0.25"
"tap-mocha-reporter": "0.0.27"
},
"dependencies": {
"async-hook": "^1.5.1",
Expand Down
2 changes: 1 addition & 1 deletion test/tap/dns.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var dns = require('dns')
, tap = require('tap')
, test = tap.test
, createNamespace = require('../context.js').createNamespace
, createNamespace = require('./../../context.js').createNamespace
;

test("continuation-local state with MakeCallback and DNS module", function (t) {
Expand Down
12 changes: 6 additions & 6 deletions test/tap/error-handling.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ test("synchronous throw checks if error exists", function (t) {
test("throw in process.nextTick attaches the context", function (t) {
t.plan(3);

var namespace = cls.createNamespace('cls@nexttick');
var namespace = cls.createNamespace('cls@nexttick2');

var d = domain.create();
d.once('error', function (e) {
t.ok(namespace.fromException(e), "context was attached to error");
t.equal(namespace.fromException(e)['value'], 'transaction set',
"found the inner value");

cls.destroyNamespace('cls@nexttick');
cls.destroyNamespace('cls@nexttick2');
});

namespace.run(function () {
Expand All @@ -103,7 +103,7 @@ test("throw in process.nextTick attaches the context", function (t) {
process.nextTick(d.bind(function () {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error("cls@nexttick explosion");
throw new Error("cls@nexttick2 explosion");
});
}));

Expand All @@ -114,15 +114,15 @@ test("throw in process.nextTick attaches the context", function (t) {
test("throw in setTimeout attaches the context", function (t) {
t.plan(3);

var namespace = cls.createNamespace('cls@nexttick');
var namespace = cls.createNamespace('cls@nexttick3');
var d = domain.create();

d.once('error', function (e) {
t.ok(namespace.fromException(e), "context was attached to error");
t.equal(namespace.fromException(e)['value'], 'transaction set',
"found the inner value");

cls.destroyNamespace('cls@nexttick');
cls.destroyNamespace('cls@nexttick3');
});

namespace.run(function () {
Expand All @@ -132,7 +132,7 @@ test("throw in setTimeout attaches the context", function (t) {
setTimeout(d.bind(function () {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error("cls@nexttick explosion");
throw new Error("cls@nexttick3 explosion");
});
}));

Expand Down
Loading

0 comments on commit f9bd0d2

Please sign in to comment.