@@ -5211,7 +5211,7 @@ const init_state = function(options){
52115211 record : [ ] ,
52125212 recordHasError : false ,
52135213 record_length : 0 ,
5214- recordDelimiterMaxLength : options . record_delimiter . length === 0 ? 2 : Math . max ( ...options . record_delimiter . map ( ( v ) => v . length ) ) ,
5214+ recordDelimiterMaxLength : options . record_delimiter . length === 0 ? 0 : Math . max ( ...options . record_delimiter . map ( ( v ) => v . length ) ) ,
52155215 trimChars : [ Buffer . from ( ' ' , options . encoding ) [ 0 ] , Buffer . from ( '\t' , options . encoding ) [ 0 ] ] ,
52165216 wasQuoting : false ,
52175217 wasRowDelimiter : false ,
@@ -5676,16 +5676,22 @@ const transform = function(original_options = {}) {
56765676 state : init_state ( options ) ,
56775677 __needMoreData : function ( i , bufLen , end ) {
56785678 if ( end ) return false ;
5679- const { quote} = this . options ;
5679+ const { encoding , escape , quote} = this . options ;
56805680 const { quoting, needMoreDataSize, recordDelimiterMaxLength} = this . state ;
56815681 const numOfCharLeft = bufLen - i - 1 ;
56825682 const requiredLength = Math . max (
56835683 needMoreDataSize ,
56845684 // Skip if the remaining buffer smaller than record delimiter
5685- recordDelimiterMaxLength ,
5686- // Skip if the remaining buffer can be record delimiter following the closing quote
5687- // 1 is for quote.length
5688- quoting ? ( quote . length + recordDelimiterMaxLength ) : 0 ,
5685+ // If "record_delimiter" is yet to be discovered:
5686+ // 1. It is equals to `[]` and "recordDelimiterMaxLength" equals `0`
5687+ // 2. We set the length to windows line ending in the current encoding
5688+ // Note, that encoding is known from user or bom discovery at that point
5689+ // recordDelimiterMaxLength,
5690+ recordDelimiterMaxLength === 0 ? Buffer . from ( '\r\n' , encoding ) . length : recordDelimiterMaxLength ,
5691+ // Skip if remaining buffer can be an escaped quote
5692+ quoting ? ( ( escape === null ? 0 : escape . length ) + quote . length ) : 0 ,
5693+ // Skip if remaining buffer can be record delimiter following the closing quote
5694+ quoting ? ( quote . length ) : 0 ,
56895695 ) ;
56905696 return numOfCharLeft < requiredLength ;
56915697 } ,
@@ -6280,22 +6286,26 @@ const transform = function(original_options = {}) {
62806286 return true ;
62816287 } ,
62826288 __autoDiscoverRecordDelimiter : function ( buf , pos ) {
6283- const { encoding} = this . options ;
6284- const chr = buf [ pos ] ;
6285- if ( chr === cr ) {
6286- if ( buf [ pos + 1 ] === nl ) {
6287- this . options . record_delimiter . push ( Buffer . from ( '\r\n' , encoding ) ) ;
6288- this . state . recordDelimiterMaxLength = 2 ;
6289- return 2 ;
6290- } else {
6291- this . options . record_delimiter . push ( Buffer . from ( '\r' , encoding ) ) ;
6292- this . state . recordDelimiterMaxLength = 1 ;
6293- return 1 ;
6289+ const { encoding } = this . options ;
6290+ // Note, we don't need to cache this information in state,
6291+ // It is only called on the first line until we find out a suitable
6292+ // record delimiter.
6293+ const rds = [
6294+ // Important, the windows line ending must be before mac os 9
6295+ Buffer . from ( '\r\n' , encoding ) ,
6296+ Buffer . from ( '\n' , encoding ) ,
6297+ Buffer . from ( '\r' , encoding ) ,
6298+ ] ;
6299+ loop: for ( let i = 0 ; i < rds . length ; i ++ ) {
6300+ const l = rds [ i ] . length ;
6301+ for ( let j = 0 ; j < l ; j ++ ) {
6302+ if ( rds [ i ] [ j ] !== buf [ pos + j ] ) {
6303+ continue loop;
6304+ }
62946305 }
6295- } else if ( chr === nl ) {
6296- this . options . record_delimiter . push ( Buffer . from ( '\n' , encoding ) ) ;
6297- this . state . recordDelimiterMaxLength = 1 ;
6298- return 1 ;
6306+ this . options . record_delimiter . push ( rds [ i ] ) ;
6307+ this . state . recordDelimiterMaxLength = rds [ i ] . length ;
6308+ return rds [ i ] . length ;
62996309 }
63006310 return 0 ;
63016311 } ,
0 commit comments