mirror of https://github.com/status-im/web3.js.git
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:
commit
0c6af6fe25
|
@ -1,3 +1,3 @@
|
|||
ethereum:web3@0.5.0
|
||||
ethereum:web3@0.7.0
|
||||
meteor@1.1.6
|
||||
underscore@1.0.3
|
||||
|
|
|
@ -3527,6 +3527,8 @@ module.exports = ICAP;
|
|||
var utils = require('../utils/utils');
|
||||
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 _this = this;
|
||||
|
@ -3535,49 +3537,78 @@ var IpcProvider = function (path, net) {
|
|||
|
||||
net = net || require('net');
|
||||
|
||||
this.connection = net.connect({path: this.path});
|
||||
|
||||
try {
|
||||
this.connection = net.connect({path: this.path});
|
||||
} catch(error) {
|
||||
throw errors.InvalidConnection(path);
|
||||
}
|
||||
|
||||
// this.connection.on('error', function(e){
|
||||
// throw errors.InvalidConnection(path);
|
||||
// });
|
||||
this.connection.on('error', function(e){
|
||||
console.error('IPC Connection Error', e);
|
||||
_this._timeout();
|
||||
});
|
||||
|
||||
this.connection.on('end', function(){
|
||||
_this._timeout();
|
||||
});
|
||||
|
||||
|
||||
// LISTEN FOR CONNECTION RESPONSES
|
||||
this.connection.on('data', function(result) {
|
||||
result = result.toString();
|
||||
this.connection.on('data', function(data) {
|
||||
/*jshint maxcomplexity: 6 */
|
||||
data = data.toString();
|
||||
|
||||
try {
|
||||
result = JSON.parse(result);
|
||||
|
||||
} catch(e) {
|
||||
throw errors.InvalidResponse(result);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
// DE-CHUNKER
|
||||
var dechunkedData = data
|
||||
.replace(/\}\{/g,'}|--|{') // }{
|
||||
.replace(/\}\]\[\{/g,'}]|--|[{') // }][{
|
||||
.replace(/\}\[\{/g,'}|--|[{') // }[{
|
||||
.replace(/\}\]\{/g,'}]|--|{') // }]{
|
||||
.split('|--|');
|
||||
|
||||
|
||||
// fire the callback
|
||||
if(_this.responseCallbacks[id]) {
|
||||
_this.responseCallbacks[id](null, result);
|
||||
delete _this.responseCallbacks[id];
|
||||
}
|
||||
dechunkedData.forEach(function(data){
|
||||
|
||||
// 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(){
|
||||
_this.timeout();
|
||||
throw errors.InvalidResponse(result);
|
||||
}, 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];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -3589,17 +3620,42 @@ which will be called if a response matching the response Id will arrive.
|
|||
*/
|
||||
IpcProvider.prototype._getResponse = function(payload, callback) {
|
||||
var id = payload.id || payload[0].id;
|
||||
var method = payload.method || payload[0].method;
|
||||
|
||||
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
|
||||
if(!this.connection._handle)
|
||||
this.connection.connect({path: this.path});
|
||||
/**
|
||||
Check if the current connection is still valid.
|
||||
|
||||
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) {
|
||||
|
@ -3607,7 +3663,7 @@ IpcProvider.prototype.send = function (payload) {
|
|||
if(this.connection.writeSync) {
|
||||
|
||||
// try reconnect, when connection is gone
|
||||
if(!this.connection._handle)
|
||||
if(!this.connection.writable)
|
||||
this.connection.connect({path: this.path});
|
||||
|
||||
var result = this.connection.writeSync(JSON.stringify(payload));
|
||||
|
@ -3627,7 +3683,7 @@ IpcProvider.prototype.send = function (payload) {
|
|||
|
||||
IpcProvider.prototype.sendAsync = function (payload, callback) {
|
||||
// try reconnect, when connection is gone
|
||||
if(!this.connection._handle)
|
||||
if(!this.connection.writable)
|
||||
this.connection.connect({path: this.path});
|
||||
|
||||
|
||||
|
@ -3828,7 +3884,7 @@ Method.prototype.formatInput = function (args) {
|
|||
* @return {Object}
|
||||
*/
|
||||
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
|
@ -3527,6 +3527,8 @@ module.exports = ICAP;
|
|||
var utils = require('../utils/utils');
|
||||
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 _this = this;
|
||||
|
@ -3535,49 +3537,78 @@ var IpcProvider = function (path, net) {
|
|||
|
||||
net = net || require('net');
|
||||
|
||||
this.connection = net.connect({path: this.path});
|
||||
|
||||
try {
|
||||
this.connection = net.connect({path: this.path});
|
||||
} catch(error) {
|
||||
throw errors.InvalidConnection(path);
|
||||
}
|
||||
|
||||
// this.connection.on('error', function(e){
|
||||
// throw errors.InvalidConnection(path);
|
||||
// });
|
||||
this.connection.on('error', function(e){
|
||||
console.error('IPC Connection Error', e);
|
||||
_this._timeout();
|
||||
});
|
||||
|
||||
this.connection.on('end', function(){
|
||||
_this._timeout();
|
||||
});
|
||||
|
||||
|
||||
// LISTEN FOR CONNECTION RESPONSES
|
||||
this.connection.on('data', function(result) {
|
||||
result = result.toString();
|
||||
this.connection.on('data', function(data) {
|
||||
/*jshint maxcomplexity: 6 */
|
||||
data = data.toString();
|
||||
|
||||
try {
|
||||
result = JSON.parse(result);
|
||||
|
||||
} catch(e) {
|
||||
throw errors.InvalidResponse(result);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
// DE-CHUNKER
|
||||
var dechunkedData = data
|
||||
.replace(/\}\{/g,'}|--|{') // }{
|
||||
.replace(/\}\]\[\{/g,'}]|--|[{') // }][{
|
||||
.replace(/\}\[\{/g,'}|--|[{') // }[{
|
||||
.replace(/\}\]\{/g,'}]|--|{') // }]{
|
||||
.split('|--|');
|
||||
|
||||
|
||||
// fire the callback
|
||||
if(_this.responseCallbacks[id]) {
|
||||
_this.responseCallbacks[id](null, result);
|
||||
delete _this.responseCallbacks[id];
|
||||
}
|
||||
dechunkedData.forEach(function(data){
|
||||
|
||||
// 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(){
|
||||
_this.timeout();
|
||||
throw errors.InvalidResponse(result);
|
||||
}, 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];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -3589,17 +3620,42 @@ which will be called if a response matching the response Id will arrive.
|
|||
*/
|
||||
IpcProvider.prototype._getResponse = function(payload, callback) {
|
||||
var id = payload.id || payload[0].id;
|
||||
var method = payload.method || payload[0].method;
|
||||
|
||||
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
|
||||
if(!this.connection._handle)
|
||||
this.connection.connect({path: this.path});
|
||||
/**
|
||||
Check if the current connection is still valid.
|
||||
|
||||
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) {
|
||||
|
@ -3607,7 +3663,7 @@ IpcProvider.prototype.send = function (payload) {
|
|||
if(this.connection.writeSync) {
|
||||
|
||||
// try reconnect, when connection is gone
|
||||
if(!this.connection._handle)
|
||||
if(!this.connection.writable)
|
||||
this.connection.connect({path: this.path});
|
||||
|
||||
var result = this.connection.writeSync(JSON.stringify(payload));
|
||||
|
@ -3627,7 +3683,7 @@ IpcProvider.prototype.send = function (payload) {
|
|||
|
||||
IpcProvider.prototype.sendAsync = function (payload, callback) {
|
||||
// try reconnect, when connection is gone
|
||||
if(!this.connection._handle)
|
||||
if(!this.connection.writable)
|
||||
this.connection.connect({path: this.path});
|
||||
|
||||
|
||||
|
@ -3828,7 +3884,7 @@ Method.prototype.formatInput = function (args) {
|
|||
* @return {Object}
|
||||
*/
|
||||
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
|
@ -25,6 +25,8 @@
|
|||
var utils = require('../utils/utils');
|
||||
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 _this = this;
|
||||
|
@ -33,49 +35,78 @@ var IpcProvider = function (path, net) {
|
|||
|
||||
net = net || require('net');
|
||||
|
||||
this.connection = net.connect({path: this.path});
|
||||
|
||||
try {
|
||||
this.connection = net.connect({path: this.path});
|
||||
} catch(error) {
|
||||
throw errors.InvalidConnection(path);
|
||||
}
|
||||
|
||||
// this.connection.on('error', function(e){
|
||||
// throw errors.InvalidConnection(path);
|
||||
// });
|
||||
this.connection.on('error', function(e){
|
||||
console.error('IPC Connection Error', e);
|
||||
_this._timeout();
|
||||
});
|
||||
|
||||
this.connection.on('end', function(){
|
||||
_this._timeout();
|
||||
});
|
||||
|
||||
|
||||
// LISTEN FOR CONNECTION RESPONSES
|
||||
this.connection.on('data', function(result) {
|
||||
result = result.toString();
|
||||
this.connection.on('data', function(data) {
|
||||
/*jshint maxcomplexity: 6 */
|
||||
data = data.toString();
|
||||
|
||||
try {
|
||||
result = JSON.parse(result);
|
||||
|
||||
} catch(e) {
|
||||
throw errors.InvalidResponse(result);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
// DE-CHUNKER
|
||||
var dechunkedData = data
|
||||
.replace(/\}\{/g,'}|--|{') // }{
|
||||
.replace(/\}\]\[\{/g,'}]|--|[{') // }][{
|
||||
.replace(/\}\[\{/g,'}|--|[{') // }[{
|
||||
.replace(/\}\]\{/g,'}]|--|{') // }]{
|
||||
.split('|--|');
|
||||
|
||||
|
||||
// fire the callback
|
||||
if(_this.responseCallbacks[id]) {
|
||||
_this.responseCallbacks[id](null, result);
|
||||
delete _this.responseCallbacks[id];
|
||||
}
|
||||
dechunkedData.forEach(function(data){
|
||||
|
||||
// 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(){
|
||||
_this.timeout();
|
||||
throw errors.InvalidResponse(result);
|
||||
}, 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];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -87,17 +118,42 @@ which will be called if a response matching the response Id will arrive.
|
|||
*/
|
||||
IpcProvider.prototype._getResponse = function(payload, callback) {
|
||||
var id = payload.id || payload[0].id;
|
||||
var method = payload.method || payload[0].method;
|
||||
|
||||
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
|
||||
if(!this.connection._handle)
|
||||
this.connection.connect({path: this.path});
|
||||
/**
|
||||
Check if the current connection is still valid.
|
||||
|
||||
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) {
|
||||
|
@ -105,7 +161,7 @@ IpcProvider.prototype.send = function (payload) {
|
|||
if(this.connection.writeSync) {
|
||||
|
||||
// try reconnect, when connection is gone
|
||||
if(!this.connection._handle)
|
||||
if(!this.connection.writable)
|
||||
this.connection.connect({path: this.path});
|
||||
|
||||
var result = this.connection.writeSync(JSON.stringify(payload));
|
||||
|
@ -125,7 +181,7 @@ IpcProvider.prototype.send = function (payload) {
|
|||
|
||||
IpcProvider.prototype.sendAsync = function (payload, callback) {
|
||||
// try reconnect, when connection is gone
|
||||
if(!this.connection._handle)
|
||||
if(!this.connection.writable)
|
||||
this.connection.connect({path: this.path});
|
||||
|
||||
|
||||
|
|
|
@ -94,7 +94,7 @@ Method.prototype.formatInput = function (args) {
|
|||
* @return {Object}
|
||||
*/
|
||||
Method.prototype.formatOutput = function (result) {
|
||||
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result;
|
||||
return this.outputFormatter && result ? this.outputFormatter(result) : result;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -13,9 +13,9 @@ describe('lib/web3/ipcprovider', function () {
|
|||
describe('send', function () {
|
||||
it('should send basic request', function () {
|
||||
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) {
|
||||
var provider = new IpcProvider();
|
||||
|
||||
provider.sendAsync({id: 1}, function (err, result) {
|
||||
assert.equal(typeof result, 'object');
|
||||
provider.sendAsync({id: 1, method: 'eth_test'}, function (err, result) {
|
||||
assert.isObject(result);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -40,7 +40,7 @@ describe('lib/web3/ipcprovider', function () {
|
|||
it('should return false', function () {
|
||||
var provider = new IpcProvider();
|
||||
|
||||
provider.connection._handle = null;
|
||||
provider.connection.writable = false;
|
||||
|
||||
assert.isFalse(provider.isConnected());
|
||||
});
|
||||
|
@ -48,7 +48,7 @@ describe('lib/web3/ipcprovider', function () {
|
|||
it('should return true, when a net handle is set', function () {
|
||||
var provider = new IpcProvider();
|
||||
|
||||
provider.connection._handle = {fd: true};
|
||||
provider.connection.writable = true;
|
||||
|
||||
assert.isTrue(provider.isConnected());
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue