socket-io-admin-ui/lib/stores.ts

102 lines
2.3 KiB
TypeScript

export abstract class Store {
abstract doesSessionExist(sessionId: string): Promise<boolean>;
abstract saveSession(sessionId: string): void;
}
export class InMemoryStore extends Store {
private sessions: Set<string> = new Set();
doesSessionExist(sessionId: string): Promise<boolean> {
return Promise.resolve(this.sessions.has(sessionId));
}
saveSession(sessionId: string) {
this.sessions.add(sessionId);
}
}
interface RedisStoreOptions {
/**
* The prefix of the keys stored in Redis
* @default "socket.io-admin"
*/
prefix: string;
/**
* The duration of the session in seconds
* @default 86400
*/
sessionDuration: number;
}
export class RedisStore extends Store {
private options: RedisStoreOptions;
constructor(readonly redisClient: any, options?: Partial<RedisStoreOptions>) {
super();
this.options = Object.assign(
{
prefix: "socket.io-admin",
sessionDuration: 86400,
},
options
);
}
private computeKey(sessionId: string) {
return `${this.options.prefix}#${sessionId}`;
}
doesSessionExist(sessionId: string): Promise<boolean> {
return new Promise((resolve, reject) => {
this.redisClient.exists(
this.computeKey(sessionId),
(err: Error | null, result: any) => {
if (err) {
reject(err);
} else {
resolve(result === 1);
}
}
);
});
}
saveSession(sessionId: string) {
this.redisClient.set(
this.computeKey(sessionId),
"1",
"EX",
"" + this.options.sessionDuration
);
}
}
export class RedisV4Store extends Store {
private options: RedisStoreOptions;
constructor(readonly redisClient: any, options?: Partial<RedisStoreOptions>) {
super();
this.options = Object.assign(
{
prefix: "socket.io-admin",
sessionDuration: 86400,
},
options
);
}
private computeKey(sessionId: string) {
return `${this.options.prefix}#${sessionId}`;
}
doesSessionExist(sessionId: string): Promise<boolean> {
return this.redisClient.exists(this.computeKey(sessionId));
}
saveSession(sessionId: string) {
return this.redisClient.set(this.computeKey(sessionId), "1", {
EX: this.options.sessionDuration,
});
}
}