@crawly/proxy
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -10,8 +10,35 @@ "use strict"; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const url_1 = require("url"); | ||
const superagent = require("superagent"); | ||
const uniqid = require("uniqid"); | ||
class ProxyBuilder { | ||
constructor() { | ||
var url_1 = require("url"); | ||
var superagent = require("superagent"); | ||
var uniqid = require("uniqid"); | ||
var ProxyBuilder = /** @class */ (function () { | ||
function ProxyBuilder() { | ||
this.key = 'PROXY_LIST'; | ||
@@ -24,31 +51,32 @@ this.dynamicKey = 'PROXY_DYNAMIC'; | ||
} | ||
withKey(key) { | ||
ProxyBuilder.prototype.withKey = function (key) { | ||
this.key = key; | ||
return this; | ||
} | ||
withDynamicKey(key) { | ||
}; | ||
ProxyBuilder.prototype.withDynamicKey = function (key) { | ||
this.dynamicKey = key; | ||
return this; | ||
} | ||
withTunnelKey(key) { | ||
}; | ||
ProxyBuilder.prototype.withTunnelKey = function (key) { | ||
this.tunnelKey = key; | ||
return this; | ||
} | ||
withRandomToken() { | ||
}; | ||
ProxyBuilder.prototype.withRandomToken = function () { | ||
this.randomToken = uniqid(); | ||
return this; | ||
} | ||
withProxyList(list = '') { | ||
}; | ||
ProxyBuilder.prototype.withProxyList = function (list) { | ||
if (list === void 0) { list = ''; } | ||
if (!list && !process.env[this.key]) { | ||
throw new Error(`[ProxyBuilder.getList] Varívavel ${this.key} não definida`); | ||
throw new Error("[ProxyBuilder.getList] Var\u00EDvavel " + this.key + " n\u00E3o definida"); | ||
} | ||
const regex = /\{\{range\:([0-9]+):([0-9]+)\}\}/g; | ||
const proxyList = list || process.env[this.key] || ''; | ||
this.proxyList = proxyList.split(',').map((p) => p.replace(regex, ($0, $1, $2) => { | ||
var regex = /\{\{range\:([0-9]+):([0-9]+)\}\}/g; | ||
var proxyList = list || process.env[this.key] || ''; | ||
this.proxyList = proxyList.split(',').map(function (p) { return p.replace(regex, function ($0, $1, $2) { | ||
return (Math.floor(Math.random() * parseInt($2, 10)) + parseInt($1, 10)).toString(); | ||
})); | ||
}); }); | ||
return this; | ||
} | ||
withRandomInstance() { | ||
const current = this.proxyList[Math.floor(Math.random() * this.proxyList.length)]; | ||
}; | ||
ProxyBuilder.prototype.withRandomInstance = function () { | ||
var current = this.proxyList[Math.floor(Math.random() * this.proxyList.length)]; | ||
if (current.toLowerCase() === 'dynamic') { | ||
@@ -64,4 +92,4 @@ this.currentInstance = this.getDynamicInstance(); | ||
return this; | ||
} | ||
getRandomToken() { | ||
}; | ||
ProxyBuilder.prototype.getRandomToken = function () { | ||
if (!this.randomToken) { | ||
@@ -71,50 +99,80 @@ throw new Error('[ProxyBuilder.getRandomToken] Propriedade "randomToken" não definida'); | ||
return this.randomToken; | ||
} | ||
getDynamicInstance() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.dynamicKey || !process.env[this.dynamicKey]) { | ||
const msg = `[ProxyBuilder.getDynamicInstance] Varívavel ${this.dynamicKey} não definida`; | ||
throw new Error(msg); | ||
} | ||
const url = process.env[this.dynamicKey] || ''; | ||
const response = yield superagent.get(url); | ||
const data = JSON.parse(response.text); | ||
return `http://${data.username}:${data.password}@${data.hostname}:${data.port}`; | ||
}; | ||
ProxyBuilder.prototype.getDynamicInstance = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var msg, url, response, data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!this.dynamicKey || !process.env[this.dynamicKey]) { | ||
msg = "[ProxyBuilder.getDynamicInstance] Var\u00EDvavel " + this.dynamicKey + " n\u00E3o definida"; | ||
throw new Error(msg); | ||
} | ||
url = process.env[this.dynamicKey] || ''; | ||
return [4 /*yield*/, superagent.get(url)]; | ||
case 1: | ||
response = _a.sent(); | ||
data = JSON.parse(response.text); | ||
return [2 /*return*/, "http://" + data.username + ":" + data.password + "@" + data.hostname + ":" + data.port]; | ||
} | ||
}); | ||
}); | ||
} | ||
getInstance() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const instance = yield this.currentInstance; | ||
if (!instance) { | ||
throw new Error('[ProxyBuilder.getInstance] Propriedade "currentInstance" não definida'); | ||
} | ||
return instance; | ||
}; | ||
ProxyBuilder.prototype.getInstance = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var instance; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.currentInstance]; | ||
case 1: | ||
instance = _a.sent(); | ||
if (!instance) { | ||
throw new Error('[ProxyBuilder.getInstance] Propriedade "currentInstance" não definida'); | ||
} | ||
return [2 /*return*/, instance]; | ||
} | ||
}); | ||
}); | ||
} | ||
getTunnel() { | ||
const value = process.env[this.tunnelKey]; | ||
}; | ||
ProxyBuilder.prototype.getTunnel = function () { | ||
var value = process.env[this.tunnelKey]; | ||
return value && value !== 'undefined' | ||
? JSON.parse(value) | ||
: undefined; | ||
} | ||
getProxyList() { | ||
}; | ||
ProxyBuilder.prototype.getProxyList = function () { | ||
return this.proxyList; | ||
} | ||
getMetadata(url = '') { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const proxyInstance = url || (yield this.currentInstance); | ||
if (!proxyInstance) { | ||
throw new Error('[ProxyBuilder.getMetadata] Informe uma instancia de proxy válida'); | ||
} | ||
const { hostname, password, port, protocol, username } = new url_1.URL(proxyInstance); | ||
const data = {}; | ||
data.username = username; | ||
data.password = password; | ||
data.hostname = hostname; | ||
data.protocol = protocol.slice(0, -1); // remove ':' | ||
data.port = port; | ||
return data; | ||
}; | ||
ProxyBuilder.prototype.getMetadata = function (url) { | ||
if (url === void 0) { url = ''; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var proxyInstance, _a, _b, hostname, password, port, protocol, username, data; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_a = url; | ||
if (_a) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.currentInstance]; | ||
case 1: | ||
_a = (_c.sent()); | ||
_c.label = 2; | ||
case 2: | ||
proxyInstance = _a; | ||
if (!proxyInstance) { | ||
throw new Error('[ProxyBuilder.getMetadata] Informe uma instancia de proxy válida'); | ||
} | ||
_b = new url_1.URL(proxyInstance), hostname = _b.hostname, password = _b.password, port = _b.port, protocol = _b.protocol, username = _b.username; | ||
data = {}; | ||
data.username = username; | ||
data.password = password; | ||
data.hostname = hostname; | ||
data.protocol = protocol.slice(0, -1); // remove ':' | ||
data.port = port; | ||
return [2 /*return*/, data]; | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
return ProxyBuilder; | ||
}()); | ||
exports.ProxyBuilder = ProxyBuilder; |
{ | ||
"name": "@crawly/proxy", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Proxy loader/parser", | ||
"main": "dist/index", | ||
"types": "dist/index.d.ts", | ||
"main": "src/index.ts", | ||
"scripts": { | ||
@@ -8,0 +7,0 @@ "tslint": "tslint -c tslint.json -p tsconfig.json", |
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es6", | ||
"target": "es5", | ||
"lib": [ | ||
"dom", | ||
"es6" | ||
"es5" | ||
], | ||
@@ -9,0 +9,0 @@ "moduleResolution": "node", |
37063
660