Skip to content

Commit

Permalink
Issue #64 - Document methods in the repo (#82)
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
KhoiUna authored Oct 31, 2023
1 parent eee2559 commit 64bff7c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/dwn-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
if (!this.dwn) {
this.dwn = await Dwn.create(getDWNConfig(this.config));
Expand Down
2 changes: 2 additions & 0 deletions src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/ws-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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));

Expand Down

0 comments on commit 64bff7c

Please sign in to comment.