@anhlp/alp-daemon 0.0.1-beta.3 → 0.0.1-beta.5

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.
Files changed (95) hide show
  1. package/README.md +4 -0
  2. package/dist/THIRD-PARTY-NOTICES.txt +26 -1
  3. package/dist/alpd.js +29928 -14898
  4. package/dist/attachments.d.ts +187 -0
  5. package/dist/attachments.js +856 -0
  6. package/dist/build-manifest.json +34 -1
  7. package/dist/conversation.d.ts +119 -0
  8. package/dist/conversation.js +688 -0
  9. package/dist/disk.d.ts +67 -0
  10. package/dist/disk.js +152 -0
  11. package/dist/files.d.ts +154 -2
  12. package/dist/files.js +392 -68
  13. package/dist/git.d.ts +93 -0
  14. package/dist/git.js +1192 -0
  15. package/dist/gitignore.d.ts +5 -0
  16. package/dist/gitignore.js +7 -1
  17. package/dist/history.d.ts +24 -0
  18. package/dist/history.js +149 -0
  19. package/dist/index.d.ts +7 -0
  20. package/dist/index.js +21 -1
  21. package/dist/lifecycle.d.ts +101 -0
  22. package/dist/lifecycle.js +239 -0
  23. package/dist/main.js +43 -11
  24. package/dist/native-terminal.d.ts +45 -0
  25. package/dist/native-terminal.js +198 -0
  26. package/dist/operations.d.ts +8 -2
  27. package/dist/operations.js +20 -10
  28. package/dist/registry.d.ts +89 -0
  29. package/dist/registry.js +343 -0
  30. package/dist/scheduler.d.ts +96 -0
  31. package/dist/scheduler.js +296 -0
  32. package/dist/schedules.d.ts +70 -0
  33. package/dist/schedules.js +246 -0
  34. package/dist/server.d.ts +14 -2
  35. package/dist/server.js +836 -90
  36. package/dist/store.d.ts +72 -2
  37. package/dist/store.js +111 -4
  38. package/dist/terminal-native/README.md +201 -0
  39. package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
  40. package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
  41. package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
  42. package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
  43. package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
  44. package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
  45. package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
  46. package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
  47. package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
  48. package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
  49. package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
  50. package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
  51. package/dist/terminal-native/terminal-owner.c +389 -0
  52. package/dist/terminal-screen.d.ts +24 -0
  53. package/dist/terminal-screen.js +99 -0
  54. package/dist/terminals.d.ts +193 -0
  55. package/dist/terminals.js +570 -0
  56. package/dist/windows-terminal.d.ts +29 -0
  57. package/dist/windows-terminal.js +122 -0
  58. package/dist/worktrees.d.ts +65 -0
  59. package/dist/worktrees.js +318 -0
  60. package/native/README.md +201 -0
  61. package/native/prebuilt/darwin-arm64/manifest.json +11 -0
  62. package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
  63. package/native/prebuilt/darwin-x64/manifest.json +11 -0
  64. package/native/prebuilt/darwin-x64/terminal-owner +0 -0
  65. package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
  66. package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
  67. package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
  68. package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
  69. package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
  70. package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
  71. package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
  72. package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
  73. package/native/terminal-owner.c +389 -0
  74. package/package.json +70 -7
  75. package/src/attachments.ts +908 -0
  76. package/src/conversation.ts +857 -0
  77. package/src/disk.ts +175 -0
  78. package/src/files.ts +528 -82
  79. package/src/git.ts +1140 -0
  80. package/src/gitignore.ts +10 -0
  81. package/src/history.ts +118 -0
  82. package/src/index.ts +7 -0
  83. package/src/lifecycle.ts +283 -0
  84. package/src/main.ts +37 -12
  85. package/src/native-terminal.ts +122 -0
  86. package/src/operations.ts +27 -11
  87. package/src/registry.ts +238 -0
  88. package/src/scheduler.ts +432 -0
  89. package/src/schedules.ts +297 -0
  90. package/src/server.ts +948 -102
  91. package/src/store.ts +117 -4
  92. package/src/terminal-screen.ts +74 -0
  93. package/src/terminals.ts +641 -0
  94. package/src/windows-terminal.ts +123 -0
  95. package/src/worktrees.ts +382 -0
