UNPKG

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