add dechunker to the ipc provider
This commit is contained in:
parent
788337dd55
commit
bcb121e58e
|
@ -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];
|
||||
}
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue