@fluyappgocore/commons-backend 1.0.212 → 1.0.213

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.
@@ -39,42 +39,87 @@ Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.getLicenseStatus = exports.licenseLoginGuard = exports.licenseWriteGuard = exports.licenseGuard = exports.getLicenseLimits = exports.isLicenseFeatureEnabled = void 0;
40
40
  var cache = null;
41
41
  var CACHE_TTL = 1000 * 60 * 60; // 1 hour
42
+ /**
43
+ * Where to fetch license data from. Two modes:
44
+ *
45
+ * - LICENSE_PROXY_URL set → in-cluster proxy (recommended).
46
+ * All MS except ms-entity hit `${LICENSE_PROXY_URL}` which resolves to
47
+ * `http://msentities-clusterip-srv:8092/api_entities/internal/license-status`.
48
+ * Only ms-entity itself actually talks to licensing.fluyapp.io. Dramatic
49
+ * reduction in outbound traffic (N × M → 1 × M).
50
+ *
51
+ * - LICENSE_URL set → direct fetch from the licensing server.
52
+ * Used by ms-entity (which IS the aggregator) and as fallback for any MS
53
+ * where the proxy is unreachable.
54
+ *
55
+ * If both are set, the proxy is tried first; on failure we fall back to the
56
+ * direct URL so a network issue between MS doesn't break licensing for
57
+ * everyone.
58
+ */
42
59
  function fetchLicense() {
43
60
  return __awaiter(this, void 0, void 0, function () {
44
- var licenseUrl, installationUuid, controller_1, timeout_1, res, data, _a;
45
- return __generator(this, function (_b) {
46
- switch (_b.label) {
61
+ function tryFetch(url, isProxy) {
62
+ return __awaiter(this, void 0, void 0, function () {
63
+ var controller_1, timeout_1, headers, res, data, _a;
64
+ return __generator(this, function (_b) {
65
+ switch (_b.label) {
66
+ case 0:
67
+ _b.trys.push([0, 3, , 4]);
68
+ controller_1 = new AbortController();
69
+ timeout_1 = setTimeout(function () { return controller_1.abort(); }, 5000);
70
+ headers = {};
71
+ if (isProxy && internalSecret)
72
+ headers["x-internal-secret"] = internalSecret;
73
+ return [4 /*yield*/, fetch(url, { signal: controller_1.signal, headers: headers })
74
+ .finally(function () { return clearTimeout(timeout_1); })];
75
+ case 1:
76
+ res = _b.sent();
77
+ if (!res.ok)
78
+ return [2 /*return*/, null];
79
+ return [4 /*yield*/, res.json()];
80
+ case 2:
81
+ data = _b.sent();
82
+ return [2 /*return*/, {
83
+ valid: data.valid,
84
+ readOnly: data.readOnly || false,
85
+ blocked: data.blocked || false,
86
+ features: data.features || {},
87
+ limits: data.limits || {},
88
+ tier: data.tier || "BASIC",
89
+ fetchedAt: Date.now(),
90
+ }];
91
+ case 3:
92
+ _a = _b.sent();
93
+ return [2 /*return*/, null];
94
+ case 4: return [2 /*return*/];
95
+ }
96
+ });
97
+ });
98
+ }
99
+ var proxyUrl, licenseUrl, installationUuid, internalSecret, cached, cached;
100
+ return __generator(this, function (_a) {
101
+ switch (_a.label) {
47
102
  case 0:
103
+ proxyUrl = process.env.LICENSE_PROXY_URL || "";
48
104
  licenseUrl = process.env.LICENSE_URL || "";
49
105
  installationUuid = process.env.INSTALLATION_UUID || process.env.ENTITY_UUID || "";
50
- if (!licenseUrl || !installationUuid)
51
- return [2 /*return*/, null];
52
- _b.label = 1;
106
+ internalSecret = process.env.INTERNAL_SECRET || "";
107
+ if (!proxyUrl) return [3 /*break*/, 2];
108
+ return [4 /*yield*/, tryFetch(proxyUrl, true)];
53
109
  case 1:
54
- _b.trys.push([1, 4, , 5]);
55
- controller_1 = new AbortController();
56
- timeout_1 = setTimeout(function () { return controller_1.abort(); }, 5000);
57
- return [4 /*yield*/, fetch(licenseUrl + "/api/license/validate/" + installationUuid, { signal: controller_1.signal }).finally(function () { return clearTimeout(timeout_1); })];
110
+ cached = _a.sent();
111
+ if (cached)
112
+ return [2 /*return*/, cached];
113
+ _a.label = 2;
58
114
  case 2:
59
- res = _b.sent();
60
- if (!res.ok)
61
- return [2 /*return*/, null];
62
- return [4 /*yield*/, res.json()];
115
+ if (!(licenseUrl && installationUuid)) return [3 /*break*/, 4];
116
+ return [4 /*yield*/, tryFetch(licenseUrl + "/api/license/validate/" + installationUuid, false)];
63
117
  case 3:
64
- data = _b.sent();
65
- return [2 /*return*/, {
66
- valid: data.valid,
67
- readOnly: data.readOnly || false,
68
- blocked: data.blocked || false,
69
- features: data.features || {},
70
- limits: data.limits || {},
71
- tier: data.tier || "BASIC",
72
- fetchedAt: Date.now(),
73
- }];
74
- case 4:
75
- _a = _b.sent();
76
- return [2 /*return*/, null];
77
- case 5: return [2 /*return*/];
118
+ cached = _a.sent();
119
+ if (cached)
120
+ return [2 /*return*/, cached];
121
+ _a.label = 4;
122
+ case 4: return [2 /*return*/, null];
78
123
  }
79
124
  });
80
125
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluyappgocore/commons-backend",
3
- "version": "1.0.212",
3
+ "version": "1.0.213",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",