UNPKG

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