Socket
Socket
Sign inDemoInstall

@emartech/cls-adapter

Package Overview
Dependencies
7
Maintainers
63
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.1.1

28

package.json
{
"name": "@emartech/cls-adapter",
"version": "1.1.0",
"description": "Wrapper around cls-hooked for easier access",
"version": "1.1.1",
"description": "Continuation Local Storage made easy",
"main": "index.js",

@@ -9,6 +9,9 @@ "scripts": {

"test:watch": "mocha ./src/ --recursive --watch",
"semantic-release": "CI=true semantic-release pre && npm publish --access public && semantic-release post",
"semantic-release": "CI=true semantic-release",
"koa-example": "DEBUG=* node examples/koa.js",
"express-example": "DEBUG=* node examples/express.js"
},
"publishConfig": {
"access": "public"
},
"repository": {

@@ -20,3 +23,3 @@ "type": "git",

"cls-hooked": "4.2.2",
"uuid": "3.1.0"
"uuid": "3.2.1"
},

@@ -26,13 +29,8 @@ "devDependencies": {

"express": "4.16.2",
"koa": "2.3.0",
"mocha": "3.5.0",
"semantic-release": "7.0.2",
"sinon": "3.2.1",
"sinon-chai": "2.13.0"
"koa": "2.5.0",
"mocha": "5.0.0",
"semantic-release": "12.4.1",
"sinon": "4.3.0",
"sinon-chai": "2.14.0"
},
"release": {
"verifyConditions": {
"path": "./node_modules/semantic-release/src/lib/plugin-noop.js"
}
},
"keywords": [],

@@ -48,2 +46,2 @@ "author": "Emartech",

"homepage": "https://github.com/emartech/cls-adapter#readme"
}
}
# cls-adapter
Wrapper around [cls-hooked](https://github.com/Jeff-Lewis/cls-hooked) for easier access.
A wrapper around the Continuation Local Storage library [cls-hooked](https://github.com/Jeff-Lewis/cls-hooked).
Makes storing and retrieving of context dependent information easier.
It acts as a thread aware storage.
The aim is to store the unique request identifier from the header (X-Request-Id)
and make it available on any function call that is after the middleware during a request lifecycle.
This way at any point of the application we can retrieve the request identifier from the storage without having to pass it down to every function call.
We can even add more information to the storage besides the request identifier and access it in consecutive function calls.
When a value is set on the storage with a given key,
that value will be available inside functions calls from the parent function.

@@ -15,14 +15,22 @@ ### Basic usage

const Koa = require('koa');
const clsAdapter = require('@emartech/cls-adapter');
const port = 3000;
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(clsAdapter.getKoaMiddleware());
app.use(async (ctx) => {
clsAdapter.setOnContext('customer_id', Math.round(Math.random() * 1000));
ClsAdapter.setOnContext('customer_id', 1000);
// will return an object with request_id and customer_id property set
console.log(clsAdapter.getContextStorage());
logWithStorage('works');
// { message: 'works', request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }
calculationResult();
// { message: 100, request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }

@@ -32,5 +40,84 @@ ctx.body = 'It works';

app.listen(port);
console.log('listening on port: ' + port);
app.listen(3000);
```
### Middlewares
##### ClsAdapter.getKoaMiddleware()
Returns a middleware function compatible with Koa that stores (or generates of missing)
the request identifier from the header (X-Request-Id) and sets it on the storage as `request_id`.
```javascript
const app = new Koa();
app.use(ClsAdapter.getKoaMiddleware());
app.use(async () => {
ClsAdapter.getContextStorage();
// { request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }
});
```
##### ClsAdapter.getExpressMiddleware()
Returns a middleware function compatible with Express that stores (or generates of missing)
the request identifier from the header (X-Request-Id) and sets it on the storage as `request_id`.
```javascript
const app = express();
app.use(ClsAdapter.getExpressMiddleware());
app.use(() => {
ClsAdapter.getContextStorage();
// { request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }
});
```
##### ClsAdapter.getContextStorage()
Returns the all the values set on the storage.
##### ClsAdapter.setOnContext(key, value)
Sets a key with a given value on the storage.
```javascript
ClsAdapter.setOnContext('customer_id', 1);
ClsAdapter.getContextStorage();
// { customer_id: 1 }
```
##### ClsAdapter.getRequestId()
Returns the the request identifier set on the storage. The identifiers key is `request_id`.
```javascript
ClsAdapter.setOnContext('request_id', 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a');
ClsAdapter.getRequestId();
// 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a'
```
##### ClsAdapter.addContextStorageToInput()
Returns a function that extends the given object with the current storage.
```javascript
ClsAdapter.setOnContext('customer_id', 1);
ClsAdapter.addContextStorageToInput()({ debug: true });
// { debug: true, customer_id: 1 }
```
##### ClsAdapter.addRequestIdToInput()
Returns a function that extends the given object with the request identifier set on the current storage.
```javascript
ClsAdapter.setOnContext('request_id', 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a');
ClsAdapter.addRequestIdToInput()({ debug: true });
// { debug: true, request_id: 'd5caaa0e-b04e-4d94-bc88-3ed3b62dc94a' }
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc