@funnelsgrove/analytics 0.1.43 → 0.1.44

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.
@@ -173,6 +173,38 @@ export const createBootstrapIdempotencyKey = (secureCrypto = globalThis.crypto)
173
173
  const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
174
174
  return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
175
175
  };
176
+ const SDK_DEPENDENCY_UNAVAILABLE = 'SDK_DEPENDENCY_UNAVAILABLE';
177
+ const DEFAULT_BOOTSTRAP_RETRY_DELAY_MS = 1000;
178
+ const MAX_BOOTSTRAP_RETRY_DELAY_MS = 5000;
179
+ const retryAfterMilliseconds = (response) => {
180
+ var _a;
181
+ const retryAfter = (_a = response.headers.get('retry-after')) === null || _a === void 0 ? void 0 : _a.trim();
182
+ if (!retryAfter) {
183
+ return DEFAULT_BOOTSTRAP_RETRY_DELAY_MS;
184
+ }
185
+ if (/^\d+$/.test(retryAfter)) {
186
+ return Math.min(Number(retryAfter) * 1000, MAX_BOOTSTRAP_RETRY_DELAY_MS);
187
+ }
188
+ const retryAt = Date.parse(retryAfter);
189
+ return Number.isNaN(retryAt)
190
+ ? DEFAULT_BOOTSTRAP_RETRY_DELAY_MS
191
+ : Math.min(Math.max(0, retryAt - Date.now()), MAX_BOOTSTRAP_RETRY_DELAY_MS);
192
+ };
193
+ const isRetryableBootstrapDependencyFailure = async (response) => {
194
+ if (response.status !== 503) {
195
+ return false;
196
+ }
197
+ try {
198
+ const payload = asRecord(await response.clone().json());
199
+ return payload.code === SDK_DEPENDENCY_UNAVAILABLE && payload.retryable === true;
200
+ }
201
+ catch (_a) {
202
+ return false;
203
+ }
204
+ };
205
+ const delay = (milliseconds) => new Promise((resolve) => {
206
+ setTimeout(resolve, milliseconds);
207
+ });
176
208
  export class PublicAnalyticsSdkService {
177
209
  constructor(options = {}) {
178
210
  this.options = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options);
@@ -195,7 +227,7 @@ export class PublicAnalyticsSdkService {
195
227
  }
196
228
  try {
197
229
  const idempotencyKey = createBootstrapIdempotencyKey();
198
- const response = await fetch(buildMainApiUrl('/sdk/public/users/bootstrap'), {
230
+ const requestBootstrap = () => fetch(buildMainApiUrl('/sdk/public/users/bootstrap'), {
199
231
  method: 'POST',
200
232
  headers: buildSdkHeaders({
201
233
  'Content-Type': 'application/json',
@@ -211,6 +243,11 @@ export class PublicAnalyticsSdkService {
211
243
  metadata: Object.assign({ environment: resolveAnalyticsEnvironment() }, asRecord(input.metadata)),
212
244
  }),
213
245
  });
246
+ let response = await requestBootstrap();
247
+ if (await isRetryableBootstrapDependencyFailure(response)) {
248
+ await delay(retryAfterMilliseconds(response));
249
+ response = await requestBootstrap();
250
+ }
214
251
  if (!response.ok) {
215
252
  this.userId = userId;
216
253
  return userId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.43",
3
+ "version": "0.1.44",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/The-Solid-Grove/funnelsgrove.git",