{"id":13716660,"url":"https://github.com/billymoon/micro-cookie-session","last_synced_at":"2025-10-24T22:58:59.455Z","repository":{"id":54910804,"uuid":"88427277","full_name":"billymoon/micro-cookie-session","owner":"billymoon","description":"Simple cookie-based session storage for micro.","archived":false,"fork":false,"pushed_at":"2019-03-09T01:15:46.000Z","size":7,"stargazers_count":16,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-07T06:39:40.507Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/billymoon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-16T16:21:30.000Z","updated_at":"2021-09-24T10:02:44.000Z","dependencies_parsed_at":"2022-08-14T06:20:53.056Z","dependency_job_id":null,"html_url":"https://github.com/billymoon/micro-cookie-session","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/billymoon/micro-cookie-session","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/billymoon%2Fmicro-cookie-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/billymoon%2Fmicro-cookie-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/billymoon%2Fmicro-cookie-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/billymoon%2Fmicro-cookie-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/billymoon","download_url":"https://codeload.github.com/billymoon/micro-cookie-session/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/billymoon%2Fmicro-cookie-session/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280878221,"owners_count":26406644,"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","status":"online","status_checked_at":"2025-10-24T02:00:06.418Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-03T00:01:13.088Z","updated_at":"2025-10-24T22:58:59.441Z","avatar_url":"https://github.com/billymoon.png","language":"JavaScript","funding_links":[],"categories":["Modules"],"sub_categories":["Utilities"],"readme":"# micro-cookie-session\n\n[![NPM Version][npm-image]][npm-url]\n[![NPM Downloads][downloads-image]][downloads-url]\n\nSimple cookie-based session storage for [micro](https://github.com/zeit/micro).\n\nActually, a tiny a wrapper for the excellent [cookie-session](https://github.com/expressjs/cookie-session) express middleware.\n\n## Install\n\nThis is a [Node.js](https://nodejs.org/en/) module available through the\n[npm registry](https://www.npmjs.com/). Installation is done using the\n[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):\n\n```sh\n$ npm install micro-cookie-session\n```\n\n## API\n\n```js\n// import micro\nconst micro = require('micro')\n\n// initiallise session\nconst session = require('micro-cookie-session')({\n  name: 'session',\n  keys: ['someverystringsecretstring'],\n  maxAge: 24 * 60 * 60 * 1000\n})\n\n// set up micro server\nconst server = micro((req, res) =\u003e {\n  // enable session storage in cookie\n  session(req, res)\n\n  // store start time and current time in session\n  req.session.demoFirstHit = req.session.demoFirstHit || new Date() * 1\n  req.session.demoLastHit = new Date() * 1\n\n  // display milliseconds since session started\n  return `Session started ${req.session.demoLastHit - req.session.demoFirstHit}ms ago`\n})\n\n// fire it up\nserver.listen(3000)\n```\n\n### session(options)\n\nCreate a new cookie session instance with the provided options. This function\nwill attach the property `session` to `req`, which provides an object representing\nthe loaded session. This session is either a new session if no valid session was\nprovided in the request, or a loaded session from the request.\n\nThe function will automatically add a `Set-Cookie` header to the response if the\ncontents of `req.session` were altered. _Note_ that no `Set-Cookie` header will be\nin the response (and thus no session created for a specific user) unless there are\ncontents in the session, so be sure to add something to `req.session` as soon as\nyou have identifying information to store for the session.\n\n#### Options\n\nMicro cookie session accepts these properties in the options object (identical to\nthe cookie-session express middleware).\n\n##### name\n\nThe name of the cookie to set, defaults to `session`.\n\n##### keys\n\nThe list of keys to use to sign \u0026 verify cookie values. Set cookies are always\nsigned with `keys[0]`, while the other keys are valid for verification, allowing\nfor key rotation.\n\n##### secret\n\nA string which will be used as single key if `keys` is not provided.\n\n##### Cookie Options\n\nOther options are passed to `cookies.get()` and `cookies.set()` allowing you\nto control security, domain, path, and signing among other settings.\n\nThe options can also contain any of the following (for the full list, see\n[cookies module documentation](https://www.npmjs.org/package/cookies#readme):\n\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  - `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  - `secure`: a boolean indicating whether the cookie is only to be sent over HTTPS (`false` by default for HTTP, `true` by default for HTTPS). If this is set to `true` and Node.js is not directly over a TLS connection, be sure to read how to [setup Express behind proxies](https://expressjs.com/en/guide/behind-proxies.html) or the cookie may not ever set correctly.\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  - `signed`: a boolean indicating whether the cookie is to be signed (`true` 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://github.com/expressjs/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 (`true` 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### req.session\n\nRepresents the session for the given request.\n\n#### .isChanged\n\nIs `true` if the session has been changed during the request.\n\n#### .isNew\n\nIs `true` if the session is new.\n\n#### .isPopulated\n\nDetermine if the session has been populated with data or is empty.\n\n### req.sessionOptions\n\nRepresents the session options for the current request. These options are a\nshallow clone of what was provided at initialisation and can be\naltered to change cookie setting behavior on a per-request basis.\n\n### Destroying a session\n\nTo destroy a session simply set it to `null`:\n\n```\nreq.session = null\n```\n\n## Examples\n\nFurther examples can be seen in [https://github.com/expressjs/cookie-session](cookie-session)\nexpress middleware but have not been tested (please report any micro related issues).\n\n## Usage Limitations\n\n### Max Cookie Size\n\nBecause the entire session object is encoded and stored in a cookie, it is\npossible to exceed the maxium cookie size limits on different browsers. The\n[RFC6265 specification](https://tools.ietf.org/html/rfc6265#section-6.1)\nrecommends that a browser **SHOULD** allow\n\n\u003e At least 4096 bytes per cookie (as measured by the sum of the length of\n\u003e the cookie's name, value, and attributes)\n\nIn practice this limit differs slightly across browsers. See a list of\n[browser limits here](http://browsercookielimits.squawky.net/). As a rule\nof thumb **don't exceed 4093 bytes per domain**.\n\nIf your session object is large enough to exceed a browser limit when encoded,\nin most cases the browser will refuse to store the cookie. This will cause the\nfollowing requests from the browser to either a) not have any session\ninformation or b) use old session information that was small enough to not\nexceed the cookie limit.\n\nIf you find your session object is hitting these limits, it is best to\nconsider if  data in your session should be loaded from a database on the\nserver instead of transmitted to/from the browser with every request. Or\nmove to an alternative session strategy.\n\n## License\n\n[MIT](LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/micro-cookie-session.svg\n[npm-url]: https://npmjs.org/package/micro-cookie-session\n[downloads-image]: https://img.shields.io/npm/dm/micro-cookie-session.svg\n[downloads-url]: https://npmjs.org/package/micro-cookie-session\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbillymoon%2Fmicro-cookie-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbillymoon%2Fmicro-cookie-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbillymoon%2Fmicro-cookie-session/lists"}