@velvetmonkey/vault-core 2.0.84 → 2.0.85

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/sqlite.js +30 -11
  2. package/package.json +1 -1
package/dist/sqlite.js CHANGED
@@ -568,6 +568,13 @@ function initSchema(db) {
568
568
  db.prepare('INSERT OR IGNORE INTO schema_version (version) VALUES (?)').run(SCHEMA_VERSION);
569
569
  }
570
570
  }
571
+ function deleteStateDbFiles(dbPath) {
572
+ for (const suffix of ['', '-wal', '-shm']) {
573
+ const p = dbPath + suffix;
574
+ if (fs.existsSync(p))
575
+ fs.unlinkSync(p);
576
+ }
577
+ }
571
578
  /**
572
579
  * Open or create the state database for a vault
573
580
  *
@@ -583,19 +590,31 @@ export function openStateDb(vaultPath) {
583
590
  const stat = fs.statSync(dbPath);
584
591
  if (stat.size === 0) {
585
592
  console.error(`[vault-core] Deleting corrupted 0-byte state.db at ${dbPath}`);
586
- fs.unlinkSync(dbPath);
587
- // Also remove WAL and SHM files if they exist
588
- const walPath = dbPath + '-wal';
589
- const shmPath = dbPath + '-shm';
590
- if (fs.existsSync(walPath))
591
- fs.unlinkSync(walPath);
592
- if (fs.existsSync(shmPath))
593
- fs.unlinkSync(shmPath);
593
+ deleteStateDbFiles(dbPath);
594
+ }
595
+ }
596
+ let db;
597
+ try {
598
+ db = new Database(dbPath);
599
+ initSchema(db);
600
+ }
601
+ catch (err) {
602
+ // Corrupted database (e.g., "file is not a database") — delete and retry once
603
+ if (fs.existsSync(dbPath)) {
604
+ const msg = err instanceof Error ? err.message : String(err);
605
+ console.error(`[vault-core] Corrupted state.db (${msg}) — deleting and recreating`);
606
+ try {
607
+ db?.close();
608
+ }
609
+ catch { /* ignore */ }
610
+ deleteStateDbFiles(dbPath);
611
+ db = new Database(dbPath);
612
+ initSchema(db);
613
+ }
614
+ else {
615
+ throw err;
594
616
  }
595
617
  }
596
- const db = new Database(dbPath);
597
- // Initialize schema
598
- initSchema(db);
599
618
  // Prepare all statements
600
619
  const stateDb = {
601
620
  db,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velvetmonkey/vault-core",
3
- "version": "2.0.84",
3
+ "version": "2.0.85",
4
4
  "description": "Shared vault utilities for Flywheel ecosystem (entity scanning, wikilinks, protected zones)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",