@endlessblink/like-i-said-v2
Version:
Task Management & Memory for Claude - Track tasks, remember context, and maintain continuity across sessions with 27 powerful tools. Works with Claude Desktop and Claude Code.
83 lines (70 loc) • 2.23 kB
text/typescript
/**
* Discover the API server port dynamically
*/
let cachedPort: number | null = null;
let discoveryPromise: Promise<number> | null = null;
export async function discoverApiPort(): Promise<number> {
// Return cached port if available
if (cachedPort !== null) {
return cachedPort;
}
// Return existing promise if discovery is in progress
if (discoveryPromise !== null) {
return discoveryPromise;
}
// Start discovery
discoveryPromise = performDiscovery();
try {
cachedPort = await discoveryPromise;
return cachedPort;
} finally {
discoveryPromise = null;
}
}
async function performDiscovery(): Promise<number> {
// Try to get port from Vite plugin endpoint
try {
const response = await fetch('/api-port');
if (response.ok) {
const data = await response.json();
if (data.port && typeof data.port === 'number') {
console.log(`🔍 Discovered API port from file: ${data.port}`);
return data.port;
}
}
} catch (error) {
console.warn('Failed to get port from /api-port:', error);
}
// Try common ports in order
const portsToTry = [3002, 3001, 3003, 3004, 3005];
for (const port of portsToTry) {
try {
const response = await fetch(`http://localhost:${port}/api/health`, {
signal: AbortSignal.timeout(1000) // 1 second timeout
});
if (response.ok) {
console.log(`✅ Found API server on port ${port}`);
return port;
}
} catch (error) {
// Port not available, try next
continue;
}
}
// Fallback to default
console.warn('⚠️ Could not discover API port, using default 3002');
return 3002;
}
export function resetPortCache() {
cachedPort = null;
discoveryPromise = null;
}
export async function getApiUrl(path: string = ''): Promise<string> {
const port = await discoverApiPort();
return `http://localhost:${port}${path}`;
}
export async function getWsUrl(): Promise<string> {
const port = await discoverApiPort();
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
return `${protocol}//localhost:${port}`;
}