pi-knowledge 0.15.0 → 0.16.1

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 (61) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +61 -23
  3. package/dist/index.d.ts +10 -4
  4. package/dist/index.js +145 -166
  5. package/dist/src/coverage-snapshot.d.ts +23 -0
  6. package/dist/src/coverage-snapshot.js +53 -0
  7. package/dist/src/daemon/client.d.ts +60 -0
  8. package/dist/src/daemon/client.js +360 -0
  9. package/dist/src/daemon/main.d.ts +2 -0
  10. package/dist/src/daemon/main.js +109 -0
  11. package/dist/src/daemon/protocol.d.ts +71 -0
  12. package/dist/src/daemon/protocol.js +170 -0
  13. package/dist/src/daemon/runtime.d.ts +26 -0
  14. package/dist/src/daemon/runtime.js +192 -0
  15. package/dist/src/daemon/server.d.ts +46 -0
  16. package/dist/src/daemon/server.js +500 -0
  17. package/dist/src/daemon/service.d.ts +49 -0
  18. package/dist/src/daemon/service.js +527 -0
  19. package/dist/src/diagnostics/health.d.ts +6 -0
  20. package/dist/src/diagnostics/health.js +135 -2
  21. package/dist/src/engine.d.ts +13 -8
  22. package/dist/src/engine.js +569 -100
  23. package/dist/src/filesystem-access.d.ts +9 -0
  24. package/dist/src/filesystem-access.js +100 -0
  25. package/dist/src/indexer/chunker.d.ts +12 -0
  26. package/dist/src/indexer/chunker.js +307 -9
  27. package/dist/src/mcp/http.d.ts +47 -0
  28. package/dist/src/mcp/http.js +564 -0
  29. package/dist/src/mcp/main.d.ts +2 -0
  30. package/dist/src/mcp/main.js +97 -0
  31. package/dist/src/mcp/protocol.d.ts +81 -0
  32. package/dist/src/mcp/protocol.js +444 -0
  33. package/dist/src/mcp/server.d.ts +42 -0
  34. package/dist/src/mcp/server.js +378 -0
  35. package/dist/src/model-worker-client.js +9 -0
  36. package/dist/src/model-worker-contract.d.ts +3 -0
  37. package/dist/src/model-worker-contract.js +8 -0
  38. package/dist/src/model-worker.js +76 -4
  39. package/dist/src/network/safe-fetch.d.ts +7 -0
  40. package/dist/src/network/safe-fetch.js +199 -0
  41. package/dist/src/node-runtime.d.ts +3 -0
  42. package/dist/src/node-runtime.js +78 -0
  43. package/dist/src/profiles.d.ts +15 -0
  44. package/dist/src/profiles.js +160 -0
  45. package/dist/src/runtime-config.js +5 -6
  46. package/dist/src/secure-files.d.ts +17 -0
  47. package/dist/src/secure-files.js +183 -0
  48. package/dist/src/status.js +12 -7
  49. package/dist/src/storage/sqlite.d.ts +4 -1
  50. package/dist/src/storage/sqlite.js +46 -3
  51. package/dist/src/type-guards.d.ts +1 -0
  52. package/dist/src/type-guards.js +3 -0
  53. package/dist/src/watcher/file-watcher.d.ts +3 -1
  54. package/dist/src/watcher/file-watcher.js +166 -32
  55. package/dist/src/windows-security.d.ts +4 -0
  56. package/dist/src/windows-security.js +67 -0
  57. package/docs/configuration.md +40 -9
  58. package/docs/daemon-profiles.md +302 -0
  59. package/docs/mcp-clients.md +266 -0
  60. package/docs/offline-mode.md +6 -6
  61. package/package.json +28 -2
@@ -0,0 +1,9 @@
1
+ export interface FilesystemAccessPolicy {
2
+ allowedRoots: string[];
3
+ allowNetwork: boolean;
4
+ }
5
+ export declare function authorizeExistingPath(input: string, policy: FilesystemAccessPolicy | undefined): string;
6
+ export declare function authorizeOutputPath(input: string, policy: FilesystemAccessPolicy | undefined): string;
7
+ export declare function assertOpenedPathAuthorized(path: string, device: number, inode: number, policy: FilesystemAccessPolicy | undefined): void;
8
+ export declare function assertOutputPathAuthorized(path: string, parentDevice: number, parentInode: number, policy: FilesystemAccessPolicy | undefined): void;
9
+ export declare function assertNetworkAccess(policy: FilesystemAccessPolicy | undefined): void;
@@ -0,0 +1,100 @@
1
+ import { lstatSync, realpathSync, statSync } from "node:fs";
2
+ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
3
+ import { assertOwnedDirectory } from "./secure-files.js";
4
+ const MAX_ALLOWED_ROOTS = 128;
5
+ const MAX_ALLOWED_ROOT_BYTES = 4_096;
6
+ function isWithin(path, root) {
7
+ const relation = relative(root, path);
8
+ return relation === "" || (relation !== ".." && !relation.startsWith(`..${sep}`) && !isAbsolute(relation));
9
+ }
10
+ function bindRoots(policy) {
11
+ if (policy.allowedRoots.length > MAX_ALLOWED_ROOTS)
12
+ throw new Error("Too many allowed filesystem roots");
13
+ return policy.allowedRoots.map((input) => {
14
+ if (typeof input !== "string" || Buffer.byteLength(input) > MAX_ALLOWED_ROOT_BYTES) {
15
+ throw new Error("Invalid allowed filesystem root");
16
+ }
17
+ const requested = resolve(input);
18
+ const direct = lstatSync(requested);
19
+ if (direct.isSymbolicLink() || !direct.isDirectory()) {
20
+ throw new Error(`Allowed root is not a regular directory: ${requested}`);
21
+ }
22
+ const canonical = realpathSync.native(requested);
23
+ assertOwnedDirectory(canonical);
24
+ const info = statSync(canonical);
25
+ return { path: canonical, device: info.dev, inode: info.ino };
26
+ });
27
+ }
28
+ function assertRootStable(root) {
29
+ const direct = lstatSync(root.path);
30
+ const canonical = realpathSync.native(root.path);
31
+ if (direct.isSymbolicLink() ||
32
+ !direct.isDirectory() ||
33
+ direct.dev !== root.device ||
34
+ direct.ino !== root.inode ||
35
+ canonical !== root.path) {
36
+ throw new Error(`Allowed root changed during the operation: ${root.path}`);
37
+ }
38
+ }
39
+ function selectRoot(path, roots) {
40
+ const root = roots.find((candidate) => isWithin(path, candidate.path));
41
+ if (!root)
42
+ throw new Error(`Path is outside the allowed filesystem roots: ${path}`);
43
+ assertRootStable(root);
44
+ return root;
45
+ }
46
+ export function authorizeExistingPath(input, policy) {
47
+ const requested = resolve(input);
48
+ if (!policy)
49
+ return requested;
50
+ const roots = bindRoots(policy);
51
+ if (roots.length === 0)
52
+ throw new Error("No allowed filesystem root permits local paths");
53
+ const canonical = realpathSync.native(requested);
54
+ const root = selectRoot(canonical, roots);
55
+ const direct = lstatSync(canonical);
56
+ if (direct.isSymbolicLink())
57
+ throw new Error(`Path is not a regular non-symlink path: ${canonical}`);
58
+ assertRootStable(root);
59
+ if (realpathSync.native(canonical) !== canonical)
60
+ throw new Error(`Path changed while authorizing access: ${canonical}`);
61
+ return canonical;
62
+ }
63
+ export function authorizeOutputPath(input, policy) {
64
+ const requested = resolve(input);
65
+ if (!policy)
66
+ return requested;
67
+ const roots = bindRoots(policy);
68
+ if (roots.length === 0)
69
+ throw new Error("No allowed filesystem root permits local paths");
70
+ const parent = realpathSync.native(dirname(requested));
71
+ const root = selectRoot(parent, roots);
72
+ assertRootStable(root);
73
+ if (realpathSync.native(parent) !== parent)
74
+ throw new Error(`Output directory changed while authorizing access: ${parent}`);
75
+ return join(parent, basename(requested));
76
+ }
77
+ export function assertOpenedPathAuthorized(path, device, inode, policy) {
78
+ if (!policy)
79
+ return;
80
+ const canonical = authorizeExistingPath(path, policy);
81
+ const current = lstatSync(canonical);
82
+ if (!current.isFile() || current.dev !== device || current.ino !== inode) {
83
+ throw new Error(`Path changed after opening: ${canonical}`);
84
+ }
85
+ }
86
+ export function assertOutputPathAuthorized(path, parentDevice, parentInode, policy) {
87
+ if (!policy)
88
+ return;
89
+ const canonical = authorizeOutputPath(path, policy);
90
+ if (canonical !== path)
91
+ throw new Error(`Output path changed while authorizing access: ${path}`);
92
+ const current = statSync(dirname(canonical));
93
+ if (current.dev !== parentDevice || current.ino !== parentInode) {
94
+ throw new Error(`Output directory changed while authorizing access: ${dirname(canonical)}`);
95
+ }
96
+ }
97
+ export function assertNetworkAccess(policy) {
98
+ if (policy && !policy.allowNetwork)
99
+ throw new Error("Network access is not allowed for this operation");
100
+ }
@@ -1,8 +1,11 @@
1
+ import { type FilesystemAccessPolicy } from "../filesystem-access.ts";
1
2
  import type { ChunkInsert, KnowledgeSymbolInsert } from "../storage/sqlite.ts";
