express-http-context
Advanced tools
Comparing version
@@ -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 @@ [](https://github.com/skonves/express-http-context/actions/workflows/build.yml) | ||
[](https://www.npmjs.com/package/express-http-context) | ||
[](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) |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
-66.67%7514
-5.22%62
-4.62%1
Infinity%82
-1.2%1
Infinity%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed