21 lines
721 B
TypeScript
21 lines
721 B
TypeScript
import { chmodSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
import { DatabaseSync } from "node:sqlite";
|
|
|
|
// Keep the historical filename so existing authentication state is preserved.
|
|
// Research and authentication use different tables in this same site database.
|
|
export function openSiteDatabase() {
|
|
const configFile = process.env.AUTH_CONFIG_FILE;
|
|
if (!configFile) throw new Error("Authentication is not configured");
|
|
const file = join(dirname(configFile), "login-state.sqlite");
|
|
const db = new DatabaseSync(file);
|
|
try {
|
|
chmodSync(file, 0o600);
|
|
db.exec("PRAGMA busy_timeout=5000; PRAGMA foreign_keys=ON;");
|
|
return db;
|
|
} catch (error) {
|
|
db.close();
|
|
throw error;
|
|
}
|
|
}
|