Skip to content

Commit 9ac2ee8

Browse files
committed
redo resp2 "map" types to return Objects to be resp3 default compatible
+ 'fix' for event 'end' test.
1 parent 2b2bbb5 commit 9ac2ee8

15 files changed

+273
-199
lines changed

packages/client/lib/client/index.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,18 @@ describe('Client', () => {
117117
});
118118

119119
testUtils.testWithClient('connect, ready and end events', async client => {
120+
const p1 = once(client, 'connect');
121+
const p2 = once(client, 'ready');
122+
const p3 = once(client, 'end');
123+
120124
await Promise.all([
121-
once(client, 'connect'),
122-
once(client, 'ready'),
125+
p1,
126+
p2,
123127
client.connect()
124128
]);
125129

126-
// TODO: This is failing and I don't know why. when I run manually it seems fine.
127130
await Promise.all([
128-
once(client, 'end'),
131+
p3,
129132
client.close()
130133
]);
131134
}, {

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,21 @@ describe('XREAD', () => {
100100
id: '0-0'
101101
}),
102102
])
103-
assert.deepEqual(reply, [{
104-
name: 'key',
105-
messages: [{
106-
id,
107-
message: Object.create(null, {
108-
field: {
109-
value: 'value',
110-
configurable: true,
111-
enumerable: true
112-
}
113-
})
114-
}]
103+
104+
const obj = Object.assign(Object.create(null), {
105+
'key': [{
106+
id: id,
107+
message: Object.create(null, {
108+
field: {
109+
value: 'value',
110+
configurable: true,
111+
enumerable: true
112+
}
113+
})
115114
}]
116-
);
115+
});
116+
117+
assert.deepStrictEqual(reply, obj);
117118
}, {
118119
client: GLOBAL.SERVERS.OPEN,
119120
cluster: GLOBAL.CLUSTERS.OPEN

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,9 @@ describe('XREADGROUP', () => {
123123
})
124124
]);
125125

