11import * as SchemaAST from "effect/SchemaAST" ;
2- import { isMany , none , type Many } from "stream-chain/defs.js" ;
2+ import { none , type Many } from "stream-chain/defs.js" ;
33import { Assembler } from "stream-json/core/assembler.js" ;
4- import { filter } from "stream-json/core/filters/filter.js" ;
54import * as StreamJson from "stream-json/core/parser.js" ;
65import type { ParserOptions , Token } from "stream-json/core/parser.js" ;
76
@@ -55,6 +54,7 @@ export class TranscriptJsonLimitError extends Error {}
5554export function createTranscriptJsonReader (
5655 reserve : ( bytes : number ) => void ,
5756 selectPath : ( path : JsonPath ) => boolean ,
57+ options ?: { readonly maxDepth ?: number } ,
5858) {
5959 // The synchronous tokenizer is exported at runtime in 3.6.0, but omitted
6060 // from its bundled types. Unlike parser(), it does not wrap tokens in an
@@ -65,9 +65,6 @@ export function createTranscriptJsonReader(
6565 ) => ( input : string | typeof none ) => Many < Token > | typeof none ;
6666 } ;
6767 const tokenize = jsonParser ( { packValues : false } ) ;
68- const select = filter ( { filter : selectPath , streamKeys : false } ) as (
69- input : Token | typeof none ,
70- ) => Token | Many < Token > | typeof none ;
7168 const assembler = new Assembler ( ) ;
7269 let key : string | null = null ;
7370 let value = "" ;
@@ -100,13 +97,62 @@ export function createTranscriptJsonReader(
10097 assembler . consume ( token ) ;
10198 }
10299 } ;
100+ // Forward actual selected keys instead of reconstructing them from path
101+ // changes: adjacent duplicate keys have the same path but JSON.parse keeps
102+ // the last value. Reconstructing paths can silently retain the first value.
103+ const stack : Array < { path : JsonPath ; key : string | number | null ; selected : boolean } > = [ ] ;
104+ let selectedValue = false ;
105+ const startValue = ( ) => {
106+ const parent = stack . at ( - 1 ) ;
107+ const path = parent ?. selected ? [ ...parent . path , parent . key ] : [ ] ;
108+ const selected = ( parent ?. selected ?? true ) && selectPath ( path ) ;
109+ if ( selected && typeof parent ?. key === "string" ) {
110+ assemble ( { name : "keyValue" , value : parent . key } ) ;
111+ }
112+ return { path, selected } ;
113+ } ;
114+ const endValue = ( ) => {
115+ const parent = stack . at ( - 1 ) ;
116+ if ( parent && typeof parent . key === "number" ) parent . key ++ ;
117+ } ;
103118 const selectToken = ( token : Token | typeof none ) => {
104- const selected = select ( token ) ;
105- if ( selected === none ) return ;
106- if ( isMany ( selected ) ) {
107- for ( const item of selected . values ) assemble ( item ) ;
108- } else {
109- assemble ( selected ) ;
119+ if ( token === none ) return ;
120+ switch ( token . name ) {
121+ case "keyValue" : {
122+ const parent = stack . at ( - 1 ) ;
123+ if ( parent ) parent . key = token . value ;
124+ return ;
125+ }
126+ case "startObject" :
127+ case "startArray" : {
128+ const frame = startValue ( ) ;
129+ stack . push ( { ...frame , key : token . name === "startArray" ? 0 : null } ) ;
130+ if ( frame . selected ) assemble ( token ) ;
131+ return ;
132+ }
133+ case "endObject" :
134+ case "endArray" :
135+ if ( stack . pop ( ) ?. selected ) assemble ( token ) ;
136+ endValue ( ) ;
137+ return ;
138+ case "startString" :
139+ case "startNumber" :
140+ selectedValue = startValue ( ) . selected ;
141+ if ( selectedValue ) assemble ( token ) ;
142+ return ;
143+ case "endString" :
144+ case "endNumber" :
145+ if ( selectedValue ) assemble ( token ) ;
146+ endValue ( ) ;
147+ return ;
148+ case "nullValue" :
149+ case "trueValue" :
150+ case "falseValue" :
151+ if ( startValue ( ) . selected ) assemble ( token ) ;
152+ endValue ( ) ;
153+ return ;
154+ default :
155+ if ( selectedValue ) assemble ( token ) ;
110156 }
111157 } ;
112158 const consume = ( input : string | typeof none ) => {
@@ -116,8 +162,8 @@ export function createTranscriptJsonReader(
116162 if ( tokens === none ) return ;
117163 for ( const token of tokens . values ) {
118164 if ( token . name === "startObject" || token . name === "startArray" ) {
119- if ( ++ depth > 128 )
120- throw new TranscriptJsonLimitError ( "Transcript JSON nesting exceeds 128 levels " ) ;
165+ if ( ++ depth > ( options ?. maxDepth ?? 128 ) )
166+ throw new TranscriptJsonLimitError ( "Transcript JSON nesting exceeds the depth limit " ) ;
121167 } else if ( token . name === "endObject" || token . name === "endArray" ) {
122168 if ( -- depth === 0 ) complete = true ;
123169 }
0 commit comments