From 82f43d9a53c3c98e09297b04bf2ba59071d83806 Mon Sep 17 00:00:00 2001 From: Tom Graham Date: Thu, 26 May 2022 23:56:10 +1000 Subject: [PATCH] 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 --- packages/client/lib/commands/HSET.spec.ts | 21 +++++++++++----- packages/client/lib/commands/HSET.ts | 29 +++++++++++++---------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/packages/client/lib/commands/HSET.spec.ts b/packages/client/lib/commands/HSET.spec.ts index e416081081..73bc966f87 100644 --- a/packages/client/lib/commands/HSET.spec.ts +++ b/packages/client/lib/commands/HSET.spec.ts @@ -41,11 +41,20 @@ describe('HSET', () => { ); }); - it('Object', () => { - assert.deepEqual( - transformArguments('key', { field: 'value' }), - ['HSET', 'key', 'field', 'value'] - ); + describe('Object', () => { + it('string', () => { + assert.deepEqual( + transformArguments('key', { field: 'value' }), + ['HSET', 'key', 'field', 'value'] + ); + }); + + it('Buffer', () => { + assert.deepEqual( + transformArguments('key', { field: Buffer.from('value') }), + ['HSET', 'key', 'field', Buffer.from('value')] + ); + }); }); }); @@ -62,4 +71,4 @@ describe('HSET', () => { 1 ); }, GLOBAL.CLUSTERS.OPEN); -}); \ No newline at end of file +}); diff --git a/packages/client/lib/commands/HSET.ts b/packages/client/lib/commands/HSET.ts index 1fe1743b6a..261ef98c77 100644 --- a/packages/client/lib/commands/HSET.ts +++ b/packages/client/lib/commands/HSET.ts @@ -20,8 +20,10 @@ export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArg const args: RedisCommandArguments = ['HSET', key]; if (typeof value === 'string' || typeof value === 'number' || Buffer.isBuffer(value)) { - pushValue(args, value); - pushValue(args, fieldValue!); + args.push( + convertValue(value), + convertValue(fieldValue!) + ); } else if (value instanceof Map) { pushMap(args, value); } else if (Array.isArray(value)) { @@ -35,8 +37,10 @@ export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArg function pushMap(args: RedisCommandArguments, map: HSETMap): void { for (const [key, value] of map.entries()) { - pushValue(args, key); - pushValue(args, value); + args.push( + convertValue(key), + convertValue(value) + ); } } @@ -47,22 +51,23 @@ function pushTuples(args: RedisCommandArguments, tuples: HSETTuples): void { continue; } - pushValue(args, tuple); + args.push(convertValue(tuple)); } } function pushObject(args: RedisCommandArguments, object: HSETObject): void { 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 { - args.push( - typeof value === 'number' ? - value.toString() : - value - ); +function convertValue(value: Types): RedisCommandArgument { + return typeof value === 'number' ? + value.toString() : + value; } export declare function transformReply(): number;