consortium
Version:
Remote control and session sharing CLI for AI coding agents
81 lines (74 loc) • 3.04 kB
JavaScript
/**
* fix-libsodium-esm.cjs
*
* libsodium-wrappers ships a broken ESM build: its
* `dist/modules-esm/libsodium-wrappers.mjs` does `import "./libsodium.mjs"`,
* but the package never publishes that sibling file — the actual module
* lives in the separately-installed `libsodium` package.
*
* In bundled-CJS-only consumers the issue is invisible. But pkgroll emits
* the grok runner as an ESM code-split chunk (`dist/runGrok-*.mjs`) that
* leaves `libsodium-wrappers` as a runtime ESM import, and Node's strict
* ESM resolver then errors with ERR_MODULE_NOT_FOUND on the missing
* `libsodium.mjs`. The grok child exits within ~750ms and the daemon
* surfaces only a webhook timeout, manifesting as "stuck on creating"
* in the mobile UI.
*
* Fix: write a one-liner shim at the missing path that re-exports the
* working module from the sibling `libsodium` package. Idempotent and
* dependency-free so it can run during `postinstall`.
*/
;
const fs = require('fs');
const path = require('path');
const SHIM = `// Auto-generated by scripts/fix-libsodium-esm.cjs.
// libsodium-wrappers.mjs imports "./libsodium.mjs" but the file is not
// shipped; the actual module lives in the sibling \`libsodium\` package.
export { default } from 'libsodium';
`;
function tryFix(wrappersDir) {
const target = path.join(wrappersDir, 'dist', 'modules-esm', 'libsodium.mjs');
const wrappersEsm = path.join(wrappersDir, 'dist', 'modules-esm', 'libsodium-wrappers.mjs');
if (!fs.existsSync(wrappersEsm)) return false; // Nothing to patch.
if (fs.existsSync(target)) {
const cur = fs.readFileSync(target, 'utf8');
if (cur === SHIM) return false; // Already up to date.
}
fs.writeFileSync(target, SHIM, 'utf8');
return true;
}
function findCandidates(startDir) {
// Walk up from this script looking for any `node_modules/libsodium-wrappers`
// directories. In a monorepo there can be multiple (root + per-package).
const candidates = [];
let dir = startDir;
for (let i = 0; i < 10; i++) {
const nm = path.join(dir, 'node_modules');
if (fs.existsSync(nm)) {
const direct = path.join(nm, 'libsodium-wrappers');
if (fs.existsSync(direct)) candidates.push(direct);
}
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
return candidates;
}
try {
const candidates = findCandidates(__dirname);
let patched = 0;
for (const c of candidates) {
if (tryFix(c)) {
console.log(`[fix-libsodium-esm] patched ${path.relative(process.cwd(), c)}`);
patched++;
}
}
if (patched === 0) {
// Not an error — the package may not be installed in this layout,
// or the shim is already in place.
}
} catch (err) {
// Never fail the install. The runtime path will fail loudly anyway.
console.error(`[fix-libsodium-esm] non-fatal: ${err && err.message ? err.message : err}`);
}