Unverified Commit d77ecc75 authored by Doron Behar's avatar Doron Behar Committed by GitHub
Browse files

fetchYarnDeps: fix by deduplicating requests (#336784)

parents fc2ffc6f 207072fc
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -141,10 +141,20 @@ const performParallel = tasks => {
	return Promise.all(workers)
}

// This could be implemented using [`Map.groupBy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy),
// but that method is only supported starting with Node 21
const uniqueBy = (arr, callback) => {
	const map = new Map()
	for (const elem of arr) {
		map.set(callback(elem), elem)
	}
	return [...map.values()]
}

const prefetchYarnDeps = async (lockContents, verbose) => {
	const lockData = lockfile.parse(lockContents)
	await performParallel(
		Object.entries(lockData.object)
		uniqueBy(Object.entries(lockData.object), ([_, value]) => value.resolved)
		.map(([key, value]) => () => downloadPkg({ key, ...value }, verbose))
	)
	await fs.promises.writeFile('yarn.lock', lockContents)