From 64bff7cc0f5f544e67f3154775cf22b17bf846eb Mon Sep 17 00:00:00 2001 From: Khoi Nguyen <63745633+KhoiUna@users.noreply.github.com> Date: Tue, 31 Oct 2023 11:09:02 -0500 Subject: [PATCH] Issue #64 - Document methods in the repo (#82) * remove comment since issue #49 already fixed * add method doc for `#setupServer` * add method doc for `#setupRoutes` * add method doc for setupHeartbeat and setupWebSocket * update doc for `#setupRoutes` in src/http-api.ts * update doc for `#setupServer` in `src/dwn-server.ts` --- src/dwn-server.ts | 4 ++++ src/http-api.ts | 2 ++ src/ws-api.ts | 10 +++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/dwn-server.ts b/src/dwn-server.ts index 53a3a4f..c54c2a3 100644 --- a/src/dwn-server.ts +++ b/src/dwn-server.ts @@ -41,6 +41,10 @@ export class DwnServer { callback?.(); } + /** + * Function to setup the servers (HTTP and WebSocket) + * The DWN creation is secondary and only happens if it hasn't already been done. + */ async #setupServer(): Promise { if (!this.dwn) { this.dwn = await Dwn.create(getDWNConfig(this.config)); diff --git a/src/http-api.ts b/src/http-api.ts index dbe1038..d40d90e 100644 --- a/src/http-api.ts +++ b/src/http-api.ts @@ -63,6 +63,8 @@ export class HttpApi { ); } + /* setupRoutes configures the HTTP server's request handlers + */ #setupRoutes(): void { this.#api.get('/health', (_req, res) => { // return 200 ok diff --git a/src/ws-api.ts b/src/ws-api.ts index d63035b..cac1cea 100644 --- a/src/ws-api.ts +++ b/src/ws-api.ts @@ -27,7 +27,6 @@ export class WsApi { this.#wsServer = new WebSocketServer({ server }); } - // TODO: github.com/TBD54566975/dwn-server/issues/49 Add code coverage tracker, similar to either dwn-sdk-js or to web5-js get address(): AddressInfo | string { return this.#wsServer.address(); } @@ -122,6 +121,10 @@ export class WsApi { }); } + /** + * This handler returns an interval to ping clients' socket every 30s + * if a pong hasn't received from a socket by the next ping, the server will terminate the socket connection. + */ #setupHeartbeat(): NodeJS.Timer { // Sometimes connections between client <-> server can get borked in such a way that // leaves both unaware of the borkage. ping messages can be used as a means to verify @@ -140,6 +143,11 @@ export class WsApi { }, HEARTBEAT_INTERVAL); } + /** + * Handler for starting a WebSocket. + * Sets listeners for `connection`, `close` events. + * It clears `heartbeatInterval` when a `close` event is made. + */ #setupWebSocket(): void { this.#wsServer.on('connection', this.#handleConnection.bind(this));