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

svelte-kit-connect-redis

v0.1.0

Published

Redis session storage for svelte-kit-sessions.

Downloads

171

Readme

svelte-kit-connect-redis

npm test style

svelte-kit-connect-redis provides Redis session storage for svelte-kit-sessions.

Installation

svelte-kit-connect-redis requires svelte-kit-sessions to installed and one of the following compatible Redis clients:

Install with redis:

$ npm install redis svelte-kit-connect-redis svelte-kit-sessions

$ yarn add redis svelte-kit-connect-redis svelte-kit-sessions

$ pnpm add redis svelte-kit-connect-redis svelte-kit-sessions

Install with ioredis:

$ npm install ioredis svelte-kit-connect-redis svelte-kit-sessions

$ yarn add ioredis svelte-kit-connect-redis svelte-kit-sessions

$ pnpm add ioredis svelte-kit-connect-redis svelte-kit-sessions

Usage

svelte-kit-connect-redis can be used as a custom store for svelte-kit-sessions as follows.

Note For more information about svelte-kit-sessions, see https://www.npmjs.com/package/svelte-kit-sessions.

redis

// src/hooks.server.ts
import type { Handle } from '@sveltejs/kit';
import { sveltekitSessionHandle } from 'svelte-kit-sessions';
import RedisStore from 'svelte-kit-connect-redis';
import { createClient } from 'redis';

const client = redis.createClient({
	url: 'redis://{your redis endpoint}'
});
const clientConnection = await client.connect();

export const handle: Handle = sveltekitSessionHandle({
	secret: 'secret',
	store: new RedisStore({ client: clientConnection })
});

ioredis

// src/hooks.server.ts
import type { Handle } from '@sveltejs/kit';
import { sveltekitSessionHandle } from 'svelte-kit-sessions';
import RedisStore from 'svelte-kit-connect-redis';
import { Redis } from 'ioredis';

const client = new Redis({
	host: '{your redis host}',
	port: 6379
});

export const handle: Handle = sveltekitSessionHandle({
	secret: 'secret',
	store: new RedisStore({ client })
});

API

import RedisStore from 'svelte-kit-connect-redis';

new RedisStore(options);

new RedisStore(options)

Create a Redis store for svelte-kit-sessions.

Options

A summary of the options is as follows.

| Name | Type | required/optional | Description | | ---------- | -------------------------------------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | | client | redis.RedisClientType | ioredis.Redis | required | An instance of redis or ioredis | | prefix | string | optional | Key prefix in Redis (default: sess:). | | serializer | Serializer | optional | Provide a custom encoder/decoder to use when storing and retrieving session data from Redis (default: JSON.parse and JSON.stringify). | | ttl | number | optional | ttl to be used if ttl is Infinity when used from svelte-kit-sessions |

client

An instance of redis or ioredis.

prefix

Key prefix in Redis (default: sess:).

serializer

Provide a custom encoder/decoder to use when storing and retrieving session data from Redis (default: JSON.parse and JSON.stringify).

Note When setting up a custom serializer, the following interface must be satisfied.

interface Serializer {
	parse(s: string): SessionStoreData | Promise<SessionStoreData>;
	stringify(data: SessionStoreData): string;
}

ttl

When svelte-kit-sessions calls a method of the store (the set function), ttl(milliseconds) is passed to it. However, if the cookie options expires and maxAge are not set, the ttl passed will be Infinity.

If the ttl passed is Infinity, the ttl to be set can be set with this option. The unit is milliseconds.

// `svelte-kit-connect-redis` implementation excerpts
const ONE_DAY_IN_SECONDS = 86400;
export default class RedisStore implements Store {
	constructor(options: RedisStoreOptions) {
		this.ttl = options.ttl || ONE_DAY_IN_SECONDS * 1000;
	}

	ttl: number;

	async set(id: string, storeData: SessionStoreData, ttl: number): Promise<void> {
		// omission ...
		if (ttl !== Infinity) {
			// if `ttl` passed as argument is *not* Infinity, use the argument `ttl` as it is.
			await this.client.set(key, serialized, { PX: ttl });
			return;
		}
		// if `ttl` passed as argument is Infinity, use `options.ttl` or default.
		await this.client.set(key, serialized, { PX: this.ttl });
	}
}

License

MIT licensed