From 3941033179d2db6f6d8a062873b866f6c52362c1 Mon Sep 17 00:00:00 2001 From: analog-nico Date: Sat, 10 Oct 2015 18:53:54 +0200 Subject: [PATCH] Added experimental support for continuation local storage (see issue #64) --- lib/rp.js | 5 ++++ package.json | 2 ++ test/fixtures/cls/single-namespace.js | 34 +++++++++++++++++++++++++++ test/spec/request-test.js | 17 ++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 test/fixtures/cls/single-namespace.js diff --git a/lib/rp.js b/lib/rp.js index eff8d34..66d6364 100644 --- a/lib/rp.js +++ b/lib/rp.js @@ -156,4 +156,9 @@ request.Request.prototype.pipe = RP$noStreaming; request.Request.prototype.pipeDest = RP$noStreaming; +request.bindCLS = function RP$bindCLS(ns) { + require('cls-bluebird')(ns); +}; + + module.exports = request; diff --git a/package.json b/package.json index 8dc16f8..eec21f8 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ }, "dependencies": { "bluebird": "^2.3", + "cls-bluebird": "^1.0.1", "lodash": "^3.10.0", "request": "^2.34" }, @@ -38,6 +39,7 @@ "chai": "2.2.x", "chai-as-promised": "4.3.x", "chalk": "^1.1.1", + "continuation-local-storage": "^3.1.4", "event-stream": "3.3.x", "gulp": "3.8.x", "gulp-coveralls": "0.1.x", diff --git a/test/fixtures/cls/single-namespace.js b/test/fixtures/cls/single-namespace.js new file mode 100644 index 0000000..bcd2ead --- /dev/null +++ b/test/fixtures/cls/single-namespace.js @@ -0,0 +1,34 @@ +'use strict'; + +var rp = require('../../../lib/rp.js'); +var cls = require('continuation-local-storage'); + + +var ns = cls.createNamespace('testNS'); +rp.bindCLS(ns); + + +function print() { + ns.run(function () { + + ns.set('value', ns.get('value') + 1); + + console.log(ns.get('value')); + + }); +} + +ns.run(function () { + + ns.set('value', 0); + + rp('http://localhost:4000/200') + .then(function () { + ns.run(function () { + ns.set('value', ns.get('value') + 1); + + setTimeout(print); + }); + }); + +}); diff --git a/test/spec/request-test.js b/test/spec/request-test.js index 1853e0b..7c6dc04 100644 --- a/test/spec/request-test.js +++ b/test/spec/request-test.js @@ -1040,6 +1040,23 @@ describe('Request-Promise', function () { }); + describe('should support continuation local storage', function () { + + it('with an untouched Bluebird prototype bound to a single namespace', function (done) { + + childProcess.exec('node ' + path.join(__dirname, '../fixtures/cls/single-namespace.js'), function (err, stdout, stderr) { + try { + expect(stdout).to.contain('2\n'); + done(); + } catch (e) { + done(e); + } + }); + + }); + + }); + describe('should run the examples', function () { it('in the README', function () {