"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MIN_COS_UPLOAD_SIZE=void 0,exports.upload=upload,exports.innerUpload=innerUpload;const tslib_1=require("tslib"),log=tslib_1.__importStar(require("../utils/log")),request_1=require("../utils/request"),pack_1=require("./utils/pack"),zlib_1=tslib_1.__importDefault(require("zlib")),sign_1=require("../utils/sign"),tools_1=require("../utils/tools"),config_1=require("../config/config"),taskstatus_1=require("../utils/taskstatus"),error_1=require("../utils/error"),locales_1=tslib_1.__importDefault(require("../utils/locales/locales")),querystring_1=tslib_1.__importDefault(require("querystring")),progressupdate_1=require("./utils/progressupdate"),url_config_1=require("../config/url.config"),jsonParse_1=require("../utils/jsonParse"),cache_1=require("../utils/cache"),cos_upload_1=require("./cos-upload"),getcompiler_1=require("./getcompiler"),tools_2=require("./utils/tools"),index_1=require("../utils/asyncTask/index"),businesstype_1=require("../config/businesstype");async function upload(e){var o,r;const t=await innerUpload(e);(null===(o=t.respBody)||void 0===o?void 0:o.dev_plugin_id)&&(log.log("Development Version Plugin ID: "+t.respBody.dev_plugin_id),t.devPluginId=t.respBody.dev_plugin_id);const i={subPackageInfo:t.subPackageInfo,pluginInfo:t.pluginInfo,devPluginId:t.devPluginId};return(null===(r=t.respBody)||void 0===r?void 0:r.str_uint64version)&&(i.strUint64Version=t.respBody.str_uint64version),i}async function innerUpload(e){const{project:o,setting:r={useProjectConfig:!1},desc:t=`robot ${e.robot||"1"} use miniprogram-ci to upload at ${(0,tools_1.formatTime)(new Date)}`,version:i="",robot:s="1",test:n,pagePath:a,searchQuery:l,threads:u=0,bigPackageSizeSupport:p,onProgressUpdate:_=(()=>{})}=e,d=(0,progressupdate_1.transProgressUpdate)(_);let{useCOS:c}=e;if(process.env.COMPILE_THREADS=u.toString(),!i)throw new error_1.CodeError(locales_1.default.config.PARAM_ERROR.format("upload","version"),config_1.PARAM_ERROR);if(!o)throw new error_1.CodeError(locales_1.default.config.PARAM_ERROR.format("upload","project"),config_1.PARAM_ERROR);cache_1.cacheManager.clean();const g=await(0,getcompiler_1.getCompiler)(o,r),f=(0,tools_2.formatCISetting)(o,r),y=await g.compile({setting:f,onProgressUpdate:d,resultType:"prod",disableSpreadingUsingComponents:!0}),b=config_1.CI_VERSION,S={codeprotect:r.codeProtect?1:0,type:o.type,appid:o.appid,version:i,desc:t,robot:s},P={scene:e.scene||1011};let q;a&&(P.path=a,S.path=a),l&&(P.query=querystring_1.default.parse(l)),S.debugLaunchInfo=JSON.stringify(P),a&&l&&(S.path+="?"+l),n&&p&&(S.bigPackageSizeSupport=1);let m={};try{q=await o.getFile(o.miniprogramRoot,"ext.json"),m=JSON.parse(q.toString("utf-8"))}catch(e){}if(m&&(m.extEnable&&(S.extAppId=m.extAppid),m.directCommit)){let e="";e=m.extEnable?"The code will be uploaded into the waiting list of extAppid.":"The code will be uploaded into the draft box of the third-party platform.",log.warn(e)}try{const r=new taskstatus_1.TaskStatus(locales_1.default.config.UPLOAD.toString());d(r);const t=`${n?url_config_1.TEST_SOURCE_URL:url_config_1.UPLOAD_URL}?${querystring_1.default.stringify(S)}`;let i,a=!1,l=0;const u=(0,pack_1.pack)(y),p=zlib_1.default.gzipSync(u.buffer);if(log.info("useCOS parameter: ",c),log.info("upload zip buffer size: ",p.length),void 0===c&&(c=p.length>exports.MIN_COS_UPLOAD_SIZE),c){log.info("upload by COS: ",c);const r=await(0,cos_upload_1.uploadByCosSafely)(p,t,o,s,n?businesstype_1.BUSINESS_TYPE.BusiType_TestSource:businesstype_1.BUSINESS_TYPE.BusiType_CommitSource,e.onProgressUpdate);r.fallback?(a=r.fallback,log.info("upload by COS failed, fallback to http way")):(i={body:r.body},l=r.uploadCOSCostTime)}if(!c||a){const e=await(0,sign_1.getSignature)(o.privateKey,o.appid);y[index_1.SIGNATURE_FILE_NAME]=JSON.stringify({signature:e,version:b});const r=(0,pack_1.pack)(y),s=zlib_1.default.gzipSync(r.buffer);log.info("request url:",t);const n=(await(0,request_1.request)({url:t,method:"post",body:s})).body.toString();if(i=(0,jsonParse_1.jsonRespParse)(n,t),0!==i.errCode)throw new Error(n)}r.done(),d(r);const _={respBody:i.body};if(Array.isArray(i.body.subpackage_info)){const e=i.body.subpackage_info;_.subPackageInfo=e}if(Array.isArray(i.body.ext_plugin_info)){const e=i.body.ext_plugin_info;_.pluginInfo=e.map(e=>({pluginProviderAppid:e.provider,version:e.version,size:e.size}))}return g.destroy(),_}catch(e){throw new error_1.CodeError(e.toString(),config_1.UPLOAD_CGI_ERR)}}exports.MIN_COS_UPLOAD_SIZE=5242880;