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

@dodiameer/sk-stateless-session

v0.1.0

Published

Stateless sessions for SvelteKit

Downloads

1

Readme

SvelteKit Stateless Session

A stateless session for SvelteKit-based apps

Wait, stateless sessions?

Yes, you read that right! Using @hapi/iron for encryption, you can take JSON data, convert it to a token, and send it to a client. When a new request arrives, you can take that token, decode it, and get back the exact same JSON data you put in. It is similar to JWTs but when using this project with it, you end up with an experience similar to regular server-side session, only without the drawbacks of being stateful.

This project is glue between Iron and SvelteKit to set-up the server-side session experience, so the stateless magic is the result of the effort of the maintainers of Iron, not me.

Installation

Install @dodiameer/sk-stateless-session with npm, pnpm, or yarn

npm install @dodiameer/sk-stateless-session
pnpm install @dodiameer/sk-stateless-session
yarn add @dodiameer/sk-stateless-session

Usage

General setup

In your src/hooks.ts / src/hooks/index.ts, add the following code:

import { statelessSession } from '@dodiameer/sk-stateless-session';

export const handle = statelessSession({
	// See parts below for options
});

export const getSession = ({ locals }) => {
	return locals.session.data;
};

If you have other hooks, you can use the sequence helper from SvelteKit:

import { sequence } from '@sveltejs/kit/hooks';
import { statelessSession } from '@dodiameer/sk-stateless-session';
import someOtherHook from './someOtherHook';

export const handle = sequence(
	someOtherHook,
	statelessSession({
		// See parts below for options
	})
);

Cookie based

Add getSessionIdFromCookie to your import:

// import { statelessSession } from "@dodiameer/sk-stateless-session"
import { statelessSession, getSessionIdFromCookie } from '@dodiameer/sk-stateless-session';

Then set your options:

export const handle = statelessSession({
	// "custom-cookie" is optional, you don't need it if you're not using a custom cookie name
	getSessionId: getSessionIdFromCookie('custom-cookie-name'),
	secret: process.env.SESSION_SECRET,
	// This part is also optional if you don't want to use a custom cookie name
	cookie: {
		name: 'custom-cookie-name'
	}
});

Custom setup

If you want to get the session id from a header and send new session ids through a header instead of using cookies, you can do the following:

export const handle = statelessSession({
	getSessionId: (request) => {
		// `request` is the same request you'd get in hooks. Do whatever you want with it here
		// You must return a string that's a session ID or null
		return sessionId || null;
	},
	secret: process.env.SESSION_SECRET,
	// Instead of sending a cookie, send the session id in "x-session-id" header (or another name you want)
	customHeader: 'x-session-id'
});

NOTE: The session ID gets changed every time session data changes, so keep that in mind when using a custom setup to handle that in your frontend.

Options

This is copy-pasted from the code. You'll also get the descriptions in your code editor if it supports JSDoc comments

interface MakeSessionOptions {
	/**
	 * Used to get the session id from the request.
	 *
	 * **Note**: If you store your session id in a cookie,
	 * your can use `getSessionIdFromCookie` provided by this package.
	 * @example
	 * const getSeal = (request) => {
	 *   const authToken = request.headers.authorization.replace('Bearer ', '') || null;
	 *   return authToken;
	 * }
	 */
	getSessionId: (request: ServerRequest) => string;
	/** Session secret */
	secret: string;
	/**
	 * Session expiration time in milliseconds. `0` means no expiration.
	 *
	 * @default 0 (no expiration)
	 */
	expiresIn?: number;
	/**
	 * If set, will return session id as a header with the provided name,
	 * and will NOT set a cookie. If not set, will set a cookie with the session id.
	 */
	customHeader?: string | false;
	/** Cookie options */
	cookie?: {
		/**
		 * Cookie name
		 *
		 * @default "sk-stateless-session"
		 */
		name?: string;
	} & Pick<CookieSerializeOptions, 'maxAge' | 'path' | 'secure' | 'httpOnly'>;
}

Credit

  • svelte-kit-cookie-session for inspiration for this project. My project uses a similar API to access the session data, but I added some features I wanted to use.
  • iron-store for providing a low-level store/session implementation with @hapi/iron. I used their codebase as a reference a few times while writing this project.

License

MIT