@contentstack/cli-utilities
Version:
Utilities for contentstack projects
104 lines (103 loc) • 4.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProxyUrl = exports.hasProxy = exports.getProxyConfig = void 0;
const tslib_1 = require("tslib");
const config_handler_1 = tslib_1.__importDefault(require("./config-handler"));
/**
* Get proxy configuration with priority: Environment variables > Global config
* @returns ProxyConfig object or undefined if no proxy is configured
*/
function getProxyConfig() {
// Priority 1: Check environment variables (HTTPS_PROXY or HTTP_PROXY)
const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
if (proxyUrl) {
try {
const url = new URL(proxyUrl);
const defaultPort = url.protocol === 'https:' ? 443 : 80;
const port = url.port ? Number.parseInt(url.port, 10) : defaultPort;
if (!Number.isNaN(port) && port >= 1 && port <= 65535) {
const protocol = url.protocol.replace(':', '');
const proxyConfig = {
protocol: protocol,
host: url.hostname,
port: port,
};
if (url.username || url.password) {
proxyConfig.auth = {
username: url.username,
password: url.password,
};
}
return proxyConfig;
}
}
catch (_a) {
// Invalid URL, continue to check global config
}
}
// Priority 2: Check global config store
const globalProxyConfig = config_handler_1.default.get('proxy');
if (globalProxyConfig) {
if (typeof globalProxyConfig === 'object') {
const port = globalProxyConfig.port;
if (port !== undefined && !Number.isNaN(port) && port >= 1 && port <= 65535) {
return globalProxyConfig;
}
}
else if (typeof globalProxyConfig === 'string') {
try {
const url = new URL(globalProxyConfig);
const defaultPort = url.protocol === 'https:' ? 443 : 80;
const port = url.port ? Number.parseInt(url.port, 10) : defaultPort;
if (!Number.isNaN(port) && port >= 1 && port <= 65535) {
const protocol = url.protocol.replace(':', '');
const proxyConfig = {
protocol: protocol,
host: url.hostname,
port: port,
};
if (url.username || url.password) {
proxyConfig.auth = {
username: url.username,
password: url.password,
};
}
return proxyConfig;
}
}
catch (_b) {
// Invalid URL, return undefined
}
}
}
return undefined;
}
exports.getProxyConfig = getProxyConfig;
/**
* Check if proxy is configured (from any source)
* @returns true if proxy is configured, false otherwise
*/
function hasProxy() {
return !!getProxyConfig() || !!process.env.HTTPS_PROXY || !!process.env.HTTP_PROXY || !!config_handler_1.default.get('proxy');
}
exports.hasProxy = hasProxy;
/**
* Get proxy URL string for display purposes
* @returns Proxy URL string or 'proxy server' if not available
*/
function getProxyUrl() {
const proxyConfig = getProxyConfig();
if (proxyConfig) {
return `${proxyConfig.protocol}://${proxyConfig.host}:${proxyConfig.port}`;
}
const envProxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
if (envProxy) {
return envProxy;
}
const globalProxy = config_handler_1.default.get('proxy');
if (globalProxy && typeof globalProxy === 'object') {
return `${globalProxy.protocol}://${globalProxy.host}:${globalProxy.port}`;
}
return 'proxy server';
}
exports.getProxyUrl = getProxyUrl;