request-context
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -7,13 +7,25 @@ /** | ||
var gulp = require('gulp'); | ||
var jsdoc = require('gulp-jsdoc'); | ||
var mocha = require('gulp-mocha'); | ||
var utils = require('gulp-util'); | ||
const gulp = require('gulp'); | ||
const jsdoc = require('gulp-jsdoc'); | ||
const mocha = require('gulp-mocha'); | ||
const eslint = require('gulp-eslint'); | ||
const utils = require('gulp-util'); | ||
/** | ||
* lint task | ||
* lint code | ||
*/ | ||
gulp.task('lint', function () { | ||
return gulp.src(['lib/**/*.js','!node_modules/**']) | ||
.pipe(eslint()) | ||
.pipe(eslint.format()) | ||
.pipe(eslint.failAfterError()); | ||
}); | ||
/** | ||
* test task | ||
* Run unit tests | ||
*/ | ||
gulp.task('test', function () { | ||
return gulp.src('lib/index.spec.js', {read: false}) | ||
gulp.task('test', ['lint'], function () { | ||
return gulp.src('lib/**/*.spec.js', {read: false}) | ||
.pipe(mocha({ | ||
@@ -20,0 +32,0 @@ ui: 'bdd', |
@@ -54,4 +54,3 @@ /** | ||
*/ | ||
get: getContext | ||
get: getContext | ||
}; | ||
@@ -76,2 +75,16 @@ | ||
/** | ||
* Initiates the context object on the provided domain | ||
* @param domain - A domain object to initialize the context object on | ||
* @returns {Object|*} | ||
*/ | ||
function initContext(domain) { | ||
if (!domain) { | ||
throw new Error('No domain found when initializing context'); | ||
} | ||
domain.__$cntxt__ = Object.create(null); | ||
return domain.__$cntxt__; | ||
} | ||
/** | ||
* Set the context for a given name | ||
@@ -121,2 +134,3 @@ * @api private | ||
initContext(d); | ||
setContext(namespace, Object.create(null), d); | ||
@@ -148,8 +162,6 @@ | ||
// no active domain found | ||
if (!current) { | ||
if (!current || !current.__$cntxt__) { | ||
return null; | ||
} | ||
// get/set the internal context store from/on the active domain object | ||
current.__$cntxt__ = current.__$cntxt__ || Object.create(null); | ||
return current.__$cntxt__; | ||
@@ -156,0 +168,0 @@ } |
@@ -52,3 +52,3 @@ 'use strict'; | ||
before(function (done) {done | ||
before(function (done) { | ||
app = connect() | ||
@@ -111,3 +111,3 @@ .use(middleware) | ||
server = app.listen(port, () => { | ||
request.get(url, (err, res) => { | ||
request.get(url, err => { | ||
// an error would be thrown if headers are send | ||
@@ -201,2 +201,32 @@ // after the head is written | ||
describe('init only in middleware', function () { | ||
it('get will return undefined several times in a row', function () { | ||
assert.deepEqual(reqContext.get('test2'), undefined); | ||
assert.deepEqual(reqContext.get('test2'), undefined); | ||
assert.deepEqual(reqContext.get('test2'), undefined); | ||
}); | ||
it('throws error when not initialized and tries to set', function () { | ||
assert.throws(() => reqContext.set('test2:yoyo', 'myVal'), /No active context found to set property/); | ||
}); | ||
it('get returns undefined when running in domain context', function () { | ||
const d = domain.create(); | ||
d.enter(); | ||
assert.deepEqual(reqContext.get('test2'), undefined); | ||
assert.deepEqual(reqContext.get('test2'), undefined); | ||
assert.deepEqual(reqContext.get('test2'), undefined); | ||
d.exit(); | ||
}); | ||
it('throws error when not initialized and tries to set when running in domain context', function () { | ||
const d = domain.create(); | ||
d.enter(); | ||
assert.throws(() => reqContext.set('test2:yoyo', 'myVal'), /No active context found to set property/); | ||
d.exit(); | ||
}); | ||
}); | ||
describe('context path syntax', function () { | ||
@@ -213,2 +243,5 @@ | ||
function run() { | ||
// Init context like we do in the middleware | ||
domain.active.__$cntxt__ = {}; | ||
setTimeout(setAsync, 0); | ||
@@ -224,2 +257,2 @@ reqContext.setContext('test:value', testValue); | ||
}); | ||
}); |
{ | ||
"name": "request-context", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Simple connect middleware to wrap the request handling in a domain and set and access data for the current request lifecycle only.", | ||
@@ -32,4 +32,5 @@ "main": "lib/index.js", | ||
"gulp": "^3.8.11", | ||
"gulp-jsdoc": "^0.1.4", | ||
"gulp-mocha": "^2.0.0", | ||
"gulp-eslint": "^3.0.1", | ||
"gulp-jsdoc3": "^1.0.1", | ||
"gulp-mocha": "^3.0.1", | ||
"gulp-util": "^3.0.4", | ||
@@ -36,0 +37,0 @@ "request": "^2.53.0" |
@@ -1,2 +0,2 @@ | ||
# request-context ![alt tag](https://travis-ci.org/michaelkrone/request-context.svg) | ||
# request-context | ||
Simple connect middleware for accessing data in a request context. | ||
@@ -13,3 +13,3 @@ Wrap the request handling in a domain and set and access data for the current request lifecycle only. | ||
See the [Domain Docs](https://nodejs.org/api/domain.html) for further information on error handling | ||
for domains. | ||
for domains. Note that the domain module is pending deprecation! | ||
@@ -68,3 +68,3 @@ ## Install | ||
this.modifiedBy = contextService.get('request:user.name'); | ||
// or this.modifiedBy = contextService.get('request').user.name; | ||
// or this.modifiedBy = contextService.get('request').user.name; | ||
next(); | ||
@@ -76,2 +76,4 @@ }); | ||
Also available on [the github pages](http://michaelkrone.github.io/request-context/). | ||
- `middleware` | ||
@@ -85,3 +87,3 @@ Returns a function that can be used as connect middleware. Takes a string as the name of the namespace as its argument. All functions called after this middleware, async or not, will have read/write access to the context. | ||
- `set`, `setContext` | ||
Set the context for a key | ||
Set the context for a key on the context created by the middleware. | ||
```js | ||
@@ -94,3 +96,3 @@ var contextService = require('request-context'); | ||
- `get`, `getContext` | ||
Get the context for a key | ||
Get the context for a key on the context created by the middleware. | ||
```js | ||
@@ -97,0 +99,0 @@ var contextService = require('request-context'); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18962
9
464
142
7