UNPKG

@sap-cloud-sdk/connectivity

Version:

SAP Cloud SDK for JavaScript connectivity

62 lines 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerDestinationCache = exports.RegisterDestinationCache = void 0; const promises_1 = require("fs/promises"); const crypto_1 = require("crypto"); const util_1 = require("@sap-cloud-sdk/util"); const async_cache_1 = require("../async-cache"); const destination_cache_1 = require("./destination-cache"); const logger = (0, util_1.createLogger)('register-destination-cache'); class DefaultMtlsCache extends async_cache_1.AsyncCache { constructor(defaultValidityTime = 300000) { super(defaultValidityTime); } } /** * @internal */ const MtlsCache = (mtlsCache = new DefaultMtlsCache()) => { const that = { useMtlsCache: false, retrieveMtlsOptionsFromCache: async () => mtlsCache.get('mtlsOptions'), cacheMtlsOptions: async () => { const getCert = (0, promises_1.readFile)(process.env.CF_INSTANCE_CERT, 'utf8'); const getKey = (0, promises_1.readFile)(process.env.CF_INSTANCE_KEY, 'utf8'); const [cert, key] = await Promise.all([getCert, getKey]); mtlsCache.set('mtlsOptions', { entry: { cert, key }, expires: getCertExpirationDate(cert) }); }, getMtlsOptions: async () => { let mtlsOptions = await that.retrieveMtlsOptionsFromCache(); if (!mtlsOptions) { await that.cacheMtlsOptions(); mtlsOptions = await that.retrieveMtlsOptionsFromCache(); if (!mtlsOptions) { logger.warn('Neither the previous nor the current mtls certificate is valid anymore.'); } } return mtlsOptions || {}; }, clear: async () => mtlsCache.clear(), getCacheInstance: () => mtlsCache }; return that; }; function getCertExpirationDate(cert) { return Number(new crypto_1.X509Certificate(cert).validTo); } /** * @internal */ const RegisterDestinationCache = () => ({ destination: (0, destination_cache_1.DestinationCache)(new destination_cache_1.DefaultDestinationCache(0)), mtls: MtlsCache() }); exports.RegisterDestinationCache = RegisterDestinationCache; /** * @internal */ exports.registerDestinationCache = (0, exports.RegisterDestinationCache)(); //# sourceMappingURL=register-destination-cache.js.map