From 207072fca31e08c45f21cf2a7c8a097521589283 Mon Sep 17 00:00:00 2001 From: Fea Date: Thu, 22 Aug 2024 20:01:28 +0200 Subject: [PATCH] fetchYarnDeps: fix by deduplicating requests --- pkgs/build-support/node/fetch-yarn-deps/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkgs/build-support/node/fetch-yarn-deps/index.js b/pkgs/build-support/node/fetch-yarn-deps/index.js index 400b64d2e920..6bcc70837138 100755 --- a/pkgs/build-support/node/fetch-yarn-deps/index.js +++ b/pkgs/build-support/node/fetch-yarn-deps/index.js @@ -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)