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.
- package/dist/core/index.cjs +23 -3
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +5 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.js +23 -3
- package/dist/core/index.js.map +1 -1
- package/package.json +1 -1
package/dist/core/index.cjs
CHANGED
|
@@ -168,6 +168,8 @@ function randomId() {
|
|
|
168
168
|
// src/core/http.ts
|
|
169
169
|
var RETRY_FRESH_STATUSES = /* @__PURE__ */ new Set([429, 500, 502, 503, 504]);
|
|
170
170
|
var MAX_RETRY_AFTER_MS = 1e4;
|
|
171
|
+
var DEFAULT_IN_PROGRESS_WAIT_MS = 12e4;
|
|
172
|
+
var IN_PROGRESS_ERROR = "idempotency_request_in_progress";
|
|
171
173
|
var defaultSleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
172
174
|
var sleep = defaultSleep;
|
|
173
175
|
function retryAfterMs(res, fallback) {
|
|
@@ -175,9 +177,19 @@ function retryAfterMs(res, fallback) {
|
|
|
175
177
|
const secs = raw ? Number(raw) : NaN;
|
|
176
178
|
return Number.isFinite(secs) && secs >= 0 ? Math.min(secs * 1e3, MAX_RETRY_AFTER_MS) : fallback;
|
|
177
179
|
}
|
|
180
|
+
async function isInProgress(res) {
|
|
181
|
+
try {
|
|
182
|
+
const body = await (res.clone ? res.clone() : res).json();
|
|
183
|
+
return (body?.detail?.error ?? body?.error) === IN_PROGRESS_ERROR;
|
|
184
|
+
} catch {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
178
188
|
async function postIdempotent(fetchImpl, url, build, options = {}) {
|
|
179
189
|
const attempts = Math.max(1, options.attempts ?? 3);
|
|
180
190
|
const base = options.baseDelayMs ?? 500;
|
|
191
|
+
const inProgressBudget = options.inProgressWaitMs ?? DEFAULT_IN_PROGRESS_WAIT_MS;
|
|
192
|
+
let inProgressWaited = 0;
|
|
181
193
|
let key = randomId();
|
|
182
194
|
let req = await build();
|
|
183
195
|
for (let attempt = 1; ; attempt++) {
|
|
@@ -194,11 +206,17 @@ async function postIdempotent(fetchImpl, url, build, options = {}) {
|
|
|
194
206
|
await sleep(base * 2 ** (attempt - 1));
|
|
195
207
|
continue;
|
|
196
208
|
}
|
|
197
|
-
if (
|
|
209
|
+
if (res.ok) return res;
|
|
198
210
|
if (res.status === 409) {
|
|
199
|
-
await
|
|
211
|
+
if (!await isInProgress(res)) return res;
|
|
212
|
+
const delay = retryAfterMs(res, base);
|
|
213
|
+
if (inProgressWaited + delay > inProgressBudget) return res;
|
|
214
|
+
inProgressWaited += delay;
|
|
215
|
+
await sleep(delay);
|
|
216
|
+
attempt--;
|
|
200
217
|
continue;
|
|
201
218
|
}
|
|
219
|
+
if (last) return res;
|
|
202
220
|
if (RETRY_FRESH_STATUSES.has(res.status)) {
|
|
203
221
|
await sleep(retryAfterMs(res, base * 2 ** (attempt - 1)));
|
|
204
222
|
key = randomId();
|
|
@@ -717,12 +735,14 @@ var fetchOf = (t) => t?.fetch ?? ((input, init) => fetch(input, init));
|
|
|
717
735
|
async function extractErrorMessage(res) {
|
|
718
736
|
const text = await res.text();
|
|
719
737
|
try {
|
|
720
|
-
const
|
|
738
|
+
const parsed = JSON.parse(text);
|
|
739
|
+
const detail = parsed.detail;
|
|
721
740
|
if (typeof detail === "string") return detail;
|
|
722
741
|
if (detail !== null && typeof detail === "object") {
|
|
723
742
|
const message = detail.message;
|
|
724
743
|
if (typeof message === "string") return message;
|
|
725
744
|
}
|
|
745
|
+
if (detail === void 0 && typeof parsed.message === "string") return parsed.message;
|
|
726
746
|
} catch {
|
|
727
747
|
}
|
|
728
748
|
return text;
|