@@ -6,8 +6,8 @@ const RXS = {
6
6
"pri" : / ^ < \d + > / ,
7
7
"prinmr" : / ^ \d + / ,
8
8
"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 } / ,
11
11
"time" : / ^ \d + : \d + : \d + / ,
12
12
"ts" : / ^ \d { 4 } - \d { 2 } - \d { 2 } T \d { 2 } : \d { 2 } : \S + / ,
13
13
"invalid" : / [ ^ a - z A - Z 0 - 9 \. \$ \- _ # % \/ \[ \] \( \) ] / ,
@@ -24,7 +24,12 @@ const DOPS = {
24
24
generateTimestamp : true
25
25
}
26
26
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 ) {
28
33
do {
29
34
var item = arr . shift ( ) ;
30
35
if ( item === undefined ) return item ;
@@ -34,6 +39,21 @@ function peek(arr) {
34
39
return item ;
35
40
}
36
41
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
+
37
57
function assign ( entry , item ) {
38
58
if ( ! entry . host ) entry . host = item . trim ( ) ;
39
59
else if ( ! entry . appName ) entry . appName = item . trim ( ) ;
@@ -78,33 +98,34 @@ function parse(line,opts) {
78
98
// Date search
79
99
var endparse = false ;
80
100
while ( line . length && ! endparse ) {
81
- var item = peek ( items ) + " " ;
101
+ var item = shiftItem ( items ) + " " ;
102
+ var nextItem = peekItem ( items ) ;
82
103
83
104
// RFC RFC5424
84
105
if ( item . match ( RXS . prinmr ) ) {
85
106
entry . version = parseInt ( item ) ;
86
107
entry . type = "RFC5424" ;
87
- item = peek ( items ) + " " ;
108
+ item = shiftItem ( items ) + " " ;
88
109
if ( item . match ( RXS . ts ) ) {
89
110
entry . ts = new Date ( Date . parse ( item . match ( RXS . ts ) [ 0 ] . trim ( ) ) ) ;
90
111
}
91
112
}
92
113
// BSD
93
- else if ( item . match ( RXS . month ) ) {
114
+ else if ( item . match ( RXS . month ) && nextItem && nextItem . match ( RXS . day ) ) {
94
115
entry . type = "BSD" ;
95
116
const month = item . trim ( ) ;
96
- const day = peek ( items ) ;
97
- let time = peek ( items ) ;
117
+ const day = shiftItem ( items ) ;
118
+ let time = shiftItem ( items ) ;
98
119
let year = new Date ( ) . getYear ( ) + 1900
99
120
let timezone = "" ;
100
121
// Check if the time is actually a year field and it is in the form "MMM dd yyyy HH:mm:ss"
101
122
if ( time . length === 4 && ! Number . isNaN ( + time ) ) {
102
123
year = + time ;
103
- time = peek ( items ) ;
124
+ time = shiftItem ( items ) ;
104
125
}
105
126
// Check if we have a timezone
106
127
if ( isValidTimeZone ( items [ 0 ] . trim ( ) ) ) {
107
- timezone = peek ( items ) ;
128
+ timezone = shiftItem ( items ) ;
108
129
}
109
130
110
131
entry . ts = new Date ( Date . parse ( `${ year } ${ month } ${ day } ${ time } ${ timezone } ` . trim ( ) ) ) ;
@@ -130,7 +151,7 @@ function parse(line,opts) {
130
151
}
131
152
132
153
while ( line . length && ! endparse ) {
133
- var item = peek ( items ) ;
154
+ var item = shiftItem ( items ) ;
134
155
if ( ! item ) {
135
156
endparse = true ;
136
157
}
0 commit comments