package/dist/files.js CHANGED
@@ -1,6 +1,8 @@
1
1
  // packages/daemon/src/files.ts
2
+ import { createHash, randomBytes } from "node:crypto";
2
3
  import { constants } from "node:fs";
3
4
  import { lstat, open, opendir, realpath, stat } from "node:fs/promises";
5
+ import os from "node:os";
4
6
  import path from "node:path";
5
7
  import { ERROR, RPC_ERROR, WORKSPACE_FILES, blockedFilePath } from "@anhlp/alp-protocol";
6
8
  import { gitIgnored, IgnoreUnknown, GIT_CHECK } from "./gitignore.js";
@@ -61,60 +63,44 @@ function rank(relative, query) {
61
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);
62
64
  var FilesError = class extends Error {
63
65
  };
64
- async function searchWorkspaceFiles(projectRoot, { query = "", limit = LIMITS.defaultLimit, now = Date.now, git } = {}) {
65
- if (!path.isAbsolute(projectRoot)) throw new FilesError("projectRoot must be an absolute path");
66
- let root;
67
- try {
68
- root = await realpath(projectRoot);
69
- if (!(await stat(root)).isDirectory()) throw new Error("not a directory");
70
- } catch {
71
- throw new FilesError("projectRoot is not a directory alpd can read");
72
- }
73
- const wanted = query.trim().replace(/\\/g, "/").replace(/^\.?\/+/, "").toLowerCase();
74
- const count = Math.max(1, Math.min(LIMITS.maxLimit, Math.trunc(limit)));
75
- const deadline = now() + LIMITS.ms;
76
- const realDeadline = Date.now() + LIMITS.ms;
66
+ async function walk(root, options) {
67
+ const { now, git } = options;
68
+ const deadline = now() + options.ms;
69
+ const realDeadline = Date.now() + options.ms;
77
70
  const found = [];
78
71
  let scanned = 0;
79
72
  let truncated = false;
73
+ let repository;
80
74
  let level = [""];
81
75
  for (let depth = 0; level.length && !truncated; depth++) {
82
- const next = [];
83
- const levelFiles = [];
76
+ const levelEntries = [];
84
77
  for (const relative of level) {
85
- if (scanned >= LIMITS.scanned || now() > deadline) {
78
+ if (scanned >= options.scanned || now() > deadline) {
86
79
  truncated = true;
87
80
  break;
88
81
  }
89
82
  const directory = relative ? path.join(root, ...relative.split("/")) : root;
90
- const files = [];
91
- const directories = [];
83
+ const entries = [];
92
84
  try {
93
85
  const before = await identity(directory);
94
86
  if (!before) continue;
95
87
  for await (const entry of await opendir(directory)) {
96
- if (++scanned > LIMITS.scanned || now() > deadline) {
88
+ if (++scanned > options.scanned || now() > deadline) {
97
89
  truncated = true;
98
90
  break;
99
91
  }
100
92
  const name = entry.name;
101
93
  const child = relative ? `${relative}/${name}` : name;
102
94
  if (name.includes("\\") || blockedFilePath(child)) continue;
103
- if (entry.isFile()) {
104
- if (name.startsWith(".")) continue;
105
- const ranked = rank(child, wanted);
106
- if (ranked) files.push(ranked);
107
- } else if (entry.isDirectory()) {
108
- if (IGNORED_DIRECTORIES.has(name) || name.startsWith(".") && !HIDDEN_DIRECTORIES.has(name)) continue;
109
- if (depth < LIMITS.depth) directories.push(child);
110
- }
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 } : {} });
111
98
  }
112
99
  if (await identity(directory) !== before) continue;
113
100
  } catch {
114
101
  continue;
115
102
  }
116
- levelFiles.push(...files);
117
- next.push(...directories);
103
+ levelEntries.push(...entries);
118
104
  if (truncated) break;
119
105
  }
120
106
  const remaining = Math.min(deadline - now(), realDeadline - Date.now());
@@ -122,21 +108,102 @@ async function searchWorkspaceFiles(projectRoot, { query = "", limit = LIMITS.de
122
108
  truncated = true;
123
109
  break;
124
110
  }
125
- let ignored;
126
- try {
127
- ignored = await gitIgnored(root, [...levelFiles.map((file) => file.path), ...next], {
128
- ...git,
129
- timeoutMs: Math.min(git?.timeoutMs ?? GIT_CHECK.timeoutMs, remaining)
130
- });
131
- } catch (error) {
132
- if (!(error instanceof IgnoreUnknown)) throw error;
133
- return { files: [], truncated: true };
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;
134
116
  }
135
- found.push(...levelFiles.filter((file) => !ignored?.has(file.path)));
136
- level = next.filter((directory) => !ignored?.has(directory));
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 };
137
140
  }
141
+ const found = walked.entries.filter((entry) => entry.kind === "file").map((entry) => rank(entry.path, wanted));
138
142
  found.sort(compare);
139
- return { files: found.slice(0, count).map((entry) => ({ path: entry.path })), ...truncated ? { truncated: true } : {} };
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
+ };
140
207
  }
