@bytescale/sdk 3.0.0-alpha.12 → 3.0.0-alpha.14

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.
@@ -1,25 +1,3 @@
1
- // ----------------------------------------
2
- // POLYFILL GUIDE:
3
- // ----------------------------------------
4
- //
5
- // "buffer" & "stream" are not required for browser environments.
6
- //
7
- // They are only used when the code detects it's running in Node.js (at runtime).
8
- //
9
- // You may polyfill them with empty objects (for browser environments):
10
- //
11
- // ----------------------------------------
12
- // Webpack:
13
- // {
14
- // resolve: {
15
- // fallback: {
16
- // buffer: false,
17
- // stream: false
18
- // }
19
- // }
20
- // }
21
- // ----------------------------------------
22
- //
23
1
  /******/ (function() { // webpackBootstrap
24
2
  /******/ "use strict";
25
3
  /******/ // The require scope
@@ -2066,10 +2044,13 @@ var UploadManagerBase = /*#__PURE__*/function () {
2066
2044
  _this2.assertNotCancelled(request);
2067
2045
  var source = _this2.processUploadSource(request.data);
2068
2046
  var preUploadInfo = _this2.getPreUploadInfo(request, source);
2069
- var onProgress = _this2.makeProgressCallback(request, preUploadInfo.size);
2047
+ var bytesTotal = preUploadInfo.size;
2048
+ var makeOnProgressForPart = _this2.makeOnProgressForPartFactory(request, bytesTotal);
2070
2049
  var init = _this2.preUpload(source);
2071
2050
  // Raise initial progress event SYNCHRONOUSLY.
2072
- onProgress(0);
2051
+ if (request.onProgress !== undefined) {
2052
+ request.onProgress(_this2.makeProgressEvent(0, bytesTotal));
2053
+ }
2073
2054
  return UploadManagerBase_await(_this2.beginUpload(request, preUploadInfo), function (uploadInfo) {
2074
2055
  var partCount = uploadInfo.uploadParts.count;
2075
2056
  var parts = UploadManagerBase_toConsumableArray(Array(partCount).keys());
@@ -2079,7 +2060,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
2079
2060
  var intervalHandle = setInterval(_this2.onIntervalTick(request, cancel), _this2.intervalMs);
2080
2061
  return UploadManagerBase_continue(UploadManagerBase_finallyRethrows(function () {
2081
2062
  return UploadManagerBase_await(_this2.mapAsync(parts, preUploadInfo.maxConcurrentUploadParts, _async(function (part) {
2082
- return _this2.uploadPart(request, source, part, uploadInfo, onProgress, addCancellationHandler);
2063
+ return _this2.uploadPart(request, source, part, uploadInfo, makeOnProgressForPart(), addCancellationHandler);
2083
2064
  })), function () {
2084
2065
  return UploadManagerBase_awaitIgnored(_this2.postUpload(init));
2085
2066
  });
@@ -2135,21 +2116,37 @@ var UploadManagerBase = /*#__PURE__*/function () {
2135
2116
  addCancellationHandler: addCancellationHandler
2136
2117
  };
2137
2118
  }
2119
+ /**
2120
+ * Returns a callback, which when called, returns a callback that can be used by ONE specific part to report its progress.
2121
+ */
2138
2122
  }, {
2139
- key: "makeProgressCallback",
2140
- value: function makeProgressCallback(request, bytesTotal) {
2123
+ key: "makeOnProgressForPartFactory",
2124
+ value: function makeOnProgressForPartFactory(request, bytesTotal) {
2125
+ var _this4 = this;
2141
2126
  var onProgress = request.onProgress;
2142
2127
  if (onProgress === undefined) {
2143
- return function () {};
2128
+ return function () {
2129
+ return function () {};
2130
+ };
2144
2131
  }
2145
2132
  var bytesSent = 0;
2146
- return function (bytesSentDelta) {
2147
- bytesSent += bytesSentDelta;
2148
- onProgress({
2149
- bytesTotal: bytesTotal,
2150
- bytesSent: bytesSent,
2151
- progress: Math.round(bytesSent / bytesTotal * 100)
2152
- });
2133
+ return function () {
2134
+ var bytesSentForPart = 0;
2135
+ return function (bytesSentTotalForPart) {
2136
+ var delta = bytesSentTotalForPart - bytesSentForPart;
2137
+ bytesSentForPart += delta;
2138
+ bytesSent += delta;
2139
+ onProgress(_this4.makeProgressEvent(bytesSent, bytesTotal));
2140
+ };
2141
+ };
2142
+ }
2143
+ }, {
2144
+ key: "makeProgressEvent",
2145
+ value: function makeProgressEvent(bytesSent, bytesTotal) {
2146
+ return {
2147
+ bytesTotal: bytesTotal,
2148
+ bytesSent: bytesSent,
2149
+ progress: Math.round(bytesSent / bytesTotal * 100)
2153
2150
  };
2154
2151
  }
2155
2152
  }, {
@@ -2172,9 +2169,9 @@ var UploadManagerBase = /*#__PURE__*/function () {
2172
2169
  mime = _ref2.mime,
2173
2170
  originalFileName = _ref2.originalFileName;
2174
2171
  try {
2175
- var _this5 = this;
2176
- return UploadManagerBase_await(_this5.uploadApi.beginMultipartUpload({
2177
- accountId: _this5.accountId,
2172
+ var _this6 = this;
2173
+ return UploadManagerBase_await(_this6.uploadApi.beginMultipartUpload({
2174
+ accountId: _this6.accountId,
2178
2175
  beginMultipartUploadRequest: {
2179
2176
  metadata: request.metadata,
2180
2177
  mime: mime,
@@ -2193,14 +2190,14 @@ var UploadManagerBase = /*#__PURE__*/function () {
2193
2190
  key: "uploadPart",
2194
2191
  value: function uploadPart(request, source, partIndex, uploadInfo, onProgress, addCancellationHandler) {
2195
2192
  try {
2196
- var _this7 = this;
2197
- _this7.assertNotCancelled(request);
2198
- return UploadManagerBase_await(_this7.getUploadPart(partIndex, uploadInfo), function (part) {
2199
- _this7.assertNotCancelled(request);
2200
- return UploadManagerBase_await(_this7.putUploadPart(part, source, onProgress, addCancellationHandler), function (etag) {
2201
- _this7.assertNotCancelled(request);
2202
- return UploadManagerBase_awaitIgnored(_this7.uploadApi.completeUploadPart({
2203
- accountId: _this7.accountId,
2193
+ var _this8 = this;
2194
+ _this8.assertNotCancelled(request);
2195
+ return UploadManagerBase_await(_this8.getUploadPart(partIndex, uploadInfo), function (part) {
2196
+ _this8.assertNotCancelled(request);
2197
+ return UploadManagerBase_await(_this8.putUploadPart(part, source, onProgress, addCancellationHandler), function (etag) {
2198
+ _this8.assertNotCancelled(request);
2199
+ return UploadManagerBase_awaitIgnored(_this8.uploadApi.completeUploadPart({
2200
+ accountId: _this8.accountId,
2204
2201
  uploadId: uploadInfo.uploadId,
2205
2202
  uploadPartIndex: partIndex,
2206
2203
  completeUploadPartRequest: {
@@ -2220,9 +2217,9 @@ var UploadManagerBase = /*#__PURE__*/function () {
2220
2217
  key: "putUploadPart",
2221
2218
  value: function putUploadPart(part, source, onProgress, addCancellationHandler) {
2222
2219
  try {
2223
- var _this9 = this;
2220
+ var _this10 = this;
2224
2221
  var contentLength = part.range.inclusiveEnd + 1 - part.range.inclusiveStart;
2225
- return UploadManagerBase_await(_this9.doPutUploadPart(part, contentLength, source, onProgress, addCancellationHandler), function (_ref3) {
2222
+ return UploadManagerBase_await(_this10.doPutUploadPart(part, contentLength, source, onProgress, addCancellationHandler), function (_ref3) {
2226
2223
  var status = _ref3.status,
2227
2224
  etag = _ref3.etag;
2228
2225
  if (Math.floor(status / 100) !== 2) {
@@ -2231,6 +2228,8 @@ var UploadManagerBase = /*#__PURE__*/function () {
2231
2228
  if (etag === undefined) {
2232
2229
  throw new Error("No 'etag' response header found in upload part response.");
2233
2230
  }
2231
+ // Always send 100% for part, as some UploadManager implementations either don't report progress, or may not report the last chunk uploaded.
2232
+ onProgress(contentLength);
2234
2233
  return etag;
2235
2234
  });
2236
2235
  } catch (e) {
@@ -2241,13 +2240,13 @@ var UploadManagerBase = /*#__PURE__*/function () {
2241
2240
  key: "getUploadPart",
2242
2241
  value: function getUploadPart(partIndex, uploadInfo) {
2243
2242
  try {
2244
- var _this11 = this;
2243
+ var _this12 = this;
2245
2244
  if (partIndex === 0) {
2246
2245
  return uploadInfo.uploadParts.first;
2247
2246
  }
2248
- return UploadManagerBase_await(_this11.uploadApi.getUploadPart({
2247
+ return UploadManagerBase_await(_this12.uploadApi.getUploadPart({
2249
2248
  uploadId: uploadInfo.uploadId,
2250
- accountId: _this11.accountId,
2249
+ accountId: _this12.accountId,
2251
2250
  uploadPartIndex: partIndex
2252
2251
  }));
2253
2252
  } catch (e) {
@@ -2318,6 +2317,11 @@ var StreamUtils = /*#__PURE__*/function () {
2318
2317
  StreamUtils.endStream(readable);
2319
2318
  return readable;
2320
2319
  }
2320
+ }, {
2321
+ key: "fromArrayBuffer",
2322
+ value: function fromArrayBuffer(buffer) {
2323
+ return StreamUtils.fromBuffer(Buffer.from(buffer));
2324
+ }
2321
2325
  }, {
2322
2326
  key: "fromBuffer",
2323
2327
  value: function fromBuffer(buffer) {
@@ -2526,6 +2530,61 @@ var NodeChunkedStream = /*#__PURE__*/function () {
2526
2530
  }();
2527
2531
  ;// CONCATENATED MODULE: external "buffer"
2528
2532
  var external_buffer_namespaceObject = require("buffer");
2533
+ ;// CONCATENATED MODULE: ./src/private/UploadManagerFetchUtils.ts
2534
+ function UploadManagerFetchUtils_typeof(obj) { "@babel/helpers - typeof"; return UploadManagerFetchUtils_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; }, UploadManagerFetchUtils_typeof(obj); }
2535
+ function UploadManagerFetchUtils_await(value, then, direct) {
2536
+ if (direct) {
2537
+ return then ? then(value) : value;
2538
+ }
2539
+ if (!value || !value.then) {
2540
+ value = Promise.resolve(value);
2541
+ }
2542
+ return then ? value.then(then) : value;
2543
+ }
2544
+ function UploadManagerFetchUtils_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2545
+ function UploadManagerFetchUtils_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, UploadManagerFetchUtils_toPropertyKey(descriptor.key), descriptor); } }
2546
+ function UploadManagerFetchUtils_createClass(Constructor, protoProps, staticProps) { if (protoProps) UploadManagerFetchUtils_defineProperties(Constructor.prototype, protoProps); if (staticProps) UploadManagerFetchUtils_defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
2547
+ function UploadManagerFetchUtils_toPropertyKey(arg) { var key = UploadManagerFetchUtils_toPrimitive(arg, "string"); return UploadManagerFetchUtils_typeof(key) === "symbol" ? key : String(key); }
2548
+ function UploadManagerFetchUtils_toPrimitive(input, hint) { if (UploadManagerFetchUtils_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (UploadManagerFetchUtils_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
2549
+
2550
+ var UploadManagerFetchUtils = /*#__PURE__*/function () {
2551
+ function UploadManagerFetchUtils() {
2552
+ UploadManagerFetchUtils_classCallCheck(this, UploadManagerFetchUtils);
2553
+ }
2554
+ UploadManagerFetchUtils_createClass(UploadManagerFetchUtils, null, [{
2555
+ key: "doPutUploadPart",
2556
+ value: function doPutUploadPart(config, part, content, contentLength, addCancellationHandler) {
2557
+ try {
2558
+ var _a;
2559
+ var fetchApi = BytescaleApiClientConfigUtils.getFetchApi(config);
2560
+ // Configure cancellation:
2561
+ var controller = new AbortController();
2562
+ var signal = controller.signal;
2563
+ addCancellationHandler(function () {
2564
+ return controller.abort();
2565
+ });
2566
+ var headers = {
2567
+ // Required to prevent fetch using "Transfer-Encoding: Chunked" when body is a stream.
2568
+ "content-length": contentLength.toString()
2569
+ };
2570
+ return UploadManagerFetchUtils_await(fetchApi(part.uploadUrl, {
2571
+ method: "PUT",
2572
+ headers: headers,
2573
+ body: content,
2574
+ signal: signal
2575
+ }), function (response) {
2576
+ return {
2577
+ etag: (_a = response.headers.get("etag")) !== null && _a !== void 0 ? _a : undefined,
2578
+ status: response.status
2579
+ };
2580
+ });
2581
+ } catch (e) {
2582
+ return Promise.reject(e);
2583
+ }
2584
+ }
2585
+ }]);
2586
+ return UploadManagerFetchUtils;
2587
+ }();
2529
2588
  ;// CONCATENATED MODULE: ./src/public/node/UploadManagerNode.ts
2530
2589
  function UploadManagerNode_typeof(obj) { "@babel/helpers - typeof"; return UploadManagerNode_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; }, UploadManagerNode_typeof(obj); }
2531
2590
  function UploadManagerNode_empty() {}
@@ -2725,6 +2784,7 @@ function UploadManagerNode_getPrototypeOf(o) { UploadManagerNode_getPrototypeOf
2725
2784
 
2726
2785
 
2727
2786
 
2787
+
2728
2788
  var UploadManager = /*#__PURE__*/function (_UploadManagerBase) {
2729
2789
  UploadManagerNode_inherits(UploadManager, _UploadManagerBase);
2730
2790
  var _super = UploadManagerNode_createSuper(UploadManager);
@@ -2756,13 +2816,19 @@ var UploadManager = /*#__PURE__*/function (_UploadManagerBase) {
2756
2816
  value: data
2757
2817
  };
2758
2818
  }
2819
+ if (data.byteLength !== undefined) {
2820
+ return {
2821
+ type: "ArrayBuffer",
2822
+ value: data
2823
+ };
2824
+ }
2759
2825
  if (data.size !== undefined) {
2760
2826
  return {
2761
2827
  type: "Blob",
2762
2828
  value: data
2763
2829
  };
2764
2830
  }
2765
- throw new Error("Unsupported type for 'data' parameter. Please provide a String, Blob, or Readable (Node.js).");
2831
+ throw new Error("Unsupported type for 'data' parameter. Please provide a String, Blob, Buffer, ArrayBuffer, or ReadableStream (Node.js).");
2766
2832
  }
2767
2833
  }, {
2768
2834
  key: "getPreUploadInfoPartial",
@@ -2771,9 +2837,10 @@ var UploadManager = /*#__PURE__*/function (_UploadManagerBase) {
2771
2837
  case "Blob":
2772
2838
  return this.getBlobInfo(data);
2773
2839
  case "Buffer":
2840
+ case "ArrayBuffer":
2774
2841
  return {
2775
2842
  mime: undefined,
2776
- size: data.value.length,
2843
+ size: data.value.byteLength,
2777
2844
  originalFileName: undefined,
2778
2845
  maxConcurrentUploadParts: undefined
2779
2846
  };
@@ -2824,40 +2891,20 @@ var UploadManager = /*#__PURE__*/function (_UploadManagerBase) {
2824
2891
  value: function doPutUploadPart(part, contentLength, source, onProgress, addCancellationHandler) {
2825
2892
  try {
2826
2893
  var _this2 = this;
2827
- var _a, _b;
2828
- var fetchApi = (_a = _this2.config.fetchApi) !== null && _a !== void 0 ? _a : fetch;
2829
- var headers = {
2830
- // Required to prevent fetch using "Transfer-Encoding: Chunked" when body is a stream.
2831
- "content-length": contentLength.toString()
2832
- };
2833
- // Configure cancellation:
2834
- var controller = new AbortController();
2835
- var signal = controller.signal;
2836
- addCancellationHandler(function () {
2837
- return controller.abort();
2838
- });
2839
2894
  // Report progress:
2840
2895
  var hasWarned = false;
2896
+ var bytesSent = 0;
2841
2897
  return UploadManagerNode_await(_this2.sliceDataForRequest(source, part), function (stream) {
2842
2898
  var streamWithProgress = stream.on("data", function (data) {
2843
- if (data.length !== undefined) {
2844
- onProgress(data.length);
2899
+ if (data.byteLength !== undefined) {
2900
+ bytesSent += data.byteLength;
2901
+ onProgress(bytesSent);
2845
2902
  } else if (!hasWarned) {
2846
2903
  hasWarned = true;
2847
2904
  console.warn("Expected stream to contain buffers, but it did not, so upload progress won't be reported.");
2848
2905
  }
2849
2906
  });
2850
- return UploadManagerNode_await(fetchApi(part.uploadUrl, {
2851
- method: "PUT",
2852
- headers: headers,
2853
- body: _this2.coerceRequestBody(streamWithProgress),
2854
- signal: signal
2855
- }), function (response) {
2856
- return {
2857
- etag: (_b = response.headers.get("etag")) !== null && _b !== void 0 ? _b : undefined,
2858
- status: response.status
2859
- };
2860
- });
2907
+ return UploadManagerNode_await(UploadManagerFetchUtils.doPutUploadPart(_this2.config, part, _this2.coerceRequestBody(streamWithProgress), contentLength, addCancellationHandler));
2861
2908
  });
2862
2909
  } catch (e) {
2863
2910
  return Promise.reject(e);
@@ -2881,12 +2928,14 @@ var UploadManager = /*#__PURE__*/function (_UploadManagerBase) {
2881
2928
  return _switch(data.type, [[function () {
2882
2929
  return "Blob";
2883
2930
  }, function () {
2884
- var _fromBuffer = StreamUtils.fromBuffer,
2885
- _Buffer = Buffer,
2886
- _from = _Buffer.from;
2931
+ var _fromArrayBuffer = StreamUtils.fromArrayBuffer;
2887
2932
  return UploadManagerNode_await(data.value.slice(start, endExclusive).arrayBuffer(), function (_data$value$slice$arr) {
2888
- return _fromBuffer.call(StreamUtils, _from.call(_Buffer, _data$value$slice$arr));
2933
+ return _fromArrayBuffer.call(StreamUtils, _data$value$slice$arr);
2889
2934
  });
2935
+ }], [function () {
2936
+ return "ArrayBuffer";
2937
+ }, function () {
2938
+ return StreamUtils.fromArrayBuffer(data.value.slice(start, endExclusive));
2890
2939
  }], [function () {
2891
2940
  return "Buffer";
2892
2941
  }, function () {
@@ -2917,17 +2966,17 @@ var EnvChecker = /*#__PURE__*/function () {
2917
2966
  EnvChecker_classCallCheck(this, EnvChecker);
2918
2967
  }
2919
2968
  EnvChecker_createClass(EnvChecker, null, [{
2920
- key: "isNodeJs",
2921
- value: function isNodeJs() {
2922
- return typeof process !== "undefined" && process.versions !== undefined && process.versions.node !== undefined;
2969
+ key: "isBrowser",
2970
+ value: function isBrowser() {
2971
+ return typeof window !== "undefined";
2923
2972
  }
2924
2973
  }, {
2925
2974
  key: "methodRequiresBrowser",
2926
2975
  value: function methodRequiresBrowser(methodName) {
2927
- if (EnvChecker.isNodeJs()) {
2928
- return new Error("You must call '".concat(methodName, "' in the browser. (You are currently in Node.js.)"));
2976
+ if (!EnvChecker.isBrowser()) {
2977
+ return new Error("You must call '".concat(methodName, "' in your client-side code. (You have called it in your server-side code.)"));
2929
2978
  }
2930
- return new Error("The '".concat(methodName, "' method cannot be called because you have bundled the Node.js implementation of the Bytescale JavaScript SDK into your client-side code. Please ensure your bundling tool (Webpack, Rollup, etc.) is honouring the \"browser\" field of the \"package.json\" file for this package, as this will allow the browser implementation of the Bytescale JavaScript SDK to be included in your client-side code."));
2979
+ return new Error("The '".concat(methodName, "' method cannot be called because you have bundled a non-browser implementation of the Bytescale JavaScript SDK into your client-side code. Please ensure your bundling tool (Webpack, Rollup, etc.) is honouring the \"browser\" field of the \"package.json\" file for this package, as this will allow the browser implementation of the Bytescale JavaScript SDK to be included in your client-side code."));
2931
2980
  }
2932
2981
  }]);
2933
2982
  return EnvChecker;