@@ -6,8 +6,8 @@ const RXS = {
66 "pri" : / ^ < \d + > / ,
77 "prinmr" : / ^ \d + / ,
88 "prival" : / < ( \d + ) > / ,
9- "month" : / ^ [ A - Z a - z ] [ a - z ] { 2 } / ,
10- "day" : / ^ \d { 1 , 2 } / ,
9+ "month" : / ^ [ A - Z a - z ] { 3 } / ,
10+ "day" : / ^ \d { 1 , 2 } / ,
1111 "time" : / ^ \d + : \d + : \d + / ,
1212 "ts" : / ^ \d { 4 } - \d { 2 } - \d { 2 } T \d { 2 } : \d { 2 } : \S + / ,
1313 "invalid" : / [ ^ a - z A - Z 0 - 9 \. \$ \- _ # % \/ \[ \] \( \) ] / ,
@@ -24,7 +24,12 @@ const DOPS = {
2424 generateTimestamp : true
2525}
2626
27- function peek ( arr ) {
27+ /**
28+ * Removes the first non whitespace item from the array and returns the item
29+ * @param {string[] } arr the array to shift the item from
30+ * @returns the first non whitespace item of the array
31+ */
32+ function shiftItem ( arr ) {
2833 do {
2934 var item = arr . shift ( ) ;
3035 if ( item === undefined ) return item ;
@@ -34,6 +39,21 @@ function peek(arr) {
3439 return item ;
3540}
3641
42+ /**
43+ * Gets the first non whitespace item from the array without mutating the array
44+ * @param {string[] } arr the array to peek for the first item
45+ * @returns the first non whitespace item of the array
46+ */
47+ function peekItem ( arr ) {
48+ for ( const item of arr ) {
49+ let trimmedItem = item . trim ( ) ;
50+ if ( trimmedItem ) {
51+ return trimmedItem ;
52+ }
53+ }
54+ return undefined ;
55+ }
56+
3757function assign ( entry , item ) {
3858 if ( ! entry . host ) entry . host = item . trim ( ) ;
3959 else if ( ! entry . appName ) entry . appName = item . trim ( ) ;
@@ -78,33 +98,34 @@ function parse(line,opts) {
7898 // Date search
7999 var endparse = false ;
80100 while ( line . length && ! endparse ) {
81- var item = peek ( items ) + " " ;
101+ var item = shiftItem ( items ) + " " ;
102+ var nextItem = peekItem ( items ) ;
82103
83104 // RFC RFC5424
84105 if ( item . match ( RXS . prinmr ) ) {
85106 entry . version = parseInt ( item ) ;
86107 entry . type = "RFC5424" ;
87- item = peek ( items ) + " " ;
108+ item = shiftItem ( items ) + " " ;
88109 if ( item . match ( RXS . ts ) ) {
89110 entry . ts = new Date ( Date . parse ( item . match ( RXS . ts ) [ 0 ] . trim ( ) ) ) ;
90111 }
91112 }
92113 // BSD
93- else if ( item . match ( RXS . month ) ) {
114+ else if ( item . match ( RXS . month ) && nextItem && nextItem . match ( RXS . day ) ) {
94115 entry . type = "BSD" ;
95116 const month = item . trim ( ) ;
96- const day = peek ( items ) ;
97- let time = peek ( items ) ;
117+ const day = shiftItem ( items ) ;
118+ let time = shiftItem ( items ) ;
98119 let year = new Date ( ) . getYear ( ) + 1900
99120 let timezone = "" ;
100121 // Check if the time is actually a year field and it is in the form "MMM dd yyyy HH:mm:ss"
101122 if ( time . length === 4 && ! Number . isNaN ( + time ) ) {
102123 year = + time ;
103- time = peek ( items ) ;
124+ time = shiftItem ( items ) ;
104125 }
105126 // Check if we have a timezone
106127 if ( isValidTimeZone ( items [ 0 ] . trim ( ) ) ) {
107- timezone = peek ( items ) ;
128+ timezone = shiftItem ( items ) ;
108129 }
109130
110131 entry . ts = new Date ( Date . parse ( `${ year } ${ month } ${ day } ${ time } ${ timezone } ` . trim ( ) ) ) ;
@@ -130,7 +151,7 @@ function parse(line,opts) {
130151 }
131152
132153 while ( line . length && ! endparse ) {
133- var item = peek ( items ) ;
154+ var item = shiftItem ( items ) ;
134155 if ( ! item ) {
135156 endparse = true ;
136157 }
0 commit comments