You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
Fix issue with buffers in objects using hSet (#2139)
* Fix issue with buffers in objects using hSet When using hSet with an object, any buffer values inside the object are converted to strings instead of left as buffers. This fix specifically handles the special case of buffers, whilst casting everything else strings (to continue "gracefully" handling the case where the value not a valid type). * Update HSET.ts * Update HSET.spec.ts Co-authored-by: Leibale Eidelman <leibale1998@gmail.com>
This commit is contained in:
@@ -41,12 +41,21 @@ describe('HSET', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Object', () => {
|
describe('Object', () => {
|
||||||
|
it('string', () => {
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
transformArguments('key', { field: 'value' }),
|
transformArguments('key', { field: 'value' }),
|
||||||
['HSET', 'key', 'field', 'value']
|
['HSET', 'key', 'field', 'value']
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Buffer', () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
transformArguments('key', { field: Buffer.from('value') }),
|
||||||
|
['HSET', 'key', 'field', Buffer.from('value')]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
testUtils.testWithClient('client.hSet', async client => {
|
testUtils.testWithClient('client.hSet', async client => {
|
||||||
|
@@ -20,8 +20,10 @@ export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArg
|
|||||||
const args: RedisCommandArguments = ['HSET', key];
|
const args: RedisCommandArguments = ['HSET', key];
|
||||||
|
|
||||||
if (typeof value === 'string' || typeof value === 'number' || Buffer.isBuffer(value)) {
|
if (typeof value === 'string' || typeof value === 'number' || Buffer.isBuffer(value)) {
|
||||||
pushValue(args, value);
|
args.push(
|
||||||
pushValue(args, fieldValue!);
|
convertValue(value),
|
||||||
|
convertValue(fieldValue!)
|
||||||
|
);
|
||||||
} else if (value instanceof Map) {
|
} else if (value instanceof Map) {
|
||||||
pushMap(args, value);
|
pushMap(args, value);
|
||||||
} else if (Array.isArray(value)) {
|
} else if (Array.isArray(value)) {
|
||||||
@@ -35,8 +37,10 @@ export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArg
|
|||||||
|
|
||||||
function pushMap(args: RedisCommandArguments, map: HSETMap): void {
|
function pushMap(args: RedisCommandArguments, map: HSETMap): void {
|
||||||
for (const [key, value] of map.entries()) {
|
for (const [key, value] of map.entries()) {
|
||||||
pushValue(args, key);
|
args.push(
|
||||||
pushValue(args, value);
|
convertValue(key),
|
||||||
|
convertValue(value)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,22 +51,23 @@ function pushTuples(args: RedisCommandArguments, tuples: HSETTuples): void {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
pushValue(args, tuple);
|
args.push(convertValue(tuple));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function pushObject(args: RedisCommandArguments, object: HSETObject): void {
|
function pushObject(args: RedisCommandArguments, object: HSETObject): void {
|
||||||
for (const key of Object.keys(object)) {
|
for (const key of Object.keys(object)) {
|
||||||
args.push(key.toString(), object[key].toString());
|
args.push(
|
||||||
|
convertValue(key),
|
||||||
|
convertValue(object[key])
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function pushValue(args: RedisCommandArguments, value: Types): void {
|
function convertValue(value: Types): RedisCommandArgument {
|
||||||
args.push(
|
return typeof value === 'number' ?
|
||||||
typeof value === 'number' ?
|
|
||||||
value.toString() :
|
value.toString() :
|
||||||
value
|
value;
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare function transformReply(): number;
|
export declare function transformReply(): number;
|
||||||
|
Reference in New Issue
Block a user