You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.9 KiB

import Module from './Module.ts'
import Instantiable from '../../../../di/src/type/Instantiable.ts'
import AppClass from '../../lifecycle/AppClass.ts'
import {Collection} from '../../collection/Collection.ts'
import {Service} from '../../../../di/src/decorator/Service.ts'
import {Request} from '../Request.ts'
import {Logging} from '../../service/logging/Logging.ts'
export interface ModuleRegistrationFluency {
before: (other?: Instantiable<Module>) => Kernel,
after: (other?: Instantiable<Module>) => Kernel,
first: () => Kernel,
last: () => Kernel,
core: () => Kernel,
}
export class KernelModuleNotFoundError extends Error {
constructor(mod_name: string) {
super(`The kernel module ${mod_name} is not registered with the kernel.`)
}
}
@Service()
export default class Kernel extends AppClass {
protected preflight: Collection<Module> = new Collection<Module>()
protected inflight?: Module
protected postflight: Collection<Module> = new Collection<Module>()
public async handle(request: Request): Promise<Request> {
for ( const module of this.preflight.toArray() ) {
request = await module.apply(request)
}
if ( this.inflight ) {
request = await this.inflight.apply(request)
}
for ( const module of this.postflight.toArray() ) {
request = await module.apply(request)
}
return request
}
public register(module: Instantiable<Module>): ModuleRegistrationFluency {
this.make(Logging).verbose(`Registering HTTP kernel module: ${module.name}`)
return {
before: (other?: Instantiable<Module>): Kernel => {
if ( !other ) {
this.preflight = this.preflight.push(this.make(module))
return this
}
let found_index = this.preflight.find((mod: Module) => mod instanceof other)
if ( typeof found_index !== 'undefined' ) {
this.preflight = this.preflight.put(found_index, this.make(module))
} else {
found_index = this.postflight.find((mod: Module) => mod instanceof other)
}
if ( typeof found_index !== 'undefined' ) {
this.postflight = this.postflight.put(found_index, this.make(module))
} else {
throw new KernelModuleNotFoundError(other.name)
}
return this
},
after: (other?: Instantiable<Module>): Kernel => {
if ( !other ) {
this.postflight = this.postflight.push(this.make(module))
return this
}
let found_index = this.preflight.find((mod: Module) => mod instanceof other)
if ( typeof found_index !== 'undefined' ) {
this.preflight = this.preflight.put(found_index + 1, this.make(module))
} else {
found_index = this.postflight.find((mod: Module) => mod instanceof other)
}
if ( typeof found_index !== 'undefined' ) {
this.postflight = this.postflight.put(found_index + 1, this.make(module))
} else {
console.log(this.preflight, this.postflight)
throw new KernelModuleNotFoundError(other.name)
}
return this
},
first: (): Kernel => {
this.preflight = this.preflight.put(0, this.make(module))
return this
},
last: (): Kernel => {
this.postflight = this.postflight.push(this.make(module))
return this
},
core: (): Kernel => {
this.inflight = this.make(module)
return this
},
}
}
}