Improve Response verbose/debug logging

This commit is contained in:
2022-03-30 21:38:25 -05:00
parent 10b3e1ecc3
commit 6f66126d38
4 changed files with 16 additions and 4 deletions

View File

@@ -100,6 +100,7 @@ export class Response {
/** Set the value of the response header. */
public setHeader(name: string, value: string | string[]): this {
this.logging.verbose(`Will set header on response: ${name}`)
if ( this.sentHeaders ) {
throw new HeadersAlreadySentError(this, name)
}
@@ -113,6 +114,7 @@ export class Response {
* @param data
*/
public setHeaders(data: {[name: string]: string | string[]}): this {
this.logging.verbose(`Will set headers on response: ${Object.keys(data).join(', ')}`)
if ( this.sentHeaders ) {
throw new HeadersAlreadySentError(this)
}
@@ -128,12 +130,16 @@ export class Response {
* @param value
*/
public appendHeader(name: string, value: string | string[]): this {
this.logging.verbose(`Will append header: ${name}`)
if ( this.sentHeaders ) {
throw new HeadersAlreadySentError(this, name)
}
if ( !Array.isArray(value) ) {
value = [value]
}
let existing = this.headers[name] ?? []
if ( !Array.isArray(existing) ) {
existing = [existing]
@@ -152,6 +158,7 @@ export class Response {
* Write the headers to the client.
*/
public sendHeaders(): this {
this.logging.verbose(`Sending headers...`)
const headers = {} as any
const setCookieHeaders = this.cookies.getSetCookieHeaders()
@@ -199,6 +206,7 @@ export class Response {
* @param data
*/
public async write(data: string | Buffer | Uint8Array | Readable): Promise<void> {
this.logging.verbose(`Writing headers & data to response... (destroyed? ${this.serverResponse.destroyed})`)
return new Promise<void>((res, rej) => {
if ( this.responseEnded || this.serverResponse.destroyed ) {
throw new ErrorWithContext('Tried to write to Response after lifecycle ended.')