UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.destinationCache = exports.getDestinationCacheKey = exports.getDestinationCacheKeyStrict = void 0;
7var util_1 = require("@sap-cloud-sdk/util");
8var moment_1 = __importDefault(require("moment"));
9var cache_1 = require("../cache");
10var tenant_1 = require("../tenant");
11var user_1 = require("../user");
12var logger = (0, util_1.createLogger)({
13 package: 'core',
14 messageContext: 'destination-cache'
15});
16var 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 * Calculates a cache key based on the jwt and destination name for the given isolation strategy.
38 * Cache keys for strategies are non-overlapping, i.e. using a cache key for strategy [[IsolationStrategy.Tenant]]
39 * will not result in a cache hit for a destination that has been cached with strategy [[IsolationStrategy.Tenant_User]].
40 * @param decodedJwt - The decoded JWT of the current request.
41 * @param destinationName - The name of the destination.
42 * @param isolationStrategy - The strategy used to isolate cache entries.
43 * @returns The cache key.
44 * @hidden
45 */
46function 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}
76exports.getDestinationCacheKeyStrict = getDestinationCacheKeyStrict;
77/**
78 * @deprecated Since v1.52.0. Use [[getDestinationCacheKeyStrict]] instead.
79 * Calculates a cache key based on the jwt and destination name for the given isolation strategy.
80 * Cache keys for strategies are non-overlapping, i.e. using a cache key for strategy [[IsolationStrategy.Tenant]]
81 * will not result in a cache hit for a destination that has been cached with strategy [[IsolationStrategy.Tenant_User]].
82 * @param decodedJwt - The decoded JWT of the current request.
83 * @param destinationName - The name of the destination.
84 * @param isolationStrategy - The strategy used to isolate cache entries.
85 * @returns The cache key.
86 * @hidden
87 */
88function 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}
100exports.getDestinationCacheKey = getDestinationCacheKey;
101function 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}
113exports.destinationCache = DestinationCache(new cache_1.Cache({ hours: 0, minutes: 5, seconds: 0 }));
114//# sourceMappingURL=destination-cache.js.map
\No newline at end of file