2020-07-22 14:38:17 +00:00
|
|
|
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'
|
2020-07-28 14:11:48 +00:00
|
|
|
import {Logging} from '../../service/logging/Logging.ts'
|
2020-07-22 14:38:17 +00:00
|
|
|
|
|
|
|
export interface ModuleRegistrationFluency {
|
|
|
|
before: (other?: Instantiable<Module>) => Kernel,
|
|
|
|
after: (other?: Instantiable<Module>) => Kernel,
|
|
|
|
first: () => Kernel,
|
|
|
|
last: () => Kernel,
|
2020-07-24 14:53:30 +00:00
|
|
|
core: () => Kernel,
|
2020-07-22 14:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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>()
|
2020-07-24 14:53:30 +00:00
|
|
|
protected inflight?: Module
|
2020-07-22 14:38:17 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:53:30 +00:00
|
|
|
if ( this.inflight ) {
|
|
|
|
request = await this.inflight.apply(request)
|
|
|
|
}
|
|
|
|
|
2020-07-22 14:38:17 +00:00
|
|
|
for ( const module of this.postflight.toArray() ) {
|
|
|
|
request = await module.apply(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
|
|
|
public register(module: Instantiable<Module>): ModuleRegistrationFluency {
|
2020-07-28 14:11:48 +00:00
|
|
|
this.make(Logging).verbose(`Registering HTTP kernel module: ${module.name}`)
|
2020-07-22 14:38:17 +00:00
|
|
|
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
|
|
|
|
},
|
2020-07-24 14:53:30 +00:00
|
|
|
core: (): Kernel => {
|
|
|
|
this.inflight = this.make(module)
|
|
|
|
return this
|
|
|
|
},
|
2020-07-22 14:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|