{"id":13800082,"url":"https://github.com/Secbone/koa-session2","last_synced_at":"2025-05-13T08:32:37.454Z","repository":{"id":71589146,"uuid":"50669034","full_name":"Secbone/koa-session2","owner":"Secbone","description":"Middleware for Koa2 to get/set session","archived":false,"fork":false,"pushed_at":"2019-12-10T01:43:13.000Z","size":93,"stargazers_count":151,"open_issues_count":6,"forks_count":29,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-01T04:36:12.424Z","etag":null,"topics":["koa2","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Secbone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-01-29T14:55:49.000Z","updated_at":"2025-04-03T02:54:21.000Z","dependencies_parsed_at":"2023-03-13T20:19:59.342Z","dependency_job_id":null,"html_url":"https://github.com/Secbone/koa-session2","commit_stats":{"total_commits":89,"total_committers":9,"mean_commits":9.88888888888889,"dds":0.2134831460674157,"last_synced_commit":"9ee7b2696f4f77bbaa354e3201952f754e98196e"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Secbone%2Fkoa-session2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Secbone%2Fkoa-session2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Secbone%2Fkoa-session2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Secbone%2Fkoa-session2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Secbone","download_url":"https://codeload.github.com/Secbone/koa-session2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253662725,"owners_count":21944120,"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":["koa2","nodejs"],"created_at":"2024-08-04T00:01:09.072Z","updated_at":"2025-05-13T08:32:37.206Z","avatar_url":"https://github.com/Secbone.png","language":"JavaScript","funding_links":[],"categories":["仓库"],"sub_categories":["中间件"],"readme":"# koa-session2\n\n[![NPM version][npm-image]][npm-url]\n[![Build Status][travis-image]][travis-url]\n[![Downloads][downloads-image]][downloads-url]\n[![Test coverage][codecov-image]][codecov-url]\n\nMiddleware for [Koa2](https://github.com/koajs/koa) to get/set session use with custom stores such as Redis or mongodb\n\nUse native ES6(async/await) by Nodejs v7.6.0 +\n\nOr you can use the old versions:\n- [babel](https://github.com/Secbone/koa-session2/tree/babel)\n- [node6](https://github.com/Secbone/koa-session2/tree/node6)\n\n## Require\nnode v7.6 +\n\n## Install\n```\nnpm install koa-session2\n```\n\n## Usage\n```js\nconst Koa = require(\"koa\");\nconst session = require(\"koa-session2\");\n\nconst app = new Koa();\n\napp.use(session({\n    key: \"SESSIONID\",   //default \"koa:sess\"\n}));\n```\n\n### Custom Stores\n\nStore.js\n```js\nconst Redis = require(\"ioredis\");\nconst { Store } = require(\"koa-session2\");\n\nclass RedisStore extends Store {\n    constructor() {\n        super();\n        this.redis = new Redis();\n    }\n\n    async get(sid, ctx) {\n        let data = await this.redis.get(`SESSION:${sid}`);\n        return JSON.parse(data);\n    }\n\n    async set(session, { sid =  this.getID(24), maxAge = 1000000 } = {}, ctx) {\n        try {\n            // Use redis set EX to automatically drop expired sessions\n            await this.redis.set(`SESSION:${sid}`, JSON.stringify(session), 'EX', maxAge / 1000);\n        } catch (e) {}\n        return sid;\n    }\n\n    async destroy(sid, ctx) {\n        return await this.redis.del(`SESSION:${sid}`);\n    }\n}\n\nmodule.exports = RedisStore;\n```\nmain.js\n```js\nconst Koa = require(\"koa\");\nconst session = require(\"koa-session2\");\nconst Store = require(\"./Store.js\");\n\nconst app = new Koa();\n\napp.use(session({\n    store: new Store()\n}));\n\napp.use(ctx =\u003e {\n    let user = ctx.session.user;\n\n    ctx.session.view = \"index\";\n});\n\napp.use(ctx =\u003e {\n    // refresh session if set maxAge\n    ctx.session.refresh()\n})\n```\n\n## Options\n\nMost options based on [cookies](https://github.com/pillarjs/cookies#cookiesset-name--value---options--)\n\n- `key`: a string for store session id in cookie\n- `store`: a class for custom store (extend {Store}, func: #get(sid), #set(session, opts), #destory(sid))\n- `maxAge`: a number representing the milliseconds from `Date.now()` for expiry\n- `expires`: a `Date` object indicating the cookie's expiration date (expires at the end of session by default).\n- `path`: a string indicating the path of the cookie (`/` by default).\n- `domain`: a string indicating the domain of the cookie (no default).\n- `secure`: a boolean indicating whether the cookie is only to be sent over HTTPS (`false` by default for HTTP, `true` by default for HTTPS).\n- `httpOnly`: a boolean indicating whether the cookie is only to be sent over HTTP(S), and not made available to client JavaScript (`true` by default).\n- `sameSite`: a boolean or string indicating whether the cookie is a \"same site\" cookie (`false` by default). This can be set to `'strict'`, `'lax'`, or `true` (which maps to `'strict'`).\n- `signed`: a boolean indicating whether the cookie is to be signed (`false` by default). If this is true, another cookie of the same name with the `.sig` suffix appended will also be sent, with a 27-byte url-safe base64 SHA1 value representing the hash of _cookie-name_=_cookie-value_ against the first [Keygrip](https://www.npmjs.com/package/keygrip) key. This signature key is used to detect tampering the next time a cookie is received.\n- `overwrite`: a boolean indicating whether to overwrite previously set cookies of the same name (`false` by default). If this is true, all cookies set during the same request with the same name (regardless of path or domain) are filtered out of the Set-Cookie header when setting this cookie.\n\n\n## Methods\n\n- `refresh()`: if you set `maxAge` in options, you can call `ctx.session.refresh()` to refresh session to your store\n\n## License\n\nMIT\n\n\n[npm-image]: https://img.shields.io/npm/v/koa-session2.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-session2\n[downloads-image]: http://img.shields.io/npm/dm/koa-session2.svg?style=flat-square\n[downloads-url]: https://npmjs.org/package/koa-session2\n[travis-image]: https://img.shields.io/travis/Secbone/koa-session2/master.svg?style=flat-square\n[travis-url]: https://travis-ci.org/Secbone/koa-session2\n[codecov-image]: https://img.shields.io/codecov/c/github/Secbone/koa-session2/master.svg?style=flat-square\n[codecov-url]: https://codecov.io/gh/Secbone/koa-session2/branch/master\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSecbone%2Fkoa-session2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSecbone%2Fkoa-session2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSecbone%2Fkoa-session2/lists"}