diff --git a/lib/resp/decoder.ts b/lib/resp/decoder.ts index b2312c57d..377e58ebe 100644 --- a/lib/resp/decoder.ts +++ b/lib/resp/decoder.ts @@ -25,6 +25,7 @@ export const RESP_TYPES = { ARRAY: 42, // * SET: 126, // ~ MAP: 37, // % + ATTRIBUTE: 124, // | PUSH: 62, // > } as const; @@ -40,6 +41,9 @@ export const PUSH_TYPE_MAPPING = { [RESP_TYPES.BLOB_STRING]: Buffer, }; +// Attribute contents aren't surfaced to callers. +function discardAttribute() {} + // this was written with performance in mind, so it's not very readable... sorry :( interface DecoderOptions { @@ -196,6 +200,13 @@ export class Decoder { this.#decodeMap(this.getTypeMapping(), chunk) ); + // RESP3 attribute: consume and discard, then decode the reply it decorates + case RESP_TYPES.ATTRIBUTE: + return this.#handleDecodedValue( + discardAttribute, + this.#decodeMap(this.getTypeMapping(), chunk) + ); + case RESP_TYPES.PUSH: return this.#handleDecodedValue( this.onPush, @@ -641,6 +652,14 @@ export class Decoder { case RESP_TYPES.MAP: return this.#decodeMap(typeMapping, chunk); + // RESP3 attribute: the element it decorates takes its place + case RESP_TYPES.ATTRIBUTE: + return this.#decodeAttribute( + this.#decodeMap(typeMapping, chunk), + this.#decodeNestedType.bind(this, typeMapping), + chunk + ); + default: throw new Error( `Unknown RESP type ${type} "${String.fromCharCode(type)}"` @@ -648,6 +667,19 @@ export class Decoder { } } + // `decodeNext` has to be the decoder for the position the attribute was found in. + #decodeAttribute(attribute, decodeNext, chunk) { + if (typeof attribute === "function") { + return this.#continueDecodeAttribute.bind(this, attribute, decodeNext); + } + + return this.#cursor >= chunk.length ? decodeNext : decodeNext(chunk); + } + + #continueDecodeAttribute(attributeCb, decodeNext, chunk) { + return this.#decodeAttribute(attributeCb(chunk), decodeNext, chunk); + } + #decodeArray(typeMapping, chunk) { // RESP 2 null // https://github.com/redis/redis-specifications/blob/master/protocol/RESP2.md#resp-arrays @@ -876,6 +908,14 @@ export class Decoder { case RESP_TYPES.BLOB_STRING: return this.#decodeBlobString(String, chunk); + // RESP3 attribute: resume on the key path so the key is still forced to a string + case RESP_TYPES.ATTRIBUTE: + return this.#decodeAttribute( + this.#decodeMap(typeMapping, chunk), + this.#decodeMapKey.bind(this, typeMapping), + chunk + ); + default: return this.#decodeNestedTypeValue(type, typeMapping, chunk); } diff --git a/test/unit/resp/decoder.ts b/test/unit/resp/decoder.ts index 849f3fd9a..210047b75 100644 --- a/test/unit/resp/decoder.ts +++ b/test/unit/resp/decoder.ts @@ -552,6 +552,38 @@ describe("RESP Decoder", () => { }); }); + describe("Attribute", () => { + test("DEBUG PROTOCOL attrib", { + toWrite: Buffer.from( + "|1\r\n$14\r\nkey-popularity\r\n*2\r\n$7\r\nkey:123\r\n:90\r\n$39\r\nSome real reply following the attribute\r\n" + ), + replies: ["Some real reply following the attribute"], + }); + + test("on an array element", { + toWrite: Buffer.from("*2\r\n|1\r\n+a\r\n+b\r\n:1\r\n:2\r\n"), + replies: [[1, 2]], + }); + + test("on a __proto__ map key", { + typeMapping: { + [RESP_TYPES.SIMPLE_STRING]: Buffer, + [RESP_TYPES.BLOB_STRING]: Buffer, + }, + toWrite: Buffer.from( + "%1\r\n|1\r\n+a\r\n+b\r\n$9\r\n__proto__\r\n*1\r\n:1\r\n" + ), + replies: [ + Object.defineProperty({}, "__proto__", { + value: [1], + configurable: true, + enumerable: true, + writable: true, + }), + ], + }); + }); + describe("Push", () => { test("[]", { toWrite: Buffer.from(">0\r\n"),