/
index.js
337 lines (293 loc) · 12 KB
/
index.js
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
const admin = require("firebase-admin");
const functions = require("firebase-functions");
let functionsV2;
try {
functionsV2 = require("firebase-functions/v2");
} catch {
// TODO: firebase-functions/lib path is unsupported, but this is the only way to access the v2 namespace in Node 10.
// Remove this ugly hack once we cut support for Node 10.
functionsV2 = require("firebase-functions/lib/v2");
}
const { PubSub } = require("@google-cloud/pubsub");
/*
* Log snippets that the driver program above checks for. Be sure to update
* ../test.js if you plan on changing these.
*/
/* Functions V1 */
const RTDB_FUNCTION_LOG = "========== RTDB FUNCTION ==========";
const FIRESTORE_FUNCTION_LOG = "========== FIRESTORE FUNCTION ==========";
const PUBSUB_FUNCTION_LOG = "========== PUBSUB FUNCTION ==========";
const AUTH_FUNCTION_LOG = "========== AUTH FUNCTION ==========";
const STORAGE_FUNCTION_ARCHIVED_LOG = "========== STORAGE FUNCTION ARCHIVED ==========";
const STORAGE_FUNCTION_DELETED_LOG = "========== STORAGE FUNCTION DELETED ==========";
const STORAGE_FUNCTION_FINALIZED_LOG = "========== STORAGE FUNCTION FINALIZED ==========";
const STORAGE_FUNCTION_METADATA_LOG = "========== STORAGE FUNCTION METADATA ==========";
const STORAGE_BUCKET_FUNCTION_ARCHIVED_LOG =
"========== STORAGE BUCKET FUNCTION ARCHIVED ==========";
const STORAGE_BUCKET_FUNCTION_DELETED_LOG = "========== STORAGE BUCKET FUNCTION DELETED ==========";
const STORAGE_BUCKET_FUNCTION_FINALIZED_LOG =
"========== STORAGE BUCKET FUNCTION FINALIZED ==========";
const STORAGE_BUCKET_FUNCTION_METADATA_LOG =
"========== STORAGE BUCKET FUNCTION METADATA ==========";
/* Functions V2 */
const PUBSUB_FUNCTION_V2_LOG = "========== PUBSUB V2 FUNCTION ==========";
const STORAGE_FUNCTION_V2_ARCHIVED_LOG = "========== STORAGE V2 FUNCTION ARCHIVED ==========";
const STORAGE_FUNCTION_V2_DELETED_LOG = "========== STORAGE V2 FUNCTION DELETED ==========";
const STORAGE_FUNCTION_V2_FINALIZED_LOG = "========== STORAGE V2 FUNCTION FINALIZED ==========";
const STORAGE_FUNCTION_V2_METADATA_LOG = "========== STORAGE V2 FUNCTION METADATA ==========";
const STORAGE_BUCKET_FUNCTION_V2_ARCHIVED_LOG =
"========== STORAGE BUCKET V2 FUNCTION ARCHIVED ==========";
const STORAGE_BUCKET_FUNCTION_V2_DELETED_LOG =
"========== STORAGE BUCKET V2 FUNCTION DELETED ==========";
const STORAGE_BUCKET_FUNCTION_V2_FINALIZED_LOG =
"========== STORAGE BUCKET V2 FUNCTION FINALIZED ==========";
const STORAGE_BUCKET_FUNCTION_V2_METADATA_LOG =
"========== STORAGE BUCKET V2 FUNCTION METADATA ==========";
/*
* We install onWrite triggers for START_DOCUMENT_NAME in both the firestore and
* database emulators. From each respective onWrite trigger, we write a document
* to both the firestore and database emulators. This exercises the
* bidirectional communication between cloud functions and each emulator.
*/
const START_DOCUMENT_NAME = "test/start";
const END_DOCUMENT_NAME = "test/done";
const PUBSUB_TOPIC = "test-topic";
const PUBSUB_SCHEDULED_TOPIC = "firebase-schedule-pubsubScheduled";
const STORAGE_FILE_NAME = "test-file.txt";
const pubsub = new PubSub();
admin.initializeApp();
exports.deleteFromFirestore = functions.https.onRequest(async (req, res) => {
await admin.firestore().doc(START_DOCUMENT_NAME).delete();
res.json({ deleted: true });
});
exports.deleteFromRtdb = functions.https.onRequest(async (req, res) => {
await admin.database().ref(START_DOCUMENT_NAME).remove();
res.json({ deleted: true });
});
exports.writeToFirestore = functions.https.onRequest(async (req, res) => {
const ref = admin.firestore().doc(START_DOCUMENT_NAME);
await ref.set({ start: new Date().toISOString() });
ref.get().then((snap) => {
res.json({ data: snap.data() });
});
});
exports.writeToRtdb = functions.https.onRequest(async (req, res) => {
const ref = admin.database().ref(START_DOCUMENT_NAME);
await ref.set({ start: new Date().toISOString() });
ref.once("value", (snap) => {
res.json({ data: snap });
});
});
exports.writeToPubsub = functions.https.onRequest(async (req, res) => {
const msg = await pubsub.topic(PUBSUB_TOPIC).publishJSON({ foo: "bar" }, { attr: "val" });
console.log("PubSub Emulator Host", process.env.PUBSUB_EMULATOR_HOST);
console.log("Wrote PubSub Message", msg);
res.json({ published: "ok" });
});
exports.writeToScheduledPubsub = functions.https.onRequest(async (req, res) => {
const msg = await pubsub
.topic(PUBSUB_SCHEDULED_TOPIC)
.publishJSON({ foo: "bar" }, { attr: "val" });
console.log("PubSub Emulator Host", process.env.PUBSUB_EMULATOR_HOST);
console.log("Wrote Scheduled PubSub Message", msg);
res.json({ published: "ok" });
});
exports.writeToAuth = functions.https.onRequest(async (req, res) => {
const time = new Date().getTime();
await admin.auth().createUser({
uid: `uid${time}`,
email: `user${time}@example.com`,
});
res.json({ created: "ok" });
});
exports.writeToDefaultStorage = functions.https.onRequest(async (req, res) => {
await admin.storage().bucket().file(STORAGE_FILE_NAME).save("hello world!");
console.log("Wrote to default Storage bucket");
res.json({ created: "ok" });
});
exports.writeToSpecificStorageBucket = functions.https.onRequest(async (req, res) => {
await admin.storage().bucket("test-bucket").file(STORAGE_FILE_NAME).save("hello world!");
console.log("Wrote to a specific Storage bucket");
res.json({ created: "ok" });
});
exports.updateDeleteFromDefaultStorage = functions.https.onRequest(async (req, res) => {
await admin.storage().bucket().file(STORAGE_FILE_NAME).save("something new!");
console.log("Wrote to Storage bucket");
await admin.storage().bucket().file(STORAGE_FILE_NAME).delete();
console.log("Deleted from Storage bucket");
res.json({ done: "ok" });
});
exports.updateDeleteFromSpecificStorageBucket = functions.https.onRequest(async (req, res) => {
await admin.storage().bucket("test-bucket").file(STORAGE_FILE_NAME).save("something new!");
console.log("Wrote to a specific Storage bucket");
await admin.storage().bucket("test-bucket").file(STORAGE_FILE_NAME).delete();
console.log("Deleted from a specific Storage bucket");
res.json({ done: "ok" });
});
exports.firestoreReaction = functions.firestore
.document(START_DOCUMENT_NAME)
.onWrite(async (/* change, ctx */) => {
console.log(FIRESTORE_FUNCTION_LOG);
/*
* Write back a completion timestamp to the firestore emulator. The test
* driver program checks for this by querying the firestore emulator
* directly.
*/
const ref = admin.firestore().doc(END_DOCUMENT_NAME + "_from_firestore");
await ref.set({ done: new Date().toISOString() });
/*
* Write a completion marker to the firestore emulator. This exercise
* cross-emulator communication.
*/
const dbref = admin.database().ref(END_DOCUMENT_NAME + "_from_firestore");
await dbref.set({ done: new Date().toISOString() });
return true;
});
exports.rtdbReaction = functions.database
.ref(START_DOCUMENT_NAME)
.onWrite(async (/* change, ctx */) => {
console.log(RTDB_FUNCTION_LOG);
const ref = admin.database().ref(END_DOCUMENT_NAME + "_from_database");
await ref.set({ done: new Date().toISOString() });
const firestoreref = admin.firestore().doc(END_DOCUMENT_NAME + "_from_database");
await firestoreref.set({ done: new Date().toISOString() });
return true;
});
exports.pubsubReaction = functions.pubsub.topic(PUBSUB_TOPIC).onPublish((msg /* , ctx */) => {
console.log(PUBSUB_FUNCTION_LOG);
console.log("Message", JSON.stringify(msg.json));
console.log("Attributes", JSON.stringify(msg.attributes));
return true;
});
exports.pubsubv2reaction = functionsV2.pubsub.onMessagePublished(PUBSUB_TOPIC, (cloudevent) => {
console.log(PUBSUB_FUNCTION_V2_LOG);
console.log("Message", JSON.stringify(cloudevent.data.message.json));
console.log("Attributes", JSON.stringify(cloudevent.data.message.attributes));
return true;
});
exports.pubsubScheduled = functions.pubsub.schedule("every mon 07:00").onRun((context) => {
console.log(PUBSUB_FUNCTION_LOG);
console.log("Resource", JSON.stringify(context.resource));
return true;
});
exports.authReaction = functions.auth.user().onCreate((user, ctx) => {
console.log(AUTH_FUNCTION_LOG);
console.log("User", JSON.stringify(user));
return true;
});
exports.storageArchiveReaction = functions.storage
.bucket()
.object()
.onArchive((object, context) => {
console.log(STORAGE_FUNCTION_ARCHIVED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageDeleteReaction = functions.storage
.bucket()
.object()
.onDelete((object, context) => {
console.log(STORAGE_FUNCTION_DELETED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageFinalizeReaction = functions.storage
.bucket()
.object()
.onFinalize((object, context) => {
console.log(STORAGE_FUNCTION_FINALIZED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageMetadataReaction = functions.storage
.bucket()
.object()
.onMetadataUpdate((object, context) => {
console.log(STORAGE_FUNCTION_METADATA_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storagev2archivedreaction = functionsV2.storage.onObjectArchived((cloudevent) => {
console.log(STORAGE_FUNCTION_V2_ARCHIVED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
});
exports.storagev2deletedreaction = functionsV2.storage.onObjectDeleted((cloudevent) => {
console.log(STORAGE_FUNCTION_V2_DELETED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
});
exports.storagev2finalizedreaction = functionsV2.storage.onObjectFinalized((cloudevent) => {
console.log(STORAGE_FUNCTION_V2_FINALIZED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
});
exports.storagev2metadatareaction = functionsV2.storage.onObjectMetadataUpdated((cloudevent) => {
console.log(STORAGE_FUNCTION_V2_METADATA_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
});
exports.storageBucketArchiveReaction = functions.storage
.bucket("test-bucket")
.object()
.onArchive((object, context) => {
console.log(STORAGE_BUCKET_FUNCTION_ARCHIVED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageBucketDeleteReaction = functions.storage
.bucket("test-bucket")
.object()
.onDelete((object, context) => {
console.log(STORAGE_BUCKET_FUNCTION_DELETED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageBucketFinalizeReaction = functions.storage
.bucket("test-bucket")
.object()
.onFinalize((object, context) => {
console.log(STORAGE_BUCKET_FUNCTION_FINALIZED_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storageBucketMetadataReaction = functions.storage
.bucket("test-bucket")
.object()
.onMetadataUpdate((object, context) => {
console.log(STORAGE_BUCKET_FUNCTION_METADATA_LOG);
console.log("Object", JSON.stringify(object));
return true;
});
exports.storagebucketv2archivedreaction = functionsV2.storage.onObjectArchived(
"test-bucket",
(cloudevent) => {
console.log(STORAGE_BUCKET_FUNCTION_V2_ARCHIVED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);
exports.storagebucketv2deletedreaction = functionsV2.storage.onObjectDeleted(
"test-bucket",
(cloudevent) => {
console.log(STORAGE_BUCKET_FUNCTION_V2_DELETED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);
exports.storagebucketv2finalizedreaction = functionsV2.storage.onObjectFinalized(
"test-bucket",
(cloudevent) => {
console.log(STORAGE_BUCKET_FUNCTION_V2_FINALIZED_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);
exports.storagebucketv2metadatareaction = functionsV2.storage.onObjectMetadataUpdated(
"test-bucket",
(cloudevent) => {
console.log(STORAGE_BUCKET_FUNCTION_V2_METADATA_LOG);
console.log("Object", JSON.stringify(cloudevent.data));
return true;
}
);