utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const cloudbase_1 = require("../cloudbase");
  4. const metadata_1 = require("./metadata");
  5. class TcbError extends Error {
  6. constructor(error) {
  7. super(error.message);
  8. this.code = error.code;
  9. this.message = error.message;
  10. this.requestId = error.requestId;
  11. }
  12. }
  13. exports.TcbError = TcbError;
  14. function isAppId(appIdStr) {
  15. return /^[1-9][0-9]{4,64}$/gim.test(appIdStr);
  16. }
  17. exports.isAppId = isAppId;
  18. exports.filterValue = function filterValue(o, value) {
  19. for (let key in o) {
  20. if (o[key] === value) {
  21. delete o[key];
  22. }
  23. }
  24. };
  25. exports.filterUndefined = function (o) {
  26. return exports.filterValue(o, undefined);
  27. };
  28. exports.E = (errObj) => {
  29. return new TcbError(errObj);
  30. };
  31. function isNonEmptyString(str) {
  32. return typeof str === 'string' && str !== '';
  33. }
  34. exports.isNonEmptyString = isNonEmptyString;
  35. function checkIsInScf() {
  36. // TENCENTCLOUD_RUNENV
  37. return process.env.TENCENTCLOUD_RUNENV === 'SCF';
  38. }
  39. exports.checkIsInScf = checkIsInScf;
  40. function checkIsInEks() {
  41. // EKS_CLUSTER_ID=cls-abcdefg
  42. // EKS_LOGS_xxx=
  43. // return isNonEmptyString(process.env.EKS_CLUSTER_ID)
  44. return !!process.env.KUBERNETES_SERVICE_HOST;
  45. }
  46. exports.checkIsInEks = checkIsInEks;
  47. const kSumeruEnvSet = new Set(['formal', 'pre', 'test']);
  48. function checkIsInSumeru() {
  49. // SUMERU_ENV=formal | test | pre
  50. return kSumeruEnvSet.has(process.env.SUMERU_ENV);
  51. }
  52. exports.checkIsInSumeru = checkIsInSumeru;
  53. async function checkIsInTencentCloud() {
  54. return isNonEmptyString(await metadata_1.lookupAppId());
  55. }
  56. exports.checkIsInTencentCloud = checkIsInTencentCloud;
  57. function second() {
  58. // istanbul ignore next
  59. return Math.floor(new Date().getTime() / 1000);
  60. }
  61. exports.second = second;
  62. function processReturn(throwOnCode, res) {
  63. if (throwOnCode === false) {
  64. // 不抛报错,正常return,兼容旧逻辑
  65. return res;
  66. }
  67. throw exports.E(Object.assign({}, res));
  68. }
  69. exports.processReturn = processReturn;
  70. function getServerInjectUrl() {
  71. const tcbContextConfig = getTcbContextConfig();
  72. return tcbContextConfig['URL'] || '';
  73. }
  74. exports.getServerInjectUrl = getServerInjectUrl;
  75. function getTcbContextConfig() {
  76. try {
  77. const { TCB_CONTEXT_CNFG } = cloudbase_1.CloudBase.getCloudbaseContext();
  78. if (TCB_CONTEXT_CNFG) {
  79. // 检查约定环境变量字段是否存在
  80. return JSON.parse(TCB_CONTEXT_CNFG);
  81. }
  82. return {};
  83. }
  84. catch (e) {
  85. /* istanbul ignore next */
  86. console.log('parse context error...');
  87. /* istanbul ignore next */
  88. return {};
  89. }
  90. }
  91. exports.getTcbContextConfig = getTcbContextConfig;
  92. /* istanbul ignore next */
  93. function getWxUrl(config) {
  94. const protocal = config.isHttp === true ? 'http' : 'https';
  95. let wxUrl = protocal + '://tcb-open.tencentcloudapi.com/admin';
  96. if (checkIsInScf()) {
  97. wxUrl = 'http://tcb-open.tencentyun.com/admin';
  98. }
  99. return wxUrl;
  100. }
  101. exports.getWxUrl = getWxUrl;
  102. function checkIsInternal() {
  103. return checkIsInScf() || checkIsInEks() || checkIsInSumeru();
  104. }
  105. exports.checkIsInternal = checkIsInternal;
  106. function checkIsInternalAsync() {
  107. return checkIsInternal() ? Promise.resolve(true) : checkIsInTencentCloud();
  108. }
  109. exports.checkIsInternalAsync = checkIsInternalAsync;
  110. function getCurrRunEnvTag() {
  111. if (checkIsInScf()) {
  112. return 'scf';
  113. }
  114. else if (checkIsInEks()) {
  115. return 'eks';
  116. }
  117. else if (checkIsInSumeru()) {
  118. return 'sumeru';
  119. }
  120. else if (checkIsInTencentCloud()) {
  121. return 'tencentcloud';
  122. }
  123. return 'unknown';
  124. }
  125. exports.getCurrRunEnvTag = getCurrRunEnvTag;
  126. /**
  127. * 是否是场景模块名
  128. *
  129. * $: 前缀,表示SaaS场景模块名,非实际环境ID,当前通过特殊环境ID标识
  130. *
  131. * @param env
  132. * @returns
  133. */
  134. function isPageModuleName(env = '') {
  135. return typeof env === 'string' && env.startsWith('$:');
  136. }
  137. exports.isPageModuleName = isPageModuleName;
  138. // 20 + 1 + 16, 限制长度 40
  139. const env_rule_reg = /^[a-z0-9_-]{1,40}$/;
  140. function isValidEnvFormat(env = '') {
  141. return typeof env === 'string' && env_rule_reg.test(env);
  142. }
  143. exports.isValidEnvFormat = isValidEnvFormat;