Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482/** In-memory adapter for the root-discovery filesystem contract.
A tree is declared as files with content, directories, and symbolic links with target text; every ancestor of a declared path is a directory. The probes follow the same rules as the `node:fs/promises` adapter: `exists` and `readSymbolicLink` look at the entry itself (a dangling link exists), while `isDirectory`, `isFile`, and `readTextFile` look through links, resolving relative targets against the link's directory.
Ships in the public entry so marker tests, and callers that hold a tree in memory, can walk it with `findRoot` without touching a disk.
@module */
import { dirname, resolve,} from '#posix-path';import { ABSENT, type RootFilesystem,} from './root-filesystem-contract.ts';
//region Types
/** Tree declaration for {@link createMemoryRootFilesystem}. */export type MemoryTree = { /** Regular files by absolute path, with their text. */ readonly files?: Readonly<Record<string, string>>;
/** Directories by absolute path. Parents of any declared path are directories already; list a directory here when it is empty. */ readonly directories?: readonly string[];
/** Symbolic links by absolute path, with their target text as a link stores it: absolute, or relative to the link's directory. */ readonly links?: Readonly<Record<string, string>>;};
/** Kind of entry found at a normalized path, or absence. */type EntryKind = 'absent' | 'directory' | 'file' | 'link';
/** Normalized lookup tables built once per adapter. */type Tables = { /** File text by normalized path. */ readonly files: ReadonlyMap<string, string>;
/** Normalized paths that are directories, declared or implied. */ readonly directories: ReadonlySet<string>;
/** Link target text by normalized path. */ readonly links: ReadonlyMap<string, string>;};
//endregion Types
//region Tables
/** Normalizes a declared path to an absolute POSIX path without dot segments or a trailing slash (the root stays `/`).
@param path - declared path
@returns normalized absolute path
@example ```ts normalizePath('/repo/a/../mise.toml'); // '/repo/mise.toml' ``` */function normalizePath(path: string,): string { return resolve([path,],);}
/** Every ancestor directory of a normalized path, the root included, the path itself excluded.
@param path - normalized absolute path
@returns ancestors from the nearest parent up to `/`
@example ```ts ancestorsOf('/repo/a/b'); // ['/repo/a', '/repo', '/'] ``` */function ancestorsOf(path: string,): readonly string[] { /** Nearest parent; equal to the path only at the root. */ const parent = dirname(path,); if (parent === path) return []; // Bounded structural walk: one level per call, ending at the root, so // the recursion depth is the path depth. return [ parent, ...ancestorsOf(parent,), ];}
/** Builds the normalized lookup tables from a tree declaration.
@param tree - declared files, directories, and links
@returns tables with every implied ancestor directory added
@example ```ts const tables = buildTables({ files: { '/repo/mise.toml': '' } }); ``` */function buildTables(tree: MemoryTree,): Tables { /** File text by normalized path. */ const files = new Map(Object.entries(tree.files ?? {},) .map(function normalizeFile([path, text,],): readonly [ string, string, ] { return [ normalizePath(path,), text, ]; },),); /** Link target text by normalized path. */ const links = new Map(Object.entries(tree.links ?? {},) .map(function normalizeLink([path, target,],): readonly [ string, string, ] { return [ normalizePath(path,), target, ]; },),); /** Declared directories, normalized. */ const declaredDirectories = (tree.directories ?? []).map(normalizePath,); /** Every declared path, whose ancestors are directories by implication. */ const declaredPaths = [ ...files.keys(), ...links.keys(), ...declaredDirectories, ]; /** Directories: the root, the declared ones, and every ancestor. */ const directories = new Set([ '/', ...declaredDirectories, ...declaredPaths.flatMap(ancestorsOf,), ],); return { directories, files, links, };}
//endregion Tables
//region Probes
/** Kind of the entry at a normalized path, without following links.
@param tables - lookup tables
@param path - normalized absolute path
@returns entry kind, or `absent`
@example ```ts entryKind({ tables, path: '/repo/.git' }); ``` */function entryKind({ tables, path,}: { readonly tables: Tables; readonly path: string;},): EntryKind { if (tables.links .has(path,)) return 'link'; if (tables.files .has(path,)) return 'file'; if (tables.directories .has(path,)) return 'directory'; return 'absent';}
/** Link hops followed before a cycle is treated as dangling; the same budget Linux applies (`ELOOP` after 40). */const MAX_LINK_HOPS = 40;
/** Non-empty segments of a normalized absolute path.
@param path - normalized absolute path
@returns names from the root downward
@example ```ts pathSegments('/repo/.git/HEAD'); // ['repo', '.git', 'HEAD'] ``` */function pathSegments(path: string,): readonly string[] { return path.split('/',) .filter(function nonEmpty(segment,): boolean { return segment !== ''; },);}
/** Joins one more name onto a normalized directory path.
@param dir - normalized directory path
@param name - entry name
@returns normalized child path
@example ```ts childPath({ dir: '/', name: 'repo' }); // '/repo' ``` */function childPath({ dir, name,}: { readonly dir: string; readonly name: string;},): string { return (dir === '/') ? `/${name}` : `${dir}/${name}`;}
/** Path of the entry a normalized path names once every link on the way is followed, the way `stat` resolves it; a dangling link resolves to its target path, which then reads as absent.
@param tables - lookup tables
@param path - normalized absolute path
@param hops - links followed so far, bounding a link cycle
@returns normalized path of the final entry
@example ```ts resolveEntryPath({ tables, path: '/repo/.git/HEAD', hops: 0 }); ``` */function resolveEntryPath({ tables, path, hops,}: { readonly tables: Tables; readonly path: string; readonly hops: number;},): string { return pathSegments(path,) .reduce( function step( current: string, name: string, ): string { /** Path of this segment under the directory resolved so far. */ const candidate = childPath({ dir: current, name, },); /** Link target text when this segment is a link. */ const target = tables.links .get(candidate,); if ((target === undefined) || (hops >= MAX_LINK_HOPS)) return candidate; // Bounded structural walk: each hop moves to one link target, and // the hop budget ends a cycle. return resolveEntryPath({ hops: hops + 1, path: resolve([ current, target, ],), tables, },); }, '/', );}
/** Path a link-aware entry probe (`lstat`) looks at: the parent chain resolved through links, the final name left as it is.
@param tables - lookup tables
@param path - normalized absolute path
@returns path whose parent is resolved and whose leaf is not
@example ```ts resolveParentPath({ tables, path: '/repo/.git/HEAD' }); ``` */function resolveParentPath({ tables, path,}: { readonly tables: Tables; readonly path: string;},): string { /** Final name; empty only for the root. */ const leaf = pathSegments(path,) .at(-1,); if (leaf === undefined) return path; return childPath({ dir: resolveEntryPath({ hops: 0, path: dirname(path,), tables, },), name: leaf, },);}
/** Builds an in-memory root-discovery filesystem from a tree declaration.
@param tree - files with text, directories, and links with target text
@returns filesystem whose probes read only the declaration
@example ```ts const fs = createMemoryRootFilesystem({ directories: ['/repo/.git/objects', '/repo/.git/refs', '/repo/a/b'], files: { '/repo/.git/HEAD': 'ref: refs/heads/main\n' }, }); const root = await findRoot({ cwd: '/repo/a/b', fs, marker: GIT_REPOSITORY }); ``` */export function createMemoryRootFilesystem(tree: MemoryTree = {},): RootFilesystem { /** Lookup tables built once for this adapter. */ const tables = buildTables(tree,);
/** Kind of the entry a path names after following every link.
@param path - declared or probed path
@returns entry kind at the end of the link chain, or `absent`
@example ```ts resolvedKind('/repo/.git/HEAD'); ``` */ function resolvedKind(path: string,): EntryKind { return entryKind({ path: resolveEntryPath({ hops: 0, path: normalizePath(path,), tables, },), tables, },); }
/** Path the entry probes look at: parents resolved, leaf as spelled.
@param path - declared or probed path
@returns normalized path with a link-resolved parent chain
@example ```ts entryPath('/repo/.git/HEAD'); ``` */ function entryPath(path: string,): string { return resolveParentPath({ path: normalizePath(path,), tables, },); }
return { exists: function memoryExists(path: string,): Promise<boolean> { return Promise.resolve(entryKind({ path: entryPath(path,), tables, },) !== 'absent',); },
isDirectory: function memoryIsDirectory(path: string,): Promise<boolean> { return Promise.resolve(resolvedKind(path,) === 'directory',); },
isFile: function memoryIsFile(path: string,): Promise<boolean> { return Promise.resolve(resolvedKind(path,) === 'file',); },
readSymbolicLink: function memoryReadSymbolicLink(path: string,): Promise<string | typeof ABSENT> { /** Link target text, absent for every other entry kind. */ const target = tables.links .get(entryPath(path,),); return Promise.resolve(target ?? ABSENT,); },
readTextFile: function memoryReadTextFile(path: string,): Promise<string | typeof ABSENT> { /** File text at the end of the link chain, absent otherwise. */ const text = tables.files .get(resolveEntryPath({ hops: 0, path: normalizePath(path,), tables, },),); return Promise.resolve(text ?? ABSENT,); }, };}
//endregion Probes