response lifecycle timeout and route handling

This commit is contained in:
2021-03-08 12:34:31 -06:00
parent 9747d40659
commit 4ecada6be8
11 changed files with 92 additions and 19 deletions

View File

@@ -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() {