@criogaid/pi-hashline-edit 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/README.zh-CN.md +12 -12
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +96 -271
- package/src/core/apply.ts +62 -169
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +5 -5
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +8 -44
- package/src/index.ts +8 -8
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +4 -7
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -19
- package/src/integration/grep-js-completeness.test.ts +45 -18
- package/src/integration/grep-line-candidates.test.ts +7 -10
- package/src/integration/grep-matrix.test.ts +6 -9
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +10 -9
- package/src/integration/grep-prefilter-coverage.test.ts +5 -8
- package/src/integration/grep-process-limits.test.ts +76 -0
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +166 -66
- package/src/integration/grep-scan-budget.test.ts +9 -9
- package/src/integration/grep-translation.test.ts +18 -32
- package/src/integration/large-text.test.ts +6 -52
- package/src/integration/model-budget-lifecycle.test.ts +10 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.testing.ts +3 -3
- package/src/integration/publication-read-changes.test.ts +161 -83
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +22 -12
- package/src/integration/report-replay.test.ts +23 -26
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +18 -17
- package/src/pi/action-fusion.test.ts +241 -353
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +150 -125
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +25 -28
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +334 -283
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +6 -8
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +7 -3
- package/src/pi/config.ts +12 -9
- package/src/pi/diagnostic-buffer.test.ts +34 -13
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +86 -211
- package/src/pi/error-text.ts +10 -9
- package/src/pi/execute.test.ts +249 -345
- package/src/pi/failure-context.test.ts +146 -127
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +182 -155
- package/src/pi/file-commit.test.ts +156 -71
- package/src/pi/file-commit.ts +126 -146
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +26 -22
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +19 -120
- package/src/pi/grep-candidates.ts +25 -76
- package/src/pi/grep-matrix.test.ts +5 -5
- package/src/pi/grep-order.test.ts +16 -15
- package/src/pi/grep-output.ts +94 -107
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +97 -0
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +41 -43
- package/src/pi/grep-search.ts +27 -39
- package/src/pi/grep-tool.ts +63 -106
- package/src/pi/grep.test.ts +177 -156
- package/src/pi/model-budgets.test.ts +27 -24
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +52 -21
- package/src/pi/mutation-runner.ts +134 -74
- package/src/pi/output-budgets.test.ts +86 -42
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +18 -23
- package/src/pi/read.test.ts +22 -41
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +85 -51
- package/src/pi/render.test.ts +88 -59
- package/src/pi/render.ts +64 -65
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -94
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +53 -38
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +10 -3
- package/src/pi/report-schema.ts +251 -148
- package/src/pi/report.test.ts +215 -67
- package/src/pi/report.ts +50 -98
- package/src/pi/result-safety.test.ts +241 -247
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +53 -75
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +35 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +36 -71
- package/src/pi/write.test.ts +31 -34
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/testing/run-tests.ts +1 -1
- package/src/pi/replace-worker-contract.ts +0 -10
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
1
|
+
import { test, type TestContext } from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
3
|
import childProcess from "node:child_process";
|
|
4
4
|
import { syncBuiltinESMExports } from "node:module";
|
|
5
5
|
import { Readable } from "node:stream";
|
|
6
|
-
import {
|
|
6
|
+
import { searchDiagnostic, runRgLines } from "./rg-process.ts";
|
|
7
7
|
import { fileURLToPath } from "node:url";
|
|
8
8
|
import { HashlineError } from "../core/errors.ts";
|
|
9
9
|
|
|
10
10
|
test("grep search exit status → accepts no matches and preserves failure diagnostics", () => {
|
|
11
|
-
for (const code
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
for (const [code, stopped, stderr, expected] of [
|
|
12
|
+
[0, false, "", undefined],
|
|
13
|
+
[1, false, "", undefined],
|
|
14
|
+
[2, false, " \n", "ripgrep exited with code 2."],
|
|
15
|
+
[null, false, "", "ripgrep exited with code null."],
|
|
16
|
+
[null, true, "", undefined],
|
|
17
|
+
[2, false, " regex parse error\n", "regex parse error"],
|
|
18
|
+
[null, true, " regex parse error\n", "regex parse error"],
|
|
19
|
+
] as const) {
|
|
20
|
+
assert.deepEqual(
|
|
21
|
+
searchDiagnostic({ code, stderr, stopped }),
|
|
22
|
+
expected === undefined ? undefined : { reason: expected },
|
|
23
|
+
JSON.stringify({ code, stderr, stopped }),
|
|
24
|
+
);
|
|
15
25
|
}
|
|
16
|
-
const warnings: string[] = [];
|
|
17
|
-
recordSearchDiagnostics({ code: 2, stderr: " regex parse error\n", stopped: false }, warnings);
|
|
18
|
-
recordSearchDiagnostics({ code: null, stderr: "", stopped: false }, warnings);
|
|
19
|
-
assert.equal(warnings.length, 2);
|
|
20
|
-
assert.equal(warnings[0], "regex parse error");
|
|
21
|
-
assert.match(warnings[1], /null/);
|
|
22
26
|
});
|
|
23
27
|
|
|
24
28
|
const listingFixture = fileURLToPath(
|
|
@@ -26,63 +30,72 @@ const listingFixture = fileURLToPath(
|
|
|
26
30
|
);
|
|
27
31
|
const parserTimeoutMs = 5_000;
|
|
28
32
|
|
|
29
|
-
async function parseListing(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
...(stop ? ["wait"] : []),
|
|
37
|
-
],
|
|
38
|
-
AbortSignal.timeout(parserTimeoutMs),
|
|
39
|
-
maxLines,
|
|
40
|
-
(path, lines) => {
|
|
41
|
-
files.push({ path, lines });
|
|
42
|
-
return !stop;
|
|
43
|
-
},
|
|
44
|
-
);
|
|
45
|
-
return { files, result };
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
test("line listing split at every byte → newline paths, empty rows, and grouped files survive", async (t) => {
|
|
49
|
-
const listing = Buffer.from(
|
|
50
|
-
"dir\n文件.txt\0" + "1:\n2:x\n10:[Omitted long matching line]\n\n" + "other.txt\0" + "3:y\n\n",
|
|
51
|
-
);
|
|
33
|
+
async function parseListing(
|
|
34
|
+
t: TestContext,
|
|
35
|
+
chunks: readonly string[],
|
|
36
|
+
maxLines = 10,
|
|
37
|
+
stop = false,
|
|
38
|
+
splitChunks?: readonly Buffer[],
|
|
39
|
+
) {
|
|
52
40
|
const spawn = childProcess.spawn;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const child = spawn(...args);
|
|
41
|
+
const mock = t.mock.method(childProcess, "spawn", (...args: Parameters<typeof spawn>) => {
|
|
42
|
+
const child = spawn(process.execPath, args[1] ?? [], args[2]);
|
|
43
|
+
if (splitChunks) {
|
|
57
44
|
assert.ok(child.stdout);
|
|
58
|
-
const stream = Readable.from(
|
|
45
|
+
const stream = Readable.from(splitChunks);
|
|
59
46
|
t.mock.method(child.stdout, Symbol.asyncIterator, () => stream[Symbol.asyncIterator]());
|
|
60
|
-
return child;
|
|
61
|
-
});
|
|
62
|
-
syncBuiltinESMExports();
|
|
63
|
-
for (let split = 1; split < listing.length; split++) {
|
|
64
|
-
chunks = [listing.subarray(0, split), listing.subarray(split)];
|
|
65
|
-
const { files, result } = await parseListing([]);
|
|
66
|
-
assert.deepEqual(
|
|
67
|
-
files,
|
|
68
|
-
[
|
|
69
|
-
{ path: "dir\n文件.txt", lines: [1, 2, 10] },
|
|
70
|
-
{ path: "other.txt", lines: [3] },
|
|
71
|
-
],
|
|
72
|
-
`split=${split}`,
|
|
73
|
-
);
|
|
74
|
-
assert.equal(result.code, 0);
|
|
75
|
-
assert.equal(result.stopped, false);
|
|
76
47
|
}
|
|
48
|
+
return child;
|
|
49
|
+
});
|
|
50
|
+
syncBuiltinESMExports();
|
|
51
|
+
try {
|
|
52
|
+
const files: { path: string; lines: readonly number[] | undefined }[] = [];
|
|
53
|
+
const result = await runRgLines(
|
|
54
|
+
[
|
|
55
|
+
listingFixture,
|
|
56
|
+
JSON.stringify(chunks.map((chunk) => Buffer.from(chunk).toString("base64"))),
|
|
57
|
+
...(stop ? ["wait"] : []),
|
|
58
|
+
],
|
|
59
|
+
AbortSignal.timeout(parserTimeoutMs),
|
|
60
|
+
maxLines,
|
|
61
|
+
(path, lines) => {
|
|
62
|
+
files.push({ path, lines });
|
|
63
|
+
return !stop;
|
|
64
|
+
},
|
|
65
|
+
);
|
|
66
|
+
return { files, result };
|
|
77
67
|
} finally {
|
|
78
|
-
|
|
68
|
+
mock.mock.restore();
|
|
79
69
|
syncBuiltinESMExports();
|
|
80
70
|
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
test("line listing split at every byte → newline paths, empty rows, and grouped files survive", async (t) => {
|
|
74
|
+
const listing = Buffer.from(
|
|
75
|
+
"dir\n文件.txt\0" + "1:\n2:x\n10:[Omitted long matching line]\n\n" + "other.txt\0" + "3:y\n\n",
|
|
76
|
+
);
|
|
77
|
+
for (let split = 1; split < listing.length; split++) {
|
|
78
|
+
const { files, result } = await parseListing(t, [], 10, false, [
|
|
79
|
+
listing.subarray(0, split),
|
|
80
|
+
listing.subarray(split),
|
|
81
|
+
]);
|
|
82
|
+
assert.deepEqual(
|
|
83
|
+
files,
|
|
84
|
+
[
|
|
85
|
+
{ path: "dir\n文件.txt", lines: [1, 2, 10] },
|
|
86
|
+
{ path: "other.txt", lines: [3] },
|
|
87
|
+
],
|
|
88
|
+
`split=${split}`,
|
|
89
|
+
);
|
|
90
|
+
assert.equal(result.code, 0);
|
|
91
|
+
assert.equal(result.stopped, false);
|
|
92
|
+
}
|
|
81
93
|
});
|
|
82
94
|
|
|
83
|
-
test("line listing exceeds maxLines → only the overflowing file requests all-line review", async () => {
|
|
95
|
+
test("line listing exceeds maxLines → only the overflowing file requests all-line review", async (t) => {
|
|
84
96
|
for (const maxLines of [2, 3]) {
|
|
85
97
|
const { files } = await parseListing(
|
|
98
|
+
t,
|
|
86
99
|
["many\0" + "1:x\n2:y\n3:z\n\nfew\0" + "1:\n\n"],
|
|
87
100
|
maxLines,
|
|
88
101
|
);
|
|
@@ -93,8 +106,9 @@ test("line listing exceeds maxLines → only the overflowing file requests all-l
|
|
|
93
106
|
}
|
|
94
107
|
});
|
|
95
108
|
|
|
96
|
-
test("line consumer returns false → process stops before the next file", async () => {
|
|
109
|
+
test("line consumer returns false → process stops before the next file", async (t) => {
|
|
97
110
|
const { files, result } = await parseListing(
|
|
111
|
+
t,
|
|
98
112
|
["first\0" + "1:x\n\nsecond\0" + "2:y\n\n"],
|
|
99
113
|
10,
|
|
100
114
|
true,
|
|
@@ -103,17 +117,17 @@ test("line consumer returns false → process stops before the next file", async
|
|
|
103
117
|
assert.equal(result.stopped, true);
|
|
104
118
|
});
|
|
105
119
|
|
|
106
|
-
test("line listing ends with incomplete path or row → RIPGREP_FAILED", async () => {
|
|
120
|
+
test("line listing ends with incomplete path or row → RIPGREP_FAILED", async (t) => {
|
|
107
121
|
for (const record of ["unfinished-path", "file\0" + "1:x", "file\0" + "1"]) {
|
|
108
122
|
await assert.rejects(
|
|
109
|
-
parseListing([record]),
|
|
123
|
+
parseListing(t, [record]),
|
|
110
124
|
(error: unknown) => error instanceof HashlineError && error.errorCode === "RIPGREP_FAILED",
|
|
111
125
|
);
|
|
112
126
|
}
|
|
113
127
|
});
|
|
114
128
|
|
|
115
|
-
test("line listing ends after a complete row → flush the final file without a separator", async () => {
|
|
116
|
-
const { files, result } = await parseListing(["final\0" + "1:x\n"]);
|
|
129
|
+
test("line listing ends after a complete row → flush the final file without a separator", async (t) => {
|
|
130
|
+
const { files, result } = await parseListing(t, ["final\0" + "1:x\n"]);
|
|
117
131
|
assert.deepEqual(files, [{ path: "final", lines: [1] }]);
|
|
118
132
|
assert.equal(result.code, 0);
|
|
119
133
|
});
|
package/src/pi/rg-process.ts
CHANGED
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { StringDecoder } from "node:string_decoder";
|
|
10
|
+
import { rgPath } from "@vscode/ripgrep";
|
|
10
11
|
import { DiagnosticBuffer } from "./diagnostic-buffer.ts";
|
|
11
|
-
import { spawn, type
|
|
12
|
+
import { spawn, type ChildProcessByStdio } from "node:child_process";
|
|
12
13
|
import type { Readable } from "node:stream";
|
|
13
14
|
import { HashlineError } from "../core/errors.ts";
|
|
14
|
-
import { throwIfCancelled } from "./error-text.ts";
|
|
15
|
+
import { throwIfCancelled, type SearchWarning } from "./error-text.ts";
|
|
15
16
|
import { formatByteSize } from "./budgets.ts";
|
|
16
17
|
|
|
17
18
|
/**
|
|
@@ -20,32 +21,13 @@ import { formatByteSize } from "./budgets.ts";
|
|
|
20
21
|
* matching stays parallel in grep-search's workers.
|
|
21
22
|
*/
|
|
22
23
|
export const COMMON_RG_ARGS = ["--no-config", "--color=never", "--sort=path"];
|
|
23
|
-
|
|
24
|
+
/** Leave room for rg's arguments and paths on hosts with small command-line limits. */
|
|
25
|
+
export const MAX_RG_PATTERN_UNITS = 24 * 1024;
|
|
26
|
+
const MAX_RG_RECORD_BYTES = 16 * 1024 * 1024;
|
|
24
27
|
const RG_RECORD_LIMIT_MESSAGE = `ripgrep output record exceeds ${formatByteSize(MAX_RG_RECORD_BYTES)}.`;
|
|
25
|
-
/**
|
|
26
|
-
* A ripgrep process or output-protocol failure. A finished run contributes its
|
|
27
|
-
* exit code and, as the cause, its stderr.
|
|
28
|
-
*/
|
|
29
|
-
function ripgrepFailure(
|
|
30
|
-
message: string,
|
|
31
|
-
run?: { readonly code: number | null; readonly stderr: string } | { readonly error: unknown },
|
|
32
|
-
): HashlineError<"RIPGREP_FAILED"> {
|
|
33
|
-
if (run === undefined) return new HashlineError("RIPGREP_FAILED", message);
|
|
34
|
-
if ("error" in run) return new HashlineError("RIPGREP_FAILED", message, { cause: run.error });
|
|
35
|
-
const stderr = run.stderr.trim();
|
|
36
|
-
return new HashlineError("RIPGREP_FAILED", message, {
|
|
37
|
-
facts: { exitCode: run.code },
|
|
38
|
-
...(stderr ? { cause: stderr } : {}),
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** rg reported a path that is not valid UTF-8; tools cannot address it. */
|
|
43
|
-
function unsupportedPath(): HashlineError {
|
|
44
|
-
return new HashlineError("UNSUPPORTED_PATH", "Non-UTF-8 search paths are not supported.");
|
|
45
|
-
}
|
|
46
28
|
|
|
47
29
|
/** Bounded ripgrep stderr retains the beginning and end with an omission notice. */
|
|
48
|
-
|
|
30
|
+
const MAX_RG_STDERR_BYTES = 64 * 1024;
|
|
49
31
|
|
|
50
32
|
interface ExitResult {
|
|
51
33
|
code: number | null;
|
|
@@ -54,7 +36,7 @@ interface ExitResult {
|
|
|
54
36
|
}
|
|
55
37
|
|
|
56
38
|
interface RunningProcess {
|
|
57
|
-
child:
|
|
39
|
+
child: ChildProcessByStdio<null, Readable, Readable>;
|
|
58
40
|
done: Promise<ExitResult>;
|
|
59
41
|
kill(): void;
|
|
60
42
|
}
|
|
@@ -65,31 +47,32 @@ export interface RgRunResult {
|
|
|
65
47
|
stopped: boolean;
|
|
66
48
|
}
|
|
67
49
|
|
|
68
|
-
/**
|
|
69
|
-
export
|
|
70
|
-
|
|
71
|
-
runRgLines: typeof runRgLines;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** Surface incomplete search diagnostics without discarding confirmed matches. */
|
|
75
|
-
export function recordSearchDiagnostics(result: RgRunResult, warnings: string[]): void {
|
|
76
|
-
const message =
|
|
50
|
+
/** An incomplete run's diagnostic, surfaced without discarding confirmed matches. */
|
|
51
|
+
export function searchDiagnostic(result: RgRunResult): SearchWarning | undefined {
|
|
52
|
+
const reason =
|
|
77
53
|
result.stderr.trim() ||
|
|
78
54
|
(!result.stopped && result.code !== 0 && result.code !== 1
|
|
79
55
|
? `ripgrep exited with code ${result.code}.`
|
|
80
|
-
:
|
|
81
|
-
|
|
56
|
+
: undefined);
|
|
57
|
+
return reason ? { reason } : undefined;
|
|
82
58
|
}
|
|
83
59
|
|
|
84
|
-
|
|
60
|
+
/** spawn reports a failure to start by throwing or by an error event, depending on the errno. */
|
|
61
|
+
function startFailure(cause: unknown): HashlineError {
|
|
62
|
+
return new HashlineError("RIPGREP_FAILED", "Failed to run ripgrep.", { cause });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function startRg(args: readonly string[], signal?: AbortSignal): RunningProcess {
|
|
85
66
|
throwIfCancelled(signal);
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
})
|
|
67
|
+
let child: ChildProcessByStdio<null, Readable, Readable>;
|
|
68
|
+
try {
|
|
69
|
+
child = spawn(rgPath, [...args], {
|
|
70
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
71
|
+
shell: false,
|
|
72
|
+
});
|
|
73
|
+
} catch (cause) {
|
|
74
|
+
throw startFailure(cause);
|
|
75
|
+
}
|
|
93
76
|
const stderr = new DiagnosticBuffer(MAX_RG_STDERR_BYTES);
|
|
94
77
|
const stderrDecoder = new StringDecoder("utf8");
|
|
95
78
|
let error: Error | undefined;
|
|
@@ -110,12 +93,6 @@ function startRg(rgPath: string, args: readonly string[], signal?: AbortSignal):
|
|
|
110
93
|
child.on("error", (cause) => {
|
|
111
94
|
error = cause;
|
|
112
95
|
});
|
|
113
|
-
child.stdin.on("error", (cause: NodeJS.ErrnoException) => {
|
|
114
|
-
if (cause.code !== "EPIPE") {
|
|
115
|
-
error = cause;
|
|
116
|
-
kill();
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
96
|
const done = new Promise<ExitResult>((resolve) => {
|
|
120
97
|
child.on("close", (code) => {
|
|
121
98
|
closed = true;
|
|
@@ -126,24 +103,25 @@ function startRg(rgPath: string, args: readonly string[], signal?: AbortSignal):
|
|
|
126
103
|
});
|
|
127
104
|
});
|
|
128
105
|
signal?.addEventListener("abort", kill, { once: true });
|
|
129
|
-
if (signal?.aborted) kill();
|
|
130
106
|
return { child, done, kill };
|
|
131
107
|
}
|
|
132
108
|
|
|
133
109
|
async function* nulDelimitedRecords(stream: Readable): AsyncGenerator<Buffer> {
|
|
134
|
-
let pending = Buffer.alloc(0);
|
|
110
|
+
let pending: Buffer = Buffer.alloc(0);
|
|
135
111
|
for await (const value of stream) {
|
|
136
|
-
const chunk =
|
|
112
|
+
const chunk = value as Buffer;
|
|
137
113
|
// Fast path: no pending data — search the chunk directly.
|
|
138
114
|
let buf = pending.length ? Buffer.concat([pending, chunk]) : chunk;
|
|
139
115
|
pending = Buffer.alloc(0);
|
|
140
116
|
let at: number;
|
|
141
117
|
while ((at = buf.indexOf("\0")) !== -1) {
|
|
142
|
-
if (at > MAX_RG_RECORD_BYTES)
|
|
118
|
+
if (at > MAX_RG_RECORD_BYTES)
|
|
119
|
+
throw new HashlineError("RIPGREP_FAILED", RG_RECORD_LIMIT_MESSAGE);
|
|
143
120
|
yield buf.subarray(0, at);
|
|
144
121
|
buf = buf.subarray(at + 1);
|
|
145
122
|
}
|
|
146
|
-
if (buf.length > MAX_RG_RECORD_BYTES)
|
|
123
|
+
if (buf.length > MAX_RG_RECORD_BYTES)
|
|
124
|
+
throw new HashlineError("RIPGREP_FAILED", RG_RECORD_LIMIT_MESSAGE);
|
|
147
125
|
pending = buf;
|
|
148
126
|
}
|
|
149
127
|
if (pending.length) yield pending;
|
|
@@ -151,34 +129,31 @@ async function* nulDelimitedRecords(stream: Readable): AsyncGenerator<Buffer> {
|
|
|
151
129
|
|
|
152
130
|
/** Run rg while read consumes its stdout; read returns false to stop rg early. */
|
|
153
131
|
async function runRg(
|
|
154
|
-
rgPath: string,
|
|
155
132
|
args: readonly string[],
|
|
156
133
|
signal: AbortSignal | undefined,
|
|
157
134
|
read: (stdout: Readable) => Promise<boolean>,
|
|
158
135
|
): Promise<RgRunResult> {
|
|
159
|
-
const
|
|
160
|
-
process.child.stdin.end();
|
|
136
|
+
const rg = startRg(args, signal);
|
|
161
137
|
try {
|
|
162
|
-
const stopped = !(await read(
|
|
163
|
-
if (stopped)
|
|
164
|
-
const result = await
|
|
138
|
+
const stopped = !(await read(rg.child.stdout));
|
|
139
|
+
if (stopped) rg.kill();
|
|
140
|
+
const result = await rg.done;
|
|
165
141
|
throwIfCancelled(signal);
|
|
166
|
-
if (result.error) throw
|
|
142
|
+
if (result.error) throw startFailure(result.error);
|
|
167
143
|
return { code: result.code, stderr: result.stderr, stopped };
|
|
168
144
|
} finally {
|
|
169
|
-
|
|
170
|
-
await
|
|
145
|
+
rg.kill();
|
|
146
|
+
await rg.done;
|
|
171
147
|
}
|
|
172
148
|
}
|
|
173
149
|
|
|
174
|
-
/**
|
|
150
|
+
/** Read NUL-delimited UTF-8 paths from an owned rg process. */
|
|
175
151
|
export function runRgPaths(
|
|
176
|
-
rgPath: string,
|
|
177
152
|
args: string[],
|
|
178
153
|
signal: AbortSignal | undefined,
|
|
179
154
|
onPath: (path: string) => boolean | Promise<boolean>,
|
|
180
155
|
): Promise<RgRunResult> {
|
|
181
|
-
return runRg(
|
|
156
|
+
return runRg(args, signal, async (stdout) => {
|
|
182
157
|
for await (const record of nulDelimitedRecords(stdout)) {
|
|
183
158
|
throwIfCancelled(signal);
|
|
184
159
|
if (!(await onPath(decodePath(record)))) return false;
|
|
@@ -187,9 +162,11 @@ export function runRgPaths(
|
|
|
187
162
|
});
|
|
188
163
|
}
|
|
189
164
|
|
|
165
|
+
/** A path rg reported; one that is not valid UTF-8 cannot be addressed by tools. */
|
|
190
166
|
function decodePath(record: Buffer): string {
|
|
191
167
|
const path = record.toString("utf8");
|
|
192
|
-
if (!Buffer.from(path, "utf8").equals(record))
|
|
168
|
+
if (!Buffer.from(path, "utf8").equals(record))
|
|
169
|
+
throw new HashlineError("UNSUPPORTED_PATH", "Non-UTF-8 search paths are not supported.");
|
|
193
170
|
return path;
|
|
194
171
|
}
|
|
195
172
|
|
|
@@ -204,19 +181,18 @@ export const LINE_LISTING_ARGS = [
|
|
|
204
181
|
];
|
|
205
182
|
|
|
206
183
|
/**
|
|
207
|
-
*
|
|
184
|
+
* Read a {@link LINE_LISTING_ARGS} listing. Each file's group is a NUL-terminated
|
|
208
185
|
* path, then one `N:` record per matching line ending in a line feed (its content is at most
|
|
209
186
|
* one byte or an omission notice), then an empty line. onFile receives a file's ascending line
|
|
210
187
|
* numbers, or undefined once there are more than maxLines.
|
|
211
188
|
*/
|
|
212
189
|
export function runRgLines(
|
|
213
|
-
rgPath: string,
|
|
214
190
|
args: string[],
|
|
215
191
|
signal: AbortSignal | undefined,
|
|
216
192
|
maxLines: number,
|
|
217
193
|
onFile: (path: string, lines: readonly number[] | undefined) => boolean | Promise<boolean>,
|
|
218
194
|
): Promise<RgRunResult> {
|
|
219
|
-
return runRg(
|
|
195
|
+
return runRg(args, signal, async (stdout) => {
|
|
220
196
|
let path: string | undefined;
|
|
221
197
|
let lines: number[] | undefined = [];
|
|
222
198
|
let pending: Buffer = Buffer.alloc(0);
|
|
@@ -245,15 +221,17 @@ export function runRgLines(
|
|
|
245
221
|
const colon = record.indexOf(58);
|
|
246
222
|
const line = colon > 0 ? Number(record.subarray(0, colon).toString("latin1")) : NaN;
|
|
247
223
|
if (!Number.isSafeInteger(line) || line < 1)
|
|
248
|
-
throw
|
|
224
|
+
throw new HashlineError("RIPGREP_FAILED", "ripgrep printed an unexpected line record.");
|
|
249
225
|
if (lines.length < maxLines) lines.push(line);
|
|
250
226
|
else lines = undefined;
|
|
251
227
|
}
|
|
252
228
|
}
|
|
253
229
|
pending = buf.subarray(at);
|
|
254
|
-
if (pending.length > MAX_RG_RECORD_BYTES)
|
|
230
|
+
if (pending.length > MAX_RG_RECORD_BYTES)
|
|
231
|
+
throw new HashlineError("RIPGREP_FAILED", RG_RECORD_LIMIT_MESSAGE);
|
|
255
232
|
}
|
|
256
|
-
if (pending.length)
|
|
233
|
+
if (pending.length)
|
|
234
|
+
throw new HashlineError("RIPGREP_FAILED", "ripgrep printed an incomplete line record.");
|
|
257
235
|
return path === undefined || flush();
|
|
258
236
|
});
|
|
259
237
|
}
|