Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
request-context
Advanced tools
Simple connect middleware to wrap the request handling in a domain and set and access data for the current request lifecycle only.
Simple connect middleware for accessing data in a request context. Wrap the request handling in a domain and set and access data for the current request lifecycle only. All following functions will be run in the created 'namespace'.
You would like to access data from the request or any middleware in a completely different context. Due to the async architecture of Node it can become a nightmare to pass the data to all callbacks is the function chain. This module provides a middleware and an easy to use API to access data from anywhere in the function chain. No matter if the functions are called async or not.
See the Domain Docs for further information on error handling for domains.
$ npm install request-context
server config in app.js:
var app = express();
var contextService = require('request-context');
// wrap requests in the 'request' namespace (can be any string)
app.use(contextService.middleware('request'));
// set the logged in user in some auth middleware
app.use(function (req, res, next) {
User.findById(req.cookies._id, function (err, user) {
// set the user who made this request on the context
contextService.set('request:user', user);
next();
});
});
// save a model in put requests
app.put(function (req, res, next) {
new Model(req.body).save(function (err, doc) {
res.json(doc);
});
});
// start server etc.
[...]
In the Model definition file:
var contextService = require('request-context');
[...]
// set the user who made changes to this document
// note that this method is called async in the document context
modelSchema.pre('save', function (next) {
// access the user object which has been set in the request middleware
this.modifiedBy = contextService.get('request:user.name');
// or this.modifiedBy = contextService.get('request').user.name;
next();
});
middleware
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.var middleware = require('request-context').middleware;
app.use(middleware('some namespace'));
set
, setContext
Set the context for a keyvar contextService = require('request-context');
contextService.set('namespace:key', {some: 'value'});
get
, getContext
Get the context for a keyvar contextService = require('request-context');
contextService.get('namespace:key.some'); // returns 'value'
Any value from the context object can be accessed by a simple object dot notation:
var contextService = require('request-context');
// set an object on the namespace
contextService.set('namespace:character', {
name: 'Arya Stark',
location: {
name: 'Winterfell',
region: 'North'
}
});
// this will return the complete object
var char = contextService.get('namespace:character');
// work with the object
var region = char.location.region;
// this will return 'Arya Stark'
contextService.get('namespace:character.name')
// this will return the location object
contextService.get('namespace:character.location')
// this will return 'North'
contextService.get('namespace:character.location.region')
To generate the jsdoc documentation run
$ gulp docs
To run the packaged tests:
$ gulp test
FAQs
Simple connect middleware to wrap the request handling in a domain and set and access data for the current request lifecycle only.
The npm package request-context receives a total of 9,927 weekly downloads. As such, request-context popularity was classified as popular.
We found that request-context demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.