@firebase/storage 0.8.7 → 0.9.0-canary.cb7299cbf

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/index.browser.cjs.js +198 -25
  3. package/dist/index.browser.cjs.js.map +1 -1
  4. package/dist/index.esm2017.js +177 -26
  5. package/dist/index.esm2017.js.map +1 -1
  6. package/dist/index.esm5.js +196 -26
  7. package/dist/index.esm5.js.map +1 -1
  8. package/dist/index.node.cjs.js +245 -45
  9. package/dist/index.node.cjs.js.map +1 -1
  10. package/dist/node-esm/index.node.esm.js +189 -46
  11. package/dist/node-esm/index.node.esm.js.map +1 -1
  12. package/dist/node-esm/src/api.browser.d.ts +32 -1
  13. package/dist/node-esm/src/api.d.ts +15 -0
  14. package/dist/node-esm/src/api.node.d.ts +32 -1
  15. package/dist/node-esm/src/implementation/connection.d.ts +14 -4
  16. package/dist/node-esm/src/implementation/request.d.ts +5 -5
  17. package/dist/node-esm/src/implementation/requestinfo.d.ts +14 -6
  18. package/dist/node-esm/src/implementation/requests.d.ts +17 -16
  19. package/dist/node-esm/src/platform/browser/connection.d.ts +24 -7
  20. package/dist/node-esm/src/platform/connection.d.ts +6 -2
  21. package/dist/node-esm/src/platform/node/connection.d.ts +30 -14
  22. package/dist/node-esm/src/reference.d.ts +13 -0
  23. package/dist/node-esm/src/service.d.ts +3 -3
  24. package/dist/node-esm/test/integration/integration.test.d.ts +4 -0
  25. package/dist/node-esm/test/{unit → node}/connection.test.d.ts +0 -0
  26. package/dist/{test/unit/connection.test.d.ts → node-esm/test/node/stream.test.d.ts} +0 -0
  27. package/dist/node-esm/test/unit/connection.d.ts +4 -3
  28. package/dist/node-esm/test/unit/testshared.d.ts +2 -2
  29. package/dist/src/api.browser.d.ts +32 -1
  30. package/dist/src/api.d.ts +15 -0
  31. package/dist/src/api.node.d.ts +32 -1
  32. package/dist/src/implementation/connection.d.ts +14 -4
  33. package/dist/src/implementation/request.d.ts +5 -5
  34. package/dist/src/implementation/requestinfo.d.ts +14 -6
  35. package/dist/src/implementation/requests.d.ts +17 -16
  36. package/dist/src/platform/browser/connection.d.ts +24 -7
  37. package/dist/src/platform/connection.d.ts +6 -2
  38. package/dist/src/platform/node/connection.d.ts +30 -14
  39. package/dist/src/reference.d.ts +13 -0
  40. package/dist/src/service.d.ts +3 -3
  41. package/dist/storage-public.d.ts +46 -0
  42. package/dist/storage.d.ts +80 -11
  43. package/dist/test/integration/integration.test.d.ts +4 -0
  44. package/dist/test/node/connection.test.d.ts +17 -0
  45. package/dist/test/node/stream.test.d.ts +17 -0
  46. package/dist/test/unit/connection.d.ts +4 -3
  47. package/dist/test/unit/testshared.d.ts +2 -2
  48. package/package.json +6 -6
@@ -556,6 +556,14 @@ var ErrorCode;
556
556
  * See the License for the specific language governing permissions and
557
557
  * limitations under the License.
558
558
  */
