You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

express-http-context

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-http-context - npm Package Compare versions

Comparing version

to
2.0.0-rc.0

1

browser.js

@@ -11,3 +11,2 @@ module.exports = {

},
ns: null,
};

25

index.js
'use strict';
const cls = require('cls-hooked');
const { AsyncLocalStorage } = require('async_hooks');
const asyncLocalStorage = new AsyncLocalStorage();
const nsid = 'a6a29a6f-6747-4b5f-b99f-07ee96e32f88';
const ns = cls.createNamespace(nsid);
/** Express.js middleware that is responsible for initializing the context for each request. */
function middleware(req, res, next) {
ns.run(() => next());
asyncLocalStorage.run(new Map(), next);
}

@@ -18,5 +16,3 @@

function get(key) {
if (ns && ns.active) {
return ns.get(key);
}
return asyncLocalStorage.getStore()?.get(key);
}

@@ -26,9 +22,11 @@

* Adds a value to the context by key. If the key already exists, its value will be overwritten. No value will persist if the context has not yet been initialized.
* @param {string} key
* @param {*} value
* @param {string} key
* @param {*} value
*/
function set(key, value) {
if (ns && ns.active) {
return ns.set(key, value);
if (asyncLocalStorage.getStore()) {
asyncLocalStorage.getStore()?.set(key, value);
return value;
}
return undefined;
}

@@ -39,4 +37,3 @@

get: get,
set: set,
ns: ns
set: set
};
{
"name": "express-http-context",
"version": "1.2.5",
"version": "2.0.0-rc.0",
"description": "Get and set request-scoped context anywhere",

@@ -8,3 +8,3 @@ "main": "index.js",

"engines": {
"node": ">=8.0.0 <10.0.0 || >=10.4.0"
"node": ">=14.8.0"
},

@@ -36,5 +36,3 @@ "scripts": {

"dependencies": {
"@types/cls-hooked": "^4.2.1",
"@types/express": "^4.16.0",
"cls-hooked": "^4.2.2"
"@types/express": "^4.16.0"
},

@@ -41,0 +39,0 @@ "devDependencies": {

@@ -5,6 +5,5 @@ [![build](https://img.shields.io/github/actions/workflow/status/skonves/express-http-context/build.yml?branch=master)](https://github.com/skonves/express-http-context/actions/workflows/build.yml)

[![npm](https://img.shields.io/npm/dm/express-http-context.svg)](https://www.npmjs.com/package/express-http-context)
[![david](https://img.shields.io/david/skonves/express-http-context.svg)](https://david-dm.org/skonves/express-http-context)
# Express HTTP Context
Get and set request-scoped context anywhere. This is just an unopinionated, idiomatic ExpressJS implementation of [cls-hooked](https://github.com/Jeff-Lewis/cls-hooked) (forked from [continuation-local-storage](https://www.npmjs.com/package/continuation-local-storage)). It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data. Context is preserved even over async/await (in node 8+).
Get and set request-scoped context anywhere. This is just an unopinionated, idiomatic ExpressJS implementation of [Node AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage). It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data.

@@ -14,3 +13,2 @@ ## How to use it

Install: `npm install --save express-http-context`
(Note: For node v4-7, use the legacy version: `npm install --save express-http-context@<1.0.0`)

@@ -63,7 +61,7 @@ Use the middleware immediately before the first middleware that needs to have access to the context.

You can access cls namespace directly as (it may be useful if you want to apply some patch to it, for example https://github.com/TimBeyer/cls-bluebird):
``` js
var ns = require('express-http-context').ns;
```
## Legacy versions
* For Node <7: `npm install --save express-http-context@0`
* For Node >=8 <12: `npm install --save express-http-context@1`
## Troubleshooting

@@ -84,3 +82,4 @@ To avoid weird behavior with express:

* William Durand (@willdurand)
* Kristopher Morris (@beeduck)
Interesting in contributing? Take a look at the [Contributing Guidlines](/CONTRIBUTING.md)