1
0
mirror of https://github.com/ZeroCatDev/Classworks.git synced 2026-05-13 19:35:07 +00:00
Classworks/assets/serverRotation-lWUrbaIN.js

2 lines
1.9 KiB
JavaScript

import{n as l}from"./index-dZx5BYJt.js";const a=["https://kv-service.houlang.cloud","https://kv-service.wuyuan.dev"],h=5*60*1e3,g=3e3,c={preferred:null,cachedAt:0,probing:!1};function v(t){c.preferred=t,c.cachedAt=Date.now()}function y(t){return t.response?t.response.status>=500:!0}async function S(t){const n=new AbortController,r=setTimeout(()=>n.abort(),g),s=Date.now();try{return await fetch(`${t}/`,{method:"HEAD",signal:n.signal}),Date.now()-s}catch{return 1/0}finally{clearTimeout(r)}}async function w(){if(!c.probing){c.probing=!0;try{const n=(await Promise.all(a.map(async r=>({url:r,latency:await S(r)})))).filter(r=>r.latency<1/0).sort((r,s)=>r.latency-s.latency);n.length>0&&v(n[0].url)}catch{}finally{c.probing=!1}}}function E(){Date.now()-c.cachedAt>h&&!c.probing&&w().catch(()=>{});const r=c.preferred;return r&&a.includes(r)?[r,...a.filter(s=>s!==r)]:[...a]}function m(t){if(t==="classworkscloud")return E();const n=l("server.domain");return n?[n]:[]}async function R(t,n={}){const r=n.provider||l("server.provider"),s=n.onServerTried,u=typeof s=="function",p=m(r),e=[];let f=null;for(const i of p){e.push({url:i,status:"trying"}),u&&s({url:i,status:"trying",tried:[...e]});try{const o=await t(i);return e[e.length-1].status="success",u&&s({url:i,status:"success",tried:[...e]}),r==="classworkscloud"&&v(i),o}catch(o){if(!y(o))throw e[e.length-1].status="client-error",u&&s({url:i,status:"client-error",error:o,tried:[...e]}),o;f=o,e[e.length-1].status="failed",e[e.length-1].error=o.message||String(o),u&&s({url:i,status:"failed",error:o,tried:[...e]}),console.warn(`Server ${i} failed, trying next:`,o.message)}}console.error("All servers failed. Tried:",e);const d=f||new Error("All servers failed");throw d.triedServers=e,d}function T(){return l("server.provider")==="classworkscloud"?c.preferred||a[0]:l("server.domain")||""}function A(){return l("server.provider")==="classworkscloud"}export{T as g,A as i,R as t};