delayed connection try for isConnected(), fixed outputformatter check of methods

This commit is contained in:
Fabian Vogelsteller 2015-07-02 19:19:32 +02:00
parent bcb121e58e
commit b44390e48c
7 changed files with 151 additions and 79 deletions

100
dist/web3-light.js vendored
View File

@ -3417,7 +3417,7 @@ module.exports = ICAP;
var utils = require('../utils/utils'); var utils = require('../utils/utils');
var errors = require('./errors'); var errors = require('./errors');
var errorTimeout = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "FRONTEND Request timed out for method \'__method__\'"}, "id": "__id__"}'; var errorTimeout = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "IPC Request timed out for method \'__method__\'"}, "id": "__id__"}';
var IpcProvider = function (path, net) { var IpcProvider = function (path, net) {
@ -3430,45 +3430,74 @@ var IpcProvider = function (path, net) {
this.connection = net.connect({path: this.path}); this.connection = net.connect({path: this.path});
this.connection.on('error', function(e){ this.connection.on('error', function(e){
console.error('IPC Connection error', e); console.error('IPC Connection Error', e);
_this._timeout(); _this._timeout();
}); });
this.connection.on('end', function(e){ this.connection.on('end', function(e){
console.error('IPC Connection ended', e);
_this._timeout(); _this._timeout();
}); });
// LISTEN FOR CONNECTION RESPONSES // LISTEN FOR CONNECTION RESPONSES
this.connection.on('data', function(result) { this.connection.on('data', function(data) {
result = result.toString(); data = data.toString();
try { // DE-CHUNKER
result = JSON.parse(result); var dechunkedData = data
.replace(/\}\{/g,'}|--|{') // }{
.replace(/\}\]\[\{/g,'}]|--|[{') // }][{
.replace(/\}\[\{/g,'}|--|[{') // }[{
.replace(/\}\]\{/g,'}]|--|{') // }]{
.split('|--|');
} catch(e) { for (var i = 0; i < dechunkedData.length; i++) {
throw errors.InvalidResponse(result); data = dechunkedData[i];
// prepend the last chunk
if(_this.lastChunk)
data = _this.lastChunk + data;
var result = data,
id = null;
try {
result = JSON.parse(result);
} catch(e) {
_this.lastChunk = data;
// start timeout to cancel all requests
clearTimeout(_this.lastChunkTimeout);
_this.lastChunkTimeout = setTimeout(function(){
throw errors.InvalidResponse(result);
_this.timeout();
}, 1000 * 15);
return;
}
// cancel timeout and set chunk to null
clearTimeout(_this.lastChunkTimeout);
_this.lastChunk = null;
// get the id which matches the returned id
if(utils.isArray(result)) {
result.forEach(function(load){
if(_this.responseCallbacks[load.id])
id = load.id;
});
} else {
id = result.id;
}
// fire the callback
if(_this.responseCallbacks[id]) {
_this.responseCallbacks[id](null, result);
delete _this.responseCallbacks[id];
}
} }
var id;
// get the id which matches the returned id
if(utils.isArray(result)) {
result.forEach(function(load){
if(_this.responseCallbacks[load.id])
id = load.id;
});
} else {
id = result.id;
}
// fire the callback
if(_this.responseCallbacks[id]) {
_this.responseCallbacks[id](null, result);
delete _this.responseCallbacks[id];
}
}); });
}; };
@ -3492,9 +3521,10 @@ Timeout all requests when the end/error event is fired
@method _timeout @method _timeout
*/ */
IpcProvider.prototype._timeout = function() { IpcProvider.prototype._timeout = function() {
for(key in this.responseCallbacks) { for(var key in this.responseCallbacks) {
if(this.responseCallback.hasOwnProperty(key)){ if(this.responseCallbacks.hasOwnProperty(key)){
this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method)); this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method));
delete this.responseCallbacks[key];
} }
} }
}; };
@ -3506,9 +3536,13 @@ Check if the current connection is still valid.
@method isConnected @method isConnected
*/ */
IpcProvider.prototype.isConnected = function() { IpcProvider.prototype.isConnected = function() {
var _this = this;
// try reconnect, when connection is gone // try reconnect, when connection is gone
if(!this.connection.writable) setTimeout(function(){
this.connection.connect({path: this.path}); if(!_this.connection.writable)
_this.connection.connect({path: _this.path});
}, 0);
return !!this.connection.writable; return !!this.connection.writable;
}; };
@ -3739,7 +3773,7 @@ Method.prototype.formatInput = function (args) {
* @return {Object} * @return {Object}
*/ */
Method.prototype.formatOutput = function (result) { Method.prototype.formatOutput = function (result) {
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result; return this.outputFormatter && result ? this.outputFormatter(result) : result;
}; };
/** /**

File diff suppressed because one or more lines are too long

100
dist/web3.js vendored
View File

@ -3417,7 +3417,7 @@ module.exports = ICAP;
var utils = require('../utils/utils'); var utils = require('../utils/utils');
var errors = require('./errors'); var errors = require('./errors');
var errorTimeout = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "FRONTEND Request timed out for method \'__method__\'"}, "id": "__id__"}'; var errorTimeout = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "IPC Request timed out for method \'__method__\'"}, "id": "__id__"}';
var IpcProvider = function (path, net) { var IpcProvider = function (path, net) {
@ -3430,45 +3430,74 @@ var IpcProvider = function (path, net) {
this.connection = net.connect({path: this.path}); this.connection = net.connect({path: this.path});
this.connection.on('error', function(e){ this.connection.on('error', function(e){
console.error('IPC Connection error', e); console.error('IPC Connection Error', e);
_this._timeout(); _this._timeout();
}); });
this.connection.on('end', function(e){ this.connection.on('end', function(e){
console.error('IPC Connection ended', e);
_this._timeout(); _this._timeout();
}); });
// LISTEN FOR CONNECTION RESPONSES // LISTEN FOR CONNECTION RESPONSES
this.connection.on('data', function(result) { this.connection.on('data', function(data) {
result = result.toString(); data = data.toString();
try { // DE-CHUNKER
result = JSON.parse(result); var dechunkedData = data
.replace(/\}\{/g,'}|--|{') // }{
.replace(/\}\]\[\{/g,'}]|--|[{') // }][{
.replace(/\}\[\{/g,'}|--|[{') // }[{
.replace(/\}\]\{/g,'}]|--|{') // }]{
.split('|--|');
} catch(e) { for (var i = 0; i < dechunkedData.length; i++) {
throw errors.InvalidResponse(result); data = dechunkedData[i];
// prepend the last chunk
if(_this.lastChunk)
data = _this.lastChunk + data;
var result = data,
id = null;
try {
result = JSON.parse(result);
} catch(e) {
_this.lastChunk = data;
// start timeout to cancel all requests
clearTimeout(_this.lastChunkTimeout);
_this.lastChunkTimeout = setTimeout(function(){
throw errors.InvalidResponse(result);
_this.timeout();
}, 1000 * 15);
return;
}
// cancel timeout and set chunk to null
clearTimeout(_this.lastChunkTimeout);
_this.lastChunk = null;
// get the id which matches the returned id
if(utils.isArray(result)) {
result.forEach(function(load){
if(_this.responseCallbacks[load.id])
id = load.id;
});
} else {
id = result.id;
}
// fire the callback
if(_this.responseCallbacks[id]) {
_this.responseCallbacks[id](null, result);
delete _this.responseCallbacks[id];
}
} }
var id;
// get the id which matches the returned id
if(utils.isArray(result)) {
result.forEach(function(load){
if(_this.responseCallbacks[load.id])
id = load.id;
});
} else {
id = result.id;
}
// fire the callback
if(_this.responseCallbacks[id]) {
_this.responseCallbacks[id](null, result);
delete _this.responseCallbacks[id];
}
}); });
}; };
@ -3492,9 +3521,10 @@ Timeout all requests when the end/error event is fired
@method _timeout @method _timeout
*/ */
IpcProvider.prototype._timeout = function() { IpcProvider.prototype._timeout = function() {
for(key in this.responseCallbacks) { for(var key in this.responseCallbacks) {
if(this.responseCallback.hasOwnProperty(key)){ if(this.responseCallbacks.hasOwnProperty(key)){
this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method)); this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method));
delete this.responseCallbacks[key];
} }
} }
}; };
@ -3506,9 +3536,13 @@ Check if the current connection is still valid.
@method isConnected @method isConnected
*/ */
IpcProvider.prototype.isConnected = function() { IpcProvider.prototype.isConnected = function() {
var _this = this;
// try reconnect, when connection is gone // try reconnect, when connection is gone
if(!this.connection.writable) setTimeout(function(){
this.connection.connect({path: this.path}); if(!_this.connection.writable)
_this.connection.connect({path: _this.path});
}, 0);
return !!this.connection.writable; return !!this.connection.writable;
}; };
@ -3739,7 +3773,7 @@ Method.prototype.formatInput = function (args) {
* @return {Object} * @return {Object}
*/ */
Method.prototype.formatOutput = function (result) { Method.prototype.formatOutput = function (result) {
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result; return this.outputFormatter && result ? this.outputFormatter(result) : result;
}; };
/** /**

7
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -132,6 +132,7 @@ IpcProvider.prototype._timeout = function() {
for(var key in this.responseCallbacks) { for(var key in this.responseCallbacks) {
if(this.responseCallbacks.hasOwnProperty(key)){ if(this.responseCallbacks.hasOwnProperty(key)){
this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method)); this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method));
delete this.responseCallbacks[key];
} }
} }
}; };
@ -143,9 +144,13 @@ Check if the current connection is still valid.
@method isConnected @method isConnected
*/ */
IpcProvider.prototype.isConnected = function() { IpcProvider.prototype.isConnected = function() {
var _this = this;
// try reconnect, when connection is gone // try reconnect, when connection is gone
if(!this.connection.writable) setTimeout(function(){
this.connection.connect({path: this.path}); if(!_this.connection.writable)
_this.connection.connect({path: _this.path});
}, 0);
return !!this.connection.writable; return !!this.connection.writable;
}; };

View File

@ -94,7 +94,7 @@ Method.prototype.formatInput = function (args) {
* @return {Object} * @return {Object}
*/ */
Method.prototype.formatOutput = function (result) { Method.prototype.formatOutput = function (result) {
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result; return this.outputFormatter && result ? this.outputFormatter(result) : result;
}; };
/** /**

View File

@ -13,9 +13,9 @@ describe('lib/web3/ipcprovider', function () {
describe('send', function () { describe('send', function () {
it('should send basic request', function () { it('should send basic request', function () {
var provider = new IpcProvider(); var provider = new IpcProvider();
var result = provider.send({}); var result = provider.send({id: 1, method: 'eth_test'});
assert.equal(typeof result, 'object'); assert.isObject(result);
}); });
}); });
@ -23,8 +23,8 @@ describe('lib/web3/ipcprovider', function () {
it('should send basic async request', function (done) { it('should send basic async request', function (done) {
var provider = new IpcProvider(); var provider = new IpcProvider();
provider.sendAsync({id: 1}, function (err, result) { provider.sendAsync({id: 1, method: 'eth_test'}, function (err, result) {
assert.equal(typeof result, 'object'); assert.isObject(result);
done(); done();
}); });
}); });