UNPKG

zephyr-agent

Version:
194 lines (193 loc) • 8.45 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__, { awsUploadStrategy: ()=>awsUploadStrategy, createBucket: ()=>createBucket }); const external_zephyr_edge_contract_namespaceObject = require("zephyr-edge-contract"); const index_js_namespaceObject = require("../edge-actions/index.js"); const distributed_hash_control_js_namespaceObject = require("../edge-hash-list/distributed-hash-control.js"); const get_application_configuration_js_namespaceObject = require("../edge-requests/get-application-configuration.js"); const external_errors_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 picocolor_js_namespaceObject = require("../logging/picocolor.js"); const external_upload_base_index_js_namespaceObject = require("./upload-base/index.js"); const AWS_MAX_BODY_SIZE = 20971520; const AWS_UPLOAD_CONCURRENCY = 6; async function awsUploadStrategy(zephyr_engine, { snapshot, getDashData, assets: { assetsMap, missingAssets } }) { const snapshotSize = Buffer.byteLength(JSON.stringify(snapshot), 'utf8'); if (snapshotSize > AWS_MAX_BODY_SIZE) { const opts = { entity_name: 'snapshot', entity_size: snapshotSize, max_allowed_size: AWS_MAX_BODY_SIZE }; throw new external_errors_index_js_namespaceObject.ZephyrError(external_errors_index_js_namespaceObject.ZeErrors.ERR_MAX_PAYLOAD_SIZE_EXCEEDED, opts); } for (const missingAsset of missingAssets)if (missingAsset.size > AWS_MAX_BODY_SIZE) { const opts = { entity_name: missingAsset.path, entity_size: missingAsset.size, max_allowed_size: AWS_MAX_BODY_SIZE }; throw new external_errors_index_js_namespaceObject.ZephyrError(external_errors_index_js_namespaceObject.ZeErrors.ERR_MAX_PAYLOAD_SIZE_EXCEEDED, opts); } await createBucket(zephyr_engine.application_uid); await uploadAssets(zephyr_engine, { assetsMap, missingAssets }).catch((error)=>{ if (error.cause?.template?.content === 'Not Implemented') return (0, external_upload_base_index_js_namespaceObject.uploadAssets)(zephyr_engine, { assetsMap, missingAssets }); throw error; }); const versionUrl = await (0, index_js_namespaceObject.zeUploadSnapshot)(zephyr_engine, { snapshot }); await (0, external_upload_base_index_js_namespaceObject.uploadBuildStatsAndEnableEnvs)(zephyr_engine, { getDashData, versionUrl }); return versionUrl; } async function createBucket(application_uid) { const { EDGE_URL, jwt } = await (0, get_application_configuration_js_namespaceObject.getApplicationConfiguration)({ application_uid }); const options = { method: 'POST', headers: { can_write_jwt: jwt } }; const url = new URL('/upload', EDGE_URL); url.searchParams.append('type', 'bucket'); const [ok, cause] = await (0, http_request_js_namespaceObject.makeRequest)(url, options); if (!ok) throw new external_errors_index_js_namespaceObject.ZephyrError(external_errors_index_js_namespaceObject.ZeErrors.ERR_FAILED_UPLOAD, { type: 'bucket', cause }); external_logging_index_js_namespaceObject.ze_log.snapshot('Done: bucket initialized...'); } async function uploadAssets(zephyr_engine, { assetsMap, missingAssets }) { const upload_success = await zeUploadAssets(zephyr_engine, { missingAssets, assetsMap }); if (upload_success && missingAssets.length) { const application_uid = zephyr_engine.application_uid; await (0, distributed_hash_control_js_namespaceObject.update_hash_list)(application_uid, assetsMap); } return upload_success; } async function zeUploadAssets(zephyr_engine, { missingAssets, assetsMap }) { const count = Object.keys(assetsMap).length; const logger = await zephyr_engine.logger; const appConfig = await zephyr_engine.application_configuration; if (0 === missingAssets.length) { logger({ level: 'info', action: 'snapshot:assets:upload:empty', message: `No assets to upload, ${(0, picocolor_js_namespaceObject.white)('skipping')}...` }); return true; } const start = Date.now(); let totalSize = 0; await (0, external_zephyr_edge_contract_namespaceObject.forEachLimit)(missingAssets.map((asset)=>()=>upload_missing_asset(asset)), AWS_UPLOAD_CONCURRENCY); logger({ level: 'info', action: 'snapshot:assets:upload:done', message: (0, picocolor_js_namespaceObject.white)(`(${(0, picocolor_js_namespaceObject.whiteBright)(missingAssets.length.toString())}/${(0, picocolor_js_namespaceObject.white)(count.toString())} assets uploaded in ${(0, picocolor_js_namespaceObject.whiteBright)((Date.now() - start).toString())}ms, ${(0, picocolor_js_namespaceObject.whiteBright)(totalSize.toFixed(2))}kb)`) }); return true; async function upload_missing_asset(asset) { const start = Date.now(); const assetWithBuffer = assetsMap[asset.hash]; const assetSize = assetWithBuffer?.buffer?.length / 1024; const result = await getUploadUrl({ hash: asset.hash, asset: assetWithBuffer }, appConfig); if (result.message) return; const [ok, cause] = await (0, http_request_js_namespaceObject.makeRequest)(result.url, { method: 'PUT', headers: { 'Content-Type': result.contentType } }, asset.buffer); if (!ok) throw new external_errors_index_js_namespaceObject.ZephyrError(external_errors_index_js_namespaceObject.ZeErrors.ERR_FAILED_UPLOAD, { type: 'file', cause }); const fileUploaded = Date.now() - start; totalSize += assetSize; external_logging_index_js_namespaceObject.ze_log.upload(`file ${asset.path} uploaded in ${fileUploaded}ms (${assetSize.toFixed(2)}kb)`); } async function getUploadUrl({ hash, asset }, { EDGE_URL, jwt }) { const type = 'uploadUrl'; const options = { method: 'POST', headers: { 'x-file-size': asset.size.toString(), 'x-file-path': asset.path, can_write_jwt: jwt, 'Content-Type': 'application/octet-stream' } }; const [ok, cause, data] = await (0, http_request_js_namespaceObject.makeRequest)({ path: '/upload', base: EDGE_URL, query: { type, hash, filename: asset.path } }, options); if (!ok) throw new external_errors_index_js_namespaceObject.ZephyrError(external_errors_index_js_namespaceObject.ZeErrors.ERR_FAILED_UPLOAD, { type, cause }); return data; } } exports.awsUploadStrategy = __webpack_exports__.awsUploadStrategy; exports.createBucket = __webpack_exports__.createBucket; for(var __rspack_i in __webpack_exports__)if (-1 === [ "awsUploadStrategy", "createBucket" ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i]; Object.defineProperty(exports, '__esModule', { value: true }); //# sourceMappingURL=aws-upload.strategy.js.map