{"name":"koa-session","version":"7.0.1","keywords":["koa","middleware","session","cookie"],"license":"MIT","_id":"koa-session@7.0.1","homepage":"https://github.com/koajs/session#readme","bugs":{"url":"https://github.com/koajs/session/issues"},"dist":{"shasum":"4eb5e57af93af44a1289d54e9bd715a904a561b6","tarball":"http://registry-npm.ywwl.com/koa-session/download/koa-session-7.0.1.tgz","fileCount":26,"integrity":"sha512-GAPWqGr9er/E9JiFFrwU6NRsT3pvLikxK0g3DHow+Rd1o+HcKR/fEhbZ1MxOtQRrgr1JV+zc0fViCxh7BIc3nw==","signatures":[{"sig":"MEYCIQCOe5tyUIlw2QROpWq+ZMO54SNHzkZxw+ofjTnsi7TLJgIhAJykVXhI8oELyS1nqexfyz98hEMgL4/XBW2tSuxGdZG0","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/koa-session@7.0.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":162876,"size":27960,"noattachment":false,"key":"/koa-session/-/koa-session-7.0.1.tgz"},"main":"./dist/commonjs/index.js","tshy":{"exports":{".":"./src/index.ts","./package.json":"./package.json"}},"type":"module","types":"./dist/commonjs/index.d.ts","module":"./dist/esm/index.js","engines":{"node":">= 18.19.0"},"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"},"gitHead":"6dfdcf675f28d2661bbeb59b9d42bf5c5afdb47f","scripts":{"ci":"egg-bin cov","lint":"eslint --cache src test --ext .ts","test":"egg-bin test","clean":"rimraf dist","preci":"npm run clean &&  npm run lint","postci":"npm run prepublishOnly && npm run clean","pretest":"npm run clean && npm run lint -- --fix","prepublishOnly":"tshy && tshy-after && attw --pack"},"_npmUser":{"name":"fengmk2","email":"fengmk2@gmail.com"},"repository":{"url":"git+ssh://git@github.com/koajs/session.git","type":"git"},"_npmVersion":"10.8.2","description":"Koa cookie session middleware with external store support","directories":{},"_nodeVersion":"20.18.1","dependencies":{"crc":"^3.8.0","zod":"^3.24.1","is-type-of":"^2.2.0"},"_hasShrinkwrap":false,"devDependencies":{"mm":"4","koa":"2","tshy":"3","eslint":"8","rimraf":"6","@eggjs/bin":"7","@types/crc":"^3.8.3","@types/koa":"^2.15.0","tshy-after":"1","typescript":"5","@types/node":"22","@types/mocha":"10","snap-shot-it":"^7.9.10","@eggjs/tsconfig":"1","@eggjs/supertest":"8","eslint-config-egg":"14","@arethetypeswrong/cli":"^0.17.1"},"_npmOperationalInternal":{"tmp":"tmp/koa-session_7.0.1_1737274657506_0.7001619365271405","host":"s3://npm-registry-packages-npm-production"},"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":1737274657693,"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":1737274657693,"dist-tags":{"latest":"7.0.2","latest-2":"2.0.1","latest-4":"4.8.1","v4":"4.8.1"}}