UNPKG

11.8 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports["default"] = void 0;
8var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
10var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
12var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
13var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
14var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
15var _err = _interopRequireDefault(require("@lskjs/err"));
16var _serverApi = _interopRequireDefault(require("@lskjs/server-api"));
17var _filterProxy = require("./utils/filterProxy");
18function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
19function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
20// import get from 'lodash/get';
21// import pick from 'lodash/pick';
22var ProxyApi = /*#__PURE__*/function (_BaseApi) {
23 (0, _inherits2["default"])(ProxyApi, _BaseApi);
24 var _super = _createSuper(ProxyApi);
25 function ProxyApi() {
26 (0, _classCallCheck2["default"])(this, ProxyApi);
27 return _super.apply(this, arguments);
28 }
29 (0, _createClass2["default"])(ProxyApi, [{
30 key: "getRoutes",
31 value: function getRoutes() {
32 return {
33 '/': this.list.bind(this),
34 '.:format': this.list.bind(this),
35 '/list': this.list.bind(this),
36 '/list.:format': this.list.bind(this),
37 '/tests': this.tests.bind(this),
38 '/test': this.test.bind(this),
39 '/buyList': this.buyList.bind(this),
40 '/:provider': this.list.bind(this),
41 '/:provider.:format': this.list.bind(this),
42 // '/buyList': () => this.app.buyList(),
43 // '/setActiveProviders': this.setActiveProviders.bind(this),
44 // '/:provider': this.providerGetList.bind(this),
45 '/:provider/list': this.list.bind(this),
46 '/:provider/list.:format': this.list.bind(this),
47 '/:provider/fetch': this.providerFetchList.bind(this),
48 '/:provider/fetchRaw': this.providerFetchListRaw.bind(this),
49 '/:provider/fetch/raw': this.providerFetchListRaw.bind(this),
50 '/:provider/raw': this.providerFetchListRaw.bind(this),
51 // '/:provider/fetch/raw': this.providerFetchListRaw.bind(this),
52 '/:provider/buy': this.providerBuyList.bind(this)
53 };
54 }
55 }, {
56 key: "providerFetchList",
57 value: function () {
58 var _providerFetchList = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(req) {
59 var provider;
60 return _regenerator["default"].wrap(function _callee$(_context) {
61 while (1) switch (_context.prev = _context.next) {
62 case 0:
63 _context.next = 2;
64 return this.app.module("proxy.providers.".concat(req.params.provider));
65 case 2:
66 provider = _context.sent;
67 if (provider) {
68 _context.next = 5;
69 break;
70 }
71 throw '!provider';
72 case 5:
73 return _context.abrupt("return", provider.fetchList());
74 case 6:
75 case "end":
76 return _context.stop();
77 }
78 }, _callee, this);
79 }));
80 function providerFetchList(_x) {
81 return _providerFetchList.apply(this, arguments);
82 }
83 return providerFetchList;
84 }()
85 }, {
86 key: "providerFetchListRaw",
87 value: function () {
88 var _providerFetchListRaw = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(req) {
89 var provider;
90 return _regenerator["default"].wrap(function _callee2$(_context2) {
91 while (1) switch (_context2.prev = _context2.next) {
92 case 0:
93 _context2.next = 2;
94 return this.app.module("proxy.providers.".concat(req.params.provider));
95 case 2:
96 provider = _context2.sent;
97 if (provider) {
98 _context2.next = 5;
99 break;
100 }
101 throw '!provider';
102 case 5:
103 return _context2.abrupt("return", provider.fetchListRaw());
104 case 6:
105 case "end":
106 return _context2.stop();
107 }
108 }, _callee2, this);
109 }));
110 function providerFetchListRaw(_x2) {
111 return _providerFetchListRaw.apply(this, arguments);
112 }
113 return providerFetchListRaw;
114 }()
115 }, {
116 key: "providerBuyList",
117 value: function () {
118 var _providerBuyList = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(req) {
119 var provider;
120 return _regenerator["default"].wrap(function _callee3$(_context3) {
121 while (1) switch (_context3.prev = _context3.next) {
122 case 0:
123 _context3.next = 2;
124 return this.app.module("proxy.providers.".concat(req.params.provider));
125 case 2:
126 provider = _context3.sent;
127 if (provider) {
128 _context3.next = 5;
129 break;
130 }
131 throw '!provider';
132 case 5:
133 return _context3.abrupt("return", provider.buyList(req.data));
134 case 6:
135 case "end":
136 return _context3.stop();
137 }
138 }, _callee3, this);
139 }));
140 function providerBuyList(_x3) {
141 return _providerBuyList.apply(this, arguments);
142 }
143 return providerBuyList;
144 }()
145 }, {
146 key: "buyList",
147 value: function () {
148 var _buyList = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(req) {
149 var proxy;
150 return _regenerator["default"].wrap(function _callee4$(_context4) {
151 while (1) switch (_context4.prev = _context4.next) {
152 case 0:
153 _context4.next = 2;
154 return this.app.module('proxy');
155 case 2:
156 proxy = _context4.sent;
157 return _context4.abrupt("return", proxy.buyList(req.data));
158 case 4:
159 case "end":
160 return _context4.stop();
161 }
162 }, _callee4, this);
163 }));
164 function buyList(_x4) {
165 return _buyList.apply(this, arguments);
166 }
167 return buyList;
168 }()
169 }, {
170 key: "tests",
171 value: function () {
172 var _tests = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(req) {
173 var proxy;
174 return _regenerator["default"].wrap(function _callee5$(_context5) {
175 while (1) switch (_context5.prev = _context5.next) {
176 case 0:
177 _context5.next = 2;
178 return this.app.module('proxy');
179 case 2:
180 proxy = _context5.sent;
181 return _context5.abrupt("return", proxy.runProxyTests(req.data.list));
182 case 4:
183 case "end":
184 return _context5.stop();
185 }
186 }, _callee5, this);
187 }));
188 function tests(_x5) {
189 return _tests.apply(this, arguments);
190 }
191 return tests;
192 }()
193 }, {
194 key: "test",
195 value: function () {
196 var _test = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(req) {
197 var proxy, _req$data, proxyKey, testId;
198 return _regenerator["default"].wrap(function _callee6$(_context6) {
199 while (1) switch (_context6.prev = _context6.next) {
200 case 0:
201 _context6.next = 2;
202 return this.app.module('proxy');
203 case 2:
204 proxy = _context6.sent;
205 _req$data = req.data, proxyKey = _req$data.proxyKey, testId = _req$data.testId;
206 if (proxyKey) {
207 _context6.next = 6;
208 break;
209 }
210 throw new _err["default"]('!proxyKey');
211 case 6:
212 if (testId) {
213 _context6.next = 8;
214 break;
215 }
216 throw new _err["default"]('!testId');
217 case 8:
218 return _context6.abrupt("return", proxy.runProxyTest({
219 proxyKey: proxyKey,
220 testId: testId,
221 force: true
222 }));
223 case 9:
224 case "end":
225 return _context6.stop();
226 }
227 }, _callee6, this);
228 }));
229 function test(_x6) {
230 return _test.apply(this, arguments);
231 }
232 return test;
233 }()
234 }, {
235 key: "list",
236 value: function () {
237 var _list = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(req, res) {
238 var proxy, prefilter, _yield$proxy$getList, testedAt, fetchedAt, rawProxyList, proxyList;
239 return _regenerator["default"].wrap(function _callee7$(_context7) {
240 while (1) switch (_context7.prev = _context7.next) {
241 case 0:
242 _context7.next = 2;
243 return this.app.module('proxy');
244 case 2:
245 proxy = _context7.sent;
246 prefilter = {};
247 if (req.params.provider) prefilter.provider = req.params.provider;
248 // console.log({prefilter})
249 _context7.next = 7;
250 return proxy.getList(prefilter);
251 case 7:
252 _yield$proxy$getList = _context7.sent;
253 testedAt = _yield$proxy$getList.testedAt;
254 fetchedAt = _yield$proxy$getList.fetchedAt;
255 rawProxyList = _yield$proxy$getList.list;
256 proxyList = (0, _filterProxy.filterProxy)(rawProxyList, req.data);
257 if (!(req.params.format === 'txt' || req.data.format === 'txt')) {
258 _context7.next = 14;
259 break;
260 }
261 return _context7.abrupt("return", res.send(proxyList.map(function (p) {
262 return p.uri;
263 }).join('\n')));
264 case 14:
265 return _context7.abrupt("return", {
266 __pack: true,
267 fetchedAt: fetchedAt,
268 testedAt: testedAt,
269 total: rawProxyList.length,
270 count: proxyList.length,
271 data: proxyList
272 // data: proxyList.map((p) => p.toObject()),
273 });
274 case 15:
275 case "end":
276 return _context7.stop();
277 }
278 }, _callee7, this);
279 }));
280 function list(_x7, _x8) {
281 return _list.apply(this, arguments);
282 }
283 return list;
284 }()
285 }]);
286 return ProxyApi;
287}(_serverApi["default"]);
288exports["default"] = ProxyApi;
289//# sourceMappingURL=ProxyApi.js.map
\No newline at end of file