559
+ /**
560
+ * Handles network logic for all Storage Requests, including error reporting and
561
+ * retries with backoff.
562
+ *
563
+ * @param I - the type of the backend's network response.
564
+ * @param - O the output type used by the rest of the SDK. The conversion
565
+ * happens in the specified `callback_`.
566
+ */
559
567
  class NetworkRequest {
560
568
  constructor(url_, method_, headers_, body_, successCodes_, additionalRetryCodes_, callback_, errorCallback_, timeout_, progressCallback_, connectionFactory_) {
561
569
  this.url_ = url_;
@@ -632,7 +640,7 @@ class NetworkRequest {
632
640
  const connection = status.connection;
633
641
  if (status.wasSuccessCode) {
634
642
  try {
635
- const result = this.callback_(connection, connection.getResponseText());
643
+ const result = this.callback_(connection, connection.getResponse());
636
644
  if (isJustDef(result)) {
637
645
  resolve(result);
638
646
  }
@@ -647,7 +655,7 @@ class NetworkRequest {
647
655
  else {
648
656
  if (connection !== null) {
649
657
  const err = unknown();
650
- err.serverResponse = connection.getResponseText();
658
+ err.serverResponse = connection.getErrorText();
651
659
  if (this.errorCallback_) {
652
660
  reject(this.errorCallback_(connection, err));
653
661
  }
@@ -778,7 +786,7 @@ function getBlobBuilder() {
778
786
  * @param args The values that will make up the resulting blob.
779
787
  * @return The blob.
780
788
  */
781
- function getBlob(...args) {
789
+ function getBlob$1(...args) {
782
790
  const BlobBuilder = getBlobBuilder();
783
791
  if (BlobBuilder !== undefined) {
784
792
  const bb = new BlobBuilder();
@@ -1128,7 +1136,7 @@ class FbsBlob {
1128
1136
  return val;
1129
1137
  }
1130
1138
  });
1131
- return new FbsBlob(getBlob.apply(null, blobby));
1139
+ return new FbsBlob(getBlob$1.apply(null, blobby));
1132
1140
  }
1133
1141
  else {
1134
1142
  const uint8Arrays = args.map((val) => {
@@ -1450,6 +1458,12 @@ function fromResponseString(service, bucket, resourceString) {
1450
1458
  return fromBackendResponse(service, bucket, resource);
1451
1459
  }
1452
1460
 
1461
+ /**
1462
+ * Contains a fully specified request.
1463
+ *
1464
+ * @param I - the type of the backend's network response.
1465
+ * @param O - the output response type used by the rest of the SDK.
1466
+ */
1453
1467
  class RequestInfo {
1454
1468
  constructor(url, method,
1455
1469
  /**
@@ -1533,7 +1547,7 @@ function sharedErrorHandler(location) {
1533
1547
  if (
1534
1548
  // This exact message string is the only consistent part of the
1535
1549
  // server's error response that identifies it as an App Check error.
1536
- xhr.getResponseText().includes('Firebase App Check token is invalid')) {
1550
+ xhr.getErrorText().includes('Firebase App Check token is invalid')) {
1537
1551
  newErr = unauthorizedApp();
1538
1552
  }
1539
1553
  else {
@@ -1605,6 +1619,19 @@ function list$2(service, location, delimiter, pageToken, maxResults) {
1605
1619
  requestInfo.errorHandler = sharedErrorHandler(location);
1606
1620
  return requestInfo;
1607
1621
  }
1622
+ function getBytes$1(service, location, maxDownloadSizeBytes) {
1623
+ const urlPart = location.fullServerUrl();
1624
+ const url = makeUrl(urlPart, service.host, service._protocol) + '?alt=media';
1625
+ const method = 'GET';
1626
+ const timeout = service.maxOperationRetryTime;
1627
+ const requestInfo = new RequestInfo(url, method, (_, data) => data, timeout);
1628
+ requestInfo.errorHandler = objectErrorHandler(location);
1629
+ if (maxDownloadSizeBytes !== undefined) {
1630
+ requestInfo.headers['Range'] = `bytes=0-${maxDownloadSizeBytes}`;
1631
+ requestInfo.successCodes = [200 /* OK */, 206 /* Partial Content */];
1632
+ }
1633
+ return requestInfo;
1634
+ }
1608
1635
  function getDownloadUrl(service, location, mappings) {
1609
1636
  const urlPart = location.fullServerUrl();
1610
1637
  const url = makeUrl(urlPart, service.host, service._protocol);
@@ -2011,7 +2038,7 @@ function async(f) {
2011
2038
  * limitations under the License.
2012
2039
  */
2013
2040
  /** An override for the text-based Connection. Used in tests. */
2014
- let connectionFactoryOverride = null;
2041
+ let textFactoryOverride = null;
2015
2042
  /**
2016
2043
  * Network layer for browsers. We use this instead of goog.net.XhrIo because
2017
2044
  * goog.net.XhrIo is hyuuuuge and doesn't work in React Native on Android.
@@ -2020,6 +2047,7 @@ class XhrConnection {
2020
2047
  constructor() {
2021
2048
  this.sent_ = false;
2022
2049
  this.xhr_ = new XMLHttpRequest();
2050
+ this.initXhr();
2023
2051
  this.errorCode_ = ErrorCode.NO_ERROR;
2024
2052
  this.sendPromise_ = new Promise(resolve => {
2025
2053
  this.xhr_.addEventListener('abort', () => {
@@ -2073,11 +2101,17 @@ class XhrConnection {
2073
2101
  return -1;
2074
2102
  }
2075
2103
  }
2076
- getResponseText() {
2104
+ getResponse() {
2077
2105
  if (!this.sent_) {
2078
- throw internalError('cannot .getResponseText() before sending');
2106
+ throw internalError('cannot .getResponse() before sending');
2079
2107
  }
2080
- return this.xhr_.responseText;
2108
+ return this.xhr_.response;
2109
+ }
2110
+ getErrorText() {
2111
+ if (!this.sent_) {
2112
+ throw internalError('cannot .getErrorText() before sending');
2113
+ }
2114
+ return this.xhr_.statusText;
2081
2115
  }
2082
2116
  /** Aborts the request. */
2083
2117
  abort() {
@@ -2097,10 +2131,29 @@ class XhrConnection {
2097
2131
  }
2098
2132
  }
2099
2133
  }
2100
- function newConnection() {
2101
- return connectionFactoryOverride
2102
- ? connectionFactoryOverride()
2103
- : new XhrConnection();
2134
+ class XhrTextConnection extends XhrConnection {
2135
+ initXhr() {
2136
+ this.xhr_.responseType = 'text';
2137
+ }
2138
+ }
2139
+ function newTextConnection() {
2140
+ return textFactoryOverride ? textFactoryOverride() : new XhrTextConnection();
2141
+ }
2142
+ class XhrBytesConnection extends XhrConnection {
2143
+ initXhr() {
2144
+ this.xhr_.responseType = 'arraybuffer';
2145
+ }
2146
+ }
2147
+ function newBytesConnection() {
2148
+ return new XhrBytesConnection();
2149
+ }
2150
+ class XhrBlobConnection extends XhrConnection {
2151
+ initXhr() {
2152
+ this.xhr_.responseType = 'blob';
2153
+ }
2154
+ }
2155
+ function newBlobConnection() {
2156
+ return new XhrBlobConnection();
2104
2157
  }
2105
2158
 
2106
2159
  /**
@@ -2242,7 +2295,7 @@ class UploadTask {
2242
2295
  _createResumable() {
2243
2296
  this._resolveToken((authToken, appCheckToken) => {
2244
2297
  const requestInfo = createResumableUpload(this._ref.storage, this._ref._location, this._mappings, this._blob, this._metadata);
2245
- const createRequest = this._ref.storage._makeRequest(requestInfo, newConnection, authToken, appCheckToken);
2298
+ const createRequest = this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken);
2246
2299
  this._request = createRequest;
2247
2300
  createRequest.getPromise().then((url) => {
2248
2301
  this._request = undefined;
@@ -2257,7 +2310,7 @@ class UploadTask {
2257
2310
  const url = this._uploadUrl;
2258
2311
  this._resolveToken((authToken, appCheckToken) => {
2259
2312
  const requestInfo = getResumableUploadStatus(this._ref.storage, this._ref._location, url, this._blob);
2260
- const statusRequest = this._ref.storage._makeRequest(requestInfo, newConnection, authToken, appCheckToken);
2313
+ const statusRequest = this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken);
2261
2314
  this._request = statusRequest;
2262
2315
  statusRequest.getPromise().then(status => {
2263
2316
  status = status;
@@ -2286,7 +2339,7 @@ class UploadTask {
2286
2339
  this._transition("error" /* ERROR */);
2287
2340
  return;
2288
2341
  }
2289
- const uploadRequest = this._ref.storage._makeRequest(requestInfo, newConnection, authToken, appCheckToken);
2342
+ const uploadRequest = this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken);
2290
2343
  this._request = uploadRequest;
2291
2344
  uploadRequest.getPromise().then((newStatus) => {
2292
2345
  this._increaseMultiplier();
@@ -2312,7 +2365,7 @@ class UploadTask {
2312
2365
  _fetchMetadata() {
2313
2366
  this._resolveToken((authToken, appCheckToken) => {
2314
2367
  const requestInfo = getMetadata$2(this._ref.storage, this._ref._location, this._mappings);
2315
- const metadataRequest = this._ref.storage._makeRequest(requestInfo, newConnection, authToken, appCheckToken);
2368
+ const metadataRequest = this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken);
2316
2369
  this._request = metadataRequest;
2317
2370
  metadataRequest.getPromise().then(metadata => {
2318
2371
  this._request = undefined;
@@ -2324,7 +2377,7 @@ class UploadTask {
2324
2377
  _oneShotUpload() {
2325
2378
  this._resolveToken((authToken, appCheckToken) => {
2326
2379
  const requestInfo = multipartUpload(this._ref.storage, this._ref._location, this._mappings, this._blob, this._metadata);
2327
- const multipartRequest = this._ref.storage._makeRequest(requestInfo, newConnection, authToken, appCheckToken);
2380
+ const multipartRequest = this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken);
2328
2381
  this._request = multipartRequest;
2329
2382
  multipartRequest.getPromise().then(metadata => {
2330
2383
  this._request = undefined;
@@ -2689,6 +2742,34 @@ class Reference {
2689
2742
  }
2690
2743
  }
2691
2744
  }
2745
+ /**
2746
+ * Download the bytes at the object's location.
2747
+ * @returns A Promise containing the downloaded bytes.
2748
+ */
2749
+ function getBytesInternal(ref, maxDownloadSizeBytes) {
2750
+ ref._throwIfRoot('getBytes');
2751
+ const requestInfo = getBytes$1(ref.storage, ref._location, maxDownloadSizeBytes);
2752
+ return ref.storage
2753
+ .makeRequestWithTokens(requestInfo, newBytesConnection)
2754
+ .then(bytes => maxDownloadSizeBytes !== undefined
2755
+ ? // GCS may not honor the Range header for small files
2756
+ bytes.slice(0, maxDownloadSizeBytes)
2757
+ : bytes);
2758
+ }
2759
+ /**
2760
+ * Download the bytes at the object's location.
2761
+ * @returns A Promise containing the downloaded blob.
2762
+ */
2763
+ function getBlobInternal(ref, maxDownloadSizeBytes) {
2764
+ ref._throwIfRoot('getBlob');
2765
+ const requestInfo = getBytes$1(ref.storage, ref._location, maxDownloadSizeBytes);
2766
+ return ref.storage
2767
+ .makeRequestWithTokens(requestInfo, newBlobConnection)
2768
+ .then(blob => maxDownloadSizeBytes !== undefined
2769
+ ? // GCS may not honor the Range header for small files
2770
+ blob.slice(0, maxDownloadSizeBytes)
2771
+ : blob);
2772
+ }
2692
2773
  /**
2693
2774
  * Uploads data to this object's location.
2694
2775
  * The upload is not resumable.
@@ -2702,7 +2783,7 @@ function uploadBytes$1(ref, data, metadata) {
2702
2783
  ref._throwIfRoot('uploadBytes');
2703
2784
  const requestInfo = multipartUpload(ref.storage, ref._location, getMappings(), new FbsBlob(data, true), metadata);
2704
2785
  return ref.storage
2705
- .makeRequestWithTokens(requestInfo, newConnection)
2786
+ .makeRequestWithTokens(requestInfo, newTextConnection)
2706
2787
  .then(finalMetadata => {
2707
2788
  return {
2708
2789
  metadata: finalMetadata,
@@ -2819,7 +2900,7 @@ function list$1(ref, options) {
2819
2900
  const op = options || {};
2820
2901
  const requestInfo = list$2(ref.storage, ref._location,
2821
2902
  /*delimiter= */ '/', op.pageToken, op.maxResults);
2822
- return ref.storage.makeRequestWithTokens(requestInfo, newConnection);
2903
+ return ref.storage.makeRequestWithTokens(requestInfo, newTextConnection);
2823
2904
  }
2824
2905
  /**
2825
2906
  * A `Promise` that resolves with the metadata for this object. If this
@@ -2831,7 +2912,7 @@ function list$1(ref, options) {
2831
2912
  function getMetadata$1(ref) {
2832
2913
  ref._throwIfRoot('getMetadata');
2833
2914
  const requestInfo = getMetadata$2(ref.storage, ref._location, getMappings());
2834
- return ref.storage.makeRequestWithTokens(requestInfo, newConnection);
2915
+ return ref.storage.makeRequestWithTokens(requestInfo, newTextConnection);
2835
2916
  }
2836
2917
  /**
2837
2918
  * Updates the metadata for this object.
@@ -2847,7 +2928,7 @@ function getMetadata$1(ref) {
2847
2928
  function updateMetadata$1(ref, metadata) {
2848
2929
  ref._throwIfRoot('updateMetadata');
2849
2930
  const requestInfo = updateMetadata$2(ref.storage, ref._location, metadata, getMappings());
2850
- return ref.storage.makeRequestWithTokens(requestInfo, newConnection);
2931
+ return ref.storage.makeRequestWithTokens(requestInfo, newTextConnection);
2851
2932
  }
2852
2933
  /**
2853
2934
  * Returns the download URL for the given Reference.
@@ -2859,7 +2940,7 @@ function getDownloadURL$1(ref) {
2859
2940
  ref._throwIfRoot('getDownloadURL');
2860
2941
  const requestInfo = getDownloadUrl(ref.storage, ref._location, getMappings());
2861
2942
  return ref.storage
2862
- .makeRequestWithTokens(requestInfo, newConnection)
2943
+ .makeRequestWithTokens(requestInfo, newTextConnection)
2863
2944
  .then(url => {
2864
2945
  if (url === null) {
2865
2946
  throw noDownloadURL();
@@ -2876,7 +2957,7 @@ function getDownloadURL$1(ref) {
2876
2957
  function deleteObject$1(ref) {
2877
2958
  ref._throwIfRoot('deleteObject');
2878
2959
  const requestInfo = deleteObject$2(ref.storage, ref._location);
2879
- return ref.storage.makeRequestWithTokens(requestInfo, newConnection);
2960
+ return ref.storage.makeRequestWithTokens(requestInfo, newTextConnection);
2880
2961
  }
2881
2962
  /**
2882
2963
  * Returns reference for object obtained by appending `childPath` to `ref`.
@@ -3133,7 +3214,7 @@ class FirebaseStorageImpl {
3133
3214
  }
3134
3215
 
3135
3216
  const name = "@firebase/storage";
3136
- const version = "0.8.7";
3217
+ const version = "0.9.0-canary.cb7299cbf";
3137
3218
 
3138
3219
  /**
3139
3220
  * @license
@@ -3172,6 +3253,24 @@ const STORAGE_TYPE = 'storage';
3172
3253
  * See the License for the specific language governing permissions and
3173
3254
  * limitations under the License.
3174
3255
  */
3256
+ /**
3257
+ * Downloads the data at the object's location. Returns an error if the object
3258
+ * is not found.
3259
+ *
3260
+ * To use this functionality, you have to whitelist your app's origin in your
3261
+ * Cloud Storage bucket. See also
3262
+ * https://cloud.google.com/storage/docs/configuring-cors
3263
+ *
3264
+ * @public
3265
+ * @param ref - StorageReference where data should be downloaded.
3266
+ * @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
3267
+ * retrieve.
3268
+ * @returns A Promise containing the object's bytes
3269
+ */
3270
+ function getBytes(ref, maxDownloadSizeBytes) {
3271
+ ref = getModularInstance(ref);
3272
+ return getBytesInternal(ref, maxDownloadSizeBytes);
3273
+ }
3175
3274
  /**
3176
3275
  * Uploads data to this object's location.
3177
3276
  * The upload is not resumable.
@@ -3346,6 +3445,58 @@ function connectStorageEmulator(storage, host, port, options = {}) {
3346
3445
  connectStorageEmulator$1(storage, host, port, options);
3347
3446
  }
3348
3447
 
3448
+ /**
3449
+ * @license
3450
+ * Copyright 2021 Google LLC
3451
+ *
3452
+ * Licensed under the Apache License, Version 2.0 (the "License");
3453
+ * you may not use this file except in compliance with the License.
3454
+ * You may obtain a copy of the License at
3455
+ *
3456
+ * http://www.apache.org/licenses/LICENSE-2.0
3457
+ *
3458
+ * Unless required by applicable law or agreed to in writing, software
3459
+ * distributed under the License is distributed on an "AS IS" BASIS,
3460
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3461
+ * See the License for the specific language governing permissions and
3462
+ * limitations under the License.
3463
+ */
3464
+ /**
3465
+ * Downloads the data at the object's location. Returns an error if the object
3466
+ * is not found.
3467
+ *
3468
+ * To use this functionality, you have to whitelist your app's origin in your
3469
+ * Cloud Storage bucket. See also
3470
+ * https://cloud.google.com/storage/docs/configuring-cors
3471
+ *
3472
+ * This API is not available in Node.
3473
+ *
3474
+ * @public
3475
+ * @param ref - StorageReference where data should be downloaded.
3476
+ * @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
3477
+ * retrieve.
3478
+ * @returns A Promise that resolves with a Blob containing the object's bytes
3479
+ */
3480
+ function getBlob(ref, maxDownloadSizeBytes) {
3481
+ ref = getModularInstance(ref);
3482
+ return getBlobInternal(ref, maxDownloadSizeBytes);
3483
+ }
3484
+ /**
3485
+ * Downloads the data at the object's location. Raises an error event if the
3486
+ * object is not found.
3487
+ *
3488
+ * This API is only available in Node.
3489
+ *
3490
+ * @public
3491
+ * @param ref - StorageReference where data should be downloaded.
3492
+ * @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
3493
+ * retrieve.
3494
+ * @returns A stream with the object's data as bytes
3495
+ */
3496
+ function getStream(ref, maxDownloadSizeBytes) {
3497
+ throw new Error('getStream() is only supported by NodeJS builds');
3498
+ }
3499
+
3349
3500
  /**
3350
3501
  * Cloud Storage for Firebase
3351
3502
  *
@@ -3366,5 +3517,5 @@ function registerStorage() {
3366
3517
  }
3367
3518
  registerStorage();
3368
3519
 
3369
- export { StringFormat, FbsBlob as _FbsBlob, Location as _Location, TaskEvent as _TaskEvent, TaskState as _TaskState, UploadTask as _UploadTask, dataFromString as _dataFromString, _getChild, invalidArgument as _invalidArgument, invalidRootOperation as _invalidRootOperation, connectStorageEmulator, deleteObject, getDownloadURL, getMetadata, getStorage, list, listAll, ref, updateMetadata, uploadBytes, uploadBytesResumable, uploadString };
3520
+ export { StringFormat, FbsBlob as _FbsBlob, Location as _Location, TaskEvent as _TaskEvent, TaskState as _TaskState, UploadTask as _UploadTask, dataFromString as _dataFromString, _getChild, invalidArgument as _invalidArgument, invalidRootOperation as _invalidRootOperation, connectStorageEmulator, deleteObject, getBlob, getBytes, getDownloadURL, getMetadata, getStorage, getStream, list, listAll, ref, updateMetadata, uploadBytes, uploadBytesResumable, uploadString };
3370
3521
  //# sourceMappingURL=index.esm2017.js.map