MCPcopy Create free account
hub / github.com/MigoXLab/coderio / calculateHashPort

Function calculateHashPort

src/tools/launch-tool/utils/dev-server-manager.ts:73–84  ·  view source on GitHub ↗

* Calculate deterministic port from workspace path hash. * Same workspace always gets the same port for consistency. * * @param workspacePath - Absolute path to workspace root * @param basePort - Base port for calculation (default: 5200, avoids common 5173-5180) * @returns Hash-based port (base

(workspacePath: string, basePort: number = 5200)

Source from the content-addressed store, hash-verified

71 * @returns Hash-based port (basePort to basePort + 999)
72 */
73function calculateHashPort(workspacePath: string, basePort: number = 5200): number {
74 // Simple hash function - same input always produces same output
75 let hash = 0;
76 for (let i = 0; i < workspacePath.length; i++) {
77 hash = (hash << 5) - hash + workspacePath.charCodeAt(i);
78 hash = hash & hash; // Convert to 32bit integer
79 }
80
81 // Map to port range (basePort to basePort + 999)
82 const offset = Math.abs(hash) % 1000;
83 return basePort + offset;
84}
85
86/**
87 * Get available port using hash-based selection strategy.

Callers 1

getFreeHashPortFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected