OmniRoute/scripts/postinstall.mjs
diegosouzapw 8f915b18b0 feat(runtime): dynamic SQLite runtime installer with 5-step fallback chain
Adds bin/cli/runtime/sqliteRuntime.mjs that resolves better-sqlite3 from:
(1) bundled optionalDependency, (2) ~/.omniroute/runtime/ install,
(3) lazy npm install into runtime dir, (4) node:sqlite stdlib (Node >=22.5),
(5) bundled sql.js WASM. Each native binary is validated against expected
platform magic bytes (ELF/Mach-O/PE) before load.

Adds bin/cli/runtime/magicBytes.mjs with validateBinaryMagic() helper
(9 tests). Adds bin/cli/runtime/index.mjs as warmUpRuntimes() orchestrator.

Adds scripts/postinstall.mjs warm-up hook (non-fatal, skipped in CI).
Integrates it as the last step of scripts/build/postinstall.mjs.

Extends src/lib/db/core.ts with ensureDbInitialized() (async, idempotent)
and getDriverInfo() so the startup orchestrator can await the resolver
before any DB access, enabling graceful degradation without crashing the
process on missing better-sqlite3.

Solves Windows EBUSY error on 'npm install -g omniroute@latest' while the
previous version is still running, and works in environments without C++
build tools or with unreachable npm registry.

Documents OMNIROUTE_SKIP_POSTINSTALL in .env.example and ENVIRONMENT.md.

Ref: 9router/cli/hooks/sqliteRuntime.js (pattern origin).
2026-05-15 00:05:49 -03:00

27 lines
751 B
JavaScript

#!/usr/bin/env node
/**
* Post-install warm-up for OmniRoute native runtimes.
*
* Runs after the main `scripts/build/postinstall.mjs` binary-copy step.
* Tries to pre-resolve better-sqlite3 into ~/.omniroute/runtime/ so that
* the first execution is fast and EBUSY-resilient on Windows.
*
* Non-fatal: any error is printed as a warning and install continues.
*/
if (
process.env.OMNIROUTE_SKIP_POSTINSTALL === "1" ||
process.env.CI === "true" ||
process.env.CI === "1"
) {
process.exit(0);
}
(async () => {
try {
const { warmUpRuntimes } = await import("../bin/cli/runtime/index.mjs");
await warmUpRuntimes();
} catch (err) {
console.warn(`[omniroute] postinstall warm-up skipped: ${err?.message ?? err}`);
}
})();