File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed
Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -18,12 +18,20 @@ function Connection(port, address){
1818 const self = this ;
1919 this . port = port ;
2020 this . address = address ;
21+ this . buffer = new Buffer ( 0 ) ;
22+ this . delimiter = Buffer . from ( '\n' , 'utf8' ) ;
2123 const read = function ( ) {
2224 process . nextTick ( function ( ) {
2325 if ( self . isOpen ( ) ) {
24- self . port . read ( function ( err , data ) {
26+ self . port . read ( function ( err , chunk ) {
2527 if ( err ) return self . emit ( 'error' , err ) ;
26- self . emit ( 'data' , data ) ;
28+ let data = Buffer . concat ( [ self . buffer , chunk ] ) ;
29+ let position ;
30+ while ( ( position = data . indexOf ( self . delimiter ) ) !== - 1 ) {
31+ self . emit ( 'data' , data . slice ( 0 , position ) ) ;
32+ data = data . slice ( position + self . delimiter . length ) ;
33+ }
34+ self . buffer = data ;
2735 read ( ) ;
2836 } ) ;
2937 }
You can’t perform that action at this time.
0 commit comments