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',
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
this.hashCache.set(cacheKey, { hash, timestamp: Date.now() });
logger.debug(`Calculated hash for ${url}: ${hash}`);
return hash;
} catch (error) {
logger.error(`Failed to calculate hash for ${url}: ${error.message}`);
@@ -202,23 +203,31 @@ export class ExpoUpdatesService {
}
const assetList = Array.from(uniqueAssets.values());
logger.info(`Building ${assetList.length} unique assets`);
// 并行计算所有 asset 的 hash
const results = await Promise.all(
assetList.map(async (asset) => {
const url = baseUrl + asset.path;
const key = asset.path.split('/').pop() || ''; // 使用文件名作为 key
const hash = await this.calculateFileHash(url);
// 分批并行计算每批10个避免并发过多
const batchSize = 10;
const results: AssetMetadata[] = [];
return {
hash,
key,
contentType: this.getContentType(asset.ext),
fileExtension: `.${asset.ext}`,
url,
};
})
);
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 key = asset.path.split('/').pop() || ''; // 使用文件名作为 key
const hash = await this.calculateFileHash(url);
return {
hash,
key,
contentType: this.getContentType(asset.ext),
fileExtension: `.${asset.ext}`,
url,
};
})
);
results.push(...batchResults);
}
return results;
}