fixes POST request in node

This commit is contained in:
Matias Alejo Garcia 2014-06-19 00:17:46 -03:00
commit 0481cf8e02
2 changed files with 175 additions and 110 deletions

View file

@ -3,6 +3,11 @@
var imports = require('soop').imports();
var bitcore = require('bitcore');
var http;
if (process.version) {
http = require('http');
};
function Insight(opts) {
opts = opts || {};
this.host = opts.host || 'localhost';
@ -130,7 +135,6 @@ Insight.prototype.getUnspent = function(addresses, cb) {
return cb(err);
}
if (res && res.length > 0) {
all = all.concat(res);
}
@ -159,95 +163,111 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) {
});
};
Insight.prototype._request = function(options, callback) {
var self = this;
if (typeof process === 'undefined' || !process.version) {
var request = new XMLHttpRequest();
// TODO: Normalize URL
var url = 'http://' + options.host;
if (options.port !== 80) {
url = url + ':' + options.port;
}
url = url + options.path;
if (options.data && options.method === 'GET') {
url = url + '?' + options.data;
}
request.open(options.method, url, true);
request.timeout = 5000;
request.ontimeout = function() {
setTimeout(function() {
return self._request(options, callback);
}, self.retryDelay);
return callback(new Error('Insight request timeout'));
Insight.prototype._requestNode = function(options, callback) {
if (options.method === 'POST') {
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': options.data.length,
};
}
request.onreadystatechange = function() {
if (request.readyState !== 4) return;
var ret, errTxt, e;
if (request.status === 200 || request.status === 304) {
var req = http.request(options, function(response) {
var ret, errTxt, e;
if (response.statusCode == 200 || response.statusCode === 304) {
response.on('data', function(chunk) {
try {
ret = JSON.parse(request.responseText);
ret = JSON.parse(chunk);
} catch (e2) {
errTxt = 'CRITICAL: Wrong response from insight' + e2;
}
} else if (request.status >= 400 && request.status < 499) {
errTxt = 'CRITICAL: Bad request to insight. Probably wrong transaction to broadcast?.';
} else {
errTxt = 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText;
setTimeout(function() {
console.log('### Retrying Insight Request....');
return self._request(options, callback);
}, self.retryDelay);
}
});
} else {
errTxt = "INSIGHT ERROR:" + response.statusCode;
console.log(errTxt);
e = new Error(errTxt);
return callback(e);
}
response.on('end', function() {
if (errTxt) {
console.log("INSIGHT ERROR:", e);
console.log("INSIGHT ERROR:" + errTxt);
e = new Error(errTxt);
}
return callback(e, ret);
};
if (options.method === 'POST') {
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
request.send(options.data || null);
} else {
var http = require('http');
var req = http.request(options, function(response) {
var ret;
if (response.statusCode == 200 || response.status === 304) {
response.on('data', function(chunk) {
try {
ret = JSON.parse(chunk);
} catch (e) {
return callback({
message: 'Wrong response from insight'
});
}
});
response.on('end', function() {
return callback(null, ret);
});
} else {
return callback({
message: 'Error ' + response.statusCode
});
}
});
response.on('error', function(e) {
return callback(e, ret);
});
});
if (options.data) {
req.write(options.data);
if (options.data) {
req.write(options.data);
}
req.end();
};
Insight.prototype._requestBrowser = function(options, callback) {
var request = new XMLHttpRequest();
// TODO: Normalize URL
var url = 'http://' + options.host;
if (options.port !== 80) {
url = url + ':' + options.port;
}
url = url + options.path;
if (options.data && options.method === 'GET') {
url = url + '?' + options.data;
}
request.open(options.method, url, true);
request.timeout = 5000;
request.ontimeout = function() {
setTimeout(function() {
return self._request(options, callback);
}, self.retryDelay);
return callback(new Error('Insight request timeout'));
};
request.onreadystatechange = function() {
if (request.readyState !== 4) return;
var ret, errTxt, e;
if (request.status === 200 || request.status === 304) {
try {
ret = JSON.parse(request.responseText);
} catch (e2) {
errTxt = 'CRITICAL: Wrong response from insight' + e2;
}
} else if (request.status >= 400 && request.status < 499) {
errTxt = 'CRITICAL: Bad request to insight. Probably wrong transaction to broadcast?.';
} else {
errTxt = 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText;
setTimeout(function() {
console.log('### Retrying Insight Request....');
return self._request(options, callback);
}, self.retryDelay);
}
if (errTxt) {
console.log("INSIGHT ERROR:", e);
e = new Error(errTxt);
}
return callback(e, ret);
};
req.end();
if (options.method === 'POST') {
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
request.send(options.data || null);
};
Insight.prototype._request = function(options, callback) {
if (typeof process === 'undefined' || !process.version) {
this._requestBrowser(options, callback);
} else {
this._requestNode(options, callback);
}
};