make HTTP server unit more configurable
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
aca4c8aa4d
commit
574ddbe9cb
@ -32,7 +32,7 @@ export class ParseIncomingBodyHTTPModule extends HTTPKernelModule {
|
|||||||
public async apply(request: Request): Promise<Request> {
|
public async apply(request: Request): Promise<Request> {
|
||||||
const contentType = request.getHeader('content-type')
|
const contentType = request.getHeader('content-type')
|
||||||
const contentTypes = (Array.isArray(contentType) ? contentType : [contentType])
|
const contentTypes = (Array.isArray(contentType) ? contentType : [contentType])
|
||||||
.filter(Boolean).map(x => x!.toLowerCase())
|
.filter(Boolean).map(x => x!.toLowerCase().split(';')[0])
|
||||||
if ( !contentType ) return request
|
if ( !contentType ) return request
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -49,6 +49,10 @@ export class ParseIncomingBodyHTTPModule extends HTTPKernelModule {
|
|||||||
return request
|
return request
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the request body as JSON.
|
||||||
|
* @param request
|
||||||
|
*/
|
||||||
public async applyJSON(request: Request): Promise<Request> {
|
public async applyJSON(request: Request): Promise<Request> {
|
||||||
await new Promise<void>((res, rej) => {
|
await new Promise<void>((res, rej) => {
|
||||||
let data = ''
|
let data = ''
|
||||||
@ -74,6 +78,10 @@ export class ParseIncomingBodyHTTPModule extends HTTPKernelModule {
|
|||||||
return request
|
return request
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the request body using Busboy. This assumes the request contents are multipart.
|
||||||
|
* @param request
|
||||||
|
*/
|
||||||
public async applyBusboy(request: Request): Promise<Request> {
|
public async applyBusboy(request: Request): Promise<Request> {
|
||||||
const config = this.config.get('server.uploads', {})
|
const config = this.config.get('server.uploads', {})
|
||||||
|
|
||||||
|
@ -90,6 +90,9 @@ export class Request extends ScopedContainer {
|
|||||||
/** Files parsed from the request. */
|
/** Files parsed from the request. */
|
||||||
public readonly uploadedFiles: {[key: string]: UniversalPath} = {}
|
public readonly uploadedFiles: {[key: string]: UniversalPath} = {}
|
||||||
|
|
||||||
|
/** If true, the response lifecycle will not time out and send errors. */
|
||||||
|
public bypassTimeout: boolean = false
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
/** The native Node.js request. */
|
/** The native Node.js request. */
|
||||||
protected clientRequest: IncomingMessage,
|
protected clientRequest: IncomingMessage,
|
||||||
|
@ -15,6 +15,7 @@ import {error} from "../http/response/ErrorResponseFactory";
|
|||||||
import {ExecuteResolvedRoutePreflightHTTPModule} from "../http/kernel/module/ExecuteResolvedRoutePreflightHTTPModule";
|
import {ExecuteResolvedRoutePreflightHTTPModule} from "../http/kernel/module/ExecuteResolvedRoutePreflightHTTPModule";
|
||||||
import {ExecuteResolvedRoutePostflightHTTPModule} from "../http/kernel/module/ExecuteResolvedRoutePostflightHTTPModule";
|
import {ExecuteResolvedRoutePostflightHTTPModule} from "../http/kernel/module/ExecuteResolvedRoutePostflightHTTPModule";
|
||||||
import {ParseIncomingBodyHTTPModule} from "../http/kernel/module/ParseIncomingBodyHTTPModule";
|
import {ParseIncomingBodyHTTPModule} from "../http/kernel/module/ParseIncomingBodyHTTPModule";
|
||||||
|
import {Config} from "./Config";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application unit that starts the HTTP/S server, creates Request and Response objects
|
* Application unit that starts the HTTP/S server, creates Request and Response objects
|
||||||
@ -25,6 +26,9 @@ export class HTTPServer extends Unit {
|
|||||||
@Inject()
|
@Inject()
|
||||||
protected readonly logging!: Logging
|
protected readonly logging!: Logging
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
protected readonly config!: Config
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
protected readonly kernel!: HTTPKernel
|
protected readonly kernel!: HTTPKernel
|
||||||
|
|
||||||
@ -32,7 +36,7 @@ export class HTTPServer extends Unit {
|
|||||||
protected server?: Server
|
protected server?: Server
|
||||||
|
|
||||||
public async up() {
|
public async up() {
|
||||||
const port = 8000
|
const port = this.config.get('server.port', 8000)
|
||||||
|
|
||||||
// TODO register these by config
|
// TODO register these by config
|
||||||
PoweredByHeaderInjectionHTTPModule.register(this.kernel)
|
PoweredByHeaderInjectionHTTPModule.register(this.kernel)
|
||||||
@ -68,18 +72,26 @@ export class HTTPServer extends Unit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get handler() {
|
public get handler() {
|
||||||
|
const timeout = this.config.get('server.timeout', 10000)
|
||||||
|
|
||||||
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(timeout, extolloReq.response.sent$.toPromise())
|
||||||
withTimeout(10000, extolloReq.response.sent$.toPromise())
|
|
||||||
.onTime(req => {
|
.onTime(req => {
|
||||||
this.logging.verbose(`Request lifecycle finished on time. (Path: ${extolloReq.path})`)
|
this.logging.verbose(`Request lifecycle finished on time. (Path: ${extolloReq.path})`)
|
||||||
})
|
})
|
||||||
.late(req => {
|
.late(req => {
|
||||||
|
if ( !extolloReq.bypassTimeout ) {
|
||||||
this.logging.warn(`Request lifecycle finished late, so an error response was returned! (Path: ${extolloReq.path})`)
|
this.logging.warn(`Request lifecycle finished late, so an error response was returned! (Path: ${extolloReq.path})`)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.timeout(() => {
|
.timeout(() => {
|
||||||
|
if ( extolloReq.bypassTimeout ) {
|
||||||
|
this.logging.info(`Request lifecycle has timed out, but bypassRequest was set. (Path: ${extolloReq.path})`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this.logging.error(`Request lifecycle has timed out. Will send error response instead. (Path: ${extolloReq.path})`)
|
this.logging.error(`Request lifecycle has timed out. Will send error response instead. (Path: ${extolloReq.path})`)
|
||||||
extolloReq.response.setStatus(HTTPStatus.REQUEST_TIMEOUT)
|
extolloReq.response.setStatus(HTTPStatus.REQUEST_TIMEOUT)
|
||||||
extolloReq.response.body = 'Sorry, your request timed out.'
|
extolloReq.response.body = 'Sorry, your request timed out.'
|
||||||
|
Loading…
Reference in New Issue
Block a user