mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-23 04:09:49 +00:00
feat(db): migrate from lowdb to SQLite with repos pattern
- Add modular DB layer (adapters, migrations, repos, helpers) - Replace localDb/usageDb/requestDetailsDb monoliths with repos - Add Tailscale tunnel integration & status check API - Add /api/cli-tools/all-statuses aggregated endpoint - Add settingsStore (Zustand) and mitm/dbReader - Add DB unit tests (benchmark, concurrent, migration, vs-lowdb)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// CJS reader for MITM standalone process. Reads SQLite mitmAlias scope.
|
||||
// Falls back to legacy db.json or db.json.migrated if SQLite unavailable.
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { DATA_DIR } = require("./paths");
|
||||
|
||||
const DB_FILE = path.join(DATA_DIR, "db", "data.sqlite");
|
||||
const LEGACY_JSON = path.join(DATA_DIR, "db.json");
|
||||
const LEGACY_MIGRATED = path.join(DATA_DIR, "db.json.migrated");
|
||||
|
||||
let sqliteDb = null;
|
||||
let sqliteFailed = false;
|
||||
|
||||
function trySqlite() {
|
||||
if (sqliteDb) return sqliteDb;
|
||||
if (sqliteFailed) return null;
|
||||
try {
|
||||
if (!fs.existsSync(DB_FILE)) return null;
|
||||
const Database = require("better-sqlite3");
|
||||
sqliteDb = new Database(DB_FILE, { readonly: true, fileMustExist: true });
|
||||
return sqliteDb;
|
||||
} catch {
|
||||
sqliteFailed = true;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function readLegacyJson() {
|
||||
for (const file of [LEGACY_JSON, LEGACY_MIGRATED]) {
|
||||
if (!fs.existsSync(file)) continue;
|
||||
try { return JSON.parse(fs.readFileSync(file, "utf-8")); } catch {}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getMitmAlias(toolName) {
|
||||
const db = trySqlite();
|
||||
if (db) {
|
||||
try {
|
||||
const row = db.prepare(`SELECT value FROM kv WHERE scope = 'mitmAlias' AND key = ?`).get(toolName);
|
||||
if (row) return JSON.parse(row.value);
|
||||
} catch {}
|
||||
}
|
||||
// Fallback to legacy JSON
|
||||
const legacy = readLegacyJson();
|
||||
return legacy?.mitmAlias?.[toolName] || null;
|
||||
}
|
||||
|
||||
module.exports = { getMitmAlias };
|
||||
+1
-1
Submodule src/mitm/dev updated: b3b7473d6d...ea459d42d0
@@ -1,6 +1,6 @@
|
||||
const { log, err } = require("../logger");
|
||||
|
||||
const DEFAULT_LOCAL_ROUTER = "http://localhost:20128";
|
||||
const DEFAULT_LOCAL_ROUTER = "http://127.0.0.1:20128";
|
||||
const ROUTER_BASE = String(process.env.MITM_ROUTER_BASE || DEFAULT_LOCAL_ROUTER)
|
||||
.trim()
|
||||
.replace(/\/+$/, "") || DEFAULT_LOCAL_ROUTER;
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ const { isCertExpired } = require("./cert/rootCA");
|
||||
const { DATA_DIR, MITM_DIR } = require("./paths");
|
||||
const { log, err } = require("./logger");
|
||||
|
||||
const DEFAULT_MITM_ROUTER_BASE = "http://localhost:20128";
|
||||
const DEFAULT_MITM_ROUTER_BASE = "http://127.0.0.1:20128";
|
||||
|
||||
function shellQuoteSingle(str) {
|
||||
if (str == null || str === "") return "''";
|
||||
|
||||
+2
-5
@@ -8,8 +8,7 @@ const { log, err, dumpRequest, createResponseDumper } = require("./logger");
|
||||
const { TARGET_HOSTS, URL_PATTERNS, MODEL_SYNONYMS, getToolForHost } = require("./config");
|
||||
const { DATA_DIR, MITM_DIR } = require("./paths");
|
||||
const { getCertForDomain } = require("./cert/generate");
|
||||
|
||||
const DB_FILE = path.join(DATA_DIR, "db.json");
|
||||
const { getMitmAlias } = require("./dbReader");
|
||||
const LOCAL_PORT = 443;
|
||||
const IS_WIN = process.platform === "win32";
|
||||
const ENABLE_FILE_LOG = true;
|
||||
@@ -108,9 +107,7 @@ function extractModel(url, body) {
|
||||
function getMappedModel(tool, model) {
|
||||
if (!model) return null;
|
||||
try {
|
||||
if (!fs.existsSync(DB_FILE)) return null;
|
||||
const db = JSON.parse(fs.readFileSync(DB_FILE, "utf-8"));
|
||||
const aliases = db.mitmAlias?.[tool];
|
||||
const aliases = getMitmAlias(tool);
|
||||
if (!aliases) return null;
|
||||
// Normalize via synonym map (e.g., gemini-default → gemini-3-flash)
|
||||
const lookup = MODEL_SYNONYMS?.[tool]?.[model] || model;
|
||||
|
||||
Reference in New Issue
Block a user