UNPKG

4.78 kBJavaScriptView Raw
1/*! firebase-admin v12.0.0 */
2"use strict";
3/*!
4 * @license
5 * Copyright 2022 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.toCloudEventProtoFormat = exports.FirebaseEventarcError = void 0;
21const error_1 = require("../utils/error");
22const uuid_1 = require("uuid");
23const validator = require("../utils/validator");
24// List of CloudEvent properties that are handled "by hand" and should be skipped by
25// automatic attribute copy.
26const TOP_LEVEL_CE_ATTRS = ['id', 'type', 'specversion', 'source', 'data', 'time', 'datacontenttype', 'subject'];
27/**
28 * Firebase Eventarc error code structure. This extends PrefixedFirebaseError.
29 *
30 * @param code - The error code.
31 * @param message - The error message.
32 * @constructor
33 */
34class FirebaseEventarcError extends error_1.PrefixedFirebaseError {
35 constructor(code, message) {
36 super('eventarc', code, message);
37 }
38}
39exports.FirebaseEventarcError = FirebaseEventarcError;
40function toCloudEventProtoFormat(ce) {
41 const source = ce.source ?? process.env.EVENTARC_CLOUD_EVENT_SOURCE;
42 if (typeof source === 'undefined' || !validator.isNonEmptyString(source)) {
43 throw new FirebaseEventarcError('invalid-argument', "CloudEvent 'source' is required.");
44 }
45 if (!validator.isNonEmptyString(ce.type)) {
46 throw new FirebaseEventarcError('invalid-argument', "CloudEvent 'type' is required.");
47 }
48 const out = {
49 '@type': 'type.googleapis.com/io.cloudevents.v1.CloudEvent',
50 'id': ce.id ?? (0, uuid_1.v4)(),
51 'type': ce.type,
52 'specVersion': ce.specversion ?? '1.0',
53 'source': source
54 };
55 if (typeof ce.time !== 'undefined') {
56 if (!validator.isISODateString(ce.time)) {
57 throw new FirebaseEventarcError('invalid-argument', "CloudEvent 'tyme' must be in ISO date format.");
58 }
59 setAttribute(out, 'time', {
60 'ceTimestamp': ce.time
61 });
62 }
63 else {
64 setAttribute(out, 'time', {
65 'ceTimestamp': new Date().toISOString()
66 });
67 }
68 if (typeof ce.datacontenttype !== 'undefined') {
69 if (!validator.isNonEmptyString(ce.datacontenttype)) {
70 throw new FirebaseEventarcError('invalid-argument', "CloudEvent 'datacontenttype' if specified must be non-empty string.");
71 }
72 setAttribute(out, 'datacontenttype', {
73 'ceString': ce.datacontenttype
74 });
75 }
76 if (ce.subject) {
77 if (!validator.isNonEmptyString(ce.subject)) {
78 throw new FirebaseEventarcError('invalid-argument', "CloudEvent 'subject' if specified must be non-empty string.");
79 }
80 setAttribute(out, 'subject', {
81 'ceString': ce.subject
82 });
83 }
84 if (typeof ce.data === 'undefined') {
85 throw new FirebaseEventarcError('invalid-argument', "CloudEvent 'data' is required.");
86 }
87 if (validator.isObject(ce.data)) {
88 out['textData'] = JSON.stringify(ce.data);
89 if (!ce.datacontenttype) {
90 setAttribute(out, 'datacontenttype', {
91 'ceString': 'application/json'
92 });
93 }
94 }
95 else if (validator.isNonEmptyString(ce.data)) {
96 out['textData'] = ce.data;
97 if (!ce.datacontenttype) {
98 setAttribute(out, 'datacontenttype', {
99 'ceString': 'text/plain'
100 });
101 }
102 }
103 else {
104 throw new FirebaseEventarcError('invalid-argument', `CloudEvent 'data' must be string or an object (which are converted to JSON), got '${typeof ce.data}'.`);
105 }
106 for (const attr in ce) {
107 if (TOP_LEVEL_CE_ATTRS.includes(attr)) {
108 continue;
109 }
110 if (!validator.isNonEmptyString(ce[attr])) {
111 throw new FirebaseEventarcError('invalid-argument', `CloudEvent extension attributes ('${attr}') must be string.`);
112 }
113 setAttribute(out, attr, {
114 'ceString': ce[attr]
115 });
116 }
117 return out;
118}
119exports.toCloudEventProtoFormat = toCloudEventProtoFormat;
120function setAttribute(event, attr, value) {
121 if (!Object.prototype.hasOwnProperty.call(event, 'attributes')) {
122 event.attributes = {};
123 }
124 event['attributes'][attr] = value;
125}