@@ -43,6 +43,7 @@ export interface TranscriptParseResult {
4343
4444export const GUARD_LENGTH = 64 ;
4545const STREAMING_THRESHOLD_BYTES = 8 * 1024 * 1024 ;
46+ const READ_CHUNK_BYTES = 256 * 1024 ;
4647const NEWLINE = 0x0a ;
4748const STAT_CONCURRENCY = 32 ;
4849const WALK_CONCURRENCY = 16 ;
@@ -168,6 +169,29 @@ async function guardMatches(
168169 }
169170}
170171
172+ /** Plain files reuse one buffer, so a chunk is only valid until the next one is requested. */
173+ async function * readChunks (
174+ handle : NodeFSP . FileHandle ,
175+ start : number ,
176+ compressed : boolean ,
177+ ) : AsyncGenerator < Buffer > {
178+ if ( compressed ) {
179+ // Compressed snapshots cache a full parse; decoded byte offsets cannot
180+ // resume the compressed input.
181+ yield * handle
182+ . createReadStream ( { start, autoClose : true , highWaterMark : READ_CHUNK_BYTES } )
183+ . compose ( NodeZlib . createZstdDecompress ( ) ) ;
184+ return ;
185+ }
186+ const buffer = Buffer . allocUnsafe ( READ_CHUNK_BYTES ) ;
187+ for ( let position = start ; ; ) {
188+ const { bytesRead } = await handle . read ( buffer , 0 , buffer . length , position ) ;
189+ if ( bytesRead === 0 ) return ;
190+ position += bytesRead ;
191+ yield buffer . subarray ( 0 , bytesRead ) ;
192+ }
193+ }
194+
171195// Match markers in one latin1 alternation; one character per byte preserves exact ASCII offsets.
172196export function transcriptLineGate ( markers : readonly [ string , ...string [ ] ] ) {
173197 if ( markers . length === 1 ) {
@@ -244,7 +268,8 @@ export async function readTranscriptRecords<State>(
244268
245269 const append = ( segment : Buffer ) => {
246270 if ( ! streaming && pendingBytes + segment . length <= streamingThresholdBytes ) {
247- if ( segment . length > 0 ) pendingChunks . push ( segment ) ;
271+ // Copy: the next read overwrites the chunk this segment points into.
272+ if ( segment . length > 0 ) pendingChunks . push ( Buffer . from ( segment ) ) ;
248273 pendingBytes += segment . length ;
249274 return ;
250275 }
@@ -275,15 +300,7 @@ export async function readTranscriptRecords<State>(
275300 streaming = undefined ;
276301 decoder = undefined ;
277302 } ;
278- const input = handle . createReadStream ( {
279- start,
280- autoClose : compressed ,
281- highWaterMark : 256 * 1024 ,
282- } ) ;
283- // Compressed snapshots cache a full parse; decoded byte offsets cannot
284- // resume the compressed input.
285- const stream = compressed ? input . compose ( NodeZlib . createZstdDecompress ( ) ) : input ;
286- for await ( const chunk of stream ) {
303+ for await ( const chunk of readChunks ( handle , start , compressed ) ) {
287304 let lineStart = 0 ;
288305 while ( lineStart < chunk . length ) {
289306 const newlineIndex = chunk . indexOf ( NEWLINE , lineStart ) ;
0 commit comments