Merge branch 'ipcProvider' of https://github.com/ethereum/ethereum.js into ipcProvider

Conflicts:
	dist/web3-light.min.js
	dist/web3.min.js
This commit is contained in:
Marek Kotewicz 2015-07-09 12:08:20 +02:00
commit 0c6af6fe25
8 changed files with 306 additions and 139 deletions

View File

@ -1,3 +1,3 @@
ethereum:web3@0.5.0 ethereum:web3@0.7.0
meteor@1.1.6 meteor@1.1.6
underscore@1.0.3 underscore@1.0.3

98
dist/web3-light.js vendored
View File

@ -3527,6 +3527,8 @@ 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": "IPC Request timed out for method \'__method__\'"}, "id": "__id__"}';
var IpcProvider = function (path, net) { var IpcProvider = function (path, net) {
var _this = this; var _this = this;
@ -3535,31 +3537,61 @@ var IpcProvider = function (path, net) {
net = net || require('net'); net = net || require('net');
try {
this.connection = net.connect({path: this.path}); this.connection = net.connect({path: this.path});
} catch(error) {
throw errors.InvalidConnection(path);
}
// this.connection.on('error', function(e){ this.connection.on('error', function(e){
// throw errors.InvalidConnection(path); console.error('IPC Connection Error', e);
// }); _this._timeout();
});
this.connection.on('end', function(){
_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(); /*jshint maxcomplexity: 6 */
data = data.toString();
// DE-CHUNKER
var dechunkedData = data
.replace(/\}\{/g,'}|--|{') // }{
.replace(/\}\]\[\{/g,'}]|--|[{') // }][{
.replace(/\}\[\{/g,'}|--|[{') // }[{
.replace(/\}\]\{/g,'}]|--|{') // }]{
.split('|--|');
dechunkedData.forEach(function(data){
// prepend the last chunk
if(_this.lastChunk)
data = _this.lastChunk + data;
var result = data,
id = null;
try { try {
result = JSON.parse(result); result = JSON.parse(result);
} catch(e) { } catch(e) {
_this.lastChunk = data;
// start timeout to cancel all requests
clearTimeout(_this.lastChunkTimeout);
_this.lastChunkTimeout = setTimeout(function(){
_this.timeout();
throw errors.InvalidResponse(result); throw errors.InvalidResponse(result);
}, 1000 * 15);
return;
} }
var id; // cancel timeout and set chunk to null
clearTimeout(_this.lastChunkTimeout);
_this.lastChunk = null;
// get the id which matches the returned id // get the id which matches the returned id
if(utils.isArray(result)) { if(utils.isArray(result)) {
@ -3571,13 +3603,12 @@ var IpcProvider = function (path, net) {
id = result.id; id = result.id;
} }
// fire the callback // fire the callback
if(_this.responseCallbacks[id]) { if(_this.responseCallbacks[id]) {
_this.responseCallbacks[id](null, result); _this.responseCallbacks[id](null, result);
delete _this.responseCallbacks[id]; delete _this.responseCallbacks[id];
} }
});
}); });
}; };
@ -3589,17 +3620,42 @@ which will be called if a response matching the response Id will arrive.
*/ */
IpcProvider.prototype._getResponse = function(payload, callback) { IpcProvider.prototype._getResponse = function(payload, callback) {
var id = payload.id || payload[0].id; var id = payload.id || payload[0].id;
var method = payload.method || payload[0].method;
this.responseCallbacks[id] = callback; this.responseCallbacks[id] = callback;
this.responseCallbacks[id].method = method;
};
/**
Timeout all requests when the end/error event is fired
@method _timeout
*/
IpcProvider.prototype._timeout = function() {
for(var key in this.responseCallbacks) {
if(this.responseCallbacks.hasOwnProperty(key)){
this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method));
delete this.responseCallbacks[key];
}
}
}; };
IpcProvider.prototype.isConnected = function() { /**
// try reconnect, when connection is gone Check if the current connection is still valid.
if(!this.connection._handle)
this.connection.connect({path: this.path});
return !!this.connection._handle; @method isConnected
*/
IpcProvider.prototype.isConnected = function() {
var _this = this;
// try reconnect, when connection is gone
setTimeout(function(){
if(!_this.connection.writable)
_this.connection.connect({path: _this.path});
}, 0);
return !!this.connection.writable;
}; };
IpcProvider.prototype.send = function (payload) { IpcProvider.prototype.send = function (payload) {
@ -3607,7 +3663,7 @@ IpcProvider.prototype.send = function (payload) {
if(this.connection.writeSync) { if(this.connection.writeSync) {
// try reconnect, when connection is gone // try reconnect, when connection is gone
if(!this.connection._handle) if(!this.connection.writable)
this.connection.connect({path: this.path}); this.connection.connect({path: this.path});
var result = this.connection.writeSync(JSON.stringify(payload)); var result = this.connection.writeSync(JSON.stringify(payload));
@ -3627,7 +3683,7 @@ IpcProvider.prototype.send = function (payload) {
IpcProvider.prototype.sendAsync = function (payload, callback) { IpcProvider.prototype.sendAsync = function (payload, callback) {
// try reconnect, when connection is gone // try reconnect, when connection is gone
if(!this.connection._handle) if(!this.connection.writable)
this.connection.connect({path: this.path}); this.connection.connect({path: this.path});
@ -3828,7 +3884,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

98
dist/web3.js vendored
View File

@ -3527,6 +3527,8 @@ 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": "IPC Request timed out for method \'__method__\'"}, "id": "__id__"}';
var IpcProvider = function (path, net) { var IpcProvider = function (path, net) {
var _this = this; var _this = this;
@ -3535,31 +3537,61 @@ var IpcProvider = function (path, net) {
net = net || require('net'); net = net || require('net');
try {
this.connection = net.connect({path: this.path}); this.connection = net.connect({path: this.path});
} catch(error) {
throw errors.InvalidConnection(path);
}
// this.connection.on('error', function(e){ this.connection.on('error', function(e){
// throw errors.InvalidConnection(path); console.error('IPC Connection Error', e);
// }); _this._timeout();
});
this.connection.on('end', function(){
_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(); /*jshint maxcomplexity: 6 */
data = data.toString();
// DE-CHUNKER
var dechunkedData = data
.replace(/\}\{/g,'}|--|{') // }{
.replace(/\}\]\[\{/g,'}]|--|[{') // }][{
.replace(/\}\[\{/g,'}|--|[{') // }[{
.replace(/\}\]\{/g,'}]|--|{') // }]{
.split('|--|');
dechunkedData.forEach(function(data){
// prepend the last chunk
if(_this.lastChunk)
data = _this.lastChunk + data;
var result = data,
id = null;
try { try {
result = JSON.parse(result); result = JSON.parse(result);
} catch(e) { } catch(e) {
_this.lastChunk = data;
// start timeout to cancel all requests
clearTimeout(_this.lastChunkTimeout);
_this.lastChunkTimeout = setTimeout(function(){
_this.timeout();
throw errors.InvalidResponse(result); throw errors.InvalidResponse(result);
}, 1000 * 15);
return;
} }
var id; // cancel timeout and set chunk to null
clearTimeout(_this.lastChunkTimeout);
_this.lastChunk = null;
// get the id which matches the returned id // get the id which matches the returned id
if(utils.isArray(result)) { if(utils.isArray(result)) {
@ -3571,13 +3603,12 @@ var IpcProvider = function (path, net) {
id = result.id; id = result.id;
} }
// fire the callback // fire the callback
if(_this.responseCallbacks[id]) { if(_this.responseCallbacks[id]) {
_this.responseCallbacks[id](null, result); _this.responseCallbacks[id](null, result);
delete _this.responseCallbacks[id]; delete _this.responseCallbacks[id];
} }
});
}); });
}; };
@ -3589,17 +3620,42 @@ which will be called if a response matching the response Id will arrive.
*/ */
IpcProvider.prototype._getResponse = function(payload, callback) { IpcProvider.prototype._getResponse = function(payload, callback) {
var id = payload.id || payload[0].id; var id = payload.id || payload[0].id;
var method = payload.method || payload[0].method;
this.responseCallbacks[id] = callback; this.responseCallbacks[id] = callback;
this.responseCallbacks[id].method = method;
};
/**
Timeout all requests when the end/error event is fired
@method _timeout
*/
IpcProvider.prototype._timeout = function() {
for(var key in this.responseCallbacks) {
if(this.responseCallbacks.hasOwnProperty(key)){
this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method));
delete this.responseCallbacks[key];
}
}
}; };
IpcProvider.prototype.isConnected = function() { /**
// try reconnect, when connection is gone Check if the current connection is still valid.
if(!this.connection._handle)
this.connection.connect({path: this.path});
return !!this.connection._handle; @method isConnected
*/
IpcProvider.prototype.isConnected = function() {
var _this = this;
// try reconnect, when connection is gone
setTimeout(function(){
if(!_this.connection.writable)
_this.connection.connect({path: _this.path});
}, 0);
return !!this.connection.writable;
}; };
IpcProvider.prototype.send = function (payload) { IpcProvider.prototype.send = function (payload) {
@ -3607,7 +3663,7 @@ IpcProvider.prototype.send = function (payload) {
if(this.connection.writeSync) { if(this.connection.writeSync) {
// try reconnect, when connection is gone // try reconnect, when connection is gone
if(!this.connection._handle) if(!this.connection.writable)
this.connection.connect({path: this.path}); this.connection.connect({path: this.path});
var result = this.connection.writeSync(JSON.stringify(payload)); var result = this.connection.writeSync(JSON.stringify(payload));
@ -3627,7 +3683,7 @@ IpcProvider.prototype.send = function (payload) {
IpcProvider.prototype.sendAsync = function (payload, callback) { IpcProvider.prototype.sendAsync = function (payload, callback) {
// try reconnect, when connection is gone // try reconnect, when connection is gone
if(!this.connection._handle) if(!this.connection.writable)
this.connection.connect({path: this.path}); this.connection.connect({path: this.path});
@ -3828,7 +3884,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

@ -25,6 +25,8 @@
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": "IPC Request timed out for method \'__method__\'"}, "id": "__id__"}';
var IpcProvider = function (path, net) { var IpcProvider = function (path, net) {
var _this = this; var _this = this;
@ -33,31 +35,61 @@ var IpcProvider = function (path, net) {
net = net || require('net'); net = net || require('net');
try {
this.connection = net.connect({path: this.path}); this.connection = net.connect({path: this.path});
} catch(error) {
throw errors.InvalidConnection(path);
}
// this.connection.on('error', function(e){ this.connection.on('error', function(e){
// throw errors.InvalidConnection(path); console.error('IPC Connection Error', e);
// }); _this._timeout();
});
this.connection.on('end', function(){
_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(); /*jshint maxcomplexity: 6 */
data = data.toString();
// DE-CHUNKER
var dechunkedData = data
.replace(/\}\{/g,'}|--|{') // }{
.replace(/\}\]\[\{/g,'}]|--|[{') // }][{
.replace(/\}\[\{/g,'}|--|[{') // }[{
.replace(/\}\]\{/g,'}]|--|{') // }]{
.split('|--|');
dechunkedData.forEach(function(data){
// prepend the last chunk
if(_this.lastChunk)
data = _this.lastChunk + data;
var result = data,
id = null;
try { try {
result = JSON.parse(result); result = JSON.parse(result);
} catch(e) { } catch(e) {
_this.lastChunk = data;
// start timeout to cancel all requests
clearTimeout(_this.lastChunkTimeout);
_this.lastChunkTimeout = setTimeout(function(){
_this.timeout();
throw errors.InvalidResponse(result); throw errors.InvalidResponse(result);
}, 1000 * 15);
return;
} }
var id; // cancel timeout and set chunk to null
clearTimeout(_this.lastChunkTimeout);
_this.lastChunk = null;
// get the id which matches the returned id // get the id which matches the returned id
if(utils.isArray(result)) { if(utils.isArray(result)) {
@ -69,13 +101,12 @@ var IpcProvider = function (path, net) {
id = result.id; id = result.id;
} }
// fire the callback // fire the callback
if(_this.responseCallbacks[id]) { if(_this.responseCallbacks[id]) {
_this.responseCallbacks[id](null, result); _this.responseCallbacks[id](null, result);
delete _this.responseCallbacks[id]; delete _this.responseCallbacks[id];
} }
});
}); });
}; };
@ -87,17 +118,42 @@ which will be called if a response matching the response Id will arrive.
*/ */
IpcProvider.prototype._getResponse = function(payload, callback) { IpcProvider.prototype._getResponse = function(payload, callback) {
var id = payload.id || payload[0].id; var id = payload.id || payload[0].id;
var method = payload.method || payload[0].method;
this.responseCallbacks[id] = callback; this.responseCallbacks[id] = callback;
this.responseCallbacks[id].method = method;
};
/**
Timeout all requests when the end/error event is fired
@method _timeout
*/
IpcProvider.prototype._timeout = function() {
for(var key in this.responseCallbacks) {
if(this.responseCallbacks.hasOwnProperty(key)){
this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method));
delete this.responseCallbacks[key];
}
}
}; };
IpcProvider.prototype.isConnected = function() { /**
// try reconnect, when connection is gone Check if the current connection is still valid.
if(!this.connection._handle)
this.connection.connect({path: this.path});
return !!this.connection._handle; @method isConnected
*/
IpcProvider.prototype.isConnected = function() {
var _this = this;
// try reconnect, when connection is gone
setTimeout(function(){
if(!_this.connection.writable)
_this.connection.connect({path: _this.path});
}, 0);
return !!this.connection.writable;
}; };
IpcProvider.prototype.send = function (payload) { IpcProvider.prototype.send = function (payload) {
@ -105,7 +161,7 @@ IpcProvider.prototype.send = function (payload) {
if(this.connection.writeSync) { if(this.connection.writeSync) {
// try reconnect, when connection is gone // try reconnect, when connection is gone
if(!this.connection._handle) if(!this.connection.writable)
this.connection.connect({path: this.path}); this.connection.connect({path: this.path});
var result = this.connection.writeSync(JSON.stringify(payload)); var result = this.connection.writeSync(JSON.stringify(payload));
@ -125,7 +181,7 @@ IpcProvider.prototype.send = function (payload) {
IpcProvider.prototype.sendAsync = function (payload, callback) { IpcProvider.prototype.sendAsync = function (payload, callback) {
// try reconnect, when connection is gone // try reconnect, when connection is gone
if(!this.connection._handle) if(!this.connection.writable)
this.connection.connect({path: this.path}); this.connection.connect({path: this.path});

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();
}); });
}); });
@ -40,7 +40,7 @@ describe('lib/web3/ipcprovider', function () {
it('should return false', function () { it('should return false', function () {
var provider = new IpcProvider(); var provider = new IpcProvider();
provider.connection._handle = null; provider.connection.writable = false;
assert.isFalse(provider.isConnected()); assert.isFalse(provider.isConnected());
}); });
@ -48,7 +48,7 @@ describe('lib/web3/ipcprovider', function () {
it('should return true, when a net handle is set', function () { it('should return true, when a net handle is set', function () {
var provider = new IpcProvider(); var provider = new IpcProvider();
provider.connection._handle = {fd: true}; provider.connection.writable = true;
assert.isTrue(provider.isConnected()); assert.isTrue(provider.isConnected());
}); });