diff --git a/lib/web3/ipcprovider.js b/lib/web3/ipcprovider.js index adc09f1..3c60b4a 100644 --- a/lib/web3/ipcprovider.js +++ b/lib/web3/ipcprovider.js @@ -48,34 +48,64 @@ var IpcProvider = function (path, net) { // LISTEN FOR CONNECTION RESPONSES - this.connection.on('data', function(result) { - result = result.toString(); + this.connection.on('data', function(data) { + data = data.toString(); - try { - result = JSON.parse(result); + // DE-CHUNKER + var dechunkedData = data + .replace(/\}\{/g,'}|--|{') // }{ + .replace(/\}\]\[\{/g,'}]|--|[{') // }][{ + .replace(/\}\[\{/g,'}|--|[{') // }[{ + .replace(/\}\]\{/g,'}]|--|{') // }]{ + .split('|--|'); - } catch(e) { - throw errors.InvalidResponse(result); + for (var i = 0; i < dechunkedData.length; i++) { + 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]; - } - }); };