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

satr

v1.0.0

Published

A session management plugin for Muneem web framework. It also covers authentication flow.

Downloads

3

Readme

सत्र (Satr)

A session management plugin for Muneem web framework.

It generates a session id, creates a cookie for session id, and entry in session store where more data related to the session can be stored.


process.env.NODE_ENV = "dev";

const Muneem = require("muneem");
const satr = new require("satr")();
const store = require("some_store");

satr.on('create', (session, asked) => {
    console.log("Creating a new session");
    console.log( store.get(session.id) );
})
satr.on('renew', (session, asked) => {
    console.log("Renewing an old session");
    console.log( store.get(session.id) );
})
satr.on('invalid', (sessionId, asked) => {
    console.log("Received an invalid session");
})
satr.on('save', (sessionId, asked) => {
    console.log("saved session into cookies and store");
    console.log( store.get(sessionId) );
})

const app = Muneem();
app.use(satr.manager, {
    name : "session-id", //default is '_si' 
    autoCreate : false,
    secret : "keep it secret and complex that no one can guess",// min 32 char or long
    life : 600,//default is 3600
    renewalDuration : 1,//duration before session end when session-id can be renewed
    store : store,
    cookie : {
        httpOnly : true,
        secure : false
    }
});

/******************** Handlers ********************/
function authentication(asked, answer, giveMe){
    const session = asked.getSession(); //current session if exist otherwise new unauthorized session
    if( session.authorized ){
        console.log("It is authorized session");
    }else{
        asked.hasSession = false; //it'll stop saving the temporary session to the store and cookies
        answer.redirectTo("/login?sendTo=" + asked.path);
    }
}

function loginHandler(asked, answer, giveMe){
    //.. validate user
    if( authInfo.isValid() ){
        var session = asked.getSession();
        session.authorized = true;
        //.. save session data to session.data
        /* session.data.userid  = "authInfo.id";
        session.add( "userid", "authInfo.id2" ); 
        session.delete( "tempid");*/

        answer.redirectTo( asked.q.sendTo ); //you'll have to parse query string yourself or use some plugin
    }else{
        answer.write('{ result : "fail"}', "application/json"); 
    }
}

function logoutHandler(asked, answer, giveMe){
    var session = asked.getSession();
    session.authorized = false;
    //.. delete from the store
    answer.redirectTo( asked.q.sendTo || "/" );
}

function privatePageProvider(asked, answer, giveMe){
    console.log("private page")
}

function publicPageProvider(asked, answer, giveMe){
    console.log("private page")
}

/******************** End Handlers ********************/

app.route([{
    uri : "/logout",
    to : logoutHandler,
},{
    uri : "/login",
    to : loginHandler,
},{
    uri : "/public/url",
    to : publicPageProvider,
},{
    uri : "/private/url",
    to : privatePageProvider,
    after: authentication
}]);

app.start();

Options

name : This plugin reads the session-id from cookie first, if not presents then reads from header.

A cookie is baked not only with it's name and value but other attributes like domain, expiry time/duration etc. Check cookie options for more detail.

life of the session (in seconds) will be set to cookie.expires .

If autoCreate option is set to true, this plugin will create session for each request if it was not previously created. Since all the sessions get saved to store at the time of sending the response, autoCreate:true may create an extra overhead on the session store. You may set it false to create sessions manually for login sessions only. If there is no session created, it'll not send cookies to the client. There are some cookie laws which don't want you to create cookies without user permission or create cookies relevant to your site only not luring ads.

store is optional for dev, ref, int, uat, test, qa environments. It'll log a warning. If the store is not provided then the sessions will be saved in memory which is not suitable for production and performance encvironment. A session store should have following methods

  • set(sessionId, session, callback)
  • get(sessionId, callback)
  • destroy(sessionId, callback)

Useful Points

  • A session-id is not expected once it's cookie has been expired. So the expiry time for auth session cookies should be longer for good user experience.
  • Short session life is an overhead on server (session store). But long life lure the hackers

Useful links

TODO

  • x-forwarded-proto header