-
Notifications
You must be signed in to change notification settings - Fork 6
/
alpine.js
210 lines (182 loc) · 5.23 KB
/
alpine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
*
* Alpine, the Apache Log Parser
*
* Created by blarsen on 02.10.14.
*/
"use strict";
var Buffer = require('./buffer');
var byline = require('byline');
var _ = require("underscore.string");
var through2 = require('through2');
var Alpine = function (logformat) {
this.setLogFormat = setLogFormat;
this.getLogFormat = getLogFormat;
this.parseLine = parseLine;
this.getObjectStream = getObjectStream;
this.getStringStream = getStringStream;
this.parseReadStream = parseReadStream;
if (logformat)
this.setLogFormat(logformat);
else
this.setLogFormat(Alpine.LOGFORMATS.COMBINED);
};
function getObjectStream() {
var thisAlpine = this;
return through2.obj(function(chunk, enc, callback) {
var data = thisAlpine.parseLine(chunk);
this.push(data);
callback();
});
}
function getStringStream() {
var thisAlpine = this;
return through2.obj(function(chunk, enc, callback) {
var data = thisAlpine.parseLine(chunk);
this.push(JSON.stringify(data));
callback();
});
}
function parseReadStream(stream, callback) {
var thisAlpine = this;
var lineStream = byline.createStream(stream);
lineStream.pipe(through2.obj(function(chunk, enc, t2callback) {
var data = thisAlpine.parseLine(chunk.toString());
callback(data);
t2callback();
}))
}
function getLogFormat() {
return this.logformat;
}
function setLogFormat(logformat) {
this.logformat = logformat;
this.formatfields = parseLogFormat(logformat);
}
function parseLine(line) {
var result = {
originalLine: line
};
var buf = new Buffer(line, 0);
this.formatfields.forEach(function(field) {
buf.skipSpaces();
var val;
if (field.isQuoted) {
if (!(buf.lookingAt() === '"'))
throw new Error("Field defined as quoted was not quoted");
buf.skip();
val = buf.getUpto('"');
buf.skip(1);
buf.skipSpaces();
} else if (field.isDate) {
if (!(buf.lookingAt() === '['))
throw new Error("Time field is not enclosed in brackets");
buf.skip();
val = buf.getUpto(']');
buf.skip(1);
buf.skipSpaces();
} else {
val = buf.getUpto(' ');
}
result[field.name] = val;
});
return result;
}
function parseLogFormat(logformat) {
var fields = [];
var buf = new Buffer(logformat, 0);
while (buf.hasMore()) {
buf.skipSpaces();
var field = buf.getUpto(" ");
var isQuoted = field[0] === '"';
field = stripQuotes(field);
// Check that this is a field definition (starting with %) and remove the prefix
if (!(field[0] === "%"))
throw new Error("Field does not start with %: "+field);
field = field.substring(1);
// Remove modifiers
if (field.indexOf("{") > 0) {
field = field.replace(/^[0-9!]+/, "");
}
field = field.replace(/[<>]/g, "");
var fieldName = FIELDS[field];
var fieldLetter = field;
// Handle parameterized fields
if (field.indexOf('{') >= 0) {
var matches = (/{(.*)}(.*)/).exec(field);
var value = matches[1];
fieldLetter = matches[2];
if (!PARAMFIELDS[fieldLetter])
throw new Error("The field "+fieldLetter+" should not be parameterized");
fieldName = PARAMFIELDS[fieldLetter] + ' ' + value;
}
if (!FIELDS[fieldLetter])
throw new Error("Unknown log format field "+fieldLetter);
fields.push({
field: field,
name: fieldName,
isQuoted: isQuoted,
isDate: fieldLetter === 't'
});
}
return fields;
}
function stripQuotes(text) {
if ((_.startsWith(text, '"') && _.endsWith(text, '"'))
|| (_.startsWith(text, '[')) && _.endsWith(text, ']'))
return text.substr(1, text.length-2);
return text;
}
Alpine.LOGFORMATS = {
COMBINED: "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"",
CLF: "%h %l %u %t \"%r\" %>s %b",
CLF_VHOST: "%v %h %l %u %t \"%r\" %>s %b"
};
var FIELDS = {
'a': 'remoteIP',
'A': 'localIP',
'B': 'size',
'b': 'sizeCLF',
'D': 'serveTime',
'f': 'filename',
'h': 'remoteHost',
'H': 'requestProtocol',
'k': 'keepaliveRequests',
'l': 'logname',
'm': 'requestMethod',
'p': 'port',
'P': 'pid',
'q': 'queryString',
'r': 'request',
'R': 'responseHandler',
's': 'status',
't': 'time',
'T': 'serveTime',
'u': 'remoteUser',
'U': 'urlPath',
'v': 'canonicalServerName',
'V': 'serverName',
'X': 'connectionStatus',
'I': 'bytesReceived',
'O': 'bytesSent',
'C': 'cookie',
'e': 'environment',
'i': 'requestHeader',
'n': 'note',
'o': 'responseHeader',
'^ti': 'requestTrailerLine',
'^to': 'responseTrailerLine'
};
var PARAMFIELDS = {
"c": "Cookie",
"e": "Environment",
"i": "RequestHeader",
"n": "Note",
"o": "ResponseHeader",
"p": "Port",
"P": "PID",
"t": "Time",
'^ti': 'RequestTrailerLine',
'^to': 'ResponseTrailerLine'
};
module.exports = Alpine;