refactor(expo-updates): 优化hash计算和并行处理逻辑,提升性能和调试能力

This commit is contained in:
richarjiang
2025-12-06 10:05:12 +08:00
parent 51d0dabc9a
commit 8fa741cc1b

View File

@@ -177,11 +177,12 @@ export class ExpoUpdatesService {
responseType: 'arraybuffer', responseType: 'arraybuffer',
timeout: 30000, timeout: 30000,
}); });
const hash = crypto.createHash('sha256').update(response.data).digest('base64url'); const hash = crypto.createHash('sha256').update(Buffer.from(response.data)).digest('base64url');
// 缓存 hash // 缓存 hash
this.hashCache.set(cacheKey, { hash, timestamp: Date.now() }); this.hashCache.set(cacheKey, { hash, timestamp: Date.now() });
logger.debug(`Calculated hash for ${url}: ${hash}`);
return hash; return hash;
} catch (error) { } catch (error) {
logger.error(`Failed to calculate hash for ${url}: ${error.message}`); logger.error(`Failed to calculate hash for ${url}: ${error.message}`);
@@ -202,10 +203,16 @@ export class ExpoUpdatesService {
} }
const assetList = Array.from(uniqueAssets.values()); const assetList = Array.from(uniqueAssets.values());
logger.info(`Building ${assetList.length} unique assets`);
// 并行计算所有 asset 的 hash // 分批并行计算每批10个避免并发过多
const results = await Promise.all( const batchSize = 10;
assetList.map(async (asset) => { const results: AssetMetadata[] = [];
for (let i = 0; i < assetList.length; i += batchSize) {
const batch = assetList.slice(i, i + batchSize);
const batchResults = await Promise.all(
batch.map(async (asset) => {
const url = baseUrl + asset.path; const url = baseUrl + asset.path;
const key = asset.path.split('/').pop() || ''; // 使用文件名作为 key const key = asset.path.split('/').pop() || ''; // 使用文件名作为 key
const hash = await this.calculateFileHash(url); const hash = await this.calculateFileHash(url);
@@ -219,6 +226,8 @@ export class ExpoUpdatesService {
}; };
}) })
); );
results.push(...batchResults);
}
return results; return results;
} }