/
storage.ts
643 lines (592 loc) · 22.5 KB
/
storage.ts
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/**
* Cloud functions to handle events from Google Cloud Storage.
* @packageDocumentation
*/
import { firebaseConfig } from "../../common/config";
import { copyIfPresent } from "../../common/encoding";
import { ResetValue } from "../../common/options";
import { initV2Endpoint, ManifestEndpoint } from "../../runtime/manifest";
import { CloudEvent, CloudFunction } from "../core";
import { wrapTraceContext } from "../trace";
import { Expression } from "../../params";
import * as options from "../options";
import { SecretParam } from "../../params/types";
/**
* An object within Google Cloud Storage.
* Ref: https://github.com/googleapis/google-cloudevents-nodejs/blob/main/cloud/storage/v1/StorageObjectData.ts
*/
export interface StorageObjectData {
/**
* The name of the bucket containing this object.
*/
bucket: string;
/**
* Cache-Control directive for the object data, matching
* [https://tools.ietf.org/html/rfc7234#section-5.2"][RFC 7234 §5.2].
*/
cacheControl?: string;
/**
* Number of underlying components that make up this object. Components are
* accumulated by compose operations.
* Attempting to set this field will result in an error.
*/
componentCount?: number;
/**
* Content-Disposition of the object data, matching
* [https://tools.ietf.org/html/rfc6266][RFC 6266].
*/
contentDisposition?: string;
/**
* Content-Encoding of the object data, matching
* [https://tools.ietf.org/html/rfc7231#section-3.1.2.2][RFC 7231 §3.1.2.2]
*/
contentEncoding?: string;
/**
* Content-Language of the object data, matching
* [https://tools.ietf.org/html/rfc7231#section-3.1.3.2][RFC 7231 §3.1.3.2].
*/
contentLanguage?: string;
/**
* Content-Type of the object data, matching
* [https://tools.ietf.org/html/rfc7231#section-3.1.1.5][RFC 7231 §3.1.1.5].
* If an object is stored without a Content-Type, it is served as
* `application/octet-stream`.
*/
contentType?: string;
/**
* CRC32c checksum. For more information about using the CRC32c
* checksum, see
* [https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI][Hashes and
* ETags: Best Practices].
*/
crc32c?: string;
/**
* Metadata of customer-supplied encryption key, if the object is encrypted by
* such a key.
*/
customerEncryption?: CustomerEncryption;
/**
* HTTP 1.1 Entity tag for the object. See
* [https://tools.ietf.org/html/rfc7232#section-2.3][RFC 7232 §2.3].
*/
etag?: string;
/**
* The content generation of this object. Used for object versioning.
* Attempting to set this field will result in an error.
*/
generation: number;
/**
* The ID of the object, including the bucket name, object name, and
* generation number.
*/
id: string;
/**
* The kind of item this is. For objects, this is always "storage#object".
*/
kind?: string;
/**
* MD5 hash of the data; encoded using base64 as per
* [https://tools.ietf.org/html/rfc4648#section-4][RFC 4648 §4]. For more
* information about using the MD5 hash, see
* [https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI][Hashes and
* ETags: Best Practices].
*/
md5Hash?: string;
/**
* Media download link.
*/
mediaLink?: string;
/**
* User-provided metadata, in key/value pairs.
*/
metadata?: { [key: string]: string };
/**
* The version of the metadata for this object at this generation. Used for
* preconditions and for detecting changes in metadata. A metageneration
* number is only meaningful in the context of a particular generation of a
* particular object.
*/
metageneration: number;
/**
* The name of the object.
*/
name: string;
/**
* The link to this object.
*/
selfLink?: string;
/**
* Content-Length of the object data in bytes, matching
* [https://tools.ietf.org/html/rfc7230#section-3.3.2][RFC 7230 §3.3.2].
*/
size: number;
/**
* Storage class of the object.
*/
storageClass: string;
/**
* The creation time of the object.
* Attempting to set this field will result in an error.
*/
timeCreated?: Date | string;
/**
* The deletion time of the object. Will be returned if and only if this
* version of the object has been deleted.
*/
timeDeleted?: Date | string;
/**
* The time at which the object's storage class was last changed.
*/
timeStorageClassUpdated?: Date | string;
/**
* The modification time of the object metadata.
*/
updated?: Date | string;
}
/**
* Metadata of customer-supplied encryption key, if the object is encrypted by
* such a key.
*/
export interface CustomerEncryption {
/**
* The encryption algorithm.
*/
encryptionAlgorithm?: string;
/**
* SHA256 hash value of the encryption key.
*/
keySha256?: string;
}
/** A CloudEvent that contains StorageObjectData */
export interface StorageEvent extends CloudEvent<StorageObjectData> {
/** The name of the bucket containing this object. */
bucket: string;
}
/** @internal */
export const archivedEvent = "google.cloud.storage.object.v1.archived";
/** @internal */
export const finalizedEvent = "google.cloud.storage.object.v1.finalized";
/** @internal */
export const deletedEvent = "google.cloud.storage.object.v1.deleted";
/** @internal */
export const metadataUpdatedEvent = "google.cloud.storage.object.v1.metadataUpdated";
/** StorageOptions extend EventHandlerOptions with a bucket name */
export interface StorageOptions extends options.EventHandlerOptions {
/** The name of the bucket containing this object. */
bucket?: string;
/**
* If true, do not deploy or emulate this function.
*/
omit?: boolean | Expression<boolean>;
/**
* Region where functions should be deployed.
*/
region?: options.SupportedRegion | string;
/**
* Amount of memory to allocate to a function.
*/
memory?: options.MemoryOption | Expression<number> | ResetValue;
/**
* Timeout for the function in seconds, possible values are 0 to 540.
* HTTPS functions can specify a higher timeout.
*
* @remarks
* The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
* function depends on the type of function: Event handling functions have a
* maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
* maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
* timeout of 1,800s (30 minutes)
*/
timeoutSeconds?: number | Expression<number> | ResetValue;
/**
* Min number of actual instances to be running at a given time.
*
* @remarks
* Instances will be billed for memory allocation and 10% of CPU allocation
* while idle.
*/
minInstances?: number | Expression<number> | ResetValue;
/**
* Max number of instances to be running in parallel.
*/
maxInstances?: number | Expression<number> | ResetValue;
/**
* Number of requests a function can serve at once.
*
* @remarks
* Can only be applied to functions running on Cloud Functions v2.
* A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
* Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
* The maximum value for concurrency is 1,000.
*/
concurrency?: number | Expression<number> | ResetValue;
/**
* Fractional number of CPUs to allocate to a function.
*
* @remarks
* Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
* This is different from the defaults when using the gcloud utility and is different from
* the fixed amount assigned in Google Cloud Functions generation 1.
* To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
* to the value "gcf_gen1"
*/
cpu?: number | "gcf_gen1";
/**
* Connect cloud function to specified VPC connector.
*/
vpcConnector?: string | ResetValue;
/**
* Egress settings for VPC connector.
*/
vpcConnectorEgressSettings?: options.VpcEgressSetting | ResetValue;
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
/**
* Ingress settings which control where this function can be called from.
*/
ingressSettings?: options.IngressSetting | ResetValue;
/**
* User labels to set on the function.
*/
labels?: Record<string, string>;
/*
* Secrets to bind to a function.
*/
secrets?: (string | SecretParam)[];
/** Whether failed executions should be delivered again. */
retry?: boolean | Expression<boolean> | ResetValue;
}
/**
* Event handler sent only when a bucket has enabled object versioning.
* This event indicates that the live version of an object has become an
* archived version, either because it was archived or because it was
* overwritten by the upload of an object of the same name.
*
* @param handler - Event handler which is run every time a Google Cloud Storage archival occurs.
*/
export function onObjectArchived(
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler sent only when a bucket has enabled object versioning.
* This event indicates that the live version of an object has become an
* archived version, either because it was archived or because it was
* overwritten by the upload of an object of the same name.
*
* @param bucket - The name of the bucket containing this object.
* @param handler - Event handler which is run every time a Google Cloud Storage archival occurs.
*/
export function onObjectArchived(
bucket: string,
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler sent only when a bucket has enabled object versioning.
* This event indicates that the live version of an object has become an
* archived version, either because it was archived or because it was
* overwritten by the upload of an object of the same name.
*
* @param opts - Options that can be set on an individual event-handling function.
* @param handler - Event handler which is run every time a Google Cloud Storage archival occurs.
*/
export function onObjectArchived(
opts: StorageOptions,
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler sent only when a bucket has enabled object versioning.
* This event indicates that the live version of an object has become an
* archived version, either because it was archived or because it was
* overwritten by the upload of an object of the same name.
*
* @param bucketOrOptsOrHandler - Options or string that may (or may not) define the bucket to be used.
* @param handler - Event handler which is run every time a Google Cloud Storage archival occurs.
*/
export function onObjectArchived(
bucketOrOptsOrHandler: string | StorageOptions | ((event: StorageEvent) => any | Promise<any>),
handler?: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent> {
return onOperation(archivedEvent, bucketOrOptsOrHandler, handler);
}
/**
* Event handler which fires every time a Google Cloud Storage object
* creation occurs.
*
* Sent when a new object (or a new generation of an existing object)
* is successfully created in the bucket. This includes copying or rewriting
* an existing object. A failed upload does not trigger this event.
*
* @param handler - Event handler which is run every time a Google Cloud Storage object creation occurs.
*/
export function onObjectFinalized(
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler which fires every time a Google Cloud Storage object
* creation occurs.
*
* Sent when a new object (or a new generation of an existing object)
* is successfully created in the bucket. This includes copying or rewriting
* an existing object. A failed upload does not trigger this event.
*
* @param bucket - The name of the bucket containing this object.
* @param handler - Event handler which is run every time a Google Cloud Storage object creation occurs.
*/
export function onObjectFinalized(
bucket: string,
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler which fires every time a Google Cloud Storage object
* creation occurs.
*
* Sent when a new object (or a new generation of an existing object)
* is successfully created in the bucket. This includes copying or rewriting
* an existing object. A failed upload does not trigger this event.
*
* @param opts - Options that can be set on an individual event-handling function.
* @param handler - Event handler which is run every time a Google Cloud Storage object creation occurs.
*/
export function onObjectFinalized(
opts: StorageOptions,
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler which fires every time a Google Cloud Storage object
* creation occurs.
*
* Sent when a new object (or a new generation of an existing object)
* is successfully created in the bucket. This includes copying or rewriting
* an existing object. A failed upload does not trigger this event.
*
* @param bucketOrOptsOrHandler - Options or string that may (or may not) define the bucket to be used.
* @param handler - Event handler which is run every time a Google Cloud Storage object creation occurs.
*/
export function onObjectFinalized(
bucketOrOptsOrHandler: string | StorageOptions | ((event: StorageEvent) => any | Promise<any>),
handler?: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent> {
return onOperation(finalizedEvent, bucketOrOptsOrHandler, handler);
}
/**
* Event handler which fires every time a Google Cloud Storage deletion occurs.
*
* Sent when an object has been permanently deleted. This includes objects
* that are overwritten or are deleted as part of the bucket's lifecycle
* configuration. For buckets with object versioning enabled, this is not
* sent when an object is archived, even if archival occurs
* via the `storage.objects.delete` method.
*
* @param handler - Event handler which is run every time a Google Cloud Storage object deletion occurs.
*/
export function onObjectDeleted(
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler which fires every time a Google Cloud Storage deletion occurs.
*
* Sent when an object has been permanently deleted. This includes objects
* that are overwritten or are deleted as part of the bucket's lifecycle
* configuration. For buckets with object versioning enabled, this is not
* sent when an object is archived, even if archival occurs
* via the `storage.objects.delete` method.
*
* @param bucket - The name of the bucket containing this object.
* @param handler - Event handler which is run every time a Google Cloud Storage object deletion occurs.
*/
export function onObjectDeleted(
bucket: string,
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler which fires every time a Google Cloud Storage deletion occurs.
*
* Sent when an object has been permanently deleted. This includes objects
* that are overwritten or are deleted as part of the bucket's lifecycle
* configuration. For buckets with object versioning enabled, this is not
* sent when an object is archived, even if archival occurs
* via the `storage.objects.delete` method.
*
* @param opts - Options that can be set on an individual event-handling function.
* @param handler - Event handler which is run every time a Google Cloud Storage object deletion occurs.
*/
export function onObjectDeleted(
opts: StorageOptions,
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler which fires every time a Google Cloud Storage deletion occurs.
*
* Sent when an object has been permanently deleted. This includes objects
* that are overwritten or are deleted as part of the bucket's lifecycle
* configuration. For buckets with object versioning enabled, this is not
* sent when an object is archived, even if archival occurs
* via the `storage.objects.delete` method.
*
* @param bucketOrOptsOrHandler - Options or string that may (or may not) define the bucket to be used.
* @param handler - Event handler which is run every time a Google Cloud Storage object deletion occurs.
*/
export function onObjectDeleted(
bucketOrOptsOrHandler: string | StorageOptions | ((event: StorageEvent) => any | Promise<any>),
handler?: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent> {
return onOperation(deletedEvent, bucketOrOptsOrHandler, handler);
}
/**
* Event handler which fires every time the metadata of an existing object
* changes.
*
* @param bucketOrOptsOrHandler - Options or string that may (or may not) define the bucket to be used.
* @param handler - Event handler which is run every time a Google Cloud Storage object metadata update occurs.
*/
export function onObjectMetadataUpdated(
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler which fires every time the metadata of an existing object
* changes.
*
* @param bucket - The name of the bucket containing this object.
* @param handler - Event handler which is run every time a Google Cloud Storage object metadata update occurs.
*/
export function onObjectMetadataUpdated(
bucket: string,
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler which fires every time the metadata of an existing object
* changes.
*
* @param opts - Options that can be set on an individual event-handling function.
* @param handler - Event handler which is run every time a Google Cloud Storage object metadata update occurs.
*/
export function onObjectMetadataUpdated(
opts: StorageOptions,
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent>;
/**
* Event handler which fires every time the metadata of an existing object
* changes.
*
* @param bucketOrOptsOrHandler - Options or string that may (or may not) define the bucket to be used.
* @param handler - Event handler which is run every time a Google Cloud Storage object metadata update occurs.
*/
export function onObjectMetadataUpdated(
bucketOrOptsOrHandler: string | StorageOptions | ((event: StorageEvent) => any | Promise<any>),
handler?: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent> {
return onOperation(metadataUpdatedEvent, bucketOrOptsOrHandler, handler);
}
/** @internal */
export function onOperation(
eventType: string,
bucketOrOptsOrHandler: string | StorageOptions | ((event: StorageEvent) => any | Promise<any>),
handler: (event: StorageEvent) => any | Promise<any>
): CloudFunction<StorageEvent> {
if (typeof bucketOrOptsOrHandler === "function") {
handler = bucketOrOptsOrHandler as (event: StorageEvent) => any | Promise<any>;
bucketOrOptsOrHandler = {};
}
const [opts, bucket] = getOptsAndBucket(bucketOrOptsOrHandler);
const func = (raw: CloudEvent<unknown>) => {
return wrapTraceContext(handler)(raw as StorageEvent);
};
func.run = handler;
Object.defineProperty(func, "__trigger", {
get: () => {
const baseOpts = options.optionsToTriggerAnnotations(options.getGlobalOptions());
const specificOpts = options.optionsToTriggerAnnotations(opts);
return {
platform: "gcfv2",
...baseOpts,
...specificOpts,
labels: {
...baseOpts?.labels,
...specificOpts?.labels,
},
eventTrigger: {
eventType,
resource: bucket, // TODO(colerogers): replace with 'bucket,' eventually
},
};
},
});
// TypeScript doesn't recognize defineProperty as adding a property and complains
// that __endpoint doesn't exist. We can either cast to any and lose all type safety
// or we can just assign a meaningless value before calling defineProperty.
func.__endpoint = {} as ManifestEndpoint;
// SDK may attempt to read FIREBASE_CONFIG env var to fetch the default bucket name.
// To prevent runtime errors when FIREBASE_CONFIG env var is missing, we use getters.
Object.defineProperty(func, "__endpoint", {
get: () => {
const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
const specificOpts = options.optionsToEndpoint(opts);
const endpoint: ManifestEndpoint = {
platform: "gcfv2",
...initV2Endpoint(options.getGlobalOptions(), opts),
...baseOpts,
...specificOpts,
labels: {
...baseOpts?.labels,
...specificOpts?.labels,
},
eventTrigger: {
eventType,
eventFilters: { bucket },
retry: false,
},
};
copyIfPresent(endpoint.eventTrigger, opts, "retry", "retry");
return endpoint;
},
});
return func;
}
/** @internal */
export function getOptsAndBucket(
bucketOrOpts: string | StorageOptions
): [options.EventHandlerOptions, string] {
let bucket: string;
let opts: options.EventHandlerOptions;
if (typeof bucketOrOpts === "string") {
bucket = bucketOrOpts;
opts = {};
} else {
bucket = bucketOrOpts.bucket || firebaseConfig()?.storageBucket;
opts = { ...bucketOrOpts };
delete (opts as any).bucket;
}
if (!bucket) {
throw new Error(
"Missing bucket name. If you are unit testing, please provide a bucket name" +
" by providing bucket name directly in the event handler or by setting process.env.FIREBASE_CONFIG."
);
}
if (!/^[a-z\d][a-z\d\\._-]{1,230}[a-z\d]$/.test(bucket)) {
throw new Error(`Invalid bucket name ${bucket}`);
}
return [opts, bucket];
}