icoa-cli 2.19.709 → 2.19.711

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ import chalk from"chalk";import{logCommand as o}from"../lib/logger.js";import{asciiOnly as e,tx as t}from"../lib/guide-panel.js";import{ENTRIES as n,currentEntry as r,loadSite as l,readEntryChoice as s,recommend as a,setEntry as i}from"../lib/site.js";export{a as recommend};const c={relay:"relay",proxy:"relay",2:"relay",hk:"relay",direct:"direct",default:"direct",off:"direct",normal:"direct",1:"direct",auto:"auto",best:"auto",fast:"auto","自动":"auto",status:"status",test:"status",list:"status"},y=()=>e()?"[ok]":"✅";async function h(o){const e=Date.now();try{const t=await fetch(`${o}/api/icoa/server-time?t=${e}`,{signal:AbortSignal.timeout(8e3)});return await t.arrayBuffer(),t.ok?Date.now()-e:null}catch{return null}}async function u(o){const e=[];for(let t=0;t<3;t++){const t=await h(n[o].base);null!==t&&e.push(t)}return e.sort((o,e)=>o-e),{key:o,ms:e.length?e[Math.floor(e.length/2)]:null,ok:e.length}}const g={direct:{en:"straight to the server",zh:"直连服务器"},relay:{en:"through the relay — for slow or lossy networks",zh:"经中转 —— 适合慢或丢包的网络"}};function d(o){if(null===o.ms)return chalk.red(t({en:"no answer",zh:"无响应"}));const e=3-o.ok,n=e?chalk.yellow(t({en:` ${e} of 3 lost`,zh:` 3 次丢 ${e} 次`})):"";return chalk.white(`${o.ms} ms`)+n}function f(){for(const o of["ICOA_LEARN_SERVER","ICOA_EXAM_SERVER"])if(process.env[o]?.trim())return`${o} is set — it wins over the entry for that service.`;const o=l();return o.ok?`venue profile "${o.profile.name}" is active — it wins over the entry until it expires.`:null}function w(){console.log(chalk.gray(t({en:" Applies the next time icoa starts — if you are inside icoa now: exit, then icoa.",zh:" 下次启动 icoa 时生效 —— 如果你现在就在 icoa 里:exit,再敲 icoa。"})))}export async function runEntry(l){o("entry"+(l?` ${l}`:""));const h=(l||"status").trim().toLowerCase(),$=c[h];return $?"status"===$?async function(){const o=e()?"[i]":"📖",n=r();console.log(),console.log(chalk.bold.white(` ${o} `)+chalk.bold.white(t({en:"Entry — which door icoa uses to reach the server",zh:"入口 —— icoa 走哪扇门连服务器"}))),console.log(chalk.gray(t({en:" Same server either way: your tokens, progress and scores do not change.",zh:" 两扇门后面是同一台服务器:token、进度、成绩都不变。"}))),console.log(chalk.gray(t({en:" Measuring both doors…",zh:" 正在测两扇门的速度…"})));const[l,i]=await Promise.all([u("direct"),u("relay")]);console.log();for(const o of[l,i]){const r=o.key===n,l=r?chalk.cyan(e()?" * ":" ● "):" ",s=chalk.cyan(o.key.padEnd(7)),a=chalk.gray(t(g[o.key])),i=r?chalk.cyan(t({en:e()?" <- in use":" ← in use",zh:" ← 正在用"})):"";console.log(` ${l}${s} ${d(o)} ${a}${i}`)}console.log();const c=f();if(c&&console.log(chalk.yellow(` ⚠ ${c}`)),null===l.ms&&null===i.ms)return console.log(chalk.yellow(t({en:" Neither door answered — this is the network here, not the entry.",zh:" 两扇门都没响应 —— 是这里的网络问题,不是入口的问题。"}))),console.log(chalk.green(t({en:" ▸ Type: doctor",zh:" ▸ 敲: doctor"}))),void console.log();console.log(chalk.gray("user"===s().by?t({en:" Fixed by you — icoa will not change it. Automatic again: ",zh:" 由你固定 —— icoa 不会改它。改回自动: "}):t({en:" Automatic — icoa re-checks about once a day. Run it now: ",zh:" 自动 —— icoa 大约每天复查一次。现在就测: "}))+chalk.cyan("entry auto"));const h=a(l,i,n);h===n?(console.log(chalk.white(t({en:` ${y()} ${n} is the right door from here — nothing to change.`,zh:` ${y()} 从这里走 ${n} 就是对的 —— 不用改。`}))),console.log(chalk.gray(t({en:" Other door: ",zh:" 另一扇门: "}))+chalk.cyan("entry "+("relay"===n?"direct":"relay")))):(console.log(chalk.white(t({en:` ${h} is clearly better from this network.`,zh:` 从这个网络走 ${h} 明显更好。`}))),console.log(chalk.green(t({en:" ▸ Type: entry auto",zh:" ▸ 敲: entry auto"})))),console.log()}():"auto"===$?async function(){const o=r();console.log(),console.log(chalk.gray(t({en:" Optimizing your network route…",zh:" 网络优化中…"})));const[e,n]=await Promise.all([u("direct"),u("relay")]);if(null===e.ms&&null===n.ms)return console.log(chalk.yellow(t({en:" The server did not answer on any route — this is the network here.",zh:" 两条线路都连不上服务器 —— 是这里的网络问题。"}))),console.log(chalk.green(t({en:" ▸ Type: doctor",zh:" ▸ 敲: doctor"}))),void console.log();const l=a(e,n,o);i(l,"auto"),l!==o?(console.log(chalk.white(t({en:` ${y()} Network optimized — a faster route is ready.`,zh:` ${y()} 网络已优化 —— 已选好更快的线路。`}))),w()):console.log(chalk.white(t({en:` ${y()} Network optimized — you are already on the best route.`,zh:` ${y()} 网络已优化 —— 当前已是最快线路。`})));const s=f();s&&console.log(chalk.yellow(` ⚠ ${s}`)),console.log(chalk.gray(t({en:" icoa re-checks this by itself. Details: ",zh:" icoa 会自己定期复查。详情: "}))+chalk.cyan("entry")),console.log()}():void function(o){const e=r(),l="relay"===o?"direct":"relay";console.log(),e===o?("user"!==s().by&&i(o,"user"),console.log(chalk.white(t({en:` ${y()} Already using ${o} (${n[o].label}) — now fixed.`,zh:` ${y()} 已经在用 ${o}(${"relay"===o?"中转":"直连"}),已固定。`})))):(i(o,"user"),console.log(chalk.white(t({en:` ${y()} Entry set to ${o} (${n[o].label}).`,zh:` ${y()} 入口已设为 ${o}(${"relay"===o?"中转":"直连"})。`}))),w());const a=f();a&&console.log(chalk.yellow(` ⚠ ${a}`)),console.log(chalk.gray(t({en:" Other door: ",zh:" 另一扇门: "}))+chalk.cyan(`entry ${l}`)+chalk.gray(t({en:" · automatic again: ",zh:" · 改回自动: "}))+chalk.cyan("entry auto")),console.log()}($):(console.log(),console.log(chalk.yellow(t({en:` "${l}" is not an entry.`,zh:` 「${l}」不是一个入口。`}))+chalk.gray(t({en:" Use one of: ",zh:" 可用: "}))+chalk.cyan("entry")+chalk.gray(" · ")+chalk.cyan("entry auto")+chalk.gray(" · ")+chalk.cyan("entry relay")+chalk.gray(" · ")+chalk.cyan("entry direct")),void console.log())}export function registerEntryCommand(o){o.command("entry [door]").description("Choose the route to the ICOA server: auto (measure and pick) · direct (default) · relay").action(runEntry)}