Skip to content

Commit 1193d5b

Browse files
authored
fix(client): include all replica states in ROLE reply type (#3420)
* fix(client): include all replica states in ROLE reply type * test(client): use two-space indentation in role states types test * fix(client): drop unreachable unknown from ROLE replica state union
1 parent 7ea1edd commit 1193d5b

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

packages/client/lib/commands/ROLE.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type SlaveRole = [
1717
role: BlobStringReply<'slave'>,
1818
masterHost: BlobStringReply,
1919
masterPort: NumberReply,
20-
state: BlobStringReply<'connect' | 'connecting' | 'sync' | 'connected'>,
20+
state: BlobStringReply<'connect' | 'connecting' | 'sync' | 'connected' | 'handshake' | 'none'>,
2121
dataReceived: NumberReply
2222
];
2323

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Compile-time regression: the ROLE replica state union must include every
3+
* status replication.c emits for a replica (handshake, none, connect,
4+
* connecting, sync, connected). Declaring only connect/connecting/sync/
5+
* connected rejects valid server states like the handshake phase of a fresh
6+
* replica. "unknown" is deliberately absent: replication.c's default branch
7+
* is unreachable because slaveIsInHandshakeState() intercepts every
8+
* intermediate REPL_STATE_* value before the switch.
9+
*
10+
* Lives outside `lib/` so it is not picked up by the production build /
11+
* typedoc. Checked with `npm run test:types -w @redis/client`.
12+
*/
13+
import { createClient } from '../index';
14+
15+
type Client = ReturnType<typeof createClient>;
16+
type RoleReply = Awaited<ReturnType<Client['role']>>;
17+
18+
export function roleSlaveStateCoversServerStates(reply: RoleReply): void {
19+
if (!reply) return;
20+
if (reply.role === 'slave') {
21+
const state = reply.state;
22+
// Comparing against real server states must be a legal check.
23+
if (state === 'handshake' || state === 'none') {
24+
console.log('transient state:', state);
25+
}
26+
console.log(state);
27+
}
28+
}

0 commit comments

Comments
 (0)