Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@northscaler/continuation-local-storage
Advanced tools
Readme
continuation-local-storage
NB: Git repo of truth is https://gitlab.com/northscaler-public/continuation-local-storage; all others are mirrors.
This module provides a means to store information along the entirety of a single Node.js asynchronous call chain, or "continuation".
It is equivalent to a Vert.x Context, and similar to ThreadLocal
from Java, .NET, and other, similar platforms.
Contexts allow you to place information in a continuation-local storage space so that it's available all the way up & down an asynchronous call stack so that you can set & get that information as necessary.
There three flavors to choose from, based on either
cls-hooked
,zone.js
, orAsyncLocalStorage
(requires Node.js >= 12.17.0).Use whichever one you want to; the Context
API is the same:
Context().run(() => { /* your code here */ }, { your: 'context', values: 'here' })
Context().set('name', 'value')
Context().get('name')
Using more than one implementation should work, but is not officially supported. Try to stick with just one provider in your app.
IMPORTANT: In order to minimize transitive dependencies in your application, this module does not require
cls-hooked
orzone.js
itself. The consuming codebase must install the library (or libraries) that it needs.
If you are using ZoneJsContext
:
npm install zone.js
yourself.require('zone.js/dist/zone-node')
at the right time for your application.See zone.js
's documentation for more information.
If you are using ClsHookedContext
:
npm install cls-hooked
yourself.If you are using AsyncLocalStorage
:
>=12.17.0
.setTimeout
, Promise.resolve
or Promise.reject
within your run
call, you must ensure that the autodispose
option is set to false
(the default) in order to make the values available in those places.See this project's package.json
devDependencies
section for the versions of cls-hooked
and zone.js
was built against and try to install compatible ones.
The basic API of these Context
s is straightforward.
Once you get the Context
(via require
or import
), these are the methods you'll use:
Context(key)
:
Retrieves a context with the given key as a string (Symbol
ic names are a todo).
run(fn, values, opts)
:
Runs a given function within the Context
, making any values
, an Object
, available. opts
currently includes only autodispose
and is true
by default.
set(key, value)
Sets the given value
at the given key
.
get(key)
Gets the value
at the given key
.
dispose()
It's not a bad idea to dispose of the Context
when you know you're done with it, but it's not strictly required.
The run
method defaults to manual disposal, but if you observe memory leaks, this would be the first thing to check.
const { ClsHookedContext: Context } = require('@northscaler/continuation-local-storage') // prerequisite: npm install --save cls-hooked
// or: const { ZoneJsContext: Context } = require('@northscaler/continuation-local-storage') // prerequisite: npm install --save zone.js
// or: const { AsyncLocalStorageContext: Context } = require('@northscaler/continuation-local-storage') // prerequisite: Node.js >= 12.17.0
Context().run(
// this is the function that will be run in inside the Context
() => { // uses the default context; pass a string name for a custom context
// Do whatever you want here.
// Context values are accessible anywhere in the sync or async call stack:
const foo = Context().get('foo') // returns 'bar'
// You can set & get values with:
Context().set('baz', 'snafu')
const baz = Context().get('baz') // returns 'snafu'
},
// these are your contextual values available in the async call stack
{
foo: 'bar' // puts the value 'bar' into the context at key 'foo'
})
FAQs
Local storage for asynchronous Node.js call stacks, similar to thread-local storage in multithreaded platforms
The npm package @northscaler/continuation-local-storage receives a total of 44 weekly downloads. As such, @northscaler/continuation-local-storage popularity was classified as not popular.
We found that @northscaler/continuation-local-storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.