Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

svelte-kit-connect-upstash-redis

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svelte-kit-connect-upstash-redis

Upstash Redis session storage for svelte-kit-sessions.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

svelte-kit-connect-upstash-redis

npm test style

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

Installation

svelte-kit-connect-upstash-redis requires svelte-kit-sessions to installed.

$ npm install @upstash/redis svelte-kit-connect-upstash-redis svelte-kit-sessions

$ yarn add @upstash/redis svelte-kit-connect-upstash-redis svelte-kit-sessions

$ pnpm add @upstash/redis svelte-kit-connect-upstash-redis svelte-kit-sessions

Usage

svelte-kit-connect-upstash-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.

// src/hooks.server.ts
import type { Handle } from '@sveltejs/kit';
import { sveltekitSessionHandle } from 'svelte-kit-sessions';
import RedisStore from 'svelte-kit-connect-upstash-redis';
import { Redis } from '@upstash/redis'; // <- for Node
// import { Redis } from '@upstash/redis/cloudflare'; // <- for Cloudflare
// import { Redis } from '@upstash/redis/fastly'; // <- for Fastly

const client = new Redis({
	url: '{your upstash redis rest url}',
	token: '{your upstash redis rest token}'
});

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

API

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

new RedisStore(options);

new RedisStore(options)

Create a Redis store for svelte-kit-sessions.

Options

A summary of the options is as follows.

NameTyperequired/optionalDescription
clientupstashRedis.Redis | upstashRedisCloudflare.Redis | upstashRedisFastly.RedisrequiredAn instance of @upstash/redis
prefixstringoptionalKey prefix in Redis (default: sess:).
serializerSerializeroptionalProvide a custom encoder/decoder to use when storing and retrieving session data from Redis (default: JSON.parse and JSON.stringify).
ttlnumberoptionalttl to be used if ttl is Infinity when used from svelte-kit-sessions
client

An instance of @upstash/redis. You can use to all of @upstash/redis (node, cloudflare, fastly).

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-upstash-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

Keywords

FAQs

Package last updated on 09 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc