cmvr-wms/mobile-app/src/core/package-update.ts
lixiaolong 6b43b0104f feat(app): 优化移动端应用配置和功能实现
- 添加生产环境服务器地址配置支持内外网切换
- 实现采购列表只看我创建的功能开关
- 更新构建脚本中的默认生产环境API地址
- 优化采购单表格单元格编辑体验改用自动补全组件
- 调整应用更新检查逻辑增强生产环境兼容性
- 修复开发服务器切换时的一些验证问题
- 增强应用启动时的服务地址配置灵活性
2026-10-30 15:02:54 +08:00

43 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {sha256} from '@noble/hashes/sha2.js';
import {bytesToHex} from '@noble/hashes/utils.js';
export type PackageRelease={versionCode:number;versionName:string;sha256:string;size:number;notes:string;environment:string;downloadPath:string};
export function newerPackage(value:any,current:number,environment:string):PackageRelease|null{
if(!Number.isInteger(current)||current<1||!value||value.environment!==environment||!Number.isInteger(value.versionCode)||value.versionCode<=current)return null;
if(!/^[a-f0-9]{64}$/.test(value.sha256)||!Number.isSafeInteger(value.size)||value.size<=0||value.size>250*1024**2||value.downloadPath!==`/system/mobile-release/${value.sha256}/content`||typeof value.versionName!=='string'||value.versionName.length>32||typeof value.notes!=='string'||value.notes.length>140)throw new Error('安装包版本信息无效,请稍后重试');
return value;
}
/** Hash incrementally; never install a partial file or merely trust its filename. */
export function packageVerifier(release:Pick<PackageRelease,'size'|'sha256'>){const hash=sha256.create();let size=0;return {write(bytes:Uint8Array){size+=bytes.length;if(size>release.size)throw new Error('安装包大小不匹配');hash.update(bytes);},finish(){if(size!==release.size||bytesToHex(hash.digest())!==release.sha256)throw new Error('安装包校验失败,请重新下载');}};}
type ProcessingOptions={blockSize?:number;yieldAfterMs?:number;progressEveryMs?:number;now?:()=>number;pause?:()=>Promise<void>};
/** Coalesce network fragments in bounded memory. The writer consumes each view synchronously.
* Yield for actual processing work, not for every network fragment; throttle rendering separately.
*/
export function createPackageDownloadProcessor(write:(chunk:Uint8Array)=>void,onProgress:(bytes:number)=>void,signal:AbortSignal,options:ProcessingOptions={}){
const {blockSize=256*1024,yieldAfterMs=16,progressEveryMs=100,now=()=>Date.now(),pause=()=>new Promise<void>(resolve=>setTimeout(resolve,0))}=options;
if(!Number.isInteger(blockSize)||blockSize<1||blockSize>1024*1024||yieldAfterMs<=0||progressEveryMs<0)throw new Error('安装包处理参数无效');
const buffer=new Uint8Array(blockSize);let used=0,processed=0,workMs=0,lastProgress=-Infinity,finished=false;
const check=()=>{if(signal.aborted)throw new Error('下载已取消');};
async function flush(final=false){
check();
if(used){const start=now();write(buffer.subarray(0,used));processed+=used;used=0;workMs+=Math.max(0,now()-start);}
const time=now();
if(processed&&(final||time-lastProgress>=progressEveryMs)){const start=now();onProgress(processed);lastProgress=time;workMs+=Math.max(0,now()-start);}
check();
if(workMs>=yieldAfterMs){workMs=0;await pause();check();}
}
return {
async push(bytes:Uint8Array){
check();if(finished)throw new Error('安装包处理已完成');
for(let offset=0;offset<bytes.length;){
check();const count=Math.min(blockSize-used,bytes.length-offset),start=now();buffer.set(bytes.subarray(offset,offset+count),used);used+=count;offset+=count;workMs+=Math.max(0,now()-start);
if(used===blockSize)await flush();
}
},
async finish(){check();if(!finished){await flush(true);finished=true;}return processed;}
};
}
/** No fallback to the Android installer when the native trust check is absent. */
export async function verifyInstalledPackage(bridge:{verify:(uri:string,version:number)=>Promise<boolean>}|undefined,uri:string,version:number){
if(!bridge||!/^file:\/\//.test(uri)||!Number.isInteger(version)||version<1||await bridge.verify(uri,version)!==true)throw new Error('无法验证安装包的包名和签名,请联系管理员。');
}