返回源码地图

packages/env/src/list-files.ts

v1.3.0 · 71023aec1546 · 21:能力探测、有界列表、LocalExecutionEnv

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1import type { Context } from "@earendil-works/chord";
2import {
3 type ExecutionEnv,
4 err,
5 FileError,
6 type FileKind,
7 type FileListEntry,
8 type FileListing,
9 type ListFilesOptions,
10 ok,
11 type Result,
12} from "@earendil-works/pi-durable/env";
13import { NodeExecutionEnv } from "@earendil-works/pi-durable/env/node";
14import type { Connection } from "./connection.ts";
15import { abortResult, toFileError } from "./errors.ts";
16
17/** Kind bytes of `listFiles` records: `f`, `d`, `l`. */
18const KINDS: Record<number, FileKind> = { 102: "file", 100: "directory", 108: "symlink" };
19
20/**
21 * `listFiles` of an absolute path through the daemon: fails with `not_supported` if the daemon does not list files
22 * (a release before `listFiles`), so callers fall back to their own walk.
23 */
24export async function listFilesOver(
25 connection: Connection,
26 path: string,
27 options: ListFilesOptions | undefined,
28 context: Context,
29): Promise<Result<FileListing, FileError>> {
30 for (const key of ["maxEntries", "maxBytes", "timeoutMs"] as const) {
31 const value = options?.[key];
32 if (value !== undefined && (!Number.isSafeInteger(value) || value < 0)) {
33 return err(new FileError("invalid", `${key} must be a non-negative safe integer`, path));
34 }
35 }
36 const aborted = abortResult<FileListing>(context.abortSignal, path);
37 if (aborted) return aborted;
38 try {
39 if (!(await connection.info()).features?.includes("listFiles")) {
40 return err(new FileError("not_supported", "The pi-env daemon does not list files; update it", path));
41 }
42 const { json, payload } = await connection.request(
43 "listFiles",
44 // Fields left undefined are not sent; the daemon applies the defaults.
45 {
46 path,
47 hidden: options?.hidden,
48 gitignore: options?.gitignore,
49 exclude: options?.exclude,
50 maxEntries: options?.maxEntries,
51 maxBytes: options?.maxBytes,
52 timeoutMs: options?.timeoutMs,
53 },
54 context.abortSignal ? { signal: context.abortSignal } : {},
55 );
56 const afterAbort = abortResult<FileListing>(context.abortSignal, path);
57 if (afterAbort) return afterAbort;
58 // Records of a kind byte, the UTF-8 path, and a NUL.
59 const entries: FileListEntry[] = [];
60 const bytes = Buffer.from(payload.buffer, payload.byteOffset, payload.length);
61 for (let start = 0; start < bytes.length; ) {
62 const end = bytes.indexOf(0, start);
63 if (end === -1) break;
64 const kind = KINDS[bytes[start]!];
65 if (kind !== undefined) entries.push({ path: bytes.toString("utf8", start + 1, end), kind });
66 start = end + 1;
67 }
68 return ok({ entries, truncated: json.truncated === true });
69 } catch (error) {
70 return err(toFileError(error, path));
71 }
72}
73
74export type LocalExecutionEnvOptions = ConstructorParameters<typeof NodeExecutionEnv>[0] & {
75 /** A connection to this machine's daemon, `new Connection({ command: [localDaemon()] })`, for `listFiles`. */
76 connection: Connection;
77};
78
79/**
80 * `NodeExecutionEnv` with `listFiles` through a pi-env daemon on the same machine, so local and remote environments
81 * list files with the same code. Everything else is `NodeExecutionEnv`'s. The connection belongs to the caller, who
82 * closes it; several environments may share it.
83 */
84export class LocalExecutionEnv extends NodeExecutionEnv implements ExecutionEnv {
85 readonly #connection: Connection;
86
87 constructor(options: LocalExecutionEnvOptions) {
88 const { connection, ...node } = options;
89 super(node);
90 this.#connection = connection;
91 }
92
93 async listFiles(
94 path: string,
95 options: ListFilesOptions | undefined,
96 context: Context,
97 ): Promise<Result<FileListing, FileError>> {
98 const resolved = await this.absolutePath(path, context);
99 if (!resolved.ok) return resolved;
100 return listFilesOver(this.#connection, resolved.value, options, context);
101 }
102}