utils.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const cloudbase_1 = require("../cloudbase");
  4. class TcbError extends Error {
  5. constructor(error) {
  6. super(error.message);
  7. this.code = error.code;
  8. this.message = error.message;
  9. this.requestId = error.requestId;
  10. }
  11. }
  12. exports.TcbError = TcbError;
  13. exports.filterValue = function filterValue(o, value) {
  14. for (let key in o) {
  15. if (o[key] === value) {
  16. delete o[key];
  17. }
  18. }
  19. };
  20. exports.filterUndefined = function (o) {
  21. return exports.filterValue(o, undefined);
  22. };
  23. exports.E = (errObj) => {
  24. return new TcbError(errObj);
  25. };
  26. exports.checkIsInScf = () => {
  27. const { TENCENTCLOUD_RUNENV } = cloudbase_1.CloudBase.getCloudbaseContext();
  28. return TENCENTCLOUD_RUNENV === 'SCF';
  29. };
  30. exports.checkIsInContainer = () => {
  31. return !!process.env.KUBERNETES_SERVICE_HOST;
  32. };
  33. function second() {
  34. // istanbul ignore next
  35. return Math.floor(new Date().getTime() / 1000);
  36. }
  37. exports.second = second;
  38. function processReturn(throwOnCode, res) {
  39. if (throwOnCode === false) {
  40. // 不抛报错,正常return,兼容旧逻辑
  41. return res;
  42. }
  43. throw exports.E(Object.assign({}, res));
  44. }
  45. exports.processReturn = processReturn;
  46. function getServerInjectUrl() {
  47. const tcbContextConfig = getTcbContextConfig();
  48. return tcbContextConfig['URL'] || '';
  49. }
  50. exports.getServerInjectUrl = getServerInjectUrl;
  51. function getTcbContextConfig() {
  52. try {
  53. const { TCB_CONTEXT_CNFG } = cloudbase_1.CloudBase.getCloudbaseContext();
  54. if (TCB_CONTEXT_CNFG) {
  55. // 检查约定环境变量字段是否存在
  56. return JSON.parse(TCB_CONTEXT_CNFG);
  57. }
  58. return {};
  59. }
  60. catch (e) {
  61. /* istanbul ignore next */
  62. console.log('parse context error...');
  63. /* istanbul ignore next */
  64. return {};
  65. }
  66. }
  67. exports.getTcbContextConfig = getTcbContextConfig;
  68. /* istanbul ignore next */
  69. function getWxUrl(config) {
  70. const protocal = config.isHttp === true ? 'http' : 'https';
  71. let wxUrl = protocal + '://tcb-open.tencentcloudapi.com/admin';
  72. if (exports.checkIsInScf()) {
  73. wxUrl = 'http://tcb-open.tencentyun.com/admin';
  74. }
  75. return wxUrl;
  76. }
  77. exports.getWxUrl = getWxUrl;