@@ -221,10 +221,89 @@ export function renderToml(document: Record<string, unknown>, prefix = ""): stri
221221 return `${ [ scalars . join ( "\n" ) , tables . join ( "\n\n" ) ] . filter ( Boolean ) . join ( "\n\n" ) } \n` ;
222222}
223223
224+ /**
225+ * Ceiling on container nesting for json documents, shared by the parse-time
226+ * scanner (config-io.ts) and the serializer walk below. One constant on
227+ * purpose: the walk must accept every document the scanner admits, or a file
228+ * the classifier reported as recoverable would refuse at rewrite time. Real
229+ * configs nest a handful of levels.
230+ */
231+ export const MAX_JSON_NESTING = 1000 ;
232+
233+ /** Error messages carry the path to the offending value; keep them readable. */
234+ function clampPath ( path : string ) : string {
235+ return path . length > 200 ? `${ path . slice ( 0 , 100 ) } …${ path . slice ( - 100 ) } ` : path ;
236+ }
237+
238+ /**
239+ * JSON.stringify writes a non-finite number as `null` and -0 as `0`; any
240+ * other finite double round-trips value-exactly (literal-level rounding is
241+ * the parse-time scanner's concern), so those two are exactly what this walk
242+ * refuses — refusing more turned a state the classifier had promised as
243+ * recoverable into a permanent refusal. Documents read from disk are already
244+ * guarded at parse time, and the writer's merge layer JSON-clones documents —
245+ * normalizing these values — before serializing, so on the apply/disable path
246+ * this walk is unreachable for them: it guards the direct serializers
247+ * (preview/export builders), same posture as the YAML and TOML renderers
248+ * above, and enforces the nesting ceiling for every json caller before the
249+ * recursive JSON.stringify can turn depth into a RangeError.
250+ *
251+ * Iterative frames instead of recursion or a node stack: depth AND size of
252+ * the document are inputs under the writer of the config file. Recursion made
253+ * a deep file a RangeError-500; materializing every node with its path made a
254+ * wide file allocate a large multiple of its size. Frames keep memory
255+ * proportional to nesting depth, and path strings exist only for the
256+ * containers on the current path plus the failing value itself.
257+ */
258+ function assertJsonNumbersRoundTrip ( document : unknown , rootPath : string ) : void {
259+ const refuse = ( value : number , path : string ) : never => {
260+ throw new UnserializableValueError ( Object . is ( value , - 0 )
261+ ? `JSON cannot rewrite -0 at ${ clampPath ( path ) } without changing it to 0`
262+ : `JSON cannot rewrite the number at ${ clampPath ( path ) } without changing it to null` ) ;
263+ } ;
264+ if ( typeof document === "number" && ( ! Number . isFinite ( document ) || Object . is ( document , - 0 ) ) ) {
265+ refuse ( document , rootPath ) ;
266+ }
267+ type Frame = { container : unknown ; keys : string [ ] | null ; index : number ; prefix : string } ;
268+ const frames : Frame [ ] = [ ] ;
269+ const pushContainer = ( value : unknown , prefix : string ) => {
270+ if ( Array . isArray ( value ) ) frames . push ( { container : value , keys : null , index : 0 , prefix } ) ;
271+ else if ( isPlainRecord ( value ) ) frames . push ( { container : value , keys : Object . keys ( value ) , index : 0 , prefix } ) ;
272+ } ;
273+ pushContainer ( document , rootPath ) ;
274+ while ( frames . length > 0 ) {
275+ const frame = frames [ frames . length - 1 ] ! ;
276+ const length = frame . keys ? frame . keys . length : ( frame . container as unknown [ ] ) . length ;
277+ if ( frame . index >= length ) { frames . pop ( ) ; continue ; }
278+ const i = frame . index ;
279+ frame . index += 1 ;
280+ const child = frame . keys
281+ ? ( frame . container as Record < string , unknown > ) [ frame . keys [ i ] ! ]
282+ : ( frame . container as unknown [ ] ) [ i ] ;
283+ const childPath = ( ) => frame . keys
284+ ? ( frame . prefix === "$" ? frame . keys [ i ] ! : `${ frame . prefix } .${ frame . keys [ i ] ! } ` )
285+ : `${ frame . prefix } [${ i } ]` ;
286+ if ( typeof child === "number" ) {
287+ if ( ! Number . isFinite ( child ) || Object . is ( child , - 0 ) ) refuse ( child , childPath ( ) ) ;
288+ continue ;
289+ }
290+ if ( typeof child === "object" && child !== null ) {
291+ if ( frames . length >= MAX_JSON_NESTING ) {
292+ throw new UnserializableValueError (
293+ `the document nests deeper than ${ MAX_JSON_NESTING } levels at ${ clampPath ( childPath ( ) ) } , which JSON serialization cannot rewrite safely` ) ;
294+ }
295+ pushContainer ( child , childPath ( ) ) ;
296+ }
297+ }
298+ }
299+
224300/** Every serializer returns text ending in exactly one newline. */
225301export function serializeDocument ( document : unknown , format : ConfigFormat ) : string {
226302 switch ( format ) {
227- case "json" : return `${ JSON . stringify ( document , null , 2 ) } \n` ;
303+ case "json" : {
304+ assertJsonNumbersRoundTrip ( document , "$" ) ;
305+ return `${ JSON . stringify ( document , null , 2 ) } \n` ;
306+ }
228307 case "json5" : return `${ Bun . JSON5 . stringify ( document , null , 2 ) } \n` ;
229308 case "yaml" : return renderYaml ( document ) ;
230309 case "toml" : {
0 commit comments