Skip to content

Commit 74db548

Browse files
committed
fix(core): fallback to safe string payload in stringifyIO when superjson fails
1 parent 4e289b5 commit 74db548

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, expect, it } from "vitest";
2+
import { stringifyIO } from "./ioSerialization.js";
3+
4+
describe("stringifyIO", () => {
5+
it("returns undefined data for undefined input", async () => {
6+
const result = await stringifyIO(undefined);
7+
expect(result).toEqual({ dataType: "application/json" });
8+
});
9+
10+
it("returns plain text for string input", async () => {
11+
const result = await stringifyIO("hello world");
12+
expect(result).toEqual({ data: "hello world", dataType: "text/plain" });
13+
});
14+
15+
it("serializes normal objects using super+json", async () => {
16+
const result = await stringifyIO({ key: "value", num: 42 });
17+
expect(result.dataType).toEqual("application/super+json");
18+
expect(typeof result.data).toBe("string");
19+
});
20+
21+
it("fallback returns string data when superjson fails", async () => {
22+
// Create an object where superjson.stringify throws or handles non-standard values
23+
const cyclic: any = { name: "test" };
24+
cyclic.self = cyclic;
25+
26+
const result = await stringifyIO(cyclic);
27+
expect(typeof result.data).toBe("string");
28+
});
29+
});

packages/core/src/v3/utils/ioSerialization.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ export async function stringifyIO(value: any): Promise<IOPacket> {
9696

9797
return { data, dataType: "application/super+json" };
9898
} catch {
99-
return { data: value, dataType: "application/json" };
99+
try {
100+
const data = JSON.stringify(value, makeSafeReplacer());
101+
102+
return { data, dataType: "application/json" };
103+
} catch {
104+
return { data: String(value), dataType: "text/plain" };
105+
}
100106
}
101107
}
102108

0 commit comments

Comments
 (0)