Skip to content

Commit 82f43d9

Browse files
Tyrisleibale
andauthored
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 <[email protected]>
1 parent bf80c16 commit 82f43d9

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ describe('HSET', () => {
4141
);
4242
});
4343

44-
it('Object', () => {
45-
assert.deepEqual(
46-
transformArguments('key', { field: 'value' }),
47-
['HSET', 'key', 'field', 'value']
48-
);
44+
describe('Object', () => {
45+
it('string', () => {
46+
assert.deepEqual(
47+
transformArguments('key', { field: 'value' }),
48+
['HSET', 'key', 'field', 'value']
49+
);
50+
});
51+
52+
it('Buffer', () => {
53+
assert.deepEqual(
54+
transformArguments('key', { field: Buffer.from('value') }),
55+
['HSET', 'key', 'field', Buffer.from('value')]
56+
);
57+
});
4958
});
5059
});
5160

@@ -62,4 +71,4 @@ describe('HSET', () => {
6271
1
6372
);
6473
}, GLOBAL.CLUSTERS.OPEN);
65-
});
74+
});

packages/client/lib/commands/HSET.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArg
2020
const args: RedisCommandArguments = ['HSET', key];
2121

2222
if (typeof value === 'string' || typeof value === 'number' || Buffer.isBuffer(value)) {
23-
pushValue(args, value);
24-
pushValue(args, fieldValue!);
23+
args.push(
24+
convertValue(value),
25+
convertValue(fieldValue!)
26+
);
2527
} else if (value instanceof Map) {
2628
pushMap(args, value);
2729
} else if (Array.isArray(value)) {
@@ -35,8 +37,10 @@ export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArg
3537

3638
function pushMap(args: RedisCommandArguments, map: HSETMap): void {
3739
for (const [key, value] of map.entries()) {
38-
pushValue(args, key);
39-
pushValue(args, value);
40+
args.push(
41+
convertValue(key),
42+
convertValue(value)
43+
);
4044
}
4145
}
4246

@@ -47,22 +51,23 @@ function pushTuples(args: RedisCommandArguments, tuples: HSETTuples): void {
4751
continue;
4852
}
4953

50-
pushValue(args, tuple);
54+
args.push(convertValue(tuple));
5155
}
5256
}
5357

5458
function pushObject(args: RedisCommandArguments, object: HSETObject): void {
5559
for (const key of Object.keys(object)) {
56-
args.push(key.toString(), object[key].toString());
60+
args.push(
61+
convertValue(key),
62+
convertValue(object[key])
63+
);
5764
}
5865
}
5966

60-
function pushValue(args: RedisCommandArguments, value: Types): void {
61-
args.push(
62-
typeof value === 'number' ?
63-
value.toString() :
64-
value
65-
);
67+
function convertValue(value: Types): RedisCommandArgument {
68+
return typeof value === 'number' ?
69+
value.toString() :
70+
value;
6671
}
6772

6873
export declare function transformReply(): number;

0 commit comments

Comments
 (0)