hifun-tools 1.4.20 → 1.4.21

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.
@@ -49,6 +49,7 @@ declare class InitCls {
49
49
  private _getTenantInfoStrictSync;
50
50
  /** 匹配默认租户 */
51
51
  private _matchDefaultTenant;
52
+ retryHttp(): Promise<void>;
52
53
  refreshHttp(): Promise<void>;
53
54
  /** 获取 lineDict.txt 内容 */
54
55
  private _fetchLineDict;
@@ -158,6 +158,14 @@ class InitCls {
158
158
  }
159
159
  throw new Error("无法获取有效的租户信息");
160
160
  }
161
+ async retryHttp() {
162
+ try {
163
+ await getOptimalDecodedString([this.getBaseUrl()]);
164
+ }
165
+ catch (error) {
166
+ await this._loadGatewayConfig(true);
167
+ }
168
+ }
161
169
  async refreshHttp() {
162
170
  const startTime = Date.now();
163
171
  const log = (msg) => {
@@ -193,7 +201,7 @@ class InitCls {
193
201
  return true;
194
202
  }
195
203
  catch (err) {
196
- loadingText("网络波动,正在优选线路...");
204
+ loadingText("正在为您检测最优线路,请稍等...");
197
205
  log(`${label}:请求失败 ❌`);
198
206
  return false;
199
207
  }
@@ -222,6 +230,7 @@ class InitCls {
222
230
  const nowLineGroup = this.tenantDictList.find((a) => toStandardUrl(a.line) == toStandardUrl(this.domainBaseUrl))?.lineGroup;
223
231
  if (!!nowLineGroup)
224
232
  Cache("ErrorDomainUrl", uniq([...this.ErrorDomainUrl, nowLineGroup]));
233
+ closeLoadingText();
225
234
  rewardMsg({
226
235
  title: "提示",
227
236
  text: "优选线路超时,刷新页面以重置" + (nowLineGroup ? "" : "..."),
@@ -271,56 +280,74 @@ class InitCls {
271
280
  return "";
272
281
  }
273
282
  /** 获取并处理 lineAddress.txt */
274
- async _loadGatewayConfig() {
275
- if (this.domainBaseUrl)
283
+ async _loadGatewayConfig(retry = false) {
284
+ if (this.domainBaseUrl && !retry)
285
+ return this.domainBaseUrl;
286
+ /** 设置 domainBaseUrl 并返回 */
287
+ const setBaseUrl = async (list) => {
288
+ this.domainBaseUrl = toStandardUrl(await getOptimalDecodedString(list));
276
289
  return this.domainBaseUrl;
290
+ };
291
+ /** 备用 Address 策略 */
292
+ const loadBackupAddress = async () => {
293
+ const backupList = JSON.parse(AesDecrypt(await this.fetchTxtFile(this.backupAddress)));
294
+ this.backupAddressList = backupList || [];
295
+ await setBaseUrl(this.backupAddressList);
296
+ console.info("✅ 备用远程Address配置成功:", this.domainBaseUrl);
297
+ };
277
298
  try {
299
+ /** 1️⃣ 读取主配置 */
278
300
  const response = await fetch(`/lineAddress.txt?t=${Date.now()}`);
279
301
  const configText = await response.text();
280
- const resArray = JSON.parse(AesDecrypt(configText));
281
- let OriginBaseUrl = resArray;
282
- this.OriginBaseUrl = OriginBaseUrl;
283
- const dictList = this.getTenantDictList().filter((a) => !this.ErrorDomainUrl.includes(a.lineGroup));
284
- const lineGroup = this.getTenantDict()?.lineGroup;
285
- let baseUrl = filterSmartLines(dictList, OriginBaseUrl, this.ErrorDomainUrl?.length == 0 ? lineGroup : "");
302
+ const originBaseUrl = JSON.parse(AesDecrypt(configText));
303
+ this.OriginBaseUrl = originBaseUrl;
304
+ /** 2️⃣ 计算智能线路 */
305
+ const tenantDictList = this.getTenantDictList().filter((a) => !this.ErrorDomainUrl.includes(a.lineGroup));
306
+ const currentLineGroup = this.getTenantDict()?.lineGroup;
307
+ let baseUrlList = filterSmartLines(tenantDictList, originBaseUrl, this.ErrorDomainUrl?.length === 0 ? currentLineGroup : "");
308
+ /** 3️⃣ 主线路测速 */
286
309
  try {
287
- this.domainBaseUrl = toStandardUrl(await getOptimalDecodedString(baseUrl));
310
+ await setBaseUrl(baseUrlList);
288
311
  console.info("✅ 成功加载生产环境配置:", this.domainBaseUrl);
289
312
  }
290
- catch (error) {
291
- const backUrl = JSON.parse(AesDecrypt(await this.fetchTxtFile(this.backupAddress)));
292
- this.backupAddressList = backUrl || [];
293
- // 备用策略
313
+ catch {
314
+ /** 4️⃣ 降级策略 */
294
315
  try {
295
- this.domainBaseUrl = toStandardUrl(await getOptimalDecodedString(backUrl));
296
- console.info("✅ 备用配置成功:", this.domainBaseUrl);
316
+ if (this.ErrorDomainUrl?.length == 0) {
317
+ const fallbackList = filterSmartLines(tenantDictList.filter((a) => a.lineGroup !== currentLineGroup), originBaseUrl);
318
+ await setBaseUrl(fallbackList);
319
+ }
320
+ else {
321
+ await loadBackupAddress();
322
+ }
297
323
  }
298
- catch (err) {
299
- this.domainBaseUrl = toStandardUrl(backUrl?.length > 0 ? backUrl[0] : OriginBaseUrl[0]);
300
- console.warn("⚠️ 备选测速失败,使用备用配置:", this.domainBaseUrl);
324
+ catch {
325
+ try {
326
+ await loadBackupAddress();
327
+ }
328
+ catch {
329
+ this.domainBaseUrl = toStandardUrl(originBaseUrl[0]);
330
+ console.warn("⚠️ 备选测速失败,使用备用配置:", this.domainBaseUrl);
331
+ }
301
332
  }
302
333
  }
303
334
  return this.domainBaseUrl;
304
335
  }
305
336
  catch (err) {
306
- if (this.getIsApp()) {
307
- if (this.appGateway) {
308
- this.domainBaseUrl = toStandardUrl(this.appGateway);
309
- console.info("🏠 使用app传入网关:", this.domainBaseUrl);
310
- return this.domainBaseUrl;
311
- }
312
- }
313
- else {
314
- this.domainBaseUrl =
315
- this.defaultBaseUrl ||
316
- toStandardUrl(this.backupAddressList?.length > 0
317
- ? this.backupAddressList[0]
318
- : this.OriginBaseUrl[0]);
319
- console.info("🏠 使用默认 BaseUrl1:", this.domainBaseUrl);
337
+ /** 5️⃣ 最终兜底 */
338
+ if (this.getIsApp() && this.appGateway) {
339
+ this.domainBaseUrl = toStandardUrl(this.appGateway);
340
+ console.info("🏠 使用app传入网关:", this.domainBaseUrl);
320
341
  return this.domainBaseUrl;
321
342
  }
343
+ this.domainBaseUrl =
344
+ this.defaultBaseUrl ||
345
+ toStandardUrl(this.backupAddressList?.length > 0
346
+ ? this.backupAddressList[0]
347
+ : this.OriginBaseUrl[0]);
348
+ console.info("🏠 使用默认 BaseUrl:", this.domainBaseUrl);
322
349
  console.error("⚠️ 加载 lineAddress.txt 失败:", err);
323
- return null;
350
+ return this.domainBaseUrl ?? null;
324
351
  }
325
352
  }
326
353
  }
@@ -138,41 +138,59 @@ export async function getOptimalDecodedString(arr, options) {
138
138
  ? targetArr.filter(isIPv4)
139
139
  : targetArr.filter((v) => !isIPv4(v));
140
140
  }
141
+ if (!targetArr.length) {
142
+ throw new Error("过滤后数组为空");
143
+ }
141
144
  const failedUrls = [];
142
145
  const controllerMap = {};
143
146
  return new Promise((resolve, reject) => {
144
147
  let resolved = false;
148
+ let pendingCount = targetArr.length;
149
+ const checkAllFailed = () => {
150
+ if (!resolved && pendingCount === 0) {
151
+ // 所有请求都完成了,但没有一个成功
152
+ if (error) {
153
+ error(failedUrls);
154
+ }
155
+ reject({ message: "测速全部失败", failedUrls });
156
+ }
157
+ };
145
158
  targetArr.forEach((url) => {
146
159
  const controller = new AbortController();
147
160
  controllerMap[url] = controller;
148
161
  const timeoutId = setTimeout(() => {
149
162
  controller.abort();
150
163
  failedUrls.push(url);
164
+ pendingCount--;
165
+ checkAllFailed();
151
166
  }, 3000);
152
167
  fetch(`${toStandardUrl(url)}/actuator-security/health?t=${Date.now()}`, {
153
168
  signal: controller.signal,
154
169
  })
155
- .then(() => {
170
+ .then((response) => {
156
171
  clearTimeout(timeoutId);
172
+ if (!response.ok) {
173
+ throw new Error(`HTTP ${response.status}`);
174
+ }
157
175
  if (!resolved) {
158
176
  resolved = true;
177
+ // 成功时中止所有其他请求
178
+ Object.values(controllerMap).forEach((c) => {
179
+ if (c !== controller)
180
+ c.abort();
181
+ });
159
182
  resolve(url);
160
183
  }
161
184
  })
162
- .catch(() => {
185
+ .catch((err) => {
163
186
  clearTimeout(timeoutId);
164
- failedUrls.push(url);
187
+ if (!failedUrls.includes(url)) {
188
+ failedUrls.push(url);
189
+ }
190
+ pendingCount--;
191
+ checkAllFailed();
165
192
  });
166
193
  });
167
- // 等待所有请求完成
168
- setTimeout(() => {
169
- if (failedUrls.length && error) {
170
- error(failedUrls);
171
- }
172
- if (!resolved) {
173
- reject({ message: "测速全部失败", failedUrls });
174
- }
175
- }, 3500);
176
194
  });
177
195
  }
178
196
  /**
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.4.19";
1
+ export declare const VERSION = "1.4.20";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.4.19";
1
+ export const VERSION = "1.4.20";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hifun-tools",
3
- "version": "1.4.20",
3
+ "version": "1.4.21",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",