diff --git a/docs/callbacks-promises-events.rst b/docs/callbacks-promises-events.rst index 18547c4..e76317a 100644 --- a/docs/callbacks-promises-events.rst +++ b/docs/callbacks-promises-events.rst @@ -21,6 +21,8 @@ This way developers can watch for additional events like on "receipt" or "transa web3.eth.sendTransaction({from: '0x123...', data: '0x432...'}) .once('transactionHash', function(hash){ ... }) .once('receipt', function(receipt){ ... }) + .on('confirmation', function(confNumber, receipt){ ... }) + .on('error', function(error){ ... }) .then(function(receipt){ // will be fired once the receipt its mined }); diff --git a/docs/getting-started.rst b/docs/getting-started.rst index ce38d85..52e03d8 100644 --- a/docs/getting-started.rst +++ b/docs/getting-started.rst @@ -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`` 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 - if (typeof web3 !== 'undefined') { - web3 = new Web3(web3.currentProvider); - } else { - // set the provider you want from Web3.providers - web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); - } + // in node.js use: var Web3 = require('web3'); + + var web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider("ws://localhost:8546")); Thats it! now you can use the ``web3`` object. diff --git a/docs/web3-bzz.rst b/docs/web3-bzz.rst index 578516c..c068068 100644 --- a/docs/web3-bzz.rst +++ b/docs/web3-bzz.rst @@ -14,13 +14,13 @@ For more see the `Swarm Docs `_. var Bzz = require('web3-bzz'); // "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 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 diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index 8eb1203..840d8a2 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -10,13 +10,13 @@ The ``web3-eth`` package allows you to interact with an Ethereum blockchain and var Eth = require('web3-eth'); // "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 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 diff --git a/docs/web3-shh.rst b/docs/web3-shh.rst index 4bc1481..e41ffce 100644 --- a/docs/web3-shh.rst +++ b/docs/web3-shh.rst @@ -14,13 +14,13 @@ For more see `Whisper Overview web3.shh diff --git a/docs/web3.rst b/docs/web3.rst index 80a61ec..a08451b 100644 --- a/docs/web3.rst +++ b/docs/web3.rst @@ -9,9 +9,10 @@ The web3.js object is a umbrella package to house all ethereum related modules. var Web3 = require('web3'); // "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.personal // -> web3.shh // -> web3.bzz // -> web3.utils diff --git a/packages/web3-core-requestManager/package.json b/packages/web3-core-requestManager/package.json index c5b3725..bd89f0b 100644 --- a/packages/web3-core-requestManager/package.json +++ b/packages/web3-core-requestManager/package.json @@ -7,6 +7,7 @@ "main": "src/index.js", "dependencies": { "web3-core-helpers": "^1.0.0", + "web3-providers-ipc": "^1.0.0", "underscore": "^1.8.3", "oboe": "^2.1.3", "xmlhttprequest": "*", diff --git a/packages/web3-core-requestManager/src/index.js b/packages/web3-core-requestManager/src/index.js index ec290b0..5788e17 100644 --- a/packages/web3-core-requestManager/src/index.js +++ b/packages/web3-core-requestManager/src/index.js @@ -30,7 +30,7 @@ var BatchManager = require('./batch'); -/** + /** * It's responsible for passing messages to providers * It's also responsible for polling the ethereum node for incoming messages * Default poll timeout is 1 second @@ -46,18 +46,27 @@ var RequestManager = function RequestManager(provider) { // ADD GIVEN PROVIDER /* jshint ignore:start */ +var IpcProvider = require('web3-providers-ipc'); var global = Function('return this')(); if(typeof global.ethereumProvider !== 'undefined') { RequestManager.givenProvider = global.ethereumProvider; } else if(typeof global.web3 !== 'undefined' && global.web3.currentProvider) { - if(global.web3.currentProvider.sendAsync) { - global.web3.currentProvider.send = global.web3.currentProvider.sendAsync; - delete global.web3.currentProvider.sendAsync; + // 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) { + global.web3.currentProvider.send = global.web3.currentProvider.sendAsync; + delete global.web3.currentProvider.sendAsync; + } + + RequestManager.givenProvider = global.web3.currentProvider; } - RequestManager.givenProvider = global.web3.currentProvider; } /* jshint ignore:end */ diff --git a/packages/web3-providers-ipc/src/index.js b/packages/web3-providers-ipc/src/index.js index da99af8..1046805 100644 --- a/packages/web3-providers-ipc/src/index.js +++ b/packages/web3-providers-ipc/src/index.js @@ -37,10 +37,8 @@ var IpcProvider = function (path, net) { this.addDefaultEvents(); - // LISTEN FOR CONNECTION RESPONSES - oboe(this.connection) - .done(function(result) { + var callback = function(result) { /*jshint maxcomplexity: 6 */ var id = null; @@ -62,12 +60,22 @@ var IpcProvider = function (path, net) { callback(null, result); }); - // fire the callback + // fire the callback } else if(_this.responseCallbacks[id]) { _this.responseCallbacks[id](null, result); 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, which will be called if a response matching the response Id will arrive.