openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
63 lines (62 loc) • 2.21 kB
JavaScript
//#region node-sqlite-capabilities.mjs
/**
* @typedef {import("./node-sqlite-capabilities.d.mts").SqliteCapabilities} SqliteCapabilities
* @typedef {import("./node-sqlite-capabilities.d.mts").SqliteCapabilityDatabase} SqliteCapabilityDatabase
* @typedef {import("./node-sqlite-capabilities.d.mts").SqliteCapabilityDatabaseConstructor} SqliteCapabilityDatabaseConstructor
*/
/**
* @param {SqliteCapabilityDatabaseConstructor} DatabaseSync
* @returns {SqliteCapabilities}
*/
function probeSqlite(e) {
/** @type {SqliteCapabilities} */
const t = {
available: false,
version: null,
text: false,
blob: false,
json: false
};
/** @type {SqliteCapabilityDatabase | undefined} */
let n;
try {
n = new e(":memory:");
t.available = true;
const r = "a\0b\0";
const i = Buffer.from(r, "utf8");
const a = JSON.stringify({ value: r });
n.exec("CREATE TABLE probe (text_value TEXT, blob_value BLOB, json_value TEXT)");
n.prepare("INSERT INTO probe VALUES (?, ?, ?)").run(r, i, a);
const o = n.prepare("SELECT sqlite_version() AS version, text_value, blob_value, json_value FROM probe").get();
t.version = typeof o?.version === "string" ? o.version : null;
t.text = typeof o?.text_value === "string" && o.text_value.length === 4 && Buffer.from(o.text_value, "utf8").equals(i);
t.blob = o?.blob_value instanceof Uint8Array && Buffer.from(o.blob_value).equals(i);
t.json = o?.json_value === a && JSON.parse(o.json_value).value === r;
} catch (e) {
let r = e;
if (n && t.version === null) try {
const e = n.prepare("SELECT sqlite_version() AS version").get()?.version;
t.version = typeof e === "string" ? e : null;
} catch (e) {
r = e;
}
t.error = r instanceof Error ? r.message : String(r);
} finally {
n?.close();
}
return t;
}
/** @param {string} value */
function isSqliteWalResetSafeVersion(e) {
const t = /^(\d+)\.(\d+)\.(\d+)$/u.exec(e.trim());
if (!t) return false;
const [n, r, i] = t.slice(1).map(Number);
if (![
n,
r,
i
].every(Number.isSafeInteger)) return false;
return n > 3 || n === 3 && (r > 51 || r === 51 && i >= 3 || r === 50 && i >= 7 || r === 44 && i >= 6);
}
//#endregion
export { probeSqlite as n, isSqliteWalResetSafeVersion as t };