1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.destinationCache = exports.getDestinationCacheKey = exports.getDestinationCacheKeyStrict = void 0;
|
7 | var util_1 = require("@sap-cloud-sdk/util");
|
8 | var moment_1 = __importDefault(require("moment"));
|
9 | var cache_1 = require("../cache");
|
10 | var tenant_1 = require("../tenant");
|
11 | var user_1 = require("../user");
|
12 | var logger = (0, util_1.createLogger)({
|
13 | package: 'core',
|
14 | messageContext: 'destination-cache'
|
15 | });
|
16 | var DestinationCache = function (cache) { return ({
|
17 | retrieveDestinationFromCache: function (decodedJwt, name, isolation) {
|
18 | return cache.get(getDestinationCacheKeyStrict(decodedJwt, name, isolation));
|
19 | },
|
20 | cacheRetrievedDestination: function (decodedJwt, destination, isolation) {
|
21 | cacheRetrievedDestination(decodedJwt, destination, isolation, cache);
|
22 | },
|
23 | cacheRetrievedDestinations: function (decodedJwt, retrievedDestinations, isolation) {
|
24 | retrievedDestinations.subaccount.forEach(function (dest) {
|
25 | return cacheRetrievedDestination(decodedJwt, dest, isolation, cache);
|
26 | });
|
27 | retrievedDestinations.instance.forEach(function (dest) {
|
28 | return cacheRetrievedDestination(decodedJwt, dest, isolation, cache);
|
29 | });
|
30 | },
|
31 | clear: function () {
|
32 | cache.clear();
|
33 | },
|
34 | getCacheInstance: function () { return cache; }
|
35 | }); };
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function getDestinationCacheKeyStrict(decodedJwt, destinationName, isolationStrategy) {
|
47 | if (isolationStrategy === void 0) { isolationStrategy = cache_1.IsolationStrategy.Tenant_User; }
|
48 | var tenant = (0, tenant_1.tenantId)(decodedJwt);
|
49 | var user = (0, user_1.userId)(decodedJwt);
|
50 | switch (isolationStrategy) {
|
51 | case cache_1.IsolationStrategy.No_Isolation:
|
52 | return "::".concat(destinationName);
|
53 | case cache_1.IsolationStrategy.Tenant:
|
54 | if (tenant) {
|
55 | return "".concat(tenant, "::").concat(destinationName);
|
56 | }
|
57 | logger.warn("Cannot get cache key. Isolation strategy ".concat(isolationStrategy, " is used, but tenant id is undefined."));
|
58 | return;
|
59 | case cache_1.IsolationStrategy.User:
|
60 | if (user) {
|
61 | return ":".concat(user, ":").concat(destinationName);
|
62 | }
|
63 | logger.warn("Cannot get cache key. Isolation strategy ".concat(isolationStrategy, " is used, but user id is undefined."));
|
64 | return;
|
65 | case cache_1.IsolationStrategy.Tenant_User:
|
66 | if (tenant && user) {
|
67 | return "".concat(user, ":").concat(tenant, ":").concat(destinationName);
|
68 | }
|
69 | logger.warn("Cannot get cache key. Isolation strategy ".concat(isolationStrategy, " is used, but tenant id or user id is undefined."));
|
70 | return;
|
71 | default:
|
72 | logger.warn("Cannot get cache key. Isolation strategy ".concat(isolationStrategy, " is not supported."));
|
73 | return;
|
74 | }
|
75 | }
|
76 | exports.getDestinationCacheKeyStrict = getDestinationCacheKeyStrict;
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function getDestinationCacheKey(decodedJwt, destinationName, isolationStrategy) {
|
89 | switch (isolationStrategy) {
|
90 | case cache_1.IsolationStrategy.No_Isolation:
|
91 | return "::".concat(destinationName);
|
92 | case cache_1.IsolationStrategy.Tenant_User:
|
93 | return "".concat((0, tenant_1.tenantId)(decodedJwt), ":").concat((0, user_1.userId)(decodedJwt), ":").concat(destinationName);
|
94 | case cache_1.IsolationStrategy.User:
|
95 | return ":".concat((0, user_1.userId)(decodedJwt), ":").concat(destinationName);
|
96 | default:
|
97 | return "".concat((0, tenant_1.tenantId)(decodedJwt), "::").concat(destinationName);
|
98 | }
|
99 | }
|
100 | exports.getDestinationCacheKey = getDestinationCacheKey;
|
101 | function cacheRetrievedDestination(decodedJwt, destination, isolation, cache) {
|
102 | var _a;
|
103 | if (!destination.name) {
|
104 | throw new Error('The destination name is undefined.');
|
105 | }
|
106 | var key = getDestinationCacheKeyStrict(decodedJwt, destination.name, isolation);
|
107 | var expiresIn = (_a = (0, util_1.first)(destination.authTokens || [])) === null || _a === void 0 ? void 0 : _a.expiresIn;
|
108 | var expirationTime = expiresIn
|
109 | ? (0, moment_1.default)().add(expiresIn, 'second').unix() * 1000
|
110 | : undefined;
|
111 | cache.set(key, destination, expirationTime);
|
112 | }
|
113 | exports.destinationCache = DestinationCache(new cache_1.Cache({ hours: 0, minutes: 5, seconds: 0 }));
|
114 |
|
\ | No newline at end of file |