Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/resp/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const RESP_TYPES = {
ARRAY: 42, // *
SET: 126, // ~
MAP: 37, // %
ATTRIBUTE: 124, // |
PUSH: 62, // >
} as const;

Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -641,13 +652,34 @@ 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)}"`
);
}
}

// `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
Expand Down Expand Up @@ -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);
}
Expand Down
32 changes: 32 additions & 0 deletions test/unit/resp/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Loading