Skip to content

Commit 3e17c2d

Browse files
committed
Fix map/set ordering on decode
1 parent 561ee42 commit 3e17c2d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

packages/react-router/__tests__/vendor/turbo-stream-test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ test("should encode and decode Map", async () => {
120120
expect(output).toEqual(input);
121121
});
122122

123+
test("should maintain order of Map entries", async () => {
124+
const input = new Map([
125+
["foo", "bar"],
126+
["baz", "qux"],
127+
]);
128+
const output = await quickDecode(encode(input));
129+
expect(Array.from(output as typeof input)).toEqual(Array.from(input));
130+
});
131+
123132
test("should encode and decode empty Map", async () => {
124133
const input = new Map();
125134
const output = await quickDecode(encode(input));
@@ -132,6 +141,12 @@ test("should encode and decode Set", async () => {
132141
expect(output).toEqual(input);
133142
});
134143

144+
test("should maintain order of Set entries", async () => {
145+
const input = new Set(["foo", "bar"]);
146+
const output = await quickDecode(encode(input));
147+
expect(Array.from(output as typeof input)).toEqual(Array.from(input));
148+
});
149+
135150
test("should encode and decode empty Set", async () => {
136151
const input = new Set();
137152
const output = await quickDecode(encode(input));

packages/react-router/vendor/turbo-stream-v2/unflatten.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function hydrate(this: ThisDecode, index: number): any {
117117
case TYPE_SET:
118118
const newSet = new Set();
119119
hydrated[index] = newSet;
120-
for (let i = 1; i < value.length; i++)
120+
for (let i = value.length - 1; i > 0; i--)
121121
stack.push([
122122
value[i],
123123
(v) => {
@@ -129,7 +129,7 @@ function hydrate(this: ThisDecode, index: number): any {
129129
case TYPE_MAP:
130130
const map = new Map();
131131
hydrated[index] = map;
132-
for (let i = 1; i < value.length; i += 2) {
132+
for (let i = value.length - 2; i > 0; i -= 2) {
133133
const r: any[] = [];
134134
stack.push([
135135
value[i + 1],

0 commit comments

Comments
 (0)