(apiKey: string)
| 40 | } |
| 41 | |
| 42 | export async function detectRegion(apiKey: string): Promise<Region> { |
| 43 | process.stderr.write("Detecting region..."); |
| 44 | const regions = Object.keys(REGIONS) as Region[]; |
| 45 | const results = await Promise.all( |
| 46 | regions.map(async (r) => ({ |
| 47 | region: r, |
| 48 | ok: await probeRegion(r, apiKey, 5000), |
| 49 | })), |
| 50 | ); |
| 51 | const match = results.find((r) => r.ok); |
| 52 | if (!match) { |
| 53 | process.stderr.write(" failed\n"); |
| 54 | process.stderr.write( |
| 55 | `Warning: API key failed validation against all regions (global, cn).\n` + |
| 56 | ` This usually means the API key is invalid or the network is blocking requests.\n` + |
| 57 | ` Falling back to 'global'. Subsequent requests may fail.\n` + |
| 58 | ` Run 'mmx auth status' to verify your credentials.\n`, |
| 59 | ); |
| 60 | return "global"; |
| 61 | } |
| 62 | const detected: Region = match.region; |
| 63 | process.stderr.write(` ${detected}\n`); |
| 64 | return detected; |
| 65 | } |
| 66 | |
| 67 | export async function saveDetectedRegion(region: Region): Promise<void> { |
| 68 | const existing = readConfigFile() as Record<string, unknown>; |
no test coverage detected