Session

Introduced in 4.0.0

Stability: 2 - Stable

const Session = require('kado/lib/Session')

The Session library provides both the means for creating middleware to load save and prune sessions. There is also a SessionStoreMemory for storing sessions locally to memory within a single process. As well as a full suite of SessionStoreLocal and SessionStorage for implementing a client to server relationship for saving and restoring sessions.

Class: Session

static Session.getMiddleware(options)

Available Settings:

static Session.generateId(ip, userAgent, secret)

Internal Use

static Session.request(req, res, options)

Internal Use

Session.constructor(sid, ip, agent, uid, store)

Session.restore()

Session.set(key, value, options = {})

From the changelog:

Session.set() now saves automatically and returns a promise that should be awaited. IN the event multiple session sets are going to happen in the same method it is advised to use { save: false } as a third option to Session.set() then call Session.save() and await the promise at the end. This is meant to make sure that session saving is not implied to be automatic. It should be explicit and handled like any other database transaction.

Example

class MyRoutes extends Module {
  async myRoute (req, res) {
    const someData = { foo: 1 }
    const moreData = { bar: 1 }
    const saveOpts = { save: false }
    req.session.set('key1', someData.foo, saveOpts)
    req.session.set('key2', moreData.bar, saveOpts)
    await req.session.save()
    res.end('done')
  }

  async myRoute2 (req, res) {
    // this method uses an extra db call
    const someData = { foo: 1 }
    const moreData = { bar: 1 }
    await req.session.set('key1', someData.foo)
    await req.session.set('key2', moreData.bar)
    res.end('done')
  }
}

Session.get(key)

Session.save()

Class: SessionStorage

This class provides a server that can be used to store sessions made by the SessionStoreLocal instance.

Example Usage

const fs = require('kado/lib/FileSystem')
const Session = require('kado/lib/Session')
const saveFile = fs.path.join(__dirname, '.session.json')
const storage = new Session.SessionStorage({ saveFile: saveFile })
storage.restoreFromDisk()
storage.listen()

static SessionStorage.create(options)

SessionStorage.constructor(options)

Available Settings:

SessionStorage.handleSocket(socket)

Internal Use

SessionStorage.save(sid, ip, agent, uid, data)

Internal Use

SessionStorage.restore(sid)

Internal Use

SessionStorage.saveToDisk()

Internal Use

SessionStorage.restoreFromDisk()

Loads the session file synchronously and sets the session data from it.

Internal Use

SessionStorage.clearInterval()

Stop the storage from saving to disk. Cannot be restarted in this instance.

SessionStorage.listen(port, host)

Class: SessionStore

SessionStore.constructor()

Available Settings:

SessionStore.save(sid, ip, agent, uid, data)

This method must be extended and will error otherwise.

SessionStore.restore(sid)

This method must be extended and will error otherwise

SessionStore.prune()

This method must be extended and will error otherwise. Also, it the storage mechanism must decide on a method for pruning.

Class: SessionStoreLocal

SessionStoreLocal.constructor(options)

Available Settings:

SessionStoreLocal.save(sid, ip, agent, uid, data)

SessionStoreLocal.restore(sid)

SessionStoreLocal.prune()

This method uses the duration and timestamps on the session data to prune the records.

Class: SessionStoreMemory

SessionStoreMemory.constructor(options)

Available Settings:

SessionStoreMemory.save(sid, ip, agent, uid, data)

SessionStoreMemory.restore(sid)

SessionStoreMemory.prune()

This method uses the duration and timestamps on the session data to prune the records.

Class: SessionStoreModel

static SessionStoreModel.fieldList()

static SessionStoreModel.createTable()

static SessionStoreModel.insert(fields)

static SessionStoreModel.update(fields)

static SessionStoreModel.byId(id)

static SessionStoreModel.bySid(sid)

static SessionStoreModel.pruneSession(boundary)

Class: SessionStoreSQL

SessionStoreSQL.constructor(options)

Available Settings:

SessionStoreSQL.save(sid, ip, agent, uid, data)

SessionStoreSQL.restore(sid)

SessionStoreSQL.prune()

This method uses the duration and timestamps on the session data to prune the records.