1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | exports.toCloudEventProtoFormat = exports.FirebaseEventarcError = void 0;
|
21 | const error_1 = require("../utils/error");
|
22 | const uuid_1 = require("uuid");
|
23 | const validator = require("../utils/validator");
|
24 |
|
25 |
|
26 | const TOP_LEVEL_CE_ATTRS = ['id', 'type', 'specversion', 'source', 'data', 'time', 'datacontenttype', 'subject'];
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | class FirebaseEventarcError extends error_1.PrefixedFirebaseError {
|
35 | constructor(code, message) {
|
36 | super('eventarc', code, message);
|
37 | }
|
38 | }
|
39 | exports.FirebaseEventarcError = FirebaseEventarcError;
|
40 | function 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 | }
|
119 | exports.toCloudEventProtoFormat = toCloudEventProtoFormat;
|
120 | function setAttribute(event, attr, value) {
|
121 | if (!Object.prototype.hasOwnProperty.call(event, 'attributes')) {
|
122 | event.attributes = {};
|
123 | }
|
124 | event['attributes'][attr] = value;
|
125 | }
|