@@ -446,13 +446,19 @@ function setupChannel(target, channel) {
446
446
channel . onread = function ( nread , pool , recvHandle ) {
447
447
// TODO(bnoordhuis) Check that nread > 0.
448
448
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 ] ;
452
460
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 ) {
456
462
var message = JSON . parse ( json ) ;
457
463
458
464
// There will be at most one NODE_HANDLE message in every chunk we
@@ -462,10 +468,8 @@ function setupChannel(target, channel) {
462
468
handleMessage ( target , message , recvHandle ) ;
463
469
else
464
470
handleMessage ( target , message , undefined ) ;
465
-
466
- start = i + 1 ;
467
- }
468
- jsonBuffer = jsonBuffer . slice ( start ) ;
471
+ } ) ;
472
+ jsonBuffer = incompleteChunk ;
469
473
this . buffering = jsonBuffer . length !== 0 ;
470
474
471
475
} else {
0 commit comments