import { readFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { OMP_HARNESS_PACKAGE, OMP_NATIVES_PACKAGE, packageNodeModulesRoot, } from "./host.ts"; export interface HarnessAttestation { path: string; version: string; nativePath: string; } /** Resolve the installed peer from this package's directory, never Bun's ambient cache. */ export function resolveHarnessEntry(moduleDir: string = import.meta.dir): string { return Bun.resolveSync(OMP_HARNESS_PACKAGE, moduleDir); } /** The installed peer version belonging to a resolved harness entry. */ export function harnessVersion(entry: string): string | undefined { const root = packageNodeModulesRoot(entry); if (root === undefined) return undefined; try { const parsed: unknown = JSON.parse( readFileSync(join(root, OMP_HARNESS_PACKAGE, "package.json"), "utf8"), ); if (parsed === null || typeof parsed !== "object") return undefined; const version = Reflect.get(parsed, "version"); return typeof version === "string" && version !== "" ? version : undefined; } catch { return undefined; } } /** * Exercise the worker's real import contract: explicitly anchored peer and * native-addon resolution, with the caller responsible for passing * `--no-install` so neither import can fall through to Bun's package cache. */ export async function probeHarness(moduleDir: string = import.meta.dir): Promise { const path = resolveHarnessEntry(moduleDir); await import(path); const version = harnessVersion(path); if (version === undefined) throw new Error(`cannot read the harness version for ${path}`); const nativePath = Bun.resolveSync(OMP_NATIVES_PACKAGE, dirname(path)); await import(nativePath); return { path, version, nativePath }; } if (import.meta.main) { try { process.stdout.write(`${JSON.stringify(await probeHarness())}\n`); } catch (cause) { process.stderr.write(`${cause instanceof Error ? cause.stack ?? cause.message : String(cause)}\n`); process.exitCode = 1; } }