forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcloud_event_to_background_event.ts
197 lines (187 loc) · 6.7 KB
/
cloud_event_to_background_event.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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Request, Response, NextFunction} from 'express';
import {
CE_SERVICE,
isBinaryCloudEvent,
getBinaryCloudEventContext,
EventConversionError,
} from '../cloud_events';
// Maps CloudEvent types to the equivalent GCF Event type
export const CE_TO_BACKGROUND_TYPE: {[k: string]: string} = {
'google.cloud.pubsub.topic.v1.messagePublished':
'google.pubsub.topic.publish',
'google.cloud.storage.object.v1.finalized': 'google.storage.object.finalize',
'google.cloud.storage.object.v1.deleted': 'google.storage.object.delete',
'google.cloud.storage.object.v1.archived': 'google.storage.object.archive',
'google.cloud.storage.object.v1.metadataUpdated':
'google.storage.object.metadataUpdate',
'google.cloud.firestore.document.v1.written':
'providers/cloud.firestore/eventTypes/document.write',
'google.cloud.firestore.document.v1.created':
'providers/cloud.firestore/eventTypes/document.create',
'google.cloud.firestore.document.v1.updated':
'providers/cloud.firestore/eventTypes/document.update',
'google.cloud.firestore.document.v1.deleted':
'providers/cloud.firestore/eventTypes/document.delete',
'google.firebase.auth.user.v1.created':
'providers/firebase.auth/eventTypes/user.create',
'google.firebase.auth.user.v1.deleted':
'providers/firebase.auth/eventTypes/user.delete',
'google.firebase.analytics.log.v1.written':
'providers/google.firebase.analytics/eventTypes/event.log',
'google.firebase.database.ref.v1.created':
'providers/google.firebase.database/eventTypes/ref.create',
'google.firebase.database.ref.v1.written':
'providers/google.firebase.database/eventTypes/ref.write',
'google.firebase.database.ref.v1.updated':
'providers/google.firebase.database/eventTypes/ref.update',
'google.firebase.database.ref.v1.deleted':
'providers/google.firebase.database/eventTypes/ref.delete',
};
const PUBSUB_MESSAGE_TYPE =
'type.googleapis.com/google.pubsub.v1.PubsubMessage';
/**
* Regex to split a CE source string into service and name components.
*/
const CE_SOURCE_REGEX = /\/\/([^/]+)\/(.+)/;
/**
* Is the given request a known CloudEvent that can be converted to a legacy event.
* @param request express request object
* @returns true if the request can be converted
*/
const isConvertableCloudEvent = (request: Request): boolean => {
if (isBinaryCloudEvent(request)) {
const ceType = request.header('ce-type');
return !!ceType && ceType in CE_TO_BACKGROUND_TYPE;
}
return false;
};
/**
* Splits a CloudEvent source string into resource and subject components.
* @param source the cloud event source
* @returns the parsed service and name components of the CE source string
*/
export const parseSource = (
source: string
): {service: string; name: string} => {
const match = source.match(CE_SOURCE_REGEX);
if (!match) {
throw new EventConversionError(
`Failed to convert CloudEvent with invalid source: "${source}"`
);
}
return {
service: match![1],
name: match![2],
};
};
/**
* Marshal a known GCP CloudEvent request the equivalent context/data legacy event format.
* @param req express request object
* @returns the request body of the equivalent legacy event request
*/
const marshalConvertableCloudEvent = (
req: Request
): {context: object; data: object} => {
const ceContext = getBinaryCloudEventContext(req);
const {service, name} = parseSource(ceContext.source!);
const subject = ceContext.subject!;
let data = req.body;
// The default resource is a string made up of the source name and subject.
let resource: string | {[key: string]: string} = `${name}/${subject}`;
switch (service) {
case CE_SERVICE.PUBSUB:
// PubSub resource format
resource = {
service: service,
name: name,
type: PUBSUB_MESSAGE_TYPE,
};
// If the data payload has a "message", it needs to be flattened
if ('message' in data) {
data = data.message;
}
if ('messageId' in data) {
delete data['messageId'];
}
if ('publishTime' in data) {
delete data['publishTime'];
}
break;
case CE_SERVICE.FIREBASE_AUTH:
// FirebaseAuth resource format
resource = name;
if ('metadata' in data) {
// Some metadata are not consistent between CloudEvents and legacy events
const metadata: object = data.metadata;
data.metadata = {};
// eslint-disable-next-line prefer-const
for (let [k, v] of Object.entries(metadata)) {
k = k === 'createTime' ? 'createdAt' : k;
k = k === 'lastSignInTime' ? 'lastSignedInAt' : k;
data.metadata[k] = v;
}
}
break;
case CE_SERVICE.FIREBASE_DB:
if (typeof resource === 'string') {
// Strip the location out of the resource string
resource = resource.replace(/\/locations\/[^/]+/, '');
}
break;
case CE_SERVICE.STORAGE:
// CloudStorage resource format
resource = {
name: `${name}/${subject}`,
service: service,
type: data.kind,
};
break;
}
return {
context: {
eventId: ceContext.id!,
timestamp: ceContext.time!,
eventType: CE_TO_BACKGROUND_TYPE[ceContext.type!],
resource,
},
data,
};
};
/**
* Express middleware to convert cloud event requests to legacy GCF events. This enables
* functions using the "EVENT" signature type to accept requests from a cloud event producer.
* @param req express request object
* @param res express response object
* @param next function used to pass control to the next middle middleware function in the stack
*/
export const cloudEventToBackgroundEventMiddleware = (
req: Request,
res: Response,
next: NextFunction
) => {
if (isConvertableCloudEvent(req)) {
// This is a CloudEvent that can be converted a known legacy event.
req.body = marshalConvertableCloudEvent(req);
} else if (isBinaryCloudEvent(req)) {
// Support CloudEvents in binary content mode, with data being the whole
// request body and context attributes retrieved from request headers.
req.body = {
context: getBinaryCloudEventContext(req),
data: req.body,
};
}
next();
};