UNPKG

cabbie-sync

Version:
185 lines (140 loc) 6.5 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.CallEvent = undefined; var _keys = require('babel-runtime/core-js/object/keys'); var _keys2 = _interopRequireDefault(_keys); var _stringify = require('babel-runtime/core-js/json/stringify'); var _stringify2 = _interopRequireDefault(_stringify); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); exports.startBufferingLogs = startBufferingLogs; exports.discardBufferedLogs = discardBufferedLogs; exports.printBufferedLogs = printBufferedLogs; var _util = require('util'); var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _ms = require('ms'); var _ms2 = _interopRequireDefault(_ms); var _options = require('./flow-types/options'); var _flowRuntime = require('flow-runtime'); var _flowRuntime2 = _interopRequireDefault(_flowRuntime); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var Options = _flowRuntime2.default.tdz(function () { return _options.Options; }); /** * * This file is generated automatically, run npm run build to re-generate. **/ var CallEvent = exports.CallEvent = _flowRuntime2.default.type('CallEvent', _flowRuntime2.default.object(_flowRuntime2.default.property('obj', _flowRuntime2.default.object()), _flowRuntime2.default.property('name', _flowRuntime2.default.string()), _flowRuntime2.default.property('args', _flowRuntime2.default.array(_flowRuntime2.default.any())), _flowRuntime2.default.property('duration', _flowRuntime2.default.number()), _flowRuntime2.default.property('success', _flowRuntime2.default.boolean()), _flowRuntime2.default.property('result', _flowRuntime2.default.any()), _flowRuntime2.default.property('err', _flowRuntime2.default.any()))); function stringFill(filler /*: string*/, length /*: number*/) /*: string*/ { var _fillerType = _flowRuntime2.default.string(); var _lengthType = _flowRuntime2.default.number(); var _returnType = _flowRuntime2.default.return(_flowRuntime2.default.string()); _flowRuntime2.default.param('filler', _fillerType).assert(filler); _flowRuntime2.default.param('length', _lengthType).assert(length); var result = ''; for (var i = 0; i < length; i++) { result += filler; } return _returnType.assert(result); } _flowRuntime2.default.annotate(stringFill, _flowRuntime2.default.function(_flowRuntime2.default.param('filler', _flowRuntime2.default.string()), _flowRuntime2.default.param('length', _flowRuntime2.default.number()), _flowRuntime2.default.return(_flowRuntime2.default.string()))); var logBufferStack = _flowRuntime2.default.array(_flowRuntime2.default.array(_flowRuntime2.default.object(_flowRuntime2.default.property('d', _flowRuntime2.default.ref(_flowRuntime2.default.tdz(function () { return Debug; }))), _flowRuntime2.default.property('e', CallEvent)))).assert([]); var Debug = function () { function Debug(options /*: Options*/) { (0, _classCallCheck3.default)(this, Debug); var _optionsType = _flowRuntime2.default.ref(Options); _flowRuntime2.default.param('options', _optionsType).assert(options); this.options = options; } (0, _createClass3.default)(Debug, [{ key: '_getIndentation', value: function _getIndentation(add /*: number*/) /*: string*/ { var _addType = _flowRuntime2.default.number(); var _returnType2 = _flowRuntime2.default.return(_flowRuntime2.default.string()); _flowRuntime2.default.param('add', _addType).assert(add); return _returnType2.assert(stringFill(' ', (this.indentation + add) * 2)); } }, { key: 'onRequest', value: function onRequest(req /*: Object*/) { var _reqType = _flowRuntime2.default.object(); _flowRuntime2.default.param('req', _reqType).assert(req); if (this.options.httpDebug) { console.log(this._getIndentation(1) + 'Request: ', (0, _stringify2.default)(req).substr(0, 5000)); } if (this.options.onRequest) { this.options.onRequest(req); } } }, { key: 'onResponse', value: function onResponse(res /*: Object*/) { var _resType = _flowRuntime2.default.object(); _flowRuntime2.default.param('res', _resType).assert(res); if (this.options.httpDebug) { var copy = _flowRuntime2.default.object().assert({}); (0, _keys2.default)(res).forEach(function (key) { copy[key] = res[key]; }); copy.body = res.body.toString('utf8'); console.log(this._getIndentation(1) + 'Response: ', (0, _stringify2.default)(copy).substr(0, 5000)); } if (this.options.onResponse) { this.options.onResponse(res); } } }, { key: 'onCall', value: function onCall(event /*: CallEvent*/) { var _eventType = CallEvent; _flowRuntime2.default.param('event', _eventType).assert(event); if (logBufferStack.length) { return logBufferStack[logBufferStack.length - 1].push({ d: this, e: event }); } if (this.options.debug && event.name !== 'requestJSON') { var message = (event.success ? _chalk2.default.magenta(' • ') : _chalk2.default.red(' ✗ ')) + (0, _util.inspect)(event.obj, { colors: true, depth: 1 }) + '.' + event.name + '(' + event.args.map(function (v) { return (0, _util.inspect)(v, { colors: true, depth: 1 }); }).join(', ') + ')'; if (event.success && event.result !== undefined) { message += ' => ' + (0, _util.inspect)(event.result, { colors: true, depth: 1 }); } else if (!event.success) { message += ' => ' + _chalk2.default.red('' + (event.err.message || event.err)); } message += _chalk2.default.cyan(' (' + (0, _ms2.default)(event.duration) + ')'); console.log(message); } if (this.options.onCall) { this.options.onCall(event); } } }]); return Debug; }(); function startBufferingLogs() { logBufferStack.push([]); } function discardBufferedLogs() { logBufferStack.pop(); } function printBufferedLogs() { var logs = logBufferStack.pop(); logs.forEach(function (_ref) { var d = _ref.d, e = _ref.e; d.onCall(e); }); } exports.default = Debug;