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,23 +203,31 @@ 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[] = [];
const url = baseUrl + asset.path;
const key = asset.path.split('/').pop() || ''; // 使用文件名作为 key
const hash = await this.calculateFileHash(url);
return { for (let i = 0; i < assetList.length; i += batchSize) {
hash, const batch = assetList.slice(i, i + batchSize);
key, const batchResults = await Promise.all(
contentType: this.getContentType(asset.ext), batch.map(async (asset) => {
fileExtension: `.${asset.ext}`, const url = baseUrl + asset.path;
url, 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; return results;
} }