@bytescale/sdk 3.59.0 → 3.61.0

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.
@@ -153,6 +153,177 @@ var FairMutex = /*#__PURE__*/function () {
153
153
  }]);
154
154
  }();
155
155
  ;// ./src/private/AuthSessionState.ts
156
+ function AuthSessionState_empty() {}
157
+ function AuthSessionState_awaitIgnored(value, direct) {
158
+ if (!direct) {
159
+ return value && value.then ? value.then(AuthSessionState_empty) : Promise.resolve();
160
+ }
161
+ }
162
+ function AuthSessionState_invoke(body, then) {
163
+ var result = body();
164
+ if (result && result.then) {
165
+ return result.then(then);
166
+ }
167
+ return then(result);
168
+ }
169
+ function _settle(pact, state, value) {
170
+ if (!pact.s) {
171
+ if (value instanceof _Pact) {
172
+ if (value.s) {
173
+ if (state & 1) {
174
+ state = value.s;
175
+ }
176
+ value = value.v;
177
+ } else {
178
+ value.o = _settle.bind(null, pact, state);
179
+ return;
180
+ }
181
+ }
182
+ if (value && value.then) {
183
+ value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2));
184
+ return;
185
+ }
186
+ pact.s = state;
187
+ pact.v = value;
188
+ var observer = pact.o;
189
+ if (observer) {
190
+ observer(pact);
191
+ }
192
+ }
193
+ }
194
+ var _Pact = /*#__PURE__*/function () {
195
+ function _Pact() {}
196
+ _Pact.prototype.then = function (onFulfilled, onRejected) {
197
+ var result = new _Pact();
198
+ var state = this.s;
199
+ if (state) {
200
+ var callback = state & 1 ? onFulfilled : onRejected;
201
+ if (callback) {
202
+ try {
203
+ _settle(result, 1, callback(this.v));
204
+ } catch (e) {
205
+ _settle(result, 2, e);
206
+ }
207
+ return result;
208
+ } else {
209
+ return this;
210
+ }
211
+ }
212
+ this.o = function (_this) {
213
+ try {
214
+ var value = _this.v;
215
+ if (_this.s & 1) {
216
+ _settle(result, 1, onFulfilled ? onFulfilled(value) : value);
217
+ } else if (onRejected) {
218
+ _settle(result, 1, onRejected(value));
219
+ } else {
220
+ _settle(result, 2, value);
221
+ }
222
+ } catch (e) {
223
+ _settle(result, 2, e);
224
+ }
225
+ };
226
+ return result;
227
+ };
228
+ return _Pact;
229
+ }();
230
+ function _isSettledPact(thenable) {
231
+ return thenable instanceof _Pact && thenable.s & 1;
232
+ }
233
+ function _for(test, update, body) {
234
+ var stage;
235
+ for (;;) {
236
+ var shouldContinue = test();
237
+ if (_isSettledPact(shouldContinue)) {
238
+ shouldContinue = shouldContinue.v;
239
+ }
240
+ if (!shouldContinue) {
241
+ return result;
242
+ }
243
+ if (shouldContinue.then) {
244
+ stage = 0;
245
+ break;
246
+ }
247
+ var result = body();
248
+ if (result && result.then) {
249
+ if (_isSettledPact(result)) {
250
+ result = result.s;
251
+ } else {
252
+ stage = 1;
253
+ break;
254
+ }
255
+ }
256
+ if (update) {
257
+ var updateValue = update();
258
+ if (updateValue && updateValue.then && !_isSettledPact(updateValue)) {
259
+ stage = 2;
260
+ break;
261
+ }
262
+ }
263
+ }
264
+ var pact = new _Pact();
265
+ var reject = _settle.bind(null, pact, 2);
266
+ (stage === 0 ? shouldContinue.then(_resumeAfterTest) : stage === 1 ? result.then(_resumeAfterBody) : updateValue.then(_resumeAfterUpdate)).then(void 0, reject);
267
+ return pact;
268
+ function _resumeAfterBody(value) {
269
+ result = value;
270
+ do {
271
+ if (update) {
272
+ updateValue = update();
273
+ if (updateValue && updateValue.then && !_isSettledPact(updateValue)) {
274
+ updateValue.then(_resumeAfterUpdate).then(void 0, reject);
275
+ return;
276
+ }
277
+ }
278
+ shouldContinue = test();
279
+ if (!shouldContinue || _isSettledPact(shouldContinue) && !shouldContinue.v) {
280
+ _settle(pact, 1, result);
281
+ return;
282
+ }
283
+ if (shouldContinue.then) {
284
+ shouldContinue.then(_resumeAfterTest).then(void 0, reject);
285
+ return;
286
+ }
287
+ result = body();
288
+ if (_isSettledPact(result)) {
289
+ result = result.v;
290
+ }
291
+ } while (!result || !result.then);
292
+ result.then(_resumeAfterBody).then(void 0, reject);
293
+ }
294
+ function _resumeAfterTest(shouldContinue) {
295
+ if (shouldContinue) {
296
+ result = body();
297
+ if (result && result.then) {
298
+ result.then(_resumeAfterBody).then(void 0, reject);
299
+ } else {
300
+ _resumeAfterBody(result);
301
+ }
302
+ } else {
303
+ _settle(pact, 1, result);
304
+ }
305
+ }
306
+ function _resumeAfterUpdate() {
307
+ if (shouldContinue = test()) {
308
+ if (shouldContinue.then) {
309
+ shouldContinue.then(_resumeAfterTest).then(void 0, reject);
310
+ } else {
311
+ _resumeAfterTest(shouldContinue);
312
+ }
313
+ } else {
314
+ _settle(pact, 1, result);
315
+ }
316
+ }
317
+ }
318
+ function AuthSessionState_await(value, then, direct) {
319
+ if (direct) {
320
+ return then ? then(value) : value;
321
+ }
322
+ if (!value || !value.then) {
323
+ value = Promise.resolve(value);
324
+ }
325
+ return then ? value.then(then) : value;
326
+ }
156
327
  function AuthSessionState_typeof(o) { "@babel/helpers - typeof"; return AuthSessionState_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, AuthSessionState_typeof(o); }
157
328
  function AuthSessionState_classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
158
329
  function AuthSessionState_defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, AuthSessionState_toPropertyKey(o.key), o); } }
@@ -259,6 +430,54 @@ var AuthSessionState = /*#__PURE__*/function () {
259
430
  jwt: undefined
260
431
  };
261
432
  }