141
208
  var FileAccessError = class extends Error {
142
209
  constructor(message, code, reason) {
@@ -149,6 +216,41 @@ var LIST_LIMITS = Object.freeze({ scanned: 2e4, ms: 3e3 });
149
216
  var OPEN_FLAGS = process.platform === "win32" ? constants.O_RDONLY : constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK;
150
217
  var notFound = (relative) => new FileAccessError(`No readable file or directory at ${relative}`, ERROR.notFound, "not_found");
151
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
+ }
152
254
  async function realRoot(projectRoot) {
153
255
  if (!path.isAbsolute(projectRoot)) throw new FilesError("projectRoot must be an absolute path");
154
256
  try {
@@ -216,6 +318,27 @@ async function ignoredOf(root, paths, git) {
216
318
  }
217
319
  }
218
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) {
219
342
  const before = await lstat(file, { bigint: true });
220
343
  if (before.isSymbolicLink()) throw Object.assign(new Error("link"), { linked: true });
221
344
  if (!before.isFile()) throw new Error("not a file");
@@ -233,22 +356,24 @@ async function readConfined(file, maxBytes, probe) {
233
356
  try {
234
357
  const opened = await handle.stat({ bigint: true });
235
358
  if (!opened.isFile() || opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink > 1n) throw Object.assign(new Error("changed"), { linked: true });
236
- const buffer = Buffer.alloc(maxBytes + 1);
237
- let length = 0;
238
- while (length < buffer.length) {
239
- const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
240
- if (!bytesRead) break;
241
- length += bytesRead;
242
- }
243
- if (length > maxBytes) return void 0;
244
- await probe?.("read");
245
- const after = await lstat(file, { bigint: true });
246
- if (after.dev !== before.dev || after.ino !== before.ino) throw Object.assign(new Error("changed"), { linked: true });
247
- return { bytes: buffer.subarray(0, length), modifiedAt: opened.mtime };
248
- } finally {
359
+ return { handle, before, opened };
360
+ } catch (error) {
249
361
  await handle.close();
362
+ throw error;
250
363
  }
251
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
+ }
252
377
  function likelyBinary(buffer) {
253
378
  if (!buffer.length) return false;
254
379
  let suspicious = 0;
@@ -260,13 +385,31 @@ function likelyBinary(buffer) {
260
385
  }
261
386
  var PRIVATE_KEY = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----|PuTTY-User-Key-File-\d+:/;
262
387
  var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
263
- async function listWorkspaceDirectory(projectRoot, { path: relative, includeIgnored = false, now = Date.now, probe, git } = {}) {
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 } = {}) {
264
403
  const root = await realRoot(projectRoot);
265
- const segments = relative === void 0 ? [] : relativeSegments(relative);
404
+ const requested = relative === void 0 ? [] : relativeSegments(relative);
266
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;
267
410
  const chain = await directoryChain(root, segments, shown);
268
411
  const directory = chain.paths.at(-1);
269
- const prefix = segments.length ? `${relative}/` : "";
412
+ const prefix = segments.length ? `${real}/` : "";
270
413
  const deadline = now() + LIST_LIMITS.ms;
271
414
  const candidates = [];
272
415
  let scanned = 0;
@@ -277,39 +420,49 @@ async function listWorkspaceDirectory(projectRoot, { path: relative, includeIgno
277
420
  truncated = true;
278
421
  break;
279
422
  }
280
- const kind = entry.isFile() ? "file" : entry.isDirectory() ? "directory" : void 0;
281
- if (!kind) continue;
282
423
  const child = `${prefix}${entry.name}`;
283
424
  if (entry.name.includes("\\") || blockedFilePath(child)) continue;
284
- candidates.push({ name: entry.name, child, kind });
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
+ }
285
431
  }
286
432
  } catch {
287
433
  throw notFound(shown);
288
434
  }
289
435
  if (!includeIgnored) {
290
- const ignored = await ignoredOf(root, [...segments.length ? [relative] : [], ...candidates.map((entry) => entry.child)], git);
291
- if (segments.length && ignored(relative)) throw new FileAccessError(`${relative} is ignored by .gitignore; ask with includeIgnored to see it`, ERROR.forbidden, "ignored");
292
- for (let index = candidates.length - 1; index >= 0; index--) if (ignored(candidates[index].child)) candidates.splice(index, 1);
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
+ }
293
443
  }
