@anhlp/alp-daemon 0.0.0-stage → 0.0.1-beta.10
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 +14 -2
- package/dist/THIRD-PARTY-NOTICES.txt +456 -0
- package/dist/alp-templates.d.ts +5 -0
- package/dist/alpd.js +64050 -0
- package/dist/attachments.d.ts +187 -0
- package/dist/attachments.js +856 -0
- package/dist/build-manifest.json +150 -0
- package/dist/conversation.d.ts +119 -0
- package/dist/conversation.js +688 -0
- package/dist/disk.d.ts +67 -0
- package/dist/disk.js +152 -0
- package/dist/files.d.ts +262 -0
- package/dist/files.js +674 -0
- package/dist/git.d.ts +98 -0
- package/dist/git.js +1246 -0
- package/dist/gitignore.d.ts +62 -0
- package/dist/gitignore.js +254 -0
- package/dist/history.d.ts +24 -0
- package/dist/history.js +149 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +28 -0
- package/dist/lifecycle.d.ts +101 -0
- package/dist/lifecycle.js +239 -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 +363 -0
- package/dist/model-catalog.d.ts +2 -0
- package/dist/model-catalog.js +44 -0
- package/dist/native-entry.d.ts +2 -0
- package/dist/native-entry.js +3 -0
- package/dist/native-register.d.ts +1 -0
- package/dist/native-register.js +13 -0
- package/dist/native-terminal.d.ts +45 -0
- package/dist/native-terminal.js +201 -0
- package/dist/operations.d.ts +40 -0
- package/dist/operations.js +313 -0
- package/dist/registry.d.ts +89 -0
- package/dist/registry.js +343 -0
- package/dist/rpc-error.d.ts +6 -0
- package/dist/rpc-error.js +11 -0
- package/dist/scheduler.d.ts +96 -0
- package/dist/scheduler.js +298 -0
- package/dist/schedules.d.ts +70 -0
- package/dist/schedules.js +246 -0
- package/dist/server.d.ts +60 -0
- package/dist/server.js +1958 -0
- package/dist/store.d.ts +178 -0
- package/dist/store.js +386 -0
- package/dist/terminal-native/README.md +201 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/dist/terminal-native/terminal-owner.c +389 -0
- package/dist/terminal-screen.d.ts +24 -0
- package/dist/terminal-screen.js +99 -0
- package/dist/terminals.d.ts +203 -0
- package/dist/terminals.js +570 -0
- package/dist/windows-terminal.d.ts +32 -0
- package/dist/windows-terminal.js +79 -0
- package/dist/worktrees.d.ts +65 -0
- package/dist/worktrees.js +318 -0
- package/native/README.md +201 -0
- package/native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/native/prebuilt/darwin-x64/manifest.json +11 -0
- package/native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/native/terminal-owner.c +389 -0
- package/package.json +163 -4
- package/src/alp-templates.d.ts +5 -0
- package/src/attachments.ts +908 -0
- package/src/conversation.ts +857 -0
- package/src/disk.ts +175 -0
- package/src/files.ts +884 -0
- package/src/git.ts +1220 -0
- package/src/gitignore.ts +277 -0
- package/src/history.ts +118 -0
- package/src/index.ts +12 -0
- package/src/lifecycle.ts +283 -0
- package/src/lock.ts +53 -0
- package/src/main.ts +381 -0
- package/src/model-catalog.ts +36 -0
- package/src/native-entry.ts +5 -0
- package/src/native-register.ts +21 -0
- package/src/native-terminal.ts +127 -0
- package/src/operations.ts +329 -0
- package/src/registry.ts +238 -0
- package/src/rpc-error.ts +4 -0
- package/src/scheduler.ts +438 -0
- package/src/schedules.ts +297 -0
- package/src/server.ts +2238 -0
- package/src/store.ts +449 -0
- package/src/terminal-screen.ts +74 -0
- package/src/terminals.ts +653 -0
- package/src/windows-terminal.ts +74 -0
- package/src/worktrees.ts +382 -0
package/dist/files.js
ADDED
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
// packages/daemon/src/files.ts
|
|
2
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
3
|
+
import { constants } from "node:fs";
|
|
4
|
+
import { lstat, open, opendir, realpath, stat } from "node:fs/promises";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { ERROR, RPC_ERROR, WORKSPACE_FILES, blockedFilePath } from "@anhlp/alp-protocol";
|
|
8
|
+
import { gitIgnored, IgnoreUnknown, GIT_CHECK } from "./gitignore.js";
|
|
9
|
+
import { gitChecksRunning, resolveGit, GIT_CHECK as GIT_CHECK2 } from "./gitignore.js";
|
|
10
|
+
var LIMITS = Object.freeze({
|
|
11
|
+
/** Directory levels below the root that are entered (Paseo's DEFAULT_MAX_DEPTH). */
|
|
12
|
+
depth: 12,
|
|
13
|
+
/** Directory entries looked at (Paseo's DEFAULT_MAX_ENTRIES_SCANNED). */
|
|
14
|
+
scanned: 2e4,
|
|
15
|
+
/** Wall time of one search. */
|
|
16
|
+
ms: 3e3,
|
|
17
|
+
/** Results by default and at most (Paseo's DEFAULT_LIMIT, MAX_LIMIT). */
|
|
18
|
+
defaultLimit: 30,
|
|
19
|
+
maxLimit: 100
|
|
20
|
+
});
|
|
21
|
+
var HIDDEN_DIRECTORIES = /* @__PURE__ */ new Set([".agents", ".alp", ".claude", ".codex", ".github", ".opencode", ".paseo", ".vscode"]);
|
|
22
|
+
var IGNORED_DIRECTORIES = /* @__PURE__ */ new Set(["node_modules", "venv", "env", "virtualenv", "dist", "build", "target", "out", "coverage", "vendor", "__pycache__", ".git"]);
|
|
23
|
+
var NONE = Number.MAX_SAFE_INTEGER;
|
|
24
|
+
async function identity(directory) {
|
|
25
|
+
const info = await lstat(directory, { bigint: true });
|
|
26
|
+
if (!info.isDirectory() || await realpath(directory) !== directory) return null;
|
|
27
|
+
return `${info.dev}:${info.ino}`;
|
|
28
|
+
}
|
|
29
|
+
function fuzzy(query, candidate) {
|
|
30
|
+
let at = 0;
|
|
31
|
+
let first = -1;
|
|
32
|
+
let previous = -1;
|
|
33
|
+
let gaps = 0;
|
|
34
|
+
for (let index = 0; index < candidate.length && at < query.length; index++) {
|
|
35
|
+
if (candidate[index] !== query[at]) continue;
|
|
36
|
+
if (first < 0) first = index;
|
|
37
|
+
if (previous >= 0) gaps += index - previous - 1;
|
|
38
|
+
previous = index;
|
|
39
|
+
at++;
|
|
40
|
+
}
|
|
41
|
+
return at === query.length && first >= 0 ? first + gaps : null;
|
|
42
|
+
}
|
|
43
|
+
function rank(relative, query) {
|
|
44
|
+
const lower = relative.toLowerCase();
|
|
45
|
+
const segments = lower.split("/");
|
|
46
|
+
const depth = segments.length;
|
|
47
|
+
if (!query) return { path: relative, tier: 3, segment: NONE, offset: NONE, fuzzy: NONE, depth };
|
|
48
|
+
const offset = lower.indexOf(query);
|
|
49
|
+
if (query.includes("/")) {
|
|
50
|
+
if (offset >= 0) return { path: relative, tier: lower.startsWith(query) ? 1 : 3, segment: NONE, offset, fuzzy: NONE, depth };
|
|
51
|
+
const spread = fuzzy(query, lower);
|
|
52
|
+
return spread === null ? null : { path: relative, tier: 4, segment: NONE, offset: NONE, fuzzy: spread, depth };
|
|
53
|
+
}
|
|
54
|
+
const last = fuzzy(query, segments.at(-1) ?? "");
|
|
55
|
+
const tiers = [(segment) => segment === query, (segment) => segment.startsWith(query), (segment) => segment.includes(query)];
|
|
56
|
+
for (const [tier, test] of tiers.entries()) {
|
|
57
|
+
const segment = segments.findIndex(test);
|
|
58
|
+
if (segment >= 0) return { path: relative, tier, segment, offset: offset >= 0 ? offset : NONE, fuzzy: last ?? NONE, depth };
|
|
59
|
+
}
|
|
60
|
+
if (last !== null) return { path: relative, tier: 4, segment: NONE, offset: NONE, fuzzy: last, depth };
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
var compare = (a, b) => 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);
|
|
64
|
+
var FilesError = class extends Error {
|
|
65
|
+
};
|
|
66
|
+
async function walk(root, options) {
|
|
67
|
+
const { now, git } = options;
|
|
68
|
+
const deadline = now() + options.ms;
|
|
69
|
+
const realDeadline = Date.now() + options.ms;
|
|
70
|
+
const found = [];
|
|
71
|
+
let scanned = 0;
|
|
72
|
+
let truncated = false;
|
|
73
|
+
let repository;
|
|
74
|
+
let level = [""];
|
|
75
|
+
for (let depth = 0; level.length && !truncated; depth++) {
|
|
76
|
+
const levelEntries = [];
|
|
77
|
+
for (const relative of level) {
|
|
78
|
+
if (scanned >= options.scanned || now() > deadline) {
|
|
79
|
+
truncated = true;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
const directory = relative ? path.join(root, ...relative.split("/")) : root;
|
|
83
|
+
const entries = [];
|
|
84
|
+
try {
|
|
85
|
+
const before = await identity(directory);
|
|
86
|
+
if (!before) continue;
|
|
87
|
+
for await (const entry of await opendir(directory)) {
|
|
88
|
+
if (++scanned > options.scanned || now() > deadline) {
|
|
89
|
+
truncated = true;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
const name = entry.name;
|
|
93
|
+
const child = relative ? `${relative}/${name}` : name;
|
|
94
|
+
if (name.includes("\\") || blockedFilePath(child)) continue;
|
|
95
|
+
const kind = entry.isFile() ? "file" : entry.isDirectory() ? "directory" : options.links && entry.isSymbolicLink() ? (await internalTarget(root, path.join(directory, name)))?.kind : void 0;
|
|
96
|
+
if (!kind || !options.keep(child, name, kind, depth)) continue;
|
|
97
|
+
entries.push({ path: child, name, kind, depth, ...entry.isSymbolicLink() ? { link: true } : {} });
|
|
98
|
+
}
|
|
99
|
+
if (await identity(directory) !== before) continue;
|
|
100
|
+
} catch {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
levelEntries.push(...entries);
|
|
104
|
+
if (truncated) break;
|
|
105
|
+
}
|
|
106
|
+
const remaining = Math.min(deadline - now(), realDeadline - Date.now());
|
|
107
|
+
if (remaining <= 0) {
|
|
108
|
+
truncated = true;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
let ignored = null;
|
|
112
|
+
if (repository !== false) {
|
|
113
|
+
const asked = levelEntries.map((entry) => entry.path);
|
|
114
|
+
ignored = await gitIgnored(root, asked, { ...git, timeoutMs: Math.min(git?.timeoutMs ?? GIT_CHECK.timeoutMs, remaining) });
|
|
115
|
+
if (asked.length) repository = ignored !== null;
|
|
116
|
+
}
|
|
117
|
+
const admitted = levelEntries.filter((entry) => !ignored?.has(entry.path) && (options.admit?.(entry, repository === true) ?? true));
|
|
118
|
+
found.push(...admitted);
|
|
119
|
+
level = admitted.filter((entry) => entry.kind === "directory" && !entry.link && depth < options.depth).map((entry) => entry.path);
|
|
120
|
+
}
|
|
121
|
+
return { entries: found, truncated };
|
|
122
|
+
}
|
|
123
|
+
async function searchWorkspaceFiles(projectRoot, { query = "", limit = LIMITS.defaultLimit, now = Date.now, git } = {}) {
|
|
124
|
+
const root = await realRoot(projectRoot);
|
|
125
|
+
const wanted = query.trim().replace(/\\/g, "/").replace(/^\.?\/+/, "").toLowerCase();
|
|
126
|
+
const count = Math.max(1, Math.min(LIMITS.maxLimit, Math.trunc(limit)));
|
|
127
|
+
let walked;
|
|
128
|
+
try {
|
|
129
|
+
walked = await walk(root, {
|
|
130
|
+
depth: LIMITS.depth,
|
|
131
|
+
scanned: LIMITS.scanned,
|
|
132
|
+
ms: LIMITS.ms,
|
|
133
|
+
now,
|
|
134
|
+
git,
|
|
135
|
+
keep: (child, name, kind, depth) => kind === "file" ? !name.startsWith(".") && rank(child, wanted) !== null : !IGNORED_DIRECTORIES.has(name) && (!name.startsWith(".") || HIDDEN_DIRECTORIES.has(name)) && depth < LIMITS.depth
|
|
136
|
+
});
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (!(error instanceof IgnoreUnknown)) throw error;
|
|
139
|
+
return { files: [], truncated: true };
|
|
140
|
+
}
|
|
141
|
+
const found = walked.entries.filter((entry) => entry.kind === "file").map((entry) => rank(entry.path, wanted));
|
|
142
|
+
found.sort(compare);
|
|
143
|
+
return { files: found.slice(0, count).map((entry) => ({ path: entry.path })), ...walked.truncated ? { truncated: true } : {} };
|
|
144
|
+
}
|
|
145
|
+
var FIND_LIMITS = Object.freeze({ depth: 64, scanned: 1e5, ms: 1e4, ttlMs: 6e4, snapshots: 16 });
|
|
146
|
+
var SKIPPED_FOLDERS = /* @__PURE__ */ new Set(["node_modules", "Library", ".git", "dist", "build", ".cache", "target", ".venv", "venv", "__pycache__"]);
|
|
147
|
+
function inOrder(query, candidate) {
|
|
148
|
+
let at = 0;
|
|
149
|
+
for (const char of candidate) if (char === query[at]) at += 1;
|
|
150
|
+
return at >= query.length;
|
|
151
|
+
}
|
|
152
|
+
async function findWorkspaceEntries(projectRoot, { query = "", kinds = ["file"], matchMode = "subsequence", now = Date.now, git } = {}) {
|
|
153
|
+
const root = await realRoot(projectRoot);
|
|
154
|
+
const wanted = query.trim().replace(/^\.\//, "").toLowerCase();
|
|
155
|
+
const matches = (candidate) => !wanted || (matchMode === "suffix" ? candidate === wanted || candidate.endsWith(`/${wanted}`) : inOrder(wanted, candidate));
|
|
156
|
+
let walked;
|
|
157
|
+
try {
|
|
158
|
+
walked = await walk(root, {
|
|
159
|
+
depth: FIND_LIMITS.depth,
|
|
160
|
+
scanned: FIND_LIMITS.scanned,
|
|
161
|
+
ms: FIND_LIMITS.ms,
|
|
162
|
+
now,
|
|
163
|
+
git,
|
|
164
|
+
links: true,
|
|
165
|
+
keep: () => true,
|
|
166
|
+
admit: (entry, repository) => repository || !(entry.name.startsWith(".") || SKIPPED_FOLDERS.has(entry.name))
|
|
167
|
+
});
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (!(error instanceof IgnoreUnknown)) throw error;
|
|
170
|
+
return { entries: [], truncated: true };
|
|
171
|
+
}
|
|
172
|
+
const entries = walked.entries.filter((entry) => kinds.includes(entry.kind) && matches(entry.path.toLowerCase())).map((entry) => ({ path: entry.path, kind: entry.kind })).sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
173
|
+
return { entries, ...walked.truncated ? { truncated: true } : {} };
|
|
174
|
+
}
|
|
175
|
+
var invalidCursor = () => new FileAccessError("cursor is not one alpd gave for these params, or it expired; ask again without it", RPC_ERROR.invalidParams, "invalid_cursor");
|
|
176
|
+
function createFindPages({ now = Date.now, git } = {}) {
|
|
177
|
+
const snapshots = /* @__PURE__ */ new Map();
|
|
178
|
+
return async function page(owner, projectRoot, { query = "", kinds = ["file"], matchMode = "subsequence", limit = WORKSPACE_FILES.maxFindEntries, cursor } = {}) {
|
|
179
|
+
const count = Math.max(1, Math.min(WORKSPACE_FILES.maxFindEntries, Math.trunc(limit)));
|
|
180
|
+
const key = JSON.stringify([projectRoot, query, [...new Set(kinds)].sort(), matchMode]);
|
|
181
|
+
for (const [id2, snapshot2] of snapshots) if (now() - snapshot2.at > FIND_LIMITS.ttlMs) snapshots.delete(id2);
|
|
182
|
+
let id;
|
|
183
|
+
let offset;
|
|
184
|
+
let snapshot;
|
|
185
|
+
if (cursor !== void 0) {
|
|
186
|
+
const named = /^([A-Za-z0-9_-]{22})\.(\d{1,7})$/.exec(cursor);
|
|
187
|
+
snapshot = named ? snapshots.get(named[1]) : void 0;
|
|
188
|
+
if (!named || !snapshot || snapshot.owner !== owner || snapshot.key !== key || Number(named[2]) > snapshot.entries.length) throw invalidCursor();
|
|
189
|
+
[id, offset] = [named[1], Number(named[2])];
|
|
190
|
+
} else {
|
|
191
|
+
const found = await findWorkspaceEntries(projectRoot, { query, kinds, matchMode, now, git });
|
|
192
|
+
snapshot = { owner, key, entries: found.entries, truncated: found.truncated === true, at: now() };
|
|
193
|
+
[id, offset] = [randomBytes(16).toString("base64url"), 0];
|
|
194
|
+
}
|
|
195
|
+
const entries = snapshot.entries.slice(offset, offset + count);
|
|
196
|
+
const end = offset + entries.length;
|
|
197
|
+
if (end < snapshot.entries.length) {
|
|
198
|
+
snapshot.at = now();
|
|
199
|
+
snapshots.set(id, snapshot);
|
|
200
|
+
for (const old of snapshots.keys()) {
|
|
201
|
+
if (snapshots.size <= FIND_LIMITS.snapshots) break;
|
|
202
|
+
snapshots.delete(old);
|
|
203
|
+
}
|
|
204
|
+
} else snapshots.delete(id);
|
|
205
|
+
return { entries, ...end < snapshot.entries.length ? { nextCursor: `${id}.${end}` } : {}, ...snapshot.truncated ? { truncated: true } : {} };
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
var FileAccessError = class extends Error {
|
|
209
|
+
constructor(message, code, reason) {
|
|
210
|
+
super(message);
|
|
211
|
+
this.code = code;
|
|
212
|
+
this.reason = reason;
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
var LIST_LIMITS = Object.freeze({ scanned: 2e4, ms: 3e3 });
|
|
216
|
+
var OPEN_FLAGS = process.platform === "win32" ? constants.O_RDONLY : constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
|
217
|
+
var notFound = (relative) => new FileAccessError(`No readable file or directory at ${relative}`, ERROR.notFound, "not_found");
|
|
218
|
+
var linked = (relative) => 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");
|
|
219
|
+
var outside = (relative) => new FileAccessError(`${relative} is a link that leads outside the workspace; alpd follows only links that stay inside it`, ERROR.forbidden, "outside");
|
|
220
|
+
function insideRoot(root, real) {
|
|
221
|
+
const relative = path.relative(root, real);
|
|
222
|
+
if (!relative || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) return void 0;
|
|
223
|
+
const inside = relative.split(path.sep).join("/");
|
|
224
|
+
return inside.includes("\\") ? void 0 : inside;
|
|
225
|
+
}
|
|
226
|
+
async function internalSegments(root, segments, relative) {
|
|
227
|
+
let real;
|
|
228
|
+
try {
|
|
229
|
+
real = await realpath(path.join(root, ...segments));
|
|
230
|
+
} catch {
|
|
231
|
+
throw notFound(relative);
|
|
232
|
+
}
|
|
233
|
+
let via;
|
|
234
|
+
for (let index = 1; index <= segments.length && via === void 0; index++) {
|
|
235
|
+
if ((await lstat(path.join(root, ...segments.slice(0, index))).catch(() => void 0))?.isSymbolicLink()) via = segments.slice(0, index).join("/");
|
|
236
|
+
}
|
|
237
|
+
if (real === root) return { segments: [], via };
|
|
238
|
+
const inside = insideRoot(root, real);
|
|
239
|
+
if (inside === void 0) throw outside(relative);
|
|
240
|
+
if (blockedFilePath(inside)) throw new FileAccessError(`${relative} leads to a path that is not readable remotely: .git, .env files, ALP configuration and key or credential files are always refused`, ERROR.forbidden, "blocked_path");
|
|
241
|
+
return { segments: inside.split("/"), via };
|
|
242
|
+
}
|
|
243
|
+
async function internalTarget(root, file) {
|
|
244
|
+
try {
|
|
245
|
+
const real = await realpath(file);
|
|
246
|
+
const target = insideRoot(root, real);
|
|
247
|
+
if (target === void 0 || blockedFilePath(target)) return void 0;
|
|
248
|
+
const info = await lstat(real);
|
|
249
|
+
return info.isFile() ? { kind: "file", target } : info.isDirectory() ? { kind: "directory", target } : void 0;
|
|
250
|
+
} catch {
|
|
251
|
+
return void 0;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
async function realRoot(projectRoot) {
|
|
255
|
+
if (!path.isAbsolute(projectRoot)) throw new FilesError("projectRoot must be an absolute path");
|
|
256
|
+
try {
|
|
257
|
+
const root = await realpath(projectRoot);
|
|
258
|
+
if (!(await stat(root)).isDirectory()) throw new Error("not a directory");
|
|
259
|
+
return root;
|
|
260
|
+
} catch {
|
|
261
|
+
throw new FilesError("projectRoot is not a directory alpd can read");
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function relativeSegments(relative) {
|
|
265
|
+
if (typeof relative !== "string" || !relative || [...relative].length > WORKSPACE_FILES.maxPathLength || relative.includes("\0") || relative.includes("\\") || relative.startsWith("/") || /^[A-Za-z]:/.test(relative)) {
|
|
266
|
+
throw new FileAccessError("path must be a relative path inside the workspace, with / between names and no . or .. segment", RPC_ERROR.invalidParams, "invalid_path");
|
|
267
|
+
}
|
|
268
|
+
const segments = relative.split("/");
|
|
269
|
+
if (segments.some((segment) => segment === "" || segment === "." || segment === "..")) {
|
|
270
|
+
throw new FileAccessError("path must be a relative path inside the workspace, with / between names and no . or .. segment", RPC_ERROR.invalidParams, "invalid_path");
|
|
271
|
+
}
|
|
272
|
+
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");
|
|
273
|
+
return segments;
|
|
274
|
+
}
|
|
275
|
+
async function directoryChain(root, segments, relative) {
|
|
276
|
+
const paths = [root];
|
|
277
|
+
for (let index = 0; index < segments.length; index++) paths.push(path.join(root, ...segments.slice(0, index + 1)));
|
|
278
|
+
const identities = [];
|
|
279
|
+
for (const directory of paths) identities.push(await directoryIdentity(directory, relative));
|
|
280
|
+
return { paths, identities };
|
|
281
|
+
}
|
|
282
|
+
async function directoryIdentity(directory, relative) {
|
|
283
|
+
let info;
|
|
284
|
+
try {
|
|
285
|
+
info = await lstat(directory, { bigint: true });
|
|
286
|
+
} catch {
|
|
287
|
+
throw notFound(relative);
|
|
288
|
+
}
|
|
289
|
+
if (info.isSymbolicLink()) throw linked(relative);
|
|
290
|
+
if (!info.isDirectory()) throw notFound(relative);
|
|
291
|
+
let real;
|
|
292
|
+
try {
|
|
293
|
+
real = await realpath(directory);
|
|
294
|
+
} catch {
|
|
295
|
+
throw notFound(relative);
|
|
296
|
+
}
|
|
297
|
+
if (real !== directory) throw linked(relative);
|
|
298
|
+
return `${info.dev}:${info.ino}`;
|
|
299
|
+
}
|
|
300
|
+
async function sameChain(chain, relative) {
|
|
301
|
+
for (const [index, directory] of chain.paths.entries()) {
|
|
302
|
+
let now;
|
|
303
|
+
try {
|
|
304
|
+
now = await directoryIdentity(directory, relative);
|
|
305
|
+
} catch {
|
|
306
|
+
throw linked(relative);
|
|
307
|
+
}
|
|
308
|
+
if (now !== chain.identities[index]) throw linked(relative);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
async function ignoredOf(root, paths, git) {
|
|
312
|
+
try {
|
|
313
|
+
const ignored = await gitIgnored(root, paths, git);
|
|
314
|
+
return ignored ? (entry) => ignored.has(entry) : () => false;
|
|
315
|
+
} catch (error) {
|
|
316
|
+
if (!(error instanceof IgnoreUnknown)) throw error;
|
|
317
|
+
return () => true;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
async function readConfined(file, maxBytes, probe) {
|
|
321
|
+
const confined = await openConfined(file, maxBytes, probe);
|
|
322
|
+
if (!confined) return void 0;
|
|
323
|
+
const { handle, before, opened } = confined;
|
|
324
|
+
try {
|
|
325
|
+
const buffer = Buffer.alloc(maxBytes + 1);
|
|
326
|
+
let length = 0;
|
|
327
|
+
while (length < buffer.length) {
|
|
328
|
+
const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
|
|
329
|
+
if (!bytesRead) break;
|
|
330
|
+
length += bytesRead;
|
|
331
|
+
}
|
|
332
|
+
if (length > maxBytes) return void 0;
|
|
333
|
+
await probe?.("read");
|
|
334
|
+
const after = await lstat(file, { bigint: true });
|
|
335
|
+
if (after.dev !== before.dev || after.ino !== before.ino) throw Object.assign(new Error("changed"), { linked: true });
|
|
336
|
+
return { bytes: buffer.subarray(0, length), modifiedAt: opened.mtime };
|
|
337
|
+
} finally {
|
|
338
|
+
await handle.close();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
async function openConfined(file, maxBytes, probe) {
|
|
342
|
+
const before = await lstat(file, { bigint: true });
|
|
343
|
+
if (before.isSymbolicLink()) throw Object.assign(new Error("link"), { linked: true });
|
|
344
|
+
if (!before.isFile()) throw new Error("not a file");
|
|
345
|
+
if (before.nlink > 1n) throw Object.assign(new Error("hard link"), { linked: true });
|
|
346
|
+
if (await realpath(file) !== file) throw Object.assign(new Error("real path differs"), { linked: true });
|
|
347
|
+
if (before.size > BigInt(maxBytes)) return void 0;
|
|
348
|
+
await probe?.("checked");
|
|
349
|
+
let handle;
|
|
350
|
+
try {
|
|
351
|
+
handle = await open(file, OPEN_FLAGS);
|
|
352
|
+
} catch (error) {
|
|
353
|
+
if (error.code === "ELOOP") throw Object.assign(new Error("link"), { linked: true });
|
|
354
|
+
throw error;
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
const opened = await handle.stat({ bigint: true });
|
|
358
|
+
if (!opened.isFile() || opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink > 1n) throw Object.assign(new Error("changed"), { linked: true });
|
|
359
|
+
return { handle, before, opened };
|
|
360
|
+
} catch (error) {
|
|
361
|
+
await handle.close();
|
|
362
|
+
throw error;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
async function readConfinedFile(root, segments, relative, maxBytes) {
|
|
366
|
+
const chain = await directoryChain(root, segments.slice(0, -1), relative);
|
|
367
|
+
let read;
|
|
368
|
+
try {
|
|
369
|
+
read = await readConfined(path.join(root, ...segments), maxBytes);
|
|
370
|
+
} catch (error) {
|
|
371
|
+
if (error.linked) throw linked(relative);
|
|
372
|
+
throw notFound(relative);
|
|
373
|
+
}
|
|
374
|
+
await sameChain(chain, relative);
|
|
375
|
+
return read === void 0 ? "too_large" : read.bytes;
|
|
376
|
+
}
|
|
377
|
+
function likelyBinary(buffer) {
|
|
378
|
+
if (!buffer.length) return false;
|
|
379
|
+
let suspicious = 0;
|
|
380
|
+
for (const byte of buffer) {
|
|
381
|
+
if (byte === 0) return true;
|
|
382
|
+
if (byte < 32 && byte !== 9 && byte !== 10 && byte !== 13 || byte === 127) suspicious++;
|
|
383
|
+
}
|
|
384
|
+
return suspicious / buffer.length > 0.3;
|
|
385
|
+
}
|
|
386
|
+
var PRIVATE_KEY = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----|PuTTY-User-Key-File-\d+:/;
|
|
387
|
+
var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
388
|
+
var listOrder = (a, b) => (a.kind === b.kind ? 0 : a.kind === "directory" ? -1 : 1) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
389
|
+
var listCursor = (entry) => Buffer.from(JSON.stringify([entry.kind === "directory" ? 0 : 1, entry.name])).toString("base64url");
|
|
390
|
+
function afterCursor(cursor) {
|
|
391
|
+
let value;
|
|
392
|
+
try {
|
|
393
|
+
value = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
|
|
394
|
+
} catch {
|
|
395
|
+
value = void 0;
|
|
396
|
+
}
|
|
397
|
+
if (!Array.isArray(value) || value.length !== 2 || value[0] !== 0 && value[0] !== 1 || typeof value[1] !== "string" || !value[1]) {
|
|
398
|
+
throw new FileAccessError("cursor is not one alpd gave for a listing", RPC_ERROR.invalidParams, "invalid_cursor");
|
|
399
|
+
}
|
|
400
|
+
return { kind: value[0] === 0 ? "directory" : "file", name: value[1] };
|
|
401
|
+
}
|
|
402
|
+
async function listWorkspaceDirectory(projectRoot, { path: relative, includeIgnored = false, cursor, internalLinks = false, now = Date.now, probe, git } = {}) {
|
|
403
|
+
const root = await realRoot(projectRoot);
|
|
404
|
+
const requested = relative === void 0 ? [] : relativeSegments(relative);
|
|
405
|
+
const shown = relative ?? ".";
|
|
406
|
+
const after = cursor === void 0 ? void 0 : afterCursor(cursor);
|
|
407
|
+
const { segments, via } = internalLinks && requested.length ? await internalSegments(root, requested, shown) : { segments: requested, via: void 0 };
|
|
408
|
+
const real = segments.join("/");
|
|
409
|
+
const target = real !== requested.join("/") ? real || "." : void 0;
|
|
410
|
+
const chain = await directoryChain(root, segments, shown);
|
|
411
|
+
const directory = chain.paths.at(-1);
|
|
412
|
+
const prefix = segments.length ? `${real}/` : "";
|
|
413
|
+
const deadline = now() + LIST_LIMITS.ms;
|
|
414
|
+
const candidates = [];
|
|
415
|
+
let scanned = 0;
|
|
416
|
+
let truncated = false;
|
|
417
|
+
try {
|
|
418
|
+
for await (const entry of await opendir(directory)) {
|
|
419
|
+
if (++scanned > LIST_LIMITS.scanned || now() > deadline) {
|
|
420
|
+
truncated = true;
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
const child = `${prefix}${entry.name}`;
|
|
424
|
+
if (entry.name.includes("\\") || blockedFilePath(child)) continue;
|
|
425
|
+
const kind = entry.isFile() ? "file" : entry.isDirectory() ? "directory" : void 0;
|
|
426
|
+
if (kind) candidates.push({ name: entry.name, child, kind });
|
|
427
|
+
else if (internalLinks && entry.isSymbolicLink()) {
|
|
428
|
+
const link = await internalTarget(root, path.join(directory, entry.name));
|
|
429
|
+
if (link) candidates.push({ name: entry.name, child, ...link });
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
} catch {
|
|
433
|
+
throw notFound(shown);
|
|
434
|
+
}
|
|
435
|
+
if (!includeIgnored) {
|
|
436
|
+
const listed = [.../* @__PURE__ */ new Set([...requested.length ? [via ?? relative] : [], ...segments.length ? [real] : []])];
|
|
437
|
+
const ignored = await ignoredOf(root, [...listed, ...candidates.flatMap((entry) => entry.target ? [entry.child, entry.target] : [entry.child])], git);
|
|
438
|
+
if (listed.some(ignored)) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to see it`, ERROR.forbidden, "ignored");
|
|
439
|
+
for (let index = candidates.length - 1; index >= 0; index--) {
|
|
440
|
+
const candidate = candidates[index];
|
|
441
|
+
if (ignored(candidate.child) || candidate.target !== void 0 && ignored(candidate.target)) candidates.splice(index, 1);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
candidates.sort(listOrder);
|
|
445
|
+
const start = after ? candidates.findIndex((candidate) => listOrder(candidate, after) > 0) : 0;
|
|
446
|
+
const entries = [];
|
|
447
|
+
let nextCursor;
|
|
448
|
+
for (const { name, child, kind, target: leadsTo } of start < 0 ? [] : candidates.slice(start)) {
|
|
449
|
+
if (entries.length >= WORKSPACE_FILES.maxListEntries) {
|
|
450
|
+
truncated = true;
|
|
451
|
+
nextCursor = listCursor(entries.at(-1));
|
|
452
|
+
break;
|
|
453
|
+
}
|
|
454
|
+
let info;
|
|
455
|
+
try {
|
|
456
|
+
info = await lstat(leadsTo === void 0 ? path.join(directory, name) : path.join(root, ...leadsTo.split("/")));
|
|
457
|
+
} catch {
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (kind === "file" ? !info.isFile() : !info.isDirectory()) continue;
|
|
461
|
+
entries.push({ name, path: child, kind, ...kind === "file" ? { size: info.size } : {}, modifiedAt: info.mtime.toISOString(), ...leadsTo === void 0 ? {} : { target: leadsTo } });
|
|
462
|
+
}
|
|
463
|
+
await probe?.("listed");
|
|
464
|
+
await sameChain(chain, shown);
|
|
465
|
+
return { entries, ...truncated ? { truncated: true } : {}, ...nextCursor ? { nextCursor } : {}, ...target ? { target } : {} };
|
|
466
|
+
}
|
|
467
|
+
async function readWorkspaceFile(projectRoot, { path: relative, includeIgnored = false, probe, git }) {
|
|
468
|
+
const root = await realRoot(projectRoot);
|
|
469
|
+
const segments = relativeSegments(relative);
|
|
470
|
+
const chain = await directoryChain(root, segments.slice(0, -1), relative);
|
|
471
|
+
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");
|
|
472
|
+
const file = path.join(root, ...segments);
|
|
473
|
+
let read;
|
|
474
|
+
try {
|
|
475
|
+
read = await readConfined(file, WORKSPACE_FILES.maxReadBytes, probe);
|
|
476
|
+
} catch (error) {
|
|
477
|
+
if (error.linked) throw linked(relative);
|
|
478
|
+
throw notFound(relative);
|
|
479
|
+
}
|
|
480
|
+
if (read === void 0) throw new FileAccessError(`${relative} is larger than ${WORKSPACE_FILES.maxReadBytes} bytes`, ERROR.failed, "too_large");
|
|
481
|
+
await sameChain(chain, relative);
|
|
482
|
+
let content;
|
|
483
|
+
try {
|
|
484
|
+
if (likelyBinary(read.bytes)) throw new Error("binary");
|
|
485
|
+
content = UTF8.decode(read.bytes);
|
|
486
|
+
} catch {
|
|
487
|
+
throw new FileAccessError(`${relative} is not a UTF-8 text file`, ERROR.failed, "binary");
|
|
488
|
+
}
|
|
489
|
+
if (PRIVATE_KEY.test(content)) throw new FileAccessError(`${relative} holds a private key; such files are never readable remotely`, ERROR.forbidden, "blocked_path");
|
|
490
|
+
return { path: relative, content, size: read.bytes.length, modifiedAt: read.modifiedAt.toISOString() };
|
|
491
|
+
}
|
|
492
|
+
var PREVIEW_IMAGES = Object.freeze({ ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".svg": "image/svg+xml" });
|
|
493
|
+
var KEY_OVERLAP = 256;
|
|
494
|
+
var CLASSIFY_BLOCK = 1024 * 1024;
|
|
495
|
+
var revisionOf = (info, kind) => `r${createHash("sha256").update(`${info.dev}:${info.ino}:${info.size}:${info.mtimeNs}:${info.ctimeNs}:${kind}`).digest("base64url").slice(0, 22)}`;
|
|
496
|
+
async function classify(handle, size) {
|
|
497
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
498
|
+
const block = Buffer.alloc(Math.max(1, Math.min(CLASSIFY_BLOCK, size)));
|
|
499
|
+
let tail = "";
|
|
500
|
+
let key = false;
|
|
501
|
+
for (let position = 0; position < size; ) {
|
|
502
|
+
const { bytesRead } = await handle.read(block, 0, Math.min(block.length, size - position), position);
|
|
503
|
+
if (!bytesRead) throw Object.assign(new Error("shrank"), { linked: true });
|
|
504
|
+
const bytes = block.subarray(0, bytesRead);
|
|
505
|
+
if (bytes.includes(0)) return "binary";
|
|
506
|
+
if (position === 0) {
|
|
507
|
+
const head = bytes.subarray(0, 16 * 1024);
|
|
508
|
+
let control = 0;
|
|
509
|
+
for (const byte of head) if (byte < 9 || byte > 13 && byte < 32) control++;
|
|
510
|
+
if (control / head.length > 0.3) return "binary";
|
|
511
|
+
}
|
|
512
|
+
try {
|
|
513
|
+
decoder.decode(bytes, { stream: true });
|
|
514
|
+
} catch {
|
|
515
|
+
return "binary";
|
|
516
|
+
}
|
|
517
|
+
const text = tail + bytes.toString("latin1");
|
|
518
|
+
key ||= PRIVATE_KEY.test(text);
|
|
519
|
+
tail = text.slice(-KEY_OVERLAP);
|
|
520
|
+
position += bytesRead;
|
|
521
|
+
}
|
|
522
|
+
try {
|
|
523
|
+
decoder.decode();
|
|
524
|
+
} catch {
|
|
525
|
+
return "binary";
|
|
526
|
+
}
|
|
527
|
+
return key ? "key" : "text";
|
|
528
|
+
}
|
|
529
|
+
async function readRange(handle, from, end) {
|
|
530
|
+
const buffer = Buffer.alloc(end - from);
|
|
531
|
+
let length = 0;
|
|
532
|
+
while (length < buffer.length) {
|
|
533
|
+
const { bytesRead } = await handle.read(buffer, length, buffer.length - length, from + length);
|
|
534
|
+
if (!bytesRead) throw Object.assign(new Error("shrank"), { linked: true });
|
|
535
|
+
length += bytesRead;
|
|
536
|
+
}
|
|
537
|
+
return buffer;
|
|
538
|
+
}
|
|
539
|
+
async function previewWorkspaceFile(projectRoot, { path: relative, includeIgnored = false, internalLinks = false, maxBytes = WORKSPACE_FILES.maxPreviewBytes, offset = 0, revision, probe, git }) {
|
|
540
|
+
const root = await realRoot(projectRoot);
|
|
541
|
+
const requested = relativeSegments(relative);
|
|
542
|
+
const { segments, via } = internalLinks ? await internalSegments(root, requested, relative) : { segments: requested, via: void 0 };
|
|
543
|
+
if (!segments.length) throw notFound(relative);
|
|
544
|
+
const real = segments.join("/");
|
|
545
|
+
const target = real !== relative ? real : void 0;
|
|
546
|
+
const limit = Math.max(1, Math.min(WORKSPACE_FILES.maxPreviewBytes, Math.trunc(maxBytes)));
|
|
547
|
+
const chain = await directoryChain(root, segments.slice(0, -1), relative);
|
|
548
|
+
if (!includeIgnored) {
|
|
549
|
+
const asked = [.../* @__PURE__ */ new Set([via ?? relative, real])];
|
|
550
|
+
if (asked.some(await ignoredOf(root, asked, git))) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to read it`, ERROR.forbidden, "ignored");
|
|
551
|
+
}
|
|
552
|
+
const file = path.join(root, ...segments);
|
|
553
|
+
const extension = path.extname(file).toLowerCase();
|
|
554
|
+
let preview;
|
|
555
|
+
try {
|
|
556
|
+
const confined = await openConfined(file, limit, probe);
|
|
557
|
+
if (!confined) throw new FileAccessError(`${relative} is larger than ${limit} bytes`, ERROR.failed, "too_large");
|
|
558
|
+
const { handle, before, opened } = confined;
|
|
559
|
+
try {
|
|
560
|
+
const size = Number(opened.size);
|
|
561
|
+
if (offset > 0 && revision === void 0) throw new FileAccessError("a chunk past offset 0 needs the revision its first chunk answered", RPC_ERROR.invalidParams, "invalid_params");
|
|
562
|
+
const image = PREVIEW_IMAGES[extension];
|
|
563
|
+
const kind = image ? "image" : offset > 0 ? "text" : await classify(handle, size);
|
|
564
|
+
if (kind === "key") throw new FileAccessError(`${relative} holds a private key; such files are never readable remotely`, ERROR.forbidden, "blocked_path");
|
|
565
|
+
const current = revisionOf(opened, kind);
|
|
566
|
+
if (revision !== void 0 && revision !== current) throw new FileAccessError(`${relative} changed since its first chunk was read; read it again from offset 0`, ERROR.conflict, "changed");
|
|
567
|
+
if (offset > 0 && offset >= size) throw new FileAccessError(`offset is past the end of ${relative}`, RPC_ERROR.invalidParams, "invalid_params");
|
|
568
|
+
const mimeType = image ?? (kind === "binary" ? "application/octet-stream" : extension === ".json" ? "application/json" : "text/plain");
|
|
569
|
+
const modifiedAt = new Date(Number(opened.mtimeNs / 1000000n) + (opened.mtimeNs % 1000000n >= 500000n ? 1 : 0)).toISOString();
|
|
570
|
+
const base = { path: relative, ...target === void 0 ? {} : { target }, kind, mimeType, size, modifiedAt, revision: current };
|
|
571
|
+
if (kind === "binary") preview = { ...base, encoding: "none" };
|
|
572
|
+
else {
|
|
573
|
+
const end = Math.min(size, offset + WORKSPACE_FILES.maxPreviewChunkBytes);
|
|
574
|
+
const from = Math.max(0, offset - KEY_OVERLAP);
|
|
575
|
+
const bytes = await readRange(handle, from, end);
|
|
576
|
+
if (PRIVATE_KEY.test(bytes.toString("latin1"))) throw new FileAccessError(`${relative} holds a private key; such files are never readable remotely`, ERROR.forbidden, "blocked_path");
|
|
577
|
+
preview = { ...base, encoding: "base64", content: bytes.subarray(offset - from).toString("base64"), offset, ...end < size ? { nextOffset: end } : {} };
|
|
578
|
+
}
|
|
579
|
+
await probe?.("read");
|
|
580
|
+
const after = await lstat(file, { bigint: true });
|
|
581
|
+
if (after.dev !== before.dev || after.ino !== before.ino) throw Object.assign(new Error("changed"), { linked: true });
|
|
582
|
+
} finally {
|
|
583
|
+
await handle.close();
|
|
584
|
+
}
|
|
585
|
+
} catch (error) {
|
|
586
|
+
if (error instanceof FileAccessError) throw error;
|
|
587
|
+
if (error.linked) throw linked(relative);
|
|
588
|
+
throw notFound(relative);
|
|
589
|
+
}
|
|
590
|
+
await sameChain(chain, relative);
|
|
591
|
+
return preview;
|
|
592
|
+
}
|
|
593
|
+
var HOST_DIRECTORIES = Object.freeze({ depth: 4, scanned: 5e3, listed: 2e4, defaultLimit: 30, maxLimit: 100 });
|
|
594
|
+
function nameRank(candidate, query) {
|
|
595
|
+
const name = candidate.toLowerCase();
|
|
596
|
+
const wanted = query.toLowerCase();
|
|
597
|
+
if (name === wanted) return 0;
|
|
598
|
+
if (name.startsWith(wanted)) return 1;
|
|
599
|
+
if (name.includes(wanted)) return 2;
|
|
600
|
+
return inOrder(wanted, name) ? 3 : void 0;
|
|
601
|
+
}
|
|
602
|
+
async function subdirectories(directory, max) {
|
|
603
|
+
const names = [];
|
|
604
|
+
let looked = 0;
|
|
605
|
+
try {
|
|
606
|
+
for await (const entry of await opendir(directory)) {
|
|
607
|
+
if (++looked > max) return { names, truncated: true };
|
|
608
|
+
if (entry.isDirectory()) names.push(entry.name);
|
|
609
|
+
}
|
|
610
|
+
} catch {
|
|
611
|
+
}
|
|
612
|
+
return { names, truncated: false };
|
|
613
|
+
}
|
|
614
|
+
async function searchHostDirectories(query, { limit = HOST_DIRECTORIES.defaultLimit, home = os.homedir() } = {}) {
|
|
615
|
+
const typed = typeof query === "string" ? query.trim() : "";
|
|
616
|
+
const count = Math.max(1, Math.min(HOST_DIRECTORIES.maxLimit, Math.trunc(limit)));
|
|
617
|
+
const done = (paths, truncated) => ({ entries: paths.slice(0, count).map((entry) => ({ path: entry })), ...truncated ? { truncated: true } : {} });
|
|
618
|
+
if (!typed) return done([], false);
|
|
619
|
+
if (path.isAbsolute(typed) || typed.startsWith("~")) {
|
|
620
|
+
const expanded = typed === "~" ? home : typed.startsWith("~/") ? path.join(home, typed.slice(2)) : typed;
|
|
621
|
+
if (!path.isAbsolute(expanded)) return done([], false);
|
|
622
|
+
const folder = expanded.endsWith("/") || expanded.endsWith(path.sep);
|
|
623
|
+
const parent = folder ? expanded : path.dirname(expanded);
|
|
624
|
+
const prefix = folder ? "" : path.basename(expanded).toLowerCase();
|
|
625
|
+
const { names, truncated } = await subdirectories(parent, HOST_DIRECTORIES.listed);
|
|
626
|
+
const matching = names.filter((name) => name.toLowerCase().startsWith(prefix) && (!name.startsWith(".") || prefix.startsWith("."))).sort((a, b) => a.localeCompare(b));
|
|
627
|
+
return done(matching.map((name) => path.join(parent, name)), truncated);
|
|
628
|
+
}
|
|
629
|
+
const found = [];
|
|
630
|
+
let queue = [home];
|
|
631
|
+
let scanned = 0;
|
|
632
|
+
for (let depth = 1; depth <= HOST_DIRECTORIES.depth && queue.length && scanned < HOST_DIRECTORIES.scanned; depth += 1) {
|
|
633
|
+
const next = [];
|
|
634
|
+
for (const directory of queue) {
|
|
635
|
+
for (const name of (await subdirectories(directory, HOST_DIRECTORIES.listed)).names) {
|
|
636
|
+
if (name.startsWith(".") || SKIPPED_FOLDERS.has(name)) continue;
|
|
637
|
+
const full = path.join(directory, name);
|
|
638
|
+
scanned += 1;
|
|
639
|
+
const score = nameRank(name, typed);
|
|
640
|
+
if (score !== void 0) found.push({ path: full, score, depth });
|
|
641
|
+
next.push(full);
|
|
642
|
+
if (scanned >= HOST_DIRECTORIES.scanned) break;
|
|
643
|
+
}
|
|
644
|
+
if (scanned >= HOST_DIRECTORIES.scanned) break;
|
|
645
|
+
}
|
|
646
|
+
queue = next;
|
|
647
|
+
}
|
|
648
|
+
found.sort((a, b) => a.score - b.score || a.depth - b.depth || a.path.localeCompare(b.path));
|
|
649
|
+
return done(found.map((entry) => entry.path), scanned >= HOST_DIRECTORIES.scanned);
|
|
650
|
+
}
|
|
651
|
+
export {
|
|
652
|
+
FIND_LIMITS,
|
|
653
|
+
FileAccessError,
|
|
654
|
+
FilesError,
|
|
655
|
+
GIT_CHECK2 as GIT_CHECK,
|
|
656
|
+
HOST_DIRECTORIES,
|
|
657
|
+
LIMITS,
|
|
658
|
+
PREVIEW_IMAGES,
|
|
659
|
+
PRIVATE_KEY,
|
|
660
|
+
createFindPages,
|
|
661
|
+
findWorkspaceEntries,
|
|
662
|
+
gitChecksRunning,
|
|
663
|
+
inOrder,
|
|
664
|
+
likelyBinary,
|
|
665
|
+
listWorkspaceDirectory,
|
|
666
|
+
nameRank,
|
|
667
|
+
previewWorkspaceFile,
|
|
668
|
+
readConfinedFile,
|
|
669
|
+
readWorkspaceFile,
|
|
670
|
+
relativeSegments,
|
|
671
|
+
resolveGit,
|
|
672
|
+
searchHostDirectories,
|
|
673
|
+
searchWorkspaceFiles
|
|
674
|
+
};
|