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,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];
}
});
};