433
+ /** Waits for request-time auth to be current before resolving it. */
434
+ }, {
435
+ key: "resolveAuthConfigAsync",
436
+ value: function resolveAuthConfigAsync(authConfigId, requireReadyDefault) {
437
+ try {
438
+ var _exit = false;
439
+ var _a, _b;
440
+ if (authConfigId === false) {
441
+ return AuthSessionState_await(undefined);
442
+ }
443
+ return AuthSessionState_await(_for(function () {
444
+ return !_exit;
445
+ }, void 0, function () {
446
+ var session = AuthSessionState.getSession();
447
+ if (session === undefined || !session.isActive || !Array.isArray(session.authConfigs)) {
448
+ var _AuthSessionState$res = AuthSessionState.resolveAuthConfig(authConfigId, requireReadyDefault);
449
+ _exit = true;
450
+ return _AuthSessionState$res;
451
+ }
452
+ var state = session.authConfigs.find(function (config) {
453
+ return config.config.authConfigId === authConfigId;
454
+ });
455
+ if (state === undefined) {
456
+ var _AuthSessionState$res2 = AuthSessionState.resolveAuthConfig(authConfigId, requireReadyDefault);
457
+ _exit = true;
458
+ return _AuthSessionState$res2;
459
+ }
460
+ var authenticationPromise = (_a = state.authenticationPromise) !== null && _a !== void 0 ? _a : state.refreshPromise;
461
+ return AuthSessionState_invoke(function () {
462
+ if (authenticationPromise !== undefined) {
463
+ return AuthSessionState_awaitIgnored(authenticationPromise);
464
+ }
465
+ }, function () {
466
+ if (AuthSessionState.getSession() !== session || !session.isActive) {
467
+ return;
468
+ }
469
+ var currentAuthenticationPromise = (_b = state.authenticationPromise) !== null && _b !== void 0 ? _b : state.refreshPromise;
470
+ if (currentAuthenticationPromise === undefined || currentAuthenticationPromise === authenticationPromise) {
471
+ var _AuthSessionState$res3 = AuthSessionState.resolveAuthConfig(authConfigId, true);
472
+ _exit = true;
473
+ return _AuthSessionState$res3;
474
+ }
475
+ });
476
+ }));
477
+ } catch (e) {
478
+ return Promise.reject(e);
479
+ }
480
+ }
262
481
  }]);
263
482
  }();
264
483
  AuthSessionState.stateKey = "BytescaleSessionState";
