{"id":18684941,"url":"https://github.com/tknf/node-session","last_synced_at":"2025-11-07T22:30:22.463Z","repository":{"id":57700496,"uuid":"496945958","full_name":"tknf/node-session","owner":"tknf","description":"Session library for NodeJS, extracted from Remix.","archived":false,"fork":false,"pushed_at":"2022-05-27T10:20:48.000Z","size":39,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-01-28T13:17:09.792Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/tknf.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}},"created_at":"2022-05-27T09:58:26.000Z","updated_at":"2022-05-27T10:11:14.000Z","dependencies_parsed_at":"2022-09-26T21:11:40.103Z","dependency_job_id":null,"html_url":"https://github.com/tknf/node-session","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tknf%2Fnode-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tknf%2Fnode-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tknf%2Fnode-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tknf%2Fnode-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tknf","download_url":"https://codeload.github.com/tknf/node-session/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239540375,"owners_count":19655990,"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":[],"created_at":"2024-11-07T10:19:49.576Z","updated_at":"2025-11-07T22:30:22.396Z","avatar_url":"https://github.com/tknf.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @tknf/node-session\n\n## Get Started\n### Install\n```bash\nyarn add @tknf/node-session\n# or\nnpm install --save @tknf/node-session\n```\n\n### Use on express\n```js\nimport express from \"express\";\nimport { } from \"@tknf/node-session\";\n\nconst MySession = new CookieSessionStorage({\n  name: \"MyCookieSession\"\n});\n\nconst app = express();\n\napp.get(\"/\", async (req, res) =\u003e {\n  const session = await MySession.getSession(req.header(\"Cookie\"));\n  session.set(\"view\", 1);\n  return res.set(\"Set-Cookie\", await MySession.commitSession(session))\n});\n```\n\n### Create your session strategy\n```js\nimport { Redis } from \"ioredis\";\nimport { SessionStorageFactory, isCookie, Cookie } from \"@tknf/node-session\";\nimport crypto from \"crypto\";\n\nconst expiresToSeconds = (expires) =\u003e {\n  const now = new Date();\n  const expiresDate = new Date(expires || Date.now());\n  const secondsDelta = expiresDate.getSeconds() - now.getSeconds();\n  return secondsDelta \u003c 0 ? 0 : secondsDelta;\n};\n\nclass RedisSessionFactory extends SessionStorageFactory {\n  constructor(private redis) {\n    super();\n  }\n\n  public async createData(data: SessionData, expires) {\n    const id = crypto.randomBytes(20).toString(\"base64\");\n    await this.redis.pipeline().set(id, JSON.stringify(data)).expire(id, expiresToSeconds(expires)).exec();\n    return id;\n  }\n\n  public async readData(id: string) {\n    try {\n      const data = await this.redis.get(id);\n      return data ? JSON.parse(data) : null;\n    } catch (err) {\n      return null;\n    }\n  }\n\n  public async updateData(id: string, data: SessionData, expires) {\n    await this.redis.pipeline().set(id, JSON.stringify(data)).expire(id, expiresToSeconds(expires)).exec();\n  }\n\n  public async deleteData(id: string) {\n    await this.redis.pipeline().del(id).exec();\n  }\n}\n\nexport class RedisSessionStorage extends SessionStorage {\n  private factory: SessionStorageFactory;\n\n  constructor(redis: Redis, cookie) {\n    super(cookie);\n    this.cookie = isCookie(cookie) ? cookie : new Cookie(cookie.name, cookie);\n    this.factory = new RedisSessionFactory(redis)\n  }\n\n  public async getSession(cookieHeader?: string | null, options?: CookieParseOptions) {\n    const id = cookieHeader \u0026\u0026 (await this.cookie.parse(cookieHeader, options));\n    const data = id \u0026\u0026 (await this.factory.readData(id));\n    return new Session(data || {}, id || \"\");\n  }\n\n  public async commitSession(session: Session, options?: CookieSerializeOptions) {\n    let { id, data } = session;\n    if (id) {\n      await this.factory.updateData(id, data, this.cookie.expires);\n    } else {\n      id = await this.factory.createData(data, this.cookie.expires);\n    }\n    return this.cookie.serialize(id, options);\n  }\n\n  public async destroySession(session: Session, options?: CookieSerializeOptions) {\n    await this.factory.deleteData(session.id);\n    return this.cookie.serialize(\"\", {\n      ...options,\n      expires: new Date(0)\n    });\n  }\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftknf%2Fnode-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftknf%2Fnode-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftknf%2Fnode-session/lists"}