zephyr-agent
Version:
Zephyr plugin agent
140 lines (139 loc) • 6.79 kB
JavaScript
;
var __webpack_require__ = {};
(()=>{
__webpack_require__.d = (exports1, getters, values)=>{
var define = (defs, kind)=>{
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
enumerable: true,
[kind]: defs[key]
});
};
define(getters, "get");
define(values, "value");
};
})();
(()=>{
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
__webpack_require__.r = (exports1)=>{
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
value: 'Module'
});
Object.defineProperty(exports1, '__esModule', {
value: true
});
};
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
getApplicationConfiguration: ()=>getApplicationConfiguration,
invalidateApplicationConfigCache: ()=>invalidateApplicationConfigCache
});
const external_zephyr_edge_contract_namespaceObject = require("zephyr-edge-contract");
const login_js_namespaceObject = require("../auth/login.js");
const index_js_namespaceObject = require("../errors/index.js");
const http_request_js_namespaceObject = require("../http/http-request.js");
const external_logging_index_js_namespaceObject = require("../logging/index.js");
const application_configuration_js_namespaceObject = require("../node-persist/application-configuration.js");
const token_js_namespaceObject = require("../node-persist/token.js");
const APPLICATION_CONFIG_TTL_MS = 60000;
const inFlightByIdentity = new Map();
const cachedConfigByIdentity = new Map();
function getApplicationIdentity(scope, application_uid) {
return JSON.stringify([
scope.apiEndpoint,
scope.apiGatewayEndpoint,
scope.environment,
scope.preview,
scope.principalFingerprint,
application_uid
]);
}
function isIdentityForApplication(identity, application_uid) {
try {
const parsed = JSON.parse(identity);
return Array.isArray(parsed) && parsed[parsed.length - 1] === application_uid;
} catch {
return false;
}
}
function isValidConfig(config, application_uid) {
return Boolean(config && config.application_uid === application_uid && (0, login_js_namespaceObject.isTokenStillValid)(config.jwt) && config.fetched_at && Date.now() - config.fetched_at <= APPLICATION_CONFIG_TTL_MS);
}
async function loadApplicationConfiguration({ application_uid }, scope, token) {
if (!application_uid) throw new index_js_namespaceObject.ZephyrError(index_js_namespaceObject.ZeErrors.ERR_MISSING_APPLICATION_UID);
const application_config_url = new URL(`${external_zephyr_edge_contract_namespaceObject.ze_api_gateway.application_config}/${application_uid}`, scope.apiGatewayEndpoint);
const [ok, cause, data] = await (0, http_request_js_namespaceObject.makeRequest)(application_config_url, {
headers: {
Authorization: `Bearer ${token}`
},
credentialToken: token
});
if (!ok || !data?.value || data.value.application_uid !== application_uid) throw new index_js_namespaceObject.ZephyrError(index_js_namespaceObject.ZeErrors.ERR_LOAD_APP_CONFIG, {
application_uid,
operation: 'get-application-config',
cause,
data: {
url: application_config_url.toString()
}
});
return {
...data.value,
fetched_at: Date.now()
};
}
async function getApplicationConfiguration({ application_uid }) {
try {
if (!application_uid) throw new index_js_namespaceObject.ZephyrError(index_js_namespaceObject.ZeErrors.ERR_MISSING_APPLICATION_UID);
const token = await (0, token_js_namespaceObject.getToken)();
const scope = (0, application_configuration_js_namespaceObject.getApplicationConfigStorageScope)(token);
const identity = getApplicationIdentity(scope, application_uid);
const cachedConfig = cachedConfigByIdentity.get(identity);
if (isValidConfig(cachedConfig, application_uid)) {
external_logging_index_js_namespaceObject.ze_log.app('Using cached application configuration');
return cachedConfig;
}
cachedConfigByIdentity.delete(identity);
const existingRequest = inFlightByIdentity.get(identity);
if (existingRequest) return existingRequest;
external_logging_index_js_namespaceObject.ze_log.app('Getting application configuration from node-persist');
const request = (async ()=>{
const storedAppConfig = await (0, application_configuration_js_namespaceObject.getAppConfig)(application_uid, scope);
if (isValidConfig(storedAppConfig, application_uid)) return storedAppConfig;
{
external_logging_index_js_namespaceObject.ze_log.app('Loading Application Configuration from API...');
const loadedAppConfig = await loadApplicationConfiguration({
application_uid
}, scope, token);
external_logging_index_js_namespaceObject.ze_log.app('Saving Application Configuration to node-persist...');
await (0, application_configuration_js_namespaceObject.saveAppConfig)(application_uid, loadedAppConfig, scope);
return loadedAppConfig;
}
})().then((config)=>{
cachedConfigByIdentity.set(identity, config);
return config;
}).finally(()=>{
if (inFlightByIdentity.get(identity) === request) inFlightByIdentity.delete(identity);
});
inFlightByIdentity.set(identity, request);
return await request;
} catch (error) {
throw index_js_namespaceObject.ZephyrError.withContext(error, 'get-application-config');
}
}
function invalidateApplicationConfigCache(application_uid) {
if (!application_uid) return void cachedConfigByIdentity.clear();
for (const identity of cachedConfigByIdentity.keys())if (isIdentityForApplication(identity, application_uid)) cachedConfigByIdentity.delete(identity);
}
exports.getApplicationConfiguration = __webpack_exports__.getApplicationConfiguration;
exports.invalidateApplicationConfigCache = __webpack_exports__.invalidateApplicationConfigCache;
for(var __rspack_i in __webpack_exports__)if (-1 === [
"getApplicationConfiguration",
"invalidateApplicationConfigCache"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
value: true
});
//# sourceMappingURL=get-application-configuration.js.map