{"name":"koa-session","description":"Koa cookie session middleware with external store support","repository":{"type":"git","url":"git+ssh://git@github.com/koajs/session.git"},"version":"7.0.2","keywords":["koa","middleware","session","cookie"],"devDependencies":{"@arethetypeswrong/cli":"^0.17.1","@eggjs/bin":"7","@eggjs/supertest":"8","@eggjs/tsconfig":"1","@types/crc":"^3.8.3","@types/koa":"^2.15.0","@types/mocha":"10","@types/node":"22","eslint":"8","eslint-config-egg":"14","koa":"2","mm":"4","rimraf":"6","snap-shot-it":"^7.9.10","tshy":"3","tshy-after":"1","typescript":"5"},"license":"MIT","dependencies":{"crc":"^3.8.0","is-type-of":"^2.2.0","zod":"^3.24.1"},"engines":{"node":">= 18.19.0"},"scripts":{"lint":"eslint --cache src test --ext .ts","pretest":"npm run clean && npm run lint -- --fix","test":"egg-bin test","preci":"npm run clean &&  npm run lint","ci":"egg-bin cov","postci":"npm run prepublishOnly && npm run clean","clean":"rimraf dist","prepublishOnly":"tshy && tshy-after && attw --pack"},"type":"module","tshy":{"exports":{".":"./src/index.ts","./package.json":"./package.json"}},"exports":{".":{"import":{"types":"./dist/esm/index.d.ts","default":"./dist/esm/index.js"},"require":{"types":"./dist/commonjs/index.d.ts","default":"./dist/commonjs/index.js"}},"./package.json":"./package.json"},"types":"./dist/commonjs/index.d.ts","main":"./dist/commonjs/index.js","module":"./dist/esm/index.js","_id":"koa-session@7.0.2","gitHead":"36beadadf9db645ae5a0c8544544e4e6cfb203cc","bugs":{"url":"https://github.com/koajs/session/issues"},"homepage":"https://github.com/koajs/session#readme","_nodeVersion":"20.18.1","_npmVersion":"10.8.2","dist":{"integrity":"sha512-nMWJndLmIuKQMTYPr5NokGQOGD2Aqal5GVi1xAhrQjrrzKq1ASy1WTFVkZ/xhwhtC4KpWi5KdqNYewZo7KJb4w==","shasum":"51b4c2ded3991c74f4ec5f3a07c06b68da5b7344","tarball":"http://registry-npm.ywwl.com/koa-session/download/koa-session-7.0.2.tgz","fileCount":26,"unpackedSize":163333,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/koa-session@7.0.2","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFu45EGGH35joyzM6HEWspXA53Mhx80+Rt7DnXuv6kr0AiAc12+D9x8hENah9dQ1ezx0oq3YtBuyWd5nYOAqOrOgGQ=="}],"size":28101,"noattachment":false,"key":"/koa-session/-/koa-session-7.0.2.tgz"},"_npmUser":{"name":"fengmk2","email":"fengmk2@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/koa-session_7.0.2_1737279319341_0.08133447708745534"},"_hasShrinkwrap":false,"readme":"# koa-session\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/koajs/session/actions/workflows/nodejs.yml/badge.svg)](https://github.com/koajs/session/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n[![Node.js Version](https://img.shields.io/node/v/koajs/session.svg?style=flat)](https://nodejs.org/en/download/)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)\n\n[npm-image]: https://img.shields.io/npm/v/koa-session.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-session\n[codecov-image]: https://codecov.io/gh/koajs/session/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/koajs/session\n[snyk-image]: https://snyk.io/test/npm/koa-session/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/koa-session\n[download-image]: https://img.shields.io/npm/dm/koa-session.svg?style=flat-square\n[download-url]: https://npmjs.org/package/koa-session\n\nSimple session middleware for Koa. Defaults to cookie-based sessions and supports external stores.\n\n## Installation\n\n```bash\nnpm install koa-session\n```\n\n## Notice\n\n7.x has a breaking change: drop Node.js < 18.19.0 support. And it support CommonJS and ESM both.\n\n6.x changed the default cookie key from `koa:sess` to `koa.sess` to ensure `set-cookie` value valid with HTTP spec.\n[See issue](https://github.com/koajs/session/issues/28).\nIf you want to be compatible with the previous version, you can manually set `config.key` to `koa:sess`.\n\n## Example\n\nView counter example:\n\n```js\nimport Koa from 'koa';\nimport session from 'koa-session';\n\nconst app = new Koa();\n\napp.keys = ['some secret hurr'];\n\nconst CONFIG = {\n  key: 'koa.sess', /** (string) cookie key (default is koa.sess) */\n  /** (number || 'session') maxAge in ms (default is 1 days) */\n  /** 'session' will result in a cookie that expires when session/browser is closed */\n  /** Warning: If a session cookie is stolen, this cookie will never expire */\n  maxAge: 86400000,\n  autoCommit: true, /** (boolean) automatically commit headers (default true) */\n  overwrite: true, /** (boolean) can overwrite or not (default true) */\n  httpOnly: true, /** (boolean) httpOnly or not (default true) */\n  signed: true, /** (boolean) signed or not (default true) */\n  rolling: false, /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */\n  renew: false, /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/\n  secure: true, /** (boolean) secure cookie*/\n  sameSite: null, /** (string) session cookie sameSite options (default null, don't set it) */\n};\n\napp.use(session(CONFIG, app));\n// or if you prefer all default config, just use => app.use(session(app));\n\napp.use(ctx => {\n  // ignore favicon\n  if (ctx.path === '/favicon.ico') return;\n\n  let n = ctx.session.views || 0;\n  ctx.session.views = ++n;\n  ctx.body = n + ' views';\n});\n\napp.listen(3000);\nconsole.log('listening on port 3000');\n```\n\n## API\n\n### Options\n\nThe cookie name is controlled by the `key` option, which defaults\nto \"koa.sess\". All other options are passed to `ctx.cookies.get()` and\n`ctx.cookies.set()` allowing you to control security, domain, path,\nand signing among other settings.\n\n#### Custom `encode/decode` Support\n\nUse `options.encode` and `options.decode` to customize your own encode/decode methods.\n\n### Hooks\n\n- `valid()`: valid session value before use it\n- `beforeSave()`: hook before save session\n\n### External Session Stores\n\nThe session is stored in a cookie by default, but it has some disadvantages:\n\n- Session is stored on client side unencrypted\n- [Browser cookies always have length limits](http://browsercookielimits.squawky.net/)\n\n  You can store the session content in external stores (Redis, MongoDB or other DBs) by passing `options.store` with three methods (these need to be async functions):\n\n- `get(key, maxAge, { rolling, ctx })`: get session object by key\n- `set(key, sess, maxAge, { rolling, changed, ctx })`: set session object for key, with a `maxAge` (in ms)\n- `destroy(key, {ctx})`: destroy session for key\n\n  Once you pass `options.store`, session storage is dependent on your external store -- you can't access the session if your external store is down. **Use external session stores only if necessary, avoid using session as a cache, keep the session lean, and store it in a cookie if possible!**\n\n  The way of generating external session id is controlled by the `options.genid(ctx)`, which defaults to `uuid.v4()`.\n\n  If you want to add prefix for all external session id, you can use `options.prefix`, it will not work if `options.genid(ctx)` present.\n\n  If your session store requires data or utilities from context, `opts.ContextStore` is also supported. `ContextStore` must be a class which claims three instance methods demonstrated above. `new ContextStore(ctx)` will be executed on every request.\n\n### Events\n\n`koa-session` will emit event on `app` when session expired or invalid:\n\n- `session:missed`: can't get session value from external store.\n- `session:invalid`: session value is invalid.\n- `session:expired`: session value is expired.\n\n### Custom External Key\n\nExternal key is used the cookie by default, but you can use `options.externalKey` to customize your own external key methods. `options.externalKey` with two methods:\n\n- `get(ctx)`: get the external key\n- `set(ctx, value)`: set the external key\n\n### Session#isNew\n\nReturns **true** if the session is new.\n\n```js\nif (this.session.isNew) {\n  // user has not logged in\n} else {\n  // user has already logged in\n}\n```\n\n### Session#maxAge\n\nGet cookie's maxAge.\n\n### Session#maxAge=\n\nSet cookie's maxAge.\n\n### Session#externalKey\n\nGet session external key, only exist when external session store present.\n\n### Session#save()\n\nSave this session no matter whether it is populated.\n\n### Session#manuallyCommit()\n\nSession headers are auto committed by default. Use this if `autoCommit` is set to `false`.\n\n### Destroying a session\n\nTo destroy a session simply set it to `null`:\n\n```js\nthis.session = null;\n```\n\n## License\n\n[MIT](LICENSE)\n\n## Contributors\n\n[![Contributors](https://contrib.rocks/image?repo=koajs/session)](https://github.com/koajs/session/graphs/contributors)\n\nMade with [contributors-img](https://contrib.rocks).\n","publish_time":1737279319561,"maintainers":[{"name":"coderhaoxin","email":"haoxinst@gmail.com"},{"name":"aaron","email":"aaron.heckmann+github@gmail.com"},{"name":"juliangruber","email":"julian@juliangruber.com"},{"name":"eivifj","email":"eivind.fjeldstad@gmail.com"},{"name":"dead_horse","email":"dead_horse@qq.com"},{"name":"tjholowaychuk","email":"tj@vision-media.ca"},{"name":"jongleberry","email":"jonathanrichardong@gmail.com"},{"name":"fengmk2","email":"fengmk2@gmail.com"}],"_cnpm_publish_time":1737279319561,"dist-tags":{"latest":"7.0.2","latest-2":"2.0.1","latest-4":"4.8.1","v4":"4.8.1"}}