UNPKG

databridge-logger

Version:

aggregate logs from all databridge clients

88 lines (70 loc) 4.85 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /** * @author Guillaume Leclerc <guillaume.leclerc.work@gmail.com> * */ var _net = require('net'); function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * * Backend made to talk directly to Spark Streaming */ var TCPBackend = function () { /** * Create a network Backend from a network url * * @param {string} hostname the host to contact * @param {number} port the port to connect to */ function TCPBackend(hostname, port) { _classCallCheck(this, TCPBackend); this.socket = new Promise(function (resolve, reject) { var client = new _net.Socket(); client.connect(port, hostname, function () { resolve(client); }); client.on('error', reject); }); } /** * Store a message (by sending it to the logging server) * * @param {Array<object>} messages an array of message or a single message * @return {Promise} promise resolved when it was sent to the logging server */ _createClass(TCPBackend, [{ key: 'store', value: function store(messages) { var _this = this; messages.forEach(function () { var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(message) { var client; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return _this.socket; case 2: client = _context.sent; client.write(JSON.stringify(message)); case 4: case 'end': return _context.stop(); } } }, _callee, _this); })); return function (_x) { return _ref.apply(this, arguments); }; }()); } }]); return TCPBackend; }(); exports.default = TCPBackend;