{"id":13686681,"url":"https://github.com/fibjs/fib-session","last_synced_at":"2025-06-12T12:13:32.465Z","repository":{"id":57235192,"uuid":"90235690","full_name":"fibjs/fib-session","owner":"fibjs","description":"Session middleware for fibjs","archived":false,"fork":false,"pushed_at":"2025-04-18T19:51:46.000Z","size":100,"stargazers_count":2,"open_issues_count":0,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-19T18:46:47.500Z","etag":null,"topics":[],"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/fibjs.png","metadata":{"files":{"readme":"Readme.md","changelog":"CHANGELOG.md","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,"zenodo":null}},"created_at":"2017-05-04T07:45:44.000Z","updated_at":"2025-04-18T19:51:50.000Z","dependencies_parsed_at":"2025-05-01T09:32:50.685Z","dependency_job_id":"3c30c35d-1532-428c-a2dc-860b389c2653","html_url":"https://github.com/fibjs/fib-session","commit_stats":{"total_commits":57,"total_committers":9,"mean_commits":6.333333333333333,"dds":0.543859649122807,"last_synced_commit":"ede4e2979efde1529a5946ab3d7e09499d046a71"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/fibjs/fib-session","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fibjs","download_url":"https://codeload.github.com/fibjs/fib-session/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-session/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259462578,"owners_count":22861514,"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-08-02T15:00:37.804Z","updated_at":"2025-06-12T12:13:32.438Z","avatar_url":"https://github.com/fibjs.png","language":"JavaScript","readme":"# fib-session\n\n[![NPM version](https://img.shields.io/npm/v/fib-session.svg)](https://www.npmjs.org/package/fib-session)\n[![Build Status](https://travis-ci.org/fibjs/fib-session.svg)](https://travis-ci.org/fibjs/fib-session)\n[![Build status](https://ci.appveyor.com/api/projects/status/ns3jmuw23pwcenhu?svg=true)](https://ci.appveyor.com/project/richardo2016/fib-session)\n\nSession middleware for fibjs\n\n## Install\n\n```sh\nnpm install fib-session [--save]\n```\n\n## Test\n\n```sh\nnpm run ci\n```\n\n## Creating a cookie-based session middleware\n\n```js\nvar Session = require('fib-session')\nvar session = new Session(conn, opts);\n\nvar srv = new http.Server(8000, [\n    session.cookie_filter, // use session ID via cookie\n    {\n        // routers\n        '^/foo$': (r) =\u003e {\n            var v = r.session.v;\n        },\n        ...\n    }\n]);\n```\n\n## Creating a api session middleware\n\n```js\nvar Session = require('fib-session')\nvar session = new Session(conn, opts);\n\nvar srv = new http.Server(8000, [\n    session.api_filter, // use api session filter\n    {\n        // routers\n        '^/foo$': (r) =\u003e {\n            var v = r.session.v;\n        },\n        '^/get-token$': session.api_token\n    }\n]);\n```\n\n*Both kv options and sesion options are in the same object.*\n\nkv-store options\n\n| options              | default | object/Map | LruCache | LevelDB | Redis | MongoDB | SQLite/MySQL |\n|----------------------|---------|------------|----------|---------|-------|---------|--------------|\n| table_name           |   \"kvs\" | x          | x        | x       | √     | √       | √            |\n| key_name             |     \"k\" | x          | x        | x       | x     | √       | √            |\n| value_name           |     \"v\" | x          | x        | x       | x     | √       | √            |\n| key_size             |      32 | x          | x        | x       | x     | x       | √            |\n| value_size           |     256 | x          | x        | x       | x     | x       | √            |\n| cleanup_interval(ms) |   60000 | x          | x        | x       | x     | x       | √            |\n| timeout(ms)          |       0 | x          | √        | x       | √     | √       | √            |\n| prefix               |      \"\" | √          | √        | √       | √     | √       | √            |\n| cache                |   false | √          | √        | √       | √     | √       | √            |\n| cache_size           |   65536 | √          | √        | √       | √     | √       | √            |\n| cache_timeout(ms)    |   60000 | √          | √        | √       | √     | √       | √            |\n\nsession options\n\n| options                   | default            |                                                                                                  |\n|---------------------------|--------------------|--------------------------------------------------------------------------------------------------|\n| session_cache_size        |              65536 | max number of session in cache                                                                   |\n| session_cache_timeout(ms) |             900000 | clear session objects which is not operated for a period of time from buffer, default 15 minutes |\n| session_cache_delay       |                100 | time delay for write session to persistent storage                                               |\n| session_id_name           |        \"sessionID\" |                                                                                                  |\n\n- session cache is used to keep session consistency among http requires.\n- the timeout of session cache must be larger than its delay\n\n- The client-side has only the session ID. The session is operated on the server-side.\n\n## Methods\n\n### session.setup()\nsetup the backend database.\n\n### v = session.cookie_filter\nreturns a cookie-based session filter.\n\n### session.api_filter\nreturns a header-based session filter.\n\n### session.api_token\nreturns an api handler that gets a new session ID.\n\n\nJSON Web Token(JWT) \n---\noptions\n\n| options                   | default |                                           |\n|---------------------------|---------|-------------------------------------------|\n| session_jwt_algo          |   null  | see jws.ALGORITHMS in fib-jws             |\n| session_jwt_key           |   null  | sign key. see in fib-jws                  |\n\n- see fib-jws https://github.com/fibjs/fib-jws\n- set session_jwt_algo and session_jwt_key to enable JWT\n```javascript\n// session_jwt_algo is encryption algorithms in fib-jws\n// session_jwt_key is to verify the signature. \nvar session = new Session(conn, { session_jwt_algo: 'HS256',  session_jwt_key: verify_key })\n```\n## Methods\n### session.setTokenCookie \n- set the JSON Web Token in cookie \n- sign_key for signature, may be different from session_jwt_key, depending on the algorithm.\n- according to JSON Web Token specification, one user session can only be called setTokenCookie once for set user session info. The second will throw an exception: new Error('Can't modify the JSON Web Token')\n- default value r.session={}\n```javascript\n// sign user info: { id: 12345, name: \"Frank\" }, and set the cookie\n// r is request\nsession.setTokenCookie(r, { id: 12345, name: \"Frank\" }, sign_key)\n```\n### session.getToken \n- get token for api filter mode, see session.api_filter\n```javascript\nsession.getToken ({ id: 12345, name: \"Frank\" }, sign_key)\n```\n","funding_links":[],"categories":["Packages"],"sub_categories":["Server App"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibjs%2Ffib-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffibjs%2Ffib-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibjs%2Ffib-session/lists"}