Skip to content

Commit f3631f8

Browse files
committed
improve decoder coverage + fix some bugs
1 parent c56f628 commit f3631f8

File tree

2 files changed

+119
-14
lines changed

2 files changed

+119
-14
lines changed

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

Lines changed: 114 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { strict as assert } from 'node:assert';
22
import { SinonSpy, spy } from 'sinon';
33
import { Decoder, RESP_TYPES } from './decoder';
4-
import { BlobError, ErrorReply, SimpleError } from '../errors';
4+
import { BlobError, SimpleError } from '../errors';
55
import { TypeMapping } from './types';
6+
import { VerbatimString } from './verbatim-string';
67

78
interface Test {
89
toWrite: Buffer;
@@ -107,6 +108,14 @@ describe('RESP Decoder', () => {
107108
toWrite: Buffer.from(':-1\r\n'),
108109
replies: [-1]
109110
});
111+
112+
test('1 as string', {
113+
typeMapping: {
114+
[RESP_TYPES.NUMBER]: String
115+
},
116+
toWrite: Buffer.from(':1\r\n'),
117+
replies: ['1']
118+
});
110119
});
111120

112121
describe('BigNumber', () => {
@@ -129,6 +138,14 @@ describe('RESP Decoder', () => {
129138
toWrite: Buffer.from('(-1\r\n'),
130139
replies: [-1n]
131140
});
141+
142+
test('1 as string', {
143+
typeMapping: {
144+
[RESP_TYPES.BIG_NUMBER]: String
145+
},
146+
toWrite: Buffer.from('(1\r\n'),
147+
replies: ['1']
148+
});
132149
});
133150

