koa-observable-redis-session

1.0.0 • Public • Published

observable-redis-session

NPM version Build status Test coverage Dependency Status License Downloads Gittip

A session module that uses Object.observe() to set values to Redis atomically and asynchronously.

Usage:

const app = require('koa')()
require('session')(app, {
  uri: 'tcp://localhost:6379'
})
 
app.use(function* (next) {
  try {
    yield* next
  } catch (err) {
    throw err
  } finally {
    // always give time for `Object.observe()` to do its thing
    yield setImmediate
  }
})
 
app.use(function* (next) {
  let session = yield this.session()
 
  session.userid = '1234'
 
  yield* next
})

API

session(app, options)

Adds this.session() to the app.

Options:

  • .uri - Redis URI
  • .client - Redis client, if not set by .uri
  • .length - session id key byte length, default 10
  • .prefix - Redis key prefix, default hash:koa-session:
  • .maxAge - max session age, default 30 days

let session = yield this.session()

Grab the session asynchronously.

session.touch()

Update the session without changing the values on the server.

session.destroy()

Destroy the session.

let session = session.regenerate()

Destroy the old session and return a new one.

session.maxAge=

Set the max age for this session, defaulting to options.maxAge.

Caveats

  • You must always yield setImmediate upstream to allow Object.observe() time to execute its callback.

Readme

Keywords

none

Package Sidebar

Install

npm i koa-observable-redis-session

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • coderhaoxin
  • jongleberry