opencode-fs-plugin 1.3.1 → 1.4.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/INSTALL.md +9 -9
- package/README.md +32 -8
- package/SPEC.md +10 -1
- package/package.json +1 -1
- package/skills/filesystem/SKILL.md +16 -0
- package/src/core/fsutil.ts +106 -26
- package/src/core/parallel.ts +29 -0
- package/src/tools/chmod.ts +6 -2
- package/src/tools/diff.ts +49 -13
- package/src/tools/du.ts +3 -2
- package/src/tools/find.ts +3 -2
- package/src/tools/grep.ts +34 -24
- package/src/tools/ls.ts +4 -3
package/INSTALL.md
CHANGED
|
@@ -17,14 +17,14 @@ Add the registry package to the plugin list:
|
|
|
17
17
|
|
|
18
18
|
```jsonc
|
|
19
19
|
{
|
|
20
|
-
"plugins": ["opencode-fs-plugin@1.
|
|
20
|
+
"plugins": ["opencode-fs-plugin@1.4.0"]
|
|
21
21
|
}
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
Or let the CLI install and configure it:
|
|
25
25
|
|
|
26
26
|
```bash
|
|
27
|
-
opencode plugin add opencode-fs-plugin@1.
|
|
27
|
+
opencode plugin add opencode-fs-plugin@1.4.0
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
A bare `opencode-fs-plugin` follows `latest`. OpenCode installs it into its
|
|
@@ -36,7 +36,7 @@ Option A if you also want the skill.
|
|
|
36
36
|
Download the release artifact and install it as a normal npm package:
|
|
37
37
|
|
|
38
38
|
```bash
|
|
39
|
-
VERSION=1.
|
|
39
|
+
VERSION=1.4.0
|
|
40
40
|
curl -L -o "opencode-fs-plugin-${VERSION}.tgz" \
|
|
41
41
|
"https://github.com/masterjg/opencode-fs-plugin/releases/download/v${VERSION}/opencode-fs-plugin-${VERSION}.tgz"
|
|
42
42
|
npm install "./opencode-fs-plugin-${VERSION}.tgz"
|
|
@@ -64,20 +64,20 @@ OpenCode installs it from Git:
|
|
|
64
64
|
|
|
65
65
|
```jsonc
|
|
66
66
|
{
|
|
67
|
-
"plugins": ["github:masterjg/opencode-fs-plugin#v1.
|
|
67
|
+
"plugins": ["github:masterjg/opencode-fs-plugin#v1.4.0"]
|
|
68
68
|
}
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
`opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.
|
|
71
|
+
`opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.4.0'` installs the
|
|
72
72
|
same spec and writes the entry for you. The equivalent full URL is
|
|
73
|
-
`git+https://github.com/masterjg/opencode-fs-plugin.git#v1.
|
|
73
|
+
`git+https://github.com/masterjg/opencode-fs-plugin.git#v1.4.0`. A tag is
|
|
74
74
|
checked for updates; use a full commit hash to pin it.
|
|
75
75
|
|
|
76
76
|
Git plugins are installed under OpenCode's cache, so their package path is not
|
|
77
77
|
stable. For a stable skill path, install the tag with npm instead:
|
|
78
78
|
|
|
79
79
|
```bash
|
|
80
|
-
npm install "github:masterjg/opencode-fs-plugin#v1.
|
|
80
|
+
npm install "github:masterjg/opencode-fs-plugin#v1.4.0"
|
|
81
81
|
```
|
|
82
82
|
|
|
83
83
|
Then configure `./node_modules/opencode-fs-plugin` in `plugins` as in the
|
|
@@ -174,9 +174,9 @@ and description until the model loads it.
|
|
|
174
174
|
To let a coding agent install the plugin, use:
|
|
175
175
|
|
|
176
176
|
```text
|
|
177
|
-
Install opencode-fs-plugin 1.
|
|
177
|
+
Install opencode-fs-plugin 1.4.0 with "npm install opencode-fs-plugin@1.4.0",
|
|
178
178
|
or from its release tarball or the Git tag
|
|
179
|
-
"github:masterjg/opencode-fs-plugin#v1.
|
|
179
|
+
"github:masterjg/opencode-fs-plugin#v1.4.0". Add
|
|
180
180
|
"./node_modules/opencode-fs-plugin" to the plugins array in opencode.jsonc, add
|
|
181
181
|
"./node_modules/opencode-fs-plugin/skills" to the skills array, then verify that
|
|
182
182
|
the filesystem skill loads with the skill tool using id "filesystem".
|
package/README.md
CHANGED
|
@@ -81,6 +81,30 @@ large downloads prefer `fs.http_request({ output })`, which streams to disk.
|
|
|
81
81
|
|
|
82
82
|
There is no shell, redirection operator, nested pipe, or `find -exec`.
|
|
83
83
|
|
|
84
|
+
## Performance and concurrency
|
|
85
|
+
|
|
86
|
+
All I/O is asynchronous; the plugin never calls a synchronous `fs` API.
|
|
87
|
+
|
|
88
|
+
The concurrency limit defaults to `os.availableParallelism()`. Override it with
|
|
89
|
+
the plugin option `concurrency` or the `OPENCODE_FS_CONCURRENCY` environment
|
|
90
|
+
variable (the option wins). It bounds every parallel path, so a large tree cannot
|
|
91
|
+
start unbounded work:
|
|
92
|
+
|
|
93
|
+
- `cp`/`mv` tree copies
|
|
94
|
+
- `hash`, `gzip`, and `gunzip` over multiple files
|
|
95
|
+
- `grep` file reads
|
|
96
|
+
- `find`, `du`, `grep -r`, and `chmod -R` traversal, including directory
|
|
97
|
+
recursion and per-entry stats
|
|
98
|
+
- `diff -r` file comparisons
|
|
99
|
+
- `ls` long-format rendering
|
|
100
|
+
|
|
101
|
+
`gzip`/`gunzip` also use the libuv thread pool for compression; raise
|
|
102
|
+
`UV_THREADPOOL_SIZE` if compression dominates.
|
|
103
|
+
|
|
104
|
+
CPU-bound JavaScript stages (`sort`, `sed`, `diff` text patching) stay
|
|
105
|
+
single-threaded. Splitting them would need worker threads and is a deliberate
|
|
106
|
+
gap; their input/output cost is dominated by the async I/O above.
|
|
107
|
+
|
|
84
108
|
## Background jobs
|
|
85
109
|
|
|
86
110
|
Every `fs.*` tool accepts `background: true`. The call returns immediately with
|
|
@@ -142,18 +166,18 @@ Add the registry package directly to the plugin list:
|
|
|
142
166
|
|
|
143
167
|
```jsonc
|
|
144
168
|
{
|
|
145
|
-
"plugins": ["opencode-fs-plugin@1.
|
|
169
|
+
"plugins": ["opencode-fs-plugin@1.4.0"]
|
|
146
170
|
}
|
|
147
171
|
```
|
|
148
172
|
|
|
149
|
-
`opencode plugin add opencode-fs-plugin@1.
|
|
173
|
+
`opencode plugin add opencode-fs-plugin@1.4.0` installs it and writes the config
|
|
150
174
|
entry. A bare `opencode-fs-plugin` follows the `latest` tag.
|
|
151
175
|
|
|
152
176
|
OpenCode manages the package under its cache, so the path is not stable. If you
|
|
153
177
|
also want the packaged skill, install it into a project instead:
|
|
154
178
|
|
|
155
179
|
```bash
|
|
156
|
-
npm install opencode-fs-plugin@1.
|
|
180
|
+
npm install opencode-fs-plugin@1.4.0
|
|
157
181
|
```
|
|
158
182
|
|
|
159
183
|
```jsonc
|
|
@@ -168,7 +192,7 @@ npm install opencode-fs-plugin@1.3.1
|
|
|
168
192
|
Download the release tarball, then install it as a normal npm package:
|
|
169
193
|
|
|
170
194
|
```bash
|
|
171
|
-
VERSION=1.
|
|
195
|
+
VERSION=1.4.0
|
|
172
196
|
curl -L -o "opencode-fs-plugin-${VERSION}.tgz" \
|
|
173
197
|
"https://github.com/masterjg/opencode-fs-plugin/releases/download/v${VERSION}/opencode-fs-plugin-${VERSION}.tgz"
|
|
174
198
|
npm install "./opencode-fs-plugin-${VERSION}.tgz"
|
|
@@ -197,20 +221,20 @@ OpenCode installs it from Git:
|
|
|
197
221
|
|
|
198
222
|
```jsonc
|
|
199
223
|
{
|
|
200
|
-
"plugins": ["github:masterjg/opencode-fs-plugin#v1.
|
|
224
|
+
"plugins": ["github:masterjg/opencode-fs-plugin#v1.4.0"]
|
|
201
225
|
}
|
|
202
226
|
```
|
|
203
227
|
|
|
204
|
-
`opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.
|
|
228
|
+
`opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.4.0'` installs the
|
|
205
229
|
same spec and writes the config entry. The equivalent full URL is
|
|
206
|
-
`git+https://github.com/masterjg/opencode-fs-plugin.git#v1.
|
|
230
|
+
`git+https://github.com/masterjg/opencode-fs-plugin.git#v1.4.0`. A tag is
|
|
207
231
|
checked for updates; use a full commit hash to pin it.
|
|
208
232
|
|
|
209
233
|
Git plugins are installed under OpenCode's cache, so their package path is not
|
|
210
234
|
stable. If you also want the packaged skill, install with npm instead:
|
|
211
235
|
|
|
212
236
|
```bash
|
|
213
|
-
npm install "github:masterjg/opencode-fs-plugin#v1.
|
|
237
|
+
npm install "github:masterjg/opencode-fs-plugin#v1.4.0"
|
|
214
238
|
```
|
|
215
239
|
|
|
216
240
|
Then use `./node_modules/opencode-fs-plugin` in `plugins` and
|
package/SPEC.md
CHANGED
|
@@ -6,7 +6,14 @@ the `fs` namespace (`tools.fs.cp`, direct `fs_cp`).
|
|
|
6
6
|
|
|
7
7
|
## Principles
|
|
8
8
|
|
|
9
|
-
- Every tool is async and uses `fs/promises
|
|
9
|
+
- Every tool is async and uses `fs/promises`; no synchronous filesystem calls.
|
|
10
|
+
- Independent I/O runs with bounded parallelism. The limit defaults to
|
|
11
|
+
`os.availableParallelism()` and can be set with the plugin `concurrency`
|
|
12
|
+
option or `OPENCODE_FS_CONCURRENCY`.
|
|
13
|
+
- Parallel paths: `cp`/`mv` trees, `hash`, `gzip`/`gunzip`, `grep` reads,
|
|
14
|
+
`find`/`du`/`grep -r`/`chmod -R` traversal, `diff -r`, and `ls` formatting.
|
|
15
|
+
- CPU-bound JS (`sort`, `sed`, text diffing) stays single-threaded; worker
|
|
16
|
+
threads are a deliberate gap.
|
|
10
17
|
- Every mutating tool asserts `fs_<tool>` permission before touching the disk.
|
|
11
18
|
- External targets require `external_directory` first.
|
|
12
19
|
- Output is native-style text plus structured metadata; errors are `ERROR:` or
|
|
@@ -56,6 +63,8 @@ These are not implemented:
|
|
|
56
63
|
- curl protocols beyond HTTP(S), cookie jars, client certificates, proxies,
|
|
57
64
|
multipart uploads, and resumable/range downloads
|
|
58
65
|
- true streaming through pipe stages; `http_request` buffers unless `output` is set
|
|
66
|
+
- worker threads for CPU-bound stages (`sort`, `sed`, text diffing)
|
|
67
|
+
- parallel member reads inside a single `tar`/`zip` archive; member order is fixed
|
|
59
68
|
|
|
60
69
|
## Format details
|
|
61
70
|
|
package/package.json
CHANGED
|
@@ -79,6 +79,7 @@ Use the exact field names; scalar `path` is only valid for `fs.mkdir`.
|
|
|
79
79
|
| `archive` + `paths` | `fs.tar` create, `fs.zip` create |
|
|
80
80
|
| `archive` + `mode` | `fs.tar` (`create`/`extract`/`list`), `fs.unzip` (`extract`/`list`) |
|
|
81
81
|
| `content` | `fs.tee` when there is no stdin |
|
|
82
|
+
| `paths` + optional `keep`/`force`/`stdout`/`level`/`suffix` | `fs.gzip`, `fs.gunzip` |
|
|
82
83
|
| `url` + optional `output` | `fs.http_request` |
|
|
83
84
|
| `stages` + optional `input`/`output` | `fs.pipe` |
|
|
84
85
|
|
|
@@ -209,6 +210,17 @@ Each call still enforces its own `fs_<tool>` and `external_directory`
|
|
|
209
210
|
permissions; loops do not bypass them. Code Mode has no timers, so use
|
|
210
211
|
`wait_for` for time-based waits and `background: true` for long jobs.
|
|
211
212
|
|
|
213
|
+
## Performance
|
|
214
|
+
|
|
215
|
+
All `fs` I/O is asynchronous. Independent work is parallelized automatically
|
|
216
|
+
within each call, up to `os.availableParallelism()`: tree copies, hashing,
|
|
217
|
+
gzip/gunzip, grep reads, `find`/`du`/`grep -r`/`chmod -R` traversal, `diff -r`,
|
|
218
|
+
and `ls` formatting. Do not add your own concurrency for those.
|
|
219
|
+
|
|
220
|
+
The limit comes from the plugin `concurrency` option or
|
|
221
|
+
`OPENCODE_FS_CONCURRENCY`. CPU-bound `sort`, `sed`, and text diffing stay
|
|
222
|
+
single-threaded, so very large inputs use one core.
|
|
223
|
+
|
|
212
224
|
## Recipes
|
|
213
225
|
|
|
214
226
|
```js
|
|
@@ -263,6 +275,10 @@ await tools.fs.http_request({
|
|
|
263
275
|
Files under `/tmp` are usually on another filesystem; use a project path or a
|
|
264
276
|
same-filesystem holding directory instead.
|
|
265
277
|
- `fs.mktemp` uses the OS temp directory unless `tmpdir` is supplied.
|
|
278
|
+
- `fs.mkdir` fails when the directory already exists; check with `fs.stat`
|
|
279
|
+
first or treat `ERROR: Already exists` as a no-op.
|
|
280
|
+
- `fs.gunzip` needs `force` to overwrite the restored file; without `keep` it
|
|
281
|
+
removes the `.gz` after decompressing.
|
|
266
282
|
|
|
267
283
|
## Paths and safety
|
|
268
284
|
|
package/src/core/fsutil.ts
CHANGED
|
@@ -8,6 +8,7 @@ import * as path from "node:path";
|
|
|
8
8
|
import { constants } from "node:fs";
|
|
9
9
|
import { FsError, fromErrno, isMissing } from "./errors.js";
|
|
10
10
|
import { formatMode } from "./mode.js";
|
|
11
|
+
import { createLimiter, mapLimit } from "./parallel.js";
|
|
11
12
|
|
|
12
13
|
export interface WalkEntry {
|
|
13
14
|
readonly path: string;
|
|
@@ -21,6 +22,8 @@ export interface WalkOptions {
|
|
|
21
22
|
readonly maxDepth?: number;
|
|
22
23
|
readonly includeHidden?: boolean;
|
|
23
24
|
readonly signal?: AbortSignal;
|
|
25
|
+
/** Parallel entry stats per directory. Default: 1 (sequential). */
|
|
26
|
+
readonly concurrency?: number;
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
export async function* walk(root: string, options: WalkOptions = {}): AsyncGenerator<WalkEntry> {
|
|
@@ -41,25 +44,95 @@ async function* walkAt(
|
|
|
41
44
|
} catch (error) {
|
|
42
45
|
throw fromErrno(error, "access");
|
|
43
46
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
47
|
+
const visible = options.includeHidden ? entries : entries.filter((dirent) => !dirent.name.startsWith("."));
|
|
48
|
+
const resolved = await mapLimit(
|
|
49
|
+
visible,
|
|
50
|
+
Math.max(1, options.concurrency ?? 1),
|
|
51
|
+
async (dirent): Promise<WalkEntry | undefined> => {
|
|
52
|
+
const full = path.join(current, dirent.name);
|
|
53
|
+
try {
|
|
54
|
+
const stats = options.followSymlinks ? await fs.stat(full) : await fs.lstat(full);
|
|
55
|
+
return { path: full, name: dirent.name, stats };
|
|
56
|
+
} catch (error) {
|
|
57
|
+
/* v8 ignore next 3 */
|
|
58
|
+
if (isMissing(error)) return undefined;
|
|
59
|
+
/* v8 ignore next */
|
|
60
|
+
throw fromErrno(error, "access");
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
options.signal,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
for (const entry of resolved) {
|
|
67
|
+
/* v8 ignore next */
|
|
68
|
+
if (entry === undefined) continue;
|
|
69
|
+
yield entry;
|
|
70
|
+
if (depth + 1 < maxDepth && entry.stats.isDirectory()) {
|
|
71
|
+
yield* walkAt(entry.path, depth + 1, maxDepth, options);
|
|
59
72
|
}
|
|
60
73
|
}
|
|
61
74
|
}
|
|
62
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Collect a tree with directory stats and subtree recursion running
|
|
78
|
+
* concurrently. Syscalls share one limit, and results keep depth-first order.
|
|
79
|
+
*/
|
|
80
|
+
export async function walkAll(root: string, options: WalkOptions = {}): Promise<WalkEntry[]> {
|
|
81
|
+
const maxDepth = options.maxDepth ?? (options.recursive === false ? 0 : Infinity);
|
|
82
|
+
const gate = createLimiter(Math.max(1, options.concurrency ?? 1));
|
|
83
|
+
return walkDir(root, 0, maxDepth, options, gate);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function walkDir(
|
|
87
|
+
current: string,
|
|
88
|
+
depth: number,
|
|
89
|
+
maxDepth: number,
|
|
90
|
+
options: WalkOptions,
|
|
91
|
+
gate: <T>(fn: () => Promise<T>) => Promise<T>,
|
|
92
|
+
): Promise<WalkEntry[]> {
|
|
93
|
+
if (options.signal?.aborted) throw new FsError("Operation interrupted.", "interrupted");
|
|
94
|
+
let entries;
|
|
95
|
+
try {
|
|
96
|
+
entries = await gate(() => fs.readdir(current, { withFileTypes: true }));
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw fromErrno(error, "access");
|
|
99
|
+
}
|
|
100
|
+
const visible = options.includeHidden ? entries : entries.filter((dirent) => !dirent.name.startsWith("."));
|
|
101
|
+
const resolved = await Promise.all(
|
|
102
|
+
visible.map(async (dirent): Promise<WalkEntry | undefined> => {
|
|
103
|
+
const full = path.join(current, dirent.name);
|
|
104
|
+
try {
|
|
105
|
+
const stats = await gate(() => (options.followSymlinks ? fs.stat(full) : fs.lstat(full)));
|
|
106
|
+
return { path: full, name: dirent.name, stats };
|
|
107
|
+
} catch (error) {
|
|
108
|
+
/* v8 ignore next 3 */
|
|
109
|
+
if (isMissing(error)) return undefined;
|
|
110
|
+
/* v8 ignore next */
|
|
111
|
+
throw fromErrno(error, "access");
|
|
112
|
+
}
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
const subtrees = await Promise.all(
|
|
116
|
+
resolved.map((entry) =>
|
|
117
|
+
entry === undefined || depth + 1 >= maxDepth || !entry.stats.isDirectory()
|
|
118
|
+
? Promise.resolve(undefined)
|
|
119
|
+
: walkDir(entry.path, depth + 1, maxDepth, options, gate),
|
|
120
|
+
),
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const output: WalkEntry[] = [];
|
|
124
|
+
for (let index = 0; index < resolved.length; index++) {
|
|
125
|
+
const entry = resolved[index];
|
|
126
|
+
/* v8 ignore next */
|
|
127
|
+
if (entry === undefined) continue;
|
|
128
|
+
output.push(entry);
|
|
129
|
+
const subtree = subtrees[index];
|
|
130
|
+
/* v8 ignore next */
|
|
131
|
+
if (subtree !== undefined) output.push(...subtree);
|
|
132
|
+
}
|
|
133
|
+
return output;
|
|
134
|
+
}
|
|
135
|
+
|
|
63
136
|
export interface CopyOptions {
|
|
64
137
|
readonly recursive?: boolean;
|
|
65
138
|
readonly dereference?: boolean;
|
|
@@ -173,19 +246,26 @@ export async function lookupGroup(gid: number): Promise<string> {
|
|
|
173
246
|
return (await lookupId("/etc/group", gid)) ?? String(gid);
|
|
174
247
|
}
|
|
175
248
|
|
|
249
|
+
const idCaches = new Map<string, Map<number, string>>();
|
|
250
|
+
|
|
176
251
|
async function lookupId(file: string, id: number): Promise<string | undefined> {
|
|
177
|
-
let
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
252
|
+
let cache = idCaches.get(file);
|
|
253
|
+
if (cache === undefined) {
|
|
254
|
+
cache = new Map();
|
|
255
|
+
let text: string;
|
|
256
|
+
try {
|
|
257
|
+
text = await fs.readFile(file, "utf8");
|
|
258
|
+
} catch {
|
|
259
|
+
/* v8 ignore next */
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
for (const line of text.split("\n")) {
|
|
263
|
+
const fields = line.split(":");
|
|
264
|
+
if (fields.length >= 3) cache.set(Number(fields[2]), fields[0] as string);
|
|
265
|
+
}
|
|
266
|
+
idCaches.set(file, cache);
|
|
187
267
|
}
|
|
188
|
-
return
|
|
268
|
+
return cache.get(id);
|
|
189
269
|
}
|
|
190
270
|
|
|
191
271
|
export function formatDate(date: Date, now = new Date()): string {
|
package/src/core/parallel.ts
CHANGED
|
@@ -66,6 +66,35 @@ export async function mapLimit<T, R>(
|
|
|
66
66
|
return results;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Bounds concurrent async work without holding a slot across nested calls.
|
|
71
|
+
* Useful when a traversal recurses while only its syscalls need limiting.
|
|
72
|
+
*/
|
|
73
|
+
export function createLimiter(limit: number): <T>(fn: () => Promise<T>) => Promise<T> {
|
|
74
|
+
if (!Number.isInteger(limit) || limit < 1) {
|
|
75
|
+
throw new FsError("parallel limit must be a positive integer.", "invalid");
|
|
76
|
+
}
|
|
77
|
+
let active = 0;
|
|
78
|
+
const queue: Array<() => void> = [];
|
|
79
|
+
const release = (): void => {
|
|
80
|
+
const next = queue.shift();
|
|
81
|
+
if (next === undefined) active--;
|
|
82
|
+
else next();
|
|
83
|
+
};
|
|
84
|
+
return async <T>(fn: () => Promise<T>): Promise<T> => {
|
|
85
|
+
if (active < limit) {
|
|
86
|
+
active++;
|
|
87
|
+
} else {
|
|
88
|
+
await new Promise<void>((resolve) => queue.push(resolve));
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
return await fn();
|
|
92
|
+
} finally {
|
|
93
|
+
release();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
69
98
|
export async function forEachLimit<T>(
|
|
70
99
|
items: readonly T[],
|
|
71
100
|
limit: number,
|
package/src/tools/chmod.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import * as fs from "node:fs/promises";
|
|
7
7
|
import { Type, type Static } from "@sinclair/typebox";
|
|
8
8
|
import { FsError, fromErrno } from "../core/errors.js";
|
|
9
|
-
import { num,
|
|
9
|
+
import { num, walkAll } from "../core/fsutil.js";
|
|
10
10
|
import { parseMode } from "../core/mode.js";
|
|
11
11
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
12
12
|
|
|
@@ -51,7 +51,11 @@ export const chmodTool: ToolSpec<Input> = {
|
|
|
51
51
|
if (input.recursive === true) {
|
|
52
52
|
const rootStats = await ctx.sandbox.lstat(root.full, requested);
|
|
53
53
|
if (rootStats.isDirectory()) {
|
|
54
|
-
for
|
|
54
|
+
for (const entry of await walkAll(root.full, {
|
|
55
|
+
recursive: true,
|
|
56
|
+
signal: ctx.signal,
|
|
57
|
+
concurrency: ctx.concurrency,
|
|
58
|
+
})) {
|
|
55
59
|
targets.push({
|
|
56
60
|
full: entry.path,
|
|
57
61
|
outside: root.outside,
|
package/src/tools/diff.ts
CHANGED
|
@@ -8,6 +8,7 @@ import * as path from "node:path";
|
|
|
8
8
|
import { createTwoFilesPatch } from "diff";
|
|
9
9
|
import { Type, type Static } from "@sinclair/typebox";
|
|
10
10
|
import { fromErrno } from "../core/errors.js";
|
|
11
|
+
import { mapLimit } from "../core/parallel.js";
|
|
11
12
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
12
13
|
|
|
13
14
|
const Schema = Type.Object(
|
|
@@ -50,7 +51,7 @@ export const diffTool: ToolSpec<Input> = {
|
|
|
50
51
|
if (input.recursive !== true) {
|
|
51
52
|
return textResult(`Common subdirectories: ${input.left} and ${input.right}\n`, {}, 1);
|
|
52
53
|
}
|
|
53
|
-
const lines = await diffDirs(left.full, right.full, input);
|
|
54
|
+
const lines = await diffDirs(left.full, right.full, input, ctx.concurrency);
|
|
54
55
|
return textResult(lines.join(""), { differences: lines.length > 0 }, lines.length > 0 ? 1 : 0);
|
|
55
56
|
}
|
|
56
57
|
|
|
@@ -87,7 +88,49 @@ export const diffTool: ToolSpec<Input> = {
|
|
|
87
88
|
},
|
|
88
89
|
};
|
|
89
90
|
|
|
90
|
-
|
|
91
|
+
type DiffItem =
|
|
92
|
+
| { readonly kind: "text"; readonly text: string }
|
|
93
|
+
| { readonly kind: "file"; readonly left: string; readonly right: string };
|
|
94
|
+
|
|
95
|
+
async function diffDirs(
|
|
96
|
+
left: string,
|
|
97
|
+
right: string,
|
|
98
|
+
input: Input,
|
|
99
|
+
concurrency: number,
|
|
100
|
+
): Promise<string[]> {
|
|
101
|
+
const items: DiffItem[] = [];
|
|
102
|
+
await planDir(left, right, input, items);
|
|
103
|
+
|
|
104
|
+
const files = items.filter((item): item is Extract<DiffItem, { kind: "file" }> => item.kind === "file");
|
|
105
|
+
const patches = await mapLimit(files, Math.max(1, concurrency), async (item) => {
|
|
106
|
+
const [leftData, rightData] = await Promise.all([fs.readFile(item.left), fs.readFile(item.right)]);
|
|
107
|
+
/* v8 ignore next */
|
|
108
|
+
if (leftData.equals(rightData)) return undefined;
|
|
109
|
+
return createTwoFilesPatch(
|
|
110
|
+
item.left,
|
|
111
|
+
item.right,
|
|
112
|
+
leftData.toString("utf8"),
|
|
113
|
+
rightData.toString("utf8"),
|
|
114
|
+
"",
|
|
115
|
+
"",
|
|
116
|
+
{ context: input.unified ?? 3 },
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const output: string[] = [];
|
|
121
|
+
let index = 0;
|
|
122
|
+
for (const item of items) {
|
|
123
|
+
if (item.kind === "text") {
|
|
124
|
+
output.push(item.text);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const patch = patches[index++];
|
|
128
|
+
if (patch !== undefined) output.push(patch);
|
|
129
|
+
}
|
|
130
|
+
return output;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function planDir(left: string, right: string, input: Input, items: DiffItem[]): Promise<void> {
|
|
91
134
|
const names = new Set<string>();
|
|
92
135
|
for (const dir of [left, right]) {
|
|
93
136
|
for (const name of await fs.readdir(dir)) {
|
|
@@ -95,7 +138,6 @@ async function diffDirs(left: string, right: string, input: Input): Promise<stri
|
|
|
95
138
|
names.add(name);
|
|
96
139
|
}
|
|
97
140
|
}
|
|
98
|
-
const output: string[] = [];
|
|
99
141
|
for (const name of [...names].sort()) {
|
|
100
142
|
const leftPath = path.join(left, name);
|
|
101
143
|
const rightPath = path.join(right, name);
|
|
@@ -103,27 +145,21 @@ async function diffDirs(left: string, right: string, input: Input): Promise<stri
|
|
|
103
145
|
const rightExists = await exists(rightPath);
|
|
104
146
|
if (!leftExists || !rightExists) {
|
|
105
147
|
/* v8 ignore next */
|
|
106
|
-
|
|
148
|
+
items.push({ kind: "text", text: `Only in ${leftExists ? left : right}: ${name}\n` });
|
|
107
149
|
continue;
|
|
108
150
|
}
|
|
109
151
|
const leftStats = await fs.lstat(leftPath);
|
|
110
152
|
const rightStats = await fs.lstat(rightPath);
|
|
111
153
|
if (leftStats.isDirectory() && rightStats.isDirectory()) {
|
|
112
|
-
|
|
154
|
+
await planDir(leftPath, rightPath, input, items);
|
|
113
155
|
/* v8 ignore next 4 */
|
|
114
156
|
} else if (leftStats.isDirectory() !== rightStats.isDirectory()) {
|
|
115
157
|
/* v8 ignore next */
|
|
116
|
-
|
|
158
|
+
items.push({ kind: "text", text: `File ${leftPath} is a directory while file ${rightPath} is not\n` });
|
|
117
159
|
} else {
|
|
118
|
-
|
|
119
|
-
const rightData = await fs.readFile(rightPath);
|
|
120
|
-
/* v8 ignore next */
|
|
121
|
-
if (!leftData.equals(rightData)) {
|
|
122
|
-
output.push(createTwoFilesPatch(leftPath, rightPath, leftData.toString("utf8"), rightData.toString("utf8"), "", "", { context: input.unified ?? 3 }));
|
|
123
|
-
}
|
|
160
|
+
items.push({ kind: "file", left: leftPath, right: rightPath });
|
|
124
161
|
}
|
|
125
162
|
}
|
|
126
|
-
return output;
|
|
127
163
|
}
|
|
128
164
|
|
|
129
165
|
async function exists(target: string): Promise<boolean> {
|
package/src/tools/du.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import * as fs from "node:fs/promises";
|
|
7
7
|
import { Type, type Static } from "@sinclair/typebox";
|
|
8
|
-
import { num,
|
|
8
|
+
import { num, walkAll } from "../core/fsutil.js";
|
|
9
9
|
import { humanSize } from "../core/format.js";
|
|
10
10
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
11
11
|
|
|
@@ -56,12 +56,13 @@ export const duTool: ToolSpec<Input> = {
|
|
|
56
56
|
local.push({ path: requested, bytes: total });
|
|
57
57
|
}
|
|
58
58
|
if (rootStats.isDirectory()) {
|
|
59
|
-
for
|
|
59
|
+
for (const entry of await walkAll(target.full, {
|
|
60
60
|
recursive: true,
|
|
61
61
|
followSymlinks: input.follow === true,
|
|
62
62
|
...(input.maxDepth === undefined ? {} : { maxDepth: input.maxDepth }),
|
|
63
63
|
includeHidden: true,
|
|
64
64
|
signal: ctx.signal,
|
|
65
|
+
concurrency: ctx.concurrency,
|
|
65
66
|
})) {
|
|
66
67
|
/* v8 ignore next */
|
|
67
68
|
if (input.oneFileSystem === true && num(entry.stats.dev) !== rootDev) continue;
|
package/src/tools/find.ts
CHANGED
|
@@ -8,7 +8,7 @@ import * as path from "node:path";
|
|
|
8
8
|
import picomatch from "picomatch";
|
|
9
9
|
import { Type, type Static } from "@sinclair/typebox";
|
|
10
10
|
import { FsError } from "../core/errors.js";
|
|
11
|
-
import { lookupGroup, lookupUser, modeString, num, typeName,
|
|
11
|
+
import { lookupGroup, lookupUser, modeString, num, typeName, walkAll } from "../core/fsutil.js";
|
|
12
12
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
13
13
|
|
|
14
14
|
const Schema = Type.Object(
|
|
@@ -161,12 +161,13 @@ export const findTool: ToolSpec<Input> = {
|
|
|
161
161
|
}
|
|
162
162
|
continue;
|
|
163
163
|
}
|
|
164
|
-
for
|
|
164
|
+
for (const entry of await walkAll(target.full, {
|
|
165
165
|
recursive: true,
|
|
166
166
|
followSymlinks: input.follow === true,
|
|
167
167
|
...(input.maxDepth === undefined ? {} : { maxDepth: input.maxDepth }),
|
|
168
168
|
includeHidden: true,
|
|
169
169
|
signal: ctx.signal,
|
|
170
|
+
concurrency: ctx.concurrency,
|
|
170
171
|
})) {
|
|
171
172
|
const relative = path.relative(target.full, entry.path);
|
|
172
173
|
const depth = relative.split(path.sep).length;
|
package/src/tools/grep.ts
CHANGED
|
@@ -7,7 +7,8 @@ import * as fs from "node:fs/promises";
|
|
|
7
7
|
import picomatch from "picomatch";
|
|
8
8
|
import { Type, type Static } from "@sinclair/typebox";
|
|
9
9
|
import { FsError, fromErrno } from "../core/errors.js";
|
|
10
|
-
import {
|
|
10
|
+
import { walkAll } from "../core/fsutil.js";
|
|
11
|
+
import { mapLimit } from "../core/parallel.js";
|
|
11
12
|
import { collect, inputStream, lines } from "../core/stream.js";
|
|
12
13
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
13
14
|
|
|
@@ -57,6 +58,13 @@ export const grepTool: ToolSpec<Input> = {
|
|
|
57
58
|
mutates: false,
|
|
58
59
|
async run(ctx, input, io) {
|
|
59
60
|
const regex = buildRegex(input);
|
|
61
|
+
const include = input.include?.length ? picomatch(input.include, { dot: true, basename: true }) : undefined;
|
|
62
|
+
const exclude = input.exclude?.length ? picomatch(input.exclude, { dot: true, basename: true }) : undefined;
|
|
63
|
+
const isIncluded = (file: string): boolean => {
|
|
64
|
+
if (include !== undefined && !include(file)) return false;
|
|
65
|
+
if (exclude !== undefined && exclude(file)) return false;
|
|
66
|
+
return true;
|
|
67
|
+
};
|
|
60
68
|
const paths = input.paths ?? [];
|
|
61
69
|
const targets: Target[] = [];
|
|
62
70
|
if (paths.length > 0) {
|
|
@@ -76,19 +84,20 @@ export const grepTool: ToolSpec<Input> = {
|
|
|
76
84
|
if (input.recursive !== true) {
|
|
77
85
|
throw new FsError(`"${requested}" is a directory; recursive is required.`, "not-file");
|
|
78
86
|
}
|
|
79
|
-
for
|
|
87
|
+
for (const entry of await walkAll(target.full, {
|
|
80
88
|
recursive: true,
|
|
81
89
|
includeHidden: true,
|
|
82
90
|
signal: ctx.signal,
|
|
91
|
+
concurrency: ctx.concurrency,
|
|
83
92
|
})) {
|
|
84
93
|
/* v8 ignore next */
|
|
85
94
|
if (!entry.stats.isFile()) continue;
|
|
86
95
|
const resource = entry.path;
|
|
87
|
-
if (!
|
|
96
|
+
if (!isIncluded(resource)) continue;
|
|
88
97
|
targets.push({ requested: resource, full: entry.path });
|
|
89
98
|
}
|
|
90
99
|
/* v8 ignore next */
|
|
91
|
-
} else if (
|
|
100
|
+
} else if (isIncluded(target.full)) {
|
|
92
101
|
targets.push({ requested, full: target.full });
|
|
93
102
|
}
|
|
94
103
|
}
|
|
@@ -105,18 +114,27 @@ export const grepTool: ToolSpec<Input> = {
|
|
|
105
114
|
output.push(...result.lines);
|
|
106
115
|
matchedAny = result.matched;
|
|
107
116
|
} else {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
117
|
+
const showName = input.noFilename !== true && (input.withFilename === true || targets.length > 1 || input.recursive === true);
|
|
118
|
+
const results = await mapLimit(
|
|
119
|
+
targets,
|
|
120
|
+
ctx.concurrency,
|
|
121
|
+
async (target) => {
|
|
122
|
+
let data: Buffer;
|
|
123
|
+
try {
|
|
124
|
+
data = await fs.readFile(target.full);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
/* v8 ignore next 3 */
|
|
127
|
+
if (input.noMessages === true) return undefined;
|
|
128
|
+
/* v8 ignore next */
|
|
129
|
+
throw fromErrno(error);
|
|
130
|
+
}
|
|
131
|
+
return search(target.requested, data, regex, input, showName);
|
|
132
|
+
},
|
|
133
|
+
ctx.signal,
|
|
134
|
+
);
|
|
135
|
+
for (const result of results) {
|
|
136
|
+
/* v8 ignore next */
|
|
137
|
+
if (result === undefined) continue;
|
|
120
138
|
output.push(...result.lines);
|
|
121
139
|
matchedAny ||= result.matched;
|
|
122
140
|
}
|
|
@@ -136,14 +154,6 @@ function buildRegex(input: Input): RegExp {
|
|
|
136
154
|
}
|
|
137
155
|
}
|
|
138
156
|
|
|
139
|
-
function included(file: string, input: Input): boolean {
|
|
140
|
-
const include = input.include?.length ? picomatch(input.include, { dot: true, basename: true }) : undefined;
|
|
141
|
-
const exclude = input.exclude?.length ? picomatch(input.exclude, { dot: true, basename: true }) : undefined;
|
|
142
|
-
if (include !== undefined && !include(file)) return false;
|
|
143
|
-
if (exclude !== undefined && exclude(file)) return false;
|
|
144
|
-
return true;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
157
|
function search(
|
|
148
158
|
name: string,
|
|
149
159
|
data: Buffer,
|
package/src/tools/ls.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { Type, type Static } from "@sinclair/typebox";
|
|
|
10
10
|
import { FsError, fromErrno } from "../core/errors.js";
|
|
11
11
|
import { formatDate, lookupGroup, lookupUser, modeString, num, typeName } from "../core/fsutil.js";
|
|
12
12
|
import { humanSize } from "../core/format.js";
|
|
13
|
+
import { mapLimit } from "../core/parallel.js";
|
|
13
14
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
14
15
|
|
|
15
16
|
const Schema = Type.Object(
|
|
@@ -105,7 +106,7 @@ async function collect(
|
|
|
105
106
|
const total = entries.reduce((sum, entry) => sum + num(entry.stats.blocks), 0);
|
|
106
107
|
lines.push(`total ${total}`);
|
|
107
108
|
}
|
|
108
|
-
lines.push(...(await formatEntries(entries, input)));
|
|
109
|
+
lines.push(...(await formatEntries(entries, input, ctx.concurrency)));
|
|
109
110
|
if (recursive) {
|
|
110
111
|
for (const entry of entries) {
|
|
111
112
|
if (entry.name === "." || entry.name === ".." || !entry.stats.isDirectory()) continue;
|
|
@@ -206,8 +207,8 @@ function versionCompare(left: string, right: string): number {
|
|
|
206
207
|
return 0;
|
|
207
208
|
}
|
|
208
209
|
|
|
209
|
-
async function formatEntries(entries: Entry[], input: Input): Promise<string[]> {
|
|
210
|
-
return
|
|
210
|
+
async function formatEntries(entries: Entry[], input: Input, concurrency: number): Promise<string[]> {
|
|
211
|
+
return mapLimit(entries, Math.max(1, concurrency), (entry) => formatEntry(entry, input));
|
|
211
212
|
}
|
|
212
213
|
|
|
213
214
|
async function formatEntry(entry: Entry, input: Input): Promise<string> {
|