-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepofs.ts
More file actions
126 lines (99 loc) · 3.63 KB
/
Copy pathrepofs.ts
File metadata and controls
126 lines (99 loc) · 3.63 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
import fsp from "node:fs/promises";
import path from "node:path";
import { Repo } from "./git/git.ts";
import type { FilterOptions } from "./pathindex/pathindex.ts";
import { PathIndex } from "./pathindex/pathindex.ts";
import type { GrepOptions, GrepResult } from "./rg/rg.ts";
import { grep } from "./rg/rg.ts";
export class RepoFsError extends Error {
constructor(message: string, options?: ErrorOptions) {
super(message, options);
this.name = "RepoFsError";
}
}
/**
* Scoped filesystem facade for a git repository. Composes {@link PathIndex} for file discovery,
* {@link Repo} for git operations, and ripgrep for content search behind a containment layer
* that prevents path escape via `..`.
*/
export class RepoFs {
readonly #root: string;
readonly #index: PathIndex;
readonly #git: Repo;
readonly #pendingReadPaths: string[] = [];
private constructor(root: string, index: PathIndex, git: Repo) {
this.#root = root;
this.#index = index;
this.#git = git;
}
/** Builds the file index and validates git in parallel. */
static async open(root: string, signal?: AbortSignal): Promise<RepoFs> {
const absRoot = path.resolve(root);
const info = await fsp.stat(absRoot).catch((err) => {
throw new RepoFsError(`${absRoot}: cannot access root`, { cause: err as Error });
});
if (!info.isDirectory()) {
throw new RepoFsError(`${absRoot}: not a directory`);
}
const [index, git] = await Promise.all([PathIndex.new(absRoot, signal), Repo.open(absRoot, signal)]);
return new RepoFs(absRoot, index, git);
}
get root(): string {
return this.#root;
}
get git(): Repo {
return this.#git;
}
/** O(1) lookup, fuzzy search, and glob filtering. Built once at {@link open}. */
get index(): PathIndex {
return this.#index;
}
/** Normalizes any path (absolute or relative) to POSIX relative from root. Throws on escape. */
resolve(p: string): string {
const abs = path.resolve(this.#root, path.posix.normalize(p.replaceAll("\\", "/")));
const rel = path.relative(this.#root, abs).replaceAll("\\", "/");
if (rel.startsWith("..")) {
throw new RepoFsError(`path escapes root: ${p}`);
}
return rel;
}
fileExists(p: string): boolean {
return this.#index.get(this.resolve(p))?.type === "file";
}
dirExists(p: string): boolean {
return this.#index.dir(this.resolve(p)) !== undefined;
}
/** Resolves path, validates file entry in index, returns raw bytes. */
async readFile(p: string): Promise<Buffer> {
const rel = this.resolve(p);
const entry = this.#index.get(rel);
if (!entry) {
throw new RepoFsError(`file not found: ${rel}`);
}
if (entry.type !== "file") {
throw new RepoFsError(`${rel}: not a regular file (${entry.type})`);
}
this.#pendingReadPaths.push(rel);
return fsp.readFile(path.resolve(this.#root, rel));
}
/** Fuzzy search via {@link PathIndex.fuzzySearch}. */
findFiles(pattern: string, options?: FilterOptions): string[] {
return this.#index.fuzzySearch(pattern, options);
}
/** Glob filter via {@link PathIndex.globSearch}. */
globFiles(options?: FilterOptions): string[] {
return this.#index.globSearch(options);
}
/** Content search via ripgrep, scoped to root. */
async grep(pattern: string, options?: GrepOptions, signal?: AbortSignal): Promise<GrepResult> {
return grep(this.#root, pattern, options, signal);
}
/** Returns and clears paths from successful {@link readFile} calls. Hook for instruction resolution. */
flushReadPaths(): string[] {
const paths = [...this.#pendingReadPaths];
this.#pendingReadPaths.length = 0;
return paths;
}
}
export type { FilterOptions } from "./pathindex/pathindex.ts";
export type { GrepOptions, GrepResult } from "./rg/rg.ts";