134151
describe('Double', () => {
@@ -182,15 +199,33 @@ describe('RESP Decoder', () => {
182199
replies: [1e1]
183200
});
184201

185-
test('-1.1E1', {
186-
toWrite: Buffer.from(',-1.1E1\r\n'),
187-
replies: [-1.1E1]
202+
test('-1.1E+1', {
203+
toWrite: Buffer.from(',-1.1E+1\r\n'),
204+
replies: [-1.1E+1]
205+
});
206+
207+
test('1 as string', {
208+
typeMapping: {
209+
[RESP_TYPES.DOUBLE]: String
210+
},
211+
toWrite: Buffer.from(',1\r\n'),
212+
replies: ['1']
188213
});
189214
});
190215

191-
test('SimpleString', {
192-
toWrite: Buffer.from('+OK\r\n'),
193-
replies: ['OK']
216+
describe('SimpleString', () => {
217+
test("'OK'", {
218+
toWrite: Buffer.from('+OK\r\n'),
219+
replies: ['OK']
220+
});
221+
222+
test("'OK' as Buffer", {
223+
typeMapping: {
224+
[RESP_TYPES.SIMPLE_STRING]: Buffer
225+
},
226+
toWrite: Buffer.from('+OK\r\n'),
227+
replies: [Buffer.from('OK')]
228+
});
194229
});
195230

196231
describe('BlobString', () => {
@@ -208,6 +243,14 @@ describe('RESP Decoder', () => {
208243
toWrite: Buffer.from('$-1\r\n'),
209244
replies: [null]
210245
});
246+
247+
test("'OK' as Buffer", {
248+
typeMapping: {
249+
[RESP_TYPES.BLOB_STRING]: Buffer
250+
},
251+
toWrite: Buffer.from('$2\r\nOK\r\n'),
252+
replies: [Buffer.from('OK')]
253+
});
211254
});
212255

213256
describe('VerbatimString', () => {
@@ -220,6 +263,22 @@ describe('RESP Decoder', () => {
220263
toWrite: Buffer.from('=10\r\ntxt:123456\r\n'),
221264
replies: ['123456']
222265
});
266+
267+
test("'OK' as VerbatimString", {
268+
typeMapping: {
269+
[RESP_TYPES.VERBATIM_STRING]: VerbatimString
270+
},
271+
toWrite: Buffer.from('=6\r\ntxt:OK\r\n'),
272+
replies: [new VerbatimString('txt', 'OK')]
273+
});
274+
275+
test("'OK' as Buffer", {
276+
typeMapping: {
277+
[RESP_TYPES.VERBATIM_STRING]: Buffer
278+
},
279+
toWrite: Buffer.from('=6\r\ntxt:OK\r\n'),
280+
replies: [Buffer.from('OK')]
281+
});
223282
});
224283

225284
test('SimpleError', {
@@ -256,9 +315,17 @@ describe('RESP Decoder', () => {
256315
});
257316

258317
test('of 0..9', {
259-
toWrite: Buffer.from(`*10\r\n:0\r\n:1\r\n:2\r\n:3\r\n:4\r\n:5\r\n:6\r\n:7\r\n:8\r\n:9\r\n`),
318+
toWrite: Buffer.from(`~10\r\n:0\r\n:1\r\n:2\r\n:3\r\n:4\r\n:5\r\n:6\r\n:7\r\n:8\r\n:9\r\n`),
260319
replies: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
261320
});
321+
322+
test('0..9 as Set', {
323+
typeMapping: {
324+
[RESP_TYPES.SET]: Set
325+
},
326+
toWrite: Buffer.from(`~10\r\n:0\r\n:1\r\n:2\r\n:3\r\n:4\r\n:5\r\n:6\r\n:7\r\n:8\r\n:9\r\n`),
327+
replies: [new Set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])]
328+
});
262329
});
263330

264331
describe('Map', () => {
@@ -282,5 +349,44 @@ describe('RESP Decoder', () => {
282349
9: { value: '9', enumerable: true }
283350
})]
284351
});
352+
353+
test("{ '0'..'9': <key> } as Map", {
354+
typeMapping: {
355+
[RESP_TYPES.MAP]: Map
356+
},
357+
toWrite: Buffer.from(`%10\r\n+0\r\n+0\r\n+1\r\n+1\r\n+2\r\n+2\r\n+3\r\n+3\r\n+4\r\n+4\r\n+5\r\n+5\r\n+6\r\n+6\r\n+7\r\n+7\r\n+8\r\n+8\r\n+9\r\n+9\r\n`),
358+
replies: [new Map([
359+
['0', '0'],
360+
['1', '1'],
361+
['2', '2'],
362+
['3', '3'],
363+
['4', '4'],
364+
['5', '5'],
365+
['6', '6'],
366+
['7', '7'],
367+
['8', '8'],
368+
['9', '9']
369+
])]
370+
});
371+
372+
test("{ '0'..'9': <key> } as Array", {
373+
typeMapping: {
374+
[RESP_TYPES.MAP]: Array
375+
},
376+
toWrite: Buffer.from(`%10\r\n+0\r\n+0\r\n+1\r\n+1\r\n+2\r\n+2\r\n+3\r\n+3\r\n+4\r\n+4\r\n+5\r\n+5\r\n+6\r\n+6\r\n+7\r\n+7\r\n+8\r\n+8\r\n+9\r\n+9\r\n`),
377+
replies: [['0', '0', '1', '1', '2', '2', '3', '3', '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9']]
378+
});
379+
});
380+
381+
describe('Push', () => {
382+
test('[]', {
383+
toWrite: Buffer.from('>0\r\n'),
384+
pushReplies: [[]]
385+
});
386+
387+
test('[0..9]', {
388+
toWrite: Buffer.from(`>10\r\n:0\r\n:1\r\n:2\r\n:3\r\n:4\r\n:5\r\n:6\r\n:7\r\n:8\r\n:9\r\n`),
389+
pushReplies: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
390+
});
285391
});
286392
});

packages/client/lib/RESP/decoder.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ export class Decoder {
598598

599599
private _continueDecodeVerbatimStringLength(lengthCb, type, chunk) {
600600
const length = lengthCb(chunk);
601-
return typeof length === 'function'?
601+
return typeof length === 'function' ?
602602
this._continueDecodeVerbatimStringLength.bind(this, length, type) :
603603
this._decodeVerbatimStringWithLength(length, type, chunk);
604604
}
@@ -616,11 +616,10 @@ export class Decoder {
616616
}
617617

618618
private _decodeVerbatimStringFormat(stringLength, chunk) {
619-
return this._continueDecodeVerbatimStringFormat(
620-
stringLength,
621-
this._decodeStringWithLength.bind(this, 3, 1, String),
622-
chunk
623-
);
619+
const formatCb = this._decodeStringWithLength.bind(this, 3, 1, String);
620+
return this._cursor >= chunk.length ?
621+
this._continueDecodeVerbatimStringFormat.bind(this, stringLength, formatCb) :
622+
this._continueDecodeVerbatimStringFormat(stringLength, formatCb, chunk);
624623
}
625624

626625
private _continueDecodeVerbatimStringFormat(stringLength, formatCb, chunk) {

0 commit comments

Comments
 (0)