feat: Use bun's redis client

This commit is contained in:
Wanjohi
2025-10-25 22:19:33 +03:00
parent c5d0242d50
commit 01eb63b36f
2 changed files with 13 additions and 21 deletions

View File

@@ -1,7 +1,6 @@
import { z } from "zod/v4"; import { z } from "zod/v4";
import { RedisClient } from "bun";
import { subjects } from "../subjects.js"; import { subjects } from "../subjects.js";
import {redis} from 'bun';
import { issuer } from "@openauthjs/openauth"; import { issuer } from "@openauthjs/openauth";
import { RedisStorage } from "./src/storage.js"; import { RedisStorage } from "./src/storage.js";
import { Team } from "@nestri/core/team/index"; import { Team } from "@nestri/core/team/index";
@@ -9,11 +8,12 @@ import { CodeProvider } from "@openauthjs/openauth/provider/code";
//!TODO: Add an auth storage adapter, maybe redis or postgres //!TODO: Add an auth storage adapter, maybe redis or postgres
const storage = RedisStorage({ const storage = RedisStorage({
redis: RedisClient.create(), client: new RedisClient(process.env.REDIS_URL),
}); });
const app = issuer({ const app = issuer({
subjects, subjects,
storage,
providers: { providers: {
email: CodeProvider({ email: CodeProvider({
async request(_req, state, form, error) { async request(_req, state, form, error) {

View File

@@ -19,11 +19,8 @@
* *
* @packageDocumentation * @packageDocumentation
*/ */
import { import { redis as RedisClient } from "bun";
joinKey, import { joinKey, StorageAdapter } from "@openauthjs/openauth/storage/storage";
StorageAdapter,
} from "@openauthjs/openauth/storage/storage";
import { Cluster, Redis } from "@nestri/core/redis/client";
const separator = String.fromCharCode(0x1f); const separator = String.fromCharCode(0x1f);
@@ -31,16 +28,16 @@ const separator = String.fromCharCode(0x1f);
* Configure the Redis connection that's created. * Configure the Redis connection that's created.
*/ */
export interface RedisStorageOptions { export interface RedisStorageOptions {
redis: Cluster | Redis;
keyPrefix?: string; keyPrefix?: string;
defaultTTL?: number; defaultTTL?: number;
client: typeof RedisClient;
} }
/** /**
* Creates a Cloudflare KV store. * Creates a Cloudflare KV store.
* @param options - The config for the adapter. * @param options - The config for the adapter.
*/ */
export function RedisStorage(options: RedisStorageOptions): StorageAdapter { export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
const { redis } = options; const { client } = options;
const prefix = "storage"; const prefix = "storage";
function createKey(key: string[]): string { function createKey(key: string[]): string {
@@ -54,7 +51,7 @@ export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
return { return {
async get(key: string[]) { async get(key: string[]) {
const redisKey = createKey(key); const redisKey = createKey(key);
const value = await redis.get(redisKey); const value = await client.get(redisKey);
if (!value) return; if (!value) return;
@@ -70,21 +67,16 @@ export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
if (ttlSeconds) { if (ttlSeconds) {
// SET key value EX ttlSeconds // SET key value EX ttlSeconds
await options.redis.set( await client.set(redisKey, JSON.stringify(value), "EX", ttlSeconds);
redisKey,
JSON.stringify(value),
"EX",
ttlSeconds,
);
} else { } else {
// No expiry → simple SET // No expiry → simple SET
await options.redis.set(redisKey, JSON.stringify(value)); await client.set(redisKey, JSON.stringify(value));
} }
}, },
async remove(key: string[]) { async remove(key: string[]) {
const redisKey = createKey(key); const redisKey = createKey(key);
await options.redis.del(redisKey); await client.del(redisKey);
}, },
async *scan(prefix: string[]) { async *scan(prefix: string[]) {
@@ -95,7 +87,7 @@ export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
do { do {
// SCAN cursor MATCH pattern COUNT 100 // SCAN cursor MATCH pattern COUNT 100
const [nextCursor, keys] = await redis.scan( const [nextCursor, keys] = await client.scan(
cursor, cursor,
"MATCH", "MATCH",
scanPattern, scanPattern,
@@ -107,7 +99,7 @@ export function RedisStorage(options: RedisStorageOptions): StorageAdapter {
if (keys.length === 0) { if (keys.length === 0) {
continue; continue;
} }
const values = await redis.mget(...keys); const values = await client.mget(...keys);
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
const key = keys[i]; const key = keys[i];
if (!key) { if (!key) {