Installations
npm install node-cls
Releases
Unable to fetch releases
Developer
alfateam
Developer Guide
Module System
CommonJS
Min. Node Version
>= 8.0.0
Typescript Support
No
Node Version
8.16.0
NPM Version
6.13.1
Statistics
11 Stars
41 Commits
6 Watching
1 Branches
2 Contributors
Updated on 25 Jun 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
408,217
Last day
6.4%
1,392
Compared to previous day
Last week
6.4%
7,259
Compared to previous week
Last month
7.9%
29,144
Compared to previous month
Last year
143.8%
224,901
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
Continuation Local Storage
The purpose with this module is to share contexts across async (and sync) calls. Contexts are accessed by keys and can be nested. It is an alternative to the deprecated domain. It is based on async_hooks that were introduced in node 8. Beware that async_hooks are still experimental in nodejs.
To avoid weird behavior with express
- Make sure you require
node-cls
in the first row of your app. Some popular packages use async which breaks CLS. - If you are using
body-parser
and context is getting lost, register it in express before you registernode-cls
's middleware.
Request handler
A typical scenario is when you need to share context in a request handler.
1let http = require('http'); 2let cls = require('node-cls'); 3 4let server = http.createServer(function (request, response) { 5 let context = cls.create('request-context'); 6 context.id = 1; 7 context.request = request; 8 context.response = response; 9 context.run(doWork); 10}) 11 12server.listen(8080) 13 14function doWork() { 15 let context = cls.get('request-context'); 16 context.response.end(`End: ${context.id}`) //End: 1 17 18}
Async calls
Context is retained in async calls.
1let cls = require('node-cls'); 2 3let context = cls.create('myContext'); 4context.run(() => { 5 context.name = 'George'; 6 setTimeout(onTimeout, 300); 7}); 8 9function onTimeout() { 10 let context = cls.get('myContext'); 11 console.log(context.name); //George 12}
Nesting
Contexts can be nested, even on the same key.
1let cls = require('node-cls'); 2 3let context = cls.create('myContext'); 4context.run(async () => { 5 context.name = 'George'; 6 let context2 = cls.create('myContext'); 7 await context2.run(onNested); 8 console.log(context.name) //George 9}); 10 11async function onNested() { 12 await Promise.resolve(); 13 let context = cls.get('myContext'); 14 console.log(context.name); //undefined 15 context.name = 'John Nested'; 16 setTimeout(onTimeout, 300); 17} 18 19function onTimeout() { 20 let context = cls.get('myContext'); 21 console.log(context.name); //John Nested 22}
Symbol as key
If you are a library author, use a Symbol as key to avoid conflicts with other libraries.
1let cls = require('node-cls'); 2let key = Symbol(); 3 4let context = cls.create(key); 5context.run(() => { 6 context.name = 'George'; 7 setTimeout(onTimeout, 300); 8}); 9 10function onTimeout() { 11 let context = cls.get(key); 12 console.log(context.name); //George 13}
Start vs run
In node 12 and above you can start a context directly instead of wrapping it in the run function. The start function returns a promise. You can leave the current context by calling exit.
1let cls = require('node-cls'); 2 3async function main() { 4 let context = cls.create('myContext'); 5 context.name = 'George'; 6 await context.start(); 7 8 let context2 = cls.create('myContext'); 9 context2.name = 'John Nested'; 10 await context2.start(); 11 console.log(cls.get('myContext').name); //John Nested 12 13 cls.exit('myContext'); 14 console.log(cls.get('myContext').name); //George 15 16 cls.exit('myContext'); 17 console.log(cls.get('myContext')); //undefined 18} 19 20main();
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
2.6
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to node-cls
cls-hooked
CLS using AsynWrap instead of async-listener - Node >= 4.7.0
cls-rtracer
Express & Koa middlewares and Fastify & Hapi plugins for CLS-based request id generation, batteries included
@types/cls-hooked
TypeScript definitions for cls-hooked
nestjs-cls
A continuation-local storage module compatible with NestJS's dependency injection.