Add foreground service, some cleanup, and start websocket server
This commit is contained in:
@@ -107,7 +107,7 @@ export class Request extends ScopedContainer implements DataContainer {
|
||||
protected clientRequest: IncomingMessage,
|
||||
|
||||
/** The native Node.js response. */
|
||||
protected serverResponse: ServerResponse,
|
||||
protected serverResponse?: ServerResponse,
|
||||
) {
|
||||
super(Container.getContainer())
|
||||
this.registerSingletonInstance(Request, this)
|
||||
|
||||
@@ -66,7 +66,7 @@ export class Response {
|
||||
public readonly request: Request,
|
||||
|
||||
/** The native Node.js ServerResponse. */
|
||||
protected readonly serverResponse: ServerResponse,
|
||||
protected readonly serverResponse?: ServerResponse,
|
||||
) { }
|
||||
|
||||
protected get logging(): Logging {
|
||||
@@ -173,6 +173,11 @@ export class Response {
|
||||
*/
|
||||
public sendHeaders(): this {
|
||||
this.logging.verbose(`Sending headers...`)
|
||||
if ( !this.serverResponse ) {
|
||||
throw new ErrorWithContext('Unable to send headers: Response has no underlying connection.', {
|
||||
suggestion: 'This usually means the Request was created by an alternative server, like WebsocketServer. You should use that server to handle the request.',
|
||||
})
|
||||
}
|
||||
const headers = {} as any
|
||||
|
||||
const setCookieHeaders = this.cookies.getSetCookieHeaders()
|
||||
@@ -220,8 +225,14 @@ 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})`)
|
||||
this.logging.verbose(`Writing headers & data to response... (destroyed? ${!this.serverResponse || this.serverResponse.destroyed})`)
|
||||
return new Promise<void>((res, rej) => {
|
||||
if ( !this.serverResponse ) {
|
||||
throw new ErrorWithContext('Unable to write response: Response has no underlying connection.', {
|
||||
suggestion: 'This usually means the Request was created by an alternative server, like WebsocketServer. You should use that server to handle the request.',
|
||||
})
|
||||
}
|
||||
|
||||
if ( this.responseEnded || this.serverResponse.destroyed ) {
|
||||
throw new ErrorWithContext('Tried to write to Response after lifecycle ended.')
|
||||
}
|
||||
@@ -274,7 +285,7 @@ export class Response {
|
||||
* or the connection has been destroyed.
|
||||
*/
|
||||
public canSend(): boolean {
|
||||
return !(this.responseEnded || this.serverResponse.destroyed)
|
||||
return !(this.responseEnded || !this.serverResponse || this.serverResponse.destroyed)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -286,7 +297,7 @@ export class Response {
|
||||
}
|
||||
|
||||
this.sentHeaders = true
|
||||
this.serverResponse.end()
|
||||
this.serverResponse?.end()
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user