@aivin-labs/cli 1.0.50 → 1.0.57

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.
@@ -45,6 +45,36 @@ export function resolveServerUrl(input, { fallback = DEFAULT_SERVER_URL } = {})
45
45
  return url.toString().replace(/\/+$/, '');
46
46
  }
47
47
 
48
+ const AIVIN_WEB_BY_ZONE = Object.freeze({
49
+ 'aivin.vn': 'https://app.aivin.vn',
50
+ 'aivin.cloud': 'https://app.aivin.cloud',
51
+ });
52
+
53
+ function aivinZone(hostname) {
54
+ const host = hostname.toLowerCase();
55
+ return Object.keys(AIVIN_WEB_BY_ZONE).find((zone) => host === zone || host.endsWith(`.${zone}`)) || null;
56
+ }
57
+
58
+ /** Web mặc định theo gốc tên miền của máy chủ Aivin; null với máy chủ tự dựng / localhost. */
59
+ export function defaultWebUrl(serverUrl) {
60
+ try {
61
+ return AIVIN_WEB_BY_ZONE[aivinZone(new URL(resolveServerUrl(serverUrl)).hostname)] || null;
62
+ } catch { return null; }
63
+ }
64
+
65
+ /**
66
+ * Chọn địa chỉ web từ `app_url` máy chủ khai. Máy chủ Aivin chính thức chỉ được dùng web cùng gốc tên miền:
67
+ * các máy chủ có thể dùng chung cấu hình nên `app_url` của api.aivin.vn có thể là brain.aivin.cloud.
68
+ */
69
+ export function webUrlForServer(serverUrl, appUrl) {
70
+ const fallback = defaultWebUrl(serverUrl);
71
+ if (!fallback) return appUrl || null;
72
+ try {
73
+ if (aivinZone(new URL(appUrl).hostname) === aivinZone(new URL(fallback).hostname)) return appUrl;
74
+ } catch { /* app_url hỏng thì dùng web mặc định */ }
75
+ return fallback;
76
+ }
77
+
48
78
  export function serverKey(serverUrl) {
49
79
  return createHash('sha256').update(resolveServerUrl(serverUrl)).digest('hex').slice(0, 24);
50
80
  }
@@ -75,4 +105,4 @@ export function serverLabel(serverUrl) {
75
105
  if (url === 'https://api.aivin.vn') return 'VN';
76
106
  if (url.startsWith('http://localhost:') || url.startsWith('http://127.0.0.1:')) return 'Local';
77
107
  return new URL(url).host;
78
- }
108
+ }