|
| 1 | +// Copyright 2018 Sourcerer Inc. All Rights Reserved. |
| 2 | +// Author: Alexander Surkov ([email protected]) |
| 3 | + |
| 4 | +(function(exports) { |
| 5 | + |
| 6 | +/** |
| 7 | + * Elastic LogStash logging. |
| 8 | + */ |
| 9 | +class LogEvent |
| 10 | +{ |
| 11 | + constructor(url, { headers }) |
| 12 | + { |
| 13 | + console.assert(url); |
| 14 | + console.assert(headers); |
| 15 | + |
| 16 | + this.url = url; |
| 17 | + if (headers['set-cookie']) { |
| 18 | + let value = headers['set-cookie']; |
| 19 | + let parts = value.split(';').filter((v) => !!v.trim()); |
| 20 | + let cookiePart = parts[0].split('='); |
| 21 | + this.session = cookiePart[1]; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Logs an error. |
| 27 | + */ |
| 28 | + error(message) |
| 29 | + { |
| 30 | + this.event('error', { message }); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Logs an event of the given type. |
| 35 | + */ |
| 36 | + event(type, fields) |
| 37 | + { |
| 38 | + let data = Object.assign({ |
| 39 | + session: this.session, |
| 40 | + type, |
| 41 | + timestamp: Date.now() |
| 42 | + }, fields); |
| 43 | + |
| 44 | + let thisobj = this; |
| 45 | + return new Promise((resolve, reject) => { |
| 46 | + ('XMLHttpRequest' in exports) ? |
| 47 | + thisobj.browserRequest(data, resolve, reject) : |
| 48 | + thisobj.nodeRequest(data, resolve, reject); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + nodeRequest(data, resolve, reject) |
| 53 | + { |
| 54 | + let params = encodeURIComponent(JSON.stringify(data)); |
| 55 | + |
| 56 | + let url = new URL(this.url); |
| 57 | + let protocol = url.protocol.replace(':', ''); |
| 58 | + if (protocol != 'http' && protocol != 'https') { |
| 59 | + reject({ error: 'Unsupported protocol' }); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const options = { |
| 64 | + hostname: url.hostname, |
| 65 | + port: url.port, |
| 66 | + path: url.pathname, |
| 67 | + method, |
| 68 | + headers: { |
| 69 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 70 | + }, |
| 71 | + agent: keepAliveAgent, |
| 72 | + timeout: 1000, |
| 73 | + checkServerIdentity: function (host, cert) { |
| 74 | + return undefined; |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + const req = require(protocol).request(options, (res) => { |
| 79 | + if (res.statusCode != 200) { |
| 80 | + reject({ |
| 81 | + error: `HTTP error`, |
| 82 | + status: res.statusCode |
| 83 | + }); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + let stream = new require('stream').Transform(); |
| 88 | + res.on('data', (chunk) => { |
| 89 | + stream.push(chunk); |
| 90 | + }); |
| 91 | + res.on('end', () => { |
| 92 | + resolve(new Buffer(stream.read())); |
| 93 | + }); |
| 94 | + }); |
| 95 | + req.on('error', (e) => { |
| 96 | + reject({ |
| 97 | + error: `HTTP error`, |
| 98 | + status: e |
| 99 | + }); |
| 100 | + }); |
| 101 | + req.end(); |
| 102 | + } |
| 103 | + |
| 104 | + browserRequest(data, resolve, reject) |
| 105 | + { |
| 106 | + const req = new XMLHttpRequest(); |
| 107 | + req.open('POST', this.url, true); |
| 108 | + |
| 109 | + // Send the proper header information along with the request. |
| 110 | + req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); |
| 111 | + req.withCredentials = true; |
| 112 | + req.onreadystatechange = () => { |
| 113 | + if (req.readyState == 4 && req.status == 200) { |
| 114 | + resolve(req.responseText); |
| 115 | + } |
| 116 | + else { |
| 117 | + reject({ |
| 118 | + error: `HTTP error`, |
| 119 | + status: req.status |
| 120 | + }); |
| 121 | + } |
| 122 | + } |
| 123 | + req.send(encodeURIComponent(JSON.stringify(data))); |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +exports.LogEvent = LogEvent; |
| 128 | + |
| 129 | +})(typeof exports === 'undefined'? this : exports); |
0 commit comments