/
cloudFunctions.ts
277 lines (240 loc) · 7.8 KB
/
cloudFunctions.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
import * as uuid from "uuid";
import { EmulatorRegistry } from "../registry";
import { EmulatorInfo, Emulators } from "../types";
import { EmulatorLogger } from "../emulatorLogger";
import { CloudStorageObjectMetadata, toSerializedDate } from "./metadata";
import { Client } from "../../apiv2";
import { StorageObjectData } from "@google/events/cloud/storage/v1/StorageObjectData";
import { CloudEvent } from "../events/types";
type StorageCloudFunctionAction = "finalize" | "metadataUpdate" | "delete" | "archive";
const STORAGE_V2_ACTION_MAP: Record<StorageCloudFunctionAction, string> = {
finalize: "finalized",
metadataUpdate: "metadataUpdated",
delete: "deleted",
archive: "archived",
};
export class StorageCloudFunctions {
private logger = EmulatorLogger.forEmulator(Emulators.STORAGE);
private functionsEmulatorInfo?: EmulatorInfo;
private multicastOrigin = "";
private multicastPath = "";
private enabled = false;
private client?: Client;
constructor(private projectId: string) {
const functionsEmulator = EmulatorRegistry.get(Emulators.FUNCTIONS);
if (functionsEmulator) {
this.enabled = true;
this.functionsEmulatorInfo = functionsEmulator.getInfo();
this.multicastOrigin = `http://${EmulatorRegistry.getInfoHostString(
this.functionsEmulatorInfo
)}`;
this.multicastPath = `/functions/projects/${projectId}/trigger_multicast`;
this.client = new Client({ urlPrefix: this.multicastOrigin, auth: false });
}
}
public async dispatch(
action: StorageCloudFunctionAction,
object: CloudStorageObjectMetadata
): Promise<void> {
if (!this.enabled) {
return;
}
const errStatus: Array<number> = [];
let err: Error | undefined;
try {
/** Legacy Google Events */
const eventBody = this.createLegacyEventRequestBody(action, object);
const eventRes = await this.client!.post(this.multicastPath, eventBody);
if (eventRes.status !== 200) {
errStatus.push(eventRes.status);
}
/** Modern CloudEvents */
const cloudEventBody = this.createCloudEventRequestBody(action, object);
const cloudEventRes = await this.client!.post<CloudEvent<StorageObjectData>, any>(
this.multicastPath,
cloudEventBody,
{
headers: { "Content-Type": "application/cloudevents+json; charset=UTF-8" },
}
);
if (cloudEventRes.status !== 200) {
errStatus.push(cloudEventRes.status);
}
} catch (e) {
err = e as Error;
}
if (err || errStatus.length > 0) {
this.logger.logLabeled(
"WARN",
"functions",
`Firebase Storage function was not triggered due to emulation error. Please file a bug.`
);
}
}
/** Legacy Google Events type */
private createLegacyEventRequestBody(
action: StorageCloudFunctionAction,
objectMetadataPayload: ObjectMetadataPayload
) {
const timestamp = new Date();
return {
eventId: `${timestamp.getTime()}`,
timestamp: toSerializedDate(timestamp),
eventType: `google.storage.object.${action}`,
resource: {
service: "storage.googleapis.com",
name: `projects/_/buckets/${objectMetadataPayload.bucket}/objects/${objectMetadataPayload.name}`,
type: "storage#object",
}, // bucket
data: objectMetadataPayload,
};
}
/** Modern CloudEvents type */
private createCloudEventRequestBody(
action: StorageCloudFunctionAction,
objectMetadataPayload: ObjectMetadataPayload
): CloudEvent<StorageObjectData> {
const ceAction = STORAGE_V2_ACTION_MAP[action];
if (!ceAction) {
throw new Error("Action is not definied as a CloudEvents action");
}
const data = (objectMetadataPayload as unknown) as StorageObjectData;
let time = new Date().toISOString();
if (data.updated) {
time = typeof data.updated === "string" ? data.updated : data.updated.toISOString();
}
return {
specversion: "1",
id: uuid.v4(),
type: `google.cloud.storage.object.v1.${ceAction}`,
source: `//storage.googleapis.com/projects/_/buckets/${objectMetadataPayload.bucket}/objects/${objectMetadataPayload.name}`,
time,
data,
};
}
}
// From https://github.com/firebase/firebase-functions/blob/master/src/providers/storage.ts
export interface ObjectMetadataPayload {
/** The kind of the object, which is always `storage#object`. */
kind: string;
/**
* The ID of the object, including the bucket name, object name, and
* generation number.
*/
id: string;
/** Storage bucket that contains the object. */
bucket: string;
/** Storage class of the object. */
storageClass: string;
/**
* The value of the `Content-Length` header, used to determine the length of
* the object data in bytes.
*/
size: string;
/** The creation time of the object in RFC 3339 format. */
timeCreated: string;
/**
* The modification time of the object metadata in RFC 3339 format.
*/
updated: string;
/** Link to access the object, assuming you have sufficient permissions. */
selfLink?: string;
/** The object's name. */
name?: string;
/**
* Generation version number that changes each time the object is
* overwritten.
*/
generation?: string;
/** The object's content type, also known as the MIME type. */
contentType?: string;
/**
* Meta-generation version number that changes each time the object's metadata
* is updated.
*/
metageneration?: string;
/**
* The deletion time of the object in RFC 3339 format. Returned
* only if this version of the object has been deleted.
*/
timeDeleted?: string;
timeStorageClassUpdated?: string;
/**
* MD5 hash for the object. All Google Cloud Storage objects
* have a CRC32C hash or MD5 hash.
*/
md5Hash?: string;
/** Media download link. */
mediaLink?: string;
/**
* Content-Encoding to indicate that an object is compressed
* (for example, with gzip compression) while maintaining its Content-Type.
*/
contentEncoding?: string;
/**
* The value of the `Content-Disposition` header, used to specify presentation
* information about the data being transmitted.
*/
contentDisposition?: string;
/** ISO 639-1 language code of the content. */
contentLanguage?: string;
/**
* The value of the `Cache-Control` header, used to determine whether Internet
* caches are allowed to cache public data for an object.
*/
cacheControl?: string;
/** User-provided metadata. */
metadata?: {
[key: string]: string;
};
acl?: [
{
kind?: string;
id?: string;
selfLink?: string;
bucket?: string;
object?: string;
generation?: string;
entity?: string;
role?: string;
email?: string;
entityId?: string;
domain?: string;
projectTeam?: {
projectNumber?: string;
team?: string;
};
etag?: string;
}
];
owner?: {
entity?: string;
entityId?: string;
};
/**
* The object's CRC32C hash. All Google Cloud Storage objects
* have a CRC32C hash or MD5 hash.
*/
crc32c?: string;
/**
* Specifies the number of originally uploaded objects from which
* a composite object was created.
*/
componentCount?: string;
etag?: string;
/**
* Customer-supplied encryption key.
*
* This object contains the following properties:
* * `encryptionAlgorithm` (`string|undefined`): The encryption algorithm that
* was used. Always contains the value `AES256`.
* * `keySha256` (`string|undefined`): An RFC 4648 base64-encoded string of the
* SHA256 hash of your encryption key. You can use this SHA256 hash to
* uniquely identify the AES-256 encryption key required to decrypt the
* object, which you must store securely.
*/
customerEncryption?: {
encryptionAlgorithm?: string;
keySha256?: string;
};
}