-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathfunction.js
58 lines (49 loc) · 1.24 KB
/
function.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
/* eslint-disable n/no-missing-require */
const fs = require('fs');
const functions = require('@google-cloud/functions-framework');
const fileName = 'function_output.json';
functions.http('writeHttpDeclarative', (req, res) => {
writeJson(req.body);
res.sendStatus(200);
});
functions.typed('writeTypedDeclarative', req => {
return {
payload: req,
};
});
functions.cloudEvent('writeCloudEventDeclarative', cloudEvent => {
cloudEvent.datacontenttype = 'application/json';
writeJson(cloudEvent);
});
functions.http('concurrentHttp', (req, res) => {
setTimeout(() => res.send('done'), 1000);
});
function writeHttp(req, res) {
writeJson(req.body);
res.sendStatus(200);
}
function writeCloudEvent(cloudEvent) {
cloudEvent.datacontenttype = 'application/json';
writeJson(cloudEvent);
}
function writeLegacyEvent(data, context) {
const content = {
data: data,
context: {
eventId: context.eventId,
timestamp: context.timestamp,
eventType: context.eventType,
resource: context.resource,
},
};
writeJson(content);
}
function writeJson(content) {
const json = JSON.stringify(content);
fs.writeFileSync(fileName, json);
}
module.exports = {
writeHttp,
writeCloudEvent,
writeLegacyEvent,
};