-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecorder.js
More file actions
39 lines (32 loc) · 995 Bytes
/
recorder.js
File metadata and controls
39 lines (32 loc) · 995 Bytes
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
const { Storage } = require('@google-cloud/storage');
const crypto = require('crypto');
const { recorder } = require('./config');
// Creates a client
const storage = new Storage();
const RecordRequest = async record => {
// saves recorded request into a file on Google Cloud
// file name is generated based on the json expression from config
const filename = recorder.filename_expression.evaluate(record);
return new Promise((resolve, reject) => {
const gcsFile = storage
.bucket(recorder.bucket_name)
.file(filename)
.createWriteStream({
contentType: 'application/json'
});
gcsFile.end(JSON.stringify(record, null, 2));
gcsFile.on('finish', () =>
resolve(
Object.assign({}, record, {
saved_at: new Date().getTime(),
saved_path: filename,
saved_bucket: recorder.bucket_name
})
)
);
gcsFile.on('error', err => reject(err));
});
};
module.exports = {
RecordRequest
};