Files
OmniRoute/tests/unit/db-core-native-error.test.ts
Bob.Hou 5f9e153971 fix(mcp): fall back when better-sqlite3 export is not callable (#13903)
* mcp/audit: fall back when better-sqlite3 export is not callable

Dashboard MCP status polls reopen a failed native sqlite load every 30s
because a minified TypeError ("a is not a function") was not treated as
a native load failure and a failed open was not cached. Classify that
shape, fall back to node:sqlite, cache the miss, and refuse to ship a
Docker image without better_sqlite3.node.

Signed-off-by: Minxi Hou <houminxi@gmail.com>

* mcp/audit: force native better-sqlite3 compile in Docker

better-sqlite3 13 ships a linux prebuild. Bare `node-gyp rebuild`
then only TOUCHes stamp files and never writes
build/Release/better_sqlite3.node, so the new test -f gate fails the
image build. Pass --force_build=1, matching the package's own
build-release script.

Signed-off-by: Minxi Hou <houminxi@gmail.com>

* db/core: keep native-load classification under the file-size cap

The audit fallback added two TypeError fingerprints in core.ts and
crossed the frozen 1788-line cap. Move the classifier into
sqliteLoadError.ts and re-export it so existing importers stay stable.

Signed-off-by: Minxi Hou <houminxi@gmail.com>

* build/bootstrap: keep the encrypted-credentials probe narrow

The native-load classifier was copied into scripts/build/bootstrap-env.mjs
alongside the runtime one, but the two files consume its verdict in opposite
directions. In src/lib/db/sqliteLoadError.ts a true verdict means "the driver
is unusable, cascade to node:sqlite", so treating a non-callable export as a
load failure is what we want. In the bootstrap the verdict feeds
hasEncryptedCredentials, where true means "no encrypted credentials found" and
clears the way to generate a fresh STORAGE_ENCRYPTION_KEY.

With the TypeError patterns in the bootstrap copy, a binding that loads but
exports something non-callable over a database full of enc:v1: rows reads as an
empty database, and the operator silently loses access to every stored
credential. Drop those two patterns from the bootstrap copy only, and note in
both files why the pair is deliberately not identical.

A corrupt binding still fails loudly there, now with the database path, the
underlying message, and a rebuild hint, so the narrower classifier does not
cost any diagnosability.

Signed-off-by: Minxi Hou <houminxi@gmail.com>

* fix(mcp): keep audit logging recoverable when the database is created later

getDb() cached a null for the "storage.sqlite does not exist yet" branch, and
closeAuditDb() returns before clearing a falsy cache — so an MCP server started
before the app created the database stayed without audit logging for the whole
process lifetime. Only a genuine driver-load failure is cached now; the
not-found branch retries, which is how it recovers when the file appears.

Covered by a new test that fails without the change.

Also replace the fabricated minified TypeError text ("a is not a function")
thrown by the loader with "better-sqlite3 export is not a function": the
operator sees a diagnosable message and isNativeSqliteLoadError() still
classifies it (it matches on "is not a function").

---------

Signed-off-by: Minxi Hou <houminxi@gmail.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-09-17 18:47:25 -03:00

78 lines
3.4 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { isNativeSqliteLoadError, isSqliteDriverUnavailableError } from "../../src/lib/db/core";
test("isNativeSqliteLoadError detects Module did not self-register", () => {
const err = new Error("Module did not self-register: better_sqlite3.node");
assert.equal(isNativeSqliteLoadError(err), true);
});
test("isNativeSqliteLoadError detects NODE_MODULE_VERSION mismatch", () => {
const err = new Error(
"The module was compiled against a different Node.js version using NODE_MODULE_VERSION 115."
);
assert.equal(isNativeSqliteLoadError(err), true);
});
test("isNativeSqliteLoadError detects ERR_DLOPEN_FAILED in message", () => {
const err = new Error("ERR_DLOPEN_FAILED while loading better_sqlite3.node");
assert.equal(isNativeSqliteLoadError(err), true);
});
test("isNativeSqliteLoadError detects ERR_DLOPEN_FAILED via error.code", () => {
const err = Object.assign(new Error("dlopen failed"), { code: "ERR_DLOPEN_FAILED" });
assert.equal(isNativeSqliteLoadError(err), true);
});
// #2358 — bun and similar runtimes skip postinstall, so the *.node binary
// is never downloaded. `bindings()` produces this exact message before any
// DLOPEN even happens, and we need to surface the friendly rebuild guide.
test("isNativeSqliteLoadError detects 'Could not locate the bindings file' (bun #2358)", () => {
const err = new Error(
"Could not locate the bindings file. Tried: → /Users/x/.../better-sqlite3/build/better_sqlite3.node"
);
assert.equal(isNativeSqliteLoadError(err), true);
});
test("isNativeSqliteLoadError detects 'Cannot find module better-sqlite3'", () => {
const err = new Error("Cannot find module 'better-sqlite3'");
assert.equal(isNativeSqliteLoadError(err), true);
});
test("isNativeSqliteLoadError detects MODULE_NOT_FOUND via error.code", () => {
const err = Object.assign(new Error("not found"), { code: "MODULE_NOT_FOUND" });
assert.equal(isNativeSqliteLoadError(err), true);
});
test("isNativeSqliteLoadError detects minified TypeError 'a is not a function'", () => {
const err = new TypeError("a is not a function");
assert.equal(isNativeSqliteLoadError(err), true);
});
test("isNativeSqliteLoadError detects 'is not a constructor' from a non-function export", () => {
const err = new TypeError("b.default is not a constructor");
assert.equal(isNativeSqliteLoadError(err), true);
});
test("isNativeSqliteLoadError returns false for unrelated errors", () => {
assert.equal(isNativeSqliteLoadError(new Error("SQLITE_BUSY: database is locked")), false);
assert.equal(isNativeSqliteLoadError(new Error("ENOENT: no such file")), false);
assert.equal(isNativeSqliteLoadError(null), false);
assert.equal(isNativeSqliteLoadError(undefined), false);
assert.equal(isNativeSqliteLoadError("some string"), false);
});
test("isSqliteDriverUnavailableError detects pre-init sql.js fallback errors", () => {
const err = new Error(
"[DB] Nenhum driver SQLite disponível para '/tmp/storage.sqlite'. Chame ensureDbInitialized() no startup. sql.js WASM ainda não foi pré-inicializado."
);
assert.equal(isSqliteDriverUnavailableError(err), true);
});
test("isSqliteDriverUnavailableError returns false for unrelated errors", () => {
assert.equal(isSqliteDriverUnavailableError(new Error("SQLITE_BUSY: database is locked")), false);
assert.equal(isSqliteDriverUnavailableError(undefined), false);
});