UNPKG

mcard-js

Version:

MCard - Content-addressable storage with cryptographic hashing, handle resolution, and vector search for Node.js and browsers

81 lines 3.1 kB
/** * Standalone WebSocket Server Worker Script. * * This script is spawned by the WebSocket Server builtin to run the actual server process. * It reads configuration from environment variables and uses the 'ws' library. */ import { WebSocketServer } from 'ws'; // Read configuration from environment variables const PORT = parseInt(process.env.PORT || '8765', 10); const HOST = process.env.HOST || 'localhost'; console.log(`Starting WebSocket Server on ${HOST}:${PORT}`); try { const wss = new WebSocketServer({ port: PORT, host: HOST }); wss.on('listening', () => { console.log(`WebSocket CLM Execution Server running on ws://${HOST}:${PORT}/`); }); wss.on('connection', (ws) => { console.log('Client connected'); ws.on('message', (message) => { try { // Determine if message is Buffer or string const msgStr = message.toString(); const data = JSON.parse(msgStr); const msgId = data.messageId; const type = data.type; if (type === 'ping') { ws.send(JSON.stringify({ messageId: msgId, type: 'pong', timestamp: data.timestamp })); } else if (type === 'clm_execute') { // Stub for CLM execution in pure JS Node server // In a real scenario, this would import the mcard-js library dynamically console.log('Received CLM execution request'); ws.send(JSON.stringify({ messageId: msgId, success: true, result: { status: "simulated_success", output: "Node.js CLM Runner (Stub)" }, executionTime: 0, logs: ["Executed on Node.js WebSocket Server"] })); } else { // Echo ws.send(JSON.stringify({ messageId: msgId, type: 'echo', original: msgStr })); } } catch (e) { console.error('Error processing message:', e); ws.send(JSON.stringify({ error: 'Invalid JSON or Internal Error: ' + e.toString() })); } }); }); wss.on('error', (e) => { console.error('Server error:', e); process.exit(1); }); // Handle graceful shutdown const cleanup = () => { console.log('Stopping WebSocket server...'); wss.close(() => { console.log('WebSocket server stopped.'); process.exit(0); }); }; process.on('SIGTERM', cleanup); process.on('SIGINT', cleanup); } catch (e) { console.error('Failed to create server:', e); process.exit(1); } // Keep process alive if server is close setInterval(() => { }, 1000); //# sourceMappingURL=websocket-server-worker.js.map