# cls-session

> CLS using async hooks instead of async-listener

Latest version **1.1.2** (published 2020-04-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install cls-session
pnpm add cls-session
yarn add cls-session
bun add cls-session
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2020-04-20 |
| First published | 2020-03-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.2.1 |
| Dependencies | 0 |
| Unpacked size | 6.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | shanyue |
| Maintainers | shanyue |
| Keywords | cls, threading, async_hoks, shared, context, domains, tracing, logging |

## Links

- npm: https://www.npmjs.com/package/cls-session
- Repository: https://github.com/shfshanyue/cls-session
- Homepage: https://github.com/shfshanyue/cls-session#readme
- Issues: https://github.com/shfshanyue/cls-session/issues
- npm.io page: https://npm.io/package/cls-session

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.1.2 (latest) — 2020-04-20
- 1.1.1 — 2020-04-07
- 1.1.0 — 2020-04-04
- 1.0.7 — 2020-04-02
- 1.0.6 — 2020-04-01
- 1.0.5 — 2020-03-29
- 1.0.4 — 2020-03-29
- 1.0.3 — 2020-03-29
- 1.0.2 — 2020-03-24
- 1.0.1 — 2020-03-18
- 1.0.0 — 2020-03-18

## README

[![NPM](https://nodei.co/npm/cls-session.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/cls-session/)

# cls-session

[![npm version](https://img.shields.io/npm/v/cls-session.svg?style=flat-square)](https://www.npmjs.org/package/cls-session)
![build status](https://img.shields.io/github/workflow/status/shfshanyue/cls-session/CLS%20Session%20Test?style=flat-square)
[![install size](https://packagephobia.now.sh/badge?p=cls-session)](https://packagephobia.now.sh/result?p=cls-session)
[![npm downloads](https://img.shields.io/npm/dw/cls-session.svg?style=flat-square)](http://npm-stat.com/charts.html?package=cls-session)

Continuation Local Storage works like thread-local storage in threaded programming. This is a implementation of CLS using async_hooks instead of async-listener.

## Installation

It requires node v8.2.1 or higher for ES2015 and async_hooks support.

``` bash
$ npm install cls-session
```

## Usage

``` js
const Session = require('cls-session')

const session = new Session()

function timeout (id) {
  session.scope(() => {
    session.set('a', id)
    setTimeout(() => {
      const a = session.get('a')
      console.log(a)
    })
  })
}

timeout(1)
timeout(2)
timeout(3)

// Output:
// 1
// 2
// 3
```

## Middleware in koa

``` js
import Session from 'cls-session'
import Koa from 'koa'

const app = new Koa()
const session = new Session()

app.use(session.middleware())

app.use(async (ctx, next) => {
  session.set('userId', 10086)
  await next()
})

app.use((ctx) => {
  const userId = session.get('userId')
  ctx.body = userId
})

app.listen(3200)
```

## Middleware in express

``` js
import Session from 'cls-session'
import express from 'express'

const app = express()
const session = new Session()

app.use(session.expressMiddleware())

app.use((req, res, next) => {
  session.set('userId', 10086)
  next()
})

app.use((req, res, next) => {
  const userId = session.get('userId')
  res.send(userId)
})

app.listen(3200, () => {
  console.log('Listen 3200')
})
```

## API

### session.scope(callback: () => Promise<any> | any): Promise<any>

Create a new context on which values can be set or read.Run all the functions that are called (either directly, or indirectly through asynchronous functions that take callbacks themselves) from the provided callback within the scope.

### session.set(key: any, value: any)

Set a value on the current continuation context.

### session.get(key: any): any

Get a value on the current continuation context.

### session.middleware()

A middleware of koa.

### session.expressMiddleware()

A middleware of express.

### session.context: Map

### session.size: number

---
_Source: https://npm.io/package/cls-session · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
