hifun-tools 1.4.35 → 1.4.36

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.
@@ -31,6 +31,7 @@ declare class InitCls {
31
31
  backupAddress?: string[];
32
32
  retry?: boolean;
33
33
  lineDictData?: string;
34
+ lineAddressData?: string;
34
35
  }): Promise<(string | null)[]>;
35
36
  getOnlyBackup(): boolean;
36
37
  setOnlyBackup(value: boolean): void;
@@ -27,7 +27,7 @@ class InitCls {
27
27
  tenantDictList = [];
28
28
  /** 初始化配置入口 */
29
29
  async InitConfig(options) {
30
- const { fileType, defaultBaseUrl, localPath, appLine, appTenant, appGateway = "", backupAddress, retry, lineDictData, // 提取这个参数
30
+ const { fileType, defaultBaseUrl, localPath, appLine, appTenant, appGateway = "", backupAddress, retry, lineDictData, lineAddressData, // ✨ 提取参数
31
31
  } = options;
32
32
  if (defaultBaseUrl)
33
33
  this.defaultBaseUrl = defaultBaseUrl;
@@ -49,7 +49,8 @@ class InitCls {
49
49
  }
50
50
  if (fileType?.includes("lineAddress")) {
51
51
  console.info("初始化 lineAddress...");
52
- results.push(await this._loadGatewayConfig(retry));
52
+ // ✨ 将 lineAddressData 传递给加载方法
53
+ results.push(await this._loadGatewayConfig(retry, lineAddressData));
53
54
  }
54
55
  console.info("✅ 所有初始化完成:", results);
55
56
  return results;
@@ -323,82 +324,84 @@ class InitCls {
323
324
  return "";
324
325
  }
325
326
  /** 获取并处理 lineAddress.txt */
326
- async _loadGatewayConfig(retry = false) {
327
+ async _loadGatewayConfig(retry = false, lineAddressData) {
327
328
  if (this.domainBaseUrl && !retry)
328
329
  return this.domainBaseUrl;
329
- /** 设置 domainBaseUrl 并返回 */
330
330
  const setBaseUrl = async (list) => {
331
331
  this.domainBaseUrl = toStandardUrl(await getOptimalDecodedString(list));
332
332
  return this.domainBaseUrl;
333
333
  };
334
- /** 备用 Address 策略 */
335
- const loadBackupAddress = async () => {
336
- if (this.tenant === "t1sport")
337
- this.backupAddress = t1BackGateWay;
338
- const backupList = JSON.parse(AesDecrypt(await this.fetchTxtFile(this.backupAddress)));
339
- this.backupAddressList = backupList || [];
340
- await setBaseUrl(this.backupAddressList);
341
- console.info("✅ 备用远程Address配置成功:", this.domainBaseUrl);
334
+ /** 封装:根据传入的加密字符串执行测速流程 */
335
+ const trySpeedTestWithData = async (encryptedData, label) => {
336
+ try {
337
+ console.info(`🧪 尝试使用 [${label}] 进行解析和测速...`);
338
+ const originBaseUrl = JSON.parse(AesDecrypt(encryptedData));
339
+ this.OriginBaseUrl = originBaseUrl;
340
+ // 计算智能线路
341
+ const tenantDictList = this.getTenantDictList().filter((a) => !this.ErrorDomainUrl.includes(a.lineGroup));
342
+ const currentLineGroup = this.getTenantDict()?.lineGroup;
343
+ let baseUrlList = filterSmartLines(tenantDictList, originBaseUrl, this.ErrorDomainUrl?.length === 0 ? currentLineGroup : "");
344
+ // 执行测速
345
+ await setBaseUrl(baseUrlList);
346
+ console.info(`✅ [${label}] 测速成功:`, this.domainBaseUrl);
347
+ return true;
348
+ }
349
+ catch (e) {
350
+ console.warn(`⚠️ [${label}] 流程失败 (解析或测速未通过)`);
351
+ return false;
352
+ }
342
353
  };
343
354
  this.onlyBackUp = false;
355
+ // --- 核心执行流 ---
356
+ // 1️⃣ 第一优先级:如果有传入 lineAddressData,先跑一遍完整流程
357
+ if (lineAddressData) {
358
+ const success = await trySpeedTestWithData(lineAddressData, "传入参数 lineAddressData");
359
+ if (success) {
360
+ localStorage.removeItem("retryHttpTime");
361
+ return this.domainBaseUrl;
362
+ }
363
+ }
364
+ // 2️⃣ 第二优先级:如果上面失败了或没传,执行原始 fetch 逻辑
344
365
  try {
345
- /** 1️⃣ 读取主配置 */
366
+ console.info("🌐 正在从服务端请求 lineAddress.txt 作为兜底...");
346
367
  const response = await fetch(`/lineAddress.txt?t=${Date.now()}`);
347
368
  const configText = await response.text();
348
- const originBaseUrl = JSON.parse(AesDecrypt(configText));
349
- this.OriginBaseUrl = originBaseUrl;
350
- /** 2️⃣ 计算智能线路 */
351
- const tenantDictList = this.getTenantDictList().filter((a) => !this.ErrorDomainUrl.includes(a.lineGroup));
352
- const currentLineGroup = this.getTenantDict()?.lineGroup;
353
- let baseUrlList = filterSmartLines(tenantDictList, originBaseUrl, this.ErrorDomainUrl?.length === 0 ? currentLineGroup : "");
354
- /** 3️⃣ 主线路测速 */
355
- try {
356
- await setBaseUrl(baseUrlList);
369
+ const success = await trySpeedTestWithData(configText, "远程文件 lineAddress.txt");
370
+ if (success) {
357
371
  localStorage.removeItem("retryHttpTime");
358
- console.info("✅ 成功加载生产环境配置:", this.domainBaseUrl);
359
- }
360
- catch {
361
- /** 4️⃣ 降级策略 */
362
- try {
363
- if (this.ErrorDomainUrl?.length == 0) {
364
- const fallbackList = filterSmartLines(tenantDictList.filter((a) => a.lineGroup !== currentLineGroup), originBaseUrl);
365
- await setBaseUrl(fallbackList);
366
- localStorage.removeItem("retryHttpTime");
367
- }
368
- else {
369
- await loadBackupAddress();
370
- }
371
- }
372
- catch {
373
- try {
374
- await loadBackupAddress();
375
- }
376
- catch {
377
- this.domainBaseUrl = toStandardUrl(originBaseUrl[0]);
378
- localStorage.removeItem("retryHttpTime");
379
- console.warn("⚠️ 备选测速失败,使用备用配置:", this.domainBaseUrl);
380
- this.onlyBackUp = true;
381
- }
382
- }
372
+ return this.domainBaseUrl;
383
373
  }
384
- return this.domainBaseUrl;
385
374
  }
386
375
  catch (err) {
387
- /** 5️⃣ 最终兜底 */
376
+ console.error("❌ 远程 lineAddress.txt 获取失败", err);
377
+ }
378
+ // 3️⃣ 降级策略:如果传入数据和远程文件都测速失败,进入备用/App兜底
379
+ try {
380
+ console.info("🚑 尝试最终降级逻辑 (备用地址/App配置)...");
381
+ // 这里执行你原本代码里的 loadBackupAddress 等逻辑
388
382
  if (this.getIsApp() && this.appGateway) {
389
383
  this.domainBaseUrl = toStandardUrl(this.appGateway);
390
- console.info("🏠 使用app传入网关:", this.domainBaseUrl);
391
384
  return this.domainBaseUrl;
392
385
  }
386
+ // 尝试备用地址
387
+ if (this.tenant === "t1sport")
388
+ this.backupAddress = t1BackGateWay;
389
+ const backupRaw = await this.fetchTxtFile(this.backupAddress);
390
+ if (backupRaw) {
391
+ const backupList = JSON.parse(AesDecrypt(backupRaw));
392
+ this.backupAddressList = backupList || [];
393
+ await setBaseUrl(this.backupAddressList);
394
+ return this.domainBaseUrl;
395
+ }
396
+ }
397
+ catch (finalErr) {
398
+ // 最终无奈的保底:取第一个已知的 URL
393
399
  this.domainBaseUrl =
394
- this.defaultBaseUrl ||
395
- toStandardUrl(this.backupAddressList?.length > 0
396
- ? this.backupAddressList[0]
397
- : this.OriginBaseUrl[0]);
398
- console.info("🏠 使用默认 BaseUrl:", this.domainBaseUrl);
399
- console.error("⚠️ 加载 lineAddress.txt 失败:", err);
400
- return this.domainBaseUrl ?? null;
400
+ this.defaultBaseUrl || toStandardUrl(this.OriginBaseUrl[0] || "");
401
+ this.onlyBackUp = true;
402
+ console.warn("⚠️ 所有链路均不可用,使用最后保底地址:", this.domainBaseUrl);
401
403
  }
404
+ return this.domainBaseUrl || null;
402
405
  }
403
406
  }
404
407
  const HF = new InitCls();
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.4.34";
1
+ export declare const VERSION = "1.4.35";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.4.34";
1
+ export const VERSION = "1.4.35";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hifun-tools",
3
- "version": "1.4.35",
3
+ "version": "1.4.36",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",