@@ -404,12 +623,43 @@ var BytescaleApiClientConfigUtils = /*#__PURE__*/function () {
404
623
  value: function getAccountId(config) {
405
624
  return BytescaleApiClientConfigUtils.resolveAuthentication(config).accountId;
406
625
  }
626
+ }, {
627
+ key: "getAccountIdAsync",
628
+ value: function getAccountIdAsync(config) {
629
+ try {
630
+ return runtime_await(BytescaleApiClientConfigUtils.resolveAuthenticationAsync(config), function (_BytescaleApiClientCo) {
631
+ return _BytescaleApiClientCo.accountId;
632
+ });
633
+ } catch (e) {
634
+ return Promise.reject(e);
635
+ }
636
+ }
407
637
  }, {
408
638
  key: "resolveAuthentication",
409
639
  value: function resolveAuthentication(config) {
410
- var _a, _b;
640
+ var _a;
411
641
  var apiKey = (_a = config.apiKey) !== null && _a !== void 0 ? _a : undefined;
412
642
  var authConfig = AuthSessionState.resolveAuthConfig(config.authConfigId, apiKey === undefined);
643
+ return BytescaleApiClientConfigUtils.resolveAuthenticationWithAuthConfig(config, authConfig);
644
+ }
645
+ }, {
646
+ key: "resolveAuthenticationAsync",
647
+ value: function resolveAuthenticationAsync(config) {
648
+ try {
649
+ var _a;
650
+ var apiKey = (_a = config.apiKey) !== null && _a !== void 0 ? _a : undefined;
651
+ return runtime_await(AuthSessionState.resolveAuthConfigAsync(config.authConfigId, apiKey === undefined), function (authConfig) {
652
+ return BytescaleApiClientConfigUtils.resolveAuthenticationWithAuthConfig(config, authConfig);
653
+ });
654
+ } catch (e) {
655
+ return Promise.reject(e);
656
+ }
657
+ }
658
+ }, {
659
+ key: "resolveAuthenticationWithAuthConfig",
660
+ value: function resolveAuthenticationWithAuthConfig(config, authConfig) {
661
+ var _a, _b;
662
+ var apiKey = (_a = config.apiKey) !== null && _a !== void 0 ? _a : undefined;
413
663
  var apiKeyAccountId = apiKey === undefined ? undefined : BytescaleApiClientConfigUtils.getApiKeyAccountId(apiKey);
414
664
  if (apiKeyAccountId !== undefined && authConfig !== undefined && apiKeyAccountId !== authConfig.accountId) {
415
665
  throw new Error("The API key belongs to account '".concat(apiKeyAccountId, "', but AuthManager configuration '").concat((_b = config.authConfigId) !== null && _b !== void 0 ? _b : "default", "' belongs to account '").concat(authConfig.accountId, "'."));
@@ -496,7 +746,6 @@ var BaseAPI = /*#__PURE__*/function () {
496
746
  try {
497
747
  var _this = this;
498
748
  var _a;
499
- Object.assign(context.headers, BytescaleApiClientConfigUtils.resolveAuthentication(_this.config).headers);
500
749
  // Key: any possible value for 'baseUrlOverride'
501
750
  // Value: user-overridden value for that base URL from the config.
502
751
  var nonDefaultBasePaths = _defineProperty({}, BytescaleApiClientConfigUtils.defaultCdnUrl, BytescaleApiClientConfigUtils.getCdnUrl(_this.config));
@@ -537,44 +786,48 @@ var BaseAPI = /*#__PURE__*/function () {
537
786
  }
538
787
  var configHeaders = _this2.config.headers;
539
788
  return runtime_await(runtime_await(configHeaders === undefined ? {} : typeof configHeaders === "function" ? configHeaders() : configHeaders, function (resolvedConfigHeaders) {
540
- for (var _i = 0, _Object$keys = Object.keys(resolvedConfigHeaders); _i < _Object$keys.length; _i++) {
541
- var key = _Object$keys[_i];
542
- if (key.toLowerCase() === "authorization" || key.toLowerCase() === "authorization-token") {
543
- for (var _i2 = 0, _Object$keys2 = Object.keys(context.headers); _i2 < _Object$keys2.length; _i2++) {
544
- var generatedKey = _Object$keys2[_i2];
545
- if (generatedKey.toLowerCase() === key.toLowerCase()) {
546
- // Custom auth headers have documented precedence; remove the generated spelling to make that deterministic.
547
- // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
548
- delete context.headers[generatedKey];
789
+ var _context$headers = context.headers;
790
+ return runtime_await(BytescaleApiClientConfigUtils.resolveAuthenticationAsync(_this2.config), function (_BytescaleApiClientCo2) {
791
+ Object.assign(_context$headers, _BytescaleApiClientCo2.headers);
792
+ for (var _i = 0, _Object$keys = Object.keys(resolvedConfigHeaders); _i < _Object$keys.length; _i++) {
793
+ var key = _Object$keys[_i];
794
+ if (key.toLowerCase() === "authorization" || key.toLowerCase() === "authorization-token") {
795
+ for (var _i2 = 0, _Object$keys2 = Object.keys(context.headers); _i2 < _Object$keys2.length; _i2++) {
796
+ var generatedKey = _Object$keys2[_i2];
797
+ if (generatedKey.toLowerCase() === key.toLowerCase()) {
798
+ // Custom auth headers have documented precedence; remove the generated spelling to make that deterministic.
799
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
800
+ delete context.headers[generatedKey];
801
+ }
549
802
  }
550
803
  }
551
804
  }
552
- }
553
- var headers = Object.assign(Object.assign({}, context.headers), resolvedConfigHeaders);
554
- Object.keys(headers).forEach(function (key) {
555
- return headers[key] === undefined ? delete headers[key] : {};
556
- });
557
- var initOverrideFn = typeof initOverrides === "function" ? initOverrides : function () {
558
- return runtime_await(initOverrides);
559
- };
560
- var initParams = {
561
- method: context.method,
562
- headers: headers,
563
- body: context.body
564
- };
565
- var _Object$assign = Object.assign({}, initParams);
566
- return runtime_await(initOverrideFn({
567
- init: initParams,
568
- context: context
569
- }), function (_initOverrideFn) {
570
- var overriddenInit = Object.assign(_Object$assign, _initOverrideFn);
571
- var init = Object.assign(Object.assign({}, overriddenInit), {
572
- body: JSON.stringify(overriddenInit.body)
805
+ var headers = Object.assign(Object.assign({}, context.headers), resolvedConfigHeaders);
806
+ Object.keys(headers).forEach(function (key) {
807
+ return headers[key] === undefined ? delete headers[key] : {};
573
808
  });
574
- return {
575
- url: url,
576
- init: init
809
+ var initOverrideFn = typeof initOverrides === "function" ? initOverrides : function () {
810
+ return runtime_await(initOverrides);
577
811
  };
812
+ var initParams = {
813
+ method: context.method,
814
+ headers: headers,
815
+ body: context.body
816
+ };
817
+ var _Object$assign = Object.assign({}, initParams);
818
+ return runtime_await(initOverrideFn({
819
+ init: initParams,
820
+ context: context
821
+ }), function (_initOverrideFn) {
822
+ var overriddenInit = Object.assign(_Object$assign, _initOverrideFn);
823
+ var init = Object.assign(Object.assign({}, overriddenInit), {
824
+ body: JSON.stringify(overriddenInit.body)
825
+ });
826
+ return {
827
+ url: url,
828
+ init: init
829
+ };
830
+ });
578
831
  });
579
832
  }, configHeaders === undefined || !(typeof configHeaders === "function")));
580
833
  } catch (e) {
@@ -602,8 +855,8 @@ var BaseAPI = /*#__PURE__*/function () {
602
855
  // unless the following is set.
603
856
  // https://github.com/nodejs/node/issues/46221#issuecomment-1383246036
604
857
  duplex: "half"
605
- })), function (_BytescaleApiClientCo) {
606
- response = _BytescaleApiClientCo;
858
+ })), function (_BytescaleApiClientCo3) {
859
+ response = _BytescaleApiClientCo3;
607
860
  });
608
861
  }, function (e) {
609
862
  // Network-level errors, CORS errors, or HTTP-level errors from intermediary services (e.g. AWS or the user's own infrastructure/proxies).
@@ -2014,21 +2267,21 @@ function _invokeIgnored(body) {
2014
2267
  return result.then(UploadManagerBase_empty);
2015
2268
  }
2016
2269
  }
2017
- function _settle(pact, state, value) {
2270
+ function UploadManagerBase_settle(pact, state, value) {
2018
2271
  if (!pact.s) {
2019
- if (value instanceof _Pact) {
2272
+ if (value instanceof UploadManagerBase_Pact) {
2020
2273
  if (value.s) {
2021
2274
  if (state & 1) {
2022
2275
  state = value.s;
2023
2276
  }
2024
2277
  value = value.v;
2025
2278
  } else {
2026
- value.o = _settle.bind(null, pact, state);
2279
+ value.o = UploadManagerBase_settle.bind(null, pact, state);
2027
2280
  return;
2028
2281
  }
2029
2282
  }
2030
2283
  if (value && value.then) {
2031
- value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2));
2284
+ value.then(UploadManagerBase_settle.bind(null, pact, state), UploadManagerBase_settle.bind(null, pact, 2));
2032
2285
  return;
2033
2286
  }
2034
2287
  pact.s = state;
@@ -2039,7 +2292,7 @@ function _settle(pact, state, value) {
2039
2292
  }
2040
2293
  }
2041
2294
  }
2042
- var _Pact = /*#__PURE__*/function () {
2295
+ var UploadManagerBase_Pact = /*#__PURE__*/function () {
2043
2296
  function _Pact() {}
2044
2297
  _Pact.prototype.then = function (onFulfilled, onRejected) {
2045
2298
  var result = new _Pact();
@@ -2048,9 +2301,9 @@ var _Pact = /*#__PURE__*/function () {
2048
2301
  var callback = state & 1 ? onFulfilled : onRejected;
2049
2302
  if (callback) {
2050
2303
  try {
2051
- _settle(result, 1, callback(this.v));
2304
+ UploadManagerBase_settle(result, 1, callback(this.v));
2052
2305
  } catch (e) {
2053
- _settle(result, 2, e);
2306
+ UploadManagerBase_settle(result, 2, e);
2054
2307
  }
2055
2308
  return result;
2056
2309
  } else {
@@ -2061,28 +2314,28 @@ var _Pact = /*#__PURE__*/function () {
2061
2314
  try {
2062
2315
  var value = _this.v;
2063
2316
  if (_this.s & 1) {
2064
- _settle(result, 1, onFulfilled ? onFulfilled(value) : value);
2317
+ UploadManagerBase_settle(result, 1, onFulfilled ? onFulfilled(value) : value);
2065
2318
  } else if (onRejected) {
2066
- _settle(result, 1, onRejected(value));
2319
+ UploadManagerBase_settle(result, 1, onRejected(value));
2067
2320
  } else {
2068
- _settle(result, 2, value);
2321
+ UploadManagerBase_settle(result, 2, value);
2069
2322
  }
2070
2323
  } catch (e) {
2071
- _settle(result, 2, e);
2324
+ UploadManagerBase_settle(result, 2, e);
2072
2325
  }
2073
2326
  };
2074
2327
  return result;
2075
2328
  };
2076
2329
  return _Pact;
2077
2330
  }();
2078
- function _isSettledPact(thenable) {
2079
- return thenable instanceof _Pact && thenable.s & 1;
2331
+ function UploadManagerBase_isSettledPact(thenable) {
2332
+ return thenable instanceof UploadManagerBase_Pact && thenable.s & 1;
2080
2333
  }
2081
- function _for(test, update, body) {
2334
+ function UploadManagerBase_for(test, update, body) {
2082
2335
  var stage;
2083
2336
  for (;;) {
2084
2337
  var shouldContinue = test();
2085
- if (_isSettledPact(shouldContinue)) {
2338
+ if (UploadManagerBase_isSettledPact(shouldContinue)) {
2086
2339
  shouldContinue = shouldContinue.v;
2087
2340
  }
2088
2341
  if (!shouldContinue) {
@@ -2094,7 +2347,7 @@ function _for(test, update, body) {
2094
2347
  }
2095
2348
  var result = body();
2096
2349
  if (result && result.then) {
2097
- if (_isSettledPact(result)) {
2350
+ if (UploadManagerBase_isSettledPact(result)) {
2098
2351
  result = result.s;
2099
2352
  } else {
2100
2353
  stage = 1;
@@ -2103,14 +2356,14 @@ function _for(test, update, body) {
2103
2356
  }
2104
2357
  if (update) {
2105
2358
  var updateValue = update();
2106
- if (updateValue && updateValue.then && !_isSettledPact(updateValue)) {
2359
+ if (updateValue && updateValue.then && !UploadManagerBase_isSettledPact(updateValue)) {
2107
2360
  stage = 2;
2108
2361
  break;
2109
2362
  }
2110
2363
  }
2111
2364
  }
2112
- var pact = new _Pact();
2113
- var reject = _settle.bind(null, pact, 2);
2365
+ var pact = new UploadManagerBase_Pact();
2366
+ var reject = UploadManagerBase_settle.bind(null, pact, 2);
2114
2367
  (stage === 0 ? shouldContinue.then(_resumeAfterTest) : stage === 1 ? result.then(_resumeAfterBody) : updateValue.then(_resumeAfterUpdate)).then(void 0, reject);
2115
2368
  return pact;
2116
2369
  function _resumeAfterBody(value) {
@@ -2118,14 +2371,14 @@ function _for(test, update, body) {
2118
2371
  do {
2119
2372
  if (update) {
2120
2373
  updateValue = update();
2121
- if (updateValue && updateValue.then && !_isSettledPact(updateValue)) {
2374
+ if (updateValue && updateValue.then && !UploadManagerBase_isSettledPact(updateValue)) {
2122
2375
  updateValue.then(_resumeAfterUpdate).then(void 0, reject);
2123
2376
  return;
2124
2377
  }
2125
2378
  }
2126
2379
  shouldContinue = test();
2127
- if (!shouldContinue || _isSettledPact(shouldContinue) && !shouldContinue.v) {
2128
- _settle(pact, 1, result);
2380
+ if (!shouldContinue || UploadManagerBase_isSettledPact(shouldContinue) && !shouldContinue.v) {
2381
+ UploadManagerBase_settle(pact, 1, result);
2129
2382
  return;
2130
2383
  }
2131
2384
  if (shouldContinue.then) {
@@ -2133,7 +2386,7 @@ function _for(test, update, body) {
2133
2386
  return;
2134
2387
  }
2135
2388
  result = body();
2136
- if (_isSettledPact(result)) {
2389
+ if (UploadManagerBase_isSettledPact(result)) {
2137
2390
  result = result.v;
2138
2391
  }
2139
2392
  } while (!result || !result.then);
@@ -2148,7 +2401,7 @@ function _for(test, update, body) {
2148
2401
  _resumeAfterBody(result);
2149
2402
  }
2150
2403
  } else {
2151
- _settle(pact, 1, result);
2404
+ UploadManagerBase_settle(pact, 1, result);
2152
2405
  }
2153
2406
  }
2154
2407
  function _resumeAfterUpdate() {
@@ -2159,7 +2412,7 @@ function _for(test, update, body) {
2159
2412
  _resumeAfterTest(shouldContinue);
2160
2413
  }
2161
2414
  } else {
2162
- _settle(pact, 1, result);
2415
+ UploadManagerBase_settle(pact, 1, result);
2163
2416
  }
2164
2417
  }
2165
2418
  }
@@ -2199,40 +2452,41 @@ var UploadManagerBase = /*#__PURE__*/function () {
2199
2452
  try {
2200
2453
  var _this = this;
2201
2454
  _this.assertNotCancelled(request);
2202
- var accountId = BytescaleApiClientConfigUtils.getAccountId(_this.config);
2203
- var source = _this.processUploadSource(request.data);
2204
- var preUploadInfo = _this.getPreUploadInfo(request, source);
2205
- var bytesTotal = preUploadInfo.size;
2206
- var makeOnProgressForPart = _this.makeOnProgressForPartFactory(request, bytesTotal);
2207
- var init = _this.preUpload(source);
2208
- // Raise initial progress event SYNCHRONOUSLY.
2209
- if (request.onProgress !== undefined) {
2210
- request.onProgress(_this.makeProgressEvent(0, bytesTotal));
2211
- }
2212
- return UploadManagerBase_await(_this.beginUpload(request, preUploadInfo, accountId), function (uploadInfo) {
2213
- var partCount = uploadInfo.uploadParts.count;
2214
- var parts = UploadManagerBase_toConsumableArray(Array(partCount).keys());
2215
- var _this$makeCancellatio = _this.makeCancellationMethods(),
2216
- cancel = _this$makeCancellatio.cancel,
2217
- addCancellationHandler = _this$makeCancellatio.addCancellationHandler;
2218
- var intervalHandle = setInterval(_this.onIntervalTick(request, cancel), _this.intervalMs);
2219
- var uploadedParts;
2220
- return UploadManagerBase_continue(UploadManagerBase_finallyRethrows(function () {
2221
- return UploadManagerBase_await(_this.mapAsync(parts, preUploadInfo.maxConcurrentUploadParts, _async(function (part) {
2222
- return _this.uploadPart(request, source, part, uploadInfo, makeOnProgressForPart(), addCancellationHandler, accountId);
2223
- })), function (_this$mapAsync) {
2224
- uploadedParts = _this$mapAsync;
2225
- return UploadManagerBase_awaitIgnored(_this.postUpload(init));
2226
- });
2227
- }, function (_wasThrown, _result) {
2228
- clearInterval(intervalHandle);
2229
- return UploadManagerBase_rethrow(_wasThrown, _result);
2230
- }), function () {
2231
- var etag = uploadedParts.flatMap(function (x) {
2232
- return x.status === "Completed" ? [x.etag] : [];
2233
- })[0];
2234
- return Object.assign(Object.assign({}, uploadInfo.file), {
2235
- etag: etag // The 'etag' in the original 'uploadInfo.file' will be null, so we set it to the final etag value here.
2455
+ return UploadManagerBase_await(BytescaleApiClientConfigUtils.getAccountIdAsync(_this.config), function (accountId) {
2456
+ var source = _this.processUploadSource(request.data);
2457
+ var preUploadInfo = _this.getPreUploadInfo(request, source);
2458
+ var bytesTotal = preUploadInfo.size;
2459
+ var makeOnProgressForPart = _this.makeOnProgressForPartFactory(request, bytesTotal);
2460
+ var init = _this.preUpload(source);
2461
+ // Raise initial progress event SYNCHRONOUSLY.
2462
+ if (request.onProgress !== undefined) {
2463
+ request.onProgress(_this.makeProgressEvent(0, bytesTotal));
2464
+ }
2465
+ return UploadManagerBase_await(_this.beginUpload(request, preUploadInfo, accountId), function (uploadInfo) {
2466
+ var partCount = uploadInfo.uploadParts.count;
2467
+ var parts = UploadManagerBase_toConsumableArray(Array(partCount).keys());
2468
+ var _this$makeCancellatio = _this.makeCancellationMethods(),
2469
+ cancel = _this$makeCancellatio.cancel,
2470
+ addCancellationHandler = _this$makeCancellatio.addCancellationHandler;
2471
+ var intervalHandle = setInterval(_this.onIntervalTick(request, cancel), _this.intervalMs);
2472
+ var uploadedParts;
2473
+ return UploadManagerBase_continue(UploadManagerBase_finallyRethrows(function () {
2474
+ return UploadManagerBase_await(_this.mapAsync(parts, preUploadInfo.maxConcurrentUploadParts, _async(function (part) {
2475
+ return _this.uploadPart(request, source, part, uploadInfo, makeOnProgressForPart(), addCancellationHandler, accountId);
2476
+ })), function (_this$mapAsync) {
2477
+ uploadedParts = _this$mapAsync;
2478
+ return UploadManagerBase_awaitIgnored(_this.postUpload(init));
2479
+ });
2480
+ }, function (_wasThrown, _result) {
2481
+ clearInterval(intervalHandle);
2482
+ return UploadManagerBase_rethrow(_wasThrown, _result);
2483
+ }), function () {
2484
+ var etag = uploadedParts.flatMap(function (x) {
2485
+ return x.status === "Completed" ? [x.etag] : [];
2486
+ })[0];
2487
+ return Object.assign(Object.assign({}, uploadInfo.file), {
2488
+ etag: etag // The 'etag' in the original 'uploadInfo.file' will be null, so we set it to the final etag value here.
2489
+ });
2236
2490
  });
2237
2491
  });
2238
2492
  });
@@ -2439,7 +2693,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2439
2693
  var result = [];
2440
2694
  var workQueue = UploadManagerBase_toConsumableArray(items);
2441
2695
  return UploadManagerBase_await(Promise.all(UploadManagerBase_toConsumableArray(Array(concurrency).keys()).map(_async(function () {
2442
- return _continueIgnored(_for(function () {
2696
+ return _continueIgnored(UploadManagerBase_for(function () {
2443
2697
  return workQueue.length > 0;
2444
2698
  }, void 0, function () {
2445
2699
  var work = workQueue.shift(); // IMPORTANT: use 'shift' instead of 'pop' to ensure 'items' are processed in order when 'concurrency = 1'.
@@ -3177,15 +3431,27 @@ function Scheduler_toPrimitive(t, r) { if ("object" != Scheduler_typeof(t) || !t
3177
3431
  * Q: Why not use 'setTimeout'?
3178
3432
  * -------------------------
3179
3433
  * A: setTimeout can be paused (e.g., during hibernation), risking JWT expiration before it triggers.
3180
- * We therefore use a scheduler to check wall-clock time every second and execute the callback at the scheduled time.
3434
+ * We therefore check wall-clock time every second and on browser resume events. A backwards clock adjustment runs
3435
+ * scheduled callbacks immediately so authentication can be safely re-established against the server-provided TTL.
3181
3436
  * -------------------------
3182
3437
  */
3183
3438
  var Scheduler = /*#__PURE__*/function () {
3184
3439
  function Scheduler() {
3440
+ var _this = this;
3185
3441
  Scheduler_classCallCheck(this, Scheduler);
3442
+ this.clockRollbackToleranceMilliseconds = 1000;
3186
3443
  this.callbacks = {};
3187
3444
  this.nextId = 0;
3188
3445
  this.intervalId = undefined;
3446
+ this.checkAfterResume = function () {
3447
+ return _this.checkCallbacks();
3448
+ };
3449
+ this.checkAfterVisibilityChange = function () {
3450
+ var _a;
3451
+ if (((_a = _this.documentEventTarget) === null || _a === void 0 ? void 0 : _a.visibilityState) === "visible") {
3452
+ _this.checkCallbacks();
3453
+ }
3454
+ };
3189
3455
  }
3190
3456
  return Scheduler_createClass(Scheduler, [{
3191
3457
  key: "schedule",
@@ -3195,7 +3461,7 @@ var Scheduler = /*#__PURE__*/function () {
3195
3461
  epoch: epoch,
3196
3462
  callback: callback
3197
3463
  };
3198
- this.startInterval();
3464
+ this.startMonitoring();
3199
3465
  return handle;
3200
3466
  }
3201
3467
  }, {
@@ -3204,34 +3470,40 @@ var Scheduler = /*#__PURE__*/function () {
3204
3470
  // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
3205
3471
  delete this.callbacks[handle];
3206
3472
  if (Object.keys(this.callbacks).length === 0) {
3207
- this.stopInterval();
3473
+ this.stopMonitoring();
3208
3474
  }
3209
3475
  }
3210
3476
  }, {
3211
- key: "startInterval",
3212
- value: function startInterval() {
3213
- var _this = this;
3477
+ key: "startMonitoring",
3478
+ value: function startMonitoring() {
3479
+ var _this2 = this;
3214
3480
  if (this.intervalId === undefined) {
3481
+ this.lastObservedEpoch = Date.now();
3215
3482
  this.intervalId = setInterval(function () {
3216
- return _this.checkCallbacks();
3483
+ return _this2.checkCallbacks();
3217
3484
  }, 1000);
3485
+ this.startListeningForResume();
3218
3486
  }
3219
3487
  }
3220
3488
  }, {
3221
- key: "stopInterval",
3222
- value: function stopInterval() {
3489
+ key: "stopMonitoring",
3490
+ value: function stopMonitoring() {
3223
3491
  if (this.intervalId !== undefined) {
3224
3492
  clearInterval(this.intervalId);
3225
3493
  this.intervalId = undefined;
3494
+ this.lastObservedEpoch = undefined;
3495
+ this.stopListeningForResume();
3226
3496
  }
3227
3497
  }
3228
3498
  }, {
3229
3499
  key: "checkCallbacks",
3230
3500
  value: function checkCallbacks() {
3231
3501
  var now = Date.now();
3502
+ var clockMovedBackwards = this.lastObservedEpoch !== undefined && now < this.lastObservedEpoch - this.clockRollbackToleranceMilliseconds;
3503
+ this.lastObservedEpoch = now;
3232
3504
  for (var handleStr in this.callbacks) {
3233
3505
  var handle = parseInt(handleStr);
3234
- if (this.callbacks[handle].epoch <= now) {
3506
+ if (clockMovedBackwards || this.callbacks[handle].epoch <= now) {
3235
3507
  try {
3236
3508
  this.callbacks[handle].callback();
3237
3509
  } catch (e) {
@@ -3241,6 +3513,38 @@ var Scheduler = /*#__PURE__*/function () {
3241
3513
  }
3242
3514
  }
3243
3515
  }
3516
+ }, {
3517
+ key: "startListeningForResume",
3518
+ value: function startListeningForResume() {
3519
+ if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
3520
+ this.windowEventTarget = window;
3521
+ for (var _i = 0, _arr = ["focus", "online", "pageshow"]; _i < _arr.length; _i++) {
3522
+ var eventName = _arr[_i];
3523
+ this.windowEventTarget.addEventListener(eventName, this.checkAfterResume);
3524
+ }
3525
+ }
3526
+ if (typeof document !== "undefined" && typeof document.addEventListener === "function") {
3527
+ this.documentEventTarget = document;
3528
+ this.documentEventTarget.addEventListener("resume", this.checkAfterResume);
3529
+ this.documentEventTarget.addEventListener("visibilitychange", this.checkAfterVisibilityChange);
3530
+ }
3531
+ }
3532
+ }, {
3533
+ key: "stopListeningForResume",
3534
+ value: function stopListeningForResume() {
3535
+ if (this.windowEventTarget !== undefined) {
3536
+ for (var _i2 = 0, _arr2 = ["focus", "online", "pageshow"]; _i2 < _arr2.length; _i2++) {
3537
+ var eventName = _arr2[_i2];
3538
+ this.windowEventTarget.removeEventListener(eventName, this.checkAfterResume);
3539
+ }
3540
+ this.windowEventTarget = undefined;
3541
+ }
3542
+ if (this.documentEventTarget !== undefined) {
3543
+ this.documentEventTarget.removeEventListener("resume", this.checkAfterResume);
3544
+ this.documentEventTarget.removeEventListener("visibilitychange", this.checkAfterVisibilityChange);
3545
+ this.documentEventTarget = undefined;
3546
+ }
3547
+ }
3244
3548
  }]);
3245
3549
  }();
3246
3550
  ;// ./src/public/browser/AuthManagerBrowser.ts
@@ -3562,10 +3866,12 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3562
3866
  authConfigs: configs.map(function (config) {
3563
3867
  return {
3564
3868
  accessToken: undefined,
3869
+ authenticationPromise: Promise.resolve(),
3565
3870
  config: config,
3566
3871
  expiresAt: undefined,
3567
3872
  jwt: undefined,
3568
- refreshHandle: undefined
3873
+ refreshHandle: undefined,
3874
+ refreshPromise: undefined
3569
3875
  };
3570
3876
  }),
3571
3877
  authServiceWorker: requiresServiceWorker && params.serviceWorkerScript !== undefined && canUseServiceWorkers ? {
@@ -3583,7 +3889,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3583
3889
  })), function (session) {
3584
3890
  return AuthManagerBrowser_catch(function () {
3585
3891
  return AuthManagerBrowser_continueIgnored(AuthManagerBrowser_forOf((_a = session.authConfigs) !== null && _a !== void 0 ? _a : [], function (config) {
3586
- return AuthManagerBrowser_awaitIgnored(_this2.refreshAuthConfig(session, config, _this2.isV2Params(session.params)));
3892
+ return AuthManagerBrowser_awaitIgnored(_this2.refreshAuthConfig(session, config));
3587
3893
  }));
3588
3894
  }, function (e) {
3589
3895
  return AuthManagerBrowser_continue(AuthManagerBrowser_catch(function () {
@@ -3638,14 +3944,14 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3638
3944
  return AuthManagerBrowser_invoke(function () {
3639
3945
  if (session.authConfigs === undefined && typeof session.params.accountId === "string") {
3640
3946
  return AuthManagerBrowser_continueIgnored(AuthManagerBrowser_catch(function () {
3641
- return AuthManagerBrowser_awaitIgnored(_this3.deleteAccessToken(session.params.options, session.params.accountId));
3947
+ return AuthManagerBrowser_awaitIgnored(_this3.deleteAccessToken(session.params.options, _this3.getCdnUrl(session.params), session.params.accountId));
3642
3948
  }, function (e) {
3643
3949
  cleanupError = e;
3644
3950
  }));
3645
3951
  } else {
3646
3952
  return AuthManagerBrowser_continueIgnored(AuthManagerBrowser_forOf(cookieConfigs, function (state) {
3647
3953
  return AuthManagerBrowser_continueIgnored(AuthManagerBrowser_catch(function () {
3648
- return AuthManagerBrowser_awaitIgnored(_this3.deleteAccessToken(session.params.options, state.config.accountId));
3954
+ return AuthManagerBrowser_awaitIgnored(_this3.deleteAccessToken(session.params.options, _this3.getConfigCdnUrl(session.params, state.config), state.config.accountId));
3649
3955
  }, function (e) {
3650
3956
  cleanupError !== null && cleanupError !== void 0 ? cleanupError : cleanupError = e;
3651
3957
  }));
@@ -3674,16 +3980,48 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3674
3980
  }, {
3675
3981
  key: "refreshAuthConfig",
3676
3982
  value: function refreshAuthConfig(session, state) {
3677
- var rejectInvalidInitialToken = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
3678
3983
  try {
3984
+ var _exit = false;
3679
3985
  var _this4 = this;
3680
- return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(_this4.authSessionMutex.safe(AuthManagerBrowser_async(function () {
3986
+ return AuthManagerBrowser_await(AuthManagerBrowser_invoke(function () {
3987
+ if (state.refreshPromise !== undefined) {
3988
+ return AuthManagerBrowser_await(state.refreshPromise, function () {
3989
+ _exit = true;
3990
+ });
3991
+ }
3992
+ }, function (_result2) {
3993
+ if (_exit) return _result2;
3994
+ // Publish the pending operation before provider code can run, including provider code that calls back into the SDK.
3995
+ var refreshPromise = Promise.resolve().then(AuthManagerBrowser_async(function () {
3996
+ return _this4.performRefreshAuthConfig(session, state);
3997
+ }));
3998
+ state.authenticationPromise = refreshPromise;
3999
+ state.refreshPromise = refreshPromise;
4000
+ var clearRefreshPromise = function clearRefreshPromise() {
4001
+ if (state.refreshPromise === refreshPromise) {
4002
+ state.refreshPromise = undefined;
4003
+ }
4004
+ };
4005
+ refreshPromise.then(clearRefreshPromise, clearRefreshPromise);
4006
+ return AuthManagerBrowser_awaitIgnored(refreshPromise);
4007
+ }));
4008
+ } catch (e) {
4009
+ return Promise.reject(e);
4010
+ }
4011
+ }
4012
+ }, {
4013
+ key: "performRefreshAuthConfig",
4014
+ value: function performRefreshAuthConfig(session, state) {
4015
+ try {
4016
+ var _this5 = this;
4017
+ return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(_this5.authSessionMutex.safe(AuthManagerBrowser_async(function () {
3681
4018
  var _a;
3682
4019
  if (!session.isActive) {
3683
4020
  return;
3684
4021
  }
3685
4022
  if (state.refreshHandle !== undefined) {
3686
- _this4.scheduler.unschedule(state.refreshHandle);
4023
+ _this5.scheduler.unschedule(state.refreshHandle);
4024
+ state.refreshHandle = undefined;
3687
4025
  }
3688
4026
  var previous = {
3689
4027
  accessToken: state.accessToken,
@@ -3691,27 +4029,27 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3691
4029
  jwt: state.jwt,
3692
4030
  serviceWorkerConfigured: session.serviceWorkerConfigured
3693
4031
  };
3694
- var refreshAt = Date.now() + _this4.retryAuthAfterErrorSeconds * 1000;
4032
+ var refreshAt = Date.now() + _this5.retryAuthAfterErrorSeconds * 1000;
3695
4033
  return AuthManagerBrowser_finallyRethrows(function () {
3696
4034
  return AuthManagerBrowser_catch(function () {
3697
- return AuthManagerBrowser_await(_this4.getAuthorizationToken(session.params, state.config), function (jwt) {
3698
- var setCookie = _this4.shouldSetCookie(session, state.config);
3699
- return AuthManagerBrowser_await(_this4.setAccessToken(session.params.options, state.config.accountId, jwt, setCookie), function (token) {
4035
+ return AuthManagerBrowser_await(_this5.getAuthorizationToken(session.params, state.config), function (jwt) {
4036
+ var setCookie = _this5.shouldSetCookie(session, state.config);
4037
+ return AuthManagerBrowser_await(_this5.setAccessToken(session.params.options, _this5.getConfigCdnUrl(session.params, state.config), state.config.accountId, jwt, setCookie), function (token) {
3700
4038
  var expiresAt = Date.now() + token.ttlSeconds * 1000;
3701
4039
  state.accessToken = token.accessToken;
3702
4040
  state.expiresAt = expiresAt;
3703
4041
  state.jwt = jwt;
3704
4042
  return AuthManagerBrowser_invoke(function () {
3705
- if (session.authServiceWorker !== undefined && (_this4.isServiceWorkerEnabled(state.config) || session.serviceWorkerConfigured !== true)) {
3706
- return AuthManagerBrowser_await(_this4.sendCurrentServiceWorkerConfig(session), function () {
3707
- return AuthManagerBrowser_await(_this4.waitForServiceWorker(), function () {
4043
+ if (session.authServiceWorker !== undefined && (_this5.isServiceWorkerEnabled(state.config) || session.serviceWorkerConfigured !== true)) {
4044
+ return AuthManagerBrowser_await(_this5.sendCurrentServiceWorkerConfig(session), function () {
4045
+ return AuthManagerBrowser_await(_this5.waitForServiceWorker(), function () {
3708
4046
  session.serviceWorkerConfigured = true;
3709
4047
  });
3710
4048
  });
3711
4049
  }
3712
4050
  }, function () {
3713
- var desiredTtl = token.ttlSeconds - _this4.refreshBeforeExpirySeconds;
3714
- var actualTtl = Math.max(desiredTtl, _this4.minJwtTtlSeconds);
4051
+ var desiredTtl = token.ttlSeconds - _this5.refreshBeforeExpirySeconds;
4052
+ var actualTtl = Math.max(desiredTtl, _this5.minJwtTtlSeconds);
3715
4053
  if (desiredTtl !== actualTtl) {
3716
4054
  ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(actualTtl, " seconds before refreshing."));
3717
4055
  }
@@ -3725,18 +4063,18 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3725
4063
  state.jwt = previous.jwt;
3726
4064
  session.serviceWorkerConfigured = previous.serviceWorkerConfigured;
3727
4065
  ConsoleUtils.warn("Unable to refresh JWT access token for auth config '".concat((_a = state.config.authConfigId) !== null && _a !== void 0 ? _a : "default", "': ").concat(e));
3728
- if (rejectInvalidInitialToken && e instanceof InvalidAuthTokenError) {
4066
+ if (_this5.isV2Params(session.params) && previous.accessToken === undefined && e instanceof InvalidAuthTokenError) {
3729
4067
  throw e;
3730
4068
  }
3731
4069
  });
3732
- }, function (_wasThrown, _result2) {
3733
- state.refreshHandle = _this4.scheduler.schedule(refreshAt, function () {
3734
- _this4.refreshAuthConfig(session, state).then(function () {}, function (e) {
4070
+ }, function (_wasThrown, _result3) {
4071
+ state.refreshHandle = _this5.scheduler.schedule(refreshAt, function () {
4072
+ _this5.refreshAuthConfig(session, state).then(function () {}, function (e) {
3735
4073
  return ConsoleUtils.error("Unexpected error when refreshing JWT access token: ".concat(e));
3736
4074
  });
3737
4075
  });
3738
- _this4.updateSessionState(session);
3739
- return AuthManagerBrowser_rethrow(_wasThrown, _result2);
4076
+ _this5.updateSessionState(session);
4077
+ return AuthManagerBrowser_rethrow(_wasThrown, _result3);
3740
4078
  });
3741
4079
  }))));
3742
4080
  } catch (e) {
@@ -3747,11 +4085,11 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3747
4085
  key: "sendCurrentServiceWorkerConfig",
3748
4086
  value: function sendCurrentServiceWorkerConfig(session) {
3749
4087
  try {
3750
- var _this5 = this;
4088
+ var _this6 = this;
3751
4089
  var _a;
3752
4090
  var now = Date.now();
3753
4091
  var config = ((_a = session.authConfigs) !== null && _a !== void 0 ? _a : []).flatMap(function (state) {
3754
- if (!_this5.isServiceWorkerEnabled(state.config) || state.jwt === undefined || state.expiresAt === undefined || state.expiresAt <= now) {
4092
+ if (!_this6.isServiceWorkerEnabled(state.config) || state.jwt === undefined || state.expiresAt === undefined || state.expiresAt <= now) {
3755
4093
  return [];
3756
4094
  }
3757
4095
  return [{
@@ -3761,10 +4099,10 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3761
4099
  value: "Bearer ".concat(state.jwt)
3762
4100
  }],
3763
4101
  sourceUrlPrefixes: state.config.sourceUrlPrefixes,
3764
- urlPrefix: "".concat(_this5.getCdnUrl(session.params), "/").concat(state.config.accountId, "/")
4102
+ urlPrefix: "".concat(_this6.getConfigCdnUrl(session.params, state.config), "/").concat(state.config.accountId, "/")
3765
4103
  }];
3766
4104
  });
3767
- return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(_this5.sendServiceWorkerConfig(session, config, _this5.isV2Params(session.params) ? session.params.urlRewriteRules : undefined)));
4105
+ return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(_this6.sendServiceWorkerConfig(session, config, _this6.isV2Params(session.params) ? session.params.urlRewriteRules : undefined)));
3768
4106
  } catch (e) {
3769
4107
  return Promise.reject(e);
3770
4108
  }
@@ -3773,21 +4111,21 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3773
4111
  key: "sendServiceWorkerConfig",
3774
4112
  value: function sendServiceWorkerConfig(session, config, urlRewriteRules) {
3775
4113
  try {
3776
- var _this6 = this;
4114
+ var _this7 = this;
3777
4115
  if (session.authServiceWorker === undefined) {
3778
4116
  throw new Error("Service worker configuration cannot be applied because service workers are unavailable.");
3779
4117
  }
3780
- return AuthManagerBrowser_await(_this6.serviceWorkerUtils.sendMessage(Object.assign({
4118
+ return AuthManagerBrowser_await(_this7.serviceWorkerUtils.sendMessage(Object.assign({
3781
4119
  type: "SET_BYTESCALE_AUTH_CONFIG",
3782
4120
  config: config.map(function (entry) {
3783
4121
  return Object.assign(Object.assign({}, entry), {
3784
- urlPrefix: "".concat(entry.sourceUrlPrefixes === undefined ? "" : _this6.sourceScopedUrlPrefixMarker).concat(entry.urlPrefix)
4122
+ urlPrefix: "".concat(entry.sourceUrlPrefixes === undefined ? "" : _this7.sourceScopedUrlPrefixMarker).concat(entry.urlPrefix)
3785
4123
  });
3786
4124
  })
3787
4125
  }, urlRewriteRules === undefined ? {} : {
3788
4126
  urlRewriteRules: urlRewriteRules
3789
- }), session.authServiceWorker, _this6.serviceWorkerScriptFieldName), function (_this6$serviceWorkerU) {
3790
- session.authServiceWorker = _this6$serviceWorkerU;
4127
+ }), session.authServiceWorker, _this7.serviceWorkerScriptFieldName), function (_this7$serviceWorkerU) {
4128
+ session.authServiceWorker = _this7$serviceWorkerU;
3791
4129
  });
3792
4130
  } catch (e) {
3793
4131
  return Promise.reject(e);
@@ -3854,6 +4192,9 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3854
4192
  if (isV2 && !this.isValidAccountId(config.accountId)) {
3855
4193
  throw new Error("Invalid Bytescale account ID: '".concat(String(config.accountId), "'."));
3856
4194
  }
4195
+ if (config.cdnUrl !== undefined && typeof config.cdnUrl !== "string") {
4196
+ throw new Error("The 'cdnUrl' field must be a string when provided.");
4197
+ }
3857
4198
  if (config.enableCookieAuth !== undefined && typeof config.enableCookieAuth !== "boolean" || config.enableServiceWorkerAuth !== undefined && typeof config.enableServiceWorkerAuth !== "boolean") {
3858
4199
  throw new Error("Authentication enablement flags must be booleans when provided.");
3859
4200
  }
@@ -3874,7 +4215,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3874
4215
  cookieConfig = config;
3875
4216
  }
3876
4217
  if (this.isServiceWorkerEnabled(config)) {
3877
- var _prefix = "".concat(this.getCdnUrl(params), "/").concat(config.accountId, "/");
4218
+ var _prefix = "".concat(this.getConfigCdnUrl(params, config), "/").concat(config.accountId, "/");
3878
4219
  if (workerConfigsByPrefix.has(_prefix)) {
3879
4220
  throw new Error("Multiple service-worker auth configurations target the same URL prefix: '".concat(_prefix, "'."));
3880
4221
  }
@@ -3887,7 +4228,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3887
4228
  _iterator2.f();
3888
4229
  }
3889
4230
  if (cookieConfig !== undefined) {
3890
- var prefix = "".concat(this.getCdnUrl(params), "/").concat(cookieConfig.accountId, "/");
4231
+ var prefix = "".concat(this.getConfigCdnUrl(params, cookieConfig), "/").concat(cookieConfig.accountId, "/");
3891
4232
  var workerConfig = workerConfigsByPrefix.get(prefix);
3892
4233
  if (workerConfig !== undefined && workerConfig !== cookieConfig) {
3893
4234
  throw new Error("Cookie and service-worker authentication cannot target the same account from different configs.");
@@ -3915,7 +4256,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3915
4256
  }, {
3916
4257
  key: "updateSessionState",
3917
4258
  value: function updateSessionState(session) {
3918
- var _this7 = this;
4259
+ var _this8 = this;
3919
4260
  var _a;
3920
4261
  var configs = (_a = session.authConfigs) !== null && _a !== void 0 ? _a : [];
3921
4262
  var defaultConfig = configs.find(function (state) {
@@ -3925,7 +4266,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3925
4266
  session.accessToken = exposeLegacyDefault && defaultConfig !== undefined && this.isConfigUsable(defaultConfig) ? defaultConfig.accessToken : undefined;
3926
4267
  session.accessTokenRefreshHandle = exposeLegacyDefault ? defaultConfig === null || defaultConfig === void 0 ? void 0 : defaultConfig.refreshHandle : undefined;
3927
4268
  session.isReady = configs.length > 0 && configs.every(function (state) {
3928
- return _this7.isConfigUsable(state);
4269
+ return _this8.isConfigUsable(state);
3929
4270
  }) && (session.authServiceWorker === undefined || session.serviceWorkerConfigured === true);
3930
4271
  }
3931
4272
  }, {
@@ -3966,8 +4307,8 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3966
4307
  }
3967
4308
  }, {
3968
4309
  key: "getAccessTokenUrl",
3969
- value: function getAccessTokenUrl(options, accountId, setCookie) {
3970
- return "".concat(BytescaleApiClientConfigUtils.getCdnUrl(options !== null && options !== void 0 ? options : {}), "/api/v1/access_tokens/").concat(accountId, "?set-cookie=").concat(setCookie ? "true" : "false");
4310
+ value: function getAccessTokenUrl(cdnUrl, accountId, setCookie) {
4311
+ return "".concat(cdnUrl, "/api/v1/access_tokens/").concat(accountId, "?set-cookie=").concat(setCookie ? "true" : "false");
3971
4312
  }
3972
4313
  }, {
3973
4314
  key: "getCdnUrl",
@@ -3975,12 +4316,20 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3975
4316
  var _a;
3976
4317
  return BytescaleApiClientConfigUtils.getCdnUrl((_a = params.options) !== null && _a !== void 0 ? _a : {});
3977
4318
  }
4319
+ }, {
4320
+ key: "getConfigCdnUrl",
4321
+ value: function getConfigCdnUrl(params, config) {
4322
+ var _a, _b;
4323
+ return BytescaleApiClientConfigUtils.getCdnUrl({
4324
+ cdnUrl: (_a = config.cdnUrl) !== null && _a !== void 0 ? _a : (_b = params.options) === null || _b === void 0 ? void 0 : _b.cdnUrl
4325
+ });
4326
+ }
3978
4327
  }, {
3979
4328
  key: "deleteAccessToken",
3980
- value: function deleteAccessToken(options, accountId) {
4329
+ value: function deleteAccessToken(options, cdnUrl, accountId) {
3981
4330
  try {
3982
- var _this8 = this;
3983
- return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(BaseAPI.fetch(_this8.getAccessTokenUrl(options, accountId, true), {
4331
+ var _this9 = this;
4332
+ return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(BaseAPI.fetch(_this9.getAccessTokenUrl(cdnUrl, accountId, true), {
3984
4333
  method: "DELETE",
3985
4334
  credentials: "include",
3986
4335
  headers: {}
@@ -3994,16 +4343,16 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3994
4343
  }
3995
4344
  }, {
3996
4345
  key: "setAccessToken",
3997
- value: function setAccessToken(options, accountId, jwt, setCookie) {
4346
+ value: function setAccessToken(options, cdnUrl, accountId, jwt, setCookie) {
3998
4347
  try {
3999
- var _this9 = this;
4348
+ var _this0 = this;
4000
4349
  var request = {
4001
4350
  accessToken: jwt
4002
4351
  };
4003
- return AuthManagerBrowser_await(BaseAPI.fetch(_this9.getAccessTokenUrl(options, accountId, setCookie), {
4352
+ return AuthManagerBrowser_await(BaseAPI.fetch(_this0.getAccessTokenUrl(cdnUrl, accountId, setCookie), {
4004
4353
  method: "PUT",
4005
4354
  credentials: "include",
4006
- headers: AuthManagerBrowser_defineProperty({}, _this9.contentType, _this9.contentTypeJson),
4355
+ headers: AuthManagerBrowser_defineProperty({}, _this0.contentType, _this0.contentTypeJson),
4007
4356
  body: JSON.stringify(request)
4008
4357
  }, {
4009
4358
  isBytescaleApi: true,
@@ -4024,20 +4373,20 @@ var AuthManagerImpl = /*#__PURE__*/function () {
4024
4373
  key: "getAuthorizationToken",
4025
4374
  value: function getAuthorizationToken(params, config) {
4026
4375
  try {
4027
- var _exit = false;
4028
- var _this0 = this;
4376
+ var _exit2 = false;
4377
+ var _this1 = this;
4029
4378
  var _a, _b;
4030
4379
  return AuthManagerBrowser_await(AuthManagerBrowser_invoke(function () {
4031
4380
  if (typeof config.getAuthorizationToken === "function") {
4032
- var _validateJwt = _this0.validateJwt;
4381
+ var _validateJwt = _this1.validateJwt;
4033
4382
  return AuthManagerBrowser_await(config.getAuthorizationToken(), function (_config$getAuthorizat) {
4034
- var _this0$validateJwt = _validateJwt.call(_this0, _config$getAuthorizat, "The 'getAuthorizationToken' callback");
4035
- _exit = true;
4036
- return _this0$validateJwt;
4383
+ var _this1$validateJwt = _validateJwt.call(_this1, _config$getAuthorizat, "The 'getAuthorizationToken' callback");
4384
+ _exit2 = true;
4385
+ return _this1$validateJwt;
4037
4386
  });
4038
4387
  }
4039
- }, function (_result3) {
4040
- if (_exit) return _result3;
4388
+ }, function (_result4) {
4389
+ if (_exit2) return _result4;
4041
4390
  var endpointName = "Your auth API endpoint";
4042
4391
  var _fetch = BaseAPI.fetch,
4043
4392
  _config$authUrl = config.authUrl;
@@ -4049,13 +4398,13 @@ var AuthManagerImpl = /*#__PURE__*/function () {
4049
4398
  isBytescaleApi: false,
4050
4399
  fetchApi: (_a = params.options) === null || _a === void 0 ? void 0 : _a.fetchApi
4051
4400
  }), function (result) {
4052
- var actualContentType = (_b = result.headers.get(_this0.contentType)) !== null && _b !== void 0 ? _b : "";
4053
- if (actualContentType.split(";")[0] !== _this0.contentTypeText) {
4054
- throw new Error("".concat(endpointName, " returned \"").concat(actualContentType, "\" for the ").concat(_this0.contentType, " response header, but the Bytescale SDK requires \"").concat(_this0.contentTypeText, "\"."));
4401
+ var actualContentType = (_b = result.headers.get(_this1.contentType)) !== null && _b !== void 0 ? _b : "";
4402
+ if (actualContentType.split(";")[0] !== _this1.contentTypeText) {
4403
+ throw new Error("".concat(endpointName, " returned \"").concat(actualContentType, "\" for the ").concat(_this1.contentType, " response header, but the Bytescale SDK requires \"").concat(_this1.contentTypeText, "\"."));
4055
4404
  }
4056
- var _validateJwt2 = _this0.validateJwt;
4405
+ var _validateJwt2 = _this1.validateJwt;
4057
4406
  return AuthManagerBrowser_await(result.text(), function (_result$text) {
4058
- return _validateJwt2.call(_this0, _result$text, endpointName);
4407
+ return _validateJwt2.call(_this1, _result$text, endpointName);
4059
4408
  });
4060
4409
  });
4061
4410
  });