MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / streamOnData

Function streamOnData

source code/utils/readFileInRange.ts:226–306  ·  view source on GitHub ↗
(this: StreamState, chunk: string)

Source from the content-addressed store, hash-verified

224}
225
226function streamOnData(this: StreamState, chunk: string): void {
227 if (this.isFirstChunk) {
228 this.isFirstChunk = false
229 if (chunk.charCodeAt(0) === 0xfeff) {
230 chunk = chunk.slice(1)
231 }
232 }
233
234 this.totalBytesRead += Buffer.byteLength(chunk)
235 if (
236 !this.truncateOnByteLimit &&
237 this.maxBytes !== undefined &&
238 this.totalBytesRead > this.maxBytes
239 ) {
240 this.stream.destroy(
241 new FileTooLargeError(this.totalBytesRead, this.maxBytes),
242 )
243 return
244 }
245
246 const data = this.partial.length > 0 ? this.partial + chunk : chunk
247 this.partial = ''
248
249 let startPos = 0
250 let newlinePos: number
251 while ((newlinePos = data.indexOf('\n', startPos)) !== -1) {
252 if (
253 this.currentLineIndex >= this.offset &&
254 this.currentLineIndex < this.endLine
255 ) {
256 let line = data.slice(startPos, newlinePos)
257 if (line.endsWith('\r')) {
258 line = line.slice(0, -1)
259 }
260 if (this.truncateOnByteLimit && this.maxBytes !== undefined) {
261 const sep = this.selectedLines.length > 0 ? 1 : 0
262 const nextBytes = this.selectedBytes + sep + Buffer.byteLength(line)
263 if (nextBytes > this.maxBytes) {
264 // Cap hit — collapse the selection range so nothing more is
265 // accumulated. Stream continues (to count totalLines).
266 this.truncatedByBytes = true
267 this.endLine = this.currentLineIndex
268 } else {
269 this.selectedBytes = nextBytes
270 this.selectedLines.push(line)
271 }
272 } else {
273 this.selectedLines.push(line)
274 }
275 }
276 this.currentLineIndex++
277 startPos = newlinePos + 1
278 }
279
280 // Only keep the trailing fragment when inside the selected range.
281 // Outside the range we just count newlines — discarding prevents
282 // unbounded memory growth on huge single-line files.
283 if (startPos < data.length) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected