refactor(file): decode ripgrep chunks synchronously

This commit is contained in:
Kit Langton 2026-04-13 11:18:31 -04:00
commit 75a87ffc5e

View file

@ -3,7 +3,7 @@ import path from "path"
import { Global } from "../global" import { Global } from "../global"
import fs from "fs/promises" import fs from "fs/promises"
import z from "zod" import z from "zod"
import { Effect, Layer, Context, Schema } from "effect" import { Array as Arr, Effect, Layer, Context, Schema } from "effect"
import * as Stream from "effect/Stream" import * as Stream from "effect/Stream"
import { ChildProcess } from "effect/unstable/process" import { ChildProcess } from "effect/unstable/process"
import { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner" import { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner"
@ -126,7 +126,7 @@ export namespace Ripgrep {
Schema.Struct({ type: Schema.Literal("summary"), data: Schema.Unknown }), Schema.Struct({ type: Schema.Literal("summary"), data: Schema.Unknown }),
]) ])
const decode = Schema.decodeUnknownEffect(Schema.fromJsonString(Row)) const decode = Schema.decodeUnknownSync(Schema.fromJsonString(Row))
export type Result = z.infer<typeof Result> export type Result = z.infer<typeof Result>
export type Match = z.infer<typeof Match> export type Match = z.infer<typeof Match>
@ -428,8 +428,11 @@ export namespace Ripgrep {
Stream.decodeText(handle.stdout).pipe( Stream.decodeText(handle.stdout).pipe(
Stream.splitLines, Stream.splitLines,
Stream.filter((line) => line.length > 0), Stream.filter((line) => line.length > 0),
Stream.mapEffect((line) => Stream.mapArrayEffect((lines) =>
decode(line).pipe(Effect.mapError((cause) => new Error("invalid ripgrep output", { cause }))), Effect.try({
try: () => Arr.map(lines, (line) => decode(line)),
catch: (cause) => new Error("invalid ripgrep output", { cause }),
}),
), ),
Stream.filter((row): row is Schema.Schema.Type<typeof Hit> => row.type === "match"), Stream.filter((row): row is Schema.Schema.Type<typeof Hit> => row.type === "match"),
Stream.map((row): Item => row.data), Stream.map((row): Item => row.data),