Add kernel modules and basic kernel handling

This commit is contained in:
2021-03-07 12:51:04 -06:00
parent 94add3d471
commit fdcd80a43e
6 changed files with 56 additions and 14 deletions

View File

@@ -50,6 +50,10 @@ export class HTTPCookieJar {
}
}
has(name: string) {
return !!this.parsed[name]
}
clear(name: string, options?: HTTPCookieOptions) {
if ( !options ) options = {}
options.expires = new Date(0)
@@ -68,7 +72,9 @@ export class HTTPCookieJar {
for ( const key in this.parsed ) {
if ( !this.parsed.hasOwnProperty(key) ) continue
const cookie = this.parsed[key]
if ( cookie.exists ) continue
const parts = []
parts.push(`${key}=${encodeURIComponent(cookie.originalValue)}`)

View File

@@ -1,14 +1,17 @@
import {HTTPKernelModule} from "../HTTPKernelModule";
import {Request} from "../../lifecycle/Request";
import {Injectable} from "@extollo/di"
import {HTTPKernel} from "../HTTPKernel";
export class PrepareRequestHTTPModule extends HTTPKernelModule {
@Injectable()
export class PoweredByHeaderInjectionHTTPModule extends HTTPKernelModule {
public static register(kernel: HTTPKernel) {
kernel.register(this).first()
kernel.register(this).after()
}
public async apply(request: Request) {
await request.prepare()
// FIXME make this configurable
request.response.setHeader('X-Powered-By', 'Extollo')
return request
}
}

View File

@@ -0,0 +1,26 @@
import {HTTPKernelModule} from "../HTTPKernelModule";
import {Injectable, Inject} from "@extollo/di";
import {uuid_v4} from "@extollo/util";
import {HTTPKernel} from "../HTTPKernel";
import {Request} from "../../lifecycle/Request";
import {Logging} from "../../../service/Logging";
@Injectable()
export class SetSessionCookieHTTPModule extends HTTPKernelModule {
@Inject()
protected readonly logging!: Logging
public static register(kernel: HTTPKernel) {
kernel.register(this).first() // TODO make this before inject session?
}
public async apply(request: Request) {
if ( !request.cookies.has('extollo.session') ) {
const session = `${uuid_v4()}-${uuid_v4()}`
this.logging.verbose(`Starting session: ${session}`)
request.cookies.set('extollo.session', session) // FIXME allow configuring this
}
return request
}
}