Migration Cleanup
This commit is contained in:
parent
d2c014e744
commit
84225a66f9
8 changed files with 224 additions and 354 deletions
65
index.js
65
index.js
|
|
@ -138,9 +138,6 @@ async function main() {
|
|||
// Initialize WebSocket server
|
||||
const wss = new WebSocketServer({ noServer: true });
|
||||
|
||||
// Store WebSocket handlers
|
||||
let wsHandlers = {};
|
||||
|
||||
try {
|
||||
await secureImportModule('checkpoint.js');
|
||||
} catch (e) {
|
||||
|
|
@ -153,10 +150,6 @@ async function main() {
|
|||
}
|
||||
try {
|
||||
await secureImportModule('plugins/proxy.js');
|
||||
const mod = await import('./plugins/proxy.js');
|
||||
if (mod.proxyWebSocketHandler) {
|
||||
wsHandlers = mod.proxyWebSocketHandler;
|
||||
}
|
||||
} catch (e) {
|
||||
logs.error('proxy', `Failed to load proxy plugin: ${e}`);
|
||||
}
|
||||
|
|
@ -183,30 +176,19 @@ async function main() {
|
|||
// Apply all plugin middlewares to Express
|
||||
const middlewareHandlers = loadPlugins();
|
||||
middlewareHandlers.forEach(handler => {
|
||||
if (typeof handler === 'function') {
|
||||
// Wrap plugin handlers to work with Express
|
||||
app.use(async (req, res, next) => {
|
||||
try {
|
||||
const result = await handler(req, { upgrade: () => false });
|
||||
if (result instanceof Response) {
|
||||
// Convert Response to Express response
|
||||
res.status(result.status);
|
||||
result.headers.forEach((value, key) => {
|
||||
res.setHeader(key, value);
|
||||
});
|
||||
const body = await result.text();
|
||||
res.send(body);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
} catch (err) {
|
||||
logs.error('server', `Handler error: ${err}`);
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
} else if (handler && handler.middleware) {
|
||||
// If plugin exports Express middleware directly
|
||||
app.use(handler.middleware);
|
||||
if (handler && handler.middleware) {
|
||||
// If plugin exports an object with middleware property
|
||||
if (Array.isArray(handler.middleware)) {
|
||||
// If middleware is an array, apply each one
|
||||
handler.middleware.forEach(mw => app.use(mw));
|
||||
} else {
|
||||
// Single middleware
|
||||
app.use(handler.middleware);
|
||||
}
|
||||
} else if (typeof handler === 'function') {
|
||||
// Legacy function-style handlers (shouldn't exist anymore)
|
||||
logs.warn('server', 'Found legacy function-style plugin handler');
|
||||
app.use(handler);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -221,30 +203,15 @@ async function main() {
|
|||
res.status(500).send(`Server Error: ${err.message}`);
|
||||
});
|
||||
|
||||
// Handle WebSocket upgrades
|
||||
// Handle WebSocket upgrades for http-proxy-middleware
|
||||
server.on('upgrade', (request, socket, head) => {
|
||||
// http-proxy-middleware handles WebSocket upgrades internally
|
||||
// This is just a placeholder for any custom WebSocket handling if needed
|
||||
wss.handleUpgrade(request, socket, head, (ws) => {
|
||||
wss.emit('connection', ws, request);
|
||||
});
|
||||
});
|
||||
|
||||
// WebSocket connection handler
|
||||
if (wsHandlers.open) {
|
||||
wss.on('connection', (ws, request) => {
|
||||
ws.data = {};
|
||||
if (wsHandlers.open) wsHandlers.open(ws);
|
||||
if (wsHandlers.message) {
|
||||
ws.on('message', (message) => wsHandlers.message(ws, message));
|
||||
}
|
||||
if (wsHandlers.close) {
|
||||
ws.on('close', (code, reason) => wsHandlers.close(ws, code, reason));
|
||||
}
|
||||
if (wsHandlers.error) {
|
||||
ws.on('error', (err) => wsHandlers.error(ws, err));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
logs.section('SERVER');
|
||||
const portNumber = Number(process.env.PORT || 3000);
|
||||
server.listen(portNumber, () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue