1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports["default"] = exports.Proxy = void 0;
|
8 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
9 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
10 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
11 | var _avg = _interopRequireDefault(require("@lskjs/utils/avg"));
|
12 | var _inc = _interopRequireDefault(require("@lskjs/utils/inc"));
|
13 | var _events = _interopRequireDefault(require("events"));
|
14 | var _pick = _interopRequireDefault(require("lodash/pick"));
|
15 | var _createKey = require("./utils/createKey");
|
16 | var _createUri = require("./utils/createUri");
|
17 | var _getIpv = require("./utils/getIpv");
|
18 | var _getProxyAgent = require("./utils/getProxyAgent");
|
19 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
20 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
21 | var Proxy = function () {
|
22 | function Proxy() {
|
23 | var _this$tags;
|
24 | var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
25 | (0, _classCallCheck2["default"])(this, Proxy);
|
26 | this.stats = {};
|
27 | Object.assign(this, props);
|
28 | if (!this.key) this.key = this.getKey();
|
29 | if (!this.uri) this.uri = this.getUri();
|
30 | if (!((_this$tags = this.tags) !== null && _this$tags !== void 0 && _this$tags.ipv) && this.ip) {
|
31 | if (!this.tags) this.tags = {};
|
32 | this.tags.ipv = (0, _getIpv.getIpv)(this.ip);
|
33 | }
|
34 | this.ee = new _events["default"]();
|
35 | }
|
36 | (0, _createClass2["default"])(Proxy, [{
|
37 | key: "getState",
|
38 | value: function getState() {
|
39 |
|
40 | return _objectSpread(_objectSpread({}, (0, _pick["default"])(this, ['type', 'host', 'port', 'user', 'password', 'provider', 'ip', 'tests', 'targets'])), {}, {
|
41 |
|
42 | tags: _objectSpread({}, this.tags),
|
43 | key: this.getKey(),
|
44 | uri: this.getUri()
|
45 | });
|
46 | }
|
47 | }, {
|
48 | key: "toObject",
|
49 | value: function toObject() {
|
50 | return this.getState();
|
51 | }
|
52 | }, {
|
53 | key: "toJSON",
|
54 | value: function toJSON() {
|
55 | return this.getState();
|
56 | }
|
57 | }, {
|
58 | key: "getJson",
|
59 | value: function getJson() {
|
60 | return this.getState();
|
61 | }
|
62 | }, {
|
63 | key: "getUrl",
|
64 | value: function getUrl() {
|
65 | return this.getUri();
|
66 | }
|
67 | }, {
|
68 | key: "getUri",
|
69 | value: function getUri() {
|
70 | if (this.provider === 'localhost') return null;
|
71 | if (this.uri) return this.uri;
|
72 | return (0, _createUri.createUri)(this);
|
73 | }
|
74 | }, {
|
75 | key: "getKey",
|
76 | value: function getKey() {
|
77 | if (this.provider === 'localhost') return 'localhost';
|
78 | return (0, _createKey.createKey)(this);
|
79 | }
|
80 | }, {
|
81 | key: "getOptions",
|
82 | value: function getOptions() {
|
83 | (0, _inc["default"])(this.stats, 'get');
|
84 | var proxy = {
|
85 | host: this.host,
|
86 | port: this.port
|
87 | };
|
88 | if (this.user && this.password) {
|
89 | proxy.auth = {
|
90 | username: this.user,
|
91 | password: this.password
|
92 | };
|
93 | }
|
94 | return proxy;
|
95 | }
|
96 | }, {
|
97 | key: "getAgent",
|
98 | value: function getAgent() {
|
99 | (0, _inc["default"])(this.stats, 'get');
|
100 | if (this.provider === 'localhost') return null;
|
101 | return (0, _getProxyAgent.getProxyAgent)(this);
|
102 | }
|
103 | }, {
|
104 | key: "getProviderOptions",
|
105 | value: function getProviderOptions() {
|
106 | (0, _inc["default"])(this.stats, 'get');
|
107 |
|
108 | if (this.provider === 'localhost') return {};
|
109 | var httpsAgent = (0, _getProxyAgent.getProxyAgent)(this);
|
110 | return {
|
111 | httpsAgent: httpsAgent,
|
112 | httpAgent: httpsAgent
|
113 | };
|
114 | }
|
115 | }, {
|
116 | key: "emit",
|
117 | value: function emit() {
|
118 | var _this$ee;
|
119 | (_this$ee = this.ee).emit.apply(_this$ee, arguments);
|
120 | }
|
121 | }, {
|
122 | key: "on",
|
123 | value: function on() {
|
124 | var _this$ee2;
|
125 | (_this$ee2 = this.ee).on.apply(_this$ee2, arguments);
|
126 | }
|
127 | }, {
|
128 | key: "feedback",
|
129 | value: function feedback() {
|
130 | var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
131 | var _props$status = props.status,
|
132 | status = _props$status === void 0 ? 'unknown' : _props$status,
|
133 | err = props.err,
|
134 | time = props.time,
|
135 | fatal = props.fatal;
|
136 | (0, _inc["default"])(this.stats, 'count');
|
137 | (0, _inc["default"])(this.stats, 'time', time);
|
138 | (0, _avg["default"])(this.stats, "statuses.".concat(status), time);
|
139 | if (fatal) (0, _inc["default"])(this.stats, 'fatal');
|
140 | if (err) (0, _avg["default"])(this.stats, "errors.".concat(err), time);
|
141 | this.emit('feedback', props, this);
|
142 | }
|
143 | }]);
|
144 | return Proxy;
|
145 | }();
|
146 | exports.Proxy = Proxy;
|
147 | var _default = Proxy;
|
148 | exports["default"] = _default;
|
149 |
|
\ | No newline at end of file |