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