|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const stream = require('stream'); |
| 4 | +const util = require('util'); |
| 5 | + |
| 6 | +const formats = { |
| 7 | + 'web_v1.0': [ |
| 8 | + 'date', 'time', 'x-edge-location', 'sc-bytes', 'c-ip', |
| 9 | + 'cs-method', 'cs-host', 'cs-uri-stem', 'sc-status', |
| 10 | + 'cs-referer', 'cs-user-agent', 'cs-uri-query', 'cs-cookie', |
| 11 | + 'x-edge-result-type', 'x-edge-request-id', 'x-host-header', |
| 12 | + 'cs-protocol', 'cs-bytes', 'time-taken', 'x-forwarded-for', |
| 13 | + 'ssl-protocol', 'ssl-cipher', 'x-edge-response-result-type', |
| 14 | + 'cs-protocol-version' |
| 15 | + ], |
| 16 | + |
| 17 | + 'rtmp_v1.0': [ |
| 18 | + 'date', 'time', 'x-edge-location', 'c-ip', 'x-event', |
| 19 | + 'sc-bytes', 'x-cf-status', 'x-cf-client-id', 'cs-uri-stem', |
| 20 | + 'cs-uri-query', 'c-referrer', 'x-page-url', 'c-user-agent' |
| 21 | + ] |
| 22 | +}; |
| 23 | + |
| 24 | +const option_defaults = { |
| 25 | + format: 'web', |
| 26 | + version: '1.0' |
| 27 | +}; |
| 28 | + |
| 29 | +const decode_field = val => { |
| 30 | + return decodeURIComponent( |
| 31 | + val.replace(/%2522/g, '"') |
| 32 | + .replace(/%255C/g, '\\') |
| 33 | + .replace(/%2520/g, ' ') |
| 34 | + ); |
| 35 | +}; |
| 36 | + |
| 37 | +const CloudFrontTransform = function (options) { |
| 38 | + |
| 39 | + this.options = options || {}; |
| 40 | + stream.Transform.call(this, { objectMode: true }); |
| 41 | +}; |
| 42 | +util.inherits(CloudFrontTransform, stream.Transform); |
| 43 | + |
| 44 | +CloudFrontTransform.prototype._transform = function (chunk, encoding, done) { |
| 45 | + |
| 46 | + encoding = encoding || 'utf8'; |
| 47 | + if (Buffer.isBuffer(chunk)) { |
| 48 | + if (encoding == 'buffer') chunk = chunk.toString(); |
| 49 | + else chunk = chunk.toString(encoding); |
| 50 | + } |
| 51 | + |
| 52 | + if (this._lastLineBuffer) chunk = this._lastLineBuffer + chunk; |
| 53 | + |
| 54 | + const lines = chunk.split('\n'); |
| 55 | + this._lastLineBuffer = lines.splice(lines.length - 1, 1)[0]; |
| 56 | + |
| 57 | + parse(lines, this.options).forEach(this.push.bind(this)); |
| 58 | + done(); |
| 59 | +}; |
| 60 | + |
| 61 | +CloudFrontTransform.prototype._flush = function (done) { |
| 62 | + |
| 63 | + if (this._lastLineBuffer) { |
| 64 | + parse(this._lastLineBuffer, this.options).forEach(this.push.bind(this)); |
| 65 | + this._lastLineBuffer = null; |
| 66 | + } |
| 67 | + |
| 68 | + done(); |
| 69 | +}; |
| 70 | + |
| 71 | +function parse(data, options, callback) { |
| 72 | + |
| 73 | + let parsed; |
| 74 | + let err; |
| 75 | + try { |
| 76 | + |
| 77 | + if (Buffer.isBuffer(data)) data = data.toString(); |
| 78 | + |
| 79 | + if (typeof data === 'string') data = data.split('\n'); |
| 80 | + |
| 81 | + parsed = data |
| 82 | + .filter(line => !line.startsWith('#')) |
| 83 | + .filter(line => line.length > 0) |
| 84 | + .map(line => parseLine(line, options)); |
| 85 | + |
| 86 | + } catch (e) { |
| 87 | + err = e; |
| 88 | + if (!callback) throw e; |
| 89 | + } |
| 90 | + |
| 91 | + if (callback) callback(err, parsed); |
| 92 | + else return parsed; |
| 93 | +} |
| 94 | + |
| 95 | +function parseLine(line, options) { |
| 96 | + |
| 97 | + options = options || {}; |
| 98 | + |
| 99 | + let format = options.format; |
| 100 | + if (format === undefined) format = option_defaults.format; |
| 101 | + |
| 102 | + let version = options.version; |
| 103 | + if (version === undefined) version = option_defaults.version; |
| 104 | + |
| 105 | + const headings = formats[`${format}_v${version}`]; |
| 106 | + if (!headings) throw new Error(`Format not recognized: ${format}`); |
| 107 | + |
| 108 | + const line_arr = line.split('\t'); |
| 109 | + return _zipLine(line_arr, headings); |
| 110 | +} |
| 111 | + |
| 112 | +function _zipLine(arr, headings) { |
| 113 | + const result = {}; |
| 114 | + for (let i = 0; i < Math.min(arr.length, headings.length); i++) { |
| 115 | + const field = headings[i]; |
| 116 | + result[field] = decode_field(arr[i]); |
| 117 | + } |
| 118 | + |
| 119 | + return result; |
| 120 | +} |
| 121 | + |
| 122 | +module.exports = CloudFrontTransform; |
| 123 | +module.exports.parse = parse; |
| 124 | +module.exports.parseLine = parseLine; |
0 commit comments