diff --git a/.gitignore b/.gitignore index 265f17f..e43b0f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -DS_Store +.DS_Store diff --git a/lib/exceptional.js b/lib/exceptional.js index f9046e0..c27c484 100644 --- a/lib/exceptional.js +++ b/lib/exceptional.js @@ -1,5 +1,5 @@ var HTTP = require('http'); -var gzip = require('./gzip').gzip; +var zlib = require('zlib'); var fs = require('fs'); var Exceptional = { @@ -57,27 +57,57 @@ var Exceptional = { }, send_error: function(doc) { - gzip(doc, 1, function(err, data) { - - var client = HTTP.createClient(Exceptional.Port, Exceptional.Host); + if(!Buffer.isBuffer(doc)) + doc = new Buffer(doc); + + zlib.gzip(doc, function (err, data) { + var path = '/api/errors?api_key=' + Exceptional.API_KEY + "&protocol_version=" + Exceptional.PROTOCOL_VERSION; + var request = null; var headers = { 'Host' : Exceptional.Host, - 'Content-Length' : data.length + 'Content-Length' : data.length, + 'Content-Type' : 'application/json; charset=UTF-8' }; - var request = client.request('POST', '/api/errors?api_key=' + Exceptional.API_KEY + "&protocol_version=" + Exceptional.PROTOCOL_VERSION, headers); + // use feature detection to fallback to deprecated + if (!HTTP.request) + { + // older versions of nodejs use http.createClient + var client = HTTP.createClient(Exceptional.Port, Exceptional.Host); + request = client.request('POST', path, headers); + + request.on('response', function (response) { + if (response.statusCode === 200) { + if (Exceptional.VERBOSE) console.log("Error data successfully sent to " + Exceptional.Host + ':' + Exceptional.Port ); + } else { + if (Exceptional.VERBOSE) console.log("Error sending to " + Exceptional.Host + ":" + Exceptional.Port + " :" + response.statusCode); + } + }); + } + else + { + // newer versions of nodejs use http.request + var options = { + host: Exceptional.Host, + port: Exceptional.Port, + path: path, + method: 'POST', + header: headers + }; + + request = HTTP.request(options, function(response) { + if (response.statusCode === 200) { + if (Exceptional.VERBOSE) console.log("Error data successfully sent to " + Exceptional.Host + ':' + Exceptional.Port ); + } else { + if (Exceptional.VERBOSE) console.log("Error sending to " + Exceptional.Host + ":" + Exceptional.Port + " :" + response.statusCode); + } + }); + } + // post the data request.write(data); request.end(); - - request.on('response', function (response) { - if (response.statusCode === 200) { - if (Exceptional.VERBOSE) console.log("Error data successfully sent to exceptional"); - } else { - if (Exceptional.VERBOSE) console.log("Error sending to api.getexceptional.com :" + response.statusCode); - } - }); }); } }; diff --git a/lib/gzip/index.js b/lib/gzip/index.js deleted file mode 100644 index bb848c5..0000000 --- a/lib/gzip/index.js +++ /dev/null @@ -1,41 +0,0 @@ -var spawn = require('child_process').spawn, - buffer = require('buffer').Buffer; - -exports.gzip = function(data) { - var rate = 8, - enc = 'utf8', - isBuffer = buffer.isBuffer(data), - args = Array.prototype.slice.call(arguments, 1), - callback; - - if (!isBuffer && typeof args[0] === 'string') { - enc = args.shift(); - } - - if (typeof args[0] === 'number') { - rate = args.shift() - 0; - } - - callback = args[0]; - - if (!callback) return; - - var gzip = spawn('gzip', ['-' + (rate-0),'-c', '-']); - - var output = new buffer(0); - - gzip.stdout.on('data', function(data) { - output = data; - }); - - gzip.on('exit', function(code) { - callback(code, output); - }); - - if (isBuffer) { - gzip.stdin.encoding = 'binary'; - gzip.stdin.end(data.length ? data: ''); - } else { - gzip.stdin.end(data ? data.toString() : '', enc); - } -}; \ No newline at end of file