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

Function createBufferedWriter

source code/utils/bufferedWriter.ts:11–102  ·  view source on GitHub ↗
({
  writeFn,
  flushIntervalMs = 1000,
  maxBufferSize = 100,
  maxBufferBytes = Infinity,
  immediateMode = false,
}: {
  writeFn: WriteFn
  flushIntervalMs?: number
  maxBufferSize?: number
  maxBufferBytes?: number
  immediateMode?: boolean
})

Source from the content-addressed store, hash-verified

9}
10
11export function createBufferedWriter({
12 writeFn,
13 flushIntervalMs = 1000,
14 maxBufferSize = 100,
15 maxBufferBytes = Infinity,
16 immediateMode = false,
17}: {
18 writeFn: WriteFn
19 flushIntervalMs?: number
20 maxBufferSize?: number
21 maxBufferBytes?: number
22 immediateMode?: boolean
23}): BufferedWriter {
24 let buffer: string[] = []
25 let bufferBytes = 0
26 let flushTimer: NodeJS.Timeout | null = null
27 // Batch detached by overflow that hasn't been written yet. Tracked so
28 // flush()/dispose() can drain it synchronously if the process exits
29 // before the setImmediate fires.
30 let pendingOverflow: string[] | null = null
31
32 function clearTimer(): void {
33 if (flushTimer) {
34 clearTimeout(flushTimer)
35 flushTimer = null
36 }
37 }
38
39 function flush(): void {
40 if (pendingOverflow) {
41 writeFn(pendingOverflow.join(''))
42 pendingOverflow = null
43 }
44 if (buffer.length === 0) return
45 writeFn(buffer.join(''))
46 buffer = []
47 bufferBytes = 0
48 clearTimer()
49 }
50
51 function scheduleFlush(): void {
52 if (!flushTimer) {
53 flushTimer = setTimeout(flush, flushIntervalMs)
54 }
55 }
56
57 // Detach the buffer synchronously so the caller never waits on writeFn.
58 // writeFn may block (e.g. errorLogSink.ts appendFileSync) — if overflow fires
59 // mid-render or mid-keystroke, deferring the write keeps the current tick
60 // short. Timer-based flushes already run outside user code paths so they
61 // stay synchronous.
62 function flushDeferred(): void {
63 if (pendingOverflow) {
64 // A previous overflow write is still queued. Coalesce into it to
65 // preserve ordering — writes land in a single setImmediate-ordered batch.
66 pendingOverflow.push(...buffer)
67 buffer = []
68 bufferBytes = 0

Callers 3

createJsonlWriterFunction · 0.85
installAsciicastRecorderFunction · 0.85
getDebugWriterFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected