![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@emartech/cls-adapter
Advanced tools
A wrapper around the Continuation Local Storage library cls-hooked. Makes storing and retrieving of context dependent information easier. It acts as a thread aware storage.
When a value is set on the storage with a given key, that value will be available inside functions calls from the parent function.
npm install @emartech/cls-adapter
const Koa = require('koa');
const ClsAdapter = require('@emartech/cls-adapter');
const logWithStorage = (message) => {
console.log(Object.assign({ message }, ClsAdapter.getContextStorage()));
};
const calculationResult = () => {
logWithStorage(100);
};
const app = new Koa();
app.use(ClsAdapter.getKoaMiddleware());
app.use(async (ctx) => {
ClsAdapter.setOnContext('customer_id', 1000);
logWithStorage('works');
// { message: 'works', request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }
calculationResult();
// { message: 100, request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }
ctx.body = 'It works';
});
app.listen(3000);
Returns a middleware function compatible with Koa that stores (or generates if missing)
the request identifier from the header (X-Request-Id) and sets it on the storage as request_id
.
const app = new Koa();
app.use(ClsAdapter.getKoaMiddleware());
app.use(async () => {
ClsAdapter.getContextStorage();
// { request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }
});
Returns a middleware function compatible with Express that stores (or generates if missing)
the request identifier from the header (X-Request-Id) and sets it on the storage as request_id
.
const app = express();
app.use(ClsAdapter.getExpressMiddleware());
app.use(() => {
ClsAdapter.getContextStorage();
// { request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }
});
Returns the all the values set on the storage.
Sets a key with a given value on the storage.
ClsAdapter.setOnContext('customer_id', 1);
ClsAdapter.setOnContext('application.customer.id', 11);
ClsAdapter.getContextStorage();
// { customer_id: 1, application: { customer: { id: 11 } } }
Returns the the request identifier set on the storage. The identifiers key is request_id
.
ClsAdapter.setOnContext('request_id', 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a');
ClsAdapter.getRequestId();
// 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a'
Returns a function that extends the given object with the current storage.
ClsAdapter.setOnContext('customer_id', 1);
ClsAdapter.addContextStorageToInput()({ debug: true });
// { debug: true, customer_id: 1 }
Returns a function that extends the given object with the request identifier set on the current storage.
ClsAdapter.setOnContext('request_id', 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a');
ClsAdapter.addRequestIdToInput()({ debug: true });
// { debug: true, request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }
FAQs
Continuation Local Storage made easy
The npm package @emartech/cls-adapter receives a total of 400 weekly downloads. As such, @emartech/cls-adapter popularity was classified as not popular.
We found that @emartech/cls-adapter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 217 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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.