mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-10-30 14:47:36 +00:00
The client was already @hanzo/kv; only the vocabulary was still Redis. Every module, symbol and comment now says what it connects to: lib/redis.ts -> lib/kv.ts (exports kv, lockKv) lib/redis-job-store.ts -> lib/kv-job-store.ts (KvJobStore) lib/redis-download-job-store.ts -> lib/kv-download-job-store.ts lib/files/tus-redis-locker.ts -> lib/files/tus-kv-locker.ts (KvLocker, KvLock) lib/api/domains/redis.ts -> lib/api/domains/kv.ts (getKey) lib/redis/ -> lib/kv-queue/ KV_URL is now the only endpoint variable — the REDIS_URL fallback and the redis://localhost default are gone, and the default scheme is kv://, which @hanzo/kv parses natively (utils/index.js). .env.example gained the KV_URL it never documented, and lost UPSTASH_REDIS_REST_LOCKER_URL/TOKEN: nothing has read those since the client changed — the locker opens a second connection to the same KV, so there is no second endpoint to configure. The second connection is `lockKv`, not `locker`: both tus routes already have a local `const locker = new KvLocker(...)`, so naming the client `locker` made the lock take itself as its own client. Verified against the repo's own gate (npx tsc --noEmit): 100 errors before and after, 0 introduced, compared set-wise by file:line:code. Behaviour is untouched — this is naming, one connection string, and dead config. Co-authored-by: Hanzo Dev <[email protected]>
87 lines
2.1 KiB
TypeScript
87 lines
2.1 KiB
TypeScript
import crypto from "crypto";
|
|
import { z } from "zod";
|
|
|
|
import { kv } from "@/lib/kv";
|
|
|
|
export const PREVIEW_EXPIRATION_TIME = 20 * 60 * 1000; // 20 minutes
|
|
|
|
const ZPreviewSessionSchema = z.object({
|
|
userId: z.string(),
|
|
linkId: z.string(),
|
|
expiresAt: z.number(),
|
|
});
|
|
|
|
type PreviewSession = z.infer<typeof ZPreviewSessionSchema>;
|
|
|
|
async function createPreviewSession(
|
|
linkId: string,
|
|
userId: string,
|
|
): Promise<{ token: string; expiresAt: number }> {
|
|
const sessionToken = crypto.randomBytes(32).toString("hex");
|
|
const expiresAt = Date.now() + PREVIEW_EXPIRATION_TIME;
|
|
|
|
const sessionData: PreviewSession = {
|
|
linkId,
|
|
userId,
|
|
expiresAt,
|
|
};
|
|
|
|
// Validate session data before storing
|
|
ZPreviewSessionSchema.parse(sessionData);
|
|
|
|
// Store session in KV
|
|
await kv.set(
|
|
`preview_session:${sessionToken}`,
|
|
JSON.stringify(sessionData),
|
|
{ pxat: expiresAt },
|
|
);
|
|
|
|
return {
|
|
token: sessionToken,
|
|
expiresAt,
|
|
};
|
|
}
|
|
|
|
async function verifyPreviewSession(
|
|
previewToken: string,
|
|
userId: string,
|
|
linkId: string,
|
|
): Promise<PreviewSession | null> {
|
|
const sessionToken = previewToken;
|
|
if (!sessionToken) return null;
|
|
|
|
const session = await kv.get(`preview_session:${sessionToken}`);
|
|
if (!session) return null;
|
|
|
|
try {
|
|
const sessionData = ZPreviewSessionSchema.parse(session);
|
|
|
|
// Check if the session is for the correct user
|
|
if (sessionData.userId !== userId) {
|
|
await kv.del(`preview_session:${sessionToken}`);
|
|
return null;
|
|
}
|
|
|
|
// Check if session is expired
|
|
if (sessionData.expiresAt < Date.now()) {
|
|
await kv.del(`preview_session:${sessionToken}`);
|
|
return null;
|
|
}
|
|
|
|
// Check if the session is for the correct link and dataroom
|
|
if (sessionData.linkId !== linkId) {
|
|
await kv.del(`preview_session:${sessionToken}`);
|
|
return null;
|
|
}
|
|
|
|
return sessionData;
|
|
} catch (error) {
|
|
console.error("Preview session verification error:", error);
|
|
await kv.del(`preview_session:${sessionToken}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export { createPreviewSession, verifyPreviewSession };
|
|
export type { PreviewSession };
|