From 6a7fc611155d82c7a9328a49b0f744a8fe6feee3 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 18 Sep 2024 15:51:03 -0700 Subject: [PATCH] Attempt to fix types-registry with retry / nAtATime (#1057) --- .changeset/fair-pigs-jog.md | 5 +++++ packages/publisher/src/publish-registry.ts | 21 ++++++++++++++++----- packages/retag/src/index.ts | 15 +-------------- packages/utils/src/async.ts | 14 ++++++++++++++ 4 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 .changeset/fair-pigs-jog.md diff --git a/.changeset/fair-pigs-jog.md b/.changeset/fair-pigs-jog.md new file mode 100644 index 0000000000..2a05938528 --- /dev/null +++ b/.changeset/fair-pigs-jog.md @@ -0,0 +1,5 @@ +--- +"@definitelytyped/utils": patch +--- + +Add retry function diff --git a/packages/publisher/src/publish-registry.ts b/packages/publisher/src/publish-registry.ts index 836bc4c6be..cec468d614 100644 --- a/packages/publisher/src/publish-registry.ts +++ b/packages/publisher/src/publish-registry.ts @@ -29,6 +29,8 @@ import { NpmPublishClient, cacheDir, isObject, + nAtATime, + retry, } from "@definitelytyped/utils"; import * as pacote from "pacote"; import * as semver from "semver"; @@ -233,11 +235,20 @@ interface Registry { async function generateRegistry(typings: readonly TypingsData[]): Promise { return { entries: Object.fromEntries( - await Promise.all( - typings.map(async (typing) => [ - typing.typesDirectoryName, - filterTags((await pacote.packument(typing.name, { cache: cacheDir }))["dist-tags"]), - ]), + await nAtATime( + 10, + typings, + async (typing) => { + const tags = await retry( + async () => (await pacote.packument(typing.name, { cache: cacheDir }))["dist-tags"], + /*count*/ 2, + /*delaySeconds*/ 5, + ); + return [ + typing.typesDirectoryName, + filterTags(tags), + ]; + }, ), ), }; diff --git a/packages/retag/src/index.ts b/packages/retag/src/index.ts index 74e1a25770..a4fb6871da 100644 --- a/packages/retag/src/index.ts +++ b/packages/retag/src/index.ts @@ -14,7 +14,7 @@ import { LoggerWithErrors, cacheDir, nAtATime, - sleep, + retry, } from "@definitelytyped/utils"; import { AnyPackage, TypingsData, AllPackages, getDefinitelyTyped } from "@definitelytyped/definitions-parser"; import * as pacote from "pacote"; @@ -83,19 +83,6 @@ async function tag(dry: boolean, definitelyTypedPath: string, name?: string) { // Don't tag notNeeded packages } -async function retry(fn: () => Promise, count: number, delaySeconds: number): Promise { - let lastError: any; - for (let i = 0; i < count; i++) { - try { - return await fn(); - } catch (e) { - await sleep(delaySeconds); - lastError = e; - } - } - throw lastError; -} - export async function updateTypeScriptVersionTags( pkg: AnyPackage, version: string, diff --git a/packages/utils/src/async.ts b/packages/utils/src/async.ts index b28c9ad478..d3ed357d92 100644 --- a/packages/utils/src/async.ts +++ b/packages/utils/src/async.ts @@ -1,5 +1,6 @@ import { ProgressBar } from "./progress"; import { initArray } from "./collections"; +import { sleep } from "./miscellany"; /** Progress options needed for `nAtATime`. Other options will be inferred. */ interface ProgressOptions { @@ -78,3 +79,16 @@ export function logUncaughtErrors(promise: Promise | (() => Promise)): process.exit(1); }); } + +export async function retry(fn: () => Awaitable, count: number, delaySeconds: number): Promise { + let lastError: any; + for (let i = 0; i < count; i++) { + try { + return await fn(); + } catch (e) { + await sleep(delaySeconds); + lastError = e; + } + } + throw lastError; +}