// `npm test` prerequisite: the libdatachannel native addon.
//
// test/orbit-address.test.mjs boots the REAL stack to prove the OrbitDB address
// (PROTOCOL.md §5) — and `helia`'s module graph eagerly pulls @libp2p/webrtc,
// whose node build loads @ipshipyard/node-datachannel's .node addon. The browser
// never touches it; only this test does.
//
// The package's own install script (`prebuild-install -r napi || build from
// source`) does NOT reliably leave the binary behind under npm 10 — verified on
// a clean `npm install` in this repo — so this script fetches the prebuild
// on demand and is a no-op once it is there.
import { spawnSync } from "node:child_process";
import { existsSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const client = dirname(dirname(fileURLToPath(import.meta.url)));
const pkg = join(client, "node_modules", "@ipshipyard", "node-datachannel");
const addon = join(pkg, "build", "Release", "node_datachannel.node");
if (existsSync(addon)) {
process.exit(0);
}
if (!existsSync(pkg)) {
console.error("ensure-native: @ipshipyard/node-datachannel is not installed — run npm install");
process.exit(1);
}
console.log("ensure-native: fetching the libdatachannel prebuild…");
const r = spawnSync("npx", ["--yes", "prebuild-install", "-r", "napi"], { cwd: pkg, stdio: "inherit" });
if (r.status !== 0 || !existsSync(addon)) {
console.error(
"ensure-native: could not fetch the prebuild. test/orbit-address.test.mjs needs it; run\n" +
" (cd node_modules/@ipshipyard/node-datachannel && npx prebuild-install -r napi)",
);
process.exit(1);
}