From 55d28e30fd5d1e57257bf12b18e2878ee5c04828 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Sat, 6 Jun 2026 15:05:52 +0800 Subject: [PATCH] refactor: consolidate CCXRAY_HOME resolution into server/paths.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hub.js, settings.js, ratelimit-log.js, and auth.js each inlined the same 'process.env.CCXRAY_HOME || path.join(os.homedir(), ".ccxray")' expression. Replace all four with resolveCcxrayHome() from server/paths.js (introduced in #50), and drop the now-unused 'os' import in each. auth.js keeps the call inside getHubDir() so it still reads the env per call. Pure de-duplication — identical behavior, no precedence change. The CCXRAY_HOME contract is now enforced in one place, so it can't drift the way config.LOGS_DIR did in #31. Closes #51 --- server/auth.js | 4 ++-- server/hub.js | 4 ++-- server/paths.js | 11 +++++------ server/ratelimit-log.js | 4 ++-- server/settings.js | 4 ++-- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/server/auth.js b/server/auth.js index 82fe659..47748bb 100644 --- a/server/auth.js +++ b/server/auth.js @@ -15,14 +15,14 @@ const crypto = require('crypto'); const fs = require('fs'); -const os = require('os'); const path = require('path'); const config = require('./config'); +const { resolveCcxrayHome } = require('./paths'); // ─── Root secret resolution ────────────────────────────────────────── function getHubDir() { - return process.env.CCXRAY_HOME || path.join(os.homedir(), '.ccxray'); + return resolveCcxrayHome(); } function ensureHubDir() { diff --git a/server/hub.js b/server/hub.js index 248bcd0..ad17d13 100644 --- a/server/hub.js +++ b/server/hub.js @@ -2,11 +2,11 @@ const fs = require('fs'); const path = require('path'); -const os = require('os'); const net = require('net'); const http = require('http'); +const { resolveCcxrayHome } = require('./paths'); -const HUB_DIR = process.env.CCXRAY_HOME || path.join(os.homedir(), '.ccxray'); +const HUB_DIR = resolveCcxrayHome(); const HUB_LOCK_PATH = path.join(HUB_DIR, 'hub.json'); const HUB_LOG_PATH = path.join(HUB_DIR, 'hub.log'); const SOCK_PATH = path.join(HUB_DIR, 'hub.sock'); diff --git a/server/paths.js b/server/paths.js index 14e2870..62fcf68 100644 --- a/server/paths.js +++ b/server/paths.js @@ -3,12 +3,11 @@ const path = require('path'); const os = require('os'); -// Single source of truth for ccxray's on-disk locations. config.js and -// storage/index.js both resolve the logs dir from here, so the startup banner -// and legacy-migration target can no longer drift from where logs actually -// get written. hub.js, settings.js, ratelimit-log.js, and auth.js still -// duplicate the home-dir resolution inline and are candidates for future -// consolidation here. +// Single source of truth for ccxray's on-disk locations. config.js, +// storage/index.js, hub.js, settings.js, ratelimit-log.js, and auth.js all +// resolve their ccxray home/logs paths from here, so the startup banner, +// legacy-migration target, and every other consumer stay in sync and can no +// longer drift from where logs actually get written. // // Precedence (mirrors the storage adapter's historical resolution): // logs dir : LOGS_DIR > /logs diff --git a/server/ratelimit-log.js b/server/ratelimit-log.js index 78bf50c..632db59 100644 --- a/server/ratelimit-log.js +++ b/server/ratelimit-log.js @@ -1,14 +1,14 @@ 'use strict'; const fs = require('fs'); -const os = require('os'); const path = require('path'); +const { resolveCcxrayHome } = require('./paths'); // Append-only log of `anthropic-ratelimit-*` headers observed from upstream responses. // Used for future calibration of plan-specific limits (Max 5x vs 20x tokens5h quota). // File: ~/.ccxray/ratelimit-samples.jsonl (respects CCXRAY_HOME env) -const CCXRAY_HOME = process.env.CCXRAY_HOME || path.join(os.homedir(), '.ccxray'); +const CCXRAY_HOME = resolveCcxrayHome(); const SAMPLES_FILE = path.join(CCXRAY_HOME, 'ratelimit-samples.jsonl'); // Dedupe per model: skip writing if headers identical to last sample for diff --git a/server/settings.js b/server/settings.js index 2e320d7..114c670 100644 --- a/server/settings.js +++ b/server/settings.js @@ -2,9 +2,9 @@ const fs = require('fs'); const path = require('path'); -const os = require('os'); +const { resolveCcxrayHome } = require('./paths'); -const SETTINGS_DIR = process.env.CCXRAY_HOME || path.join(os.homedir(), '.ccxray'); +const SETTINGS_DIR = resolveCcxrayHome(); const SETTINGS_PATH = path.join(SETTINGS_DIR, 'settings.json'); const DEFAULTS = {