openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
161 lines (160 loc) • 7.97 kB
JavaScript
import { c as isRecord } from "./record-coerce-TIsquoVR.mjs";
import { t as installProcessWarningFilter } from "./warning-filter-DPiX2pYu.mjs";
import { t as formatErrorMessage } from "./errors-BvOkmQM5.mjs";
import { c as registerNodeSqliteDisposeCallback } from "./kysely-sync-cache-state-DbAMR_DD.mjs";
import { a as registerSqliteReaderConnection } from "./sqlite-reader-lifecycle-CF8Y2ajA.mjs";
import { r as isSqliteWalResetSafeVersion } from "./node-sqlite-C2ThNhQ8.mjs";
import { o as ensureSqliteLibrarySelected, r as SQLITE_NATIVE_RUNTIME_ADMISSION_KEY, s as getSqliteRuntimeCapabilities } from "./bun-sqlite-library-DlNkYpEb.mjs";
import { n as compareValidSemver } from "./semver-aYpwYdrQ.mjs";
import { g as trackSqliteSchema } from "./sqlite-schema-facts-BVf0E_0z.mjs";
import { createRequire } from "node:module";
import { pathToFileURL } from "node:url";
import path from "node:path";
import { isDeepStrictEqual } from "node:util";
import { getEnvironmentData, isMainThread, setEnvironmentData } from "node:worker_threads";
//#region src/infra/node-sqlite.ts
const require = createRequire(import.meta.url);
let validatedSqliteModule;
let extensionLoadingSupported = false;
let jsonbSupported = false;
let walCheckpointNoopSupported = false;
function resolveSqliteFilesystemPath(pathname) {
if (process.platform !== "win32") return pathname;
return path.toNamespacedPath(path.resolve(pathname));
}
function resolveNodeSqliteLocation(location) {
if (location === "" || location === ":memory:" || location.startsWith("file:")) return location;
return resolveSqliteFilesystemPath(location);
}
/** Preserve native Windows path prefixes before adding SQLite URI parameters. */
function resolveSqliteFileUriPath(pathname, platform) {
if (platform === "win32") {
const namespacedPath = path.win32.toNamespacedPath(path.win32.resolve(pathname));
return `file:${encodeURIComponent(namespacedPath)}`;
}
return pathToFileURL(path.resolve(pathname)).href;
}
/** Open an existing writable database without SQLite's create-if-missing flag. */
function resolveExistingSqliteFileUri(pathname, platform = process.platform) {
return `${resolveSqliteFileUriPath(pathname, platform)}?mode=rw`;
}
/** Build an immutable SQLite URI without losing the Windows long-path namespace. */
function resolveImmutableSqliteFileUri(pathname, platform = process.platform) {
return `${resolveSqliteFileUriPath(pathname, platform)}?mode=ro&immutable=1`;
}
function assertSqliteWalResetSafeVersion(version, nodeVersion) {
if (isSqliteWalResetSafeVersion(version)) return;
const variables = process.config?.variables;
const isShared = variables?.node_shared_sqlite === true || variables?.node_shared_sqlite === "true";
throw new Error(`OpenClaw requires SQLite 3.51.3+, 3.50.7+ within 3.50.x, or 3.44.6+ within 3.44.x for WAL safety; Node ${nodeVersion} ${isShared ? "uses shared system" : "embeds"} SQLite ${version}, which is affected by the upstream WAL-reset database corruption bug. ${isShared ? "Upgrade the system SQLite library to one of those safe versions, or use a Node build embedding a safe version." : "Upgrade to Node 24.22.1+ or 26.12.1+ before retrying."}`);
}
function sqliteNativeRuntimeIdentity() {
return {
pid: process.pid,
executable: process.execPath,
nodeVersion: process.versions.node,
bunVersion: process.versions.bun,
library: ensureSqliteLibrarySelected()
};
}
function parseSqliteNativeRuntimeAdmission(value) {
if (!isRecord(value) || value.format !== 1 || typeof value.version !== "string" || typeof value.extensionLoadingSupported !== "boolean") return;
const runtime = sqliteNativeRuntimeIdentity();
if (!isDeepStrictEqual(value.runtime, runtime)) return;
return {
format: 1,
runtime,
version: value.version,
extensionLoadingSupported: value.extensionLoadingSupported
};
}
function safeSqliteNativeRuntimeAdmission(value) {
try {
const admission = parseSqliteNativeRuntimeAdmission(value);
return admission && isSqliteWalResetSafeVersion(admission.version) ? admission : void 0;
} catch {
return;
}
}
/** Reuse a validated isolate fact without opening SQLite or probing the native library. */
function captureSqliteNativeRuntimeAdmission() {
return safeSqliteNativeRuntimeAdmission(getEnvironmentData(SQLITE_NATIVE_RUNTIME_ADMISSION_KEY));
}
/** Publish to later sibling workers; database authority remains with its existing owners. */
function installSqliteNativeRuntimeAdmission(value) {
const admission = safeSqliteNativeRuntimeAdmission(value);
if (admission) setEnvironmentData(SQLITE_NATIVE_RUNTIME_ADMISSION_KEY, admission);
}
function assertSafeSqliteRuntime(sqlite) {
if (validatedSqliteModule === sqlite) return;
const inherited = isMainThread ? void 0 : parseSqliteNativeRuntimeAdmission(getEnvironmentData(SQLITE_NATIVE_RUNTIME_ADMISSION_KEY));
if (inherited) {
assertSqliteWalResetSafeVersion(inherited.version, process.versions.node);
jsonbSupported = (compareValidSemver(inherited.version, "3.45.0") ?? -1) >= 0;
walCheckpointNoopSupported = (compareValidSemver(inherited.version, "3.53.0") ?? -1) >= 0;
extensionLoadingSupported = inherited.extensionLoadingSupported;
validatedSqliteModule = sqlite;
return;
}
const database = new sqlite.DatabaseSync(":memory:");
let version;
let extensions;
try {
const row = database.prepare("SELECT sqlite_version() AS version, sqlite_compileoption_used('OMIT_LOAD_EXTENSION') AS omitted").get();
version = typeof row?.version === "string" ? row.version : "unknown";
assertSqliteWalResetSafeVersion(version, process.versions.node);
extensions = row?.omitted === 0;
} finally {
database.close();
}
jsonbSupported = (compareValidSemver(version, "3.45.0") ?? -1) >= 0;
walCheckpointNoopSupported = (compareValidSemver(version, "3.53.0") ?? -1) >= 0;
extensionLoadingSupported = extensions;
validatedSqliteModule = sqlite;
setEnvironmentData(SQLITE_NATIVE_RUNTIME_ADMISSION_KEY, {
format: 1,
runtime: sqliteNativeRuntimeIdentity(),
version,
extensionLoadingSupported: extensions
});
}
/** Load node:sqlite after installing the process warning filter. */
function requireNodeSqlite() {
installProcessWarningFilter();
try {
ensureSqliteLibrarySelected();
const sqlite = require("node:sqlite");
assertSafeSqliteRuntime(sqlite);
return sqlite;
} catch (err) {
const message = formatErrorMessage(err);
throw new Error(`SQLite support is unavailable or unsafe in this Node runtime. ${message}`, { cause: err });
}
}
/** Whether the loaded SQLite library supports native extensions. */
function supportsNodeSqliteExtensionLoading() {
requireNodeSqlite();
return extensionLoadingSupported;
}
/** JSONB is absent from the supported SQLite 3.44 maintenance line. */
function supportsNodeSqliteJsonb() {
requireNodeSqlite();
return jsonbSupported;
}
/** Older SQLite versions can interpret NOOP as a mutating checkpoint mode. */
function supportsNodeSqliteWalCheckpointNoop() {
requireNodeSqlite();
return walCheckpointNoopSupported;
}
/** Open node:sqlite through OpenClaw's runtime and filesystem-location boundary. */
function openNodeSqliteDatabase(location, options) {
const sqlite = requireNodeSqlite();
const resolvedLocation = resolveNodeSqliteLocation(location);
const database = new sqlite.DatabaseSync(resolvedLocation, options ?? {});
trackSqliteSchema(database, sqlite);
if (!getSqliteRuntimeCapabilities().explicitSqliteCloseReleasesNativeResources) registerNodeSqliteDisposeCallback(database, () => {});
registerSqliteReaderConnection(database);
return database;
}
//#endregion
export { resolveExistingSqliteFileUri as a, resolveSqliteFilesystemPath as c, supportsNodeSqliteWalCheckpointNoop as d, requireNodeSqlite as i, supportsNodeSqliteExtensionLoading as l, installSqliteNativeRuntimeAdmission as n, resolveImmutableSqliteFileUri as o, openNodeSqliteDatabase as r, resolveNodeSqliteLocation as s, captureSqliteNativeRuntimeAdmission as t, supportsNodeSqliteJsonb as u };