@alicloud/alfa-core 1.6.0-beta.1 → 1.6.0-beta.10

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/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { createMicroApp } from './createMicroApp';
2
- export { addRequestInterceptor } from './utils/interceptors';
2
+ export { addRequestInterceptor, normalizeCdnUrl } from './utils/interceptors';
3
3
  export { getRelease } from './utils/getRelease';
4
4
  export { getManifest } from './utils/getManifest';
5
5
  export { getConfig, getConfigV2 } from './utils/getConfig';
@@ -15,11 +15,21 @@ export function getChannel(value) {
15
15
  }
16
16
 
17
17
  // 尝试根据域名判断
18
- if (['alibabacloud.com', 'idptcloud01cs.com', 'idptcloud01-inc.com'].find(function (domain) {
18
+ if (['alibabacloud.com'].find(function (domain) {
19
19
  return location.hostname.endsWith(domain);
20
20
  })) {
21
21
  return 'SIN';
22
22
  }
23
+ if (['idptcloud01.com', 'idptcloud01cs.com', 'idptcloud01-inc.com'].find(function (domain) {
24
+ return location.hostname.endsWith(domain);
25
+ })) {
26
+ return 'EA134';
27
+ }
28
+ if (['idptcloud.com'].find(function (domain) {
29
+ return location.hostname.endsWith(domain);
30
+ })) {
31
+ return 'CM10';
32
+ }
23
33
 
24
34
  // 默认返回 OFFICIAL
25
35
  return 'OFFICIAL';
package/es/utils/env.js CHANGED
@@ -1,3 +1,15 @@
1
+ // 无预发环境的域名后缀(一国一云及类似环境)
2
+ var noPrepubDomains = ['idptcloud01.com', 'idptcloud01-inc.com', 'uscloud.alibaba', 'idptcloud03.alibaba', 'idptcloud03-inc.alibaba'];
3
+ var isNoPrepubDomain = function isNoPrepubDomain() {
4
+ try {
5
+ return noPrepubDomains.some(function (domain) {
6
+ return location.hostname.endsWith(domain);
7
+ });
8
+ } catch (e) {
9
+ return false;
10
+ }
11
+ };
12
+
1
13
  /**
2
14
  * get alfa env
3
15
  * @returns
@@ -11,7 +23,11 @@ export var getEnv = function getEnv(value) {
11
23
  return 'local';
12
24
  }
13
25
  }
26
+ var env = ((_window = window) === null || _window === void 0 ? void 0 : (_window$ALIYUN_CONSOL = _window.ALIYUN_CONSOLE_CONFIG) === null || _window$ALIYUN_CONSOL === void 0 ? void 0 : _window$ALIYUN_CONSOL.fEnv) || 'prod';
14
27
 
15
- // default return prod
16
- return ((_window = window) === null || _window === void 0 ? void 0 : (_window$ALIYUN_CONSOL = _window.ALIYUN_CONSOLE_CONFIG) === null || _window$ALIYUN_CONSOL === void 0 ? void 0 : _window$ALIYUN_CONSOL.fEnv) || 'prod';
28
+ // 无预发环境的域名强制使用 prod
29
+ if ((env === 'pre' || env === 'daily') && isNoPrepubDomain()) {
30
+ return 'prod';
31
+ }
32
+ return env;
17
33
  };
@@ -93,7 +93,7 @@ export var getConfig = /*#__PURE__*/function () {
93
93
  */
94
94
  export var getConfigV2 = /*#__PURE__*/function () {
95
95
  var _ref4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(config) {
96
- var releaseConfig, _ref5, relatedConsoleAppId, currentConfig, _ALIYUN_CONSOLE_CONFI, res, _t3;
96
+ var releaseConfig, _ref5, relatedConsoleAppId, currentConfig, _ALIYUN_CONSOLE_CONFI, mainDomain, res, _t3;
97
97
  return _regeneratorRuntime.wrap(function (_context2) {
98
98
  while (1) switch (_context2.prev = _context2.next) {
99
99
  case 0:
@@ -117,8 +117,11 @@ export var getConfigV2 = /*#__PURE__*/function () {
117
117
  return _context2.abrupt("return", currentConfig);
118
118
  case 2:
119
119
  _context2.prev = 2;
120
+ // 内网域名(如 aliyun-inc.com / alibaba-inc.com)的 fecs 服务实际部署在去掉 `-inc` 的公网域名上,
121
+ // 这里剥离一级域名结尾的 `-inc` 后缀,确保 fecs 请求命中正确域名
122
+ mainDomain = parseEnv().MAIN_DOMAIN.replace(/-inc(?=\.[^.]+$)/, '');
120
123
  _context2.next = 3;
121
- return request.get("https://".concat(config.env !== 'prod' ? 'pre-' : '', "fecs.console.").concat(parseEnv().MAIN_DOMAIN, "/api/alfa/console/config?appId=").concat(relatedConsoleAppId), {
124
+ return request.get("https://".concat(config.env !== 'prod' ? 'pre-' : '', "fecs.console.").concat(mainDomain, "/api/alfa/console/config?appId=").concat(relatedConsoleAppId), {
122
125
  withCredentials: true
123
126
  });
124
127
  case 3:
@@ -4,10 +4,32 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
4
4
  import _regeneratorRuntime from "@babel/runtime/regenerator";
5
5
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
6
6
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7
+ import parseEnv from '@alicloud/console-base-conf-parse-env';
7
8
  import cache from './cacheManager';
8
9
  import { getMicroAppRelease } from './oss';
9
10
  import { getEnv } from './env';
10
11
  import { getChannel } from './channel';
12
+ var idptcloud03CdnHost = 'idptcloud03asset.alibaba';
13
+ var getMainResourceCdn = function getMainResourceCdn() {
14
+ try {
15
+ var _parseEnv;
16
+ return ((_parseEnv = parseEnv()) === null || _parseEnv === void 0 ? void 0 : _parseEnv.MAIN_RESOURCE_CDN) || '';
17
+ } catch (e) {
18
+ return '';
19
+ }
20
+ };
21
+ var isIdptcloud03Cdn = function isIdptcloud03Cdn() {
22
+ return getMainResourceCdn().includes(idptcloud03CdnHost);
23
+ };
24
+ var getCwsHost = function getCwsHost(channel) {
25
+ if (isIdptcloud03Cdn()) {
26
+ return "cws.".concat(idptcloud03CdnHost);
27
+ }
28
+ if (['SIN', 'EA134', 'CM10'].includes(channel)) {
29
+ return 'cws.alcasset.com';
30
+ }
31
+ return 'cws.alyasset.com';
32
+ };
11
33
 
12
34
  /**
13
35
  * get alfa release url
@@ -17,15 +39,10 @@ import { getChannel } from './channel';
17
39
  */
18
40
  export var getReleaseUrl = function getReleaseUrl(name, env, channel) {
19
41
  var _process;
20
- var cdnUrl = "https://cws.alicdn.com/Release/alfa/".concat(name, "/release.json");
21
- var cdnPreUrl = "https://cws2.alicdn.com/Prepub/alfa/".concat(name, "/release.json");
22
- if (channel === 'OFFICIAL') {
23
- cdnUrl = "htts://cws.alyasset.com/Release/alfa/".concat(name, "/release.json");
24
- cdnPreUrl = "https://cws.alyasset.com/Prepub/alfa/".concat(name, "/release.json");
25
- } else if (channel === 'SIN') {
26
- cdnUrl = "https://cws.alcasset.com/Release/alfa/".concat(name, "/release.json");
27
- cdnPreUrl = "https://cws.alcasset.com/Prepub/alfa/".concat(name, "/release.json");
28
- }
42
+ var cwsHost = getCwsHost(channel);
43
+ var path = isIdptcloud03Cdn() ? 'Release' : 'Prepub';
44
+ var cdnUrl = "https://".concat(cwsHost, "/Release/alfa/").concat(name, "/release.json");
45
+ var cdnPreUrl = "https://".concat(cwsHost, "/").concat(path, "/alfa/").concat(name, "/release.json");
29
46
  switch (env) {
30
47
  case 'local':
31
48
  return ((_process = process) === null || _process === void 0 ? void 0 : _process.env.ALFA_RELEASE_URL) || cdnPreUrl;
@@ -48,12 +65,8 @@ function patchReleaseResult(releaseConfig, channel) {
48
65
  if (!domain) {
49
66
  return domain;
50
67
  }
51
- if (channel === 'OFFICIAL') {
52
- return domain.replace(/cws2?\.alicdn\.com/, 'cws.alyasset.com');
53
- } else if (channel === 'SIN') {
54
- return domain.replace(/cws2?\.alicdn\.com/, 'cws.alcasset.com');
55
- }
56
- return domain;
68
+ var path = isIdptcloud03Cdn() ? '/Release/' : '/Prepub/';
69
+ return domain.replace(/cws2?\.alicdn\.com/, getCwsHost(channel)).replace(/\/Prepub\//g, path);
57
70
  };
58
71
  var mapObject = function mapObject(obj, fn) {
59
72
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
@@ -109,7 +122,7 @@ export var getRelease = /*#__PURE__*/function () {
109
122
  _context.next = 6;
110
123
  return getMicroAppRelease(name, env);
111
124
  case 6:
112
- return _context.abrupt("return", _t2(_context.sent));
125
+ return _context.abrupt("return", _t2(_context.sent, channel));
113
126
  case 7:
114
127
  _context.prev = 7;
115
128
  _t3 = _context["catch"](5);
package/es/utils/index.js CHANGED
@@ -76,8 +76,9 @@ export var getRelativePath = function getRelativePath(from, to, base) {
76
76
  toHost = _URL2.host,
77
77
  toPath = _URL2.pathname;
78
78
 
79
- // from 'g.alicdn.com' to 'dev.g.alicdn.com' regarded as same host
80
- if (fromHost !== toHost && (toHost !== 'g.alicdn.com' || fromHost !== 'dev.g.alicdn.com')) return to;
79
+ // g.xxx and dev.g.xxx / dev-g.xxx regarded as same host
80
+ var isSameHost = fromHost === toHost || /^dev[.-]g\./.test(fromHost) && toHost === fromHost.replace(/^dev[.-]g\./, 'g.');
81
+ if (!isSameHost) return to;
81
82
  var fromParts = trimArray(fromPath.split('/'));
82
83
  var toParts = trimArray(toPath.split('/'));
83
84
  var length = Math.min(fromParts.length, toParts.length);
@@ -1,4 +1,5 @@
1
1
  import instance from '../request';
2
+ export { normalizeCdnUrl } from './requestInterceptor';
2
3
  export var addRequestInterceptor = function addRequestInterceptor(fn) {
3
4
  var interceptor = function interceptor(config) {
4
5
  try {
@@ -1,11 +1,13 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
2
- var _window$ALIYUN_CONSOL;
2
+ var _ALIYUN_CONSOLE_CONFI;
3
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
4
4
  // 支持的主权云 CDN Host
5
- // @ts-ignore
6
- var whiteList = ['idptcloud01cdn.com', ((_window$ALIYUN_CONSOL = window.ALIYUN_CONSOLE_CONFIG) === null || _window$ALIYUN_CONSOL === void 0 ? void 0 : _window$ALIYUN_CONSOL.MAIN_RESOURCE_CDN) || 'alicdn.com'];
5
+ var whiteList = ['idptcloud01cdn.com', 'uscasset.alibaba', 'idptcloud03asset.alibaba', ((_ALIYUN_CONSOLE_CONFI = window.ALIYUN_CONSOLE_CONFIG) === null || _ALIYUN_CONSOLE_CONFI === void 0 ? void 0 : _ALIYUN_CONSOLE_CONFI.MAIN_RESOURCE_CDN) || 'alicdn.com'];
7
6
  var win = window;
8
7
 
8
+ // 无预发环境的 CDN 域名(一国一云及类似环境)
9
+ var noPrepubCdnHosts = ['idptcloud01cdn.com', 'uscasset.alibaba', 'idptcloud03asset.alibaba'];
10
+
9
11
  // 兼容非公有云环境,用于替换请求的 hostname
10
12
  var getCurrentCdnHost = function getCurrentCdnHost() {
11
13
  try {
@@ -24,6 +26,8 @@ var getCurrentCdnHost = function getCurrentCdnHost() {
24
26
  var _URL = new URL(scriptUrl),
25
27
  hostname = _URL.hostname;
26
28
  var parts = hostname.split('.');
29
+
30
+ // 支持 .com 和 .alibaba 等多种顶级域名:取最后两段作为 CDN host
27
31
  var _currentCdnHost = parts.length > 2 ? parts.slice(-2).join('.') : hostname;
28
32
  if (!win.__alfa_cdn_host__) {
29
33
  win.__alfa_cdn_host__ = _currentCdnHost;
@@ -37,6 +41,22 @@ var currentCdnHost = getCurrentCdnHost();
37
41
 
38
42
  // 如果 host 被 o2 插件替换,则可以被用来判断是否需要替换 cdnHost
39
43
  var gCdnHost = '//g.alicdn.com/';
44
+ var noDevGCdnPattern = /dev\.g\.(idptcloud01cdn\.com|uscasset\.alibaba|idptcloud03asset\.alibaba)/g;
45
+ export var normalizeCdnUrl = function normalizeCdnUrl(url) {
46
+ var isSupportedCdnHost = currentCdnHost && currentCdnHost !== 'alicdn.com' && (whiteList.includes(currentCdnHost) || gCdnHost.includes(currentCdnHost));
47
+ if (isSupportedCdnHost) {
48
+ return url.replace(/\.alicdn\.com/g, ".".concat(currentCdnHost))
49
+ // 预发域名也需替换(非公有云无独立预发域名)
50
+ .replace(/\.aliyun-inc\.com/g, ".".concat(currentCdnHost))
51
+ // 非公有云下无 cws2
52
+ .replace('cws2.', 'cws.')
53
+ // 无预发环境时使用 Release 路径
54
+ .replace(/\/Prepub\//g, noPrepubCdnHosts.includes(currentCdnHost) ? '/Release/' : '/Prepub/')
55
+ // 这些域名无 dev.g 子域名
56
+ .replace(noDevGCdnPattern, 'dev-g.$1');
57
+ }
58
+ return url;
59
+ };
40
60
 
41
61
  // 根据不同环境替换 cdn 的域名
42
62
  export default function requestInterceptor(_x) {
@@ -47,12 +67,8 @@ function _requestInterceptor() {
47
67
  return _regeneratorRuntime.wrap(function (_context) {
48
68
  while (1) switch (_context.prev = _context.next) {
49
69
  case 0:
50
- if (currentCdnHost && currentCdnHost !== 'alicdn.com' && (whiteList.includes(currentCdnHost) || gCdnHost.includes(currentCdnHost))) {
51
- // if cdn is not alicdn, replace it
52
- if (config.url) {
53
- config.url = config.url.replace(new RegExp('.alicdn.com'), ".".concat(currentCdnHost)).replace('cws2.', 'cws.') // 非公有云下无 cws2
54
- .replace('dev.g.idptcloud01cdn.com', 'dev-g.idptcloud01cdn.com'); // idptcloud01cdn 无 dev.g 子域名
55
- }
70
+ if (config.url) {
71
+ config.url = normalizeCdnUrl(config.url);
56
72
  }
57
73
  return _context.abrupt("return", config);
58
74
  case 1:
@@ -10,7 +10,8 @@ function isManifest(url, data) {
10
10
  return false;
11
11
  }
12
12
  function isRelease(url, data) {
13
- var pathname = new URL(url).pathname;
13
+ var _URL = new URL(url),
14
+ pathname = _URL.pathname;
14
15
  if (!data) return false;
15
16
 
16
17
  // central cws release.json
@@ -65,6 +66,9 @@ function isLegal(url, data) {
65
66
  return false;
66
67
  }
67
68
 
69
+ // 无 cws2 fallback 的 CDN 域名(一国一云及类似环境)
70
+ var noCws2FallbackHosts = ['idptcloud01cdn.com', 'uscasset.alibaba', 'idptcloud03asset.alibaba'];
71
+
68
72
  // check response content after networkErrorInterceptor
69
73
  // if it legal, cache it
70
74
  // if it is illegal, fallback cws.alicdn.com to cws2.alicdn.com.
@@ -73,7 +77,7 @@ export default function responseInterceptor(_x) {
73
77
  }
74
78
  function _responseInterceptor() {
75
79
  _responseInterceptor = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(response) {
76
- var data, config, url, newResponse, error;
80
+ var data, config, url, shouldSkipFallback, newResponse, error;
77
81
  return _regeneratorRuntime.wrap(function (_context) {
78
82
  while (1) switch (_context.prev = _context.next) {
79
83
  case 0:
@@ -85,15 +89,25 @@ function _responseInterceptor() {
85
89
  }
86
90
  return _context.abrupt("return", response);
87
91
  case 1:
92
+ // 非公有云域名(一国一云等)没有 cws2 fallback,直接跳过
93
+ shouldSkipFallback = url && noCws2FallbackHosts.some(function (host) {
94
+ return url.includes(host);
95
+ });
96
+ if (shouldSkipFallback) {
97
+ _context.next = 3;
98
+ break;
99
+ }
88
100
  if (['cws.alyasset.com', 'cws.alcasset.com'].some(function (domain) {
89
101
  return url === null || url === void 0 ? void 0 : url.includes("://".concat(domain, "/"));
90
102
  })) {
91
- _context.next = 3;
92
- break;
103
+ config.url = url === null || url === void 0 ? void 0 : url.replace(/:\/\/cws\.al[yc]asset\.com\//, function () {
104
+ return '://cws2.alicdn.com/';
105
+ });
106
+ } else {
107
+ config.url = url === null || url === void 0 ? void 0 : url.replace(/:\/\/cws\.([a-z0-9-]+\.(?:com|alibaba))\//, function (match, p1) {
108
+ return "://cws2.".concat(p1, "/");
109
+ });
93
110
  }
94
- config.url = url === null || url === void 0 ? void 0 : url.replace(/:\/\/cws\.([a-z0-9-]+)\.com\//, function (match, p1) {
95
- return "://cws2.".concat(p1, ".com/");
96
- });
97
111
  _context.next = 2;
98
112
  return axios(config);
99
113
  case 2:
@@ -75,8 +75,8 @@ var Logger = /*#__PURE__*/function () {
75
75
  value: function track(method, topic, params) {
76
76
  var data = this.mergeData(params);
77
77
 
78
- // just report in aliyun.com && alibabacloud.com
79
- if (!['aliyun.com', 'alibabacloud.com', 'aliyun-inc.com', 'alibaba-inc.com'].find(function (domain) {
78
+ // 国内站、国际站、idpt、一国一云允许上报
79
+ if (!['aliyun.com', 'alibabacloud.com', 'aliyun-inc.com', 'alibaba-inc.com', 'idptcloud01.com', 'idptcloud01-inc.com', 'uscloud.alibaba', 'idptcloud03.alibaba', 'idptcloud03-inc.alibaba'].find(function (domain) {
80
80
  return location.hostname.endsWith(domain);
81
81
  })) return false;
82
82
 
package/lib/index.js CHANGED
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", {
7
7
  var _exportNames = {
8
8
  createMicroApp: true,
9
9
  addRequestInterceptor: true,
10
+ normalizeCdnUrl: true,
10
11
  getRelease: true,
11
12
  getManifest: true,
12
13
  getConfig: true,
@@ -106,6 +107,12 @@ Object.defineProperty(exports, "loadExposedModule", {
106
107
  return _consoleOsKernal.loadExposedModule;
107
108
  }
108
109
  });
110
+ Object.defineProperty(exports, "normalizeCdnUrl", {
111
+ enumerable: true,
112
+ get: function get() {
113
+ return _interceptors.normalizeCdnUrl;
114
+ }
115
+ });
109
116
  Object.defineProperty(exports, "prefetch", {
110
117
  enumerable: true,
111
118
  get: function get() {
@@ -21,11 +21,21 @@ function getChannel(value) {
21
21
  }
22
22
 
23
23
  // 尝试根据域名判断
24
- if (['alibabacloud.com', 'idptcloud01cs.com', 'idptcloud01-inc.com'].find(function (domain) {
24
+ if (['alibabacloud.com'].find(function (domain) {
25
25
  return location.hostname.endsWith(domain);
26
26
  })) {
27
27
  return 'SIN';
28
28
  }
29
+ if (['idptcloud01.com', 'idptcloud01cs.com', 'idptcloud01-inc.com'].find(function (domain) {
30
+ return location.hostname.endsWith(domain);
31
+ })) {
32
+ return 'EA134';
33
+ }
34
+ if (['idptcloud.com'].find(function (domain) {
35
+ return location.hostname.endsWith(domain);
36
+ })) {
37
+ return 'CM10';
38
+ }
29
39
 
30
40
  // 默认返回 OFFICIAL
31
41
  return 'OFFICIAL';
package/lib/utils/env.js CHANGED
@@ -4,6 +4,18 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getEnv = void 0;
7
+ // 无预发环境的域名后缀(一国一云及类似环境)
8
+ var noPrepubDomains = ['idptcloud01.com', 'idptcloud01-inc.com', 'uscloud.alibaba', 'idptcloud03.alibaba', 'idptcloud03-inc.alibaba'];
9
+ var isNoPrepubDomain = function isNoPrepubDomain() {
10
+ try {
11
+ return noPrepubDomains.some(function (domain) {
12
+ return location.hostname.endsWith(domain);
13
+ });
14
+ } catch (e) {
15
+ return false;
16
+ }
17
+ };
18
+
7
19
  /**
8
20
  * get alfa env
9
21
  * @returns
@@ -17,7 +29,11 @@ var getEnv = exports.getEnv = function getEnv(value) {
17
29
  return 'local';
18
30
  }
19
31
  }
32
+ var env = ((_window = window) === null || _window === void 0 ? void 0 : (_window$ALIYUN_CONSOL = _window.ALIYUN_CONSOLE_CONFIG) === null || _window$ALIYUN_CONSOL === void 0 ? void 0 : _window$ALIYUN_CONSOL.fEnv) || 'prod';
20
33
 
21
- // default return prod
22
- return ((_window = window) === null || _window === void 0 ? void 0 : (_window$ALIYUN_CONSOL = _window.ALIYUN_CONSOLE_CONFIG) === null || _window$ALIYUN_CONSOL === void 0 ? void 0 : _window$ALIYUN_CONSOL.fEnv) || 'prod';
34
+ // 无预发环境的域名强制使用 prod
35
+ if ((env === 'pre' || env === 'daily') && isNoPrepubDomain()) {
36
+ return 'prod';
37
+ }
38
+ return env;
23
39
  };
@@ -100,7 +100,7 @@ var getConfig = exports.getConfig = /*#__PURE__*/function () {
100
100
  */
101
101
  var getConfigV2 = exports.getConfigV2 = /*#__PURE__*/function () {
102
102
  var _ref4 = (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee2(config) {
103
- var releaseConfig, _ref5, relatedConsoleAppId, currentConfig, _ALIYUN_CONSOLE_CONFI, res, _t3;
103
+ var releaseConfig, _ref5, relatedConsoleAppId, currentConfig, _ALIYUN_CONSOLE_CONFI, mainDomain, res, _t3;
104
104
  return _regenerator.default.wrap(function (_context2) {
105
105
  while (1) switch (_context2.prev = _context2.next) {
106
106
  case 0:
@@ -124,8 +124,11 @@ var getConfigV2 = exports.getConfigV2 = /*#__PURE__*/function () {
124
124
  return _context2.abrupt("return", currentConfig);
125
125
  case 2:
126
126
  _context2.prev = 2;
127
+ // 内网域名(如 aliyun-inc.com / alibaba-inc.com)的 fecs 服务实际部署在去掉 `-inc` 的公网域名上,
128
+ // 这里剥离一级域名结尾的 `-inc` 后缀,确保 fecs 请求命中正确域名
129
+ mainDomain = (0, _consoleBaseConfParseEnv.default)().MAIN_DOMAIN.replace(/-inc(?=\.[^.]+$)/, '');
127
130
  _context2.next = 3;
128
- return _request.default.get("https://".concat(config.env !== 'prod' ? 'pre-' : '', "fecs.console.").concat((0, _consoleBaseConfParseEnv.default)().MAIN_DOMAIN, "/api/alfa/console/config?appId=").concat(relatedConsoleAppId), {
131
+ return _request.default.get("https://".concat(config.env !== 'prod' ? 'pre-' : '', "fecs.console.").concat(mainDomain, "/api/alfa/console/config?appId=").concat(relatedConsoleAppId), {
129
132
  withCredentials: true
130
133
  });
131
134
  case 3:
@@ -9,12 +9,35 @@ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"))
9
9
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
10
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
11
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
12
+ var _consoleBaseConfParseEnv = _interopRequireDefault(require("@alicloud/console-base-conf-parse-env"));
12
13
  var _cacheManager = _interopRequireDefault(require("./cacheManager"));
13
14
  var _oss = require("./oss");
14
15
  var _env = require("./env");
15
16
  var _channel = require("./channel");
16
17
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
17
18
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
19
+ var idptcloud03CdnHost = 'idptcloud03asset.alibaba';
20
+ var getMainResourceCdn = function getMainResourceCdn() {
21
+ try {
22
+ var _parseEnv;
23
+ return ((_parseEnv = (0, _consoleBaseConfParseEnv.default)()) === null || _parseEnv === void 0 ? void 0 : _parseEnv.MAIN_RESOURCE_CDN) || '';
24
+ } catch (e) {
25
+ return '';
26
+ }
27
+ };
28
+ var isIdptcloud03Cdn = function isIdptcloud03Cdn() {
29
+ return getMainResourceCdn().includes(idptcloud03CdnHost);
30
+ };
31
+ var getCwsHost = function getCwsHost(channel) {
32
+ if (isIdptcloud03Cdn()) {
33
+ return "cws.".concat(idptcloud03CdnHost);
34
+ }
35
+ if (['SIN', 'EA134', 'CM10'].includes(channel)) {
36
+ return 'cws.alcasset.com';
37
+ }
38
+ return 'cws.alyasset.com';
39
+ };
40
+
18
41
  /**
19
42
  * get alfa release url
20
43
  * @param appId
@@ -23,15 +46,10 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
23
46
  */
24
47
  var getReleaseUrl = exports.getReleaseUrl = function getReleaseUrl(name, env, channel) {
25
48
  var _process;
26
- var cdnUrl = "https://cws.alicdn.com/Release/alfa/".concat(name, "/release.json");
27
- var cdnPreUrl = "https://cws2.alicdn.com/Prepub/alfa/".concat(name, "/release.json");
28
- if (channel === 'OFFICIAL') {
29
- cdnUrl = "htts://cws.alyasset.com/Release/alfa/".concat(name, "/release.json");
30
- cdnPreUrl = "https://cws.alyasset.com/Prepub/alfa/".concat(name, "/release.json");
31
- } else if (channel === 'SIN') {
32
- cdnUrl = "https://cws.alcasset.com/Release/alfa/".concat(name, "/release.json");
33
- cdnPreUrl = "https://cws.alcasset.com/Prepub/alfa/".concat(name, "/release.json");
34
- }
49
+ var cwsHost = getCwsHost(channel);
50
+ var path = isIdptcloud03Cdn() ? 'Release' : 'Prepub';
51
+ var cdnUrl = "https://".concat(cwsHost, "/Release/alfa/").concat(name, "/release.json");
52
+ var cdnPreUrl = "https://".concat(cwsHost, "/").concat(path, "/alfa/").concat(name, "/release.json");
35
53
  switch (env) {
36
54
  case 'local':
37
55
  return ((_process = process) === null || _process === void 0 ? void 0 : _process.env.ALFA_RELEASE_URL) || cdnPreUrl;
@@ -54,12 +72,8 @@ function patchReleaseResult(releaseConfig, channel) {
54
72
  if (!domain) {
55
73
  return domain;
56
74
  }
57
- if (channel === 'OFFICIAL') {
58
- return domain.replace(/cws2?\.alicdn\.com/, 'cws.alyasset.com');
59
- } else if (channel === 'SIN') {
60
- return domain.replace(/cws2?\.alicdn\.com/, 'cws.alcasset.com');
61
- }
62
- return domain;
75
+ var path = isIdptcloud03Cdn() ? '/Release/' : '/Prepub/';
76
+ return domain.replace(/cws2?\.alicdn\.com/, getCwsHost(channel)).replace(/\/Prepub\//g, path);
63
77
  };
64
78
  var mapObject = function mapObject(obj, fn) {
65
79
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
@@ -115,7 +129,7 @@ var getRelease = exports.getRelease = /*#__PURE__*/function () {
115
129
  _context.next = 6;
116
130
  return (0, _oss.getMicroAppRelease)(name, env);
117
131
  case 6:
118
- return _context.abrupt("return", _t2(_context.sent));
132
+ return _context.abrupt("return", _t2(_context.sent, channel));
119
133
  case 7:
120
134
  _context.prev = 7;
121
135
  _t3 = _context["catch"](5);
@@ -106,8 +106,9 @@ var getRelativePath = exports.getRelativePath = function getRelativePath(from, t
106
106
  toHost = _URL2.host,
107
107
  toPath = _URL2.pathname;
108
108
 
109
- // from 'g.alicdn.com' to 'dev.g.alicdn.com' regarded as same host
110
- if (fromHost !== toHost && (toHost !== 'g.alicdn.com' || fromHost !== 'dev.g.alicdn.com')) return to;
109
+ // g.xxx and dev.g.xxx / dev-g.xxx regarded as same host
110
+ var isSameHost = fromHost === toHost || /^dev[.-]g\./.test(fromHost) && toHost === fromHost.replace(/^dev[.-]g\./, 'g.');
111
+ if (!isSameHost) return to;
111
112
  var fromParts = trimArray(fromPath.split('/'));
112
113
  var toParts = trimArray(toPath.split('/'));
113
114
  var length = Math.min(fromParts.length, toParts.length);
@@ -5,7 +5,14 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.addRequestInterceptor = void 0;
8
+ Object.defineProperty(exports, "normalizeCdnUrl", {
9
+ enumerable: true,
10
+ get: function get() {
11
+ return _requestInterceptor.normalizeCdnUrl;
12
+ }
13
+ });
8
14
  var _request = _interopRequireDefault(require("../request"));
15
+ var _requestInterceptor = require("./requestInterceptor");
9
16
  var addRequestInterceptor = exports.addRequestInterceptor = function addRequestInterceptor(fn) {
10
17
  var interceptor = function interceptor(config) {
11
18
  try {
@@ -5,14 +5,17 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.default = requestInterceptor;
8
+ exports.normalizeCdnUrl = void 0;
8
9
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
10
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
10
- var _window$ALIYUN_CONSOL;
11
+ var _ALIYUN_CONSOLE_CONFI;
11
12
  // 支持的主权云 CDN Host
12
- // @ts-ignore
13
- var whiteList = ['idptcloud01cdn.com', ((_window$ALIYUN_CONSOL = window.ALIYUN_CONSOLE_CONFIG) === null || _window$ALIYUN_CONSOL === void 0 ? void 0 : _window$ALIYUN_CONSOL.MAIN_RESOURCE_CDN) || 'alicdn.com'];
13
+ var whiteList = ['idptcloud01cdn.com', 'uscasset.alibaba', 'idptcloud03asset.alibaba', ((_ALIYUN_CONSOLE_CONFI = window.ALIYUN_CONSOLE_CONFIG) === null || _ALIYUN_CONSOLE_CONFI === void 0 ? void 0 : _ALIYUN_CONSOLE_CONFI.MAIN_RESOURCE_CDN) || 'alicdn.com'];
14
14
  var win = window;
15
15
 
16
+ // 无预发环境的 CDN 域名(一国一云及类似环境)
17
+ var noPrepubCdnHosts = ['idptcloud01cdn.com', 'uscasset.alibaba', 'idptcloud03asset.alibaba'];
18
+
16
19
  // 兼容非公有云环境,用于替换请求的 hostname
17
20
  var getCurrentCdnHost = function getCurrentCdnHost() {
18
21
  try {
@@ -31,6 +34,8 @@ var getCurrentCdnHost = function getCurrentCdnHost() {
31
34
  var _URL = new URL(scriptUrl),
32
35
  hostname = _URL.hostname;
33
36
  var parts = hostname.split('.');
37
+
38
+ // 支持 .com 和 .alibaba 等多种顶级域名:取最后两段作为 CDN host
34
39
  var _currentCdnHost = parts.length > 2 ? parts.slice(-2).join('.') : hostname;
35
40
  if (!win.__alfa_cdn_host__) {
36
41
  win.__alfa_cdn_host__ = _currentCdnHost;
@@ -44,6 +49,22 @@ var currentCdnHost = getCurrentCdnHost();
44
49
 
45
50
  // 如果 host 被 o2 插件替换,则可以被用来判断是否需要替换 cdnHost
46
51
  var gCdnHost = '//g.alicdn.com/';
52
+ var noDevGCdnPattern = /dev\.g\.(idptcloud01cdn\.com|uscasset\.alibaba|idptcloud03asset\.alibaba)/g;
53
+ var normalizeCdnUrl = exports.normalizeCdnUrl = function normalizeCdnUrl(url) {
54
+ var isSupportedCdnHost = currentCdnHost && currentCdnHost !== 'alicdn.com' && (whiteList.includes(currentCdnHost) || gCdnHost.includes(currentCdnHost));
55
+ if (isSupportedCdnHost) {
56
+ return url.replace(/\.alicdn\.com/g, ".".concat(currentCdnHost))
57
+ // 预发域名也需替换(非公有云无独立预发域名)
58
+ .replace(/\.aliyun-inc\.com/g, ".".concat(currentCdnHost))
59
+ // 非公有云下无 cws2
60
+ .replace('cws2.', 'cws.')
61
+ // 无预发环境时使用 Release 路径
62
+ .replace(/\/Prepub\//g, noPrepubCdnHosts.includes(currentCdnHost) ? '/Release/' : '/Prepub/')
63
+ // 这些域名无 dev.g 子域名
64
+ .replace(noDevGCdnPattern, 'dev-g.$1');
65
+ }
66
+ return url;
67
+ };
47
68
 
48
69
  // 根据不同环境替换 cdn 的域名
49
70
  function requestInterceptor(_x) {
@@ -54,12 +75,8 @@ function _requestInterceptor() {
54
75
  return _regenerator.default.wrap(function (_context) {
55
76
  while (1) switch (_context.prev = _context.next) {
56
77
  case 0:
57
- if (currentCdnHost && currentCdnHost !== 'alicdn.com' && (whiteList.includes(currentCdnHost) || gCdnHost.includes(currentCdnHost))) {
58
- // if cdn is not alicdn, replace it
59
- if (config.url) {
60
- config.url = config.url.replace(new RegExp('.alicdn.com'), ".".concat(currentCdnHost)).replace('cws2.', 'cws.') // 非公有云下无 cws2
61
- .replace('dev.g.idptcloud01cdn.com', 'dev-g.idptcloud01cdn.com'); // idptcloud01cdn 无 dev.g 子域名
62
- }
78
+ if (config.url) {
79
+ config.url = normalizeCdnUrl(config.url);
63
80
  }
64
81
  return _context.abrupt("return", config);
65
82
  case 1:
@@ -17,7 +17,8 @@ function isManifest(url, data) {
17
17
  return false;
18
18
  }
19
19
  function isRelease(url, data) {
20
- var pathname = new URL(url).pathname;
20
+ var _URL = new URL(url),
21
+ pathname = _URL.pathname;
21
22
  if (!data) return false;
22
23
 
23
24
  // central cws release.json
@@ -72,6 +73,9 @@ function isLegal(url, data) {
72
73
  return false;
73
74
  }
74
75
 
76
+ // 无 cws2 fallback 的 CDN 域名(一国一云及类似环境)
77
+ var noCws2FallbackHosts = ['idptcloud01cdn.com', 'uscasset.alibaba', 'idptcloud03asset.alibaba'];
78
+
75
79
  // check response content after networkErrorInterceptor
76
80
  // if it legal, cache it
77
81
  // if it is illegal, fallback cws.alicdn.com to cws2.alicdn.com.
@@ -80,7 +84,7 @@ function responseInterceptor(_x) {
80
84
  }
81
85
  function _responseInterceptor() {
82
86
  _responseInterceptor = (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee(response) {
83
- var data, config, url, newResponse, error;
87
+ var data, config, url, shouldSkipFallback, newResponse, error;
84
88
  return _regenerator.default.wrap(function (_context) {
85
89
  while (1) switch (_context.prev = _context.next) {
86
90
  case 0:
@@ -92,15 +96,25 @@ function _responseInterceptor() {
92
96
  }
93
97
  return _context.abrupt("return", response);
94
98
  case 1:
99
+ // 非公有云域名(一国一云等)没有 cws2 fallback,直接跳过
100
+ shouldSkipFallback = url && noCws2FallbackHosts.some(function (host) {
101
+ return url.includes(host);
102
+ });
103
+ if (shouldSkipFallback) {
104
+ _context.next = 3;
105
+ break;
106
+ }
95
107
  if (['cws.alyasset.com', 'cws.alcasset.com'].some(function (domain) {
96
108
  return url === null || url === void 0 ? void 0 : url.includes("://".concat(domain, "/"));
97
109
  })) {
98
- _context.next = 3;
99
- break;
110
+ config.url = url === null || url === void 0 ? void 0 : url.replace(/:\/\/cws\.al[yc]asset\.com\//, function () {
111
+ return '://cws2.alicdn.com/';
112
+ });
113
+ } else {
114
+ config.url = url === null || url === void 0 ? void 0 : url.replace(/:\/\/cws\.([a-z0-9-]+\.(?:com|alibaba))\//, function (match, p1) {
115
+ return "://cws2.".concat(p1, "/");
116
+ });
100
117
  }
101
- config.url = url === null || url === void 0 ? void 0 : url.replace(/:\/\/cws\.([a-z0-9-]+)\.com\//, function (match, p1) {
102
- return "://cws2.".concat(p1, ".com/");
103
- });
104
118
  _context.next = 2;
105
119
  return (0, _axios.default)(config);
106
120
  case 2:
@@ -82,8 +82,8 @@ var Logger = exports.default = /*#__PURE__*/function () {
82
82
  value: function track(method, topic, params) {
83
83
  var data = this.mergeData(params);
84
84
 
85
- // just report in aliyun.com && alibabacloud.com
86
- if (!['aliyun.com', 'alibabacloud.com', 'aliyun-inc.com', 'alibaba-inc.com'].find(function (domain) {
85
+ // 国内站、国际站、idpt、一国一云允许上报
86
+ if (!['aliyun.com', 'alibabacloud.com', 'aliyun-inc.com', 'alibaba-inc.com', 'idptcloud01.com', 'idptcloud01-inc.com', 'uscloud.alibaba', 'idptcloud03.alibaba', 'idptcloud03-inc.alibaba'].find(function (domain) {
87
87
  return location.hostname.endsWith(domain);
88
88
  })) return false;
89
89
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alicloud/alfa-core",
3
- "version": "1.6.0-beta.1",
3
+ "version": "1.6.0-beta.10",
4
4
  "description": "MicroFront End SDK for alicloud",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { createMicroApp } from './createMicroApp';
2
- export { addRequestInterceptor } from './utils/interceptors';
2
+ export { addRequestInterceptor, normalizeCdnUrl } from './utils/interceptors';
3
3
  export { getRelease } from './utils/getRelease';
4
4
  export { getManifest } from './utils/getManifest';
5
5
  export { getConfig, getConfigV2 } from './utils/getConfig';
@@ -1,4 +1,5 @@
1
1
  import { AxiosRequestConfig } from 'axios';
2
+ export { normalizeCdnUrl } from './requestInterceptor';
2
3
  export interface RequestInterceptor {
3
4
  (config: AxiosRequestConfig): AxiosRequestConfig | Promise<AxiosRequestConfig>;
4
5
  }
@@ -1,2 +1,3 @@
1
1
  import type { AxiosRequestConfig } from 'axios';
2
+ export declare const normalizeCdnUrl: (url: string) => string;
2
3
  export default function requestInterceptor(config: AxiosRequestConfig): Promise<AxiosRequestConfig>;