/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@suntree/react-native-update@6.1.4/lib/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import{tryBackupEndpoints,getCheckUrl,setCustomEndpoints}from"./endpoint";import{NativeEventEmitter,NativeModules,Platform,PermissionsAndroid}from"react-native";export{setCustomEndpoints};const{version:v}=require("react-native/Libraries/Core/ReactNativeVersion"),RNVersion=`${v.major}.${v.minor}.${v.patch}`;let Pushy=NativeModules.Pushy;if(!Pushy)throw new Error("react-native-update模块无法加载，请对照安装文档检查配置。");export const downloadRootDir=Pushy.downloadRootDir;export const packageVersion=Pushy.packageVersion;export const currentVersion=Pushy.currentVersion;export const isFirstTime=Pushy.isFirstTime;export const isRolledBack=Pushy.isRolledBack;export const buildTime=Pushy.buildTime;let blockUpdate=Pushy.blockUpdate,uuid=Pushy.uuid;if("android"===Platform.OS&&!Pushy.isUsingBundleUrl)throw new Error("react-native-update模块无法加载，请对照文档检查Bundle URL的配置");const eventEmitter=new NativeEventEmitter(Pushy);function logger(e){console.log(`Pushy: ${e}`)}uuid||(uuid=require("uuid/v4")(),Pushy.setUuid(uuid)),logger("uuid: "+uuid);export const cInfo={pushy:require("../package.json").version,rn:RNVersion,os:Platform.OS+" "+Platform.Version,uuid:uuid};function assertRelease(){if(__DEV__)throw new Error("react-native-update can only run on RELEASE version.")}let checkingThrottling=!1;export async function checkUpdate(e,o){if(assertRelease(),checkingThrottling)return void logger("repeated checking, ignored");if(checkingThrottling=!0,setTimeout((()=>{checkingThrottling=!1}),3e3),blockUpdate&&blockUpdate.until>Date.now()/1e3)throw new Error(`热更新已暂停，原因：${blockUpdate.reason}。请在"${new Date(1e3*blockUpdate.until).toLocaleString()}"之后重试。`);if("string"!=typeof e)throw new Error("未检查到合法的APPKEY，请查看update.json文件是否正确生成");let r;logger("checking update");try{r=await fetch(getCheckUrl(e),{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify({packageVersion:packageVersion,hash:currentVersion,buildTime:buildTime,cInfo:cInfo})})}catch(r){if(o)throw new Error("Could not connect to pushy server");return await tryBackupEndpoints(e),checkUpdate(e,!0)}const t=await r.json();if(checkOperation(t.op),200!==r.status)throw new Error(t.message);return t}function checkOperation(e){Array.isArray(e)&&e.forEach((e=>{"block"===e.type&&(blockUpdate={reason:e.reason,until:Math.round((Date.now()+e.duration)/1e3)},Pushy.setBlockUpdate(blockUpdate))}))}let downloadedHash,readyHash,downloadingThrottling=!1;export async function downloadUpdate(e,o){if(assertRelease(),!e.update)return;if(downloadedHash===e.hash)return void logger(`duplicated downloaded hash ${downloadedHash}, ignored`);if(downloadingThrottling)return void logger("repeated downloading, ignored");let r;if(downloadingThrottling=!0,setTimeout((()=>{downloadingThrottling=!1}),3e3),o&&o.onDownloadProgress){const t=o.onDownloadProgress;r=eventEmitter.addListener("RCTPushyDownloadProgress",(o=>{o.hash===e.hash&&t(o)}))}if(e.diffUrl){logger("downloading diff");try{await Pushy.downloadPatchFromPpk({updateUrl:e.diffUrl,hash:e.hash,originHash:currentVersion})}catch(o){logger(o.message),logger("diff error, try pdiff"),await Pushy.downloadPatchFromPackage({updateUrl:e.pdiffUrl,hash:e.hash})}}else e.pdiffUrl&&(logger("downloading pdiff"),await Pushy.downloadPatchFromPackage({updateUrl:e.pdiffUrl,hash:e.hash}));return r&&r.remove(),downloadedHash=e.hash,e.hash}export function switchVersion(e){assertRelease(),logger("switchVersion"),Pushy.reloadUpdate({hash:e})}export function switchVersionLater(e){assertRelease(),readyHash!==e?(readyHash=e,logger("switchVersionLater"),Pushy.setNeedUpdate({hash:e})):logger(`duplicated ready hash ${readyHash}, ignored`)}let marked=!1;export function markSuccess(){assertRelease(),marked?logger("repeated markSuccess, ignored"):(marked=!0,logger("markSuccess"),Pushy.markSuccess())}export async function downloadAndInstallApk({url:e,onDownloadProgress:o}){if(logger("downloadAndInstallApk"),"android"===Platform.OS&&Platform.Version<=23)try{if(await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE)!==PermissionsAndroid.RESULTS.GRANTED)return}catch(e){console.warn(e)}let r,t=Date.now().toString();o&&(r=eventEmitter.addListener("RCTPushyDownloadProgress",(e=>{e.hash===t&&o(e)}))),await Pushy.downloadAndInstallApk({url:e,target:"update.apk",hash:t}),r&&r.remove()}
//# sourceMappingURL=/sm/a9acc4b4a2840d4b435d8ced653ae58424225c5498a3275b07091e5b3c850886.map