@exchanges/binance
Version:
Exchange provider for Binance API
57 lines • 2.83 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestProxyInterceptor = void 0;
const socks_proxy_agent_1 = require("socks-proxy-agent");
function requestProxyInterceptor(getProxies, proxyMaxRequestAttempt) {
getProxies = getProxies || [];
proxyMaxRequestAttempt = proxyMaxRequestAttempt || 5;
let lastProxyUsed = 0;
return (instance) => {
const id = instance.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
const proxies = typeof getProxies === 'function' ? yield getProxies() : getProxies;
if (lastProxyUsed >= proxies.length)
lastProxyUsed = 0;
const proxy = proxies[lastProxyUsed];
if (!proxy)
return config;
const { host, port, login, password } = typeof proxy === 'string' ? parseProxyAddress(proxy) : proxy;
lastProxyUsed++;
config.httpsAgent = new socks_proxy_agent_1.SocksProxyAgent({
host,
port,
auth: login && [login, password].filter(Boolean).join(':'),
});
return config;
}));
return () => {
instance.interceptors.request.eject(id);
};
};
}
exports.requestProxyInterceptor = requestProxyInterceptor;
function parseProxyAddress(value) {
if (!value)
return null;
if (typeof value !== 'string')
return null;
const firstSplit = value.split('@');
const authPath = firstSplit.length <= 1 ? null : firstSplit[0];
const addressPath = firstSplit.length <= 1 ? firstSplit[0] : firstSplit[1];
const authSplit = authPath && authPath.split(':');
const login = authSplit ? authSplit[0] : null;
const password = authSplit ? (authSplit.length <= 1 ? null : authSplit[1]) : null;
const adressSplit = addressPath && addressPath.split(':');
const host = adressSplit ? adressSplit[0] : null;
const port = adressSplit ? (adressSplit.length <= 1 ? null : adressSplit[1]) : null;
return { login, password, host, port };
}
//# sourceMappingURL=requestProxyInterceptor.js.map