-
Notifications
You must be signed in to change notification settings - Fork 0
/
_parse_har.js
212 lines (180 loc) · 5.95 KB
/
_parse_har.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
module.exports = function() {
var fs = require('fs'),
_ = require('underscore')._,
// readline = require('readline'),
Work = {},
/**
* Not sure about this options but should work for now
* @type {Object}
*/
fileOptions = {
'-o': '-o',
'--output': '--output',
'-f': '-f',
'--file': '--file'
},
data, entries, fileOutputName, harFile;
/**
* Function to read params and store file's names
*/
Work.init = function() {
/**
* Input args like: --o or --f
*/
var args = process.argv;
/**
* splice because first params are: nodejs and index.js
*/
args.splice(2).forEach(function(val, index, array) {
var opt = val.split('='),
key = opt[0];
if (!key && !fileOptions[key.toLowerCase()]) {
return;
}
if (!opt[1]) {
return;
}
switch (key) {
case '-o':
case '--output':
fileOutputName = opt[1];
break;
case '-f':
case '--file':
harFile = opt[1];
}
});
// var line = readline.createInterface(process.stdin, process.stdout);
// line.setPrompt('guess > Name of the har file with/without .har extension: ');
// line.prompt();
// line.on('line', function(input) {
// if (!input.split('.')[1]) {
// input += '.har';
// }
// if (Work.checkFile(input)) {
// line.close();
// }
// line.prompt();
// }).on('close', function() {
// process.exit(0);
// });
fs.readFile(harFile, function(err, data) {
if (err) {
return console.log(err);
}
result = JSON.parse(data);
entries = result.log.entries;
/**
* Call a function to work with current file content
*/
Work.parseHar(entries);
});
};
/**
* Check if file name exist
* @param {String} fileName .har file
* @return {Boolean} true if file exist
*/
Work.checkFile = function(fileName) {
fs.exists(fileName, function(exist) {
if (!exist) {
console.log('File with name: ' + fileName + ' doesnt exist \n');
}
return exist;
});
};
/**
* Store only request with contentType = json,
* @param {Object} entries Array of objects(request and response object)
*/
Work.parseHar = function(entries) {
var opts = [],
types = {
'application/json': 'application/json'
};
entries.forEach(function(entry) {
var responseType = entry.response.content.mimeType;
if (entry.request.method == 'OPTIONS') {
return;
}
return types[responseType] ? opts.push(entry) : '';
});
Work.cleanHar(opts);
};
/**
* From the request we need:
* method request.method
* url request.url
* queryString request.queryString
*
* From the response we need:
* status response.status
* json response.content.text
*/
Work.cleanHar = function(entries) {
var result = [],
store = {},
csv = [];
entries.forEach(function(entry) {
var query = entry.request.postData,
url = entry.request.url,
object, pairs, first;
url = url.split('?');
var tmpUrl = url[0];
if (url[1]) {
url[1].split('&').map(function(i) {
var key = i.split('=')[0];
if (key && key == 'base') {
tmpUrl = i.split('=')[1];
}
});
}
if (query) {
query = JSON.stringify(query.text);
} else {
var tmpQuery = [];
entry.request.queryString.forEach(function(q) {
var values = _.values(q);
if (values[0] != 'apiKey' && values[0] != 'hash' && values[0] != 'time') {
return tmpQuery.push(_.values(q).join('='));
}
});
query = tmpQuery.join(',');
}
// if(store[url + query]){
// return;
// }
// store[url + query ] = url + query;
object = {
api_call: tmpUrl,
query: query,
method: entry.request.method,
destination: entry.request.url,
status: entry.response.status,
response: entry.response.content.text || ''
};
try {
object.response = JSON.parse(object.response);
pairs = _.pairs(object.response);
first = pairs[0];
if (_.isArray(first[1])) {
object.response = JSON.stringify(first[1][0]);
} else {
object.response = JSON.stringify(object.response);
}
} catch (e) {
object.response = JSON.stringify(object.response);
console.log('Cannot parse the response because: ', e);
console.log("\nAdding as response: ", object.response);
}
csv.push(_.values(object).join('~') + '\n');
});
/**
* Write a 'csv' to file or default result.csv
*/
fs.writeFile(fileOutputName || 'result.csv', csv, function() {
console.log("\nEend of process information...");
});
};
return Work;
};