Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,6 @@ fileignoreconfig:
checksum: 57566af0bbd95a28b454e4436b0396dfec2fe05ddd5b448d46e3a8b68db8b9cb
- filename: packages/contentstack-export/test/unit/export/modules/entries.test.ts
checksum: 5950c6f697224e11bec32736e6a967b0ab7ac98e9c8f8bb8eaaf10af60913e40
- filename: packages/contentstack-utilities/src/contentstack-management-sdk.ts
checksum: 3e6f6cf2a88632069a1c4c8fffa76f9631b29f5bb917fff9da8bca36fd5e192f
version: "1.0"
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,49 @@
}

async createAPIClient(config): Promise<ContentstackClient> {
// Get proxy configuration with priority: config.proxy > configStore.proxy > HTTPS_PROXY > HTTP_PROXY
let proxyConfig = config.proxy;

// Check global config store if not provided in config
if (!proxyConfig) {
proxyConfig = configStore.get('proxy');
}

// Check environment variables if still not found
if (!proxyConfig) {
const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
if (proxyUrl) {
// Parse URL string into proxy object format (SDK expects object with host, port, protocol)
try {
const url = new URL(proxyUrl);
const parsedProxy: any = {
protocol: url.protocol.replace(':', '') as 'http' | 'https',
host: url.hostname,
port: Number.parseInt(url.port) || (url.protocol === 'https:' ? 443 : 80),
};
// Include auth if present in URL
if (url.username || url.password) {
parsedProxy.auth = {
username: url.username,
password: url.password,
};
}
proxyConfig = parsedProxy;
} catch (error) {
// If URL parsing fails, ignore proxy config
proxyConfig = undefined;
}
}
}

const option: ContentstackConfig = {
host: config.host,
maxContentLength: config.maxContentLength || 100000000,
maxBodyLength: config.maxBodyLength || 1000000000,
maxRequests: 10,
retryLimit: 3,
timeout: 60000,
// Reduce timeout when proxy is configured to fail faster on invalid proxy
timeout: proxyConfig ? 10000 : 60000, // 10s timeout with proxy, 60s without
delayMs: config.delayMs,
httpsAgent: new Agent({
maxSockets: 100,
Expand Down Expand Up @@ -76,6 +112,42 @@
},
};

// Set proxy configuration if found
if (proxyConfig) {
if (typeof proxyConfig === 'object') {
option.proxy = proxyConfig;
// Log proxy configuration for debugging (enable with DEBUG_PROXY=true)
if (process.env.DEBUG_PROXY === 'true') {
const safeProxyConfig = { ...proxyConfig };
if (safeProxyConfig.auth) {
safeProxyConfig.auth = {
username: safeProxyConfig.auth.username || '',
password: safeProxyConfig.auth.password ? 'REDACTED' : undefined,
};
}
console.log('[PROXY] Using proxy:', JSON.stringify(safeProxyConfig));
Comment thread Fixed
}
} else if (typeof proxyConfig === 'string') {
// If proxy is provided as string URL, parse it to object format
try {
const url = new URL(proxyConfig);
const parsedProxy: any = {
protocol: url.protocol.replace(':', '') as 'http' | 'https',
host: url.hostname,
port: Number.parseInt(url.port) || (url.protocol === 'https:' ? 443 : 80),
};
if (url.username || url.password) {
parsedProxy.auth = {
username: url.username,
password: url.password,
};
}
option.proxy = parsedProxy;
} catch {
// If URL parsing fails, ignore proxy config
}
}
}
if (config.endpoint) {
option.endpoint = config.endpoint;
}
Expand Down
Loading