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
Prev Previous commit
test: keep one attribute case per decoder path
Three cases cover what the patch touches - top level, nested, and the
map-key path with Buffer mapping - and the decoder comments are down to
one line per case.
  • Loading branch information
Develop-KIM committed Oct 7, 2026
commit a9f096b1095bbf7029c166091d811e2409573866
20 changes: 5 additions & 15 deletions lib/resp/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ 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.
// Attribute contents aren't surfaced to callers.
function discardAttribute() {}

// this was written with performance in mind, so it's not very readable... sorry :(
Expand Down Expand Up @@ -201,11 +200,7 @@ 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.
// RESP3 attribute: consume and discard, then decode the reply it decorates
case RESP_TYPES.ATTRIBUTE:
return this.#handleDecodedValue(
discardAttribute,
Expand Down Expand Up @@ -657,8 +652,7 @@ 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.
// RESP3 attribute: the element it decorates takes its place
case RESP_TYPES.ATTRIBUTE:
return this.#decodeAttribute(
this.#decodeMap(typeMapping, chunk),
Expand All @@ -673,9 +667,7 @@ export class Decoder {
}
}

// `decodeNext` decodes whatever the attribute decorates, and has to be the
// decoder for the position the attribute was found in: a map key still needs
// #decodeMapKey, which forces string keys, and not the generic nested path.
// `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);
Expand Down Expand Up @@ -916,9 +908,7 @@ export class Decoder {
case RESP_TYPES.BLOB_STRING:
return this.#decodeBlobString(String, chunk);

// An attributed key is still a key: resume on the key path so it keeps
// being coerced to a string. Leaving it to the nested path would hand
// #decodeMapAsObject a Buffer key, which bypasses its `__proto__` guard.
// 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),
Expand Down
74 changes: 0 additions & 74 deletions test/unit/resp/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,6 @@ 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(
Expand All @@ -564,68 +560,11 @@ describe("RESP Decoder", () => {
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 }],
});

// An attributed key has to stay on the map-key path, which forces keys to
// strings. A Buffer key would read back as `Buffer(foo)` from a Map and
// would walk straight past the `__proto__` guard in #decodeMapAsObject.
test("on a map key with blob strings as Buffer", {
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$3\r\nfoo\r\n:1\r\n"),
replies: [{ foo: 1 }],
});

test("on a map key as Map with blob strings as Buffer", {
typeMapping: {
[RESP_TYPES.MAP]: Map,
[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$3\r\nfoo\r\n:1\r\n"),
replies: [new Map([["foo", 1]])],
});

test("on a __proto__ map key", {
typeMapping: {
[RESP_TYPES.SIMPLE_STRING]: Buffer,
Expand All @@ -643,19 +582,6 @@ describe("RESP Decoder", () => {
}),
],
});

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", () => {
Expand Down
Loading