diff --git a/src/json-pack/resp/RespDecoder.ts b/src/json-pack/resp/RespDecoder.ts index c63c337cd9..54cb42997a 100644 --- a/src/json-pack/resp/RespDecoder.ts +++ b/src/json-pack/resp/RespDecoder.ts @@ -81,6 +81,25 @@ export class RespDecoder { const decoder = new RespDecoder(); @@ -215,3 +215,21 @@ describe('nulls', () => { expect(decoded).toBe(null); }); }); + +describe('commands', () => { + test('can decode a PING command', () => { + const encoded = encoder.encodeCmd(['PING']); + const decoder = new RespDecoder(); + decoder.reader.reset(encoded); + const decoded = decoder.readCmd(); + expect(decoded).toEqual(['PING']); + }); + + test('can decode a SET command', () => { + const encoded = encoder.encodeCmd(['SET', 'foo', 'bar']); + const decoder = new RespDecoder(); + decoder.reader.reset(encoded); + const decoded = decoder.readCmd(); + expect(decoded).toEqual(['SET', utf8`foo`, utf8`bar`]); + }); +}); diff --git a/src/json-pack/resp/__tests__/RespStreamingDecoder.spec.ts b/src/json-pack/resp/__tests__/RespStreamingDecoder.spec.ts index ec185ffcc2..112fd3a054 100644 --- a/src/json-pack/resp/__tests__/RespStreamingDecoder.spec.ts +++ b/src/json-pack/resp/__tests__/RespStreamingDecoder.spec.ts @@ -2,6 +2,7 @@ import {RespStreamingDecoder} from '../RespStreamingDecoder'; import {RespEncoder} from '../RespEncoder'; import {concatList} from '../../../util/buffers/concat'; import {documents} from '../../../__tests__/json-documents'; +import {utf8} from '../../../util/buffers/strings'; const encoder = new RespEncoder(); @@ -66,3 +67,11 @@ test('can stream 49 bytes at a time', () => { } expect(decoded).toEqual(docs); }); + +test('can decode a command', () => { + const encoded = encoder.encodeCmd(['SET', 'foo', 'bar']); + const decoder = new RespStreamingDecoder(); + decoder.push(encoded); + const decoded = decoder.readCmd(); + expect(decoded).toEqual(['SET', utf8`foo`, utf8`bar`]); +}); diff --git a/src/util/buffers/strings.ts b/src/util/buffers/strings.ts new file mode 100644 index 0000000000..f4ac5b7378 --- /dev/null +++ b/src/util/buffers/strings.ts @@ -0,0 +1,16 @@ +import {bufferToUint8Array} from './bufferToUint8Array'; + +export const ascii = (txt: TemplateStringsArray | string | [string]): Uint8Array => { + if (typeof txt === 'string') return ascii([txt]); + [txt] = txt; + const len = txt.length; + const res = new Uint8Array(len); + for (let i = 0; i < len; i++) res[i] = txt.charCodeAt(i); + return res; +}; + +export const utf8 = (txt: TemplateStringsArray | [string] | string): Uint8Array => { + if (typeof txt === 'string') return utf8([txt]); + [txt] = txt; + return bufferToUint8Array(Buffer.from(txt, 'utf8')); +};