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

@curveball/session

v1.0.0

Published

Session storage using HTTP cookies

Downloads

505

Readme

Curveball Session Middleware

This package adds support for sessions to the Curveball framework.

Features:

  • It's lazy. It will only start a session if there is something in the store.
  • It will also automatically wipe the session data if session data was emptied.
  • It provides features for generating and validating CSRF tokens.

Installation

npm install @curveball/session

Upgrading from versions 0.5 and below

If you are upgrading from a 0.5.x release or earlier, this package introduces a BC break since 0.6.

In 0.5 session data was available in ctx.state.session and ctx.state.sessionId, but this has been moved to ctx.session and ctx.sessionId.

Getting started

Adding the middleware

import session from '@curveball/session';

app.use(session({
  store: 'memory',
});

This will add the in-memory session store to curveball. This store is mostly meant for testing.

Here is another example with more options:

import session from '@curveball/session';

app.use(session({
  store: 'memory',
  cookieName: 'MY_SESSION',
  expiry: 7200,
  cookieOptions: {
    secure: true,
    path: '/',
    sameSite: true,
  },
});
  • cookieName - Updates the name of the HTTP Cookie. It's CB by default.
  • expiry - The number of seconds of inactivity before the session disappears. this is 3600 seconds by default. It only pertains to the longevity of the session in the store, it doesn't influence cookie parameters.
  • cookieOptions - If set, override cookie options from the default. The list of supported options can be found in the documentation of the [cookie package][3].

Using the session store

In your own controllers and middlewares, you can set and update session data via the ctx.session property.

app.use( ctx => {

  // Running this will create the session
  ctx.session = { userId: 5 };
  ctx.response.body = 'Hello world';

});

Deleting a session

To delete an open session, just clear the session data:

app.use( ctx => {

  // Running this will create the session
  ctx.session = null;

});

Re-generate a session id.

If you clear the session id, but there is still data, the middleware will remove the old session and automatically create a new session id:

app.use( ctx => {

  // This will kill the old session and start a new one with the same data.
  ctx.sessionId = null;

});

CSRF token support

To obtain a CSRF token for forms, the middleware provides a getCsrf() function:

app.use( async ctx => {

  // Obtain a CSRF token for HTML forms:
  const csrfToken = await ctx.getCsrf();

});

It's recommended to embed this token in HTML forms as such:

<input type="hidden" name="csrf-token" value="....token goes here" />

Then on POST requests, you can easily validate the token with the validateCsrf function. If the token was incorrect, this will automatically result in a 403 error:

app.use(route.post('/form-submit', ctx => {

  // Throws error if csrf-token was not supplied or incorrect
  ctx.validateCsrf();

}));

API

It's desirable to create your own stores for product usage. Eventually this project will probably add a few more default stores.

Until then, you must implement the following interface:

interface SessionStore {

  set(id: string, values: SessionValues, expire: number): Promise<void>;
  get(id: string): Promise<SessionValues>,
  delete(id: string): Promise<void>,
  newSessionId(): Promise<string>,

}

SessionValues is simply a key->value object. expire is expressed as a unix timestamp.