"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.uploadToCosCore=uploadToCosCore,exports.uploadToCosInCI=uploadToCosInCI;const COSForNode=require("cos-nodejs-sdk-v5"),fs=require("fs");function compareCache(e,o){const t=Object.keys(o);for(const r of Object.keys(e)){if(!t.includes(r))return!1;if(o[r]!=e[r])return!1}return!0}async function uploadToCosCore({uploadBuf:e,region:o,getUploadInfoFunc:t,getAuthFunc:r,onProgress:i}){return new Promise((n,s)=>{t().then(t=>{let u=null;const c=new COSForNode({getAuthorization(e,o){var i;if(1!=e.Scope.length)return void o("");const n=Object.assign({},t,{action:e.Scope[0].action});(null===(i=e.Query)||void 0===i?void 0:i.uploadId)&&(n.uploadId=e.Query.uploadId),null!==u&&compareCache(n,u)&&u.AuthExpireTime>Math.round(Date.now()/1e3)?o({Authorization:u.Authorization,SecurityToken:u.SecurityToken,SignFrom:"client"}):r(n).then(e=>{u=Object.assign({},n,{Authorization:e.Authorization,SecurityToken:e.SecurityToken,SignFrom:"client",AuthExpireTime:e.AuthExpireTime}),o({Authorization:e.Authorization,SecurityToken:e.SecurityToken,SignFrom:"client"})},()=>{o("")})}});let l=void 0,a={};if("string"==typeof e){if(!fs.existsSync(e)||fs.statSync(e).isDirectory())throw new Error(`[upload Cos failed] file path$ ${e} is not a file or not exists`);a.name=e}else try{const o=require("tmp");o.setGracefulCleanup(),a=o.fileSync({mode:420,prefix:"",postfix:""}),fs.writeSync(a.fd,e,0,e.length,null),fs.closeSync(a.fd)}catch(e){throw console.error("Error:",e),new Error("[upload Cos failed] write compileresult to tmp file failed: "+e.message)}c.sliceUploadFile({Bucket:t.bucket,Region:o,Key:t.object,Body:l,FilePath:a.name,SliceSize:5242880,onProgress(e){null==i||i(e)},AsyncLimit:1},(e,o)=>{e?s({err:e,data:o}):n({err:e,data:o})})},e=>{s({err:e})})})}function uploadToCosInCI(){}