1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

fix #1846 - handle arguments that are not buffers or strings (#1849)

* fix #1846 - handle arguments that are not buffers or strings

* use toString() instead of throw TypeError

* remove .only and uncomment tests
This commit is contained in:
Leibale Eidelman
2022-01-24 12:04:30 -05:00
committed by GitHub
parent 551d2041dc
commit 7ded3dd79f
7 changed files with 64 additions and 73 deletions

View File

@@ -95,25 +95,25 @@ export function* encodeCommand(args: RedisCommandArguments): IterableIterator<Re
let strings = `*${args.length}${DELIMITER}`,
stringsLength = 0;
for (const arg of args) {
const isString = typeof arg === 'string',
byteLength = isString ? Buffer.byteLength(arg): arg.length;
strings += `$${byteLength}${DELIMITER}`;
if (isString) {
const totalLength = stringsLength + byteLength;
if (totalLength > 1024) {
yield strings;
strings = arg;
stringsLength = byteLength;
} else {
strings += arg;
stringsLength = totalLength;
}
} else {
yield strings;
if (Buffer.isBuffer(arg)) {
yield `${strings}$${arg.length}${DELIMITER}`;
strings = '';
stringsLength = 0;
yield arg;
} else {
const string = arg?.toString?.() ?? '',
byteLength = Buffer.byteLength(string);
strings += `$${byteLength}${DELIMITER}`;
const totalLength = stringsLength + byteLength;
if (totalLength > 1024) {
yield strings;
strings = string;
stringsLength = byteLength;
} else {
strings += string;
stringsLength = totalLength;
}
}
strings += DELIMITER;
@@ -133,18 +133,3 @@ export function transformCommandReply(
return command.transformReply(rawReply, preserved);
}
export type LegacyCommandArguments = Array<string | number | Buffer | LegacyCommandArguments>;
export function transformLegacyCommandArguments(args: LegacyCommandArguments, flat: RedisCommandArguments = []): RedisCommandArguments {
for (const arg of args) {
if (Array.isArray(arg)) {
transformLegacyCommandArguments(arg, flat);
continue;
}
flat.push(typeof arg === 'number' ? arg.toString() : arg);
}
return flat;
}