Swadhin Biswas feat: implement mervelas cli engine, built-in tools, and reactive UI · 4/3/2026
0358986
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// ---------------------------------------------------------------------------
// readFileInRange — line-oriented file reader with two code paths
// ---------------------------------------------------------------------------
//
// Returns lines [offset, offset + maxLines) from a file.
//
// Fast path (regular files < 10 MB):
// Opens the file, stats the fd, reads the whole file with readFile(),
// then splits lines in memory. This avoids the per-chunk async overhead
// of createReadStream and is ~2x faster for typical source files.
//
// Streaming path (large files, pipes, devices, etc.):
// Uses createReadStream with manual indexOf('\n') scanning. Content is
// only accumulated for lines inside the requested range — lines outside
// the range are counted (for totalLines) but discarded, so reading line
// 1 of a 100 GB file won't balloon RSS.
//
// All event handlers (streamOnOpen/Data/End) are module-level named
// functions with zero closures. State lives in a StreamState object;
// handlers access it via `this`, bound at registration time.
//
// Lifecycle: `open`, `end`, and `error` use .once() (auto-remove).
// `data` fires until the stream ends or is destroyed — either way the
// stream and state become unreachable together and are GC'd.
//
// On error (including maxBytes exceeded), stream.destroy(err) emits
// 'error' → reject (passed directly to .once('error')).
//
// Both paths strip UTF-8 BOM and \r (CRLF → LF).
//
// mtime comes from fstat/stat on the already-open fd — no extra open().
//
// maxBytes behavior depends on options.truncateOnByteLimit:
// false (default): legacy semantics — throws FileTooLargeError if the FILE
// size (fast path) or total streamed bytes (streaming) exceed maxBytes.
// true: caps SELECTED OUTPUT at maxBytes. Stops at the last complete line
// that fits; sets truncatedByBytes in the result. Never throws.
// ---------------------------------------------------------------------------
import { createReadStream, fstat } from 'fs'
import { stat as fsStat, readFile } from 'fs/promises'
import { formatFileSize } from './format.js'
const FAST_PATH_MAX_SIZE = 10 * 1024 * 1024 // 10 MB
export type ReadFileRangeResult = {
content: string
lineCount: number
totalLines: number
totalBytes: number
readBytes: number
mtimeMs: number
/** true when output was clipped to maxBytes under truncate mode */
truncatedByBytes?: boolean
}
export class FileTooLargeError extends Error {
constructor(
public sizeInBytes: number,
public maxSizeBytes: number,
) {
super(
`File content (${formatFileSize(sizeInBytes)}) exceeds maximum allowed size (${formatFileSize(maxSizeBytes)}). Use offset and limit parameters to read specific portions of the file, or search for specific content instead of reading the whole file.`,
)
this.name = 'FileTooLargeError'
}
}
// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------
export async function readFileInRange(
filePath: string,
offset = 0,
maxLines?: number,
maxBytes?: number,
signal?: AbortSignal,
options?: { truncateOnByteLimit?: boolean },
): Promise<ReadFileRangeResult> {
signal?.throwIfAborted()
const truncateOnByteLimit = options?.truncateOnByteLimit ?? false
// stat to decide the code path and guard against OOM.
// For regular files under 10 MB: readFile + in-memory split (fast).
// Everything else (large files, FIFOs, devices): streaming.
const stats = await fsStat(filePath)
if (stats.isDirectory()) {
throw new Error(
`EISDIR: illegal operation on a directory, read '${filePath}'`,
)
}
if (stats.isFile() && stats.size < FAST_PATH_MAX_SIZE) {
if (
!truncateOnByteLimit &&
maxBytes !== undefined &&
stats.size > maxBytes
) {
throw new FileTooLargeError(stats.size, maxBytes)
}
const text = await readFile(filePath, { encoding: 'utf8', signal })
return readFileInRangeFast(
text,
stats.mtimeMs,
offset,
maxLines,
truncateOnByteLimit ? maxBytes : undefined,
)
}
return readFileInRangeStreaming(
filePath,
offset,
maxLines,
maxBytes,
truncateOnByteLimit,
signal,
)
}
// ---------------------------------------------------------------------------
// Fast path — readFile + in-memory split
// ---------------------------------------------------------------------------
function readFileInRangeFast(
raw: string,
mtimeMs: number,
offset: number,
maxLines: number | undefined,
truncateAtBytes: number | undefined,
): ReadFileRangeResult {
const endLine = maxLines !== undefined ? offset + maxLines : Infinity
// Strip BOM.
const text = raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw
// Split lines, strip \r, select range.
const selectedLines: string[] = []
let lineIndex = 0
let startPos = 0
let newlinePos: number
let selectedBytes = 0
let truncatedByBytes = false
function tryPush(line: string): boolean {
if (truncateAtBytes !== undefined) {
const sep = selectedLines.length > 0 ? 1 : 0
const nextBytes = selectedBytes + sep + Buffer.byteLength(line)
if (nextBytes > truncateAtBytes) {
truncatedByBytes = true
return false
}
selectedBytes = nextBytes
}
selectedLines.push(line)
return true
}
while ((newlinePos = text.indexOf('\n', startPos)) !== -1) {
if (lineIndex >= offset && lineIndex < endLine && !truncatedByBytes) {
let line = text.slice(startPos, newlinePos)
if (line.endsWith('\r')) {
line = line.slice(0, -1)
}
tryPush(line)
}
lineIndex++
startPos = newlinePos + 1
}
// Final fragment (no trailing newline).
if (lineIndex >= offset && lineIndex < endLine && !truncatedByBytes) {
let line = text.slice(startPos)
if (line.endsWith('\r')) {
line = line.slice(0, -1)
}
tryPush(line)
}
lineIndex++
const content = selectedLines.join('\n')
return {
content,
lineCount: selectedLines.length,
totalLines: lineIndex,
totalBytes: Buffer.byteLength(text, 'utf8'),
readBytes: Buffer.byteLength(content, 'utf8'),
mtimeMs,
...(truncatedByBytes ? { truncatedByBytes: true } : {}),
}
}
// ---------------------------------------------------------------------------
// Streaming path — createReadStream + event handlers
// ---------------------------------------------------------------------------
type StreamState = {
stream: ReturnType<typeof createReadStream>
offset: number
endLine: number
maxBytes: number | undefined
truncateOnByteLimit: boolean
resolve: (value: ReadFileRangeResult) => void
totalBytesRead: number
selectedBytes: number
truncatedByBytes: boolean
currentLineIndex: number
selectedLines: string[]
partial: string
isFirstChunk: boolean
resolveMtime: (ms: number) => void
mtimeReady: Promise<number>
}
function streamOnOpen(this: StreamState, fd: number): void {
fstat(fd, (err, stats) => {
this.resolveMtime(err ? 0 : stats.mtimeMs)
})
}
function streamOnData(this: StreamState, chunk: string): void {
if (this.isFirstChunk) {
this.isFirstChunk = false
if (chunk.charCodeAt(0) === 0xfeff) {
chunk = chunk.slice(1)
}
}
this.totalBytesRead += Buffer.byteLength(chunk)
if (
!this.truncateOnByteLimit &&
this.maxBytes !== undefined &&
this.totalBytesRead > this.maxBytes
) {
this.stream.destroy(
new FileTooLargeError(this.totalBytesRead, this.maxBytes),
)
return
}
const data = this.partial.length > 0 ? this.partial + chunk : chunk
this.partial = ''
let startPos = 0
let newlinePos: number
while ((newlinePos = data.indexOf('\n', startPos)) !== -1) {
if (
this.currentLineIndex >= this.offset &&
this.currentLineIndex < this.endLine
) {
let line = data.slice(startPos, newlinePos)
if (line.endsWith('\r')) {
line = line.slice(0, -1)
}
if (this.truncateOnByteLimit && this.maxBytes !== undefined) {
const sep = this.selectedLines.length > 0 ? 1 : 0
const nextBytes = this.selectedBytes + sep + Buffer.byteLength(line)
if (nextBytes > this.maxBytes) {
// Cap hit — collapse the selection range so nothing more is
// accumulated. Stream continues (to count totalLines).
this.truncatedByBytes = true
this.endLine = this.currentLineIndex
} else {
this.selectedBytes = nextBytes
this.selectedLines.push(line)
}
} else {
this.selectedLines.push(line)
}
}
this.currentLineIndex++
startPos = newlinePos + 1
}
// Only keep the trailing fragment when inside the selected range.
// Outside the range we just count newlines — discarding prevents
// unbounded memory growth on huge single-line files.
if (startPos < data.length) {
if (
this.currentLineIndex >= this.offset &&
this.currentLineIndex < this.endLine
) {
const fragment = data.slice(startPos)
// In truncate mode, `partial` can grow unboundedly if the selected
// range contains a huge single line (no newline across many chunks).
// Once the fragment alone would overflow the remaining budget, we know
// the completed line can never fit — set truncated, collapse the
// selection range, and discard the fragment to stop accumulation.
if (this.truncateOnByteLimit && this.maxBytes !== undefined) {
const sep = this.selectedLines.length > 0 ? 1 : 0
const fragBytes = this.selectedBytes + sep + Buffer.byteLength(fragment)
if (fragBytes > this.maxBytes) {
this.truncatedByBytes = true
this.endLine = this.currentLineIndex
return
}
}
this.partial = fragment
}
}
}
function streamOnEnd(this: StreamState): void {
let line = this.partial
if (line.endsWith('\r')) {
line = line.slice(0, -1)
}
if (
this.currentLineIndex >= this.offset &&
this.currentLineIndex < this.endLine
) {
if (this.truncateOnByteLimit && this.maxBytes !== undefined) {
const sep = this.selectedLines.length > 0 ? 1 : 0
const nextBytes = this.selectedBytes + sep + Buffer.byteLength(line)
if (nextBytes > this.maxBytes) {
this.truncatedByBytes = true
} else {
this.selectedLines.push(line)
}
} else {
this.selectedLines.push(line)
}
}
this.currentLineIndex++
const content = this.selectedLines.join('\n')
const truncated = this.truncatedByBytes
this.mtimeReady.then(mtimeMs => {
this.resolve({
content,
lineCount: this.selectedLines.length,
totalLines: this.currentLineIndex,
totalBytes: this.totalBytesRead,
readBytes: Buffer.byteLength(content, 'utf8'),
mtimeMs,
...(truncated ? { truncatedByBytes: true } : {}),
})
})
}
function readFileInRangeStreaming(
filePath: string,
offset: number,
maxLines: number | undefined,
maxBytes: number | undefined,
truncateOnByteLimit: boolean,
signal?: AbortSignal,
): Promise<ReadFileRangeResult> {
return new Promise((resolve, reject) => {
const state: StreamState = {
stream: createReadStream(filePath, {
encoding: 'utf8',
highWaterMark: 512 * 1024,
...(signal ? { signal } : undefined),
}),
offset,
endLine: maxLines !== undefined ? offset + maxLines : Infinity,
maxBytes,
truncateOnByteLimit,
resolve,
totalBytesRead: 0,
selectedBytes: 0,
truncatedByBytes: false,
currentLineIndex: 0,
selectedLines: [],
partial: '',
isFirstChunk: true,
resolveMtime: () => {},
mtimeReady: null as unknown as Promise<number>,
}
state.mtimeReady = new Promise<number>(r => {
state.resolveMtime = r
})
state.stream.once('open', streamOnOpen.bind(state))
state.stream.on('data', streamOnData.bind(state))
state.stream.once('end', streamOnEnd.bind(state))
state.stream.once('error', reject)
})
}