fix: eslint errors, switch to using maps

This commit is contained in:
Huskydog9988
2025-04-15 20:04:45 -04:00
parent e362f732e7
commit 8f429e1e56
21 changed files with 158 additions and 159 deletions

View File

@ -1,7 +1,6 @@
import { LRUCache } from "lru-cache";
import prisma from "../db/database";
import type { Session, SessionProvider } from "./types";
import { Prisma } from "@prisma/client";
export default function createDBSessionHandler(): SessionProvider {
const cache = new LRUCache<string, Session>({

View File

@ -1,5 +1,4 @@
import type { H3Event } from "h3";
import createMemorySessionProvider from "./memory";
import type { Session, SessionProvider } from "./types";
import { randomUUID } from "node:crypto";
import { parse as parseCookies } from "cookie-es";
@ -90,7 +89,7 @@ export class SessionHandler {
* @returns session token
*/
private getSessionToken(
request: MinimumRequestObject | undefined
request: MinimumRequestObject | undefined,
): string | undefined {
if (!request) throw new Error("Native web request not available");
const cookieHeader = request.headers.get("Cookie");

View File

@ -1,29 +1,29 @@
import type { Session, SessionProvider } from "./types";
export default function createMemorySessionHandler() {
const sessions: { [key: string]: Session } = {};
const sessions = new Map<string, Session>();
const memoryProvider: SessionProvider = {
async setSession(token, data) {
sessions[token] = data;
sessions.set(token, data);
return true;
},
async getSession<T extends Session>(token: string): Promise<T | undefined> {
const session = sessions[token];
const session = sessions.get(token);
return session ? (session as T) : undefined; // Ensure undefined is returned if session is not found
},
async updateSession(token, data) {
return this.setSession(token, data);
},
async removeSession(token) {
delete sessions[token];
sessions.delete(token);
return true;
},
async cleanupSessions() {
const now = new Date();
for (const token in sessions) {
for (const [token, session] of sessions) {
// if expires at time is before now, the session is expired
if (sessions[token].expiresAt < now) await this.removeSession(token);
if (session.expiresAt < now) await this.removeSession(token);
}
},
};

View File

@ -1,9 +1,8 @@
import { H3Event } from "h3";
export type Session = {
userId: string;
expiresAt: Date;
data: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
};
};