"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var i=Object.getOwnPropertyDescriptor(t,r);i&&!("get"in i?!t.__esModule:i.writable||i.configurable)||(i={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,i)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.unpack=void 0;const path_1=__importDefault(require("path")),fs_extra_1=__importDefault(require("fs-extra")),rimraf_1=__importDefault(require("rimraf")),unzipper_1=__importDefault(require("unzipper")),Error_1=__importStar(require("../../../common/Error")),i18n_1=__importDefault(require("../../../i18n")),TAG_LEN=4;let DIST="";async function writeFile(e,t){try{return await fs_extra_1.default.ensureFile(e),fs_extra_1.default.writeFile(e,t)}catch(e){throw e}}async function writeFiles(e){try{const t=Object.keys(e);for(let r=0;r<t.length;r++){const n=t[r];await writeFile(n,e[n])}}catch(e){throw e}}function readBytes(e,t,r){const n=t+r;if(n>e.length)throw new Error(i18n_1.default.t("UNPACK_FAIL_EXCEED_LIMIT_ERROR"));return e.slice(t,n)}function decodeHeader(e,t,r){if(!e)throw new Error(i18n_1.default.t("UNPACK_FAIL_NO_DATA_DECODE_ERROR"));const n=readBytes(e,0,r).toString();t+=r;const i=e.readUInt32LE(t);t+=4;const _=e.readUInt32LE(t);return{tag:n,version:i,ext:readBytes(e,t+=4,_).toString(),offset:t+=_}}function decodeBody(e,t){if(!e)throw new Error(i18n_1.default.t("UNPACK_FAIL_NO_DATA_DECODE_ERROR"));const r=e.readUInt32LE(t);t+=4;const n={};for(let i=0;i<r;i++){const r=e.readUInt32LE(t),i=readBytes(e,t+=4,r).toString();t+=r;const _=e.readUInt32LE(t);t+=4;const a=e.readUInt32LE(t);t+=4;const o=readBytes(e,_,a);n[path_1.default.join(DIST,i)]=o}return n}function unZip(e,t){return new Promise((r,n)=>{fs_extra_1.default.createReadStream(e).pipe(unzipper_1.default.Extract({path:t})).on("error",n).on("finish",r)})}async function unpack(e,t,r="pkg"){if(!e)throw new Error_1.default({code:Error_1.CoreErrorCode.UNPACK_FAIL,message:i18n_1.default.t("UNPACK_FAIL_ERROR",{errorStr:i18n_1.default.t("UNPACK_FAIL_INVALID_PKG_PATH_ERROR")}),advise:i18n_1.default.t("UNPACK_FAIL_INVALID_PKG_PATH_ADVISE")});t||(t=path_1.default.join(__dirname,"../../public/__unpack__"));let n=0;try{if(rimraf_1.default.sync(t),DIST=t,"pkg"===r){const t=fs_extra_1.default.readFileSync(e);n=decodeHeader(t,n,4).offset;const r=decodeBody(t,n);await writeFiles(r)}else"zip"===r&&await unZip(e,t)}catch(e){throw new Error_1.default({code:Error_1.CoreErrorCode.UNPACK_FAIL,message:i18n_1.default.t("UNPACK_FAIL_ERROR",{errorStr:e.message}),advise:i18n_1.default.t("UNPACK_FAIL_ERROR_ADVISE")})}}exports.unpack=unpack;