UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

33 lines (32 loc) • 1.3 kB
import { dirname, isAbsolute, sep } from "node:path"; import { isUncSpelling } from "../../core/unc-spelling.js"; import { err, ok } from "../harness/types.js"; export function anchorAtLinkDir(linkDir, target, separator) { const endsInSeparator = linkDir.endsWith(separator) || (separator === "\\" && linkDir.endsWith("/")); return endsInSeparator ? `${linkDir}${target}` : `${linkDir}${separator}${target}`; } export const SYMLINK_CHAIN_MAX_HOPS = 64; export async function walkSymlinkChain(start, readLink) { const hops = []; const seen = new Set([start]); let cur = start; for (let hop = 0; hop < SYMLINK_CHAIN_MAX_HOPS; hop++) { const read = await readLink(cur); if (!read.ok) { const code = read.error.code; if (code === "invalid" || code === "not_found" || code === "not_directory") return ok({ hops }); return err(read.error); } const target = read.value; const next = isAbsolute(target) ? target : anchorAtLinkDir(dirname(cur), target, sep); if (seen.has(next)) break; hops.push(next); if (process.platform === "win32" && isUncSpelling(next)) break; seen.add(next); cur = next; } return ok({ hops }); }