Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug and add option #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/istanbul-proxy
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ program
.option('-r, --reportDir [path]', 'The directory in which to write HTML reporting files.')
.option('-t, --reportingTimeout [millis]', 'How long after window.onload the coverage report should be reported to the server. If set to 0, coverage will not be reported. Your pages must then call istanbulProxy.sendReport() when finished.')
.option('-n, --passThroughUrls [urls]', 'URLs that should not be instrumented')
.option('-i, --includeRegex [pattern]', 'Pattern that will shoul be instrumented')
.parse(process.argv);

require('../index')({
Expand All @@ -15,5 +16,6 @@ require('../index')({
reportingTimeout : program.reportingTimeout,
passThroughUrls : typeof program.passThroughUrls === 'string' ?
program.passThroughUrls.split(/\s+/g) :
program.passThroughUrls
program.passThroughUrls,
includeRegex: typeof program.includeRegex === 'string' ? program.includeRegex: "."
});
3 changes: 3 additions & 0 deletions bin/istanbul-proxy-daemon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/sh
istanbul-proxy $* > /dev/null 2>&1 &
exit 0
54 changes: 42 additions & 12 deletions lib/istanbul-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function proxy(req, res, getProxyResponseTransform) {
proxy_req.write(chunk, 'binary');
});
req.on('end', function() {
proxy_req.end();
proxy_req.end();
});
}

Expand All @@ -108,20 +108,27 @@ function instrumentationResponse(options, req, res) {

proxy_res.on('end', function() {
if (needsInstrumentation) {
res.write(instrument(responseStr, req.url, options));
var instrumented = instrument(responseStr, req.url, options);
proxy_res.headers['content-length'] = instrumented.length;
res.writeHead(proxy_res.statusCode, proxy_res.headers);
res.write(instrumented,'binary');
}
if (needsReport) {
res.writeHead(proxy_res.statusCode, proxy_res.headers);
res.write(options.reportingTimeout && insertReporter(responseStr, options.reportingTimeout));
} else {
res.writeHead(proxy_res.statusCode, proxy_res.headers);
}
res.end();
});
res.writeHead(proxy_res.statusCode, proxy_res.headers);
};
}

function instrument(source, sourceUrl, options) {
// Generate a valid filepath from the url
var filepath = url2path.url2pathRelative(sourceUrl);
//extract onth path from url
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var result = parse_url.exec(sourceUrl);
var filepath = result[5];
options.sourceStore.set(filepath, source);
return options.instrumenter.instrumentSync(source, filepath);
}
Expand All @@ -132,7 +139,9 @@ function proxyThroughInstrumentation(req, res, options) {

function cleanResponse(req, res) {
return function (proxy_res) {
proxy_res.on('data', function(chunk) { res.write(chunk, 'binary'); });
proxy_res.on('data', function(chunk) {
res.write(chunk, 'binary');
});
proxy_res.on('end', res.end.bind(res));
res.writeHead(proxy_res.statusCode, proxy_res.headers);
};
Expand All @@ -152,6 +161,7 @@ module.exports = function(options) {
var reportingTimeout = options.reportingTimeout = options.reportingTimeout === undefined ?
1000 :
parseInt(options.reportingTimeout || 0, 10);
var includeRegex = typeof options.includeRegex==="string" ? new RegExp(options.includeRegex,"g") : /./

// These urls are used by the istanbul reporter, and shouldn't be instrumented.
var passThroughUrls = options.passThroughUrls = [
Expand Down Expand Up @@ -180,6 +190,11 @@ module.exports = function(options) {
sourceStore: sourceStore,
verbose: false
}).writeReport(collector, !!'sync');
Report.create('cobertura', {
dir : reportDir,
sourceStore: sourceStore,
verbose: false
}).writeReport(collector, !!'sync');
}
isDirty = false;
cb && cb();
Expand Down Expand Up @@ -221,10 +236,10 @@ module.exports = function(options) {
req.on('data', function(chunk) { reqBody += chunk; });
req.on('end', function() {
var json;
try {
try {
json = JSON.parse(reqBody);
if (json) {
reportCoverage(json.coverage, json.url);
reportCoverage(json.coverage || {}, json.url);
}
} catch (e) {
return handleUserError(res, e);
Expand All @@ -237,17 +252,23 @@ module.exports = function(options) {
}
}


http.createServer(function(req, res) {
console.log(req.url);
if (~passThroughUrls.indexOf(req.url)) {
return proxy(req, res);
}

var hostAndPort = req.headers['host'].split(':');
var parsedPort = parseInt(hostAndPort[1] || 0, 10);
var isMyPort = parsedPort === port || (port === 80 && !parsedPort);

if (!isMyPort) {
return handleKnownHostRequest(false, req, res);
if (!req.url.match(includeRegex) && !req.url.match(/istanbul$/)) {
return proxy(req, res);
} else {
console.log(req.url);
return handleKnownHostRequest(false, req, res);
}
}
isMyHostname(hostAndPort[0], function(err, isMyHostname) {
if (err) {
Expand All @@ -256,13 +277,22 @@ module.exports = function(options) {
res.end();
return;
}
console.log(req.url);
handleKnownHostRequest(isMyHostname, req, res);
});
}).listen(port, function() {
console.log('Proxy server running on port ' + port);
})

process.on('SIGINT', function() {
writeReport(function() {
console.log("Report writed to exit.");
process.exit(0);
});
});

console.log('HTML reporting files will be stored in ' + reportDir);
if (!reportingTimeout) {
console.log('Coverage is not being automatically reported to the server. Call istanbulProxy.sendReport() manually.');
}
}
};