Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #64 - Document methods in the repo #82

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/dwn-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class DwnServer {
callback?.();
}

/**
* Handler for creating a DWN if none exists.
KhoiUna marked this conversation as resolved.
Show resolved Hide resolved
* Handler also starts an HTTP server.
* If `webSocketServerEnabled` is true, this handler also starts a WebSocketServer.
*/
async #setupServer(): Promise<void> {
if (!this.dwn) {
this.dwn = await Dwn.create(getDWNConfig(this.config));
Expand Down
3 changes: 3 additions & 0 deletions src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export class HttpApi {
);
}

/* Handler for setting up Express API routes
* API routes for `/`, `/health`, `/metrics`, `/:did/records/:id`
EbonyLouis marked this conversation as resolved.
Show resolved Hide resolved
*/
#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