2
3
  export interface ScanOptions {
3
4
  includeSuggestedText?: boolean;
4
5
  includePaths?: string[];
5
6
  excludePaths?: string[];
7
+ accessPolicy?: FilesystemAccessPolicy;
8
+ timeoutMs?: number;
6
9
  }
7
10
  export interface ScannedFile {
8
11
  path: string;
@@ -15,6 +18,11 @@ export interface ScannableFile {
15
18
  relPath: string;
16
19
  fileType: string;
17
20
  size: number;
21
+ mtimeMs: number;
22
+ ctimeMs: number;
23
+ device: number;
24
+ inode: number;
25
+ rootPath?: string;
18
26
  }
19
27
  export interface SkippedScanEntry {
20
28
  path: string;
@@ -33,7 +41,11 @@ export declare function createSkippedScanStats(): ScanResult["skipped"];
33
41
  export declare function addSkippedScanEntry(skipped: ScanResult["skipped"], entry: SkippedScanEntry): void;
34
42
  export declare function summarizeSkippedScan(skipped: ScanResult["skipped"]): string;
35
43
  export declare function isSupportedDocumentFile(filePath: string): boolean;
44
+ export declare function iterateScannableFileVisits(dirPath: string, skipped?: ScanResult["skipped"], options?: ScanOptions): Generator<ScannableFile | undefined>;
45
+ export declare function resolveDirectoryScanTimeoutMs(env?: NodeJS.ProcessEnv): number;
46
+ export declare function iterateScannableFileVisitsAsync(dirPath: string, skipped?: ScanResult["skipped"], options?: ScanOptions, signal?: AbortSignal): AsyncGenerator<ScannableFile | undefined>;
36
47
  export declare function iterateScannableFiles(dirPath: string, skipped?: ScanResult["skipped"], options?: ScanOptions): Generator<ScannableFile>;
48
+ export declare function iterateScannableFilesAsync(dirPath: string, skipped?: ScanResult["skipped"], options?: ScanOptions, signal?: AbortSignal): AsyncGenerator<ScannableFile>;
37
49
  export declare function iterateScannedFiles(dirPath: string, skipped?: ScanResult["skipped"], options?: ScanOptions): Generator<ScannedFile>;
38
50
  export declare function iterateScannedFilesAsync(dirPath: string, onFile: (file: ScannedFile) => Promise<void> | void, skipped?: ScanResult["skipped"], options?: ScanOptions): Promise<ScanResult["skipped"]>;
39
51
  export declare function walkDir(dirPath: string, options?: ScanOptions): ScannedFile[];
@@ -1,7 +1,9 @@
1
1
  import { createHash } from "node:crypto";
2
- import { closeSync, existsSync, openSync, readdirSync, readFileSync, readSync, statSync } from "node:fs";
3
- import { extname, join, relative, sep } from "node:path";
2
+ import { closeSync, constants, existsSync, openSync, readdirSync, readFileSync, readSync, statSync, } from "node:fs";
3
+ import { lstat as lstatFile, opendir, open as openFile, realpath as realpathFile } from "node:fs/promises";
4
+ import { extname, isAbsolute, join, relative, sep } from "node:path";
4
5
  import ignore from "ignore";
6
+ import { authorizeExistingPath } from "../filesystem-access.js";
5
7
  import { extractSymbols } from "./symbols.js";
6
8
  const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
7
9
  const DOCUMENT_EXTENSIONS = { ".doc": true, ".docx": true, ".pdf": true };
@@ -221,7 +223,7 @@ function directoryCouldContainIncludedPath(relPath, includePaths) {
221
223
  const normalized = normalizeScanPath(relPath);
222
224
  return includePaths.some((includePath) => includePath.startsWith(`${normalized}/`));
223
225
  }
224
- export function* iterateScannableFiles(dirPath, skipped = createSkippedScanStats(), options = {}) {
226
+ export function* iterateScannableFileVisits(dirPath, skipped = createSkippedScanStats(), options = {}) {
225
227
  const ig = buildIgnoreMatcher(dirPath);
226
228
  const includePaths = normalizeScanPaths(options.includePaths);
227
229
  const excludePaths = normalizeScanPaths(options.excludePaths);
@@ -236,6 +238,7 @@ export function* iterateScannableFiles(dirPath, skipped = createSkippedScanStats
236
238
  return;
237
239
  }
238
240
  for (const entry of entries) {
241
+ yield undefined;
239
242
  const fullPath = join(dir, entry.name);
240
243
  const relPath = relative(dirPath, fullPath).split(sep).join("/");
241
244
  const explicitlyIncluded = pathMatches(relPath, includePaths);
@@ -263,28 +266,323 @@ export function* iterateScannableFiles(dirPath, skipped = createSkippedScanStats
263
266
  yield* walk(fullPath);
264
267
  }
265
268
  else if (entry.isFile()) {
266
- let size = 0;
269
+ let info;
267
270
  try {
268
- size = statSync(fullPath).size;
271
+ info = statSync(fullPath);
269
272
  }
270
273
  catch {
271
274
  addSkipped(skipped, { path: relPath, reason: "unreadable" });
272
275
  continue;
273
276
  }
274
- if (size > MAX_FILE_SIZE) {
275
- addSkipped(skipped, { path: relPath, reason: "oversized", size });
277
+ if (info.size > MAX_FILE_SIZE) {
278
+ addSkipped(skipped, { path: relPath, reason: "oversized", size: info.size });
276
279
  continue;
277
280
  }
278
281
  if (isBinaryFile(fullPath)) {
279
- addSkipped(skipped, { path: relPath, reason: "binary", size });
282
+ addSkipped(skipped, { path: relPath, reason: "binary", size: info.size });
280
283
  continue;
281
284
  }
282
- yield { path: fullPath, relPath, fileType: detectFileType(fullPath), size };
285
+ yield {
286
+ path: fullPath,
287
+ relPath,
288
+ fileType: detectFileType(fullPath),
289
+ size: info.size,
290
+ mtimeMs: info.mtimeMs,
291
+ ctimeMs: info.ctimeMs,
292
+ device: info.dev,
293
+ inode: info.ino,
294
+ };
283
295
  }
284
296
  }
285
297
  }
286
298
  yield* walk(dirPath);
287
299
  }
300
+ const MAX_GITIGNORE_BYTES = 1024 * 1024;
301
+ const MAX_SCAN_ENTRIES = 1_000_000;
302
+ const MAX_SCAN_TOTAL_BYTES = 64 * 1024 * 1024 * 1024;
303
+ const MAX_SCAN_DEPTH = 128;
304
+ const DIRECTORY_SCAN_TIMEOUT_ENV = "PI_KNOWLEDGE_SCAN_TIMEOUT_MS";
305
+ export function resolveDirectoryScanTimeoutMs(env = process.env) {
306
+ const raw = env[DIRECTORY_SCAN_TIMEOUT_ENV]?.trim();
307
+ if (!raw)
308
+ return 0;
309
+ if (!/^(0|[1-9]\d*)$/.test(raw)) {
310
+ throw new Error(`${DIRECTORY_SCAN_TIMEOUT_ENV} must be 0 or a positive integer`);
311
+ }
312
+ const timeoutMs = Number(raw);
313
+ if (!Number.isSafeInteger(timeoutMs)) {
314
+ throw new Error(`${DIRECTORY_SCAN_TIMEOUT_ENV} must be a safe integer`);
315
+ }
316
+ return timeoutMs;
317
+ }
318
+ function directoryScanTimeoutMs(options) {
319
+ if (options.timeoutMs === undefined)
320
+ return resolveDirectoryScanTimeoutMs();
321
+ if (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs < 0) {
322
+ throw new Error("Directory scan timeout must be 0 or a positive safe integer");
323
+ }
324
+ return options.timeoutMs;
325
+ }
326
+ function scanPathIsWithin(path, root) {
327
+ const rel = relative(root, path);
328
+ return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
329
+ }
330
+ async function readBoundedScanFile(filePath, maxBytes) {
331
+ let direct;
332
+ try {
333
+ direct = await lstatFile(filePath);
334
+ }
335
+ catch (error) {
336
+ if (error.code === "ENOENT")
337
+ return undefined;
338
+ throw error;
339
+ }
340
+ if (direct.isSymbolicLink() || !direct.isFile()) {
341
+ throw new Error(`Scan configuration is not a regular non-symlink file: ${filePath}`);
342
+ }
343
+ if (direct.size > maxBytes)
344
+ throw new Error(`Scan configuration exceeds ${maxBytes} bytes: ${filePath}`);
345
+ const handle = await openFile(filePath, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
346
+ try {
347
+ const opened = await handle.stat();
348
+ if (!opened.isFile() || opened.dev !== direct.dev || opened.ino !== direct.ino) {
349
+ throw new Error(`Scan configuration changed while opening: ${filePath}`);
350
+ }
351
+ if (opened.size > maxBytes)
352
+ throw new Error(`Scan configuration exceeds ${maxBytes} bytes: ${filePath}`);
353
+ const buffer = Buffer.alloc(opened.size + 1);
354
+ const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
355
+ if (bytesRead > maxBytes)
356
+ throw new Error(`Scan configuration exceeds ${maxBytes} bytes: ${filePath}`);
357
+ return buffer.subarray(0, bytesRead).toString("utf8");
358
+ }
359
+ finally {
360
+ await handle.close();
361
+ }
362
+ }
363
+ async function buildIgnoreMatcherAsync(dirPath) {
364
+ const ig = ignore();
365
+ ig.add(DEFAULT_SUGGESTED_EXCLUDE);
366
+ const gitignore = await readBoundedScanFile(join(dirPath, ".gitignore"), MAX_GITIGNORE_BYTES);
367
+ if (gitignore !== undefined)
368
+ ig.add(gitignore);
369
+ return ig;
370
+ }
371
+ async function isBinaryFileAsync(filePath, expected) {
372
+ if (isSupportedDocumentFile(filePath))
373
+ return false;
374
+ if (BINARY_EXTENSIONS[extname(filePath).toLowerCase()] === true)
375
+ return true;
376
+ let handle;
377
+ try {
378
+ handle = await openFile(filePath, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
379
+ const opened = await handle.stat();
380
+ if (!opened.isFile() || opened.dev !== expected.dev || opened.ino !== expected.ino)
381
+ return true;
382
+ const sample = Buffer.alloc(8_000);
383
+ const { bytesRead } = await handle.read(sample, 0, sample.length, 0);
384
+ return bytesRead > 0 && sample.subarray(0, bytesRead).includes(0x00);
385
+ }
386
+ catch {
387
+ return true;
388
+ }
389
+ finally {
390
+ await handle?.close();
391
+ }
392
+ }
393
+ export async function* iterateScannableFileVisitsAsync(dirPath, skipped = createSkippedScanStats(), options = {}, signal) {
394
+ const timeoutMs = directoryScanTimeoutMs(options);
395
+ const startedAt = timeoutMs === 0 ? 0 : Date.now();
396
+ const authorizedRoot = authorizeExistingPath(dirPath, options.accessPolicy);
397
+ const initialRootInfo = await lstatFile(authorizedRoot);
398
+ if (initialRootInfo.isSymbolicLink() || !initialRootInfo.isDirectory()) {
399
+ throw new Error(`Scan root is not a regular non-symlink directory: ${authorizedRoot}`);
400
+ }
401
+ const root = await realpathFile(authorizedRoot);
402
+ if (authorizeExistingPath(root, options.accessPolicy) !== root) {
403
+ throw new Error(`Scan root changed while it was being authorized: ${authorizedRoot}`);
404
+ }
405
+ const [canonicalRootInfo, currentRootInfo] = await Promise.all([lstatFile(root), lstatFile(authorizedRoot)]);
406
+ if (canonicalRootInfo.isSymbolicLink() ||
407
+ !canonicalRootInfo.isDirectory() ||
408
+ currentRootInfo.isSymbolicLink() ||
409
+ !currentRootInfo.isDirectory() ||
410
+ canonicalRootInfo.dev !== initialRootInfo.dev ||
411
+ canonicalRootInfo.ino !== initialRootInfo.ino ||
412
+ currentRootInfo.dev !== initialRootInfo.dev ||
413
+ currentRootInfo.ino !== initialRootInfo.ino) {
414
+ throw new Error(`Scan root changed while it was being resolved: ${authorizedRoot}`);
415
+ }
416
+ const ig = await buildIgnoreMatcherAsync(root);
417
+ const includePaths = normalizeScanPaths(options.includePaths);
418
+ const excludePaths = normalizeScanPaths(options.excludePaths);
419
+ const includeSuggestedText = options.includeSuggestedText === true;
420
+ let visitedEntries = 0;
421
+ let totalBytes = 0;
422
+ const assertScanActive = () => {
423
+ if (signal?.aborted)
424
+ throw new Error("Cancelled");
425
+ if (timeoutMs > 0 && Date.now() - startedAt > timeoutMs) {
426
+ throw new Error(`Directory scan exceeded the configured ${timeoutMs}ms timeout; increase ${DIRECTORY_SCAN_TIMEOUT_ENV} or set it to 0 to disable the wall-clock limit`);
427
+ }
428
+ };
429
+ async function* walk(dir, depth) {
430
+ assertScanActive();
431
+ if (depth > MAX_SCAN_DEPTH)
432
+ throw new Error(`Directory scan exceeds maximum depth ${MAX_SCAN_DEPTH}`);
433
+ let direct;
434
+ let canonical;
435
+ try {
436
+ direct = await lstatFile(dir);
437
+ canonical = await realpathFile(dir);
438
+ }
439
+ catch {
440
+ addSkipped(skipped, { path: relative(root, dir).split(sep).join("/") || ".", reason: "inaccessible" });
441
+ return;
442
+ }
443
+ if (direct.isSymbolicLink() ||
444
+ !direct.isDirectory() ||
445
+ !scanPathIsWithin(canonical, root) ||
446
+ (depth === 0 && (direct.dev !== initialRootInfo.dev || direct.ino !== initialRootInfo.ino))) {
447
+ addSkipped(skipped, { path: relative(root, dir).split(sep).join("/") || ".", reason: "inaccessible" });
448
+ return;
449
+ }
450
+ let directory;
451
+ try {
452
+ directory = await opendir(canonical);
453
+ }
454
+ catch {
455
+ addSkipped(skipped, { path: relative(root, dir).split(sep).join("/") || ".", reason: "inaccessible" });
456
+ return;
457
+ }
458
+ const assertDirectoryStable = async () => {
459
+ const current = await lstatFile(canonical);
460
+ const currentCanonical = await realpathFile(canonical);
461
+ if (current.isSymbolicLink() ||
462
+ !current.isDirectory() ||
463
+ current.dev !== direct.dev ||
464
+ current.ino !== direct.ino ||
465
+ currentCanonical !== canonical) {
466
+ throw new Error(`Directory changed during scan: ${dir}`);
467
+ }
468
+ };
469
+ try {
470
+ await assertDirectoryStable();
471
+ }
472
+ catch (error) {
473
+ await directory.close();
474
+ throw error;
475
+ }
476
+ for await (const entry of directory) {
477
+ assertScanActive();
478
+ await assertDirectoryStable();
479
+ visitedEntries++;
480
+ if (visitedEntries > MAX_SCAN_ENTRIES) {
481
+ throw new Error(`Directory scan exceeds ${MAX_SCAN_ENTRIES} entries`);
482
+ }
483
+ yield undefined;
484
+ const fullPath = join(canonical, entry.name);
485
+ const relPath = relative(root, fullPath).split(sep).join("/");
486
+ const explicitlyIncluded = pathMatches(relPath, includePaths);
487
+ if (pathMatches(relPath, excludePaths)) {
488
+ addSkipped(skipped, { path: relPath, reason: "suggested_excluded" });
489
+ continue;
490
+ }
491
+ let info;
492
+ try {
493
+ info = await lstatFile(fullPath);
494
+ }
495
+ catch {
496
+ addSkipped(skipped, { path: relPath, reason: "unreadable" });
497
+ continue;
498
+ }
499
+ if (info.isSymbolicLink()) {
500
+ addSkipped(skipped, { path: relPath, reason: "inaccessible" });
501
+ continue;
502
+ }
503
+ let safePath;
504
+ try {
505
+ safePath = await realpathFile(fullPath);
506
+ }
507
+ catch {
508
+ addSkipped(skipped, { path: relPath, reason: "inaccessible" });
509
+ continue;
510
+ }
511
+ if (!scanPathIsWithin(safePath, root)) {
512
+ addSkipped(skipped, { path: relPath, reason: "inaccessible" });
513
+ continue;
514
+ }
515
+ if (ig.ignores(relPath) && !includeSuggestedText && !explicitlyIncluded) {
516
+ if (info.isDirectory() && directoryCouldContainIncludedPath(relPath, includePaths)) {
517
+ yield* walk(safePath, depth + 1);
518
+ continue;
519
+ }
520
+ addSkipped(skipped, { path: relPath, reason: "suggested_excluded" });
521
+ continue;
522
+ }
523
+ if (info.isDirectory()) {
524
+ if (ig.ignores(`${relPath}/`) && !includeSuggestedText && !explicitlyIncluded) {
525
+ if (directoryCouldContainIncludedPath(relPath, includePaths)) {
526
+ yield* walk(safePath, depth + 1);
527
+ continue;
528
+ }
529
+ addSkipped(skipped, { path: `${relPath}/`, reason: "suggested_excluded" });
530
+ continue;
531
+ }
532
+ yield* walk(safePath, depth + 1);
533
+ }
534
+ else if (info.isFile()) {
535
+ totalBytes += info.size;
536
+ if (totalBytes > MAX_SCAN_TOTAL_BYTES) {
537
+ throw new Error(`Directory scan exceeds ${MAX_SCAN_TOTAL_BYTES} total bytes`);
538
+ }
539
+ if (info.size > MAX_FILE_SIZE) {
540
+ addSkipped(skipped, { path: relPath, reason: "oversized", size: info.size });
541
+ continue;
542
+ }
543
+ if (await isBinaryFileAsync(safePath, info)) {
544
+ addSkipped(skipped, { path: relPath, reason: "binary", size: info.size });
545
+ continue;
546
+ }
547
+ await assertDirectoryStable();
548
+ const [currentFile, currentPath] = await Promise.all([lstatFile(safePath), realpathFile(safePath)]);
549
+ if (currentFile.isSymbolicLink() ||
550
+ !currentFile.isFile() ||
551
+ currentFile.dev !== info.dev ||
552
+ currentFile.ino !== info.ino ||
553
+ currentPath !== safePath ||
554
+ !scanPathIsWithin(currentPath, root)) {
555
+ addSkipped(skipped, { path: relPath, reason: "inaccessible", size: info.size });
556
+ continue;
557
+ }
558
+ yield {
559
+ path: safePath,
560
+ relPath,
561
+ fileType: detectFileType(safePath),
562
+ size: info.size,
563
+ mtimeMs: info.mtimeMs,
564
+ ctimeMs: info.ctimeMs,
565
+ device: info.dev,
566
+ inode: info.ino,
567
+ rootPath: root,
568
+ };
569
+ }
570
+ }
571
+ }
572
+ yield* walk(root, 0);
573
+ }
574
+ export function* iterateScannableFiles(dirPath, skipped = createSkippedScanStats(), options = {}) {
575
+ for (const file of iterateScannableFileVisits(dirPath, skipped, options)) {
576
+ if (file)
577
+ yield file;
578
+ }
579
+ }
580
+ export async function* iterateScannableFilesAsync(dirPath, skipped = createSkippedScanStats(), options = {}, signal) {
581
+ for await (const file of iterateScannableFileVisitsAsync(dirPath, skipped, options, signal)) {
582
+ if (file)
583
+ yield file;
584
+ }
585
+ }
288
586
  export function* iterateScannedFiles(dirPath, skipped = createSkippedScanStats(), options = {}) {
289
587
  for (const file of iterateScannableFiles(dirPath, skipped, options)) {
290
588
  try {
@@ -0,0 +1,47 @@
1
+ import { type McpServerOptions } from "./protocol.ts";
2
+ export interface McpHttpServerOptions extends McpServerOptions {
3
+ host: string;
4
+ port: number;
5
+ endpoint: string;
6
+ allowedOrigins: string[];
7
+ authToken: string;
8
+ }
9
+ export interface McpHttpAddress {
10
+ host: string;
11
+ port: number;
12
+ endpoint: string;
13
+ url: string;
14
+ }
15
+ export declare class McpHttpServer {
16
+ private readonly options;
17
+ private readonly server;
18
+ private readonly sessions;
19
+ private readonly requestSessions;
20
+ private readonly operations;
21
+ private activeRequests;
22
+ private accepting;
23
+ private started;
24
+ private closePromise;
25
+ private startWaiter;
26
+ private addressValue;
27
+ private readonly closedPromise;
28
+ private readonly resolveClosed;
29
+ private readonly rejectClosed;
30
+ constructor(options: McpHttpServerOptions);
31
+ get address(): McpHttpAddress | undefined;
32
+ private serverOptions;
33
+ private pruneSessions;
34
+ private validateOrigin;
35
+ private validateCommonPostHeaders;
36
+ private validateModernHeaders;
37
+ private validateLegacyVersion;
38
+ private executeProtocolRequest;
39
+ private initializeLegacy;
40
+ private handlePost;
41
+ private deleteLegacySession;
42
+ private handleHttpRequest;
43
+ start(): Promise<McpHttpAddress>;
44
+ waitUntilClosed(): Promise<void>;
45
+ private closeInternal;
46
+ close(): Promise<void>;
47
+ }