refactor(expo-updates): 优化hash计算和并行处理逻辑,提升性能和调试能力
This commit is contained in:
@@ -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
|
for (let i = 0; i < assetList.length; i += batchSize) {
|
||||||
const hash = await this.calculateFileHash(url);
|
const batch = assetList.slice(i, i + batchSize);
|
||||||
|
const batchResults = await Promise.all(
|
||||||
return {
|
batch.map(async (asset) => {
|
||||||
hash,
|
const url = baseUrl + asset.path;
|
||||||
key,
|
const key = asset.path.split('/').pop() || ''; // 使用文件名作为 key
|
||||||
contentType: this.getContentType(asset.ext),
|
const hash = await this.calculateFileHash(url);
|
||||||
fileExtension: `.${asset.ext}`,
|
|
||||||
url,
|
return {
|
||||||
};
|
hash,
|
||||||
})
|
key,
|
||||||
);
|
contentType: this.getContentType(asset.ext),
|
||||||
|
fileExtension: `.${asset.ext}`,
|
||||||
|
url,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
results.push(...batchResults);
|
||||||
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user