444
+ candidates.sort(listOrder);
445
+ const start = after ? candidates.findIndex((candidate) => listOrder(candidate, after) > 0) : 0;
294
446
  const entries = [];
295
- for (const { name, child, kind } of candidates) {
447
+ let nextCursor;
448
+ for (const { name, child, kind, target: leadsTo } of start < 0 ? [] : candidates.slice(start)) {
296
449
  if (entries.length >= WORKSPACE_FILES.maxListEntries) {
297
450
  truncated = true;
451
+ nextCursor = listCursor(entries.at(-1));
298
452
  break;
299
453
  }
300
454
  let info;
301
455
  try {
302
- info = await lstat(path.join(directory, name));
456
+ info = await lstat(leadsTo === void 0 ? path.join(directory, name) : path.join(root, ...leadsTo.split("/")));
303
457
  } catch {
304
458
  continue;
305
459
  }
306
460
  if (kind === "file" ? !info.isFile() : !info.isDirectory()) continue;
307
- entries.push({ name, path: child, kind, ...kind === "file" ? { size: info.size } : {}, modifiedAt: info.mtime.toISOString() });
461
+ entries.push({ name, path: child, kind, ...kind === "file" ? { size: info.size } : {}, modifiedAt: info.mtime.toISOString(), ...leadsTo === void 0 ? {} : { target: leadsTo } });
308
462
  }
309
463
  await probe?.("listed");
310
464
  await sameChain(chain, shown);
311
- 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));
312
- return { entries, ...truncated ? { truncated: true } : {} };
465
+ return { entries, ...truncated ? { truncated: true } : {}, ...nextCursor ? { nextCursor } : {}, ...target ? { target } : {} };
313
466
  }
314
467
  async function readWorkspaceFile(projectRoot, { path: relative, includeIgnored = false, probe, git }) {
315
468
  const root = await realRoot(projectRoot);
@@ -336,15 +489,186 @@ async function readWorkspaceFile(projectRoot, { path: relative, includeIgnored =
336
489
  if (PRIVATE_KEY.test(content)) throw new FileAccessError(`${relative} holds a private key; such files are never readable remotely`, ERROR.forbidden, "blocked_path");
337
490
  return { path: relative, content, size: read.bytes.length, modifiedAt: read.modifiedAt.toISOString() };
338
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
+ }
339
651
  export {
652
+ FIND_LIMITS,
340
653
  FileAccessError,
341
654
  FilesError,
342
655
  GIT_CHECK2 as GIT_CHECK,
656
+ HOST_DIRECTORIES,
343
657
  LIMITS,
658
+ PREVIEW_IMAGES,
659
+ PRIVATE_KEY,
660
+ createFindPages,
661
+ findWorkspaceEntries,
344
662
  gitChecksRunning,
663
+ inOrder,
664
+ likelyBinary,
345
665
  listWorkspaceDirectory,
666
+ nameRank,
667
+ previewWorkspaceFile,
668
+ readConfinedFile,
346
669
  readWorkspaceFile,
347
670
  relativeSegments,
348
671
  resolveGit,
672
+ searchHostDirectories,
349
673
  searchWorkspaceFiles
350
674
  };