response lifecycle timeout and route handling
This commit is contained in:
@@ -5,7 +5,7 @@ export class HTTPError extends ErrorWithContext {
|
||||
public readonly status: HTTPStatus = 500,
|
||||
public readonly message: string = ''
|
||||
) {
|
||||
super(message || HTTPMessage[status])
|
||||
super('HTTP ERROR')
|
||||
this.message = message || HTTPMessage[status]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import {Instantiable, Singleton, Inject} from "@extollo/di"
|
||||
import {Collection} from "@extollo/util"
|
||||
import {Inject, Instantiable, Singleton} from "@extollo/di"
|
||||
import {Collection, HTTPStatus} from "@extollo/util"
|
||||
import {HTTPKernelModule} from "./HTTPKernelModule";
|
||||
import {Logging} from "../../service/Logging";
|
||||
import {AppClass} from "../../lifecycle/AppClass";
|
||||
import {Request} from "../lifecycle/Request";
|
||||
import {http} from "../response/HTTPErrorResponseFactory";
|
||||
|
||||
/**
|
||||
* Interface for fluently registering kernel modules into the kernel.
|
||||
@@ -72,11 +73,10 @@ export class HTTPKernel extends AppClass {
|
||||
}
|
||||
} catch (e: any) {
|
||||
this.logging.error(e)
|
||||
// FIXME handle error response
|
||||
// const error_response = error(e)
|
||||
// await error_response.write(request)
|
||||
await http(HTTPStatus.REQUEST_TIMEOUT).write(request)
|
||||
}
|
||||
|
||||
this.logging.verbose('Finished kernel lifecycle')
|
||||
return request
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import {HTTPKernelModule} from "../HTTPKernelModule";
|
||||
import {HTTPKernel} from "../HTTPKernel";
|
||||
import {Request} from "../../lifecycle/Request";
|
||||
import {ActivatedRoute} from "../../routing/ActivatedRoute";
|
||||
import {ResponseObject} from "../../routing/Route";
|
||||
import {plaintext} from "../../response/StringResponseFactory";
|
||||
import {ResponseFactory} from "../../response/ResponseFactory";
|
||||
import {json} from "../../response/JSONResponseFactory";
|
||||
import {http} from "../../response/HTTPErrorResponseFactory";
|
||||
import {HTTPStatus} from "@extollo/util";
|
||||
|
||||
export class ExecuteResolvedRouteHandlerHTTPModule extends HTTPKernelModule {
|
||||
public static register(kernel: HTTPKernel) {
|
||||
kernel.register(this).core()
|
||||
}
|
||||
|
||||
public async apply(request: Request) {
|
||||
if ( request.hasInstance(ActivatedRoute) ) {
|
||||
const route = <ActivatedRoute> request.make(ActivatedRoute)
|
||||
let object: ResponseObject = await route.handler(request)
|
||||
|
||||
if ( (typeof object === 'string') || (typeof object === 'number') ) {
|
||||
object = plaintext(String(object))
|
||||
}
|
||||
|
||||
if ( object instanceof ResponseFactory ) {
|
||||
await object.write(request)
|
||||
} else if ( typeof object !== 'undefined' ) {
|
||||
await json(object).write(request)
|
||||
} else {
|
||||
await plaintext('').write(request)
|
||||
}
|
||||
} else {
|
||||
await http(HTTPStatus.NOT_FOUND).write(request)
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import {Request} from "./Request";
|
||||
import {ErrorWithContext, HTTPStatus} from "@extollo/util"
|
||||
import {ErrorWithContext, HTTPStatus, BehaviorSubject} from "@extollo/util"
|
||||
import {ServerResponse} from "http"
|
||||
|
||||
export class HeadersAlreadySentError extends ErrorWithContext {
|
||||
@@ -20,7 +20,9 @@ export class Response {
|
||||
private _sentHeaders: boolean = false
|
||||
private _responseEnded: boolean = false
|
||||
private _status: HTTPStatus = HTTPStatus.OK
|
||||
public body: any
|
||||
public body: string = ''
|
||||
public readonly sending$: BehaviorSubject<Response> = new BehaviorSubject<Response>()
|
||||
public readonly sent$: BehaviorSubject<Response> = new BehaviorSubject<Response>()
|
||||
|
||||
constructor(
|
||||
public readonly request: Request,
|
||||
@@ -96,9 +98,12 @@ export class Response {
|
||||
})
|
||||
}
|
||||
|
||||
public async send(data?: any) {
|
||||
await this.write(data ?? this.body ?? '')
|
||||
public async send() {
|
||||
await this.sending$.next(this)
|
||||
this.setHeader('Content-Length', String(this.body?.length ?? 0))
|
||||
await this.write(this.body ?? '')
|
||||
this.end()
|
||||
await this.sent$.next(this)
|
||||
}
|
||||
|
||||
public end() {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import {HTTPStatus} from "@extollo/util"
|
||||
import {Instantiable} from "@extollo/di"
|
||||
import {Request} from "../lifecycle/Request"
|
||||
|
||||
export abstract class ResponseFactory {
|
||||
|
||||
@@ -20,6 +20,8 @@ export class MemorySession extends Session {
|
||||
protected sessionID?: string
|
||||
protected data?: SessionData
|
||||
|
||||
constructor() { super() }
|
||||
|
||||
public getKey(): string {
|
||||
if ( !this.sessionID ) throw new NoSessionKeyError()
|
||||
return this.sessionID
|
||||
|
||||
@@ -17,6 +17,8 @@ export class SessionFactory extends AbstractFactory {
|
||||
protected readonly logging: Logging
|
||||
protected readonly config: Config
|
||||
|
||||
private static loggedMemorySessionWarningOnce = false
|
||||
|
||||
constructor() {
|
||||
super({})
|
||||
this.logging = Container.getContainer().make<Logging>(Logging)
|
||||
@@ -24,7 +26,6 @@ export class SessionFactory extends AbstractFactory {
|
||||
}
|
||||
|
||||
produce(dependencies: any[], parameters: any[]): Session {
|
||||
this.logging.warn(`You are using the default memory-based session driver. It is recommended you configure a persistent session driver instead.`)
|
||||
return new (this.getSessionClass())
|
||||
}
|
||||
|
||||
@@ -53,8 +54,11 @@ export class SessionFactory extends AbstractFactory {
|
||||
|
||||
protected getSessionClass() {
|
||||
const SessionClass = this.config.get('server.session.driver', MemorySession)
|
||||
if ( SessionClass === MemorySession && !SessionFactory.loggedMemorySessionWarningOnce ) {
|
||||
this.logging.warn(`You are using the default memory-based session driver. It is recommended you configure a persistent session driver instead.`)
|
||||
SessionFactory.loggedMemorySessionWarningOnce = true
|
||||
}
|
||||
|
||||
// TODO check that session class is valid
|
||||
if ( !isInstantiable(SessionClass) || !(SessionClass.prototype instanceof Session) ) {
|
||||
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Session');
|
||||
e.context = {
|
||||
|
||||
Reference in New Issue
Block a user