126-
assert.deepEqual(readGroupReply, [{
127-
name: 'key',
128-
messages: [{
129-
id,
126+
const obj = Object.assign(Object.create(null), {
127+
'key': [{
128+
id: id,
130129
message: Object.create(null, {
131130
field: {
132131
value: 'value',
@@ -135,7 +134,9 @@ describe('XREADGROUP', () => {
135134
}
136135
})
137136
}]
138-
}]);
137+
});
138+
139+
assert.deepStrictEqual(readGroupReply, obj);
139140
}, {
140141
client: GLOBAL.SERVERS.OPEN,
141142
cluster: GLOBAL.CLUSTERS.OPEN

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

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -468,52 +468,58 @@ export function transformStreamMessagesReply(r: ArrayReply<StreamMessageRawReply
468468
type StreamMessagesRawReply = TuplesReply<[name: BlobStringReply, ArrayReply<StreamMessageRawReply>]>;
469469
type StreamsMessagesRawReply2 = ArrayReply<StreamMessagesRawReply>;
470470

471-
export function transformStreamsMessagesReplyResp2(reply: UnwrapReply<StreamsMessagesRawReply2> | UnwrapReply<NullReply>): StreamsMessagesReply | null {
472-
if (reply === null) return null;
471+
export function transformStreamsMessagesReplyResp2(
472+
reply: UnwrapReply<StreamsMessagesRawReply2 | NullReply>
473+
): Record<string, StreamMessagesReply> | NullReply {
474+
if (reply === null) return null as unknown as NullReply;
473475

474-
return reply.map((s) => {
475-
const stream = s as unknown as UnwrapReply<typeof s>;
476+
const ret: Record<string, StreamMessagesReply> = Object.create(null);
476477

477-
return {
478-
name: stream[0],
479-
messages: transformStreamMessagesReply(stream[1])
480-
}
481-
})
478+
for (let i=0; i < reply.length; i ++) {
479+
const stream = reply[i] as unknown as UnwrapReply<StreamMessagesRawReply>;
480+
481+
const name = stream[0] as unknown as UnwrapReply<BlobStringReply>;
482+
const rawMessages = stream[1];
483+
484+
ret[name.toString()] = transformStreamMessagesReply(rawMessages);
485+
}
486+
487+
return ret;
482488
}
483489

484490
type StreamsMessagesRawReply3 = MapReply<BlobStringReply, ArrayReply<StreamMessageRawReply>>;
485491

486-
export function transformStreamsMessagesReplyResp3(reply: UnwrapReply<StreamsMessagesRawReply3> | UnwrapReply<NullReply>): StreamsMessagesReply | null {
487-
if (reply === null) return null;
492+
export function transformStreamsMessagesReplyResp3(reply: UnwrapReply<StreamsMessagesRawReply3 | NullReply>): MapReply<BlobStringReply, StreamMessagesReply> | NullReply {
493+
if (reply === null) return null as unknown as NullReply;
488494

489-
const ret: StreamsMessagesReply = [];
490495
if (reply instanceof Map) {
491-
for (const [name, rawMessages] of reply) {
492-
ret.push({
493-
name,
494-
messages: transformStreamMessagesReply(rawMessages),
495-
})
496+
const ret = new Map<string, StreamMessagesReply>();
497+
498+
for (const [n, rawMessages] of reply) {
499+
const name = n as unknown as UnwrapReply<BlobStringReply>;
500+
501+
ret.set(name.toString(), transformStreamMessagesReply(rawMessages));
496502
}
497503

498-
return ret;
504+
return ret as unknown as MapReply<BlobStringReply, StreamMessagesReply>
499505
} else if (reply instanceof Array) {
506+
const ret = [];
507+
500508
for (let i=0; i < reply.length; i += 2) {
501-
const name = reply[i] as BlobStringReply
509+
const name = reply[i] as BlobStringReply;
502510
const rawMessages = reply[i+1] as ArrayReply<StreamMessageRawReply>;
503511

504-
ret.push({
505-
name,
506-
messages: transformStreamMessagesReply(rawMessages)
507-
});
512+
ret.push(name);
513+
ret.push(transformStreamMessagesReply(rawMessages));
508514
}
515+
516+
return ret as unknown as MapReply<BlobStringReply, StreamMessagesReply>
509517
} else {
518+
const ret = Object.create(null);
510519
for (const [name, rawMessages] of Object.entries(reply)) {
511-
ret.push({
512-
name,
513-
messages: transformStreamMessagesReply(rawMessages),
514-
})
520+
ret[name] = transformStreamMessagesReply(rawMessages);
515521
}
516-
}
517522

518-
return ret;
519-
}
523+
return ret as unknown as MapReply<BlobStringReply, StreamMessagesReply>
524+
}
525+
}

packages/time-series/lib/commands/MGET.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ describe('TS.MGET', () => {
2929
client.ts.mGet('label=value')
3030
]);
3131

32-
assert.deepEqual(reply, [{
33-
key: 'key',
34-
sample: {
35-
timestamp: 0,
36-
value: 0
32+
const obj = Object.assign(Object.create(null), {
33+
'key': {
34+
sample: {
35+
timestamp: 0,
36+
value: 0
37+
}
3738
}
38-
}]);
39+
});
40+
41+
assert.deepStrictEqual(reply, obj);
3942
}, GLOBAL.SERVERS.OPEN);
4043
});

packages/time-series/lib/commands/MGET.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CommandArguments, Command, BlobStringReply, ArrayReply, UnwrapReply, Resp2Reply, MapReply, TuplesReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { RawLabels2, RawLabels3, resp3MapToValue, SampleRawReply, transformSampleReply } from '.';
3+
import { RawLabels2, RawLabels3, resp2MapToValue, resp3MapToValue, SampleRawReply, transformSampleReply } from '.';
44

55
export interface TsMGetOptions {
66
LATEST?: boolean;
@@ -19,11 +19,13 @@ export function pushFilterArgument(args: CommandArguments, filter: RedisVariadic
1919
return pushVariadicArguments(args, filter);
2020
}
2121

22-
export type MGetRawReply2 = ArrayReply<[
22+
export type MGetRawReplyValue2 = TuplesReply<[
2323
key: BlobStringReply,
2424
labels: RawLabels2,
2525
sample: Resp2Reply<SampleRawReply>
26-
]>;
26+
]>
27+
28+
export type MGetRawReply2 = ArrayReply<MGetRawReplyValue2>;
2729

2830
export type MGetRawReplyValue3 = TuplesReply<[
2931
labels: RawLabels3,
@@ -45,15 +47,16 @@ export interface MGetReply3 {
4547
sample: ReturnType<typeof transformSampleReply[3]>;
4648
}
4749

48-
export function parseResp3Mget(
49-
key: BlobStringReply | string,
50-
value: UnwrapReply<MGetRawReplyValue3>
51-
): MGetReply3 {
52-
50+
export function parseResp3Mget(value: UnwrapReply<MGetRawReplyValue3>) {
5351
return {
54-
key: key,
5552
sample: transformSampleReply[3](value[1])
56-
}
53+
};
54+
}
55+
56+
export function parseResp2Mget(value: UnwrapReply<MGetRawReplyValue2>) {
57+
return {
58+
sample: transformSampleReply[2](value[2])
59+
};
5760
}
5861

5962
export default {
@@ -64,13 +67,10 @@ export default {
6467
return pushFilterArgument(args, filter);
6568
},
6669
transformReply: {
67-
2(reply: UnwrapReply<MGetRawReply2>): Array<MGetReply2> {
68-
return reply.map(([key, _, sample]) => ({
69-
key: key,
70-
sample: transformSampleReply[2](sample)
71-
}));
70+
2(reply: UnwrapReply<MGetRawReply2>) {
71+
return resp2MapToValue(reply, parseResp2Mget);
7272
},
73-
3(reply: UnwrapReply<MGetRawReply3>): Array<MGetReply3> {
73+
3(reply: UnwrapReply<MGetRawReply3>) {
7474
return resp3MapToValue(reply, parseResp3Mget)
7575
}
7676
}

packages/time-series/lib/commands/MGET_WITHLABELS.spec.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ describe('TS.MGET_WITHLABELS', () => {
2929
client.ts.mGetWithLabels('label=value')
3030
]);
3131

32-
assert.deepEqual(reply, [{
33-
key: 'key',
34-
labels: { label: 'value' },
35-
sample: {
36-
timestamp: 0,
37-
value: 0
32+
const obj = Object.assign(Object.create(null), {
33+
'key': {
34+
labels: { label: 'value' },
35+
sample: {
36+
timestamp: 0,
37+
value: 0
38+
}
3839
}
39-
}]);
40+
});
41+
42+
assert.deepStrictEqual(reply, obj);
4043
}, GLOBAL.SERVERS.OPEN);
4144
});
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { BlobStringReply, Command, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
1+
import { Command, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
22
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
3-
import { TsMGetOptions, pushLatestArgument, pushFilterArgument, MGetReply2, MGetRawReply2, MGetReply3, MGetRawReplyValue3, MGetRawReply3, parseResp3Mget } from './MGET';
4-
import { Labels, pushWithLabelsArgument, resp3MapToValue, transformLablesReply2, transformLablesReply3, transformSampleReply } from '.';
3+
import { TsMGetOptions, pushLatestArgument, pushFilterArgument, MGetReply2, MGetRawReply2, MGetReply3, MGetRawReplyValue3, MGetRawReply3, parseResp3Mget, parseResp2Mget, MGetRawReplyValue2 } from './MGET';
4+
import { Labels, pushWithLabelsArgument, resp2MapToValue, resp3MapToValue, transformLablesReply2, transformLablesReply3 } from '.';
55

66
export interface TsMGetWithLabelsOptions extends TsMGetOptions {
77
SELECTED_LABELS?: RedisVariadicArgument;
@@ -15,11 +15,17 @@ export interface MGetWithLabelsReply3 extends MGetReply3 {
1515
labels: Labels;
1616
};
1717

18-
function parseResp3MgetWithLabels(
19-
key: BlobStringReply | string,
20-
value: UnwrapReply<MGetRawReplyValue3>
18+
function parseResp2MgetWithLabels(
19+
value: UnwrapReply<MGetRawReplyValue2>,
2120
): MGetWithLabelsReply3 {
22-
const ret = parseResp3Mget(key, value) as MGetWithLabelsReply3;
21+
const ret = parseResp2Mget(value) as unknown as MGetWithLabelsReply3;
22+
ret.labels = transformLablesReply2(value[1]);
23+
24+
return ret;
25+
}
26+
27+
function parseResp3MgetWithLabels(value: UnwrapReply<MGetRawReplyValue3>): MGetWithLabelsReply3 {
28+
const ret = parseResp3Mget(value) as MGetWithLabelsReply3;
2329
ret.labels = transformLablesReply3(value[0]);
2430

2531
return ret;
@@ -34,15 +40,11 @@ export default {
3440
return pushFilterArgument(args, filter);
3541
},
3642
transformReply: {
37-
2(reply: UnwrapReply<MGetRawReply2>): Array<MGetWithLabelsReply2> {
38-
return reply.map(([key, labels, sample]) => ({
39-
key: key,
40-
labels: transformLablesReply2(labels),
41-
sample: transformSampleReply[2](sample)
42-
}));
43+
2(reply: UnwrapReply<MGetRawReply2>) {
44+
return resp2MapToValue(reply, parseResp2MgetWithLabels);
4345
},
44-
3(reply: UnwrapReply<MGetRawReply3>): Array<MGetReply3> {
45-
return resp3MapToValue(reply, parseResp3MgetWithLabels)
46+
3(reply: UnwrapReply<MGetRawReply3>) {
47+
return resp3MapToValue(reply, parseResp3MgetWithLabels);
4648
}
4749
},
4850
} as const satisfies Command;

packages/time-series/lib/commands/MRANGE.spec.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../test-utils';
33
import MRANGE, { TIME_SERIES_REDUCERS } from './MRANGE';
44
import { TIME_SERIES_AGGREGATION_TYPE } from './CREATERULE';
5+
import { CommandArguments } from '@redis/client/lib/RESP/types';
56

67
describe('TS.MRANGE', () => {
78
it('transformArguments', () => {
9+
const expectedReply: CommandArguments = [
10+
'TS.MRANGE', '-', '+', 'FILTER_BY_TS', '0', 'FILTER_BY_VALUE', '0', '1',
11+
'COUNT', '1', 'ALIGN', '-', 'AGGREGATION', 'AVG', '1', 'FILTER', 'label=value',
12+
'GROUPBY', 'label', 'REDUCE', 'SUM'
13+
];
14+
expectedReply.preserve = true;
15+
816
assert.deepEqual(
917
MRANGE.transformArguments('-', '+', 'label=value', {
1018
FILTER_BY_TS: [0],
@@ -23,11 +31,7 @@ describe('TS.MRANGE', () => {
2331
reducer: TIME_SERIES_REDUCERS.SUM
2432
},
2533
}),
26-
[
27-
'TS.MRANGE', '-', '+', 'FILTER_BY_TS', '0', 'FILTER_BY_VALUE', '0', '1',
28-
'COUNT', '1', 'ALIGN', '-', 'AGGREGATION', 'AVG', '1', 'FILTER', 'label=value',
29-
'GROUPBY', 'label', 'REDUCE', 'SUM'
30-
]
34+
expectedReply
3135
);
3236
});
3337

@@ -43,12 +47,15 @@ describe('TS.MRANGE', () => {
4347
})
4448
]);
4549

46-
assert.deepEqual(reply, [{
47-
key: 'key',
48-
samples: [{
49-
timestamp: 0,
50-
value: 0
51-
}]
52-
}]);
50+
const obj = Object.assign(Object.create(null), {
51+
'key': {
52+
samples: [{
53+
timestamp: 0,
54+
value: 0
55+
}]
56+
}
57+
});
58+
59+
assert.deepStrictEqual(reply, obj);
5360
}, GLOBAL.SERVERS.OPEN);
5461
});

0 commit comments

Comments
 (0)