{"version":3,"sources":["../../../src/internals/helpers/retry.ts"],"names":["pRetry","fn","options","handler","attempt","remaining","factor","ms","Math","round","pow","setTimeout","undefined","signal","e","meta","FrameworkError","isAbortError","cause","onFailedAttempt","shouldRetry","signalRace","retries"],"mappings":";;;;;;;;AAiCA,eAAsBA,MAAAA,CACpBC,IACAC,OAAiB,EAAA;AAEjB,EAAMC,MAAAA,OAAAA,mBAAiBC,MAAAA,CAAAA,OAAAA,OAAAA,EAAiBC,SAAAA,KAAAA;AACtC,IAAI,IAAA;AACF,MAAMC,MAAAA,MAAAA,GAASJ,SAASI,MAAU,IAAA,CAAA;AAClC,MAAA,IAAIF,UAAU,CAAG,EAAA;AACf,QAAMG,MAAAA,EAAAA,GAAKC,KAAKC,KAAMD,CAAAA,IAAAA,CAAKE,IAAIJ,MAAQF,EAAAA,OAAAA,GAAU,CAAA,CAAA,CAAM,GAAA,GAAA;AACvD,QAAMO,MAAAA,mBAAAA,CAAWJ,IAAIK,KAAW,CAAA,EAAA;AAC9BC,UAAAA,MAAAA,EAAQX,OAASW,EAAAA;SACnB,CAAA;AACF;AAEA,MAAO,OAAA,MAAMZ,GAAGG,OAAAA,CAAAA;AAClB,KAAA,CAAA,OAASU,CAAG,EAAA;AACV,MAAA,MAAMC,IAAa,GAAA;AACjBX,QAAAA,OAAAA;AACAC,QAAAA;AACF,OAAA;AAEA,MAAIW,IAAAA,yBAAAA,CAAeC,YAAaH,CAAAA,CAAAA,CAAI,EAAA;AAClC,QAAA,MAAMA,EAAEI,KAASJ,IAAAA,CAAAA;AACnB;AAEA,MAAMZ,MAAAA,OAAAA,EAASiB,eAAkBL,GAAAA,CAAAA,EAAGC,IAAAA,CAAAA;AACpC,MAAA,IAAIV,aAAa,CAAG,EAAA;AAClB,QAAMS,MAAAA,CAAAA;AACR;AAEA,MAAA,IAAK,MAAMZ,OAASkB,EAAAA,WAAAA,GAAcN,CAAGC,EAAAA,IAAAA,MAAW,KAAO,EAAA;AACrD,QAAMD,MAAAA,CAAAA;AACR;AACA,MAAA,OAAO,MAAMX,OAAAA,CAAQC,OAAU,GAAA,CAAA,EAAGC,YAAY,CAAA,CAAA;AAChD;GA9Bc,EAAA,SAAA,CAAA;AAiChB,EAAO,OAAA,MAAMgB,sBAAW,CAAA,MAAMlB,OAAQ,CAAA,CAAA,EAAGD,SAASoB,OAAW,IAAA,CAAA,CAAIpB,EAAAA,OAAAA,EAASW,MAAAA,CAAAA;AAC5E;AAtCsBb,MAAAA,CAAAA,MAAAA,EAAAA,QAAAA,CAAAA","file":"retry.cjs","sourcesContent":["/**\n * Copyright 2025 IBM Corp.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { setTimeout } from \"node:timers/promises\";\nimport { signalRace } from \"@/internals/helpers/promise.js\";\nimport { FrameworkError } from \"@/errors.js\";\n\ninterface Meta {\n  attempt: number;\n  remaining: number;\n}\n\ninterface Options {\n  signal?: AbortSignal;\n  factor?: number;\n  retries?: number;\n  shouldRetry?: (e: Error, meta: Meta) => boolean | Promise<boolean>;\n  onFailedAttempt?: (e: Error, meta: Meta) => void | Promise<void>;\n}\n\nexport async function pRetry<T>(\n  fn: (attempt: number) => Promise<T>,\n  options?: Options,\n): Promise<T> {\n  const handler = async (attempt: number, remaining: number) => {\n    try {\n      const factor = options?.factor ?? 2;\n      if (attempt > 1) {\n        const ms = Math.round(Math.pow(factor, attempt - 1)) * 1000;\n        await setTimeout(ms, undefined, {\n          signal: options?.signal,\n        });\n      }\n\n      return await fn(attempt);\n    } catch (e) {\n      const meta: Meta = {\n        attempt,\n        remaining,\n      };\n\n      if (FrameworkError.isAbortError(e)) {\n        throw e.cause || e;\n      }\n\n      await options?.onFailedAttempt?.(e, meta);\n      if (remaining <= 0) {\n        throw e;\n      }\n\n      if ((await options?.shouldRetry?.(e, meta)) === false) {\n        throw e;\n      }\n      return await handler(attempt + 1, remaining - 1);\n    }\n  };\n\n  return await signalRace(() => handler(1, options?.retries ?? 0), options?.signal);\n}\n"]}