digital-samba-mcp-server
Version:
Digital Samba MCP Server - Model Context Protocol server for Digital Samba's video conferencing API
133 lines • 4.15 kB
JavaScript
/**
* Digital Samba MCP Server - Connection Manager Module (Simplified)
*
* This is a simplified version that focuses on basic fetch functionality
* without the complexity of connection pools and reconnection logic.
*/
// Node.js built-in modules
import { EventEmitter } from 'events';
// External dependencies
import fetch from 'node-fetch';
// Local modules
import logger from './logger.js';
import { ApiRequestError } from './errors.js';
/**
* Connection state enum
*/
export var ConnectionState;
(function (ConnectionState) {
ConnectionState["CONNECTING"] = "connecting";
ConnectionState["CONNECTED"] = "connected";
ConnectionState["DISCONNECTED"] = "disconnected";
ConnectionState["RECONNECTING"] = "reconnecting";
ConnectionState["ERROR"] = "error";
})(ConnectionState || (ConnectionState = {}));
/**
* Simplified Connection Manager class
*/
export class ConnectionManager extends EventEmitter {
/**
* Connection Manager constructor
* @param options Connection manager options
*/
constructor(options) {
super();
// Set default options
this.options = {
apiUrl: options.apiUrl,
fetchImpl: options.fetchImpl || fetch,
poolSize: options.poolSize || 1 // Default to 1 connection if not specified
};
// Set fetch implementation
this._fetch = this.options.fetchImpl;
logger.info('Connection Manager initialized', {
apiUrl: this.options.apiUrl,
poolSize: this.options.poolSize
});
}
/**
* Execute a request
* @param url URL to fetch
* @param options Request options
* @returns Promise resolving to the response
*/
async fetch(url, options = {}) {
// Create full URL if relative
const fullUrl = url.toString().startsWith('http')
? url
: `${this.options.apiUrl}${url.toString()}`;
try {
// Execute request
const response = await this._fetch(fullUrl, options);
// Return response
return response;
}
catch (error) {
// Log error
logger.error('Error making request', {
url: String(fullUrl).split('?')[0], // Log without query params
error: error instanceof Error ? error.message : String(error)
});
// Rethrow as API request error
throw new ApiRequestError(`Request failed: ${error instanceof Error ? error.message : String(error)}`, { cause: error instanceof Error ? error : undefined });
}
}
/**
* Reset the connection manager
*/
reset() {
logger.info('Connection manager reset');
this.emit('reset');
}
/**
* Check if the connection manager is healthy
* @returns True if the connection manager is healthy
*/
isHealthy() {
// Simple health check - in a real implementation, this would check connection status
return true;
}
/**
* Get connection manager statistics
* @returns Connection manager statistics
*/
getStats() {
return {
connections: {
// This would contain actual connection stats in a real implementation
status: 'healthy',
poolSize: this.options.poolSize,
// In a real implementation, we would track active connections
active: 0
}
};
}
/**
* Clean up resources
*/
destroy() {
this.removeAllListeners();
logger.info('Connection manager destroyed');
}
}
/**
* Create a connection manager
* @param apiUrl API URL
* @param options Additional connection manager options
* @returns A new connection manager instance
*/
export function createConnectionManager(apiUrl, options = {}) {
return new ConnectionManager({
apiUrl,
...options
});
}
/**
* Export default connection manager utilities
*/
export default {
ConnectionManager,
createConnectionManager,
ConnectionState
};
//# sourceMappingURL=connection-manager.js.map