Add basic memory-based session driver

This commit is contained in:
2021-03-07 13:26:14 -06:00
parent fdcd80a43e
commit 338b9be506
7 changed files with 202 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY
} from "@extollo/di"
import {Collection} from "@extollo/util"
import {MemorySession} from "./MemorySession";
import {Session} from "./Session";
import {Logging} from "../../service/Logging";
export class SessionFactory extends AbstractFactory {
protected readonly logging: Logging
constructor() {
super({})
this.logging = Container.getContainer().make<Logging>(Logging)
}
produce(dependencies: any[], parameters: any[]): any {
this.logging.warn(`You are using the default memory-based session driver. It is recommended you configure a persistent session driver instead.`)
return new MemorySession() // FIXME allow configuring
}
match(something: any) {
return something === Session
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.token)
if ( meta ) return meta
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = MemorySession // FIXME allow configuring
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
if ( loadedMeta ) meta.concat(loadedMeta)
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
}