Files
OmniRoute/tests/unit/runtime/magicBytes.test.ts
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

63 lines
2.1 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { writeFileSync, mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { validateBinaryMagic, platformBinaryLabel } from "../../../bin/cli/runtime/magicBytes.mjs";
const dir = mkdtempSync(join(tmpdir(), "magic-test-"));
test("detects ELF", () => {
const p = join(dir, "fake.so");
writeFileSync(p, Buffer.from([0x7f, 0x45, 0x4c, 0x46, 0, 0, 0, 0]));
assert.equal(validateBinaryMagic(p), "elf");
});
test("detects Mach-O 64-bit BE", () => {
const p = join(dir, "fake.dylib");
writeFileSync(p, Buffer.from([0xfe, 0xed, 0xfa, 0xcf, 0, 0, 0, 0]));
assert.equal(validateBinaryMagic(p), "macho");
});
test("detects Mach-O LE", () => {
const p = join(dir, "fake-le.dylib");
writeFileSync(p, Buffer.from([0xcf, 0xfa, 0xed, 0xfe, 0, 0, 0, 0]));
assert.equal(validateBinaryMagic(p), "macho-le");
});
test("detects Mach-O fat binary", () => {
const p = join(dir, "fake.fat");
writeFileSync(p, Buffer.from([0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 0]));
assert.equal(validateBinaryMagic(p), "macho-fat");
});
test("detects PE (MZ)", () => {
const p = join(dir, "fake.node");
writeFileSync(p, Buffer.from([0x4d, 0x5a, 0, 0, 0, 0, 0, 0]));
assert.equal(validateBinaryMagic(p), "pe");
});
test("returns null for non-binary", () => {
const p = join(dir, "fake.txt");
writeFileSync(p, "hello world", "utf-8");
assert.equal(validateBinaryMagic(p), null);
});
test("returns null for missing file", () => {
assert.equal(validateBinaryMagic(join(dir, "nope.bin")), null);
});
test("returns null for too-short file", () => {
const p = join(dir, "short.bin");
writeFileSync(p, Buffer.from([0x7f, 0x45]));
assert.equal(validateBinaryMagic(p), null);
});
test("platformBinaryLabel matches process.platform", () => {
const expected =
process.platform === "win32" ? "pe" : process.platform === "darwin" ? "macho" : "elf";
assert.equal(platformBinaryLabel(), expected);
});
test.after(() => rmSync(dir, { recursive: true, force: true }));