mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-12 20:15:40 +00:00
fix(cliproxy): address edge cases in sync module
- Reset isSyncing flag in stopAutoSyncWatcher (prevents stale state) - Validate empty profile names in mapProfileToClaudeKey - Add whitespace validation in API routes (POST/DELETE /aliases) - Add Number.isInteger check for port validation - Wrap response.json() in try-catch for React hooks (handles non-JSON 502)
This commit is contained in:
1 parent
75a4e68f95
commit
9924b2fb25
5 files changed
+69
-22
No files matched your search
@@ -28,7 +28,7 @@ const DEFAULT_HTTPS_PORT = 443;
|
|||||||
* Get effective port based on config and protocol.
|
* Get effective port based on config and protocol.
|
||||||
*/
|
*/
|
||||||
function getEffectivePort(port: number | undefined, protocol: 'http' | 'https'): number {
|
function getEffectivePort(port: number | undefined, protocol: 'http' | 'https'): number {
|
||||||
if (port !== undefined && port > 0 && port <= 65535) {
|
if (port !== undefined && Number.isInteger(port) && port > 0 && port <= 65535) {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
return protocol === 'https' ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
|
return protocol === 'https' ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
|
||||||
|
|||||||
@@ -152,6 +152,9 @@ export async function stopAutoSyncWatcher(): Promise<void> {
|
|||||||
watcherInstance = null;
|
watcherInstance = null;
|
||||||
log('Watcher stopped');
|
log('Watcher stopped');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset flag to prevent stale state
|
||||||
|
isSyncing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -111,7 +111,11 @@ export function mapProfileToClaudeKey(profile: SyncableProfile): ClaudeKey | nul
|
|||||||
const baseUrl = env.ANTHROPIC_BASE_URL;
|
const baseUrl = env.ANTHROPIC_BASE_URL;
|
||||||
|
|
||||||
// Generate prefix from profile name (e.g., "glm" -> "glm-")
|
// Generate prefix from profile name (e.g., "glm" -> "glm-")
|
||||||
const prefix = `${sanitizeProfileName(profile.name)}-`;
|
const sanitizedName = sanitizeProfileName(profile.name);
|
||||||
|
if (!sanitizedName || sanitizedName === '') {
|
||||||
|
return null; // Skip profiles with invalid names
|
||||||
|
}
|
||||||
|
const prefix = `${sanitizedName}-`;
|
||||||
|
|
||||||
// Load model aliases for this profile
|
// Load model aliases for this profile
|
||||||
const aliases = getProfileAliases(profile.name);
|
const aliases = getProfileAliases(profile.name);
|
||||||
|
|||||||
@@ -127,14 +127,17 @@ router.get('/aliases', async (_req: Request, res: Response): Promise<void> => {
|
|||||||
router.post('/aliases', async (req: Request, res: Response): Promise<void> => {
|
router.post('/aliases', async (req: Request, res: Response): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { profile, from, to } = req.body;
|
const { profile, from, to } = req.body;
|
||||||
|
const trimmedProfile = typeof profile === 'string' ? profile.trim() : '';
|
||||||
|
const trimmedFrom = typeof from === 'string' ? from.trim() : '';
|
||||||
|
const trimmedTo = typeof to === 'string' ? to.trim() : '';
|
||||||
|
|
||||||
if (!profile || !from || !to) {
|
if (!trimmedProfile || !trimmedFrom || !trimmedTo) {
|
||||||
res.status(400).json({ error: 'Missing required fields: profile, from, to' });
|
res.status(400).json({ error: 'Missing required fields: profile, from, to' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
addProfileAlias(profile, from, to);
|
addProfileAlias(trimmedProfile, trimmedFrom, trimmedTo);
|
||||||
res.json({ success: true, profile, from, to });
|
res.json({ success: true, profile: trimmedProfile, from: trimmedFrom, to: trimmedTo });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: (error as Error).message });
|
res.status(500).json({ error: (error as Error).message });
|
||||||
}
|
}
|
||||||
@@ -148,14 +151,16 @@ router.post('/aliases', async (req: Request, res: Response): Promise<void> => {
|
|||||||
router.delete('/aliases', async (req: Request, res: Response): Promise<void> => {
|
router.delete('/aliases', async (req: Request, res: Response): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const { profile, from } = req.body;
|
const { profile, from } = req.body;
|
||||||
|
const trimmedProfile = typeof profile === 'string' ? profile.trim() : '';
|
||||||
|
const trimmedFrom = typeof from === 'string' ? from.trim() : '';
|
||||||
|
|
||||||
if (!profile || !from) {
|
if (!trimmedProfile || !trimmedFrom) {
|
||||||
res.status(400).json({ error: 'Missing required fields: profile, from' });
|
res.status(400).json({ error: 'Missing required fields: profile, from' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const removed = removeProfileAlias(profile, from);
|
const removed = removeProfileAlias(trimmedProfile, trimmedFrom);
|
||||||
res.json({ success: removed, profile, from });
|
res.json({ success: removed, profile: trimmedProfile, from: trimmedFrom });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: (error as Error).message });
|
res.status(500).json({ error: (error as Error).message });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,8 +66,14 @@ export interface AliasesResponse {
|
|||||||
async function fetchSyncStatus(): Promise<SyncStatus> {
|
async function fetchSyncStatus(): Promise<SyncStatus> {
|
||||||
const response = await fetch('/api/cliproxy/sync/status');
|
const response = await fetch('/api/cliproxy/sync/status');
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const error = await response.json();
|
let message = 'Failed to fetch sync status';
|
||||||
throw new Error(error.message || 'Failed to fetch sync status');
|
try {
|
||||||
|
const error = await response.json();
|
||||||
|
message = error.error || error.message || message;
|
||||||
|
} catch {
|
||||||
|
// Non-JSON response (e.g., 502 Bad Gateway)
|
||||||
|
}
|
||||||
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
}
|
}
|
||||||
@@ -78,8 +84,14 @@ async function fetchSyncStatus(): Promise<SyncStatus> {
|
|||||||
async function fetchSyncPreview(): Promise<SyncPreview> {
|
async function fetchSyncPreview(): Promise<SyncPreview> {
|
||||||
const response = await fetch('/api/cliproxy/sync/preview');
|
const response = await fetch('/api/cliproxy/sync/preview');
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const error = await response.json();
|
let message = 'Failed to fetch sync preview';
|
||||||
throw new Error(error.message || 'Failed to fetch sync preview');
|
try {
|
||||||
|
const error = await response.json();
|
||||||
|
message = error.error || error.message || message;
|
||||||
|
} catch {
|
||||||
|
// Non-JSON response (e.g., 502 Bad Gateway)
|
||||||
|
}
|
||||||
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
}
|
}
|
||||||
@@ -93,13 +105,18 @@ async function executeSync(): Promise<SyncResult> {
|
|||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(data.error || 'Sync failed');
|
let message = 'Sync failed';
|
||||||
|
try {
|
||||||
|
const data = await response.json();
|
||||||
|
message = data.error || data.message || message;
|
||||||
|
} catch {
|
||||||
|
// Non-JSON response (e.g., 502 Bad Gateway)
|
||||||
|
}
|
||||||
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,8 +125,14 @@ async function executeSync(): Promise<SyncResult> {
|
|||||||
async function fetchAliases(): Promise<AliasesResponse> {
|
async function fetchAliases(): Promise<AliasesResponse> {
|
||||||
const response = await fetch('/api/cliproxy/sync/aliases');
|
const response = await fetch('/api/cliproxy/sync/aliases');
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const error = await response.json();
|
let message = 'Failed to fetch aliases';
|
||||||
throw new Error(error.message || 'Failed to fetch aliases');
|
try {
|
||||||
|
const error = await response.json();
|
||||||
|
message = error.error || error.message || message;
|
||||||
|
} catch {
|
||||||
|
// Non-JSON response (e.g., 502 Bad Gateway)
|
||||||
|
}
|
||||||
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
}
|
}
|
||||||
@@ -129,8 +152,14 @@ async function addAlias(params: {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const error = await response.json();
|
let message = 'Failed to add alias';
|
||||||
throw new Error(error.error || 'Failed to add alias');
|
try {
|
||||||
|
const error = await response.json();
|
||||||
|
message = error.error || error.message || message;
|
||||||
|
} catch {
|
||||||
|
// Non-JSON response (e.g., 502 Bad Gateway)
|
||||||
|
}
|
||||||
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.json();
|
return response.json();
|
||||||
@@ -150,8 +179,14 @@ async function removeAlias(params: {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const error = await response.json();
|
let message = 'Failed to remove alias';
|
||||||
throw new Error(error.error || 'Failed to remove alias');
|
try {
|
||||||
|
const error = await response.json();
|
||||||
|
message = error.error || error.message || message;
|
||||||
|
} catch {
|
||||||
|
// Non-JSON response (e.g., 502 Bad Gateway)
|
||||||
|
}
|
||||||
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.json();
|
return response.json();
|
||||||
|
|||||||
Reference in new issue
Block a user