hifun-tools 1.4.47 → 1.4.49

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.
@@ -15,8 +15,9 @@ declare class InitCls {
15
15
  isRefreshNow: boolean;
16
16
  private localPath;
17
17
  private onlyBackUp;
18
- private OriginBaseUrl;
18
+ OriginBaseUrl: string[];
19
19
  retry: boolean | undefined;
20
+ speedDelay: number;
20
21
  private tenant;
21
22
  private tenantConfig;
22
23
  private tenantDict;
@@ -63,6 +64,13 @@ declare class InitCls {
63
64
  private _fetchLineDict;
64
65
  /** 按顺序依次请求 txt 文件,成功即返回内容,全部失败返回空字符串 */
65
66
  private fetchTxtFile;
67
+ /**
68
+ * 手动切换address线路
69
+ * 直接传入域名
70
+ */
71
+ switchAddressLine(line: string): void;
72
+ /** 手动更新线路速度延迟 */
73
+ updateLineDelay(speed: number): void;
66
74
  /** 获取并处理 lineAddress.txt */
67
75
  private _loadGatewayConfig;
68
76
  }
@@ -23,6 +23,7 @@ class InitCls {
23
23
  onlyBackUp = false;
24
24
  OriginBaseUrl = [];
25
25
  retry = false;
26
+ speedDelay = 99;
26
27
  tenant = "";
27
28
  tenantConfig = null;
28
29
  tenantDict = null;
@@ -335,16 +336,116 @@ class InitCls {
335
336
  // 所有 url 都失败
336
337
  return "";
337
338
  }
339
+ /**
340
+ * 手动切换address线路
341
+ * 直接传入域名
342
+ */
343
+ switchAddressLine(line) {
344
+ this.domainBaseUrl = toStandardUrl(line);
345
+ console.log("切换线路为:", line);
346
+ }
347
+ /** 手动更新线路速度延迟 */
348
+ updateLineDelay(speed) {
349
+ this.speedDelay = speed;
350
+ }
338
351
  /** 获取并处理 lineAddress.txt */
339
352
  async _loadGatewayConfig(retry = false, lineAddressData) {
340
353
  if (this.domainBaseUrl && !retry)
341
354
  return this.domainBaseUrl;
355
+ // 检查 defaultBaseUrl 是否正常
342
356
  if (this.defaultBaseUrl && !retry) {
343
- this.domainBaseUrl = this.defaultBaseUrl;
344
- return this.domainBaseUrl;
357
+ try {
358
+ // 测试 defaultBaseUrl 是否可用
359
+ const startTime = Date.now();
360
+ // 创建 AbortController 实例用于控制超时
361
+ const controller = new AbortController();
362
+ const timeoutId = setTimeout(() => controller.abort(), 3000); // 3秒超时
363
+ const response = await fetch(`${toStandardUrl(this.defaultBaseUrl)}/actuator-security/health?t=${Date.now()}`, {
364
+ method: "GET",
365
+ cache: "no-store",
366
+ signal: controller.signal, // 添加信号以支持超时
367
+ });
368
+ clearTimeout(timeoutId); // 请求完成后清除超时定时器
369
+ const endTime = Date.now();
370
+ this.speedDelay = endTime - startTime; // 记录实际速度(单位ms)
371
+ if (response.ok) {
372
+ const tenantDictList = this.getTenantDictList().filter((a) => !this.ErrorDomainUrl.includes(a.lineGroup));
373
+ this.domainBaseUrl = this.defaultBaseUrl;
374
+ console.info(`✅ defaultBaseUrl 可用,响应时间: ${this.speedDelay}ms`);
375
+ // 尝试获取 OriginBaseUrl 数据,不用于测速,仅用于设置 OriginBaseUrl
376
+ let originBaseUrlRetrieved = false;
377
+ // 1. 尝试使用传入的 lineAddressData
378
+ if (lineAddressData && !originBaseUrlRetrieved) {
379
+ try {
380
+ const originBaseUrl = JSON.parse(AesDecrypt(lineAddressData));
381
+ this.OriginBaseUrl = filterSmartLines(tenantDictList, originBaseUrl);
382
+ if (this.OriginBaseUrl.length > 0)
383
+ originBaseUrlRetrieved = true;
384
+ console.info("✅ 使用传入的 lineAddressData 设置 OriginBaseUrl");
385
+ }
386
+ catch (err) {
387
+ console.info("⚠️ 传入的 lineAddressData 解析失败,尝试其他方式");
388
+ }
389
+ }
390
+ // 2. 尝试获取远程 lineAddress.txt
391
+ if (!originBaseUrlRetrieved) {
392
+ try {
393
+ console.info("🌐 尝试获取远程 lineAddress.txt 设置 OriginBaseUrl...");
394
+ const response = await fetch(`/lineAddress.txt?t=${Date.now()}`);
395
+ if (response.ok) {
396
+ const configText = await response.text();
397
+ const originBaseUrl = JSON.parse(AesDecrypt(configText));
398
+ this.OriginBaseUrl = filterSmartLines(tenantDictList, originBaseUrl);
399
+ if (this.OriginBaseUrl.length > 0)
400
+ originBaseUrlRetrieved = true;
401
+ console.info("✅ 使用远程 lineAddress.txt 设置 OriginBaseUrl");
402
+ }
403
+ }
404
+ catch (err) {
405
+ console.info("⚠️ 获取远程 lineAddress.txt 失败,尝试备用地址");
406
+ }
407
+ }
408
+ // 3. 尝试备用地址
409
+ if (!originBaseUrlRetrieved) {
410
+ try {
411
+ console.info(" ambulance 尝试备用地址设置 OriginBaseUrl...");
412
+ if (this.tenant === "t1sport")
413
+ this.backupAddress = t1BackGateWay;
414
+ const backupRaw = await this.fetchTxtFile(this.backupAddress);
415
+ if (backupRaw) {
416
+ const backupList = JSON.parse(AesDecrypt(backupRaw));
417
+ this.backupAddressList = backupList || [];
418
+ this.OriginBaseUrl = filterSmartLines(tenantDictList, this.backupAddressList);
419
+ if (this.OriginBaseUrl.length > 0)
420
+ originBaseUrlRetrieved = true;
421
+ console.info("✅ 使用备用地址设置 OriginBaseUrl");
422
+ }
423
+ }
424
+ catch (err) {
425
+ console.info("⚠️ 备用地址获取失败");
426
+ }
427
+ }
428
+ // 如果所有方式都失败了,至少保留 defaultBaseUrl
429
+ if (!originBaseUrlRetrieved) {
430
+ this.OriginBaseUrl = [this.defaultBaseUrl];
431
+ console.info("📋 使用 defaultBaseUrl 设置 OriginBaseUrl(兜底方案)");
432
+ }
433
+ return this.domainBaseUrl;
434
+ }
435
+ else {
436
+ console.info(`⚠️ defaultBaseUrl 不可用,HTTP状态: ${response.status},将进入后续选择线路逻辑`);
437
+ }
438
+ }
439
+ catch (err) {
440
+ console.info(`⚠️ defaultBaseUrl 请求失败,将进入后续选择线路逻辑`);
441
+ console.error(err);
442
+ }
345
443
  }
346
444
  const setBaseUrl = async (list) => {
445
+ const startTime = Date.now();
347
446
  this.domainBaseUrl = toStandardUrl(await getOptimalDecodedString(list));
447
+ const endTime = Date.now();
448
+ this.speedDelay = endTime - startTime; // 记录实际速度(单位ms)
348
449
  return this.domainBaseUrl;
349
450
  };
350
451
  /** 封装:根据传入的加密字符串执行测速流程 */
@@ -393,7 +494,7 @@ class InitCls {
393
494
  }
394
495
  // 3️⃣ 降级策略:如果传入数据和远程文件都测速失败,进入备用/App兜底
395
496
  try {
396
- console.info("🚑 尝试最终降级逻辑 (备用地址/App配置)...");
497
+ console.info(" ambulance 尝试最终降级逻辑 (备用地址/App配置)...");
397
498
  // 这里执行你原本代码里的 loadBackupAddress 等逻辑
398
499
  if (this.getIsApp() && this.appGateway) {
399
500
  this.domainBaseUrl = toStandardUrl(this.appGateway);
@@ -406,6 +507,7 @@ class InitCls {
406
507
  if (backupRaw) {
407
508
  const backupList = JSON.parse(AesDecrypt(backupRaw));
408
509
  this.backupAddressList = backupList || [];
510
+ this.OriginBaseUrl = this.backupAddressList; // 设置OriginBaseUrl
409
511
  await setBaseUrl(this.backupAddressList);
410
512
  return this.domainBaseUrl;
411
513
  }
@@ -413,7 +515,9 @@ class InitCls {
413
515
  catch (finalErr) {
414
516
  // 最终无奈的保底:取第一个已知的 URL
415
517
  this.domainBaseUrl =
416
- this.defaultBaseUrl || toStandardUrl(this.OriginBaseUrl[0]) || (location.origin + '/api');
518
+ this.defaultBaseUrl ||
519
+ toStandardUrl(this.OriginBaseUrl[0]) ||
520
+ location.origin + "/api";
417
521
  this.onlyBackUp = true;
418
522
  console.warn("⚠️ 所有链路均不可用,使用最后保底地址:", this.domainBaseUrl);
419
523
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.4.46";
1
+ export declare const VERSION = "1.4.48";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.4.46";
1
+ export const VERSION = "1.4.48";
package/package.json CHANGED
@@ -1,37 +1,37 @@
1
- {
2
- "name": "hifun-tools",
3
- "version": "1.4.47",
4
- "main": "dist/index.js",
5
- "module": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "publishConfig": {
8
- "access": "public"
9
- },
10
- "scripts": {
11
- "build": "node scripts/generate-version.js && tsc",
12
- "pub": "npm run build && npm version patch && npm publish --access public",
13
- "test": "jest",
14
- "test:watch": "jest --watch"
15
- },
16
- "files": [
17
- "dist"
18
- ],
19
- "devDependencies": {
20
- "@babel/core": "^7.28.5",
21
- "@babel/preset-typescript": "^7.28.5",
22
- "@types/jest": "^30.0.0",
23
- "@types/lodash-es": "^4.17.12",
24
- "jest": "^30.2.0",
25
- "jest-environment-jsdom": "^30.2.0",
26
- "jscodeshift": "^17.3.0",
27
- "prettier": "^3.6.2",
28
- "ts-jest": "^29.4.5",
29
- "typescript": "^5.6.3"
30
- },
31
- "dependencies": {
32
- "crypto-js": "^4.2.0",
33
- "lodash-es": "^4.17.22",
34
- "md5": "^2.3.0",
35
- "tldts": "^7.0.28"
36
- }
37
- }
1
+ {
2
+ "name": "hifun-tools",
3
+ "version": "1.4.49",
4
+ "main": "dist/index.js",
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "scripts": {
11
+ "build": "node scripts/generate-version.js && tsc",
12
+ "pub": "npm run build && npm version patch && npm publish --access public",
13
+ "test": "jest",
14
+ "test:watch": "jest --watch"
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "devDependencies": {
20
+ "@babel/core": "^7.28.5",
21
+ "@babel/preset-typescript": "^7.28.5",
22
+ "@types/jest": "^30.0.0",
23
+ "@types/lodash-es": "^4.17.12",
24
+ "jest": "^30.2.0",
25
+ "jest-environment-jsdom": "^30.2.0",
26
+ "jscodeshift": "^17.3.0",
27
+ "prettier": "^3.6.2",
28
+ "ts-jest": "^29.4.5",
29
+ "typescript": "^5.6.3"
30
+ },
31
+ "dependencies": {
32
+ "crypto-js": "^4.2.0",
33
+ "lodash-es": "^4.17.22",
34
+ "md5": "^2.3.0",
35
+ "tldts": "^7.0.28"
36
+ }
37
+ }