-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
142 lines (109 loc) · 3.99 KB
/
index.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
'use strict';
const stream = require('stream');
const util = require('util');
const formats = {
'web_v1.0': [
'date', 'time', 'x-edge-location', 'sc-bytes', 'c-ip',
'cs-method', 'cs-host', 'cs-uri-stem', 'sc-status',
'cs-referer', 'cs-user-agent', 'cs-uri-query', 'cs-cookie',
'x-edge-result-type', 'x-edge-request-id', 'x-host-header',
'cs-protocol', 'cs-bytes', 'time-taken', 'x-forwarded-for',
'ssl-protocol', 'ssl-cipher', 'x-edge-response-result-type',
'cs-protocol-version', 'fle-status', 'fle-encrypted-fields',
'c-port', 'time-to-first-byte', 'x-edge-detailed-result-type',
'sc-content-type', 'sc-content-len', 'sc-range-start',
'sc-range-end'
],
'rtmp_v1.0': [
'date', 'time', 'x-edge-location', 'c-ip', 'x-event',
'sc-bytes', 'x-cf-status', 'x-cf-client-id', 'cs-uri-stem',
'cs-uri-query', 'c-referrer', 'x-page-url', 'c-user-agent',
'x-sname', 'x-sname-query', 'x-file-ext', 'x-sid'
],
'kinesis_v1.0': [
'timestamp', 'c-ip', 'time-to-first-byte', 'sc-status',
'sc-bytes', 'cs-method', 'cs-protocol', 'cs-host',
'cs-uri-stem', 'cs-bytes', 'x-edge-location', 'x-edge-request-id',
'x-host-header', 'time-taken', 'cs-protocol-version', 'c-ip-version',
'cs-user-agent', 'cs-referer', 'cs-cookie', 'cs-uri-query',
'x-edge-response-result-type', 'x-forwarded-for', 'ssl-protocol', 'ssl-cipher',
'x-edge-result-type', 'fle-encrypted-fields', 'fle-status', 'sc-content-type',
'sc-content-len', 'sc-range-start', 'sc-range-end', 'c-port',
'x-edge-detailed-result-type', 'c-country', 'cs-accept-encoding', 'cs-accept',
'cache-behavior-path-pattern', 'cs-headers', 'cs-header-names', 'cs-headers-count'
]
};
const option_defaults = {
format: 'web',
version: '1.0'
};
const decode_field = val => {
return unescape(
val.replace(/%2522/g, '"')
.replace(/%255C/g, '\\')
.replace(/%2520/g, ' ')
);
};
const CloudFrontTransform = function (options) {
this.options = options || {};
stream.Transform.call(this, { objectMode: true });
};
util.inherits(CloudFrontTransform, stream.Transform);
CloudFrontTransform.prototype._transform = function (chunk, encoding, done) {
encoding = encoding || 'utf8';
if (Buffer.isBuffer(chunk)) {
if (encoding == 'buffer') chunk = chunk.toString();
else chunk = chunk.toString(encoding);
}
if (this._lastLineBuffer) chunk = this._lastLineBuffer + chunk;
const lines = chunk.split('\n');
this._lastLineBuffer = lines.splice(lines.length - 1, 1)[0];
parse(lines, this.options).forEach(this.push.bind(this));
done();
};
CloudFrontTransform.prototype._flush = function (done) {
if (this._lastLineBuffer) {
parse(this._lastLineBuffer, this.options).forEach(this.push.bind(this));
this._lastLineBuffer = null;
}
done();
};
function parse(data, options, callback) {
let parsed;
let err;
try {
if (Buffer.isBuffer(data)) data = data.toString();
if (typeof data === 'string') data = data.split('\n');
parsed = data
.filter(line => !line.startsWith('#'))
.filter(line => line.length > 0)
.map(line => parseLine(line, options));
} catch (e) {
err = e;
if (!callback) throw e;
}
if (callback) callback(err, parsed);
else return parsed;
}
function parseLine(line, options) {
options = options || {};
let format = options.format;
if (format === undefined) format = option_defaults.format;
let version = options.version;
if (version === undefined) version = option_defaults.version;
const headings = formats[`${format}_v${version}`];
if (!headings) throw new Error(`Format not recognized: ${format}`);
const line_arr = line.split('\t');
return _zipLine(line_arr, headings);
}
function _zipLine(arr, headings) {
const result = {};
for (let i = 0; i < Math.min(arr.length, headings.length); i++) {
const field = headings[i];
result[field] = decode_field(arr[i]);
}
return result;
}
module.exports = CloudFrontTransform;
module.exports.parse = parse;
module.exports.parseLine = parseLine;