response lifecycle timeout and route handling
This commit is contained in:
parent
9747d40659
commit
4ecada6be8
@ -5,7 +5,7 @@ export class HTTPError extends ErrorWithContext {
|
|||||||
public readonly status: HTTPStatus = 500,
|
public readonly status: HTTPStatus = 500,
|
||||||
public readonly message: string = ''
|
public readonly message: string = ''
|
||||||
) {
|
) {
|
||||||
super(message || HTTPMessage[status])
|
super('HTTP ERROR')
|
||||||
this.message = message || HTTPMessage[status]
|
this.message = message || HTTPMessage[status]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import {Instantiable, Singleton, Inject} from "@extollo/di"
|
import {Inject, Instantiable, Singleton} from "@extollo/di"
|
||||||
import {Collection} from "@extollo/util"
|
import {Collection, HTTPStatus} from "@extollo/util"
|
||||||
import {HTTPKernelModule} from "./HTTPKernelModule";
|
import {HTTPKernelModule} from "./HTTPKernelModule";
|
||||||
import {Logging} from "../../service/Logging";
|
import {Logging} from "../../service/Logging";
|
||||||
import {AppClass} from "../../lifecycle/AppClass";
|
import {AppClass} from "../../lifecycle/AppClass";
|
||||||
import {Request} from "../lifecycle/Request";
|
import {Request} from "../lifecycle/Request";
|
||||||
|
import {http} from "../response/HTTPErrorResponseFactory";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface for fluently registering kernel modules into the kernel.
|
* Interface for fluently registering kernel modules into the kernel.
|
||||||
@ -72,11 +73,10 @@ export class HTTPKernel extends AppClass {
|
|||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this.logging.error(e)
|
this.logging.error(e)
|
||||||
// FIXME handle error response
|
await http(HTTPStatus.REQUEST_TIMEOUT).write(request)
|
||||||
// const error_response = error(e)
|
|
||||||
// await error_response.write(request)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logging.verbose('Finished kernel lifecycle')
|
||||||
return request
|
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 {Request} from "./Request";
|
||||||
import {ErrorWithContext, HTTPStatus} from "@extollo/util"
|
import {ErrorWithContext, HTTPStatus, BehaviorSubject} from "@extollo/util"
|
||||||
import {ServerResponse} from "http"
|
import {ServerResponse} from "http"
|
||||||
|
|
||||||
export class HeadersAlreadySentError extends ErrorWithContext {
|
export class HeadersAlreadySentError extends ErrorWithContext {
|
||||||
@ -20,7 +20,9 @@ export class Response {
|
|||||||
private _sentHeaders: boolean = false
|
private _sentHeaders: boolean = false
|
||||||
private _responseEnded: boolean = false
|
private _responseEnded: boolean = false
|
||||||
private _status: HTTPStatus = HTTPStatus.OK
|
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(
|
constructor(
|
||||||
public readonly request: Request,
|
public readonly request: Request,
|
||||||
@ -96,9 +98,12 @@ export class Response {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public async send(data?: any) {
|
public async send() {
|
||||||
await this.write(data ?? this.body ?? '')
|
await this.sending$.next(this)
|
||||||
|
this.setHeader('Content-Length', String(this.body?.length ?? 0))
|
||||||
|
await this.write(this.body ?? '')
|
||||||
this.end()
|
this.end()
|
||||||
|
await this.sent$.next(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
public end() {
|
public end() {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import {HTTPStatus} from "@extollo/util"
|
import {HTTPStatus} from "@extollo/util"
|
||||||
import {Instantiable} from "@extollo/di"
|
|
||||||
import {Request} from "../lifecycle/Request"
|
import {Request} from "../lifecycle/Request"
|
||||||
|
|
||||||
export abstract class ResponseFactory {
|
export abstract class ResponseFactory {
|
||||||
|
@ -20,6 +20,8 @@ export class MemorySession extends Session {
|
|||||||
protected sessionID?: string
|
protected sessionID?: string
|
||||||
protected data?: SessionData
|
protected data?: SessionData
|
||||||
|
|
||||||
|
constructor() { super() }
|
||||||
|
|
||||||
public getKey(): string {
|
public getKey(): string {
|
||||||
if ( !this.sessionID ) throw new NoSessionKeyError()
|
if ( !this.sessionID ) throw new NoSessionKeyError()
|
||||||
return this.sessionID
|
return this.sessionID
|
||||||
|
@ -17,6 +17,8 @@ export class SessionFactory extends AbstractFactory {
|
|||||||
protected readonly logging: Logging
|
protected readonly logging: Logging
|
||||||
protected readonly config: Config
|
protected readonly config: Config
|
||||||
|
|
||||||
|
private static loggedMemorySessionWarningOnce = false
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super({})
|
super({})
|
||||||
this.logging = Container.getContainer().make<Logging>(Logging)
|
this.logging = Container.getContainer().make<Logging>(Logging)
|
||||||
@ -24,7 +26,6 @@ export class SessionFactory extends AbstractFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
produce(dependencies: any[], parameters: any[]): Session {
|
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())
|
return new (this.getSessionClass())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,8 +54,11 @@ export class SessionFactory extends AbstractFactory {
|
|||||||
|
|
||||||
protected getSessionClass() {
|
protected getSessionClass() {
|
||||||
const SessionClass = this.config.get('server.session.driver', MemorySession)
|
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) ) {
|
if ( !isInstantiable(SessionClass) || !(SessionClass.prototype instanceof Session) ) {
|
||||||
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Session');
|
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Session');
|
||||||
e.context = {
|
e.context = {
|
||||||
|
@ -5,6 +5,8 @@ export * from './lifecycle/Application'
|
|||||||
export * from './lifecycle/AppClass'
|
export * from './lifecycle/AppClass'
|
||||||
export * from './lifecycle/Unit'
|
export * from './lifecycle/Unit'
|
||||||
|
|
||||||
|
export * from './http/HTTPError'
|
||||||
|
|
||||||
export * from './http/kernel/module/InjectSessionHTTPModule'
|
export * from './http/kernel/module/InjectSessionHTTPModule'
|
||||||
export * from './http/kernel/module/MountActivatedRouteHTTPModule'
|
export * from './http/kernel/module/MountActivatedRouteHTTPModule'
|
||||||
export * from './http/kernel/module/PersistSessionHTTPModule'
|
export * from './http/kernel/module/PersistSessionHTTPModule'
|
||||||
@ -32,12 +34,13 @@ export * from './http/routing/ActivatedRoute'
|
|||||||
export * from './http/routing/Route'
|
export * from './http/routing/Route'
|
||||||
export * from './http/routing/RouteGroup'
|
export * from './http/routing/RouteGroup'
|
||||||
|
|
||||||
|
export * from './http/kernel/module/ExecuteResolvedRouteHandlerHTTPModule'
|
||||||
|
|
||||||
export * from './http/session/Session'
|
export * from './http/session/Session'
|
||||||
export * from './http/session/SessionFactory'
|
export * from './http/session/SessionFactory'
|
||||||
export * from './http/session/MemorySession'
|
export * from './http/session/MemorySession'
|
||||||
|
|
||||||
export * from './http/Controller'
|
export * from './http/Controller'
|
||||||
export * from './http/HTTPError'
|
|
||||||
|
|
||||||
export * from './service/Canonical'
|
export * from './service/Canonical'
|
||||||
export * from './service/CanonicalInstantiable'
|
export * from './service/CanonicalInstantiable'
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import {Singleton, Inject} from "@extollo/di"
|
import {Inject, Singleton} from "@extollo/di"
|
||||||
|
import {HTTPStatus, withTimeout} from "@extollo/util"
|
||||||
import {Unit} from "../lifecycle/Unit";
|
import {Unit} from "../lifecycle/Unit";
|
||||||
import {createServer, IncomingMessage, ServerResponse, Server} from "http";
|
import {createServer, IncomingMessage, Server, ServerResponse} from "http";
|
||||||
import {Logging} from "./Logging";
|
import {Logging} from "./Logging";
|
||||||
import {Request} from "../http/lifecycle/Request";
|
import {Request} from "../http/lifecycle/Request";
|
||||||
import {HTTPKernel} from "../http/kernel/HTTPKernel";
|
import {HTTPKernel} from "../http/kernel/HTTPKernel";
|
||||||
@ -9,6 +10,7 @@ import {SetSessionCookieHTTPModule} from "../http/kernel/module/SetSessionCookie
|
|||||||
import {InjectSessionHTTPModule} from "../http/kernel/module/InjectSessionHTTPModule";
|
import {InjectSessionHTTPModule} from "../http/kernel/module/InjectSessionHTTPModule";
|
||||||
import {PersistSessionHTTPModule} from "../http/kernel/module/PersistSessionHTTPModule";
|
import {PersistSessionHTTPModule} from "../http/kernel/module/PersistSessionHTTPModule";
|
||||||
import {MountActivatedRouteHTTPModule} from "../http/kernel/module/MountActivatedRouteHTTPModule";
|
import {MountActivatedRouteHTTPModule} from "../http/kernel/module/MountActivatedRouteHTTPModule";
|
||||||
|
import {ExecuteResolvedRouteHandlerHTTPModule} from "../http/kernel/module/ExecuteResolvedRouteHandlerHTTPModule";
|
||||||
|
|
||||||
@Singleton()
|
@Singleton()
|
||||||
export class HTTPServer extends Unit {
|
export class HTTPServer extends Unit {
|
||||||
@ -29,6 +31,7 @@ export class HTTPServer extends Unit {
|
|||||||
InjectSessionHTTPModule.register(this.kernel)
|
InjectSessionHTTPModule.register(this.kernel)
|
||||||
PersistSessionHTTPModule.register(this.kernel)
|
PersistSessionHTTPModule.register(this.kernel)
|
||||||
MountActivatedRouteHTTPModule.register(this.kernel)
|
MountActivatedRouteHTTPModule.register(this.kernel)
|
||||||
|
ExecuteResolvedRouteHandlerHTTPModule.register(this.kernel)
|
||||||
|
|
||||||
await new Promise<void>((res, rej) => {
|
await new Promise<void>((res, rej) => {
|
||||||
this.server = createServer(this.handler)
|
this.server = createServer(this.handler)
|
||||||
@ -55,8 +58,26 @@ export class HTTPServer extends Unit {
|
|||||||
public get handler() {
|
public get handler() {
|
||||||
return async (request: IncomingMessage, response: ServerResponse) => {
|
return async (request: IncomingMessage, response: ServerResponse) => {
|
||||||
const extolloReq = new Request(request, response)
|
const extolloReq = new Request(request, response)
|
||||||
|
|
||||||
|
// FIXME make timeout configurable
|
||||||
|
withTimeout(10000, extolloReq.response.sent$.toPromise())
|
||||||
|
.onTime(req => {
|
||||||
|
this.logging.verbose(`Request lifecycle finished on time. (Path: ${extolloReq.path})`)
|
||||||
|
})
|
||||||
|
.late(req => {
|
||||||
|
this.logging.warn(`Request lifecycle finished late, so an error response was returned! (Path: ${extolloReq.path})`)
|
||||||
|
})
|
||||||
|
.timeout(() => {
|
||||||
|
this.logging.error(`Request lifecycle has timed out. Will send error response instead. (Path: ${extolloReq.path})`)
|
||||||
|
extolloReq.response.setStatus(HTTPStatus.REQUEST_TIMEOUT)
|
||||||
|
extolloReq.response.body = 'Sorry, your request timed out.'
|
||||||
|
extolloReq.response.send()
|
||||||
|
})
|
||||||
|
.run()
|
||||||
|
.catch(e => this.logging.error(e))
|
||||||
|
|
||||||
await this.kernel.handle(extolloReq)
|
await this.kernel.handle(extolloReq)
|
||||||
await extolloReq.response.send('Hi, from Extollo!!')
|
await extolloReq.response.send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"target": "es5",
|
"target": "es6",
|
||||||
"sourceMap": true
|
"sourceMap": true
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es5",
|
"target": "es6",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"outDir": "./lib",
|
"outDir": "./lib",
|
||||||
|
Loading…
Reference in New Issue
Block a user