Skip to content

Commit 8369448

Browse files
committed
ref #2565 - fix X[AUTO]CLAIM
1 parent 51a1000 commit 8369448

File tree

7 files changed

+59
-42
lines changed

7 files changed

+59
-42
lines changed

packages/client/lib/commands/XAUTOCLAIM.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ describe('XAUTOCLAIM', () => {
3131
}
3232
});
3333

34-
const [, , id1, id2, , , reply] = await Promise.all([
34+
const [, id1, id2, , , reply] = await Promise.all([
3535
client.xGroupCreate('key', 'group', '$', {
3636
MKSTREAM: true
3737
}),
38-
client.xGroupCreateConsumer('key', 'group', 'consumer'),
3938
client.xAdd('key', '*', message),
4039
client.xAdd('key', '*', message),
4140
client.xReadGroup('group', 'consumer', {

packages/client/lib/commands/XAUTOCLAIM.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { RedisArgument, TuplesReply, BlobStringReply, ArrayReply, NullReply, UnwrapReply, Command } from '../RESP/types';
2-
import { StreamMessageRawReply, isNullReply, transformStreamMessageReply } from './generic-transformers';
2+
import { StreamMessageReply, transformStreamMessageNullReply } from './generic-transformers';
33

44
export interface XAutoClaimOptions {
55
COUNT?: number;
66
}
77

88
export type XAutoClaimRawReply = TuplesReply<[
99
nextId: BlobStringReply,
10-
messages: ArrayReply<StreamMessageRawReply | NullReply>,
10+
messages: ArrayReply<StreamMessageReply | NullReply>,
1111
deletedMessages: ArrayReply<BlobStringReply>
1212
]>;
1313

@@ -40,9 +40,7 @@ export default {
4040
transformReply(reply: UnwrapReply<XAutoClaimRawReply>) {
4141
return {
4242
nextId: reply[0],
43-
messages: (reply[1] as unknown as UnwrapReply<typeof reply[1]>).map(message => {
44-
return isNullReply(message) ? null : transformStreamMessageReply(message);
45-
}),
43+
messages: (reply[1] as unknown as UnwrapReply<typeof reply[1]>).map(transformStreamMessageNullReply),
4644
deletedMessages: reply[2]
4745
};
4846
}

packages/client/lib/commands/XCLAIM.spec.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,35 @@ describe('XCLAIM', () => {
8989
});
9090
});
9191

92-
// TODO: test with messages
9392
testUtils.testAll('xClaim', async client => {
94-
const [, reply] = await Promise.all([
93+
const message = Object.create(null, {
94+
field: {
95+
value: 'value',
96+
enumerable: true
97+
}
98+
});
99+
100+
const [, , , , , reply] = await Promise.all([
95101
client.xGroupCreate('key', 'group', '$', {
96102
MKSTREAM: true
97103
}),
98-
client.xClaim('key', 'group', 'consumer', 1, '0-0')
104+
client.xAdd('key', '1-0', message),
105+
client.xAdd('key', '2-0', message),
106+
client.xReadGroup('group', 'consumer', {
107+
key: 'key',
108+
id: '>'
109+
}),
110+
client.xTrim('key', 'MAXLEN', 1),
111+
client.xClaim('key', 'group', 'consumer', 0, ['1-0', '2-0'])
99112
]);
100113

101-
assert.deepEqual(reply, []);
114+
assert.deepEqual(reply, [
115+
...(testUtils.isVersionGreaterThan([7, 0]) ? [] : [null]),
116+
{
117+
id: '2-0',
118+
message
119+
}
120+
]);
102121
}, {
103122
client: GLOBAL.SERVERS.OPEN,
104123
cluster: GLOBAL.CLUSTERS.OPEN

packages/client/lib/commands/XCLAIM.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { RedisArgument, Command } from '../RESP/types';
2-
import { RedisVariadicArgument, pushVariadicArguments, transformStreamMessagesReply } from './generic-transformers';
1+
import { RedisArgument, ArrayReply, NullReply, UnwrapReply, Command } from '../RESP/types';
2+
import { RedisVariadicArgument, pushVariadicArguments, StreamMessageReply, transformStreamMessageNullReply } from './generic-transformers';
33

44
export interface XClaimOptions {
55
IDLE?: number;
@@ -50,5 +50,7 @@ export default {
5050

5151
return args;
5252
},
53-
transformReply: transformStreamMessagesReply
53+
transformReply(reply: UnwrapReply<ArrayReply<StreamMessageReply | NullReply>>) {
54+
return reply.map(transformStreamMessageNullReply);
55+
}
5456
} as const satisfies Command;

packages/client/lib/commands/XINFO_STREAM.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { TuplesToMapReply, BlobStringReply, NumberReply, NullReply, Resp2Reply, Command, RespType, RESP_TYPES, RedisArgument } from '../RESP/types';
2-
import { StreamMessageRawReply, transformStreamMessageReply } from './generic-transformers';
1+
import { RedisArgument, TuplesToMapReply, BlobStringReply, NumberReply, NullReply, TuplesReply, ArrayReply, UnwrapReply, Command } from '../RESP/types';
2+
import { isNullReply, transformTuplesReply } from './generic-transformers';
33

44
export type XInfoStreamReply = TuplesToMapReply<[
55
[BlobStringReply<'length'>, NumberReply],
@@ -13,8 +13,8 @@ export type XInfoStreamReply = TuplesToMapReply<[
1313
/** added in 7.2 */
1414
[BlobStringReply<'recorded-first-entry-id'>, BlobStringReply],
1515
[BlobStringReply<'groups'>, NumberReply],
16-
[BlobStringReply<'first-entry'>, ReturnType<typeof transformStreamMessageReply> | NullReply],
17-
[BlobStringReply<'last-entry'>, ReturnType<typeof transformStreamMessageReply> | NullReply]
16+
[BlobStringReply<'first-entry'>, ReturnType<typeof transformEntry>],
17+
[BlobStringReply<'last-entry'>, ReturnType<typeof transformEntry>]
1818
]>;
1919

2020
export default {
@@ -66,6 +66,17 @@ export default {
6666
}
6767
} as const satisfies Command;
6868

69-
function transformEntry(entry: StreamMessageRawReply | NullReply) {
70-
return entry === null ? null : transformStreamMessageReply(entry as StreamMessageRawReply);
69+
type RawEntry = TuplesReply<[
70+
id: BlobStringReply,
71+
message: ArrayReply<BlobStringReply>
72+
]> | NullReply;
73+
74+
function transformEntry(entry: RawEntry) {
75+
if (isNullReply(entry)) return entry;
76+
77+
const [id, message] = entry as unknown as UnwrapReply<typeof entry>;
78+
return {
79+
id,
80+
message: transformTuplesReply(message)
81+
};
7182
}

packages/client/lib/commands/XRANGE.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { RedisArgument, Command } from '../RESP/types';
2-
import { transformStreamMessagesReply } from './generic-transformers';
1+
import { RedisArgument, ArrayReply, UnwrapReply, Command } from '../RESP/types';
2+
import { StreamMessageReply, transformStreamMessageReply } from './generic-transformers';
33

44
export interface XRangeOptions {
55
COUNT?: number;
@@ -25,5 +25,7 @@ export default {
2525
FIRST_KEY_INDEX: 1,
2626
IS_READ_ONLY: true,
2727
transformArguments: transformXRangeArguments.bind(undefined, 'XRANGE'),
28-
transformReply: transformStreamMessagesReply
28+
transformReply(reply: UnwrapReply<ArrayReply<StreamMessageReply>>) {
29+
return reply.map(transformStreamMessageReply);
30+
}
2931
} as const satisfies Command;

packages/client/lib/commands/generic-transformers.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,37 +84,23 @@ export function transformTuplesReply(
8484
return message;
8585
}
8686

87-
export type StreamMessageRawReply = TuplesReply<[
87+
export type StreamMessageReply = TuplesReply<[
8888
id: BlobStringReply,
8989
message: ArrayReply<BlobStringReply>
9090
]>;
9191

92-
export function transformStreamMessageReply(reply: StreamMessageRawReply) {
93-
const [id, message] = reply as unknown as UnwrapReply<typeof reply>;
92+
export function transformStreamMessageReply(reply: StreamMessageReply) {
93+
const [ id, message ] = reply as unknown as UnwrapReply<typeof reply>;
9494
return {
9595
id,
9696
message: transformTuplesReply(message)
9797
};
9898
}
9999

100-
export type StreamMessagesRawReply = ArrayReply<StreamMessageRawReply>;
101-
102-
export function transformStreamMessagesReply(reply: StreamMessagesRawReply) {
103-
return (reply as unknown as UnwrapReply<typeof reply>)
104-
.map(message => transformStreamMessageReply(message));
100+
export function transformStreamMessageNullReply(reply: StreamMessageReply | NullReply) {
101+
return isNullReply(reply) ? reply : transformStreamMessageReply(reply);
105102
}
106103

107-
// export type StreamsMessagesReply = MapReply<BlobStringReply, StreamMessagesRawReply>;
108-
109-
// export function transformStreamsMessagesReply(reply: Array<any> | null): StreamsMessagesReply | null {
110-
// if (reply === null) return null;
111-
112-
// return reply.map(([name, rawMessages]) => ({
113-
// name,
114-
// messages: transformStreamMessagesReply(rawMessages)
115-
// }));
116-
// }
117-
118104
export interface SortedSetMember {
119105
value: RedisArgument;
120106
score: number;

0 commit comments

Comments
 (0)