Skip to content

Commit 4b13964

Browse files
committed
child_process: stop indexOf() on whole IPC buffer
1 parent b506f72 commit 4b13964

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

lib/internal/child_process.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,19 @@ function setupChannel(target, channel) {
446446
channel.onread = function(nread, pool, recvHandle) {
447447
// TODO(bnoordhuis) Check that nread > 0.
448448
if (pool) {
449-
jsonBuffer += decoder.write(pool);
450-
451-
var i, start = 0;
449+
// Linebreak is used as a message end sign
450+
var lines = decoder.write(pool).split('\n');
451+
var chunks = lines.slice(0, -1);
452+
// Last line does not have trailing linebreak
453+
var incompleteChunk = lines[lines.length - 1];
454+
if (chunks.length === 0) {
455+
jsonBuffer += incompleteChunk;
456+
this.buffering = jsonBuffer.length !== 0;
457+
return;
458+
}
459+
chunks[0] = jsonBuffer + chunks[0];
452460

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

458464
// There will be at most one NODE_HANDLE message in every chunk we
@@ -462,10 +468,8 @@ function setupChannel(target, channel) {
462468
handleMessage(target, message, recvHandle);
463469
else
464470
handleMessage(target, message, undefined);
465-
466-
start = i + 1;
467-
}
468-
jsonBuffer = jsonBuffer.slice(start);
471+
});
472+
jsonBuffer = incompleteChunk;
469473
this.buffering = jsonBuffer.length !== 0;
470474

471475
} else {

0 commit comments

Comments
 (0)