File tree Expand file tree Collapse file tree
packages/core/src/v3/utils Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments