-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathindex.ts
More file actions
349 lines (295 loc) · 9.74 KB
/
Copy pathpathindex.ts
File metadata and controls
349 lines (295 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { createReadStream } from "node:fs";
import fs from "node:fs/promises";
import nativePath from "node:path";
import path from "node:path/posix";
import fuzzysort from "fuzzysort";
import picomatch from "picomatch";
import { ErrBinaryContent, LineIter } from "../lineiter/lineiter.ts";
export type IndexEntryFile = {
type: "file";
name: string;
size: number;
/** Total number of lines. 0 for binary files. */
lineCount: number;
/** Byte length of the longest line (excluding `\n`). 0 for binary/empty files. */
maxLineLen: number;
/** True when the file's leading bytes contain a null byte. */
isBinary: boolean;
};
export type IndexEntryDir = {
type: "dir";
name: string;
children: IndexEntry[];
};
export type IndexEntrySymlink = {
type: "symlink";
name: string;
/** Raw readlink result — may be relative to the symlink's parent directory. */
target: string;
/** What the symlink resolves to. "unknown" for dangling symlinks. */
targetType: "file" | "dir" | "unknown";
/** Target's byte size if targetType is "file", 0 otherwise. */
size: number;
};
export type IndexEntry = IndexEntryFile | IndexEntryDir | IndexEntrySymlink;
export type FilterOptions = {
/** Keep paths matching at least one glob. No slash → matchBase (any depth). */
includes?: string[];
/** Drop paths matching any glob. No slash → matchBase (any depth). */
excludes?: string[];
maxResults?: number;
};
/**
* Tree-structured file index. Built once via {@link PathIndex.new}, immutable after construction.
* Paths are relative to root, forward slashes on all platforms.
*/
export class PathIndex {
readonly absRoot: string;
readonly root: IndexEntryDir = { type: "dir", name: "", children: [] };
readonly paths: string[] = [];
readonly #entries = new Map<string, IndexEntry>();
readonly #fdPool = new Pool(256);
private constructor(rootPath: string) {
this.absRoot = nativePath.resolve(rootPath);
}
/** Paths ending with "/" are directories; without are files. */
static from(rootPath: string, paths: string[]): PathIndex {
const ix = new PathIndex(rootPath);
for (let p of paths.toSorted()) {
const segments = p.split("/");
let file: string | undefined;
if (p.endsWith("/")) {
segments.pop();
p = p.slice(0, -1);
} else {
file = segments.pop();
}
let parent = ix.root;
for (const [i, name] of segments.entries()) {
if (!name) continue;
const relPath = segments.slice(0, i + 1).join("/");
let dir = ix.#entries.get(relPath) as IndexEntryDir | undefined;
if (!dir) {
dir = { type: "dir", name, children: [] };
parent.children.push(dir);
ix.#entries.set(relPath, dir);
}
parent = dir;
}
if (file) {
const entry: IndexEntryFile = {
type: "file",
name: file,
size: 0,
lineCount: 0,
maxLineLen: 0,
isBinary: false,
};
parent.children.push(entry);
ix.#entries.set(p, entry);
}
ix.paths.push(p);
}
return ix;
}
/** Walk the filesystem tree rooted at `rootPath` and return a tree-structured index. */
static async new(rootPath: string, signal?: AbortSignal): Promise<PathIndex> {
const ix = new PathIndex(rootPath);
ix.root.children = (await ix.#walkDir("", signal)).children;
ix.paths.sort();
return ix;
}
get length(): number {
return this.paths.length;
}
/** O(1) lookup of an entry by its relative path. */
get(entryPath: string): IndexEntry | undefined {
return this.#entries.get(entryPath);
}
/** Filter paths by include/exclude globs. */
globSearch(opts?: FilterOptions): string[] {
return capResults(applyGlobFilters(this.paths, opts), opts?.maxResults);
}
/** Multi-word fuzzy search with optional glob pre-filtering. Each word narrows survivors of the previous. */
fuzzySearch(pattern: string, opts?: FilterOptions): string[] {
const source = applyGlobFilters(this.paths, opts);
const words = pattern.split(/\s+/).filter(Boolean);
if (words.length === 0) return capResults(source, opts?.maxResults);
let current = source;
for (const word of words) {
if (current.length === 0) break;
const matches = fuzzysort.go(word, current);
current = matches.map((m) => m.target);
}
return capResults(current, opts?.maxResults);
}
/** O(1) directory lookup by relative path. `"."` or `""` returns root. */
dir(dirPath: string): IndexEntryDir | undefined {
const normalized = path.join(dirPath || ".", ".");
if (normalized === ".") return this.root;
const entry = this.#entries.get(normalized);
return entry?.type === "dir" ? entry : undefined;
}
/** Parallel recursive walk — all children (subdirs + file stats) dispatched via Promise.all per directory. */
async #walkDir(dir: string, signal?: AbortSignal): Promise<IndexEntryDir> {
signal?.throwIfAborted();
const dirEntries = await fs.readdir(nativePath.resolve(this.absRoot, dir), { withFileTypes: true });
const dirEntry: IndexEntryDir = { type: "dir", name: path.basename(dir), children: [] };
const work: Promise<void>[] = [];
for (const dirent of dirEntries) {
const relPath = path.join(dir, dirent.name);
const absPath = nativePath.join(this.absRoot, relPath);
if (dirent.isSymbolicLink()) {
work.push(
this.#resolveSymlink(absPath, dirent.name).then((entry) => {
if (entry) {
dirEntry.children.push(entry);
this.paths.push(relPath);
this.#entries.set(relPath, entry);
}
}),
);
continue;
}
if (dirent.isDirectory()) {
work.push(
this.#walkDir(relPath, signal).then((subtree) => {
dirEntry.children.push(subtree);
this.#entries.set(relPath, subtree);
}),
);
continue;
}
work.push(
Promise.all([fs.stat(absPath), this.#countLines(absPath)]).then(([s, lc]) => {
const entry: IndexEntryFile = {
type: "file",
name: dirent.name,
size: s.size,
lineCount: lc.lineCount,
maxLineLen: lc.maxLineLen,
isBinary: lc.isBinary,
};
dirEntry.children.push(entry);
this.paths.push(relPath);
this.#entries.set(relPath, entry);
}),
);
}
await Promise.all(work);
return dirEntry;
}
/** readlink for target path, stat (follows symlink) for size/type. Returns undefined on error. */
async #resolveSymlink(absPath: string, name: string): Promise<IndexEntrySymlink | undefined> {
try {
const [target, stats] = await Promise.all([fs.readlink(absPath), fs.stat(absPath).catch(() => null)]);
let targetType: IndexEntrySymlink["targetType"] = "unknown";
let size = 0;
if (stats) {
if (stats.isFile()) {
targetType = "file";
size = stats.size;
} else if (stats.isDirectory()) {
targetType = "dir";
}
}
return { type: "symlink", name, target, targetType, size };
} catch {
return undefined;
}
}
/** Body-less line scan via {@link LineIter}. Binary files are detected by the null byte probe on the first chunk. */
async #countLines(absPath: string): Promise<{ lineCount: number; maxLineLen: number; isBinary: boolean }> {
await this.#fdPool.acquire();
const stream = createReadStream(absPath);
try {
const iter = await LineIter.new(stream, { lineCap: 0 });
let maxLineLen = 0;
while (await iter.next()) {
if (iter.len() > maxLineLen) maxLineLen = iter.len();
}
return { lineCount: iter.num(), maxLineLen, isBinary: false };
} catch (err) {
if (err instanceof ErrBinaryContent) {
return { lineCount: 0, maxLineLen: 0, isBinary: true };
}
throw err;
} finally {
stream.destroy();
this.#fdPool.release();
}
}
}
/** Bounds concurrent async operations. {@link acquire} blocks when the limit is reached. */
class Pool {
readonly #limit: number;
#active = 0;
#queue: (() => void)[] = [];
constructor(limit: number) {
this.#limit = limit;
}
/** Take a slot. Returns a promise that resolves when a slot is available. */
acquire(): Promise<void> | void {
if (this.#active < this.#limit) {
this.#active++;
return;
}
return new Promise<void>((resolve) => this.#queue.push(resolve));
}
/** Return a slot. Wakes the next waiter if any. */
release(): void {
const next = this.#queue.shift();
if (next) {
next();
} else {
this.#active--;
}
}
}
function applyGlobFilters(paths: string[], opts?: FilterOptions): string[] {
if (!opts?.includes?.length && !opts?.excludes?.length) return paths;
const includeMatchers = opts.includes?.length ? compileGlobs(opts.includes) : [];
const excludeMatchers = opts.excludes?.length ? compileGlobs(opts.excludes) : [];
const filtered: string[] = [];
for (const p of paths) {
if (excludeMatchers.length > 0 && matchesAny(p, excludeMatchers)) continue;
if (includeMatchers.length > 0 && !matchesAny(p, includeMatchers)) continue;
filtered.push(p);
}
return filtered;
}
/** No slash in pattern → matchBase: true (matches basename at any depth). Slash → path-aware match. */
function compileGlobs(patterns: string[]): picomatch.Matcher[] {
const matchers: picomatch.Matcher[] = [];
for (const pattern of patterns) {
try {
matchers.push(picomatch(pattern, pattern.includes("/") ? undefined : { matchBase: true }));
} catch {
// skip invalid patterns
}
}
return matchers;
}
/** Returns an error message for the first invalid glob, or undefined if all are valid. */
export function validateGlobs(patterns: string[]): string | undefined {
for (const pattern of patterns) {
try {
picomatch(pattern);
} catch (err) {
return `invalid glob ${JSON.stringify(pattern)}: ${err instanceof Error ? err.message : String(err)}`;
}
}
return undefined;
}
function matchesAny(p: string, matchers: picomatch.Matcher[]): boolean {
for (const m of matchers) {
if (m(p)) return true;
}
return false;
}
function capResults(paths: string[], limit?: number): string[] {
if (limit != null && limit > 0 && paths.length > limit) {
return paths.slice(0, limit);
}
return paths;
}