UNPKG

5.37 kBJavaScriptView Raw
1/*! firebase-admin v10.0.0 */
2"use strict";
3/*!
4 * @license
5 * Copyright 2017 Google Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19Object.defineProperty(exports, "__esModule", { value: true });
20exports.Storage = void 0;
21var error_1 = require("../utils/error");
22var credential_internal_1 = require("../app/credential-internal");
23var utils = require("../utils/index");
24var validator = require("../utils/validator");
25/**
26 * The default `Storage` service if no
27 * app is provided or the `Storage` service associated with the provided
28 * app.
29 */
30var Storage = /** @class */ (function () {
31 /**
32 * @param app - The app for this Storage service.
33 * @constructor
34 * @internal
35 */
36 function Storage(app) {
37 if (!validator.isNonNullObject(app) || !('options' in app)) {
38 throw new error_1.FirebaseError({
39 code: 'storage/invalid-argument',
40 message: 'First argument passed to admin.storage() must be a valid Firebase app instance.',
41 });
42 }
43 if (!process.env.STORAGE_EMULATOR_HOST && process.env.FIREBASE_STORAGE_EMULATOR_HOST) {
44 var firebaseStorageEmulatorHost = process.env.FIREBASE_STORAGE_EMULATOR_HOST;
45 if (firebaseStorageEmulatorHost.match(/https?:\/\//)) {
46 throw new error_1.FirebaseError({
47 code: 'storage/invalid-emulator-host',
48 message: 'FIREBASE_STORAGE_EMULATOR_HOST should not contain a protocol (http or https).',
49 });
50 }
51 process.env.STORAGE_EMULATOR_HOST = "http://" + process.env.FIREBASE_STORAGE_EMULATOR_HOST;
52 }
53 var storage;
54 try {
55 storage = require('@google-cloud/storage').Storage;
56 }
57 catch (err) {
58 throw new error_1.FirebaseError({
59 code: 'storage/missing-dependencies',
60 message: 'Failed to import the Cloud Storage client library for Node.js. '
61 + 'Make sure to install the "@google-cloud/storage" npm package. '
62 + ("Original error: " + err),
63 });
64 }
65 var projectId = utils.getExplicitProjectId(app);
66 var credential = app.options.credential;
67 if (credential instanceof credential_internal_1.ServiceAccountCredential) {
68 this.storageClient = new storage({
69 // When the SDK is initialized with ServiceAccountCredentials an explicit projectId is
70 // guaranteed to be available.
71 projectId: projectId,
72 credentials: {
73 private_key: credential.privateKey,
74 client_email: credential.clientEmail,
75 },
76 });
77 }
78 else if (credential_internal_1.isApplicationDefault(app.options.credential)) {
79 // Try to use the Google application default credentials.
80 this.storageClient = new storage();
81 }
82 else {
83 throw new error_1.FirebaseError({
84 code: 'storage/invalid-credential',
85 message: 'Failed to initialize Google Cloud Storage client with the available credential. ' +
86 'Must initialize the SDK with a certificate credential or application default credentials ' +
87 'to use Cloud Storage API.',
88 });
89 }
90 this.appInternal = app;
91 }
92 /**
93 * Gets a reference to a Cloud Storage bucket.
94 *
95 * @param name - Optional name of the bucket to be retrieved. If name is not specified,
96 * retrieves a reference to the default bucket.
97 * @returns A {@link https://cloud.google.com/nodejs/docs/reference/storage/latest/Bucket | Bucket}
98 * instance as defined in the `@google-cloud/storage` package.
99 */
100 Storage.prototype.bucket = function (name) {
101 var bucketName = (typeof name !== 'undefined')
102 ? name : this.appInternal.options.storageBucket;
103 if (validator.isNonEmptyString(bucketName)) {
104 return this.storageClient.bucket(bucketName);
105 }
106 throw new error_1.FirebaseError({
107 code: 'storage/invalid-argument',
108 message: 'Bucket name not specified or invalid. Specify a valid bucket name via the ' +
109 'storageBucket option when initializing the app, or specify the bucket name ' +
110 'explicitly when calling the getBucket() method.',
111 });
112 };
113 Object.defineProperty(Storage.prototype, "app", {
114 /**
115 * Optional app whose `Storage` service to
116 * return. If not provided, the default `Storage` service will be returned.
117 */
118 get: function () {
119 return this.appInternal;
120 },
121 enumerable: false,
122 configurable: true
123 });
124 return Storage;
125}());
126exports.Storage = Storage;