From 2a6a21560034d259264fde15122cf000e5960616 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 20 Feb 2024 16:47:12 +0100 Subject: [PATCH] refactor(cluster): add explicit types --- lib/cluster-adapter.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/cluster-adapter.ts b/lib/cluster-adapter.ts index 7482d03..fc63a75 100644 --- a/lib/cluster-adapter.ts +++ b/lib/cluster-adapter.ts @@ -19,6 +19,16 @@ type DistributiveOmit = T extends any ? Omit : never; +/** + * The unique ID of this server + */ +type ServerId = string; + +/** + * The id of a message (for the Connection state recovery) + */ +type Offset = string; + export interface ClusterAdapterOptions { /** * The number of ms between two heartbeats. @@ -49,7 +59,7 @@ export enum MessageType { } export type ClusterMessage = { - uid: string; + uid: ServerId; nsp: string; } & ( | { @@ -106,7 +116,7 @@ interface ClusterRequest { } export type ClusterResponse = { - uid: string; + uid: ServerId; nsp: string; } & ( | { @@ -167,7 +177,7 @@ function decodeOptions(opts): BroadcastOptions { * - call {@link ClusterAdapter#onMessage} and {@link ClusterAdapter#onResponse} */ export abstract class ClusterAdapter extends Adapter { - protected readonly uid: string; + protected readonly uid: ServerId; private requests: Map = new Map(); private ackRequests: Map = new Map(); @@ -418,7 +428,7 @@ export abstract class ClusterAdapter extends Adapter { private addOffsetIfNecessary( packet: any, opts: BroadcastOptions, - offset: string + offset: Offset ) { if (!this.nsp.server.opts.connectionStateRecovery) { return; @@ -646,10 +656,10 @@ export abstract class ClusterAdapter extends Adapter { * @protected * @return an offset, if applicable */ - protected abstract doPublish(message: ClusterMessage): Promise; + protected abstract doPublish(message: ClusterMessage): Promise; protected publishResponse( - requesterUid: string, + requesterUid: ServerId, response: Omit ) { (response as ClusterResponse).uid = this.uid; @@ -669,7 +679,7 @@ export abstract class ClusterAdapter extends Adapter { * @protected */ protected abstract doPublishResponse( - requesterUid: string, + requesterUid: ServerId, response: ClusterResponse ): Promise; } @@ -686,7 +696,7 @@ export abstract class ClusterAdapterWithHeartbeat extends ClusterAdapter { private readonly _opts: Required; private heartbeatTimer: NodeJS.Timeout; - private nodesMap: Map = new Map(); // uid => timestamp of last message + private nodesMap: Map = new Map(); // uid => timestamp of last message private readonly cleanupTimer: NodeJS.Timeout | undefined; private customRequests: Map = new Map(); @@ -941,7 +951,7 @@ export abstract class ClusterAdapterWithHeartbeat extends ClusterAdapter { } } - private removeNode(uid: string) { + private removeNode(uid: ServerId) { this.customRequests.forEach((request, requestId) => { request.missingUids.delete(uid); if (request.missingUids.size === 0) {