UNPKG

zephyr-agent

Version:
122 lines (121 loc) • 5.06 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__, { createSnapshotUploadTargets: ()=>createSnapshotUploadTargets, uploadSnapshot: ()=>uploadSnapshot }); const external_zephyr_edge_contract_namespaceObject = require("zephyr-edge-contract"); const get_application_configuration_js_namespaceObject = require("../edge-requests/get-application-configuration.js"); const index_js_namespaceObject = require("../errors/index.js"); const external_logging_index_js_namespaceObject = require("../logging/index.js"); const external_http_request_js_namespaceObject = require("./http-request.js"); const MAX_SNAPSHOT_TARGET_CONCURRENCY = 3; function canonicalEdgeUrl(edgeUrl) { return new URL(edgeUrl).toString(); } function snapshotForTarget(snapshot, edgeUrl) { return { ...snapshot, domain: edgeUrl }; } function createSnapshotUploadTargets(snapshot, config) { const targets = new Map(); const addTarget = (edgeUrl)=>{ const canonicalUrl = canonicalEdgeUrl(edgeUrl); if (targets.has(canonicalUrl)) return; targets.set(canonicalUrl, { edgeUrl, snapshot: snapshotForTarget(snapshot, edgeUrl) }); }; addTarget(config.EDGE_URL); for (const [, target] of Object.entries(config.ENVIRONMENTS ?? {}).sort(([left], [right])=>left.localeCompare(right)))addTarget(target.edgeUrl); return [ ...targets.values() ].map(({ edgeUrl, snapshot: targetSnapshot })=>({ edgeUrl, snapshot: targetSnapshot })); } async function uploadSnapshot({ body, application_uid }) { const config = await (0, get_application_configuration_js_namespaceObject.getApplicationConfiguration)({ application_uid }); const targets = createSnapshotUploadTargets(body, config); const [primary, ...additionalTargets] = targets; if (!primary) throw new index_js_namespaceObject.ZephyrError(index_js_namespaceObject.ZeErrors.ERR_FAILED_UPLOAD, { type: 'snapshot', cause: new Error('No snapshot upload target was configured.') }); external_logging_index_js_namespaceObject.ze_log.snapshot('Sending target-specific snapshot to edge:', JSON.stringify(primary.snapshot, null, 2)); const resp = await doUploadSnapshotRequest({ json: JSON.stringify(primary.snapshot), edge_url: primary.edgeUrl, jwt: config.jwt }); await (0, external_zephyr_edge_contract_namespaceObject.forEachLimit)(additionalTargets.map((target)=>()=>doUploadSnapshotRequest({ json: JSON.stringify(target.snapshot), edge_url: target.edgeUrl, jwt: config.jwt })), MAX_SNAPSHOT_TARGET_CONCURRENCY); external_logging_index_js_namespaceObject.ze_log.snapshot('Done: snapshot uploaded'); return resp; } async function doUploadSnapshotRequest({ json, edge_url, jwt }) { const options = { method: 'POST', headers: { 'Content-Length': Buffer.byteLength(json).toString(), 'Content-Type': 'application/json; charset=utf-8', can_write_jwt: jwt } }; const url = new URL('/upload', edge_url); url.searchParams.append('type', 'snapshot'); url.searchParams.append('skip_assets', 'true'); external_logging_index_js_namespaceObject.ze_log.snapshot('Upload URL:', url.toString()); const [ok, cause, resp] = await (0, external_http_request_js_namespaceObject.makeRequest)(url, options, json); if (!ok) throw new index_js_namespaceObject.ZephyrError(index_js_namespaceObject.ZeErrors.ERR_FAILED_UPLOAD, { type: 'snapshot', cause }); return resp; } exports.createSnapshotUploadTargets = __webpack_exports__.createSnapshotUploadTargets; exports.uploadSnapshot = __webpack_exports__.uploadSnapshot; for(var __rspack_i in __webpack_exports__)if (-1 === [ "createSnapshotUploadTargets", "uploadSnapshot" ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i]; Object.defineProperty(exports, '__esModule', { value: true }); //# sourceMappingURL=upload-snapshot.js.map