2021-06-03 03:36:25 +00:00
|
|
|
import {NoSessionKeyError, Session, SessionData, SessionNotLoadedError} from './Session'
|
|
|
|
import {Injectable} from '../../di'
|
2021-03-07 19:26:14 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Implementation of the session driver that stores session data in memory.
|
|
|
|
* This is the default, for compatibility, but it is recommended that you replace
|
|
|
|
* this driver with one with a persistent backend.
|
|
|
|
*/
|
2021-03-07 19:26:14 +00:00
|
|
|
@Injectable()
|
|
|
|
export class MemorySession extends Session {
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Mapping of session key to session data object. */
|
2021-03-07 19:26:14 +00:00
|
|
|
private static sessionsByID: {[key: string]: SessionData} = {}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Get a particular session by ID. */
|
2021-03-07 19:26:14 +00:00
|
|
|
private static getSession(id: string) {
|
|
|
|
if ( !this.sessionsByID[id] ) {
|
|
|
|
this.sessionsByID[id] = {} as SessionData
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.sessionsByID[id]
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Store the given session data by its ID. */
|
2021-03-07 19:26:14 +00:00
|
|
|
private static setSession(id: string, data: SessionData) {
|
|
|
|
this.sessionsByID[id] = data
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** The ID of this session. */
|
2021-03-07 19:26:14 +00:00
|
|
|
protected sessionID?: string
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The associated data for this session. */
|
2021-03-07 19:26:14 +00:00
|
|
|
protected data?: SessionData
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
}
|
2021-03-08 18:34:31 +00:00
|
|
|
|
2021-03-07 19:26:14 +00:00
|
|
|
public getKey(): string {
|
2021-06-03 03:36:25 +00:00
|
|
|
if ( !this.sessionID ) {
|
|
|
|
throw new NoSessionKeyError()
|
|
|
|
}
|
2021-03-07 19:26:14 +00:00
|
|
|
return this.sessionID
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public setKey(key: string): void {
|
2021-03-07 19:26:14 +00:00
|
|
|
this.sessionID = key
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public load(): void {
|
|
|
|
if ( !this.sessionID ) {
|
|
|
|
throw new NoSessionKeyError()
|
|
|
|
}
|
|
|
|
|
2021-03-07 19:26:14 +00:00
|
|
|
this.data = MemorySession.getSession(this.sessionID)
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public persist(): void {
|
|
|
|
if ( !this.sessionID ) {
|
|
|
|
throw new NoSessionKeyError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !this.data ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
|
|
|
|
2021-03-07 19:26:14 +00:00
|
|
|
MemorySession.setSession(this.sessionID, this.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
public getData(): SessionData {
|
2021-06-03 03:36:25 +00:00
|
|
|
if ( !this.data ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
|
|
|
|
2021-03-07 19:26:14 +00:00
|
|
|
return this.data
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public setData(data: SessionData): void {
|
2021-03-07 19:26:14 +00:00
|
|
|
this.data = data
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public get(key: string, fallback?: unknown): any {
|
|
|
|
if ( !this.data ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
|
|
|
|
2021-03-07 19:26:14 +00:00
|
|
|
return this.data?.[key] ?? fallback
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public set(key: string, value: unknown): void {
|
|
|
|
if ( !this.data ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
|
|
|
|
2021-03-07 19:26:14 +00:00
|
|
|
this.data[key] = value
|
|
|
|
}
|
2021-07-17 17:49:07 +00:00
|
|
|
|
|
|
|
public forget(key: string): void {
|
|
|
|
if ( !this.data ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
delete this.data[key]
|
|
|
|
}
|
2021-03-07 19:26:14 +00:00
|
|
|
}
|