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

sveltekit-statefull-session

v0.0.2

Published

## Setup

Downloads

73

Readme

Sveltekit Statefull session

Setup

Create a src/hooks.server.ts file in your svelte project using the following code:

// src/hooks.server.ts
import { InMemorySessionRepository, handleSession } from '@sveltekit-statefull-session/core';

const repository = new InMemorySessionRepository({ ttl: '5s'});
export const handle = handleSession(repository, { debug: true });

If you already have a hooks.server.ts file declared, you can chain handler with the sequence function.

In your src/app.d.ts file, copy the following code, and fill in the Session type


// See https://kit.svelte.dev/docs/types#app
import type { SveltekitSession } from "@sveltekit-statefull-session/core";

// for information about these interfaces
declare global {
	namespace App {
		// interface Error {}
		interface Locals {
			session: SveltekitSession<Session>;
		}
		// interface PageData {}
		// interface Platform {}
	}

	type Session = {
		// TODO: Whatever you want in your session
	}
}

export { };

You can access only from the server side to the session using the locals.session available from the RequestEvent property in any load function (or action function).

handleSession

handleSession takes 2 paramèters :

  • repository: Must be any instance of the SessionRepository interface
  • options: (optional) to fine tune the session

Options

| Option | Description | Default | |-------------------|----------------------------------------|---------------------------------| | sessionCookieName | Name of the session cookie | 'svelte-session-key' | | keyGenerator | Function to generate a new session key | crypto.randomUUID() | | debug | Log session events | false | | secure | Secure the session cookie | true |

InMemorySessionRepository

Create an instance of InMemorySessionRepository using the 2 followings parameters :

  • ttl: A simple string describing how long should the session last. More information here

InMemorySessionRepository works only with the node adapter and only with a single instance of node.

RedisSessionRepository

You need to install the @redis/client, and create an instance in your src/hooks.server.ts using the createClient function.

You need to install the @sveltekit-statefull-session/repository-redis and create an instance of RedisSessionRepository.

Create an instance of RedisSessionRepository using the 2 followings parameters :

  • ttl: A simple string describing how long should the session last. More information here
  • client: The previously created instance of redis client

TTL

A ttl is a string declaring how long the session should last. The string must follow the following format ${number}${unit} :

  • number is the quantity of time
  • unit is the unit of time (currently only s: second, m: minute, h: hour are supported)

For instance :

const ttl1: TTL = '5m'  // 5 minutes
const ttl2: TTL = '2h'  // 2 hours
const ttl3: TTL = '36s' // 36 seconds

Examples

You can see a working example here: simple-session

Compile project

pnpm i
pnpm build
pnpm example

Todo

  • [x] Session TTL
  • [x] Cookie session id
  • [x] Create InMemory repository
  • [x] Create Redis repository
  • [ ] Create Prisma repository
  • [ ] Option for handleSession to refresh the TTL on update or not
  • [ ] Publish on npm repository with a github action
  • [ ] Playwright tests
  • [x] Create mono-repo for every repositories