MCPcopy Create free account
hub / github.com/Noumena-Network/code / ripGrepStream

Function ripGrepStream

src/utils/ripgrep.ts:287–335  ·  view source on GitHub ↗
(
  args: string[],
  target: string,
  abortSignal: AbortSignal,
  onLines: (lines: string[]) => void,
)

Source from the content-addressed store, hash-verified

285 * timeout, stderr is ignored; interactive callers own recovery.
286 */
287export async function ripGrepStream(
288 args: string[],
289 target: string,
290 abortSignal: AbortSignal,
291 onLines: (lines: string[]) => void,
292): Promise<void> {
293 await codesignRipgrepIfNecessary()
294 const { rgPath, rgArgs, argv0 } = ripgrepCommand()
295
296 return new Promise<void>((resolve, reject) => {
297 const child = spawn(rgPath, [...rgArgs, ...args, target], {
298 argv0,
299 signal: abortSignal,
300 windowsHide: true,
301 stdio: ['ignore', 'pipe', 'ignore'],
302 })
303
304 const stripCR = (l: string) => (l.endsWith('\r') ? l.slice(0, -1) : l)
305 let remainder = ''
306 child.stdout?.on('data', (chunk: Buffer) => {
307 const data = remainder + chunk.toString()
308 const lines = data.split('\n')
309 remainder = lines.pop() ?? ''
310 if (lines.length) onLines(lines.map(stripCR))
311 })
312
313 // On Windows, both 'close' and 'error' can fire for the same process.
314 let settled = false
315 child.on('close', code => {
316 if (settled) return
317 // Abort races close — don't flush a torn tail from a killed process.
318 // Promise still settles: spawn's signal option fires 'error' with
319 // AbortError → reject below.
320 if (abortSignal.aborted) return
321 settled = true
322 if (code === 0 || code === 1) {
323 if (remainder) onLines([stripCR(remainder)])
324 resolve()
325 } else {
326 reject(new Error(`ripgrep exited with code ${code}`))
327 }
328 })
329 child.on('error', err => {
330 if (settled) return
331 settled = true
332 reject(err)
333 })
334 })
335}
336
337export async function ripGrep(
338 args: string[],

Callers 1

_temp4Function · 0.85

Calls 6

ripgrepCommandFunction · 0.85
spawnFunction · 0.85
stripCRFunction · 0.85
resolveFunction · 0.70
toStringMethod · 0.65

Tested by

no test coverage detected