58 lines
2.2 KiB
JavaScript
58 lines
2.2 KiB
JavaScript
const Holesail = require('holesail');
|
|
const { checkPortAvailability, freePort } = require('../maintenance/cleanup');
|
|
const originalConsole = {
|
|
log: console.log,
|
|
error: console.error,
|
|
warn: console.warn,
|
|
debug: console.debug
|
|
};
|
|
|
|
process.on('message', async (msg) => {
|
|
if (msg.type === 'start') {
|
|
try {
|
|
// Override console methods to send logs via IPC
|
|
console.log = (...args) => process.send({ type: 'log', level: 'info', message: args.join(' ') });
|
|
console.error = (...args) => process.send({ type: 'log', level: 'error', message: args.join(' ') });
|
|
console.warn = (...args) => process.send({ type: 'log', level: 'warn', message: args.join(' ') });
|
|
console.debug = (...args) => process.send({ type: 'log', level: 'debug', message: args.join(' ') });
|
|
|
|
const opts = msg.opts;
|
|
|
|
// Only check and free port for clients, not servers
|
|
if (!opts.server) {
|
|
let portFree = true;
|
|
try {
|
|
await checkPortAvailability(opts.host, opts.port);
|
|
console.debug(`Port ${opts.port} on ${opts.host} is available`);
|
|
} catch (err) {
|
|
console.warn(`Initial port check failed for ${opts.host}:${opts.port}: ${err.message}`);
|
|
console.info(`Attempting to free the port`);
|
|
portFree = await freePort(opts.host, opts.port);
|
|
if (!portFree) {
|
|
throw new Error(`Unable to free port ${opts.port} on ${opts.host}`);
|
|
}
|
|
console.info(`Port ${opts.port} on ${opts.host} freed successfully`);
|
|
}
|
|
} else {
|
|
console.debug(`Skipping port check for server on ${opts.host}:${opts.port}`);
|
|
}
|
|
|
|
const holesail = new Holesail(opts);
|
|
await holesail.ready();
|
|
process.send({ type: 'ready', info: holesail.info });
|
|
// Keep running
|
|
} catch (err) {
|
|
process.send({ type: 'error', message: err.message });
|
|
process.exit(1);
|
|
}
|
|
}
|
|
});
|
|
|
|
// Handle uncaught errors
|
|
process.on('uncaughtException', (err) => {
|
|
process.send({ type: 'log', level: 'error', message: `Uncaught exception: ${err.message}` });
|
|
});
|
|
|
|
process.on('unhandledRejection', (reason) => {
|
|
process.send({ type: 'log', level: 'error', message: `Unhandled rejection: ${reason}` });
|
|
}); |