Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 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,10 @@ export const PUSH_TYPE_MAPPING = {
[RESP_TYPES.BLOB_STRING]: Buffer,
};

// Attribute contents aren't surfaced to callers; this keeps the decoded map
// from being reported as a reply of its own.
function discardAttribute() {}

// this was written with performance in mind, so it's not very readable... sorry :(

interface DecoderOptions {
Expand Down Expand Up @@ -196,6 +201,17 @@ export class Decoder {
this.#decodeMap(this.getTypeMapping(), chunk)
);

// An attribute is metadata for the reply that follows it, not a reply of
// its own. The spec lets clients ignore the contents, but they still have
// to be consumed: leaving them in the stream turns the reply behind them
// into a protocol error. Dropping out of #decodeTypeValue without calling
// a callback resumes the main loop on the real reply.
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 +657,36 @@ export class Decoder {
case RESP_TYPES.MAP:
return this.#decodeMap(typeMapping, chunk);

// Nested attributes decorate the element that follows them, so the
// element takes their place in the array/set/map being decoded.
case RESP_TYPES.ATTRIBUTE:
return this.#decodeAttribute(
this.#decodeMap(typeMapping, chunk),
typeMapping,
chunk
);

default:
throw new Error(
`Unknown RESP type ${type} "${String.fromCharCode(type)}"`
);
}
}

#decodeAttribute(attribute, typeMapping, chunk) {
if (typeof attribute === "function") {
return this.#continueDecodeAttribute.bind(this, attribute, typeMapping);
}

return this.#cursor >= chunk.length
? this.#decodeNestedType.bind(this, typeMapping)
: this.#decodeNestedType(typeMapping, chunk);
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}

#continueDecodeAttribute(attributeCb, typeMapping, chunk) {
return this.#decodeAttribute(attributeCb(chunk), typeMapping, chunk);
}

#decodeArray(typeMapping, chunk) {
// RESP 2 null
// https://github.com/redis/redis-specifications/blob/master/protocol/RESP2.md#resp-arrays
Expand Down
66 changes: 66 additions & 0 deletions test/unit/resp/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,72 @@ describe("RESP Decoder", () => {
});
});

// Attributes are metadata attached to the reply behind them. They carry no
// reply of their own, so nothing reaches onReply until the real reply is
// decoded - and a client that doesn't consume them reads that real reply as
// an unknown type and kills the connection.
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("empty", {
toWrite: Buffer.from("|0\r\n+OK\r\n"),
replies: ["OK"],
});

test("two in a row", {
toWrite: Buffer.from("|1\r\n+a\r\n+b\r\n|1\r\n+c\r\n+d\r\n:1\r\n"),
replies: [1],
});

test("before an error", {
toWrite: Buffer.from("|1\r\n+a\r\n+b\r\n-ERROR\r\n"),
errorReplies: [new SimpleError("ERROR")],
});

test("before a push", {
toWrite: Buffer.from("|1\r\n+a\r\n+b\r\n>2\r\n:0\r\n:1\r\n"),
pushReplies: [[0, 1]],
});

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 nested array element", {
toWrite: Buffer.from("*1\r\n*1\r\n|1\r\n+a\r\n+b\r\n:1\r\n"),
replies: [[[1]]],
});

test("on a map value", {
toWrite: Buffer.from("%1\r\n+key\r\n|1\r\n+a\r\n+b\r\n:1\r\n"),
replies: [{ key: 1 }],
});

test("on a map key", {
toWrite: Buffer.from("%1\r\n|1\r\n+a\r\n+b\r\n+key\r\n:1\r\n"),
replies: [{ key: 1 }],
});

test("on a set element as Set", {
typeMapping: {
[RESP_TYPES.SET]: Set,
},
toWrite: Buffer.from("~1\r\n|1\r\n+a\r\n+b\r\n:1\r\n"),
replies: [new Set([1])],
});

test("with a map value of its own", {
toWrite: Buffer.from("|1\r\n+a\r\n%1\r\n+b\r\n:1\r\n:2\r\n"),
replies: [2],
});
});

describe("Push", () => {
test("[]", {
toWrite: Buffer.from(">0\r\n"),
Expand Down
Loading