-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
executable file
·70 lines (58 loc) · 1.96 KB
/
main.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
#!/usr/bin/env node
'use strict';
// Loading modules
var fs = require("fs");
var _ = require("underscore");
var _s = require('underscore.string');
var request = require("request");
var moment = require("moment");
var commander = require("commander");
commander
.option('-s, --site <site>', 'only fire requests that are for this domain (ignore everything else)')
.option('-f, --file <file>', 'HAR file to replay')
.parse(process.argv);
// Check arguments (file is required)
if (commander.file == undefined) {
commander.outputHelp();
process.exit();
}
// Do the Bartman! :)
fs.readFile(commander.file, function(err, data) {
if (err) throw err;
var har = JSON.parse(data);
var basetime = moment(har['log']['entries'][0]['startedDateTime']);
// Passing in an object
_.forEach(har['log']['entries'], function(entry) {
// Ignore other sites if so requested
if (commander.site != undefined && entry.request.url.substring(7, 7 + commander.site.length) != commander.site)
return;
// How late did this request happen?
var diff = moment(entry['startedDateTime']).diff(basetime, 'miliseconds');
// Send a request into the future
_.delay(function() {
// New request
var req = request({
url: entry.request.url,
method: entry.request.method,
// reformat headers from HAR format to a dict
headers: _.reduce(entry.request.headers, function(memo, e) {
if(e['name'][0] != ':')
memo[e['name']] = e['value'];
return memo;
}, {})
}, function(error, response, body) {
// Just print a status, drop the files as soon as possible
if(response) {
console.log(entry.request.url + " => " + response.statusCode);
} else {
// Likely failed because it was a websocket
console.log(entry.request.url + " => 0");
}
});
// Garbage collect, if we can (if started with --expose-gc)
if (global.gc) {
global.gc();
}
}, diff);
});
});