Skip to content

Commit 3ec17e3

Browse files
authored
fix legacy mode resp encoder (#2118)
* fix legacy mode resp encoder * Update encoder.ts
1 parent 94dbcc8 commit 3ec17e3

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

packages/client/lib/client/RESP2/encoder.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ const CRLF = '\r\n';
55
export default function encodeCommand(args: RedisCommandArguments): Array<RedisCommandArgument> {
66
const toWrite: Array<RedisCommandArgument> = [];
77

8-
let strings = `*${args.length}${CRLF}`;
8+
let strings = '*' + args.length + CRLF;
99

1010
for (let i = 0; i < args.length; i++) {
1111
const arg = args[i];
1212
if (typeof arg === 'string') {
13-
const byteLength = Buffer.byteLength(arg);
14-
strings += `$${byteLength}${CRLF}`;
15-
strings += arg;
13+
strings += '$' + Buffer.byteLength(arg) + CRLF + arg + CRLF;
1614
} else if (arg instanceof Buffer) {
17-
toWrite.push(`${strings}$${arg.length}${CRLF}`);
18-
strings = '';
19-
toWrite.push(arg);
15+
toWrite.push(
16+
strings + '$' + arg.length.toString() + CRLF,
17+
arg
18+
);
19+
strings = CRLF;
2020
} else {
2121
throw new TypeError('Invalid argument type');
2222
}
23-
24-
strings += CRLF;
2523
}
2624

2725
toWrite.push(strings);

packages/client/lib/commander.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ export function transformCommandArguments<T>(
123123
}
124124

125125
export function transformLegacyCommandArguments(args: Array<any>): Array<any> {
126-
return args.flat().map(x => x?.toString?.());
126+
return args.flat().map(arg => {
127+
return typeof arg === 'number' || arg instanceof Date ?
128+
arg.toString() :
129+
arg;
130+
});
127131
}
128132

129133
export function transformCommandReply<C extends RedisCommand>(

0 commit comments

Comments
 (0)