UNPKG

@contentstack/cli-utilities

Version:

Utilities for contentstack projects

152 lines (151 loc) 7.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.managementSDKInitiator = void 0; const tslib_1 = require("tslib"); const management_1 = require("@contentstack/management"); const auth_handler_1 = tslib_1.__importDefault(require("./auth-handler")); const node_https_1 = require("node:https"); const config_handler_1 = tslib_1.__importDefault(require("./config-handler")); const proxy_helper_1 = require("./proxy-helper"); const dotenv_1 = tslib_1.__importDefault(require("dotenv")); dotenv_1.default.config(); class ManagementSDKInitiator { constructor() { } init(context) { this.analyticsInfo = context === null || context === void 0 ? void 0 : context.analyticsInfo; } async createAPIClient(config) { // Get proxy configuration with priority: Environment variables > Global config const proxyConfig = (0, proxy_helper_1.getProxyConfig)(); const option = { host: config.host, maxContentLength: config.maxContentLength || 100000000, maxBodyLength: config.maxBodyLength || 1000000000, maxRequests: 10, retryLimit: 3, timeout: proxyConfig ? 10000 : 60000, delayMs: config.delayMs, httpsAgent: new node_https_1.Agent({ maxSockets: 100, maxFreeSockets: 10, keepAlive: true, timeout: 60000, // active socket keepalive for 60 seconds // NOTE freeSocketTimeout option not exist in https client // freeSocketTimeout: 30000, // free socket keepalive for 30 seconds }), retryDelay: Math.floor(Math.random() * (8000 - 3000 + 1) + 3000), logHandler: (level, data) => { }, retryCondition: (error) => { // Don't retry proxy connection errors - fail fast // Check if proxy is configured and this is a connection error if (proxyConfig) { const errorCode = error.code || error.errno || error.syscall; const errorMessage = error.message || String(error); // Detect proxy connection failures - don't retry these if (errorCode === 'ECONNREFUSED' || errorCode === 'ETIMEDOUT' || errorCode === 'ENOTFOUND' || errorCode === 'ERR_BAD_RESPONSE' || (errorMessage === null || errorMessage === void 0 ? void 0 : errorMessage.includes('ECONNREFUSED')) || (errorMessage === null || errorMessage === void 0 ? void 0 : errorMessage.includes('connect ECONNREFUSED')) || (errorMessage === null || errorMessage === void 0 ? void 0 : errorMessage.includes('ERR_BAD_RESPONSE')) || (errorMessage === null || errorMessage === void 0 ? void 0 : errorMessage.includes('Bad response')) || (errorMessage === null || errorMessage === void 0 ? void 0 : errorMessage.includes('proxy')) || (errorCode === 'ETIMEDOUT' && proxyConfig) // Timeout with proxy likely means proxy issue ) { // Don't retry - return false to fail immediately // The error will be thrown and handled by error formatter return false; } } // LINK ***REMOVED***vascript/blob/72fee8ad75ba7d1d5bab8489ebbbbbbaefb1c880/src/core/stack.js#L49 if (error.response && error.response.status) { switch (error.response.status) { case 401: case 429: case 408: case 422: return true; default: return false; } } return false; }, retryDelayOptions: { base: 1000, customBackoff: (retryCount, error) => { return 1; }, }, refreshToken: () => { return new Promise((resolve, reject) => { const authorisationType = config_handler_1.default.get('authorisationType'); if (authorisationType === 'BASIC') { // Handle basic auth 401 here reject('Session timed out, please login to proceed'); } else if (authorisationType === 'OAUTH') { return auth_handler_1.default .compareOAuthExpiry(true) .then(() => { resolve({ authorization: `Bearer ${config_handler_1.default.get('oauthAccessToken')}`, }); }) .catch((error) => { reject(error); }); } else { reject('You do not have permissions to perform this action, please login to proceed'); } }); }, }; if (proxyConfig) { option.proxy = proxyConfig; } if (config.endpoint) { option.endpoint = config.endpoint; } if (typeof config.branchName === 'string') { if (!option.headers) option.headers = {}; option.headers.branch = config.branchName; } if (this.analyticsInfo) { if (!option.headers) option.headers = {}; option.headers['X-CS-CLI'] = this.analyticsInfo; } if (!config.management_token) { const authorisationType = config_handler_1.default.get('authorisationType'); if (authorisationType === 'BASIC') { option.authtoken = config_handler_1.default.get('authtoken'); option.authorization = ''; } else if (authorisationType === 'OAUTH') { if (!config.skipTokenValidity) { await auth_handler_1.default.compareOAuthExpiry(); option.authorization = `Bearer ${config_handler_1.default.get('oauthAccessToken')}`; } else { option.authtoken = ''; option.authorization = ''; } } else { option.authtoken = ''; option.authorization = ''; } } const earlyAccessHeaders = config_handler_1.default.get(`earlyAccessHeaders`); if (earlyAccessHeaders && Object.keys(earlyAccessHeaders).length > 0) { option.early_access = Object.values(earlyAccessHeaders); } return (0, management_1.client)(option); } } exports.managementSDKInitiator = new ManagementSDKInitiator(); exports.default = exports.managementSDKInitiator.createAPIClient.bind(exports.managementSDKInitiator);