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 🙏

© 2026 – Pkg Stats / Ryan Hefner

xsess

v0.0.6

Published

Framework-agnostic session middleware with cookie and header support

Readme

xsess

Framework-agnostic session middleware with cookie and header support — works seamlessly with Express, Hono, Socket.IO, and any Web Standard framework.

Unlike express-session, xsess accepts session tokens from both cookies and the X-Session header, making it a drop-in choice for APIs consumed by browsers, mobile apps, and other HTTP clients — and it can authenticate WebSocket connections against the same sessions.

Install

pnpm add xsess

Quick Start

Express

import express from 'express';
import { session } from 'xsess/express';

const app = express();

app.use(
  session({
    secret: process.env.SESSION_SECRET!,
  }),
);

app.get('/', (req, res) => {
  req.session.views = ((req.session.views as number) ?? 0) + 1;
  res.json({ views: req.session.views });
});

Hono

import { Hono } from 'hono';
import { session } from 'xsess/hono';

const app = new Hono();

app.use(session({ secret: process.env.SESSION_SECRET! }));

app.get('/', (c) => {
  const sess = c.get('session');
  sess.views = ((sess.views as number) ?? 0) + 1;
  return c.json({ views: sess.views });
});

Socket.IO

Authenticate WebSocket connections against sessions established over HTTP. The middleware resolves the session from the handshake — cookie first, then the X-Session header, then a token under socket.handshake.auth — and attaches it to socket.data.session.

import { Server } from 'socket.io';
import { session, getSession } from 'xsess/socket.io';

const io = new Server();

io.use(session({ secret: process.env.SESSION_SECRET!, required: true }));

io.on('connection', (socket) => {
  const sess = getSession(socket);
  socket.emit('whoami', sess?.userId ?? null);
});

It is read-only — a handshake has no response to write to, so it never creates a session or sets a cookie. Call socket.data.session.save() to persist changes made during the connection. In addition to the shared options below, the Socket.IO middleware accepts:

| Option | Default | Behavior | |--------|---------|----------| | required | false | Reject the connection (calls next(err)) when no valid session is found. | | authKey | 'token' | Key under socket.handshake.auth to read a signed session ID from, for clients that cannot send cookies/headers. |

Options

session({
  // Cookie name (default: 'sid')
  name: 'sid',

  // Secret for signing session IDs (required)
  secret: 'my-secret',

  // Session storage (default: in-memory storage). Also accepts a factory
  // `() => Storage | Promise<Storage>`, resolved lazily on first use — handy when
  // the storage comes from a DI container resolved after the middleware is built.
  storage: new MyCustomStorage(),

  // Re-set cookie on every response to refresh maxAge (default: false)
  rolling: false,

  // Save session to storage on every response, even if unmodified (default: false)
  resave: false,

  // Save new sessions with no data to storage (default: false)
  saveUninitialized: false,

  // Cookie options
  cookie: {
    maxAge: 86_400, // 1 day (in seconds)
    httpOnly: true,
    secure: true,
    sameSite: 'lax',
  },

  // Header-based session ID options
  header: {
    name: 'X-Session',   // Header name (default: 'X-Session')
    policy: 'init',       // When to send response header: 'never' | 'init' | 'always'
  },
});

Header Policy

Controls when the session ID is included in the response header:

| Policy | Behavior | |--------|----------| | 'never' | Never set response header automatically. Set it manually on login. (default) | | 'init' | Set response header when a new session is created or regenerated. | | 'always' | Set response header on every response. |

Session API

// Read/write session data
session.userId = 42;

// Session ID
session.id;        // unsigned
session.signedId;  // signed with secret

// Cookie options (can be modified per-session)
session.cookie;

// Persist session to storage
await session.save();

// Destroy session and clear cookie
await session.destroy();

// Generate new session ID, preserving data
await session.regenerate();

All methods support both promises and callbacks:

await session.save();
session.save((err) => { /* ... */ });

Custom Storage

Extend the Storage base class to implement a custom storage backend:

import { Storage } from 'xsess';
import type { StoredSession } from 'xsess';

class RedisStorage extends Storage {
  async get(id: string): Promise<StoredSession | null> {
    const data = await redis.get(`sess:${id}`);
    if (!data) return null;
    const session = JSON.parse(data) as StoredSession;
    if (this.isExpired(session)) return null;
    return session;
  }

  async set(id: string, session: StoredSession): Promise<void> {
    const ttl = session.cookie.originalMaxAge ?? 86400;
    await redis.set(`sess:${id}`, JSON.stringify(session), 'EX', ttl);
  }

  async destroy(id: string): Promise<void> {
    await redis.del(`sess:${id}`);
  }
}

The Storage base class provides:

  • isExpired(session) — check if a session has expired based on its cookie
  • touch(id, session) — update session expiry without rewriting data (defaults to calling set)

How It Works

  1. On each request, xsess looks for a session ID in the cookie first, then falls back to the request header.
  2. If found and valid, the session is loaded from storage.
  3. If not found, a new session is created with a UUID v7 ID.
  4. Session data is saved when the response completes, ensuring sequential requests always see the latest state.
  5. Cookies are set for new sessions (and on every response if rolling is enabled).
  6. The response header is set based on the header.policy option.

License

MIT