From fdcd80a43edfa0cef1470487b7bdb7fdd16de5df Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sun, 7 Mar 2021 12:51:04 -0600 Subject: [PATCH] Add kernel modules and basic kernel handling --- src/http/kernel/HTTPCookieJar.ts | 6 +++++ ... => PoweredByHeaderInjectionHTTPModule.ts} | 9 ++++--- .../module/SetSessionCookieHTTPModule.ts | 26 +++++++++++++++++++ src/http/lifecycle/Request.ts | 4 --- src/lifecycle/Application.ts | 8 +++--- src/service/HTTPServer.ts | 17 +++++++++--- 6 files changed, 56 insertions(+), 14 deletions(-) rename src/http/kernel/module/{PrepareRequestHTTPModule.ts => PoweredByHeaderInjectionHTTPModule.ts} (50%) create mode 100644 src/http/kernel/module/SetSessionCookieHTTPModule.ts diff --git a/src/http/kernel/HTTPCookieJar.ts b/src/http/kernel/HTTPCookieJar.ts index 98c5704..e66def6 100644 --- a/src/http/kernel/HTTPCookieJar.ts +++ b/src/http/kernel/HTTPCookieJar.ts @@ -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)}`) diff --git a/src/http/kernel/module/PrepareRequestHTTPModule.ts b/src/http/kernel/module/PoweredByHeaderInjectionHTTPModule.ts similarity index 50% rename from src/http/kernel/module/PrepareRequestHTTPModule.ts rename to src/http/kernel/module/PoweredByHeaderInjectionHTTPModule.ts index 521a489..9fd1b37 100644 --- a/src/http/kernel/module/PrepareRequestHTTPModule.ts +++ b/src/http/kernel/module/PoweredByHeaderInjectionHTTPModule.ts @@ -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 } } diff --git a/src/http/kernel/module/SetSessionCookieHTTPModule.ts b/src/http/kernel/module/SetSessionCookieHTTPModule.ts new file mode 100644 index 0000000..cfb8f55 --- /dev/null +++ b/src/http/kernel/module/SetSessionCookieHTTPModule.ts @@ -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 + } +} diff --git a/src/http/lifecycle/Request.ts b/src/http/lifecycle/Request.ts index 2d785b5..09ae976 100644 --- a/src/http/lifecycle/Request.ts +++ b/src/http/lifecycle/Request.ts @@ -100,10 +100,6 @@ export class Request extends ScopedContainer { this.response = new Response(this, serverResponse) } - public async prepare() { - - } - public getHeader(name: string) { return this.clientRequest.headers[name.toLowerCase()] } diff --git a/src/lifecycle/Application.ts b/src/lifecycle/Application.ts index 63be160..75f4bc7 100644 --- a/src/lifecycle/Application.ts +++ b/src/lifecycle/Application.ts @@ -33,19 +33,19 @@ export class Application extends Container { public static getApplication(): Application { const existing = globalRegistry.getGlobal('extollo/injector') - if ( existing && !(existing instanceof Application) ) { + if ( existing instanceof Application ) { + return existing + } else if ( existing ) { const app = new Application() existing.cloneTo(app) globalRegistry.setGlobal('extollo/injector', app) return app - } else if ( !existing ) { + } else { const app = new Application() globalRegistry.setGlobal('extollo/injector', app) return app } - - return existing } protected baseDir!: string diff --git a/src/service/HTTPServer.ts b/src/service/HTTPServer.ts index 39aa774..f828848 100644 --- a/src/service/HTTPServer.ts +++ b/src/service/HTTPServer.ts @@ -3,17 +3,27 @@ import {Unit} from "../lifecycle/Unit"; import {createServer, IncomingMessage, ServerResponse, Server} from "http"; import {Logging} from "./Logging"; import {Request} from "../http/lifecycle/Request"; +import {HTTPKernel} from "../http/kernel/HTTPKernel"; +import {PoweredByHeaderInjectionHTTPModule} from "../http/kernel/module/PoweredByHeaderInjectionHTTPModule"; +import {SetSessionCookieHTTPModule} from "../http/kernel/module/SetSessionCookieHTTPModule"; @Singleton() export class HTTPServer extends Unit { @Inject() protected readonly logging!: Logging + @Inject() + protected readonly kernel!: HTTPKernel + protected server?: Server public async up() { const port = 8000 + // TODO register these by config + PoweredByHeaderInjectionHTTPModule.register(this.kernel) + SetSessionCookieHTTPModule.register(this.kernel) + await new Promise((res, rej) => { this.server = createServer(this.handler) @@ -37,10 +47,11 @@ export class HTTPServer extends Unit { } public get handler() { - return (request: IncomingMessage, response: ServerResponse) => { + return async (request: IncomingMessage, response: ServerResponse) => { const extolloReq = new Request(request, response) - extolloReq.cookies.set('testing123', {foo: 'bar', bob: 123}) - extolloReq.response.send('Hi, from Extollo!!') + + await this.kernel.handle(extolloReq) + await extolloReq.response.send('Hi, from Extollo!!') } }