import { BridgeService } from './bridge-service.js';
import { v4 as uuidv4 } from 'uuid';
export class ProxyBridgeService extends BridgeService {
private primaryBaseUrl: string;
readonly proxyInstanceId: string;
private proxyRequestTimeout = 30000;
constructor(primaryBaseUrl: string) {
super();
this.primaryBaseUrl = primaryBaseUrl;
this.proxyInstanceId = uuidv4();
}
override async sendRequest(endpoint: string, data: any): Promise<any> {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.proxyRequestTimeout);
try {
const response = await fetch(`${this.primaryBaseUrl}/proxy`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ endpoint, data, proxyInstanceId: this.proxyInstanceId }),
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!response.ok) {
const body = await response.text();
throw new Error(`Proxy request failed (${response.status}): ${body}`);
}
const result = await response.json() as { response?: any; error?: string };
if (result.error) {
throw new Error(result.error);
}
return result.response;
} catch (err: any) {
clearTimeout(timeoutId);
if (err.name === 'AbortError') {
throw new Error('Proxy request timeout');
}
throw err;
}
}
override cleanupOldRequests(): void {
// No-op: primary bridge owns the pending request state
}
override clearAllPendingRequests(): void {
// No-op: primary bridge owns the pending request state
}
}