@contentstack/cli-utilities 1.18.0-beta.0 → 1.19.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,8 +15,14 @@ class ManagementSDKInitiator {
15
15
  this.analyticsInfo = context === null || context === void 0 ? void 0 : context.analyticsInfo;
16
16
  }
17
17
  async createAPIClient(config) {
18
- // Get proxy configuration with priority: Environment variables > Global config
19
- const proxyConfig = (0, proxy_helper_1.getProxyConfig)();
18
+ // Resolve host so NO_PROXY applies even when config.host is omitted (e.g. from region.cma)
19
+ const host = (0, proxy_helper_1.resolveRequestHost)(config);
20
+ // NO_PROXY has priority over HTTP_PROXY/HTTPS_PROXY and config-set proxy
21
+ const proxyConfig = (0, proxy_helper_1.getProxyConfigForHost)(host);
22
+ // When bypassing, clear proxy env immediately so SDK never see it (they may read at init or first request).
23
+ if (!proxyConfig) {
24
+ (0, proxy_helper_1.clearProxyEnv)();
25
+ }
20
26
  const option = {
21
27
  host: config.host,
22
28
  maxContentLength: config.maxContentLength || 100000000,
@@ -7,6 +7,22 @@ const http_response_1 = require("./http-response");
7
7
  const config_handler_1 = tslib_1.__importDefault(require("../config-handler"));
8
8
  const auth_handler_1 = tslib_1.__importDefault(require("../auth-handler"));
9
9
  const proxy_helper_1 = require("../proxy-helper");
10
+ /**
11
+ * Derive request host from baseURL or url for NO_PROXY checks.
12
+ */
13
+ function getRequestHost(baseURL, url) {
14
+ const toTry = [baseURL, url].filter(Boolean);
15
+ for (const candidateUrl of toTry) {
16
+ try {
17
+ const parsed = new URL(candidateUrl.startsWith('http') ? candidateUrl : `https://${candidateUrl}`);
18
+ return parsed.hostname || undefined;
19
+ }
20
+ catch (_a) {
21
+ // Invalid URL; try next candidate (baseURL or url)
22
+ }
23
+ }
24
+ return undefined;
25
+ }
10
26
  class HttpClient {
11
27
  /**
12
28
  * Createa new pending HTTP request instance.
@@ -340,9 +356,10 @@ class HttpClient {
340
356
  this.headers({ 'x-header-ea': Object.values(earlyAccessHeaders).join(',') });
341
357
  }
342
358
  }
343
- // Configure proxy if available (priority: request.proxy > getProxyConfig())
359
+ // Configure proxy if available. NO_PROXY has priority: hosts in NO_PROXY never use proxy.
344
360
  if (!this.request.proxy) {
345
- const proxyConfig = (0, proxy_helper_1.getProxyConfig)();
361
+ const host = getRequestHost(this.request.baseURL, url);
362
+ const proxyConfig = host ? (0, proxy_helper_1.getProxyConfigForHost)(host) : (0, proxy_helper_1.getProxyConfig)();
346
363
  if (proxyConfig) {
347
364
  this.request.proxy = proxyConfig;
348
365
  }
@@ -8,10 +8,51 @@ export interface ProxyConfig {
8
8
  };
9
9
  }
10
10
  /**
11
- * Get proxy configuration with priority: Environment variables > Global config
11
+ * Parse NO_PROXY / no_proxy env (both uppercase and lowercase).
12
+ * NO_PROXY has priority over HTTP_PROXY/HTTPS_PROXY: hosts in this list never use the proxy.
13
+ * Values are hostnames only, comma-separated; leading dot matches subdomains (e.g. .contentstack.io).
14
+ * The bypass list is fully dynamic: only env values are used (no hardcoded default).
15
+ * @returns List of trimmed entries, or empty array when NO_PROXY/no_proxy is unset
16
+ */
17
+ export declare function getNoProxyList(): string[];
18
+ /**
19
+ * Check if the given host should bypass the proxy based on NO_PROXY / no_proxy.
20
+ * Supports: exact host, leading-dot subdomain match (e.g. .contentstack.io), and wildcard *.
21
+ * @param host - Request hostname (with or without port; will be normalized)
22
+ * @returns true if proxy should not be used for this host
23
+ */
24
+ export declare function shouldBypassProxy(host: string): boolean;
25
+ /**
26
+ * Get proxy configuration. Sources (in order): env (HTTP_PROXY/HTTPS_PROXY), then global config
27
+ * from `csdx config:set:proxy --host <host> --port <port> --protocol <protocol>`.
28
+ * For per-request use, prefer getProxyConfigForHost(host) so NO_PROXY overrides both sources.
12
29
  * @returns ProxyConfig object or undefined if no proxy is configured
13
30
  */
14
31
  export declare function getProxyConfig(): ProxyConfig | undefined;
32
+ /**
33
+ * Get proxy config only when the request host is not in NO_PROXY.
34
+ * NO_PROXY has priority over both HTTP_PROXY/HTTPS_PROXY and over proxy set via
35
+ * `csdx config:set:proxy` — if the host matches NO_PROXY, no proxy is used.
36
+ * Use this for all outbound requests so Contentstack and localhost bypass the proxy when set.
37
+ * @param host - Request hostname (e.g. api.contentstack.io or full URL like https://api.contentstack.io)
38
+ * @returns ProxyConfig or undefined if proxy is disabled or host should bypass (NO_PROXY)
39
+ */
40
+ export declare function getProxyConfigForHost(host: string): ProxyConfig | undefined;
41
+ /**
42
+ * Resolve request host for proxy/NO_PROXY checks: config.host or default CMA from region.
43
+ * Use when the caller may omit host so NO_PROXY still applies (e.g. from region.cma).
44
+ * @param config - Object with optional host (e.g. API client config)
45
+ * @returns Host string (hostname or empty)
46
+ */
47
+ export declare function resolveRequestHost(config: {
48
+ host?: string;
49
+ }): string;
50
+ /**
51
+ * Temporarily clear proxy-related env vars so SDK/axios cannot use them.
52
+ * Call the returned function to restore. Use when creating a client for a host in NO_PROXY.
53
+ * @returns Restore function (call to put env back)
54
+ */
55
+ export declare function clearProxyEnv(): () => void;
15
56
  /**
16
57
  * Check if proxy is configured (from any source)
17
58
  * @returns true if proxy is configured, false otherwise
@@ -1,14 +1,94 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getProxyUrl = exports.hasProxy = exports.getProxyConfig = void 0;
3
+ exports.getProxyUrl = exports.hasProxy = exports.clearProxyEnv = exports.resolveRequestHost = exports.getProxyConfigForHost = exports.getProxyConfig = exports.shouldBypassProxy = exports.getNoProxyList = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const config_handler_1 = tslib_1.__importDefault(require("./config-handler"));
6
6
  /**
7
- * Get proxy configuration with priority: Environment variables > Global config
7
+ * Parse NO_PROXY / no_proxy env (both uppercase and lowercase).
8
+ * NO_PROXY has priority over HTTP_PROXY/HTTPS_PROXY: hosts in this list never use the proxy.
9
+ * Values are hostnames only, comma-separated; leading dot matches subdomains (e.g. .contentstack.io).
10
+ * The bypass list is fully dynamic: only env values are used (no hardcoded default).
11
+ * @returns List of trimmed entries, or empty array when NO_PROXY/no_proxy is unset
12
+ */
13
+ function getNoProxyList() {
14
+ const raw = process.env.NO_PROXY || process.env.no_proxy || '';
15
+ return raw
16
+ .split(',')
17
+ .map((s) => s.trim())
18
+ .filter(Boolean);
19
+ }
20
+ exports.getNoProxyList = getNoProxyList;
21
+ /**
22
+ * Normalize host for NO_PROXY matching: strip protocol/URL, port, lowercase, handle IPv6 brackets.
23
+ * Accepts hostname, host:port, or full URL (e.g. https://api.contentstack.io).
24
+ */
25
+ function normalizeHost(host) {
26
+ if (!host || typeof host !== 'string')
27
+ return '';
28
+ let h = host.trim().toLowerCase();
29
+ // If it looks like a URL, extract hostname so NO_PROXY matching works (e.g. region.cma is full URL)
30
+ if (h.includes('://')) {
31
+ try {
32
+ const u = new URL(h);
33
+ h = u.hostname;
34
+ }
35
+ catch (_a) {
36
+ // fall through to port stripping below
37
+ }
38
+ }
39
+ const portIdx = h.lastIndexOf(':');
40
+ if (h.startsWith('[')) {
41
+ const close = h.indexOf(']');
42
+ if (close !== -1 && h.length > close + 1 && h[close + 1] === ':') {
43
+ h = h.slice(1, close);
44
+ }
45
+ }
46
+ else if (portIdx !== -1) {
47
+ const after = h.slice(portIdx + 1);
48
+ if (/^\d+$/.test(after)) {
49
+ h = h.slice(0, portIdx);
50
+ }
51
+ }
52
+ return h;
53
+ }
54
+ /**
55
+ * Check if the given host should bypass the proxy based on NO_PROXY / no_proxy.
56
+ * Supports: exact host, leading-dot subdomain match (e.g. .contentstack.io), and wildcard *.
57
+ * @param host - Request hostname (with or without port; will be normalized)
58
+ * @returns true if proxy should not be used for this host
59
+ */
60
+ function shouldBypassProxy(host) {
61
+ const normalized = normalizeHost(host);
62
+ if (!normalized)
63
+ return false;
64
+ const list = getNoProxyList();
65
+ for (const entry of list) {
66
+ const e = entry.trim().toLowerCase();
67
+ if (!e)
68
+ continue;
69
+ if (e === '*')
70
+ return true;
71
+ if (e.startsWith('.')) {
72
+ const domain = e.slice(1);
73
+ if (normalized === domain || normalized.endsWith(e))
74
+ return true;
75
+ }
76
+ else {
77
+ if (normalized === e)
78
+ return true;
79
+ }
80
+ }
81
+ return false;
82
+ }
83
+ exports.shouldBypassProxy = shouldBypassProxy;
84
+ /**
85
+ * Get proxy configuration. Sources (in order): env (HTTP_PROXY/HTTPS_PROXY), then global config
86
+ * from `csdx config:set:proxy --host <host> --port <port> --protocol <protocol>`.
87
+ * For per-request use, prefer getProxyConfigForHost(host) so NO_PROXY overrides both sources.
8
88
  * @returns ProxyConfig object or undefined if no proxy is configured
9
89
  */
10
90
  function getProxyConfig() {
11
- // Priority 1: Check environment variables (HTTPS_PROXY or HTTP_PROXY)
91
+ // Priority 1: Environment variables (HTTPS_PROXY or HTTP_PROXY)
12
92
  const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
13
93
  if (proxyUrl) {
14
94
  try {
@@ -35,7 +115,7 @@ function getProxyConfig() {
35
115
  // Invalid URL, continue to check global config
36
116
  }
37
117
  }
38
- // Priority 2: Check global config store
118
+ // Priority 2: Global config (csdx config:set:proxy)
39
119
  const globalProxyConfig = config_handler_1.default.get('proxy');
40
120
  if (globalProxyConfig) {
41
121
  if (typeof globalProxyConfig === 'object') {
@@ -73,6 +153,68 @@ function getProxyConfig() {
73
153
  return undefined;
74
154
  }
75
155
  exports.getProxyConfig = getProxyConfig;
156
+ /**
157
+ * Get proxy config only when the request host is not in NO_PROXY.
158
+ * NO_PROXY has priority over both HTTP_PROXY/HTTPS_PROXY and over proxy set via
159
+ * `csdx config:set:proxy` — if the host matches NO_PROXY, no proxy is used.
160
+ * Use this for all outbound requests so Contentstack and localhost bypass the proxy when set.
161
+ * @param host - Request hostname (e.g. api.contentstack.io or full URL like https://api.contentstack.io)
162
+ * @returns ProxyConfig or undefined if proxy is disabled or host should bypass (NO_PROXY)
163
+ */
164
+ function getProxyConfigForHost(host) {
165
+ if (shouldBypassProxy(host))
166
+ return undefined;
167
+ return getProxyConfig();
168
+ }
169
+ exports.getProxyConfigForHost = getProxyConfigForHost;
170
+ /**
171
+ * Resolve request host for proxy/NO_PROXY checks: config.host or default CMA from region.
172
+ * Use when the caller may omit host so NO_PROXY still applies (e.g. from region.cma).
173
+ * @param config - Object with optional host (e.g. API client config)
174
+ * @returns Host string (hostname or empty)
175
+ */
176
+ function resolveRequestHost(config) {
177
+ var _a;
178
+ if (config.host)
179
+ return config.host;
180
+ const cma = (_a = config_handler_1.default.get('region')) === null || _a === void 0 ? void 0 : _a.cma;
181
+ if (cma && typeof cma === 'string') {
182
+ if (cma.startsWith('http')) {
183
+ try {
184
+ const u = new URL(cma);
185
+ return u.hostname || cma;
186
+ }
187
+ catch (_b) {
188
+ return cma;
189
+ }
190
+ }
191
+ return cma;
192
+ }
193
+ return '';
194
+ }
195
+ exports.resolveRequestHost = resolveRequestHost;
196
+ /**
197
+ * Temporarily clear proxy-related env vars so SDK/axios cannot use them.
198
+ * Call the returned function to restore. Use when creating a client for a host in NO_PROXY.
199
+ * @returns Restore function (call to put env back)
200
+ */
201
+ function clearProxyEnv() {
202
+ const saved = {};
203
+ const keys = ['HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy', 'ALL_PROXY', 'all_proxy'];
204
+ for (const k of keys) {
205
+ if (k in process.env) {
206
+ saved[k] = process.env[k];
207
+ delete process.env[k];
208
+ }
209
+ }
210
+ return () => {
211
+ for (const k of keys) {
212
+ if (saved[k] !== undefined)
213
+ process.env[k] = saved[k];
214
+ }
215
+ };
216
+ }
217
+ exports.clearProxyEnv = clearProxyEnv;
76
218
  /**
77
219
  * Check if proxy is configured (from any source)
78
220
  * @returns true if proxy is configured, false otherwise
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentstack/cli-utilities",
3
- "version": "1.18.0-beta.0",
3
+ "version": "1.19.0-beta.0",
4
4
  "description": "Utilities for contentstack projects",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -82,4 +82,4 @@
82
82
  "ts-node": "^10.9.2",
83
83
  "typescript": "^4.9.5"
84
84
  }
85
- }
85
+ }