UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

49 lines (48 loc) • 2.13 kB
import { existsSync, statSync } from "node:fs"; import { join } from "node:path"; import { parentPort, workerData } from "node:worker_threads"; import { DatabaseSync } from "node:sqlite"; //#region src/infra/bun-sqlite-close-probe.worker.ts const retained = []; parentPort?.on("message", () => retained.length); function probe() { const directory = workerData; const other = new DatabaseSync(join(directory, "unrelated.sqlite")); retained.push(other); other.exec("CREATE TABLE unrelated(x); INSERT INTO unrelated VALUES (42)"); for (const mode of ["close", "dispose"]) { const path = join(directory, `${mode}.sqlite`); const db = new DatabaseSync(path); retained.push(db); if (db.prepare("PRAGMA journal_mode=WAL").get()?.journal_mode !== "wal") return "SQLite close check cannot establish WAL mode"; db.exec("PRAGMA wal_autocheckpoint=0; CREATE TABLE data(x); INSERT INTO data VALUES (1),(2),(3)"); const stepped = db.prepare("SELECT x FROM data"); stepped.get(); const live = db.prepare("SELECT x FROM data"); const cursor = live.iterate(); cursor.next(); retained.push(stepped, live, cursor, db.prepare("SELECT x FROM data")); const pages = db.prepare("PRAGMA page_count").get()?.page_count; const pageSize = db.prepare("PRAGMA page_size").get()?.page_size; const sidecars = ["-wal", "-shm"].map((suffix) => path + suffix); if (!sidecars.every(existsSync) || typeof pages !== "number" || typeof pageSize !== "number") return "SQLite close check WAL preconditions are unavailable"; if (mode === "close") db.close(); else db[Symbol.dispose](); if (sidecars.some(existsSync) || statSync(path).size !== pages * pageSize) return `SQLite ${mode} retained native WAL resources`; let invalidated = false; try { stepped.get(); } catch { invalidated = true; } if (!invalidated || other.prepare("SELECT x FROM unrelated").get()?.x !== 42) return `SQLite ${mode} failed statement or sibling isolation`; } return ""; } try { parentPort?.postMessage(probe(), []); } catch (error) { parentPort?.postMessage(`SQLite close check failed: ${String(error)}`, []); } //#endregion export {};