@corbat-tech/coco 2.29.0 → 2.30.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.
package/dist/cli/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import { execFile, execFileSync, execSync, spawn, exec } from 'child_process';
2
3
  import { setGlobalDispatcher, EnvHttpProxyAgent } from 'undici';
3
4
  import * as fs5 from 'fs';
4
5
  import fs5__default, { accessSync, readFileSync, constants as constants$1 } from 'fs';
@@ -14,7 +15,6 @@ import JSON5 from 'json5';
14
15
  import * as crypto from 'crypto';
15
16
  import { randomUUID, randomBytes, createHash } from 'crypto';
16
17
  import * as http from 'http';
17
- import { execFile, execSync, spawn, execFileSync, exec } from 'child_process';
18
18
  import { promisify } from 'util';
19
19
  import * as p26 from '@clack/prompts';
20
20
  import chalk from 'chalk';
@@ -83,14 +83,111 @@ function maskProxyUrl(url) {
83
83
  return "[invalid proxy URL]";
84
84
  }
85
85
  }
86
- function installProxyDispatcher() {
87
- if (installed) return getProxyFromEnv() ? maskProxyUrl(getProxyFromEnv()) : null;
88
- const proxy = getProxyFromEnv();
89
- if (!proxy) return null;
86
+ function defaultRunner(cmd, args) {
87
+ try {
88
+ return execFileSync(cmd, args, {
89
+ encoding: "utf-8",
90
+ timeout: 2e3,
91
+ stdio: ["ignore", "pipe", "ignore"]
92
+ });
93
+ } catch {
94
+ return null;
95
+ }
96
+ }
97
+ function parseMacOsProxy(output) {
98
+ const getField = (name) => {
99
+ const re = new RegExp(`^\\s*${name}\\s*:\\s*(.+?)\\s*$`, "m");
100
+ return output.match(re)?.[1];
101
+ };
102
+ if (getField("ProxyAutoConfigEnable") === "1") {
103
+ return null;
104
+ }
105
+ const pick = (prefix) => {
106
+ if (getField(`${prefix}Enable`) !== "1") return null;
107
+ const host = getField(`${prefix}Proxy`);
108
+ const port = getField(`${prefix}Port`);
109
+ if (!host) return null;
110
+ return `http://${host}${port ? `:${port}` : ""}`;
111
+ };
112
+ const proxyUrl = pick("HTTPS") ?? pick("HTTP");
113
+ if (!proxyUrl) return null;
114
+ const exceptionsMatch = output.match(/ExceptionsList\s*:\s*<array>\s*\{([\s\S]*?)\}/);
115
+ const exceptions = [];
116
+ const exceptionsBody = exceptionsMatch?.[1];
117
+ if (exceptionsBody) {
118
+ for (const line of exceptionsBody.split("\n")) {
119
+ const entry = line.match(/^\s*\d+\s*:\s*(.+?)\s*$/)?.[1];
120
+ if (entry) exceptions.push(entry);
121
+ }
122
+ }
123
+ return {
124
+ proxyUrl,
125
+ noProxy: exceptions.length > 0 ? exceptions.join(",") : void 0
126
+ };
127
+ }
128
+ function parseWindowsProxy(output) {
129
+ if (/Direct access/i.test(output)) return null;
130
+ const raw = output.match(/Proxy\s+Server\(s\)\s*:\s*(\S.*?)\s*$/m)?.[1]?.trim();
131
+ if (!raw) return null;
132
+ let hostPort = raw;
133
+ if (raw.includes("=")) {
134
+ const parts = raw.split(";").map((p47) => p47.trim());
135
+ const httpsEntry = parts.find((p47) => p47.toLowerCase().startsWith("https="));
136
+ const httpEntry = parts.find((p47) => p47.toLowerCase().startsWith("http="));
137
+ const chosen = httpsEntry ?? httpEntry;
138
+ if (!chosen) return null;
139
+ hostPort = chosen.split("=", 2)[1]?.trim() ?? "";
140
+ if (!hostPort) return null;
141
+ }
142
+ const proxyUrl = /^https?:\/\//i.test(hostPort) ? hostPort : `http://${hostPort}`;
143
+ let noProxy;
144
+ const bypass = output.match(/Bypass\s+List\s*:\s*(\S.*?)\s*$/m)?.[1]?.trim();
145
+ if (bypass && !/\(none\)/i.test(bypass)) {
146
+ noProxy = bypass.replace(/;/g, ",");
147
+ }
148
+ return { proxyUrl, noProxy };
149
+ }
150
+ function getProxyFromSystem(platform = process.platform, run = defaultRunner) {
151
+ if (platform === "darwin") {
152
+ const out = run("scutil", ["--proxy"]);
153
+ return out ? parseMacOsProxy(out) : null;
154
+ }
155
+ if (platform === "win32") {
156
+ const out = run("netsh", ["winhttp", "show", "proxy"]);
157
+ return out ? parseWindowsProxy(out) : null;
158
+ }
159
+ return null;
160
+ }
161
+ function installProxyDispatcher(resolveSystem = () => getProxyFromSystem()) {
162
+ if (installed) {
163
+ const existing = getProxyFromEnv();
164
+ return existing ? maskProxyUrl(existing) : null;
165
+ }
166
+ const envProxy = getProxyFromEnv();
167
+ if (envProxy) {
168
+ return applyDispatcher(envProxy);
169
+ }
170
+ const sys = resolveSystem();
171
+ if (sys) {
172
+ seedEnv("HTTPS_PROXY", sys.proxyUrl);
173
+ seedEnv("HTTP_PROXY", sys.proxyUrl);
174
+ if (sys.noProxy && !process.env.NO_PROXY && !process.env.no_proxy) {
175
+ seedEnv("NO_PROXY", sys.noProxy);
176
+ }
177
+ return applyDispatcher(sys.proxyUrl);
178
+ }
179
+ return null;
180
+ }
181
+ function seedEnv(key, value) {
182
+ if (process.env[key] !== void 0) return;
183
+ process.env[key] = value;
184
+ seededEnvKeys.push(key);
185
+ }
186
+ function applyDispatcher(proxyUrl) {
90
187
  try {
91
188
  setGlobalDispatcher(new EnvHttpProxyAgent());
92
189
  installed = true;
93
- return maskProxyUrl(proxy);
190
+ return maskProxyUrl(proxyUrl);
94
191
  } catch {
95
192
  return null;
96
193
  }
@@ -119,11 +216,12 @@ function unwrapCause(error) {
119
216
  }
120
217
  return void 0;
121
218
  }
122
- var PROXY_ENV_VARS, installed;
219
+ var PROXY_ENV_VARS, installed, seededEnvKeys;
123
220
  var init_proxy = __esm({
124
221
  "src/utils/proxy.ts"() {
125
222
  PROXY_ENV_VARS = ["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"];
126
223
  installed = false;
224
+ seededEnvKeys = [];
127
225
  }
128
226
  });
129
227
  function findPackageJson() {