UNPKG

6.64 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2017 Firebase
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.ObjectBuilder = exports.BucketBuilder = exports._objectWithOptions = exports._bucketWithOptions = exports.object = exports.bucket = exports.service = exports.provider = void 0;
25const config_1 = require("../../common/config");
26const cloud_functions_1 = require("../cloud-functions");
27/** @internal */
28exports.provider = "google.storage";
29/** @internal */
30exports.service = "storage.googleapis.com";
31/**
32 * Registers a Cloud Function scoped to a specific storage bucket.
33 *
34 * @param bucket Name of the bucket to which this Cloud Function is
35 * scoped.
36 *
37 * @returns Storage bucket builder interface.
38 */
39function bucket(bucket) {
40 return _bucketWithOptions({}, bucket);
41}
42exports.bucket = bucket;
43/**
44 * Registers a Cloud Function scoped to the default storage bucket for the
45 * project.
46 *
47 * @returns Storage object builder interface.
48 */
49function object() {
50 return _objectWithOptions({});
51}
52exports.object = object;
53/** @internal */
54function _bucketWithOptions(options, bucket) {
55 const resourceGetter = () => {
56 bucket = bucket || (0, config_1.firebaseConfig)().storageBucket;
57 if (!bucket) {
58 throw new Error("Missing bucket name. If you are unit testing, please provide a bucket name" +
59 " through `functions.storage.bucket(bucketName)`, or set process.env.FIREBASE_CONFIG.");
60 }
61 if (!/^[a-z\d][a-z\d\\._-]{1,230}[a-z\d]$/.test(bucket)) {
62 throw new Error(`Invalid bucket name ${bucket}`);
63 }
64 return `projects/_/buckets/${bucket}`;
65 };
66 return new BucketBuilder(resourceGetter, options);
67}
68exports._bucketWithOptions = _bucketWithOptions;
69/** @internal */
70function _objectWithOptions(options) {
71 return _bucketWithOptions(options).object();
72}
73exports._objectWithOptions = _objectWithOptions;
74/**
75 * The Google Cloud Storage bucket builder interface.
76 *
77 * Access via `functions.storage.bucket()`.
78 */
79class BucketBuilder {
80 /** @internal */
81 constructor(triggerResource, options) {
82 this.triggerResource = triggerResource;
83 this.options = options;
84 }
85 /**
86 * Event handler which fires every time a Google Cloud Storage change occurs.
87 *
88 * @returns Storage object builder interface scoped to the specified storage
89 * bucket.
90 */
91 object() {
92 return new ObjectBuilder(this.triggerResource, this.options);
93 }
94}
95exports.BucketBuilder = BucketBuilder;
96/**
97 * The Google Cloud Storage object builder interface.
98 *
99 * Access via `functions.storage.object()`.
100 */
101class ObjectBuilder {
102 /** @internal */
103 constructor(triggerResource, options) {
104 this.triggerResource = triggerResource;
105 this.options = options;
106 }
107 /**
108 * Event handler sent only when a bucket has enabled object versioning.
109 * This event indicates that the live version of an object has become an
110 * archived version, either because it was archived or because it was
111 * overwritten by the upload of an object of the same name.
112 *
113 * @param handler Event handler which is run every time a Google Cloud Storage
114 * archival occurs.
115 *
116 * @returns A function which you can export and deploy.
117 */
118 onArchive(handler) {
119 return this.onOperation(handler, "object.archive");
120 }
121 /**
122 * Event handler which fires every time a Google Cloud Storage deletion occurs.
123 *
124 * Sent when an object has been permanently deleted. This includes objects
125 * that are overwritten or are deleted as part of the bucket's lifecycle
126 * configuration. For buckets with object versioning enabled, this is not
127 * sent when an object is archived, even if archival occurs
128 * via the `storage.objects.delete` method.
129 *
130 * @param handler Event handler which is run every time a Google Cloud Storage
131 * deletion occurs.
132 *
133 * @returns A function which you can export and deploy.
134 */
135 onDelete(handler) {
136 return this.onOperation(handler, "object.delete");
137 }
138 /**
139 * Event handler which fires every time a Google Cloud Storage object
140 * creation occurs.
141 *
142 * Sent when a new object (or a new generation of an existing object)
143 * is successfully created in the bucket. This includes copying or rewriting
144 * an existing object. A failed upload does not trigger this event.
145 *
146 * @param handler Event handler which is run every time a Google Cloud Storage
147 * object creation occurs.
148 *
149 * @returns A function which you can export and deploy.
150 */
151 onFinalize(handler) {
152 return this.onOperation(handler, "object.finalize");
153 }
154 /**
155 * Event handler which fires every time the metadata of an existing object
156 * changes.
157 *
158 * @param handler Event handler which is run every time a Google Cloud Storage
159 * metadata update occurs.
160 *
161 * @returns A function which you can export and deploy.
162 */
163 onMetadataUpdate(handler) {
164 return this.onOperation(handler, "object.metadataUpdate");
165 }
166 /** @hidden */
167 onOperation(handler, eventType) {
168 return (0, cloud_functions_1.makeCloudFunction)({
169 handler,
170 provider: exports.provider,
171 service: exports.service,
172 eventType,
173 triggerResource: this.triggerResource,
174 options: this.options,
175 });
176 }
177}
178exports.ObjectBuilder = ObjectBuilder;