{"id":21863183,"url":"https://github.com/shfshanyue/cls-session","last_synced_at":"2025-04-14T19:44:38.092Z","repository":{"id":40783491,"uuid":"247445958","full_name":"shfshanyue/cls-session","owner":"shfshanyue","description":"a cls session impletation using async_hooks","archived":false,"fork":false,"pushed_at":"2023-01-24T01:46:29.000Z","size":714,"stargazers_count":5,"open_issues_count":13,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-15T04:27:34.592Z","etag":null,"topics":["async-hooks","context","logger","trace"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shfshanyue.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-15T10:39:34.000Z","updated_at":"2023-05-29T07:13:54.000Z","dependencies_parsed_at":"2023-02-04T10:16:38.309Z","dependency_job_id":null,"html_url":"https://github.com/shfshanyue/cls-session","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shfshanyue%2Fcls-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shfshanyue%2Fcls-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shfshanyue%2Fcls-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shfshanyue%2Fcls-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shfshanyue","download_url":"https://codeload.github.com/shfshanyue/cls-session/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226852601,"owners_count":17692307,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async-hooks","context","logger","trace"],"created_at":"2024-11-28T03:20:25.747Z","updated_at":"2024-11-28T03:20:26.365Z","avatar_url":"https://github.com/shfshanyue.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM](https://nodei.co/npm/cls-session.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://nodei.co/npm/cls-session/)\n\n# cls-session\n\n[![npm version](https://img.shields.io/npm/v/cls-session.svg?style=flat-square)](https://www.npmjs.org/package/cls-session)\n![build status](https://img.shields.io/github/workflow/status/shfshanyue/cls-session/CLS%20Session%20Test?style=flat-square)\n[![install size](https://packagephobia.now.sh/badge?p=cls-session)](https://packagephobia.now.sh/result?p=cls-session)\n[![npm downloads](https://img.shields.io/npm/dw/cls-session.svg?style=flat-square)](http://npm-stat.com/charts.html?package=cls-session)\n\nContinuation Local Storage works like thread-local storage in threaded programming. This is a implementation of CLS using async_hooks instead of async-listener.\n\n## Installation\n\nIt requires node v8.2.1 or higher for ES2015 and async_hooks support.\n\n``` bash\n$ npm install cls-session\n```\n\n## Usage\n\n``` js\nconst Session = require('cls-session')\n\nconst session = new Session()\n\nfunction timeout (id) {\n  session.scope(() =\u003e {\n    session.set('a', id)\n    setTimeout(() =\u003e {\n      const a = session.get('a')\n      console.log(a)\n    })\n  })\n}\n\ntimeout(1)\ntimeout(2)\ntimeout(3)\n\n// Output:\n// 1\n// 2\n// 3\n```\n\n## Middleware in koa\n\n``` js\nimport Session from 'cls-session'\nimport Koa from 'koa'\n\nconst app = new Koa()\nconst session = new Session()\n\napp.use(session.middleware())\n\napp.use(async (ctx, next) =\u003e {\n  session.set('userId', 10086)\n  await next()\n})\n\napp.use((ctx) =\u003e {\n  const userId = session.get('userId')\n  ctx.body = userId\n})\n\napp.listen(3200)\n```\n\n## Middleware in express\n\n``` js\nimport Session from 'cls-session'\nimport express from 'express'\n\nconst app = express()\nconst session = new Session()\n\napp.use(session.expressMiddleware())\n\napp.use((req, res, next) =\u003e {\n  session.set('userId', 10086)\n  next()\n})\n\napp.use((req, res, next) =\u003e {\n  const userId = session.get('userId')\n  res.send(userId)\n})\n\napp.listen(3200, () =\u003e {\n  console.log('Listen 3200')\n})\n```\n\n## API\n\n### session.scope(callback: () =\u003e Promise\u003cany\u003e | any): Promise\u003cany\u003e\n\nCreate 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.\n\n### session.set(key: any, value: any)\n\nSet a value on the current continuation context.\n\n### session.get(key: any): any\n\nGet a value on the current continuation context.\n\n### session.middleware()\n\nA middleware of koa.\n\n### session.expressMiddleware()\n\nA middleware of express.\n\n### session.context: Map\n\n### session.size: number\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshfshanyue%2Fcls-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshfshanyue%2Fcls-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshfshanyue%2Fcls-session/lists"}