UNPKG

2.39 kBJavaScriptView Raw
1const QueryString = require('querystring');
2const Https = require('https');
3const Fs = require('fs');
4const Tmp = require('tmp');
5const FormData = require('form-data')
6
7const Files = require('./file.js')
8
9
10module.exports = {
11 /*
12 description : function to create the form for submission
13 requires : token -> str,
14 raw_data -> JSON
15 returns : FormData.FormData
16 */
17 createForm: function(token, raw_data){
18 var tmp_obj = Tmp.fileSync();
19 Files.saveResult(tmp_obj.name, raw_data);
20 var form = new FormData();
21 form.append('token', token);
22 form.append('my_buffer', new Buffer(10));
23 form.append('fileformat', 'json');
24 form.append('reportfile', Fs.createReadStream(tmp_obj.name));
25
26 return form;
27 },
28
29
30 /*
31 URLSTR := newType('URLSTR', str)
32 description : function to create the url used for communication
33 requires : host -> str,
34 port -> int
35 returns : URLSTR
36 */
37 createUrl: function(host, port){
38 const end = '/r1_report_upload_endpoint/';
39 var start = (port == 443) ? 'https://' : 'http://';
40 var mid = (host == null) ? 'reshift.softwaresecured.com' : host;
41 return start + mid + ":" + port + end;
42 },
43
44
45 /*
46 description : function to transport to reshift
47 requires : token -> str,
48 raw_data -> JSON,
49 host -> str,
50 port -> int
51 returns : None
52 */
53 sendResult: function(token, raw_data, host, port) {
54 // Build the post string from an object
55 var form = this.createForm(token, raw_data);
56 var reshift_url = this.createUrl(host, port);
57
58 form.submit(reshift_url, function(error, response, body){
59 if (!error && response.statusCode == 200) {
60 console.log("INFO - Successfully upload the report.");
61 } else {
62 let status = (response == null) ? null : response.statusCode
63 let msg = (response == null) ? null : response.statusMessage
64 console.log("INFO - Failed to upload the report, status: " + status);
65 console.log("INFO - Return message: " + msg);
66 }
67 });
68 }
69};