Refactor error handling and localDb structure, and fix usage tracking bug.

This commit is contained in:
decolua
2026-04-16 10:58:35 +07:00
parent 3badf1cbb6
commit 75ad0bef8e
13 changed files with 162 additions and 109 deletions

View File

@@ -2,32 +2,12 @@ import { Low } from "lowdb";
import { JSONFile } from "lowdb/node";
import { v4 as uuidv4 } from "uuid";
import path from "node:path";
import os from "node:os";
import fs from "node:fs";
import lockfile from "proper-lockfile";
import { DATA_DIR } from "@/lib/dataDir.js";
const DEFAULT_MITM_ROUTER_BASE = "http://localhost:20128";
const isCloud = typeof caches !== 'undefined' || typeof caches === 'object';
function getAppName() {
return "9router";
}
function getUserDataDir() {
if (isCloud) return "/tmp";
if (process.env.DATA_DIR) return process.env.DATA_DIR;
const platform = process.platform;
const homeDir = os.homedir();
const appName = getAppName();
if (platform === "win32") {
return path.join(process.env.APPDATA || path.join(homeDir, "AppData", "Roaming"), appName);
}
return path.join(homeDir, `.${appName}`);
}
const DATA_DIR = getUserDataDir();
const DB_FILE = isCloud ? null : path.join(DATA_DIR, "db.json");
if (!isCloud && !fs.existsSync(DATA_DIR)) {