Fix: Add Windows support for SQLite database access (Issue #23)
Replace CLI-based sqlite3 extraction with better-sqlite3 library: - Create shared src/db/database.js module for cross-platform SQLite access - Remove duplicate extractTokenFromDB functions from token-extractor.js and account-manager.js - Add better-sqlite3 dependency with prebuilt Windows/Mac/Linux binaries - Improve error handling with specific messages for common issues Fixes #23 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
93
src/db/database.js
Normal file
93
src/db/database.js
Normal file
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* SQLite Database Access Module
|
||||
* Provides cross-platform database operations for Antigravity state.
|
||||
*
|
||||
* Uses better-sqlite3 for:
|
||||
* - Windows compatibility (no CLI dependency)
|
||||
* - Native performance
|
||||
* - Synchronous API (simple error handling)
|
||||
*/
|
||||
|
||||
import Database from 'better-sqlite3';
|
||||
import { ANTIGRAVITY_DB_PATH } from '../constants.js';
|
||||
|
||||
/**
|
||||
* Query Antigravity database for authentication status
|
||||
* @param {string} [dbPath] - Optional custom database path
|
||||
* @returns {Object} Parsed auth data with apiKey, email, name, etc.
|
||||
* @throws {Error} If database doesn't exist, query fails, or no auth status found
|
||||
*/
|
||||
export function getAuthStatus(dbPath = ANTIGRAVITY_DB_PATH) {
|
||||
let db;
|
||||
try {
|
||||
// Open database in read-only mode
|
||||
db = new Database(dbPath, {
|
||||
readonly: true,
|
||||
fileMustExist: true
|
||||
});
|
||||
|
||||
// Prepare and execute query
|
||||
const stmt = db.prepare(
|
||||
"SELECT value FROM ItemTable WHERE key = 'antigravityAuthStatus'"
|
||||
);
|
||||
const row = stmt.get();
|
||||
|
||||
if (!row || !row.value) {
|
||||
throw new Error('No auth status found in database');
|
||||
}
|
||||
|
||||
// Parse JSON value
|
||||
const authData = JSON.parse(row.value);
|
||||
|
||||
if (!authData.apiKey) {
|
||||
throw new Error('Auth data missing apiKey field');
|
||||
}
|
||||
|
||||
return authData;
|
||||
} catch (error) {
|
||||
// Enhance error messages for common issues
|
||||
if (error.code === 'SQLITE_CANTOPEN') {
|
||||
throw new Error(
|
||||
`Database not found at ${dbPath}. ` +
|
||||
'Make sure Antigravity is installed and you are logged in.'
|
||||
);
|
||||
}
|
||||
// Re-throw with context if not already our error
|
||||
if (error.message.includes('No auth status') || error.message.includes('missing apiKey')) {
|
||||
throw error;
|
||||
}
|
||||
throw new Error(`Failed to read Antigravity database: ${error.message}`);
|
||||
} finally {
|
||||
// Always close database connection
|
||||
if (db) {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if database exists and is accessible
|
||||
* @param {string} [dbPath] - Optional custom database path
|
||||
* @returns {boolean} True if database exists and can be opened
|
||||
*/
|
||||
export function isDatabaseAccessible(dbPath = ANTIGRAVITY_DB_PATH) {
|
||||
let db;
|
||||
try {
|
||||
db = new Database(dbPath, {
|
||||
readonly: true,
|
||||
fileMustExist: true
|
||||
});
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
} finally {
|
||||
if (db) {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
getAuthStatus,
|
||||
isDatabaseAccessible
|
||||
};
|
||||
Reference in New Issue
Block a user