mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-14 00:31:25 +10:00
fix: eslint errors, switch to using maps
This commit is contained in:
@ -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>({
|
||||
|
||||
@ -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");
|
||||
|
||||
@ -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);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
3
server/internal/session/types.d.ts
vendored
3
server/internal/session/types.d.ts
vendored
@ -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;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user