attestid_js_sdk 0.1.12 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,11 @@ interface RetryOptions {
4
4
  attempts?: number;
5
5
  /** Base backoff in ms (doubled each retry). Default 500. */
6
6
  baseDelayMs?: number;
7
+ /**
8
+ * How long to keep waiting while the server says the same request is still
9
+ * in progress (409 idempotency_request_in_progress). Default 120 000 ms.
10
+ */
11
+ inProgressWaitMs?: number;
7
12
  }
8
13
 
9
14
  type IdType = 'national_id' | 'passport' | 'drivers_licence';
@@ -4,6 +4,11 @@ interface RetryOptions {
4
4
  attempts?: number;
5
5
  /** Base backoff in ms (doubled each retry). Default 500. */
6
6
  baseDelayMs?: number;
7
+ /**
8
+ * How long to keep waiting while the server says the same request is still
9
+ * in progress (409 idempotency_request_in_progress). Default 120 000 ms.
10
+ */
11
+ inProgressWaitMs?: number;
7
12
  }
8
13
 
9
14
  type IdType = 'national_id' | 'passport' | 'drivers_licence';
@@ -99,6 +99,8 @@ function randomId() {
99
99
  // src/core/http.ts
100
100
  var RETRY_FRESH_STATUSES = /* @__PURE__ */ new Set([429, 500, 502, 503, 504]);
101
101
  var MAX_RETRY_AFTER_MS = 1e4;
102
+ var DEFAULT_IN_PROGRESS_WAIT_MS = 12e4;
103
+ var IN_PROGRESS_ERROR = "idempotency_request_in_progress";
102
104
  var defaultSleep = (ms) => new Promise((r) => setTimeout(r, ms));
103
105
  var sleep = defaultSleep;
104
106
  function retryAfterMs(res, fallback) {
@@ -106,9 +108,19 @@ function retryAfterMs(res, fallback) {
106
108
  const secs = raw ? Number(raw) : NaN;
107
109
  return Number.isFinite(secs) && secs >= 0 ? Math.min(secs * 1e3, MAX_RETRY_AFTER_MS) : fallback;
108
110
  }
111
+ async function isInProgress(res) {
112
+ try {
113
+ const body = await (res.clone ? res.clone() : res).json();
114
+ return (body?.detail?.error ?? body?.error) === IN_PROGRESS_ERROR;
115
+ } catch {
116
+ return false;
117
+ }
118
+ }
109
119
  async function postIdempotent(fetchImpl, url, build, options = {}) {
110
120
  const attempts = Math.max(1, options.attempts ?? 3);
111
121
  const base = options.baseDelayMs ?? 500;
122
+ const inProgressBudget = options.inProgressWaitMs ?? DEFAULT_IN_PROGRESS_WAIT_MS;
123
+ let inProgressWaited = 0;
112
124
  let key = randomId();
113
125
  let req = await build();
114
126
  for (let attempt = 1; ; attempt++) {
@@ -125,11 +137,17 @@ async function postIdempotent(fetchImpl, url, build, options = {}) {
125
137
  await sleep(base * 2 ** (attempt - 1));
126
138
  continue;
127
139
  }
128
- if (last || res.ok) return res;
140
+ if (res.ok) return res;
129
141
  if (res.status === 409) {
130
- await sleep(retryAfterMs(res, base));
142
+ if (!await isInProgress(res)) return res;
143
+ const delay = retryAfterMs(res, base);
144
+ if (inProgressWaited + delay > inProgressBudget) return res;
145
+ inProgressWaited += delay;
146
+ await sleep(delay);
147
+ attempt--;
131
148
  continue;
132
149
  }
150
+ if (last) return res;
133
151
  if (RETRY_FRESH_STATUSES.has(res.status)) {
134
152
  await sleep(retryAfterMs(res, base * 2 ** (attempt - 1)));
135
153
  key = randomId();
@@ -648,12 +666,14 @@ var fetchOf = (t) => t?.fetch ?? ((input, init) => fetch(input, init));
648
666
  async function extractErrorMessage(res) {
649
667
  const text = await res.text();
650
668
  try {
651
- const detail = JSON.parse(text).detail;
669
+ const parsed = JSON.parse(text);
670
+ const detail = parsed.detail;
652
671
  if (typeof detail === "string") return detail;
653
672
  if (detail !== null && typeof detail === "object") {
654
673
  const message = detail.message;
655
674
  if (typeof message === "string") return message;
656
675
  }
676
+ if (detail === void 0 && typeof parsed.message === "string") return parsed.message;
657
677
  } catch {
658
678
  }
659
679
  return text;