|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Runs Alto bundler on port 4338 and CORS proxy on port 4337 |
| 5 | + * This allows browser-based applications to interact with Alto without CORS issues |
| 6 | + */ |
| 7 | + |
| 8 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 9 | +const { spawn } = require("child_process"); |
| 10 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 11 | +const path = require("path"); |
| 12 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 13 | +const net = require("net"); |
| 14 | + |
| 15 | +const ALTO_CONFIG = path.join(__dirname, "..", "alto-with-proxy.json"); |
| 16 | +const PROXY_SCRIPT = path.join(__dirname, "bundler-proxy.js"); |
| 17 | + |
| 18 | +// ANSI color codes |
| 19 | +const colors = { |
| 20 | + reset: "\x1b[0m", |
| 21 | + cyan: "\x1b[36m", |
| 22 | + green: "\x1b[32m", |
| 23 | + yellow: "\x1b[33m", |
| 24 | + red: "\x1b[31m", |
| 25 | +}; |
| 26 | + |
| 27 | +function log(prefix, message, color = colors.reset) { |
| 28 | + console.log(`${color}[${prefix}]${colors.reset} ${message}`); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Check if a port is listening |
| 33 | + * @param {number} port - The port to check |
| 34 | + * @param {number} maxAttempts - Maximum number of attempts |
| 35 | + * @param {number} delayMs - Delay between attempts in milliseconds |
| 36 | + * @returns {Promise<boolean>} - True if port is listening, false otherwise |
| 37 | + */ |
| 38 | +async function waitForPort(port, maxAttempts = 30, delayMs = 1000) { |
| 39 | + for (let attempt = 1; attempt <= maxAttempts; attempt++) { |
| 40 | + const isListening = await new Promise((resolve) => { |
| 41 | + const client = new net.Socket(); |
| 42 | + |
| 43 | + client.once("connect", () => { |
| 44 | + client.destroy(); |
| 45 | + resolve(true); |
| 46 | + }); |
| 47 | + |
| 48 | + client.once("error", () => { |
| 49 | + client.destroy(); |
| 50 | + resolve(false); |
| 51 | + }); |
| 52 | + |
| 53 | + client.connect(port, "localhost"); |
| 54 | + }); |
| 55 | + |
| 56 | + if (isListening) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + if (attempt < maxAttempts) { |
| 61 | + await new Promise((resolve) => setTimeout(resolve, delayMs)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return false; |
| 66 | +} |
| 67 | + |
| 68 | +let proxy; |
| 69 | + |
| 70 | +// Start Alto bundler |
| 71 | +log("SETUP", "Starting Alto bundler on port 4338...", colors.cyan); |
| 72 | +const alto = spawn("alto", ["--config", ALTO_CONFIG], { |
| 73 | + stdio: "inherit", |
| 74 | + shell: true, |
| 75 | +}); |
| 76 | + |
| 77 | +alto.on("error", (error) => { |
| 78 | + log("ALTO", `Failed to start: ${error.message}`, colors.red); |
| 79 | + process.exit(1); |
| 80 | +}); |
| 81 | + |
| 82 | +alto.on("exit", (code) => { |
| 83 | + log("ALTO", `Exited with code ${code}`, colors.yellow); |
| 84 | + // Kill proxy if alto exits |
| 85 | + if (proxy) { |
| 86 | + proxy.kill(); |
| 87 | + } |
| 88 | + process.exit(code); |
| 89 | +}); |
| 90 | + |
| 91 | +// Handle cleanup on exit - register handlers early to avoid race condition |
| 92 | +process.on("SIGINT", () => { |
| 93 | + log("SETUP", "Shutting down...", colors.yellow); |
| 94 | + alto.kill(); |
| 95 | + if (proxy) { |
| 96 | + proxy.kill(); |
| 97 | + } |
| 98 | + process.exit(0); |
| 99 | +}); |
| 100 | + |
| 101 | +process.on("SIGTERM", () => { |
| 102 | + log("SETUP", "Shutting down...", colors.yellow); |
| 103 | + alto.kill(); |
| 104 | + if (proxy) { |
| 105 | + proxy.kill(); |
| 106 | + } |
| 107 | + process.exit(0); |
| 108 | +}); |
| 109 | + |
| 110 | +// Wait for Alto to be ready before starting proxy |
| 111 | +(async () => { |
| 112 | + log("SETUP", "Waiting for Alto bundler to be ready on port 4338...", colors.cyan); |
| 113 | + |
| 114 | + const altoReady = await waitForPort(4338); |
| 115 | + if (!altoReady) { |
| 116 | + log("ALTO", "Failed to start - port 4338 not listening after 30 seconds", colors.red); |
| 117 | + alto.kill(); |
| 118 | + process.exit(1); |
| 119 | + } |
| 120 | + |
| 121 | + log("ALTO", "Ready and listening on port 4338", colors.green); |
| 122 | + log("SETUP", "Starting CORS proxy on port 4337...", colors.cyan); |
| 123 | + |
| 124 | + proxy = spawn("node", [PROXY_SCRIPT], { |
| 125 | + stdio: "inherit", |
| 126 | + shell: true, |
| 127 | + }); |
| 128 | + |
| 129 | + proxy.on("error", (error) => { |
| 130 | + log("PROXY", `Failed to start: ${error.message}`, colors.red); |
| 131 | + alto.kill(); |
| 132 | + process.exit(1); |
| 133 | + }); |
| 134 | + |
| 135 | + proxy.on("exit", (code) => { |
| 136 | + log("PROXY", `Exited with code ${code}`, colors.yellow); |
| 137 | + // Kill alto if proxy exits |
| 138 | + alto.kill(); |
| 139 | + process.exit(code); |
| 140 | + }); |
| 141 | + |
| 142 | + // Wait for proxy to be ready |
| 143 | + log("SETUP", "Waiting for CORS proxy to be ready on port 4337...", colors.cyan); |
| 144 | + const proxyReady = await waitForPort(4337); |
| 145 | + if (!proxyReady) { |
| 146 | + log("PROXY", "Failed to start - port 4337 not listening after 30 seconds", colors.red); |
| 147 | + alto.kill(); |
| 148 | + proxy.kill(); |
| 149 | + process.exit(1); |
| 150 | + } |
| 151 | + |
| 152 | + log("PROXY", "Ready and listening on port 4337", colors.green); |
| 153 | + log("SETUP", "Both services started successfully!", colors.green); |
| 154 | + log("SETUP", "Alto bundler: http://localhost:4338", colors.green); |
| 155 | + log("SETUP", "CORS proxy: http://localhost:4337", colors.green); |
| 156 | +})(); |
0 commit comments