@unieai/rabi-util-ripgrep 0.0.0-stage → 0.2.2-rc.5
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/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/index.js +103 -0
- package/lib/types/index.d.ts +37 -0
- package/package.json +33 -4
- package/src/index.ts +109 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unieai/rabi-util-ripgrep
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Packaged ripgrep resolution and subprocess streaming helpers
|
|
4
|
+
|
|
5
|
+
Part of Rabi, the agent harness UnieAI Code runs on. Installed as a dependency of `@unieai/rabi`;
|
|
6
|
+
not meant to be used on its own.
|
|
7
|
+
|
|
8
|
+
See LICENSE for the license and copyright of this package.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join, parse } from "node:path";
|
|
3
|
+
//#region lib/types/index.js
|
|
4
|
+
/** Packaged ripgrep resolution and subprocess execution, independent of tool or Remote consumers. */
|
|
5
|
+
let rgPathPromise;
|
|
6
|
+
/**
|
|
7
|
+
* Resolve the packaged executable lazily, once per process. Single-file runtimes use an existing
|
|
8
|
+
* `-rg` sidecar; Electron uses the unpacked ASAR path. Resolution failures reject
|
|
9
|
+
* this call, not package loading. No system executable or remote fallback is used.
|
|
10
|
+
* @returns the packaged executable's absolute path.
|
|
11
|
+
*/
|
|
12
|
+
function resolveRgPath() {
|
|
13
|
+
rgPathPromise ??= Promise.resolve().then(async () => {
|
|
14
|
+
const executable = parse(process.execPath);
|
|
15
|
+
const executableSidecar = process.platform === "win32" ? join(executable.dir, `${executable.name}-rg.exe`) : `${process.execPath}-rg`;
|
|
16
|
+
if ("pkg" in process && existsSync(executableSidecar)) return executableSidecar;
|
|
17
|
+
const dependency = (await import("@vscode/ripgrep")).rgPath;
|
|
18
|
+
return process.versions.electron === void 0 ? dependency : dependency.replace(/\.asar(?=[\\/])/u, ".asar.unpacked");
|
|
19
|
+
});
|
|
20
|
+
return rgPathPromise;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Start packaged ripgrep with explicit subprocess settings. `spec.argv` contains
|
|
24
|
+
* only rg arguments, without an executable. This function prepends the resolved
|
|
25
|
+
* rg executable path and `--no-config`, so ambient configuration cannot enable a
|
|
26
|
+
* preprocessor. The caller owns exit classification and process cleanup.
|
|
27
|
+
* @param subprocess - the composed subprocess service.
|
|
28
|
+
* @param spec - cwd, rg arguments (without the executable), stdio, signal and termination grace.
|
|
29
|
+
* @returns the live subprocess handle; resolution and synchronous spawn failures reject.
|
|
30
|
+
*/
|
|
31
|
+
async function spawnRipgrep(subprocess, spec) {
|
|
32
|
+
const executable = await resolveRgPath();
|
|
33
|
+
return subprocess.spawn({
|
|
34
|
+
...spec,
|
|
35
|
+
argv: [
|
|
36
|
+
executable,
|
|
37
|
+
"--no-config",
|
|
38
|
+
...spec.argv
|
|
39
|
+
]
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Read UTF-8 stdout records without retaining the complete output. A false
|
|
44
|
+
* callback return terminates the process and suppresses remaining records.
|
|
45
|
+
* Completion, callback failure and stream failure all await process quiescence;
|
|
46
|
+
* provider failures reject. Exit codes and cancellation remain consumer-owned.
|
|
47
|
+
* @param handle - an owned ripgrep process spawned with piped stdout.
|
|
48
|
+
* Records are decoded as UTF-8, so bytes of a name that is not valid UTF-8
|
|
49
|
+
* arrive replaced with U+FFFD.
|
|
50
|
+
* @param separator - newline for text output, or NUL for file paths that may contain newlines.
|
|
51
|
+
* @param onLine - synchronous record consumer; false requests early termination.
|
|
52
|
+
* @returns exit facts after stdout and the managed process range settle.
|
|
53
|
+
*/
|
|
54
|
+
async function readRipgrepLines(handle, separator, onLine) {
|
|
55
|
+
const stdout = handle.stdout;
|
|
56
|
+
if (stdout === void 0) {
|
|
57
|
+
handle.terminate();
|
|
58
|
+
await Promise.allSettled([handle.done, handle.waitForExit()]);
|
|
59
|
+
throw new Error("ripgrep requires piped stdout");
|
|
60
|
+
}
|
|
61
|
+
stdout.setEncoding("utf8");
|
|
62
|
+
let stopped = false;
|
|
63
|
+
let completed = false;
|
|
64
|
+
const reading = (async () => {
|
|
65
|
+
let pending = "";
|
|
66
|
+
for await (const chunk of stdout) {
|
|
67
|
+
if (stopped) continue;
|
|
68
|
+
pending += chunk;
|
|
69
|
+
let end;
|
|
70
|
+
while ((end = pending.indexOf(separator)) !== -1) {
|
|
71
|
+
const line = pending.slice(0, end);
|
|
72
|
+
pending = pending.slice(end + 1);
|
|
73
|
+
if (!onLine(line)) {
|
|
74
|
+
stopped = true;
|
|
75
|
+
pending = "";
|
|
76
|
+
handle.terminate();
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (pending.length > 0 && !onLine(pending)) {
|
|
82
|
+
stopped = true;
|
|
83
|
+
handle.terminate();
|
|
84
|
+
}
|
|
85
|
+
})();
|
|
86
|
+
try {
|
|
87
|
+
const [, outcome] = await Promise.all([reading, handle.done]);
|
|
88
|
+
completed = true;
|
|
89
|
+
return {
|
|
90
|
+
outcome,
|
|
91
|
+
stopped
|
|
92
|
+
};
|
|
93
|
+
} finally {
|
|
94
|
+
if (!completed) {
|
|
95
|
+
handle.terminate();
|
|
96
|
+
stdout.destroy();
|
|
97
|
+
}
|
|
98
|
+
await Promise.allSettled([reading, handle.done]);
|
|
99
|
+
await handle.waitForExit();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
export { readRipgrepLines, resolveRgPath, spawnRipgrep };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { SubprocessHandle, SubprocessOutcome, SubprocessRuntime, SubprocessSpawnSpec } from '@unieai/rabi-subprocess';
|
|
2
|
+
/**
|
|
3
|
+
* Resolve the packaged executable lazily, once per process. Single-file runtimes use an existing
|
|
4
|
+
* `-rg` sidecar; Electron uses the unpacked ASAR path. Resolution failures reject
|
|
5
|
+
* this call, not package loading. No system executable or remote fallback is used.
|
|
6
|
+
* @returns the packaged executable's absolute path.
|
|
7
|
+
*/
|
|
8
|
+
export declare function resolveRgPath(): Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Start packaged ripgrep with explicit subprocess settings. `spec.argv` contains
|
|
11
|
+
* only rg arguments, without an executable. This function prepends the resolved
|
|
12
|
+
* rg executable path and `--no-config`, so ambient configuration cannot enable a
|
|
13
|
+
* preprocessor. The caller owns exit classification and process cleanup.
|
|
14
|
+
* @param subprocess - the composed subprocess service.
|
|
15
|
+
* @param spec - cwd, rg arguments (without the executable), stdio, signal and termination grace.
|
|
16
|
+
* @returns the live subprocess handle; resolution and synchronous spawn failures reject.
|
|
17
|
+
*/
|
|
18
|
+
export declare function spawnRipgrep(subprocess: Pick<SubprocessRuntime, 'spawn'>, spec: SubprocessSpawnSpec): Promise<SubprocessHandle>;
|
|
19
|
+
/** Exit facts and whether the line consumer requested early termination. */
|
|
20
|
+
export interface RipgrepLinesResult {
|
|
21
|
+
readonly outcome: SubprocessOutcome;
|
|
22
|
+
readonly stopped: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Read UTF-8 stdout records without retaining the complete output. A false
|
|
26
|
+
* callback return terminates the process and suppresses remaining records.
|
|
27
|
+
* Completion, callback failure and stream failure all await process quiescence;
|
|
28
|
+
* provider failures reject. Exit codes and cancellation remain consumer-owned.
|
|
29
|
+
* @param handle - an owned ripgrep process spawned with piped stdout.
|
|
30
|
+
* Records are decoded as UTF-8, so bytes of a name that is not valid UTF-8
|
|
31
|
+
* arrive replaced with U+FFFD.
|
|
32
|
+
* @param separator - newline for text output, or NUL for file paths that may contain newlines.
|
|
33
|
+
* @param onLine - synchronous record consumer; false requests early termination.
|
|
34
|
+
* @returns exit facts after stdout and the managed process range settle.
|
|
35
|
+
*/
|
|
36
|
+
export declare function readRipgrepLines(handle: SubprocessHandle, separator: '\n' | '\0', onLine: (line: string) => boolean): Promise<RipgrepLinesResult>;
|
|
37
|
+
//# sourceMappingURL=index.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unieai/rabi-util-ripgrep",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Packaged ripgrep resolution and subprocess streaming helpers",
|
|
4
|
+
"version": "0.2.2-rc.5",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"types": "lib/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./lib/types/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./src/*": "./src/*",
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"lib/index.js",
|
|
21
|
+
"lib/types/**/*.d.ts",
|
|
22
|
+
"lib/types/**/*.d.ts.map",
|
|
23
|
+
"lib/**/*.js.map",
|
|
24
|
+
"src",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@vscode/ripgrep": "^1.18.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@unieai/cordis": "4.1.0",
|
|
33
|
+
"@unieai/rabi-subprocess": "0.2.2-rc.5"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/** Packaged ripgrep resolution and subprocess execution, independent of tool or Remote consumers. */
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
3
|
+
import { join, parse } from 'node:path'
|
|
4
|
+
import type { SubprocessHandle, SubprocessOutcome, SubprocessRuntime, SubprocessSpawnSpec } from '@unieai/rabi-subprocess'
|
|
5
|
+
|
|
6
|
+
let rgPathPromise: Promise<string> | undefined
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the packaged executable lazily, once per process. Single-file runtimes use an existing
|
|
10
|
+
* `-rg` sidecar; Electron uses the unpacked ASAR path. Resolution failures reject
|
|
11
|
+
* this call, not package loading. No system executable or remote fallback is used.
|
|
12
|
+
* @returns the packaged executable's absolute path.
|
|
13
|
+
*/
|
|
14
|
+
export function resolveRgPath(): Promise<string> {
|
|
15
|
+
rgPathPromise ??= Promise.resolve().then(async () => {
|
|
16
|
+
const executable = parse(process.execPath)
|
|
17
|
+
const executableSidecar = process.platform === 'win32'
|
|
18
|
+
? join(executable.dir, `${executable.name}-rg.exe`)
|
|
19
|
+
: `${process.execPath}-rg`
|
|
20
|
+
if ('pkg' in process && existsSync(executableSidecar)) return executableSidecar
|
|
21
|
+
const dependency = (await import('@vscode/ripgrep')).rgPath
|
|
22
|
+
return process.versions.electron === undefined
|
|
23
|
+
? dependency
|
|
24
|
+
: dependency.replace(/\.asar(?=[\\/])/u, '.asar.unpacked')
|
|
25
|
+
})
|
|
26
|
+
return rgPathPromise
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Start packaged ripgrep with explicit subprocess settings. `spec.argv` contains
|
|
31
|
+
* only rg arguments, without an executable. This function prepends the resolved
|
|
32
|
+
* rg executable path and `--no-config`, so ambient configuration cannot enable a
|
|
33
|
+
* preprocessor. The caller owns exit classification and process cleanup.
|
|
34
|
+
* @param subprocess - the composed subprocess service.
|
|
35
|
+
* @param spec - cwd, rg arguments (without the executable), stdio, signal and termination grace.
|
|
36
|
+
* @returns the live subprocess handle; resolution and synchronous spawn failures reject.
|
|
37
|
+
*/
|
|
38
|
+
export async function spawnRipgrep(subprocess: Pick<SubprocessRuntime, 'spawn'>, spec: SubprocessSpawnSpec): Promise<SubprocessHandle> {
|
|
39
|
+
const executable = await resolveRgPath()
|
|
40
|
+
return subprocess.spawn({ ...spec, argv: [executable, '--no-config', ...spec.argv] })
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Exit facts and whether the line consumer requested early termination. */
|
|
44
|
+
export interface RipgrepLinesResult {
|
|
45
|
+
readonly outcome: SubprocessOutcome
|
|
46
|
+
readonly stopped: boolean
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Read UTF-8 stdout records without retaining the complete output. A false
|
|
51
|
+
* callback return terminates the process and suppresses remaining records.
|
|
52
|
+
* Completion, callback failure and stream failure all await process quiescence;
|
|
53
|
+
* provider failures reject. Exit codes and cancellation remain consumer-owned.
|
|
54
|
+
* @param handle - an owned ripgrep process spawned with piped stdout.
|
|
55
|
+
* Records are decoded as UTF-8, so bytes of a name that is not valid UTF-8
|
|
56
|
+
* arrive replaced with U+FFFD.
|
|
57
|
+
* @param separator - newline for text output, or NUL for file paths that may contain newlines.
|
|
58
|
+
* @param onLine - synchronous record consumer; false requests early termination.
|
|
59
|
+
* @returns exit facts after stdout and the managed process range settle.
|
|
60
|
+
*/
|
|
61
|
+
export async function readRipgrepLines(
|
|
62
|
+
handle: SubprocessHandle,
|
|
63
|
+
separator: '\n' | '\0',
|
|
64
|
+
onLine: (line: string) => boolean,
|
|
65
|
+
): Promise<RipgrepLinesResult> {
|
|
66
|
+
const stdout = handle.stdout
|
|
67
|
+
if (stdout === undefined) {
|
|
68
|
+
handle.terminate()
|
|
69
|
+
await Promise.allSettled([handle.done, handle.waitForExit()])
|
|
70
|
+
throw new Error('ripgrep requires piped stdout')
|
|
71
|
+
}
|
|
72
|
+
stdout.setEncoding('utf8')
|
|
73
|
+
let stopped = false
|
|
74
|
+
let completed = false
|
|
75
|
+
const reading = (async () => {
|
|
76
|
+
let pending = ''
|
|
77
|
+
for await (const chunk of stdout as AsyncIterable<string>) {
|
|
78
|
+
if (stopped) continue
|
|
79
|
+
pending += chunk
|
|
80
|
+
let end: number
|
|
81
|
+
while ((end = pending.indexOf(separator)) !== -1) {
|
|
82
|
+
const line = pending.slice(0, end)
|
|
83
|
+
pending = pending.slice(end + 1)
|
|
84
|
+
if (!onLine(line)) {
|
|
85
|
+
stopped = true
|
|
86
|
+
pending = ''
|
|
87
|
+
handle.terminate()
|
|
88
|
+
break
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (pending.length > 0 && !onLine(pending)) {
|
|
93
|
+
stopped = true
|
|
94
|
+
handle.terminate()
|
|
95
|
+
}
|
|
96
|
+
})()
|
|
97
|
+
try {
|
|
98
|
+
const [, outcome] = await Promise.all([reading, handle.done])
|
|
99
|
+
completed = true
|
|
100
|
+
return { outcome, stopped }
|
|
101
|
+
} finally {
|
|
102
|
+
if (!completed) {
|
|
103
|
+
handle.terminate()
|
|
104
|
+
stdout.destroy()
|
|
105
|
+
}
|
|
106
|
+
await Promise.allSettled([reading, handle.done])
|
|
107
|
+
await handle.waitForExit()
|
|
108
|
+
}
|
|
109
|
+
}
|