@bytescale/sdk 3.9.0 → 3.10.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.
@@ -45,7 +45,7 @@ __lib_require__.d(__lib_exports__, {
45
45
  "querystring": function() { return /* reexport */ querystring; }
46
46
  });
47
47
 
48
- ;// CONCATENATED MODULE: ./src/private/Mutex.ts
48
+ ;// CONCATENATED MODULE: ./src/private/FairMutex.ts
49
49
  function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
50
50
  function _call(body, then, direct) {
51
51
  if (direct) {
@@ -88,157 +88,12 @@ function _awaitIgnored(value, direct) {
88
88
  return value && value.then ? value.then(_empty) : Promise.resolve();
89
89
  }
90
90
  }
91
- function _settle(pact, state, value) {
92
- if (!pact.s) {
93
- if (value instanceof _Pact) {
94
- if (value.s) {
95
- if (state & 1) {
96
- state = value.s;
97
- }
98
- value = value.v;
99
- } else {
100
- value.o = _settle.bind(null, pact, state);
101
- return;
102
- }
103
- }
104
- if (value && value.then) {
105
- value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2));
106
- return;
107
- }
108
- pact.s = state;
109
- pact.v = value;
110
- var observer = pact.o;
111
- if (observer) {
112
- observer(pact);
113
- }
114
- }
115
- }
116
- var _Pact = /*#__PURE__*/function () {
117
- function _Pact() {}
118
- _Pact.prototype.then = function (onFulfilled, onRejected) {
119
- var result = new _Pact();
120
- var state = this.s;
121
- if (state) {
122
- var callback = state & 1 ? onFulfilled : onRejected;
123
- if (callback) {
124
- try {
125
- _settle(result, 1, callback(this.v));
126
- } catch (e) {
127
- _settle(result, 2, e);
128
- }
129
- return result;
130
- } else {
131
- return this;
132
- }
133
- }
134
- this.o = function (_this) {
135
- try {
136
- var value = _this.v;
137
- if (_this.s & 1) {
138
- _settle(result, 1, onFulfilled ? onFulfilled(value) : value);
139
- } else if (onRejected) {
140
- _settle(result, 1, onRejected(value));
141
- } else {
142
- _settle(result, 2, value);
143
- }
144
- } catch (e) {
145
- _settle(result, 2, e);
146
- }
147
- };
148
- return result;
149
- };
150
- return _Pact;
151
- }();
152
- function _isSettledPact(thenable) {
153
- return thenable instanceof _Pact && thenable.s & 1;
154
- }
155
- function _for(test, update, body) {
156
- var stage;
157
- for (;;) {
158
- var shouldContinue = test();
159
- if (_isSettledPact(shouldContinue)) {
160
- shouldContinue = shouldContinue.v;
161
- }
162
- if (!shouldContinue) {
163
- return result;
164
- }
165
- if (shouldContinue.then) {
166
- stage = 0;
167
- break;
168
- }
169
- var result = body();
170
- if (result && result.then) {
171
- if (_isSettledPact(result)) {
172
- result = result.s;
173
- } else {
174
- stage = 1;
175
- break;
176
- }
177
- }
178
- if (update) {
179
- var updateValue = update();
180
- if (updateValue && updateValue.then && !_isSettledPact(updateValue)) {
181
- stage = 2;
182
- break;
183
- }
184
- }
185
- }
186
- var pact = new _Pact();
187
- var reject = _settle.bind(null, pact, 2);
188
- (stage === 0 ? shouldContinue.then(_resumeAfterTest) : stage === 1 ? result.then(_resumeAfterBody) : updateValue.then(_resumeAfterUpdate)).then(void 0, reject);
189
- return pact;
190
- function _resumeAfterBody(value) {
191
- result = value;
192
- do {
193
- if (update) {
194
- updateValue = update();
195
- if (updateValue && updateValue.then && !_isSettledPact(updateValue)) {
196
- updateValue.then(_resumeAfterUpdate).then(void 0, reject);
197
- return;
198
- }
199
- }
200
- shouldContinue = test();
201
- if (!shouldContinue || _isSettledPact(shouldContinue) && !shouldContinue.v) {
202
- _settle(pact, 1, result);
203
- return;
204
- }
205
- if (shouldContinue.then) {
206
- shouldContinue.then(_resumeAfterTest).then(void 0, reject);
207
- return;
208
- }
209
- result = body();
210
- if (_isSettledPact(result)) {
211
- result = result.v;
212
- }
213
- } while (!result || !result.then);
214
- result.then(_resumeAfterBody).then(void 0, reject);
215
- }
216
- function _resumeAfterTest(shouldContinue) {
217
- if (shouldContinue) {
218
- result = body();
219
- if (result && result.then) {
220
- result.then(_resumeAfterBody).then(void 0, reject);
221
- } else {
222
- _resumeAfterBody(result);
223
- }
224
- } else {
225
- _settle(pact, 1, result);
226
- }
227
- }
228
- function _resumeAfterUpdate() {
229
- if (shouldContinue = test()) {
230
- if (shouldContinue.then) {
231
- shouldContinue.then(_resumeAfterTest).then(void 0, reject);
232
- } else {
233
- _resumeAfterTest(shouldContinue);
234
- }
235
- } else {
236
- _settle(pact, 1, result);
237
- }
91
+ function _invoke(body, then) {
92
+ var result = body();
93
+ if (result && result.then) {
94
+ return result.then(then);
238
95
  }
239
- }
240
- function _continue(value, then) {
241
- return value && value.then ? value.then(then) : then(value);
96
+ return then(result);
242
97
  }
243
98
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
244
99
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
@@ -246,28 +101,31 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
246
101
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
247
102
  function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
248
103
  /**
249
- * A lightweight mutex. (Other libraries contain too many features and we want to keep size down).
104
+ * A lightweight fair mutex. (Other libraries contain too many features and we want to keep size down).
250
105
  *
251
106
  * Characteristics:
252
107
  * - Non-reentrant.
253
- * - Unfair. (Multiple callers awaiting 'acquire' will be granted the mutex in no order.)
108
+ * - Fair.
109
+ * - This means multiple callers awaiting 'lock' will be granted the mutex in the order they requested it.
110
+ * - This is important, as in React, developers calling 'AuthManager.endAuthSession' in a 'useEffect' cleanup need it
111
+ * to take effect immediately, such that subsequent 'AuthManager.beginAuthSession' calls will always succeed.
254
112
  * - When calling `safe` consecutively with no 'awaits' in-between, the current context will synchronously acquire
255
113
  * the mutex every time.
256
114
  */
257
- var Mutex = /*#__PURE__*/function () {
258
- function Mutex() {
259
- _classCallCheck(this, Mutex);
260
- this.mutex = undefined;
261
- this.resolver = undefined;
115
+ var FairMutex = /*#__PURE__*/function () {
116
+ function FairMutex() {
117
+ _classCallCheck(this, FairMutex);
118
+ this.locked = false;
119
+ this.queue = [];
262
120
  }
263
- _createClass(Mutex, [{
121
+ _createClass(FairMutex, [{
264
122
  key: "safe",
265
123
  value: function safe(callback) {
266
124
  try {
267
125
  var _this2 = this;
268
- return _await(_this2.acquire(), function () {
126
+ return _await(_this2.lock(), function () {
269
127
  return _finallyRethrows(callback, function (_wasThrown, _result) {
270
- _this2.release();
128
+ _this2.unlock();
271
129
  return _rethrow(_wasThrown, _result);
272
130
  });
273
131
  });
@@ -276,37 +134,48 @@ var Mutex = /*#__PURE__*/function () {
276
134
  }
277
135
  }
278
136
  }, {
279
- key: "acquire",
280
- value: function acquire() {
137
+ key: "lock",
138
+ value: function lock() {
281
139
  try {
140
+ var _exit2 = false;
282
141
  var _this4 = this;
283
- // Loop necessary for when multiple calls are made to 'acquire' before a 'release' is called, else the call to
284
- // 'release' will resume every caller currently waiting on 'acquire'.
285
- return _continue(_for(function () {
286
- return _this4.mutex !== undefined;
287
- }, void 0, function () {
288
- return _awaitIgnored(_this4.mutex);
289
- }), function () {
290
- _this4.mutex = new Promise(function (resolve) {
291
- _this4.resolver = resolve;
292
- });
142
+ return _invoke(function () {
143
+ if (_this4.locked) {
144
+ var unlockNext;
145
+ var lockPromise = new Promise(function (resolve) {
146
+ unlockNext = resolve;
147
+ });
148
+ if (unlockNext === undefined) {
149
+ throw new Error("unlockNext was undefined");
150
+ }
151
+ _this4.queue.push({
152
+ resolve: unlockNext
153
+ });
154
+ return _awaitIgnored(lockPromise);
155
+ }
156
+ }, function (_result2) {
157
+ if (_exit2) return _result2;
158
+ _this4.locked = true;
293
159
  });
294
160
  } catch (e) {
295
161
  return Promise.reject(e);
296
162
  }
297
163
  }
298
164
  }, {
299
- key: "release",
300
- value: function release() {
301
- if (this.resolver === undefined) {
302
- throw new Error("Unable to release mutex: already released.");
165
+ key: "unlock",
166
+ value: function unlock() {
167
+ if (!this.locked) {
168
+ throw new Error("Mutex is not locked.");
169
+ }
170
+ var nextInQueue = this.queue.shift();
171
+ if (nextInQueue !== undefined) {
172
+ nextInQueue.resolve();
173
+ } else {
174
+ this.locked = false;
303
175
  }
304
- this.resolver();
305
- this.resolver = undefined;
306
- this.mutex = undefined;
307
176
  }
308
177
  }]);
309
- return Mutex;
178
+ return FairMutex;
310
179
  }();
311
180
  ;// CONCATENATED MODULE: ./src/private/AuthSessionState.ts
312
181
  function AuthSessionState_typeof(obj) { "@babel/helpers - typeof"; return AuthSessionState_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, AuthSessionState_typeof(obj); }
@@ -342,7 +211,7 @@ var AuthSessionState = /*#__PURE__*/function () {
342
211
  var key = AuthSessionState.mutexKey;
343
212
  var mutex = window[key];
344
213
  if (mutex === undefined) {
345
- mutex = new Mutex();
214
+ mutex = new FairMutex();
346
215
  window[key] = mutex;
347
216
  }
348
217
  return mutex;
@@ -395,10 +264,10 @@ function _catch(body, recover) {
395
264
  return result;
396
265
  }
397
266
  function runtime_empty() {}
398
- function runtime_continue(value, then) {
267
+ function _continue(value, then) {
399
268
  return value && value.then ? value.then(then) : then(value);
400
269
  }
401
- function _invoke(body, then) {
270
+ function runtime_invoke(body, then) {
402
271
  var result = body();
403
272
  if (result && result.then) {
404
273
  return result.then(then);
@@ -589,7 +458,7 @@ var BaseAPI = /*#__PURE__*/function () {
589
458
  var _exit3 = false;
590
459
  var _a, _b;
591
460
  var response;
592
- return runtime_continue(_catch(function () {
461
+ return _continue(_catch(function () {
593
462
  return runtime_await(BytescaleApiClientConfigUtils.getFetchApi(config)(url, Object.assign(Object.assign({}, init), {
594
463
  // This is specifically added to cater for Next.js's Fetch implementation, which caches POST requests...
595
464
  //
@@ -609,10 +478,10 @@ var BaseAPI = /*#__PURE__*/function () {
609
478
  }), function (_result) {
610
479
  var _exit2 = false;
611
480
  if (_exit3) return _result;
612
- return response.status >= 200 && response.status < 300 ? response : _invoke(function () {
481
+ return response.status >= 200 && response.status < 300 ? response : runtime_invoke(function () {
613
482
  if (config.isBytescaleApi) {
614
483
  var jsonError = undefined;
615
- return runtime_continue(_catch(function () {
484
+ return _continue(_catch(function () {
616
485
  return runtime_await(response.json(), function (_response$json) {
617
486
  jsonError = _response$json;
618
487
  });
@@ -1818,21 +1687,21 @@ function _invokeIgnored(body) {
1818
1687
  return result.then(UploadManagerBase_empty);
1819
1688
  }
1820
1689
  }
1821
- function UploadManagerBase_settle(pact, state, value) {
1690
+ function _settle(pact, state, value) {
1822
1691
  if (!pact.s) {
1823
- if (value instanceof UploadManagerBase_Pact) {
1692
+ if (value instanceof _Pact) {
1824
1693
  if (value.s) {
1825
1694
  if (state & 1) {
1826
1695
  state = value.s;
1827
1696
  }
1828
1697
  value = value.v;
1829
1698
  } else {
1830
- value.o = UploadManagerBase_settle.bind(null, pact, state);
1699
+ value.o = _settle.bind(null, pact, state);
1831
1700
  return;
1832
1701
  }
1833
1702
  }
1834
1703
  if (value && value.then) {
1835
- value.then(UploadManagerBase_settle.bind(null, pact, state), UploadManagerBase_settle.bind(null, pact, 2));
1704
+ value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2));
1836
1705
  return;
1837
1706
  }
1838
1707
  pact.s = state;
@@ -1843,7 +1712,7 @@ function UploadManagerBase_settle(pact, state, value) {
1843
1712
  }
1844
1713
  }
1845
1714
  }
1846
- var UploadManagerBase_Pact = /*#__PURE__*/function () {
1715
+ var _Pact = /*#__PURE__*/function () {
1847
1716
  function _Pact() {}
1848
1717
  _Pact.prototype.then = function (onFulfilled, onRejected) {
1849
1718
  var result = new _Pact();
@@ -1852,9 +1721,9 @@ var UploadManagerBase_Pact = /*#__PURE__*/function () {
1852
1721
  var callback = state & 1 ? onFulfilled : onRejected;
1853
1722
  if (callback) {
1854
1723
  try {
1855
- UploadManagerBase_settle(result, 1, callback(this.v));
1724
+ _settle(result, 1, callback(this.v));
1856
1725
  } catch (e) {
1857
- UploadManagerBase_settle(result, 2, e);
1726
+ _settle(result, 2, e);
1858
1727
  }
1859
1728
  return result;
1860
1729
  } else {
@@ -1865,28 +1734,28 @@ var UploadManagerBase_Pact = /*#__PURE__*/function () {
1865
1734
  try {
1866
1735
  var value = _this.v;
1867
1736
  if (_this.s & 1) {
1868
- UploadManagerBase_settle(result, 1, onFulfilled ? onFulfilled(value) : value);
1737
+ _settle(result, 1, onFulfilled ? onFulfilled(value) : value);
1869
1738
  } else if (onRejected) {
1870
- UploadManagerBase_settle(result, 1, onRejected(value));
1739
+ _settle(result, 1, onRejected(value));
1871
1740
  } else {
1872
- UploadManagerBase_settle(result, 2, value);
1741
+ _settle(result, 2, value);
1873
1742
  }
1874
1743
  } catch (e) {
1875
- UploadManagerBase_settle(result, 2, e);
1744
+ _settle(result, 2, e);
1876
1745
  }
1877
1746
  };
1878
1747
  return result;
1879
1748
  };
1880
1749
  return _Pact;
1881
1750
  }();
1882
- function UploadManagerBase_isSettledPact(thenable) {
1883
- return thenable instanceof UploadManagerBase_Pact && thenable.s & 1;
1751
+ function _isSettledPact(thenable) {
1752
+ return thenable instanceof _Pact && thenable.s & 1;
1884
1753
  }
1885
- function UploadManagerBase_for(test, update, body) {
1754
+ function _for(test, update, body) {
1886
1755
  var stage;
1887
1756
  for (;;) {
1888
1757
  var shouldContinue = test();
1889
- if (UploadManagerBase_isSettledPact(shouldContinue)) {
1758
+ if (_isSettledPact(shouldContinue)) {
1890
1759
  shouldContinue = shouldContinue.v;
1891
1760
  }
1892
1761
  if (!shouldContinue) {
@@ -1898,7 +1767,7 @@ function UploadManagerBase_for(test, update, body) {
1898
1767
  }
1899
1768
  var result = body();
1900
1769
  if (result && result.then) {
1901
- if (UploadManagerBase_isSettledPact(result)) {
1770
+ if (_isSettledPact(result)) {
1902
1771
  result = result.s;
1903
1772
  } else {
1904
1773
  stage = 1;
@@ -1907,14 +1776,14 @@ function UploadManagerBase_for(test, update, body) {
1907
1776
  }
1908
1777
  if (update) {
1909
1778
  var updateValue = update();
1910
- if (updateValue && updateValue.then && !UploadManagerBase_isSettledPact(updateValue)) {
1779
+ if (updateValue && updateValue.then && !_isSettledPact(updateValue)) {
1911
1780
  stage = 2;
1912
1781
  break;
1913
1782
  }
1914
1783
  }
1915
1784
  }
1916
- var pact = new UploadManagerBase_Pact();
1917
- var reject = UploadManagerBase_settle.bind(null, pact, 2);
1785
+ var pact = new _Pact();
1786
+ var reject = _settle.bind(null, pact, 2);
1918
1787
  (stage === 0 ? shouldContinue.then(_resumeAfterTest) : stage === 1 ? result.then(_resumeAfterBody) : updateValue.then(_resumeAfterUpdate)).then(void 0, reject);
1919
1788
  return pact;
1920
1789
  function _resumeAfterBody(value) {
@@ -1922,14 +1791,14 @@ function UploadManagerBase_for(test, update, body) {
1922
1791
  do {
1923
1792
  if (update) {
1924
1793
  updateValue = update();
1925
- if (updateValue && updateValue.then && !UploadManagerBase_isSettledPact(updateValue)) {
1794
+ if (updateValue && updateValue.then && !_isSettledPact(updateValue)) {
1926
1795
  updateValue.then(_resumeAfterUpdate).then(void 0, reject);
1927
1796
  return;
1928
1797
  }
1929
1798
  }
1930
1799
  shouldContinue = test();
1931
- if (!shouldContinue || UploadManagerBase_isSettledPact(shouldContinue) && !shouldContinue.v) {
1932
- UploadManagerBase_settle(pact, 1, result);
1800
+ if (!shouldContinue || _isSettledPact(shouldContinue) && !shouldContinue.v) {
1801
+ _settle(pact, 1, result);
1933
1802
  return;
1934
1803
  }
1935
1804
  if (shouldContinue.then) {
@@ -1937,7 +1806,7 @@ function UploadManagerBase_for(test, update, body) {
1937
1806
  return;
1938
1807
  }
1939
1808
  result = body();
1940
- if (UploadManagerBase_isSettledPact(result)) {
1809
+ if (_isSettledPact(result)) {
1941
1810
  result = result.v;
1942
1811
  }
1943
1812
  } while (!result || !result.then);
@@ -1952,7 +1821,7 @@ function UploadManagerBase_for(test, update, body) {
1952
1821
  _resumeAfterBody(result);
1953
1822
  }
1954
1823
  } else {
1955
- UploadManagerBase_settle(pact, 1, result);
1824
+ _settle(pact, 1, result);
1956
1825
  }
1957
1826
  }
1958
1827
  function _resumeAfterUpdate() {
@@ -1963,7 +1832,7 @@ function UploadManagerBase_for(test, update, body) {
1963
1832
  _resumeAfterTest(shouldContinue);
1964
1833
  }
1965
1834
  } else {
1966
- UploadManagerBase_settle(pact, 1, result);
1835
+ _settle(pact, 1, result);
1967
1836
  }
1968
1837
  }
1969
1838
  }
@@ -2077,7 +1946,9 @@ var UploadManagerBase = /*#__PURE__*/function () {
2077
1946
  size = _ref$value.size,
2078
1947
  type = _ref$value.type;
2079
1948
  return {
2080
- mime: type,
1949
+ // Some browsers/OSs return 'type: ""' for files with unknown MIME types, like HEICs, which causes a validation
1950
+ // error from the Bytescale API as "" is not a valid MIME type, so we coalesce to undefined here.
1951
+ mime: type === "" ? undefined : type,
2081
1952
  size: size,
2082
1953
  originalFileName: name,
2083
1954
  maxConcurrentUploadParts: undefined
@@ -2265,7 +2136,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2265
2136
  try {
2266
2137
  var workQueue = UploadManagerBase_toConsumableArray(items);
2267
2138
  return UploadManagerBase_awaitIgnored(Promise.all(UploadManagerBase_toConsumableArray(Array(concurrency).keys()).map(_async(function () {
2268
- return _continueIgnored(UploadManagerBase_for(function () {
2139
+ return _continueIgnored(_for(function () {
2269
2140
  return workQueue.length > 0;
2270
2141
  }, void 0, function () {
2271
2142
  var work = workQueue.shift(); // IMPORTANT: use 'shift' instead of 'pop' to ensure 'items' are processed in order when 'concurrency = 1'.
@@ -1,5 +1,5 @@
1
1
  import { AuthSession } from "./model/AuthSession";
2
- import { Mutex } from "./Mutex";
2
+ import { FairMutex } from "./FairMutex";
3
3
  /**
4
4
  * Maintains a global session state, even across package versions.
5
5
  *
@@ -18,7 +18,7 @@ export declare class AuthSessionState {
18
18
  /**
19
19
  * Called in the browser only.
20
20
  */
21
- static getMutex(): Mutex;
21
+ static getMutex(): FairMutex;
22
22
  /**
23
23
  * Called in the browser only.
24
24
  */
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A lightweight fair mutex. (Other libraries contain too many features and we want to keep size down).
3
+ *
4
+ * Characteristics:
5
+ * - Non-reentrant.
6
+ * - Fair.
7
+ * - This means multiple callers awaiting 'lock' will be granted the mutex in the order they requested it.
8
+ * - This is important, as in React, developers calling 'AuthManager.endAuthSession' in a 'useEffect' cleanup need it
9
+ * to take effect immediately, such that subsequent 'AuthManager.beginAuthSession' calls will always succeed.
10
+ * - When calling `safe` consecutively with no 'awaits' in-between, the current context will synchronously acquire
11
+ * the mutex every time.
12
+ */
13
+ export declare class FairMutex {
14
+ private locked;
15
+ private readonly queue;
16
+ safe<T>(callback: () => Promise<T>): Promise<T>;
17
+ private lock;
18
+ private unlock;
19
+ }