Skip to content

Commit

Permalink
child_process: stop indexOf() on whole IPC buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ypresto committed Dec 31, 2016
1 parent b506f72 commit 4b13964
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,19 @@ function setupChannel(target, channel) {
channel.onread = function(nread, pool, recvHandle) {
// TODO(bnoordhuis) Check that nread > 0.
if (pool) {
jsonBuffer += decoder.write(pool);

var i, start = 0;
// Linebreak is used as a message end sign
var lines = decoder.write(pool).split('\n');
var chunks = lines.slice(0, -1);
// Last line does not have trailing linebreak
var incompleteChunk = lines[lines.length - 1];
if (chunks.length === 0) {
jsonBuffer += incompleteChunk;
this.buffering = jsonBuffer.length !== 0;
return;
}
chunks[0] = jsonBuffer + chunks[0];

//Linebreak is used as a message end sign
while ((i = jsonBuffer.indexOf('\n', start)) >= 0) {
var json = jsonBuffer.slice(start, i);
chunks.forEach(function(json) {
var message = JSON.parse(json);

// There will be at most one NODE_HANDLE message in every chunk we
Expand All @@ -462,10 +468,8 @@ function setupChannel(target, channel) {
handleMessage(target, message, recvHandle);
else
handleMessage(target, message, undefined);

start = i + 1;
}
jsonBuffer = jsonBuffer.slice(start);
});
jsonBuffer = incompleteChunk;
this.buffering = jsonBuffer.length !== 0;

} else {
Expand Down

0 comments on commit 4b13964

Please sign in to comment.