Add foreground service, some cleanup, and start websocket server

This commit is contained in:
2022-07-13 21:35:18 -05:00
parent 4d7769de56
commit 6476416c67
12 changed files with 158 additions and 43 deletions

View File

@@ -136,6 +136,14 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
return new Route(method, endpoint)
}
/**
* Create a new WebSocket route on the given endpoint.
* @param endpoint
*/
public static socket(endpoint: string): Route<void, [void]> {
return new Route<void, [void]>('ws', endpoint)
}
/**
* Create a new GET route on the given endpoint.
*/
@@ -188,10 +196,14 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
protected displays: Collection<{stage: 'pre'|'post'|'handler', display: string}> = new Collection()
constructor(
protected method: HTTPMethod | HTTPMethod[],
protected method: 'ws' | HTTPMethod | HTTPMethod[],
protected route: string,
) {}
public isForWebSocket(): boolean {
return this.method === 'ws'
}
/**
* Set a programmatic name for this route.
* @param name
@@ -212,6 +224,10 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
* Get the string-form methods supported by the route.
*/
public getMethods(): HTTPMethod[] {
if ( this.method === 'ws' ) {
return []
}
if ( !Array.isArray(this.method) ) {
return [this.method]
}
@@ -250,10 +266,14 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
* @param method
* @param potential
*/
public match(method: HTTPMethod, potential: string): boolean {
if ( Array.isArray(this.method) && !this.method.includes(method) ) {
public match(method: 'ws' | HTTPMethod, potential: string): boolean {
if ( method === 'ws' && !this.isForWebSocket() ) {
return false
} else if ( !Array.isArray(this.method) && this.method !== method ) {
} else if ( method !== 'ws' && this.isForWebSocket() ) {
return false
} else if ( method !== 'ws' && Array.isArray(this.method) && !this.method.includes(method) ) {
return false
} else if ( method !== 'ws' && !Array.isArray(this.method) && this.method !== method ) {
return false
}