UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

68 lines • 2.95 kB
import { lstatSync, readlinkSync, realpathSync } from "node:fs"; import { basename, dirname, isAbsolute, join, sep, win32 } from "node:path"; import { parentPort } from "node:worker_threads"; import { realpathWithoutOpenStrict } from "../../../../utils/paths.js"; import { createDetector, readFirmlinks } from "./alias-detector.js"; import { readMountTable } from "./mount-table.js"; import { createPathIdentity } from "./path-identity.js"; const MOUNT_REFRESH_MS = 60_000; function codeOf(error) { const code = error?.code; return typeof code === "string" ? code : "EUNKNOWN"; } /** The resolver's filesystem work, with the real primitives; the worker entry below hosts it off the main thread. */ export function createResolverService(readTable = readMountTable) { const identity = createPathIdentity({ realpathNative: (value) => realpathSync.native(value), lstat: (value, options) => (options ? lstatSync(value, options) : lstatSync(value, { bigint: true })), readlink: (value) => readlinkSync(value), pathApi: { join, dirname, basename, isAbsolute, sep, win32Resolve: win32.resolve }, platform: process.platform, }); let detector; const snapshot = () => { const table = readTable(); detector = createDetector(table); return { type: "snapshot", snapshot: table, firmlinks: readFirmlinks() }; }; const detect = (value) => { if (!detector) snapshot(); return detector ? detector(value).alias : true; }; return { snapshot, serve(request) { try { switch (request.kind) { case "canonicalize": return { id: request.id, ok: true, value: identity.canonical(request.path, request.cwd) }; case "canonicalizeRule": return { id: request.id, ok: true, value: identity.canonicalRule(request.pattern, request.cwd) }; case "detect": return { id: request.id, ok: true, value: detect(request.path) }; case "walk": return { id: request.id, ok: true, value: realpathWithoutOpenStrict(request.path) }; case "refresh": return { id: request.id, ok: true, value: undefined }; } } catch (error) { return { id: request.id, ok: false, code: codeOf(error) }; } }, }; } if (parentPort) { const port = parentPort; const service = createResolverService(); const publish = () => port.postMessage(service.snapshot()); publish(); setInterval(publish, MOUNT_REFRESH_MS).unref(); port.on("message", (request) => { if (request.kind === "refresh") publish(); port.postMessage(service.serve(request)); }); } //# sourceMappingURL=path-identity-worker.js.map