-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathindex.ts
More file actions
507 lines (449 loc) · 11.9 KB
/
Copy pathpathindex.ts
File metadata and controls
507 lines (449 loc) · 11.9 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
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";
/** Directory names skipped during filesystem walk. Never descended into. */
const WALK_EXCLUDE_DIRS: ReadonlySet<string> = new Set([".git"]);
/** Extensions treated as binary without opening the file. Stat-only — no line counting, no fd consumed. */
const BINARY_EXTENSIONS: ReadonlySet<string> = new Set([
// Images
".png",
".jpg",
".jpeg",
".gif",
".bmp",
".ico",
".svg",
".webp",
".tiff",
".tif",
".psd",
".dds",
".tga",
".hdr",
".exr",
".ktx",
".ktx2",
".astc",
".pvr",
".basis",
// Audio
".wav",
".mp3",
".ogg",
".flac",
".aac",
".wma",
".m4a",
".opus",
// Video
".mp4",
".avi",
".mkv",
".mov",
".wmv",
".webm",
".flv",
// 3D / geometry
".fbx",
".obj",
".gltf",
".glb",
".blend",
".dae",
".3ds",
".stl",
".usd",
".usda",
".usdc",
".usdz",
// Compiled / binary
".exe",
".dll",
".so",
".dylib",
".o",
".a",
".lib",
".pdb",
".wasm",
".class",
".jar",
".pyc",
".pyo",
// Archives
".zip",
".tar",
".gz",
".bz2",
".xz",
".7z",
".rar",
".zst",
".lz4",
// Fonts
".ttf",
".otf",
".woff",
".woff2",
".eot",
// Documents / data
".pdf",
".doc",
".docx",
".xls",
".xlsx",
".ppt",
".pptx",
".sqlite",
".db",
".mdb",
// Game engine assets (general)
".bin",
".pak",
".dat",
".res",
".asset",
".bundle",
".bank",
".bsp",
".nav",
".lightmap",
".cubemap",
".mdl",
".vtf",
".vpk",
".pck",
// Compiled shaders
".spv",
".cso",
".dxbc",
".metallib",
// Unreal Engine
".uasset",
".umap",
".ubulk",
".upk",
// Audio middleware (FMOD / Wwise)
".fsb",
".fev",
".bnk",
// Dagor Engine
".dag",
".dynmodel",
".rendinst",
".composit",
".gameobj",
".lod",
]);
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) {
if (WALK_EXCLUDE_DIRS.has(dirent.name)) continue;
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;
}
const ext = path.extname(dirent.name).toLowerCase();
if (BINARY_EXTENSIONS.has(ext)) {
work.push(
fs.stat(absPath).then((s) => {
const entry: IndexEntryFile = {
type: "file",
name: dirent.name,
size: s.size,
lineCount: 0,
maxLineLen: 0,
isBinary: true,
};
dirEntry.children.push(entry);
this.paths.push(relPath);
this.#entries.set(relPath, entry);
}),
);
} else {
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;
}