@anhlp/alp-daemon 0.0.0-stage → 0.0.1-beta.3
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 +202 -0
- package/NOTICE +7 -0
- package/README.md +10 -2
- package/dist/THIRD-PARTY-NOTICES.txt +431 -0
- package/dist/alp-templates.d.ts +5 -0
- package/dist/alpd.js +42558 -0
- package/dist/build-manifest.json +111 -0
- package/dist/files.d.ts +110 -0
- package/dist/files.js +350 -0
- package/dist/gitignore.d.ts +57 -0
- package/dist/gitignore.js +248 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +8 -0
- package/dist/lock.d.ts +21 -0
- package/dist/lock.js +42 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +271 -0
- package/dist/operations.d.ts +23 -0
- package/dist/operations.js +275 -0
- package/dist/rpc-error.d.ts +6 -0
- package/dist/rpc-error.js +11 -0
- package/dist/server.d.ts +48 -0
- package/dist/server.js +1158 -0
- package/dist/store.d.ts +108 -0
- package/dist/store.js +279 -0
- package/package.json +100 -4
- package/src/alp-templates.d.ts +5 -0
- package/src/files.ts +438 -0
- package/src/gitignore.ts +267 -0
- package/src/index.ts +5 -0
- package/src/lock.ts +53 -0
- package/src/main.ts +286 -0
- package/src/operations.ts +300 -0
- package/src/rpc-error.ts +4 -0
- package/src/server.ts +1332 -0
- package/src/store.ts +336 -0
package/src/files.ts
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { lstat, open, opendir, realpath, stat } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { ERROR, RPC_ERROR, WORKSPACE_FILES, blockedFilePath } from '@anhlp/alp-protocol';
|
|
5
|
+
import { gitIgnored, IgnoreUnknown, GIT_CHECK, type GitCheckOptions } from './gitignore.js';
|
|
6
|
+
|
|
7
|
+
// For tests: which git alpd would run, and how many run now.
|
|
8
|
+
export { gitChecksRunning, resolveGit, GIT_CHECK } from './gitignore.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `workspace.files.search` (docs/protocol/alp-json-rpc-v1.md §12.3): the files of a workspace whose
|
|
12
|
+
* path matches a query, as Paseo's command center finds them (server/utils/directory-suggestions.ts
|
|
13
|
+
* at b63a325, with its workspace settings: relative paths, hidden and heavy directories skipped).
|
|
14
|
+
*
|
|
15
|
+
* Read-only and confined to the workspace: the walk starts at the root's real path and never
|
|
16
|
+
* follows a symbolic link (a link is neither listed nor entered). Each directory is checked before
|
|
17
|
+
* and after it is read: a real directory (not a link), at the real path it was reached by, the same
|
|
18
|
+
* one (device and inode) both times; otherwise what was read is dropped. Only names are read, never
|
|
19
|
+
* contents. A link swapped in and back out between those checks, by a writer racing inside the
|
|
20
|
+
* workspace, can still expose names of the directory it pointed to (Node has no openat/O_NOFOLLOW
|
|
21
|
+
* walk); docs/protocol/alp-json-rpc-v1.md §12.3. The walk stops at {@link LIMITS}, checked per
|
|
22
|
+
* entry; the answer then says `truncated`.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export const LIMITS = Object.freeze({
|
|
26
|
+
/** Directory levels below the root that are entered (Paseo's DEFAULT_MAX_DEPTH). */
|
|
27
|
+
depth: 12,
|
|
28
|
+
/** Directory entries looked at (Paseo's DEFAULT_MAX_ENTRIES_SCANNED). */
|
|
29
|
+
scanned: 20_000,
|
|
30
|
+
/** Wall time of one search. */
|
|
31
|
+
ms: 3_000,
|
|
32
|
+
/** Results by default and at most (Paseo's DEFAULT_LIMIT, MAX_LIMIT). */
|
|
33
|
+
defaultLimit: 30,
|
|
34
|
+
maxLimit: 100,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
/** Hidden directories that are still searched (Paseo's WORKSPACE_SEARCH_HIDDEN_DIRECTORIES, with ALP's own). */
|
|
38
|
+
const HIDDEN_DIRECTORIES = new Set(['.agents', '.alp', '.claude', '.codex', '.github', '.opencode', '.paseo', '.vscode']);
|
|
39
|
+
/** Directories never searched (Paseo's IGNORED_DIRECTORY_NAMES). */
|
|
40
|
+
const IGNORED_DIRECTORIES = new Set(['node_modules', 'venv', 'env', 'virtualenv', 'dist', 'build', 'target', 'out', 'coverage', 'vendor', '__pycache__', '.git']);
|
|
41
|
+
|
|
42
|
+
const NONE = Number.MAX_SAFE_INTEGER;
|
|
43
|
+
|
|
44
|
+
type Rank = { path: string; tier: number; segment: number; offset: number; fuzzy: number; depth: number };
|
|
45
|
+
|
|
46
|
+
/** The directory at `directory` as reached by name: a real directory there, not a link, or null. */
|
|
47
|
+
async function identity(directory: string): Promise<string | null> {
|
|
48
|
+
const info = await lstat(directory, { bigint: true });
|
|
49
|
+
if (!info.isDirectory() || (await realpath(directory)) !== directory) return null;
|
|
50
|
+
return `${info.dev}:${info.ino}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Paseo's scoreFuzzySubsequence: the query's letters in order; the first offset plus the gaps, or null. */
|
|
54
|
+
function fuzzy(query: string, candidate: string): number | null {
|
|
55
|
+
let at = 0;
|
|
56
|
+
let first = -1;
|
|
57
|
+
let previous = -1;
|
|
58
|
+
let gaps = 0;
|
|
59
|
+
for (let index = 0; index < candidate.length && at < query.length; index++) {
|
|
60
|
+
if (candidate[index] !== query[at]) continue;
|
|
61
|
+
if (first < 0) first = index;
|
|
62
|
+
if (previous >= 0) gaps += index - previous - 1;
|
|
63
|
+
previous = index;
|
|
64
|
+
at++;
|
|
65
|
+
}
|
|
66
|
+
return at === query.length && first >= 0 ? first + gaps : null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Paseo's rankTextMatch (a word) and rankPathMatch (a query with a slash), simplified; null: no match. */
|
|
70
|
+
function rank(relative: string, query: string): Rank | null {
|
|
71
|
+
const lower = relative.toLowerCase();
|
|
72
|
+
const segments = lower.split('/');
|
|
73
|
+
const depth = segments.length;
|
|
74
|
+
if (!query) return { path: relative, tier: 3, segment: NONE, offset: NONE, fuzzy: NONE, depth };
|
|
75
|
+
const offset = lower.indexOf(query);
|
|
76
|
+
if (query.includes('/')) {
|
|
77
|
+
if (offset >= 0) return { path: relative, tier: lower.startsWith(query) ? 1 : 3, segment: NONE, offset, fuzzy: NONE, depth };
|
|
78
|
+
const spread = fuzzy(query, lower);
|
|
79
|
+
return spread === null ? null : { path: relative, tier: 4, segment: NONE, offset: NONE, fuzzy: spread, depth };
|
|
80
|
+
}
|
|
81
|
+
const last = fuzzy(query, segments.at(-1) ?? '');
|
|
82
|
+
const tiers: Array<(segment: string) => boolean> = [segment => segment === query, segment => segment.startsWith(query), segment => segment.includes(query)];
|
|
83
|
+
for (const [tier, test] of tiers.entries()) {
|
|
84
|
+
const segment = segments.findIndex(test);
|
|
85
|
+
if (segment >= 0) return { path: relative, tier, segment, offset: offset >= 0 ? offset : NONE, fuzzy: last ?? NONE, depth };
|
|
86
|
+
}
|
|
87
|
+
if (last !== null) return { path: relative, tier: 4, segment: NONE, offset: NONE, fuzzy: last, depth };
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const compare = (a: Rank, b: Rank) =>
|
|
92
|
+
a.tier - b.tier || a.segment - b.segment || a.offset - b.offset || a.fuzzy - b.fuzzy || a.depth - b.depth || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
93
|
+
|
|
94
|
+
export class FilesError extends Error {}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The files under `projectRoot` matching `query`, best first. `projectRoot` must be an absolute path
|
|
98
|
+
* of a directory; anything else throws FilesError.
|
|
99
|
+
*/
|
|
100
|
+
export async function searchWorkspaceFiles(projectRoot: string, { query = '', limit = LIMITS.defaultLimit, now = Date.now, git }: { query?: string; limit?: number; now?: () => number; git?: GitCheckOptions } = {}): Promise<{ files: Array<{ path: string }>; truncated?: true }> {
|
|
101
|
+
if (!path.isAbsolute(projectRoot)) throw new FilesError('projectRoot must be an absolute path');
|
|
102
|
+
let root: string;
|
|
103
|
+
try {
|
|
104
|
+
root = await realpath(projectRoot);
|
|
105
|
+
if (!(await stat(root)).isDirectory()) throw new Error('not a directory');
|
|
106
|
+
} catch {
|
|
107
|
+
throw new FilesError('projectRoot is not a directory alpd can read');
|
|
108
|
+
}
|
|
109
|
+
const wanted = query.trim().replace(/\\/g, '/').replace(/^\.?\/+/, '').toLowerCase();
|
|
110
|
+
const count = Math.max(1, Math.min(LIMITS.maxLimit, Math.trunc(limit)));
|
|
111
|
+
const deadline = now() + LIMITS.ms;
|
|
112
|
+
const realDeadline = Date.now() + LIMITS.ms;
|
|
113
|
+
const found: Rank[] = [];
|
|
114
|
+
let scanned = 0;
|
|
115
|
+
let truncated = false;
|
|
116
|
+
// Breadth first: shallow files are found before the limits stop the walk.
|
|
117
|
+
let level: string[] = [''];
|
|
118
|
+
for (let depth = 0; level.length && !truncated; depth++) {
|
|
119
|
+
const next: string[] = [];
|
|
120
|
+
const levelFiles: Rank[] = [];
|
|
121
|
+
for (const relative of level) {
|
|
122
|
+
if (scanned >= LIMITS.scanned || now() > deadline) { truncated = true; break; }
|
|
123
|
+
// Reached by names alone from the real root, so it is inside it unless a link took its place.
|
|
124
|
+
const directory = relative ? path.join(root, ...relative.split('/')) : root;
|
|
125
|
+
const files: Rank[] = [];
|
|
126
|
+
const directories: string[] = [];
|
|
127
|
+
try {
|
|
128
|
+
const before = await identity(directory);
|
|
129
|
+
if (!before) continue;
|
|
130
|
+
// Iterated, not read whole: the limits hold inside one large directory too.
|
|
131
|
+
for await (const entry of await opendir(directory)) {
|
|
132
|
+
if (++scanned > LIMITS.scanned || now() > deadline) { truncated = true; break; }
|
|
133
|
+
const name = entry.name;
|
|
134
|
+
const child = relative ? `${relative}/${name}` : name;
|
|
135
|
+
if (name.includes('\\') || blockedFilePath(child)) continue;
|
|
136
|
+
if (entry.isFile()) {
|
|
137
|
+
if (name.startsWith('.')) continue;
|
|
138
|
+
const ranked = rank(child, wanted);
|
|
139
|
+
if (ranked) files.push(ranked);
|
|
140
|
+
} else if (entry.isDirectory()) {
|
|
141
|
+
if (IGNORED_DIRECTORIES.has(name) || (name.startsWith('.') && !HIDDEN_DIRECTORIES.has(name))) continue;
|
|
142
|
+
if (depth < LIMITS.depth) directories.push(child);
|
|
143
|
+
}
|
|
144
|
+
// Links, sockets, devices: neither listed nor entered.
|
|
145
|
+
}
|
|
146
|
+
if ((await identity(directory)) !== before) continue;
|
|
147
|
+
} catch {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
levelFiles.push(...files);
|
|
151
|
+
next.push(...directories);
|
|
152
|
+
if (truncated) break;
|
|
153
|
+
}
|
|
154
|
+
// One bounded Git request per breadth-first level, before entering its directories or
|
|
155
|
+
// ranking its files. Git evaluates nested rules and ignored ancestors itself.
|
|
156
|
+
const remaining = Math.min(deadline - now(), realDeadline - Date.now());
|
|
157
|
+
if (remaining <= 0) { truncated = true; break; }
|
|
158
|
+
let ignored: Set<string> | null;
|
|
159
|
+
try {
|
|
160
|
+
ignored = await gitIgnored(root, [...levelFiles.map(file => file.path), ...next], {
|
|
161
|
+
...git, timeoutMs: Math.min(git?.timeoutMs ?? GIT_CHECK.timeoutMs, remaining),
|
|
162
|
+
});
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (!(error instanceof IgnoreUnknown)) throw error;
|
|
165
|
+
// No partial result may reveal paths whose ignore status could not be established.
|
|
166
|
+
return { files: [], truncated: true };
|
|
167
|
+
}
|
|
168
|
+
found.push(...levelFiles.filter(file => !ignored?.has(file.path)));
|
|
169
|
+
level = next.filter(directory => !ignored?.has(directory));
|
|
170
|
+
}
|
|
171
|
+
found.sort(compare);
|
|
172
|
+
return { files: found.slice(0, count).map(entry => ({ path: entry.path })), ...(truncated ? { truncated: true as const } : {}) };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---- workspace.files.list and workspace.files.read (docs/protocol/alp-json-rpc-v1.md §12.4) ----
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* A refusal of workspace.files.list or workspace.files.read: the JSON-RPC code and `data.reason`
|
|
179
|
+
* alpd answers with. Messages are fixed or name only the client's own relative path, never a host
|
|
180
|
+
* path or anything read from a file.
|
|
181
|
+
*/
|
|
182
|
+
export class FileAccessError extends Error {
|
|
183
|
+
constructor(message: string, readonly code: number, readonly reason: string) {
|
|
184
|
+
super(message);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Extra bounds of one listing: entries looked at, and wall time. */
|
|
189
|
+
const LIST_LIMITS = Object.freeze({ scanned: 20_000, ms: 3_000 });
|
|
190
|
+
/** Opening never follows a final link nor waits on a FIFO; Windows has neither flag. */
|
|
191
|
+
const OPEN_FLAGS = process.platform === 'win32' ? constants.O_RDONLY : constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
|
192
|
+
|
|
193
|
+
const notFound = (relative: string) => new FileAccessError(`No readable file or directory at ${relative}`, ERROR.notFound, 'not_found');
|
|
194
|
+
const linked = (relative: string) => new FileAccessError(`${relative} is reached through a link, has more than one name, or changed while it was read; alpd does not follow links`, ERROR.forbidden, 'linked');
|
|
195
|
+
|
|
196
|
+
/** The real path of a workspace root that is a directory, else FilesError. */
|
|
197
|
+
async function realRoot(projectRoot: string): Promise<string> {
|
|
198
|
+
if (!path.isAbsolute(projectRoot)) throw new FilesError('projectRoot must be an absolute path');
|
|
199
|
+
try {
|
|
200
|
+
const root = await realpath(projectRoot);
|
|
201
|
+
if (!(await stat(root)).isDirectory()) throw new Error('not a directory');
|
|
202
|
+
return root;
|
|
203
|
+
} catch {
|
|
204
|
+
throw new FilesError('projectRoot is not a directory alpd can read');
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* A path relative to the workspace as a client may send it, its segments, or a refusal: '/'
|
|
210
|
+
* separated, no leading slash, drive or backslash, no `.`, `..` or empty segment, no NUL, at most
|
|
211
|
+
* WORKSPACE_FILES.maxPathLength characters. A blocked path (blockedFilePath) is refused as such.
|
|
212
|
+
*/
|
|
213
|
+
export function relativeSegments(relative: unknown): string[] {
|
|
214
|
+
if (typeof relative !== 'string' || !relative || [...relative].length > WORKSPACE_FILES.maxPathLength || relative.includes('\0') || relative.includes('\\') || relative.startsWith('/') || /^[A-Za-z]:/.test(relative)) {
|
|
215
|
+
throw new FileAccessError('path must be a relative path inside the workspace, with / between names and no . or .. segment', RPC_ERROR.invalidParams, 'invalid_path');
|
|
216
|
+
}
|
|
217
|
+
const segments = relative.split('/');
|
|
218
|
+
if (segments.some(segment => segment === '' || segment === '.' || segment === '..')) {
|
|
219
|
+
throw new FileAccessError('path must be a relative path inside the workspace, with / between names and no . or .. segment', RPC_ERROR.invalidParams, 'invalid_path');
|
|
220
|
+
}
|
|
221
|
+
if (blockedFilePath(relative)) throw new FileAccessError(`${relative} is not readable remotely: .git, .env files, ALP configuration and key or credential files are always refused`, ERROR.forbidden, 'blocked_path');
|
|
222
|
+
return segments;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The directories from the root to `segments` (the root first), each checked as reached by name:
|
|
227
|
+
* a real directory, not a link, at its own real path. Their identities (device and inode) are
|
|
228
|
+
* returned so the caller can check them again after reading.
|
|
229
|
+
*/
|
|
230
|
+
async function directoryChain(root: string, segments: string[], relative: string): Promise<{ paths: string[]; identities: string[] }> {
|
|
231
|
+
const paths = [root];
|
|
232
|
+
for (let index = 0; index < segments.length; index++) paths.push(path.join(root, ...segments.slice(0, index + 1)));
|
|
233
|
+
const identities: string[] = [];
|
|
234
|
+
for (const directory of paths) identities.push(await directoryIdentity(directory, relative));
|
|
235
|
+
return { paths, identities };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** The identity of a directory reached by name, or the refusal: missing or not a directory, or a link. */
|
|
239
|
+
async function directoryIdentity(directory: string, relative: string): Promise<string> {
|
|
240
|
+
let info;
|
|
241
|
+
try {
|
|
242
|
+
info = await lstat(directory, { bigint: true });
|
|
243
|
+
} catch {
|
|
244
|
+
throw notFound(relative);
|
|
245
|
+
}
|
|
246
|
+
if (info.isSymbolicLink()) throw linked(relative);
|
|
247
|
+
if (!info.isDirectory()) throw notFound(relative);
|
|
248
|
+
let real;
|
|
249
|
+
try {
|
|
250
|
+
real = await realpath(directory);
|
|
251
|
+
} catch {
|
|
252
|
+
throw notFound(relative);
|
|
253
|
+
}
|
|
254
|
+
if (real !== directory) throw linked(relative);
|
|
255
|
+
return `${info.dev}:${info.ino}`;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** The chain checked again: the same directories, else the read is refused. */
|
|
259
|
+
async function sameChain(chain: { paths: string[]; identities: string[] }, relative: string): Promise<void> {
|
|
260
|
+
for (const [index, directory] of chain.paths.entries()) {
|
|
261
|
+
let now;
|
|
262
|
+
try {
|
|
263
|
+
now = await directoryIdentity(directory, relative);
|
|
264
|
+
} catch {
|
|
265
|
+
throw linked(relative);
|
|
266
|
+
}
|
|
267
|
+
if (now !== chain.identities[index]) throw linked(relative);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Which of `paths` .gitignore hides (gitignore.ts, Git's own answer), failing closed: when Git
|
|
273
|
+
* cannot answer, every path counts as ignored. No repository or no Git: none is.
|
|
274
|
+
*/
|
|
275
|
+
async function ignoredOf(root: string, paths: string[], git?: GitCheckOptions): Promise<(path: string) => boolean> {
|
|
276
|
+
try {
|
|
277
|
+
const ignored = await gitIgnored(root, paths, git);
|
|
278
|
+
return ignored ? entry => ignored.has(entry) : () => false;
|
|
279
|
+
} catch (error) {
|
|
280
|
+
if (!(error instanceof IgnoreUnknown)) throw error;
|
|
281
|
+
return () => true;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The bytes of a regular file reached by name, or undefined when it is larger than `maxBytes`;
|
|
287
|
+
* throws when it is missing, not a regular file, a link, has more than one name, or is not the
|
|
288
|
+
* file it was a moment before. The file is opened without following a final link and checked by
|
|
289
|
+
* its handle (same device and inode as by name, before and after).
|
|
290
|
+
*/
|
|
291
|
+
/** Test probes: called between the checks of a read, where a racing writer could change the file. */
|
|
292
|
+
export type FileProbe = (step: 'checked' | 'read' | 'listed') => Promise<void> | void;
|
|
293
|
+
|
|
294
|
+
async function readConfined(file: string, maxBytes: number, probe?: FileProbe): Promise<{ bytes: Buffer; modifiedAt: Date } | undefined> {
|
|
295
|
+
const before = await lstat(file, { bigint: true });
|
|
296
|
+
if (before.isSymbolicLink()) throw Object.assign(new Error('link'), { linked: true });
|
|
297
|
+
if (!before.isFile()) throw new Error('not a file');
|
|
298
|
+
if (before.nlink > 1n) throw Object.assign(new Error('hard link'), { linked: true });
|
|
299
|
+
if ((await realpath(file)) !== file) throw Object.assign(new Error('real path differs'), { linked: true });
|
|
300
|
+
if (before.size > BigInt(maxBytes)) return undefined;
|
|
301
|
+
await probe?.('checked');
|
|
302
|
+
let handle;
|
|
303
|
+
try {
|
|
304
|
+
handle = await open(file, OPEN_FLAGS);
|
|
305
|
+
} catch (error) {
|
|
306
|
+
if ((error as NodeJS.ErrnoException).code === 'ELOOP') throw Object.assign(new Error('link'), { linked: true });
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
const opened = await handle.stat({ bigint: true });
|
|
311
|
+
if (!opened.isFile() || opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink > 1n) throw Object.assign(new Error('changed'), { linked: true });
|
|
312
|
+
// One byte more than allowed tells a file that grew from one that fits.
|
|
313
|
+
const buffer = Buffer.alloc(maxBytes + 1);
|
|
314
|
+
let length = 0;
|
|
315
|
+
while (length < buffer.length) {
|
|
316
|
+
const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
|
|
317
|
+
if (!bytesRead) break;
|
|
318
|
+
length += bytesRead;
|
|
319
|
+
}
|
|
320
|
+
if (length > maxBytes) return undefined;
|
|
321
|
+
await probe?.('read');
|
|
322
|
+
const after = await lstat(file, { bigint: true });
|
|
323
|
+
if (after.dev !== before.dev || after.ino !== before.ino) throw Object.assign(new Error('changed'), { linked: true });
|
|
324
|
+
return { bytes: buffer.subarray(0, length), modifiedAt: opened.mtime };
|
|
325
|
+
} finally {
|
|
326
|
+
await handle.close();
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Paseo's isLikelyBinary (file-explorer/service.ts at b63a325): a NUL byte, or over 30% control characters. */
|
|
331
|
+
function likelyBinary(buffer: Buffer): boolean {
|
|
332
|
+
if (!buffer.length) return false;
|
|
333
|
+
let suspicious = 0;
|
|
334
|
+
for (const byte of buffer) {
|
|
335
|
+
if (byte === 0) return true;
|
|
336
|
+
if ((byte < 32 && byte !== 9 && byte !== 10 && byte !== 13) || byte === 127) suspicious++;
|
|
337
|
+
}
|
|
338
|
+
return suspicious / buffer.length > 0.3;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// The BOM is kept: the content is the file's bytes, decoded.
|
|
342
|
+
/** PEM, OpenSSH and PuTTY private-key headers: a file holding one is refused whatever its name. */
|
|
343
|
+
const PRIVATE_KEY = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----|PuTTY-User-Key-File-\d+:/;
|
|
344
|
+
|
|
345
|
+
const UTF8 = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
346
|
+
|
|
347
|
+
export type ListedEntry = { name: string; path: string; kind: 'file' | 'directory'; size?: number; modifiedAt?: string };
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The entries of a directory under `projectRoot` (its root without `relative`): regular files and
|
|
351
|
+
* directories only (links, sockets, devices are left out), never a blocked one (blockedFilePath)
|
|
352
|
+
* and, unless `includeIgnored`, none .gitignore'd. Directories first, then by name. Each directory
|
|
353
|
+
* on the way is checked (real directory, real path, same device and inode) before and after the
|
|
354
|
+
* read. At most WORKSPACE_FILES.maxListEntries entries; then `truncated`.
|
|
355
|
+
*/
|
|
356
|
+
export async function listWorkspaceDirectory(projectRoot: string, { path: relative, includeIgnored = false, now = Date.now, probe, git }: { path?: string; includeIgnored?: boolean; now?: () => number; probe?: FileProbe; git?: GitCheckOptions } = {}): Promise<{ entries: ListedEntry[]; truncated?: true }> {
|
|
357
|
+
const root = await realRoot(projectRoot);
|
|
358
|
+
const segments = relative === undefined ? [] : relativeSegments(relative);
|
|
359
|
+
const shown = relative ?? '.';
|
|
360
|
+
const chain = await directoryChain(root, segments, shown);
|
|
361
|
+
const directory = chain.paths.at(-1)!;
|
|
362
|
+
const prefix = segments.length ? `${relative}/` : '';
|
|
363
|
+
const deadline = now() + LIST_LIMITS.ms;
|
|
364
|
+
// Regular files and directories that are not blocked, before .gitignore is asked about them.
|
|
365
|
+
const candidates: Array<{ name: string; child: string; kind: 'file' | 'directory' }> = [];
|
|
366
|
+
let scanned = 0;
|
|
367
|
+
let truncated = false;
|
|
368
|
+
try {
|
|
369
|
+
for await (const entry of await opendir(directory)) {
|
|
370
|
+
if (++scanned > LIST_LIMITS.scanned || now() > deadline) { truncated = true; break; }
|
|
371
|
+
const kind = entry.isFile() ? 'file' : entry.isDirectory() ? 'directory' : undefined;
|
|
372
|
+
if (!kind) continue;
|
|
373
|
+
const child = `${prefix}${entry.name}`;
|
|
374
|
+
if (entry.name.includes('\\') || blockedFilePath(child)) continue;
|
|
375
|
+
candidates.push({ name: entry.name, child, kind });
|
|
376
|
+
}
|
|
377
|
+
} catch {
|
|
378
|
+
throw notFound(shown);
|
|
379
|
+
}
|
|
380
|
+
// One Git process for the directory and every candidate: Git also says a path inside an
|
|
381
|
+
// ignored directory is ignored, and a `!` rule can bring one back.
|
|
382
|
+
if (!includeIgnored) {
|
|
383
|
+
const ignored = await ignoredOf(root, [...(segments.length ? [relative!] : []), ...candidates.map(entry => entry.child)], git);
|
|
384
|
+
if (segments.length && ignored(relative!)) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to see it`, ERROR.forbidden, 'ignored');
|
|
385
|
+
for (let index = candidates.length - 1; index >= 0; index--) if (ignored(candidates[index]!.child)) candidates.splice(index, 1);
|
|
386
|
+
}
|
|
387
|
+
const entries: ListedEntry[] = [];
|
|
388
|
+
for (const { name, child, kind } of candidates) {
|
|
389
|
+
if (entries.length >= WORKSPACE_FILES.maxListEntries) { truncated = true; break; }
|
|
390
|
+
let info;
|
|
391
|
+
try {
|
|
392
|
+
info = await lstat(path.join(directory, name));
|
|
393
|
+
} catch {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
// Replaced by something else since it was listed: left out.
|
|
397
|
+
if (kind === 'file' ? !info.isFile() : !info.isDirectory()) continue;
|
|
398
|
+
entries.push({ name, path: child, kind, ...(kind === 'file' ? { size: info.size } : {}), modifiedAt: info.mtime.toISOString() });
|
|
399
|
+
}
|
|
400
|
+
await probe?.('listed');
|
|
401
|
+
await sameChain(chain, shown);
|
|
402
|
+
entries.sort((a, b) => (a.kind === b.kind ? 0 : a.kind === 'directory' ? -1 : 1) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
403
|
+
return { entries, ...(truncated ? { truncated: true as const } : {}) };
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* The content of a UTF-8 text file under `projectRoot`, read-only: refused when it is blocked
|
|
408
|
+
* (blockedFilePath), .gitignore'd (unless `includeIgnored`), missing or not a regular file,
|
|
409
|
+
* reached through a link (any directory on the way or the file), has more than one name (a hard
|
|
410
|
+
* link may share a file from anywhere), changes while it is read, is over
|
|
411
|
+
* WORKSPACE_FILES.maxReadBytes, or is not UTF-8 text.
|
|
412
|
+
*/
|
|
413
|
+
export async function readWorkspaceFile(projectRoot: string, { path: relative, includeIgnored = false, probe, git }: { path: string; includeIgnored?: boolean; probe?: FileProbe; git?: GitCheckOptions }): Promise<{ path: string; content: string; size: number; modifiedAt: string }> {
|
|
414
|
+
const root = await realRoot(projectRoot);
|
|
415
|
+
const segments = relativeSegments(relative);
|
|
416
|
+
const chain = await directoryChain(root, segments.slice(0, -1), relative);
|
|
417
|
+
if (!includeIgnored && (await ignoredOf(root, [relative], git))(relative)) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to read it`, ERROR.forbidden, 'ignored');
|
|
418
|
+
const file = path.join(root, ...segments);
|
|
419
|
+
let read: { bytes: Buffer; modifiedAt: Date } | undefined;
|
|
420
|
+
try {
|
|
421
|
+
read = await readConfined(file, WORKSPACE_FILES.maxReadBytes, probe);
|
|
422
|
+
} catch (error) {
|
|
423
|
+
if ((error as { linked?: boolean }).linked) throw linked(relative);
|
|
424
|
+
throw notFound(relative);
|
|
425
|
+
}
|
|
426
|
+
if (read === undefined) throw new FileAccessError(`${relative} is larger than ${WORKSPACE_FILES.maxReadBytes} bytes`, ERROR.failed, 'too_large');
|
|
427
|
+
await sameChain(chain, relative);
|
|
428
|
+
let content: string;
|
|
429
|
+
try {
|
|
430
|
+
if (likelyBinary(read.bytes)) throw new Error('binary');
|
|
431
|
+
content = UTF8.decode(read.bytes);
|
|
432
|
+
} catch {
|
|
433
|
+
throw new FileAccessError(`${relative} is not a UTF-8 text file`, ERROR.failed, 'binary');
|
|
434
|
+
}
|
|
435
|
+
// A private key under any name (id_work, a service account's JSON) is refused as a blocked path.
|
|
436
|
+
if (PRIVATE_KEY.test(content)) throw new FileAccessError(`${relative} holds a private key; such files are never readable remotely`, ERROR.forbidden, 'blocked_path');
|
|
437
|
+
return { path: relative, content, size: read.bytes.length, modifiedAt: read.modifiedAt.toISOString() };
|
|
438
|
+
}
|