Setup eslint and enforce rules
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {NoSessionKeyError, Session, SessionData, SessionNotLoadedError} from "./Session";
|
||||
import {Injectable} from "../../di";
|
||||
import {NoSessionKeyError, Session, SessionData, SessionNotLoadedError} from './Session'
|
||||
import {Injectable} from '../../di'
|
||||
|
||||
/**
|
||||
* Implementation of the session driver that stores session data in memory.
|
||||
@@ -31,44 +31,66 @@ export class MemorySession extends Session {
|
||||
/** The associated data for this session. */
|
||||
protected data?: SessionData
|
||||
|
||||
constructor() { super() }
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
public getKey(): string {
|
||||
if ( !this.sessionID ) throw new NoSessionKeyError()
|
||||
if ( !this.sessionID ) {
|
||||
throw new NoSessionKeyError()
|
||||
}
|
||||
return this.sessionID
|
||||
}
|
||||
|
||||
public setKey(key: string) {
|
||||
public setKey(key: string): void {
|
||||
this.sessionID = key
|
||||
}
|
||||
|
||||
public load() {
|
||||
if ( !this.sessionID ) throw new NoSessionKeyError()
|
||||
public load(): void {
|
||||
if ( !this.sessionID ) {
|
||||
throw new NoSessionKeyError()
|
||||
}
|
||||
|
||||
this.data = MemorySession.getSession(this.sessionID)
|
||||
}
|
||||
|
||||
public persist() {
|
||||
if ( !this.sessionID ) throw new NoSessionKeyError()
|
||||
if ( !this.data ) throw new SessionNotLoadedError()
|
||||
public persist(): void {
|
||||
if ( !this.sessionID ) {
|
||||
throw new NoSessionKeyError()
|
||||
}
|
||||
|
||||
if ( !this.data ) {
|
||||
throw new SessionNotLoadedError()
|
||||
}
|
||||
|
||||
MemorySession.setSession(this.sessionID, this.data)
|
||||
}
|
||||
|
||||
public getData(): SessionData {
|
||||
if ( !this.data ) throw new SessionNotLoadedError()
|
||||
if ( !this.data ) {
|
||||
throw new SessionNotLoadedError()
|
||||
}
|
||||
|
||||
return this.data
|
||||
}
|
||||
|
||||
public setData(data: SessionData) {
|
||||
public setData(data: SessionData): void {
|
||||
this.data = data
|
||||
}
|
||||
|
||||
public get(key: string, fallback?: any): any {
|
||||
if ( !this.data ) throw new SessionNotLoadedError()
|
||||
public get(key: string, fallback?: unknown): any {
|
||||
if ( !this.data ) {
|
||||
throw new SessionNotLoadedError()
|
||||
}
|
||||
|
||||
return this.data?.[key] ?? fallback
|
||||
}
|
||||
|
||||
public set(key: string, value: any) {
|
||||
if ( !this.data ) throw new SessionNotLoadedError()
|
||||
public set(key: string, value: unknown): void {
|
||||
if ( !this.data ) {
|
||||
throw new SessionNotLoadedError()
|
||||
}
|
||||
|
||||
this.data[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {Injectable, Inject} from "../../di"
|
||||
import {ErrorWithContext} from "../../util"
|
||||
import {Request} from "../lifecycle/Request"
|
||||
import {Injectable, Inject} from '../../di'
|
||||
import {ErrorWithContext} from '../../util'
|
||||
import {Request} from '../lifecycle/Request'
|
||||
|
||||
/**
|
||||
* Type alias describing some inflated session data.
|
||||
@@ -53,8 +53,8 @@ export abstract class Session {
|
||||
public abstract setData(data: SessionData): void
|
||||
|
||||
/** Get a value from the session by key. */
|
||||
public abstract get(key: string, fallback?: any): any
|
||||
public abstract get(key: string, fallback?: unknown): any
|
||||
|
||||
/** Set a value in the session by key. */
|
||||
public abstract set(key: string, value: any): void
|
||||
public abstract set(key: string, value: unknown): void
|
||||
}
|
||||
|
||||
@@ -5,20 +5,21 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY
|
||||
} from "../../di"
|
||||
import {Collection, ErrorWithContext} from "../../util"
|
||||
import {MemorySession} from "./MemorySession";
|
||||
import {Session} from "./Session";
|
||||
import {Logging} from "../../service/Logging";
|
||||
import {Config} from "../../service/Config";
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable,
|
||||
} from '../../di'
|
||||
import {Collection, ErrorWithContext} from '../../util'
|
||||
import {MemorySession} from './MemorySession'
|
||||
import {Session} from './Session'
|
||||
import {Logging} from '../../service/Logging'
|
||||
import {Config} from '../../service/Config'
|
||||
|
||||
/**
|
||||
* A dependency injection factory that matches the abstract Session class
|
||||
* and produces an instance of the configured session driver implementation.
|
||||
*/
|
||||
export class SessionFactory extends AbstractFactory {
|
||||
export class SessionFactory extends AbstractFactory<Session> {
|
||||
protected readonly logging: Logging
|
||||
|
||||
protected readonly config: Config
|
||||
|
||||
/** True if we have printed the memory session warning at least once. */
|
||||
@@ -30,17 +31,19 @@ export class SessionFactory extends AbstractFactory {
|
||||
this.config = Container.getContainer().make<Config>(Config)
|
||||
}
|
||||
|
||||
produce(dependencies: any[], parameters: any[]): Session {
|
||||
return new (this.getSessionClass())
|
||||
produce(): Session {
|
||||
return new (this.getSessionClass())()
|
||||
}
|
||||
|
||||
match(something: any) {
|
||||
match(something: unknown): boolean {
|
||||
return something === Session
|
||||
}
|
||||
|
||||
getDependencyKeys(): Collection<DependencyRequirement> {
|
||||
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getSessionClass())
|
||||
if ( meta ) return meta
|
||||
if ( meta ) {
|
||||
return meta
|
||||
}
|
||||
return new Collection<DependencyRequirement>()
|
||||
}
|
||||
|
||||
@@ -50,7 +53,9 @@ export class SessionFactory extends AbstractFactory {
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
if ( loadedMeta ) meta.concat(loadedMeta)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
currentToken = Object.getPrototypeOf(currentToken)
|
||||
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
|
||||
|
||||
@@ -62,7 +67,7 @@ export class SessionFactory extends AbstractFactory {
|
||||
* @protected
|
||||
* @return Instantiable<Session>
|
||||
*/
|
||||
protected getSessionClass() {
|
||||
protected getSessionClass(): Instantiable<Session> {
|
||||
const SessionClass = this.config.get('server.session.driver', MemorySession)
|
||||
if ( SessionClass === MemorySession && !SessionFactory.loggedMemorySessionWarningOnce ) {
|
||||
this.logging.warn(`You are using the default memory-based session driver. It is recommended you configure a persistent session driver instead.`)
|
||||
@@ -70,9 +75,9 @@ export class SessionFactory extends AbstractFactory {
|
||||
}
|
||||
|
||||
if ( !isInstantiable(SessionClass) || !(SessionClass.prototype instanceof Session) ) {
|
||||
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Session');
|
||||
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Session')
|
||||
e.context = {
|
||||
config_key: 'server.session.driver',
|
||||
configKey: 'server.session.driver',
|
||||
class: SessionClass.toString(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user