mirror of https://github.com/status-im/web3.js.git
added dechunker back for backwards compatibility
This commit is contained in:
parent
4a090ff371
commit
55e0832ac8
|
@ -21,6 +21,8 @@ This way developers can watch for additional events like on "receipt" or "transa
|
||||||
web3.eth.sendTransaction({from: '0x123...', data: '0x432...'})
|
web3.eth.sendTransaction({from: '0x123...', data: '0x432...'})
|
||||||
.once('transactionHash', function(hash){ ... })
|
.once('transactionHash', function(hash){ ... })
|
||||||
.once('receipt', function(receipt){ ... })
|
.once('receipt', function(receipt){ ... })
|
||||||
|
.on('confirmation', function(confNumber, receipt){ ... })
|
||||||
|
.on('error', function(error){ ... })
|
||||||
.then(function(receipt){
|
.then(function(receipt){
|
||||||
// will be fired once the receipt its mined
|
// will be fired once the receipt its mined
|
||||||
});
|
});
|
||||||
|
|
|
@ -32,15 +32,13 @@ First you need to get web3.js into your project. This can be done using the foll
|
||||||
- pure js: link the ``dist/web3.min.js``
|
- pure js: link the ``dist/web3.min.js``
|
||||||
|
|
||||||
After that you need to create a web3 instance and set a provider.
|
After that you need to create a web3 instance and set a provider.
|
||||||
If you are in a ethereum supported Browser like Mist or MetaMask check if the ``web3`` object is available:
|
Ethereum supported Browsers like Mist or MetaMask will have a ``ethereumProvider`` available, web3.js is setting this one to ``Web3.givenProvider``.
|
||||||
|
If this property is ``null`` you need to connect to a remote/local node.
|
||||||
|
|
||||||
.. code-block:: javascript
|
.. code-block:: javascript
|
||||||
|
|
||||||
if (typeof web3 !== 'undefined') {
|
// in node.js use: var Web3 = require('web3');
|
||||||
web3 = new Web3(web3.currentProvider);
|
|
||||||
} else {
|
var web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider("ws://localhost:8546"));
|
||||||
// set the provider you want from Web3.providers
|
|
||||||
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
|
|
||||||
}
|
|
||||||
|
|
||||||
Thats it! now you can use the ``web3`` object.
|
Thats it! now you can use the ``web3`` object.
|
||||||
|
|
|
@ -14,13 +14,13 @@ For more see the `Swarm Docs <http://swarm-guide.readthedocs.io/en/latest/>`_.
|
||||||
var Bzz = require('web3-bzz');
|
var Bzz = require('web3-bzz');
|
||||||
|
|
||||||
// "Bzz.providers.givenProvider" will be set if in an Ethereum supported browser.
|
// "Bzz.providers.givenProvider" will be set if in an Ethereum supported browser.
|
||||||
var bzz = new Bzz(Bzz.providers.givenProvider || new Bzz.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
var bzz = new Bzz(Bzz.givenProvider || new Bzz.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
||||||
|
|
||||||
|
|
||||||
// or using the web3 umbrella package
|
// or using the web3 umbrella package
|
||||||
|
|
||||||
var Web3 = require('web3');
|
var Web3 = require('web3');
|
||||||
var web3 = new Web3(Web3.providers.givenProvider || new Web3.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
var web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
||||||
|
|
||||||
// -> web3.bzz
|
// -> web3.bzz
|
||||||
|
|
||||||
|
|
|
@ -10,13 +10,13 @@ The ``web3-eth`` package allows you to interact with an Ethereum blockchain and
|
||||||
var Eth = require('web3-eth');
|
var Eth = require('web3-eth');
|
||||||
|
|
||||||
// "Eth.providers.givenProvider" will be set if in an Ethereum supported browser.
|
// "Eth.providers.givenProvider" will be set if in an Ethereum supported browser.
|
||||||
var eth = new Eth(Eth.providers.givenProvider || new Eth.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
var eth = new Eth(Eth.givenProvider || new Eth.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
||||||
|
|
||||||
|
|
||||||
// or using the web3 umbrella package
|
// or using the web3 umbrella package
|
||||||
|
|
||||||
var Web3 = require('web3');
|
var Web3 = require('web3');
|
||||||
var web3 = new Web3(Web3.providers.givenProvider || new Web3.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
var web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
||||||
|
|
||||||
// -> web3.eth
|
// -> web3.eth
|
||||||
|
|
||||||
|
|
|
@ -14,13 +14,13 @@ For more see `Whisper Overview <https://github.com/ethereum/wiki/wiki/Whisper-O
|
||||||
var Shh = require('web3-shh');
|
var Shh = require('web3-shh');
|
||||||
|
|
||||||
// "Shh.providers.givenProvider" will be set if in an Ethereum supported browser.
|
// "Shh.providers.givenProvider" will be set if in an Ethereum supported browser.
|
||||||
var shh = new Shh(Shh.providers.givenProvider || new Shh.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
var shh = new Shh(Shh.givenProvider || new Shh.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
||||||
|
|
||||||
|
|
||||||
// or using the web3 umbrella package
|
// or using the web3 umbrella package
|
||||||
|
|
||||||
var Web3 = require('web3');
|
var Web3 = require('web3');
|
||||||
var web3 = new Web3(Web3.providers.givenProvider || new Web3.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
var web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
||||||
|
|
||||||
// -> web3.shh
|
// -> web3.shh
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,10 @@ The web3.js object is a umbrella package to house all ethereum related modules.
|
||||||
var Web3 = require('web3');
|
var Web3 = require('web3');
|
||||||
|
|
||||||
// "Web3.providers.givenProvider" will be set if in an Ethereum supported browser.
|
// "Web3.providers.givenProvider" will be set if in an Ethereum supported browser.
|
||||||
var web3 = new Web3(Web3.providers.givenProvider || new Web3.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
var web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://some.local-or-remote.node:8546'));
|
||||||
|
|
||||||
// -> web3.eth
|
// -> web3.eth
|
||||||
|
// -> web3.personal
|
||||||
// -> web3.shh
|
// -> web3.shh
|
||||||
// -> web3.bzz
|
// -> web3.bzz
|
||||||
// -> web3.utils
|
// -> web3.utils
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"web3-core-helpers": "^1.0.0",
|
"web3-core-helpers": "^1.0.0",
|
||||||
|
"web3-providers-ipc": "^1.0.0",
|
||||||
"underscore": "^1.8.3",
|
"underscore": "^1.8.3",
|
||||||
"oboe": "^2.1.3",
|
"oboe": "^2.1.3",
|
||||||
"xmlhttprequest": "*",
|
"xmlhttprequest": "*",
|
||||||
|
|
|
@ -46,12 +46,19 @@ var RequestManager = function RequestManager(provider) {
|
||||||
|
|
||||||
// ADD GIVEN PROVIDER
|
// ADD GIVEN PROVIDER
|
||||||
/* jshint ignore:start */
|
/* jshint ignore:start */
|
||||||
|
var IpcProvider = require('web3-providers-ipc');
|
||||||
var global = Function('return this')();
|
var global = Function('return this')();
|
||||||
|
|
||||||
if(typeof global.ethereumProvider !== 'undefined') {
|
if(typeof global.ethereumProvider !== 'undefined') {
|
||||||
RequestManager.givenProvider = global.ethereumProvider;
|
RequestManager.givenProvider = global.ethereumProvider;
|
||||||
|
|
||||||
} else if(typeof global.web3 !== 'undefined' && global.web3.currentProvider) {
|
} else if(typeof global.web3 !== 'undefined' && global.web3.currentProvider) {
|
||||||
|
// if connection object is available, create new provider
|
||||||
|
if (global.web3.currentProvider.connection) {
|
||||||
|
RequestManager.givenProvider = new IpcProvider('', global.web3.currentProvider.connection);
|
||||||
|
|
||||||
|
// otherwise subscription aren't available
|
||||||
|
} else {
|
||||||
if(global.web3.currentProvider.sendAsync) {
|
if(global.web3.currentProvider.sendAsync) {
|
||||||
global.web3.currentProvider.send = global.web3.currentProvider.sendAsync;
|
global.web3.currentProvider.send = global.web3.currentProvider.sendAsync;
|
||||||
delete global.web3.currentProvider.sendAsync;
|
delete global.web3.currentProvider.sendAsync;
|
||||||
|
@ -59,6 +66,8 @@ if(typeof global.ethereumProvider !== 'undefined') {
|
||||||
|
|
||||||
RequestManager.givenProvider = global.web3.currentProvider;
|
RequestManager.givenProvider = global.web3.currentProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
/* jshint ignore:end */
|
/* jshint ignore:end */
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -37,10 +37,8 @@ var IpcProvider = function (path, net) {
|
||||||
|
|
||||||
this.addDefaultEvents();
|
this.addDefaultEvents();
|
||||||
|
|
||||||
|
|
||||||
// LISTEN FOR CONNECTION RESPONSES
|
// LISTEN FOR CONNECTION RESPONSES
|
||||||
oboe(this.connection)
|
var callback = function(result) {
|
||||||
.done(function(result) {
|
|
||||||
/*jshint maxcomplexity: 6 */
|
/*jshint maxcomplexity: 6 */
|
||||||
|
|
||||||
var id = null;
|
var id = null;
|
||||||
|
@ -67,7 +65,17 @@ var IpcProvider = function (path, net) {
|
||||||
_this.responseCallbacks[id](null, result);
|
_this.responseCallbacks[id](null, result);
|
||||||
delete _this.responseCallbacks[id];
|
delete _this.responseCallbacks[id];
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// use oboe.js for Sockets
|
||||||
|
if (net.constructor.name === 'Socket') {
|
||||||
|
oboe(this.connection)
|
||||||
|
.done(callback);
|
||||||
|
} else {
|
||||||
|
this.connection.on('data', function(data){
|
||||||
|
_this._parseResponse(data.toString()).forEach(callback);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -101,6 +109,63 @@ IpcProvider.prototype.addDefaultEvents = function(){
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Will parse the response and make an array out of it.
|
||||||
|
|
||||||
|
NOTE, this exists for backwards compatibility reasons.
|
||||||
|
|
||||||
|
@method _parseResponse
|
||||||
|
@param {String} data
|
||||||
|
*/
|
||||||
|
IpcProvider.prototype._parseResponse = function(data) {
|
||||||
|
var _this = this,
|
||||||
|
returnValues = [];
|
||||||
|
|
||||||
|
// DE-CHUNKER
|
||||||
|
var dechunkedData = data
|
||||||
|
.replace(/\}[\n\r]?\{/g,'}|--|{') // }{
|
||||||
|
.replace(/\}\][\n\r]?\[\{/g,'}]|--|[{') // }][{
|
||||||
|
.replace(/\}[\n\r]?\[\{/g,'}|--|[{') // }[{
|
||||||
|
.replace(/\}\][\n\r]?\{/g,'}]|--|{') // }]{
|
||||||
|
.split('|--|');
|
||||||
|
|
||||||
|
dechunkedData.forEach(function(data){
|
||||||
|
|
||||||
|
// prepend the last chunk
|
||||||
|
if(_this.lastChunk)
|
||||||
|
data = _this.lastChunk + data;
|
||||||
|
|
||||||
|
var result = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = JSON.parse(data);
|
||||||
|
|
||||||
|
} catch(e) {
|
||||||
|
|
||||||
|
_this.lastChunk = data;
|
||||||
|
|
||||||
|
// start timeout to cancel all requests
|
||||||
|
clearTimeout(_this.lastChunkTimeout);
|
||||||
|
_this.lastChunkTimeout = setTimeout(function(){
|
||||||
|
_this._timeout();
|
||||||
|
throw errors.InvalidResponse(data);
|
||||||
|
}, 1000 * 15);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// cancel timeout and set chunk to null
|
||||||
|
clearTimeout(_this.lastChunkTimeout);
|
||||||
|
_this.lastChunk = null;
|
||||||
|
|
||||||
|
if(result)
|
||||||
|
returnValues.push(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
return returnValues;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Get the adds a callback to the responseCallbacks object,
|
Get the adds a callback to the responseCallbacks object,
|
||||||
which will be called if a response matching the response Id will arrive.
|
which will be called if a response matching the response Id will arrive.
|
||||||
|
|
Loading…
Reference in New Issue