refactor(expo-updates): 优化更新ID生成逻辑,基于bundle hash确保ID唯一性

This commit is contained in:
richarjiang
2025-12-05 20:11:29 +08:00
parent 14d791f552
commit 190bc5bce9

View File

@@ -73,8 +73,12 @@ export class ExpoUpdatesService {
}
}
// ID 基于 bundle hash 生成,确保内容不变时 ID 固定
const updateId = this.configService.get<string>('EXPO_UPDATE_ID')
|| this.hashToUUID(bundleHash);
return {
id: this.configService.get<string>('EXPO_UPDATE_ID') || this.generateId(),
id: updateId,
createdAt: this.configService.get<string>('EXPO_CREATED_AT') || new Date().toISOString(),
runtimeVersion: configRuntimeVersion || runtimeVersion,
launchAsset: {
@@ -93,9 +97,12 @@ export class ExpoUpdatesService {
return { type: 'noUpdateAvailable' };
}
private generateId(): string {
const timestamp = Date.now().toString(36);
const random = Math.random().toString(36).substring(2, 10);
return `${timestamp}-${random}`;
/**
* 将 hash 转换为 UUID 格式
*/
private hashToUUID(hash: string): string {
// 使用 hash 的前32个字符生成 UUID 格式
const hex = Buffer.from(hash, 'base64url').toString('hex').padEnd(32, '0').slice(0, 32);
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
}
}