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 🙏

© 2025 – Pkg Stats / Ryan Hefner

hapi_primus_sessions

v0.0.2

Published

A hapi and primus plugin which extends primus' spark with a `getSession(cb)` method which returns the current hapi session object.

Downloads

5

Readme

Hapi Primus Sessions

A hapi and primus plugin which extends primus' spark with a getSession(cb) method which returns the current hapi session object.

Why?

Typically primus binds to the same http server as hapi, but listens for it's own specific requests for setting up the websockets. These requests aren't routed through the hapi stack, so it won't have the session object added to the request object, which is needed if you want to check user session/auth information from within a primus connection/request.

This plugin extends both hapi and primus to allow you access to the session from within a primus request.

How?

Don't ask.

Ok, really?

You sure?

Ok, fine. The spark object in primus is a partial request object, all it really has access to is the raw HTTP headers. But these are typically encrypted and decrypted deep in the bowels of hapi, so they aren't much use outside of the hapi stack.

Hapi doesn't expose any methods to retrieve a session, so we have to get funky. The plugin creates a fake http request and response object, which it emits from the http server that hapi is listening to, pointed at a custom route we inject into hapi. We add the encrypted headers from the primus spark to the request object, and a custom writeSession method to the response object which is scoped within the primus spark object.

Hapi happily takes the fake request, decodes the session information, and passes it all along to our custom hapi route handler, along with our fake response object. Our hapi handler then calls our custom writeSession method on the fake response object passing the session object, and writeSession passes it back into the scope of the spark.

Is there a better way to do this

I hope so, but we haven't found it yet.

Usage

Require in your hapi server pack as with any hapi plugin:

var Hapi = require('hapi');
var server = new Hapi.Server('localhost', 3030);

var Primus = require('primus');
vae primus = new Primus(server.listener, { transformer: 'socket.io' });

server.pack({
        hapi_primus_sessions: { primus: primus, server: server }
    },
    function (pluginsCallback (err) {
        if (err) throw err;
    }
});

primus.on('connection', function (spark) {
    spark.getSession(function (err, session) {
        if (err) throw err;

        // You can now use the session object as if you were within hapi
        var user = session.user;
        var loggedIn = session._isAuthenticated(); //if using travelogue
        // ... etc
    });
});

Hapi Plugin options

The hapi plugin takes a number of options:

  • primus [required] - a primus instance
  • server [required] - the hapi server instance
  • debug [optional, default=false] - whether to log debug messages
  • logger [optional, default=console] - where to log debug messages, anything with a log method will work
  • routePath [optional, default='/hapi-primus-sesssions/write-session'] - the internal route url created to share sessions. You should only need to change this if you want to use that "prime piece of real-estate" path within your own app.

spark.getSession(cb)

A getSession method is added to every primus spark object. It accepts a callback with the signature: function (err, session).

Session is the session object direct from hapi, so you can use it to auth/lookup the current user/etc as if you were within the hapi stack.

License

MIT

Infrequently Asked Questions

  • Is this stable? Well, it works for me.
  • Can I contribute/suggest a better way of doing this? Please do.
  • Are there tests? If you can show me how to test this, there will be.

Who

Philip Roberts with much help from Michael Garvin and Nathan LaFreniere. All &yetis.