Centralize configure-able factory classes

This commit is contained in:
2022-09-26 11:34:23 -05:00
parent 5557aae543
commit c0595f3ef9
23 changed files with 262 additions and 682 deletions

View File

@@ -20,7 +20,7 @@ export class InjectSessionHTTPModule extends HTTPKernelModule {
}
public async apply(request: Request): Promise<Request> {
request.registerFactory(new SessionFactory())
request.registerFactory(request.make(SessionFactory))
const session = <Session> request.make(Session)
const id = request.cookies.get('extollo.session')

View File

@@ -1,87 +1,23 @@
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
Instantiable, getPropertyInjectionMetadata,
} from '../../di'
import {Collection, ErrorWithContext} from '../../util'
import {Instantiable} from '../../di'
import {Maybe} from '../../util'
import {MemorySession} from './MemorySession'
import {Session} from './Session'
import {Logging} from '../../service/Logging'
import {Config} from '../../service/Config'
import {ConfiguredSingletonFactory} from '../../di/factory/ConfiguredSingletonFactory'
/**
* 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<Session> {
protected readonly logging: Logging
protected readonly config: Config
/** True if we have printed the memory session warning at least once. */
private static loggedMemorySessionWarningOnce = false
constructor() {
super({})
this.logging = Container.getContainer().make<Logging>(Logging)
this.config = Container.getContainer().make<Config>(Config)
export class SessionFactory extends ConfiguredSingletonFactory<Session> {
protected getConfigKey(): string {
return 'server.session.driver'
}
produce(): Session {
return new (this.getSessionClass())()
protected getDefaultImplementation(): Instantiable<Session> {
return MemorySession
}
match(something: unknown): boolean {
return something === Session
protected getAbstractImplementation(): any {
return Session
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getSessionClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getSessionClass()
do {
const loadedMeta = getPropertyInjectionMetadata(currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Return the instantiable class of the configured session backend.
* @protected
* @return Instantiable<Session>
*/
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.`)
SessionFactory.loggedMemorySessionWarningOnce = true
}
if ( !isInstantiable(SessionClass) || !(SessionClass.prototype instanceof Session) ) {
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Session')
e.context = {
configKey: 'server.session.driver',
class: SessionClass.toString(),
}
}
return SessionClass
protected getDefaultImplementationWarning(): Maybe<string> {
return 'You are using the default memory-based session driver. It is recommended you configure a persistent session driver instead.'
}
}