UNPKG

zephyr-agent

Version:
122 lines (121 loc) • 5.84 kB
"use strict"; 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__, { getAppConfig: ()=>getAppConfig, getApplicationConfigPrincipalFingerprint: ()=>getApplicationConfigPrincipalFingerprint, getApplicationConfigStorageScope: ()=>getApplicationConfigStorageScope, removeAppConfig: ()=>removeAppConfig, saveAppConfig: ()=>saveAppConfig }); const external_node_crypto_namespaceObject = require("node:crypto"); const external_zephyr_edge_contract_namespaceObject = require("zephyr-edge-contract"); const index_js_namespaceObject = require("../errors/index.js"); const external_storage_keys_js_namespaceObject = require("./storage-keys.js"); const external_storage_js_namespaceObject = require("./storage.js"); const STORED_APPLICATION_CONFIG_VERSION = 2; const PRINCIPAL_FINGERPRINT_CONTEXT = 'zephyr-application-config-principal\0'; function normalizeEndpoint(endpoint) { return new URL(endpoint).toString(); } function getApplicationConfigPrincipalFingerprint(token) { return (0, external_node_crypto_namespaceObject.createHash)('sha256').update(PRINCIPAL_FINGERPRINT_CONTEXT).update(token ?? '<anonymous>').digest('base64url'); } function getApplicationConfigStorageScope(token) { return Object.freeze({ apiEndpoint: normalizeEndpoint((0, external_zephyr_edge_contract_namespaceObject.ZEPHYR_API_ENDPOINT)()), apiGatewayEndpoint: normalizeEndpoint((0, external_zephyr_edge_contract_namespaceObject.ZE_API_ENDPOINT)()), environment: (0, external_zephyr_edge_contract_namespaceObject.ZE_ENV)()?.trim() || null, preview: Boolean((0, external_zephyr_edge_contract_namespaceObject.ZE_IS_PREVIEW)()), principalFingerprint: getApplicationConfigPrincipalFingerprint(token) }); } function scopesMatch(left, right) { return left.apiEndpoint === right.apiEndpoint && left.apiGatewayEndpoint === right.apiGatewayEndpoint && left.environment === right.environment && left.preview === right.preview && left.principalFingerprint === right.principalFingerprint; } function scopeKey(scope) { return (0, external_node_crypto_namespaceObject.createHash)('sha256').update(JSON.stringify(scope)).digest('base64url'); } function get_key(application_uid, scope) { return [ external_storage_keys_js_namespaceObject.StorageKeys.ze_app_config_token, scopeKey(scope), application_uid ].join('.'); } function isStoredApplicationConfig(value, application_uid, scope) { if (!value || 'object' != typeof value) return false; const stored = value; return Boolean(stored.version === STORED_APPLICATION_CONFIG_VERSION && stored.applicationUid === application_uid && stored.scope && scopesMatch(stored.scope, scope) && stored.config && stored.config.application_uid === application_uid); } async function saveAppConfig(application_uid, json, scope = getApplicationConfigStorageScope()) { if (json.application_uid !== application_uid) throw new index_js_namespaceObject.ZephyrError(index_js_namespaceObject.ZeErrors.ERR_LOAD_APP_CONFIG, { application_uid, data: { reason: 'application UID mismatch' } }); await external_storage_js_namespaceObject.storage; const stored = { version: STORED_APPLICATION_CONFIG_VERSION, applicationUid: application_uid, scope: { ...scope }, config: json }; await (0, external_storage_js_namespaceObject.setPrivateItem)(get_key(application_uid, scope), stored, { ttl: 300000 }); } async function getAppConfig(application_uid, scope = getApplicationConfigStorageScope()) { await external_storage_js_namespaceObject.storage; const stored = await (0, external_storage_js_namespaceObject.getItem)(get_key(application_uid, scope)); return isStoredApplicationConfig(stored, application_uid, scope) ? stored.config : void 0; } async function removeAppConfig(application_uid, scope = getApplicationConfigStorageScope()) { await external_storage_js_namespaceObject.storage; await (0, external_storage_js_namespaceObject.removeItem)(get_key(application_uid, scope)); } exports.getAppConfig = __webpack_exports__.getAppConfig; exports.getApplicationConfigPrincipalFingerprint = __webpack_exports__.getApplicationConfigPrincipalFingerprint; exports.getApplicationConfigStorageScope = __webpack_exports__.getApplicationConfigStorageScope; exports.removeAppConfig = __webpack_exports__.removeAppConfig; exports.saveAppConfig = __webpack_exports__.saveAppConfig; for(var __rspack_i in __webpack_exports__)if (-1 === [ "getAppConfig", "getApplicationConfigPrincipalFingerprint", "getApplicationConfigStorageScope", "removeAppConfig", "saveAppConfig" ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i]; Object.defineProperty(exports, '__esModule', { value: true }); //# sourceMappingURL=application-configuration.js.map