JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q35631F
  • License MIT

A minimalist session-manager for NodeJS

Package Exports

  • c2b-session

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (c2b-session) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

c2b-session

A minimalist session-manager for NodeJS.

Unit-Tests are included.


API

Methods (fn) of the module:

* setTimeout(time <minutes>, callback <function>(ident, time)) Fn
* create(config <object>, callback <function>(err, session)) Fn
* retrive(ident <string>, callback <function>(err, session)) Fn
* exists(ident <string>) Boolean
* is_connected(ident <string>) Boolean
* getAll() Object
* getOnline() Array
* destroy() Void

Methods (fn) of a session-object:

* connect(callback <function>(err)) Fn
* disconnect(callback <function>(err)) Fn
* online() Boolean
* put(callback <function>(err)) Fn
* get([key <string> (optional)], callback <function>(err, result)) Fn

Default template of session-object:

{
    t_connect: Date.now(),      //timestamp of a sessions initial connection
    t_last_action: Date.now(),  //timestamp of a sessions last action, eg. put() [required]
    connected: false,           //state of a sessions connection [required]
    ident: '',                  //ident of a session [required]
    data: {}                    //storage of session data [required]
}

Setup

var sessionManager = require('c2b-session');

Set a timeout for sessions (optional)

//Optionally set a timeout (min) for sessions...
sessionManager.setTimeout(1);

//as of version 0.0.3 you can pass a function too
sessionManager.setTimeout(1, function(ident, time){
    console.log(`[SESSION] ID ${ident} timed-out (${time})`);
});

Create a session

sessionManager.create({
    ident: "my-session-name"

}, function(err, session){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully created!");
    console.log(session);
});

//You may also pass some default data to your session

sessionManager.create({
    ident: "my-session-name",
    data: {
        A: "Data 1",
        B: "Data 2",
        C: "Data 3"
    }

}, function(err, session){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully created!");
    console.log(session);
});

Retrieve a session

sessionManager.retrive("my-session-name", function(err, session){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully retrived!");
    console.log(session);
});

Test whether session exist

if(sessionManager.exists("my-session-name")){
    console.log("[SESSION] Exist!");
}else{
    console.log("[SESSION] Invalid session!");
}

Test whether session is connected

if(sessionManager.is_connected("my-session-name")){
    console.log("[SESSION] Online!");
}else{
    console.log("[SESSION] Offline!");
}

Get all sessions

sessionManager.getAll();

//or
sessionManager.sessions;

Get online sessions

sessionManager.getOnline();

Destroy a session

sessionManager.destroy("my-session-name");

Session Handling

A session-object is returned by the modules create and retrive methods.

Connect a session

session.connect(function(err){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully connected!");
});

Disonnect a session

session.disconnect(function(err){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully disconnected!");
});

Test status

if(session.online()){
    console.log("[SESSION] Online!");
}else{
    console.log("[SESSION] Offline!");
}

Store session-data

session.put({ 
    D: "Data 4",
    E: "Data 5",
    F: "Data 6"

}, function(err){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully stored the data!");
});

Query session-data

//Query by key
session.get("E", function(err, result){
    if(err){ return console.log(err); };
    
    console.log("[SESSION] Successfully fetched the data!");
    console.log(result);
});

//Query all
session.get(function(err, result){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully fetched the data!");
    console.log(result);
});

Example 1

Create a session if it does not exist, otherwise retrive the exisiting session:

if(!sessionHandler.exists("my-session-name")){

    sessionHandler.create({
        ident: "my-session-name"

    }, function(err, session){
        if(err){ return console.log(err); };
        console.log("[SESSION] Successfully created!");
        console.log(session);

        //You may now connect via session.connect(...)
    });

}else{

    sessionHandler.retrive("my-session-name", function(err, session){
        if(err){ return console.log(err); };
        console.log("[SESSION] Successfully retrived!");
        console.log(session);

        //You may now connect via session.connect(...)
    });
}

This example is the recommended approach.

sessionHandler.createOrRetrive({
    ident: "my-session-name"

}, function(err, session, state){
    if(err){ return console.log(err); };

    if(state == 1){ console.log("Successfully created!"); }
    if(state == 2){ console.log("Successfully retrived!"); }

    if(!session.online()){
        session.connect(function(err){
            if(err){ return console.log(err); };
            console.log("Successfully connected!");
        });
    }

});

Unit-Test

Two test-cases are available:

  • test/test.basic.js
  • test/test.extended.js

Simply run those tests as you please:

Make

make test

make test-ext

NPM

npm test

npm test-ext


##How to install: Use npm install c2b-session