npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa2-session-ioredis

v1.2.1

Published

koa2 session with ioredis using koa-session

Downloads

38

Readme

koa2-session-ioredis

NPM version build status Coveralls David deps David devDeps node version npm download license

Redis storage for koa session middleware/cache using ioredis.

NPM

Installation

npm i koa2-session-ioredis ioredis --save

Usage

koa2-session-ioredis works with koa-session (a session middleware for koa v2).

Example

const session = require('koa-session');
const RedisStore = require('koa2-session-ioredis');
const Koa = require('koa');

const app = new Koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
  key: 'koa:sess', /** (string) cookie key (default is koa:sess) */
  /** (number || 'session') maxAge in ms (default is 1 days) */
  /** 'session' will result in a cookie that expires when session/browser is closed */
  /** Warning: If a session cookie is stolen, this cookie will never expire */
  maxAge: 86400000,
  overwrite: true, /** (boolean) can overwrite or not (default true) */
  httpOnly: true, /** (boolean) httpOnly or not (default true) */
  signed: true, /** (boolean) signed or not (default true) */
  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) */
  renew: false, /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/
  store: new RedisStore({
    // Options specified here
    // all `ioredis` options
  })
}, app));

app.use(async ctx => {
  switch (ctx.path) {
  case '/get':
    get.call(this);
    break;
  case '/remove':
    remove.call(this);
    break;
  case '/regenerate':
    await regenerate.call(this);
    break;
  }
});

function get() {
  var session = this.session;
  session.count = session.count || 0;
  session.count++;
  this.body = session.count;
}

function remove() {
  this.session = null;
  this.body = 0;
}

async function regenerate() {
  get.call(this);
  await this.regenerateSession();
  get.call(this);
}

app.listen(8080);

For more examples, please see the examples folder of koa-session.

Options

  • all ioredis options
  • Useful things include host, port, and path to the server. Defaults to 127.0.0.1:6379
  • client (object) - supply your own client, all other options are ignored unless duplicate is also supplied
  • duplicate (boolean) - When true, it will run client.duplicate(options) on the supplied client and use all other options supplied. This is useful if you want to select a different DB for sessions but also want to base from the same client object.

Events

See the ioredis docs for more info.

  • ready
  • connect
  • reconnecting
  • error
  • end
  • close
  • idle

API

These are some the funcitons that koa-session uses that you can use manually. You will need to inintialize differently than the example above:

const session = require('koa-session');
const redisStore = require('koa2-session-ioredis')({
  // Options specified here
});
const app = require('koa')();

app.keys = ['keys', 'keykeys'];
app.use(session({
  //... other options
  store: new redisStore()
}, app));

module([options])

Initialize the Redis connection with the optionally provided options (see above). The variable session below references this.

session.get(sid)

Generator that gets a session by ID. Returns parsed JSON is exists, null if it does not exist, and nothing upon error.

session.set(sid, sess, ttl)

Generator that sets a JSON session by ID with an optional time-to-live (ttl) in milliseconds. Yields ioredis's client.set() or client.setex().

session.destroy(sid)

Generator that destroys a session (removes it from Redis) by ID. Yields ioredis's client.del().

session.quit()

Generator that stops a Redis session after everything in the queue has completed. Yields ioredis's client.quit().

session.end()

Alias to session.quit(). It is not safe to use the real end function, as it cuts of the queue.

session.status

String giving the connection status updated using client.status after any of the events above is fired.

  • connecting
  • connect
  • ready
  • reconnecting
  • end
  • monitoring

session.client

Direct access to the ioredis client.

Benchmark

|Server|Transaction rate|Response time| |------|----------------|-------------| |connect without session|6763.56 trans/sec|0.01 secs| |koa without session|5684.75 trans/sec|0.01 secs| |connect with session|2759.70 trans/sec|0.02 secs| |koa with session|2355.38 trans/sec|0.02 secs|

Detailed benchmark report here

Testing

  1. Start a Redis server on localhost:6379. You can use redis-windows if you are on Windows or just want a quick VM-based server.
  2. Clone the repository and run npm i in it (Windows should work fine).
  3. If you want to see debug output, turn on the prompt's DEBUG flag.
  4. Run npm test to run the tests and generate coverage. To run the tests without generating coverage, run npm run-script test-only.

Authors

See the contributing tab

Licences

(The MIT License)

Copyright (c) 2015 dead-horse and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.