All files / src service.ts

87.93% Statements 51/58
83.33% Branches 10/12
75% Functions 12/16
87.5% Lines 49/56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158                                  2x 2x 2x 2x 2x     2x               2x     15x             15x             14x 14x 6x   8x 8x 8x     13x             2x   6x 4x     16x 9x       9x 9x 2x   7x               2x   11x 2x   9x 9x   1x     14x 8x     2x       2x 4x               2x       2x 4x               2x       2x 3x   2x         2x       13x             2x 3x 3x   2x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { FirebaseApp } from '@firebase/app-types';
import * as args from './implementation/args';
import { AuthWrapper } from './implementation/authwrapper';
import { Location } from './implementation/location';
import * as fbsPromiseImpl from './implementation/promise_external';
import * as RequestExports from './implementation/request';
import { Request } from './implementation/request';
import { XhrIoPool } from './implementation/xhriopool';
import { Reference } from './reference';
 
/**
 * A service that provides firebaseStorage.Reference instances.
 * @param opt_url gs:// url to a custom Storage Bucket
 *
 * @struct
 */
export class Service {
  authWrapper_: AuthWrapper;
  private app_: FirebaseApp;
  private bucket_: Location | null = null;
  private internals_: ServiceInternals;
 
  constructor(app: FirebaseApp, pool: XhrIoPool, url?: string) {
    function maker(authWrapper: AuthWrapper, loc: Location) {
      return new Reference(authWrapper, loc);
    }
    this.authWrapper_ = new AuthWrapper(
      app,
      maker,
      RequestExports.makeRequest,
      this,
      pool
    );
    this.app_ = app;
    if (url != null) {
      this.bucket_ = Location.makeFromBucketSpec(url);
    } else {
      const authWrapperBucket = this.authWrapper_.bucket();
      Eif (authWrapperBucket != null) {
        this.bucket_ = new Location(authWrapperBucket, '');
      }
    }
    this.internals_ = new ServiceInternals(this);
  }
 
  /**
   * Returns a firebaseStorage.Reference for the given path in the default
   * bucket.
   */
  ref(path?: string): Reference {
    function validator(path: string) {
      if (/^[A-Za-z]+:\/\//.test(path)) {
        throw 'Expected child path but got a URL, use refFromURL instead.';
      }
    }
    args.validate('ref', [args.stringSpec(validator, true)], arguments);
    Iif (this.bucket_ == null) {
      throw new Error('No Storage Bucket defined in Firebase Options.');
    }
 
    let ref = new Reference(this.authWrapper_, this.bucket_);
    if (path != null) {
      return ref.child(path);
    } else {
      return ref;
    }
  }
 
  /**
   * Returns a firebaseStorage.Reference object for the given absolute URL,
   * which must be a gs:// or http[s]:// URL.
   */
  refFromURL(url: string): Reference {
    function validator(p: string) {
      if (!/^[A-Za-z]+:\/\//.test(p)) {
        throw 'Expected full URL but got a child path, use ref instead.';
      }
      try {
        Location.makeFromUrl(p);
      } catch (e) {
        throw 'Expected valid full URL but got an invalid one.';
      }
    }
    args.validate('refFromURL', [args.stringSpec(validator, false)], arguments);
    return new Reference(this.authWrapper_, url);
  }
 
  get maxUploadRetryTime(): number {
    return this.authWrapper_.maxUploadRetryTime();
  }
 
  setMaxUploadRetryTime(time: number) {
    args.validate(
      'setMaxUploadRetryTime',
      [args.nonNegativeNumberSpec()],
      arguments
    );
    this.authWrapper_.setMaxUploadRetryTime(time);
  }
 
  get maxOperationRetryTime(): number {
    return this.authWrapper_.maxOperationRetryTime();
  }
 
  setMaxOperationRetryTime(time: number) {
    args.validate(
      'setMaxOperationRetryTime',
      [args.nonNegativeNumberSpec()],
      arguments
    );
    this.authWrapper_.setMaxOperationRetryTime(time);
  }
 
  get app(): FirebaseApp {
    return this.app_;
  }
 
  get INTERNAL(): ServiceInternals {
    return this.internals_;
  }
}
 
/**
 * @struct
 */
export class ServiceInternals {
  service_: Service;
 
  constructor(service: Service) {
    this.service_ = service;
  }
 
  /**
   * Called when the associated app is deleted.
   * @see {!fbs.AuthWrapper.prototype.deleteApp}
   */
  delete(): Promise<void> {
    this.service_.authWrapper_.deleteApp();
    return fbsPromiseImpl.resolve<void>(undefined);
  }
}