ports.ts (1210B)
1 /** 2 * Predefined port pools for API and Vite dev server. 3 * Override with PORT / VITE_DEV_PORT, or pick a slot with PORT_INDEX (and optional VITE_PORT_INDEX). 4 */ 5 6 export const PREDEFINED_API_PORTS = [38471, 41892, 45203, 47741] as const; 7 export const PREDEFINED_DEV_WEB_PORTS = [38472, 41893, 45204, 47742] as const; 8 9 function clampIndex(i: number, len: number): number { 10 if (!Number.isFinite(i) || i < 0) return 0; 11 if (i >= len) return len - 1; 12 return Math.floor(i); 13 } 14 15 export function resolveApiPort(env: NodeJS.ProcessEnv): number { 16 const explicit = env.PORT?.trim(); 17 if (explicit) { 18 const n = Number(explicit); 19 if (Number.isFinite(n) && n > 0 && n < 65536) return n; 20 } 21 const idx = clampIndex(Number(env.PORT_INDEX), PREDEFINED_API_PORTS.length); 22 return PREDEFINED_API_PORTS[idx]!; 23 } 24 25 export function resolveDevWebPort(env: NodeJS.ProcessEnv): number { 26 const explicit = env.VITE_DEV_PORT?.trim(); 27 if (explicit) { 28 const n = Number(explicit); 29 if (Number.isFinite(n) && n > 0 && n < 65536) return n; 30 } 31 const raw = env.VITE_PORT_INDEX ?? env.PORT_INDEX; 32 const idx = clampIndex(Number(raw), PREDEFINED_DEV_WEB_PORTS.length); 33 return PREDEFINED_DEV_WEB_PORTS[idx]!; 34 }