Skip to content

Commit d7c6544

Browse files
authored
fix: multi-byte character corruption when converting Buffers to strings (#3100)
* add tests for multi-byte character buffer * support multi-byte characters when decoding buffers fixes: #2993
1 parent 96d6445 commit d7c6544

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

packages/client/lib/RESP/decoder.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ describe('RESP Decoder', () => {
226226
toWrite: Buffer.from('+OK\r\n'),
227227
replies: [Buffer.from('OK')]
228228
});
229+
230+
test("'é'", {
231+
toWrite: Buffer.from('+é\r\n'),
232+
replies: ['é']
233+
});
229234
});
230235

231236
describe('BlobString', () => {
@@ -251,6 +256,11 @@ describe('RESP Decoder', () => {
251256
toWrite: Buffer.from('$2\r\nOK\r\n'),
252257
replies: [Buffer.from('OK')]
253258
});
259+
260+
test("'é'", {
261+
toWrite: Buffer.from('$2\r\né\r\n'),
262+
replies: ['é']
263+
});
254264
});
255265

256266
describe('VerbatimString', () => {
@@ -279,6 +289,11 @@ describe('RESP Decoder', () => {
279289
toWrite: Buffer.from('=6\r\ntxt:OK\r\n'),
280290
replies: [Buffer.from('OK')]
281291
});
292+
293+
test("'é'", {
294+
toWrite: Buffer.from('=6\r\ntxt:é\r\n'),
295+
replies: ['é']
296+
});
282297
});
283298

284299
test('SimpleError', {

packages/client/lib/RESP/decoder.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,8 @@ export class Decoder {
506506
}
507507

508508
chunks.push(chunk.subarray(start, crlfIndex));
509-
return type === Buffer ?
510-
Buffer.concat(chunks) :
511-
chunks.join('');
509+
const buffer = Buffer.concat(chunks);
510+
return type === Buffer ? buffer : buffer.toString();
512511
}
513512

514513
#decodeBlobString(type, chunk) {
@@ -578,9 +577,8 @@ export class Decoder {
578577

579578
chunks.push(chunk.subarray(this.#cursor, end));
580579
this.#cursor = end + skip;
581-
return type === Buffer ?
582-
Buffer.concat(chunks) :
583-
chunks.join('');
580+
const buffer = Buffer.concat(chunks);
581+
return type === Buffer ? buffer : buffer.toString();
584582
}
585583

586584
#decodeBlobStringWithLength(length, type, chunk) {

0 commit comments

Comments
 (0)