constructor()

in src/storage/storage.ts [40:97]


  constructor(app: App) {
    if (!validator.isNonNullObject(app) || !('options' in app)) {
      throw new FirebaseError({
        code: 'storage/invalid-argument',
        message: 'First argument passed to admin.storage() must be a valid Firebase app instance.',
      });
    }

    if (!process.env.STORAGE_EMULATOR_HOST && process.env.FIREBASE_STORAGE_EMULATOR_HOST) {
      const firebaseStorageEmulatorHost = process.env.FIREBASE_STORAGE_EMULATOR_HOST;

      if (firebaseStorageEmulatorHost.match(/https?:\/\//)) {
        throw new FirebaseError({
          code: 'storage/invalid-emulator-host',
          message: 'FIREBASE_STORAGE_EMULATOR_HOST should not contain a protocol (http or https).',
        });
      }

      process.env.STORAGE_EMULATOR_HOST = `http://${process.env.FIREBASE_STORAGE_EMULATOR_HOST}`;
    }

    let storage: typeof StorageClient;
    try {
      storage = require('@google-cloud/storage').Storage;
    } catch (err) {
      throw new FirebaseError({
        code: 'storage/missing-dependencies',
        message: 'Failed to import the Cloud Storage client library for Node.js. '
          + 'Make sure to install the "@google-cloud/storage" npm package. '
          + `Original error: ${err}`,
      });
    }

    const projectId: string | null = utils.getExplicitProjectId(app);
    const credential = app.options.credential;
    if (credential instanceof ServiceAccountCredential) {
      this.storageClient = new storage({
        // When the SDK is initialized with ServiceAccountCredentials an explicit projectId is
        // guaranteed to be available.
        projectId: projectId!,
        credentials: {
          private_key: credential.privateKey,
          client_email: credential.clientEmail,
        },
      });
    } else if (isApplicationDefault(app.options.credential)) {
      // Try to use the Google application default credentials.
      this.storageClient = new storage();
    } else {
      throw new FirebaseError({
        code: 'storage/invalid-credential',
        message: 'Failed to initialize Google Cloud Storage client with the available credential. ' +
          'Must initialize the SDK with a certificate credential or application default credentials ' +
          'to use Cloud Storage API.',
      });
    }
    this.appInternal = app;
  }