add dechunker to the ipc provider

This commit is contained in:
Fabian Vogelsteller 2015-07-02 17:47:35 +02:00
parent 788337dd55
commit bcb121e58e
1 changed files with 55 additions and 25 deletions

View File

@ -48,17 +48,47 @@ 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();
// DE-CHUNKER
var dechunkedData = data
.replace(/\}\{/g,'}|--|{') // }{
.replace(/\}\]\[\{/g,'}]|--|[{') // }][{
.replace(/\}\[\{/g,'}|--|[{') // }[{
.replace(/\}\]\{/g,'}]|--|{') // }]{
.split('|--|');
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;
}
var id;
// 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)) {
@ -75,7 +105,7 @@ var IpcProvider = function (path, net) {
_this.responseCallbacks[id](null, result);
delete _this.responseCallbacks[id];
}
}
});
};