lceda-connect 0.3.0 → 0.4.1

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +92 -30
  3. package/dist/{cli-CjcJVM6z.mjs → cli-1_urvve3.mjs} +4 -4
  4. package/dist/{cli-C4ZkWlV2.mjs → cli-CXk5pvVm.mjs} +1 -1
  5. package/dist/{cli-C6saKrQp.mjs → cli-DFqBHofJ.mjs} +18 -3
  6. package/dist/cli-bridge-server.mjs +1 -1
  7. package/dist/cli-bridge.mjs +1 -1
  8. package/dist/cli-cdp.mjs +1 -1
  9. package/dist/cli-mcp.mjs +1 -1
  10. package/dist/cli.mjs +1 -1
  11. package/dist/config-BKbwyvsG.mjs +516 -0
  12. package/dist/{discover-D5mRrsxg.mjs → discover-DmRYlAlR.mjs} +13 -64
  13. package/dist/environment-BegP0n2N.mjs +439 -0
  14. package/dist/environment-Clx1-nDW.mjs +146 -0
  15. package/dist/http-Cdp6S2rm.mjs +366 -0
  16. package/dist/http-D7sYy2y4.mjs +1 -0
  17. package/dist/index.d.mts +226 -6
  18. package/dist/index.mjs +7 -5
  19. package/dist/{main-CcKvXSJG.mjs → main-B3aaKvqY.mjs} +688 -89
  20. package/dist/mcpHost-CRoN6MnG.mjs +331 -0
  21. package/dist/{registry-Dmti2u6s.mjs → registry-3WTFCZsR.mjs} +84 -24
  22. package/dist/registry-DdJ8LEdd.mjs +1 -0
  23. package/dist/{server-BduSjwzu.mjs → server-zw6t1Pmk.mjs} +9 -14
  24. package/dist/{session-DvIXO5Hf.mjs → session-AQbRX6Ys.mjs} +2 -2
  25. package/dist/{sideEffects-DF_tIVsf.mjs → sideEffects-Ds_T-_B4.mjs} +3 -3
  26. package/dist/stdio-BZPlcfDR.mjs +27 -0
  27. package/dist/stdio-Du1oLqfJ.mjs +1 -0
  28. package/dist/{targets-D5nmbsyz.mjs → targets-8LBEgvbq.mjs} +1 -1
  29. package/dist/{verbs-5M7rj1-E.mjs → verbs-DjQQTL2k.mjs} +191 -54
  30. package/dist/windowsHost-CTZXMA8D.mjs +1 -0
  31. package/dist/windowsHost-CtRidgJp.mjs +309 -0
  32. package/docs/architecture.md +81 -0
  33. package/docs/environments.md +175 -0
  34. package/docs/quickstart.md +59 -4
  35. package/docs/transports.md +189 -0
  36. package/package.json +5 -2
  37. package/dist/registry-zIKWeXV6.mjs +0 -1
  38. package/dist/server-Chka6eAQ.mjs +0 -1
@@ -0,0 +1,516 @@
1
+ import { a as hostOverrideWarning, i as detectEnvironment, l as windowsPathToWslPath, n as cdpProbeHosts, s as isWindowsDrivePath, t as bridgeProbeHost, u as wslPathToWindowsPath } from "./environment-BegP0n2N.mjs";
2
+ import { homedir } from "node:os";
3
+ import path from "node:path";
4
+ import fs from "node:fs";
5
+ import { spawnSync } from "node:child_process";
6
+ // src/core/exeDiscovery.ts
7
+ const EXE_NAME = "lceda-pro.exe";
8
+ const DRIVE_LETTERS = [
9
+ "c",
10
+ "d",
11
+ "e",
12
+ "f"
13
+ ];
14
+ const PROGRAM_DIRS = ["Program Files", "Program Files (x86)"];
15
+ const WSL_HOST_EXE_NAMES = [
16
+ "lceda-pro.exe",
17
+ "lceda-pro",
18
+ "easyeda-pro.exe"
19
+ ];
20
+ const POSIX_EXE_NAMES = [
21
+ "lceda-pro",
22
+ "lceda-pro.exe",
23
+ "easyeda-pro",
24
+ "easyeda-pro.AppImage"
25
+ ];
26
+ function stripQuotes(value) {
27
+ const trimmed = value.trim();
28
+ if (trimmed.length >= 2 && trimmed.startsWith("\"") && trimmed.endsWith("\"")) return trimmed.slice(1, -1);
29
+ return trimmed;
30
+ }
31
+ function splitPath(value, delimiter) {
32
+ if (!value) return [];
33
+ return value.split(delimiter).map((entry) => entry.trim()).filter((entry) => entry.length > 0);
34
+ }
35
+ function pushCandidate(list, seen, candidate) {
36
+ const key = candidate.path.toLowerCase();
37
+ if (seen.has(key)) return;
38
+ seen.add(key);
39
+ list.push(candidate);
40
+ }
41
+ /** macOS `.app` 包展开成包内可执行文件候选;不是 app 包时返回空数组。 */
42
+ function expandMacAppBundle(value) {
43
+ if (!value.toLowerCase().endsWith(".app")) return [];
44
+ return [
45
+ path.join(value, "Contents", "MacOS", "lceda-pro"),
46
+ path.join(value, "Contents", "MacOS", "EasyEDA Pro"),
47
+ path.join(value, "Contents", "MacOS", "EasyEDA")
48
+ ];
49
+ }
50
+ function windowsNativeCandidates(env) {
51
+ const list = [];
52
+ const seen = /* @__PURE__ */ new Set();
53
+ for (const base of [env.ProgramFiles, env["ProgramFiles(x86)"]]) if (base) pushCandidate(list, seen, {
54
+ path: path.win32.join(base, "lceda-pro", EXE_NAME),
55
+ source: "Program Files"
56
+ });
57
+ if (env.LOCALAPPDATA) {
58
+ pushCandidate(list, seen, {
59
+ path: path.win32.join(env.LOCALAPPDATA, "Programs", "lceda-pro", EXE_NAME),
60
+ source: "LOCALAPPDATA\\Programs"
61
+ });
62
+ pushCandidate(list, seen, {
63
+ path: path.win32.join(env.LOCALAPPDATA, "lceda-pro", EXE_NAME),
64
+ source: "LOCALAPPDATA"
65
+ });
66
+ }
67
+ pushCandidate(list, seen, {
68
+ path: "D:\\Program Files\\lceda-pro\\lceda-pro.exe",
69
+ source: "历史默认路径"
70
+ });
71
+ for (const entry of splitPath(env.PATH ?? env.Path, ";")) pushCandidate(list, seen, {
72
+ path: path.win32.join(entry, EXE_NAME),
73
+ source: "PATH"
74
+ });
75
+ return list;
76
+ }
77
+ function wslHostCandidates(env) {
78
+ const list = [];
79
+ const seen = /* @__PURE__ */ new Set();
80
+ const entries = splitPath(env.PATH, ":");
81
+ const hinted = entries.filter((entry) => /lceda|easyeda/i.test(entry));
82
+ const hintedSet = new Set(hinted);
83
+ const others = entries.filter((entry) => !hintedSet.has(entry)).slice(0, 24);
84
+ for (const entry of [...hinted, ...others]) for (const name of WSL_HOST_EXE_NAMES) pushCandidate(list, seen, {
85
+ path: path.posix.join(entry, name),
86
+ source: "WSL PATH"
87
+ });
88
+ for (const drive of DRIVE_LETTERS) for (const dir of PROGRAM_DIRS) pushCandidate(list, seen, {
89
+ path: `/mnt/${drive}/${dir}/lceda-pro/${EXE_NAME}`,
90
+ source: `/mnt/${drive}/${dir}`
91
+ });
92
+ return list;
93
+ }
94
+ function posixCandidates(env, home, options = {}) {
95
+ const list = [];
96
+ const seen = /* @__PURE__ */ new Set();
97
+ for (const entry of splitPath(env.PATH, ":")) {
98
+ if (options.excludeWindowsMounts && /^\/mnt\//i.test(entry)) continue;
99
+ for (const name of POSIX_EXE_NAMES) pushCandidate(list, seen, {
100
+ path: path.posix.join(entry, name),
101
+ source: "PATH"
102
+ });
103
+ }
104
+ for (const fixed of [
105
+ "/opt/lceda-pro/lceda-pro",
106
+ "/opt/easyeda-pro/lceda-pro",
107
+ "/usr/local/bin/lceda-pro",
108
+ "/usr/bin/lceda-pro"
109
+ ]) pushCandidate(list, seen, {
110
+ path: fixed,
111
+ source: "常见安装目录"
112
+ });
113
+ if (home) for (const name of POSIX_EXE_NAMES) pushCandidate(list, seen, {
114
+ path: path.posix.join(home, ".local", "bin", name),
115
+ source: "~/.local/bin"
116
+ });
117
+ return list;
118
+ }
119
+ function macosCandidates(env) {
120
+ const list = [];
121
+ const seen = /* @__PURE__ */ new Set();
122
+ for (const bundle of [
123
+ "/Applications/lceda-pro.app",
124
+ "/Applications/EasyEDA Pro.app",
125
+ path.posix.join(env.HOME ?? "", "Applications", "lceda-pro.app")
126
+ ]) for (const inner of expandMacAppBundle(bundle)) pushCandidate(list, seen, {
127
+ path: inner,
128
+ source: "Applications"
129
+ });
130
+ for (const entry of splitPath(env.PATH, ":")) for (const name of POSIX_EXE_NAMES) pushCandidate(list, seen, {
131
+ path: path.posix.join(entry, name),
132
+ source: "PATH"
133
+ });
134
+ return list;
135
+ }
136
+ /**
137
+ * 生成候选列表。纯函数:不碰文件系统,顺序即优先级。
138
+ * `explicitPath` 不在这里(它单独处理,避免被候选顺序淹没)。
139
+ */
140
+ function clientExeCandidates(environment, env = process.env, location = environment.clientLocations[0] ?? "host") {
141
+ const list = [];
142
+ const seen = /* @__PURE__ */ new Set();
143
+ const fromEnv = stripQuotes(env.LCEDA_PRO_EXE ?? "");
144
+ if (fromEnv) {
145
+ if (environment.kind === "wsl2" && isWindowsDrivePath(fromEnv)) pushCandidate(list, seen, {
146
+ path: windowsPathToWslPath(fromEnv),
147
+ source: "env:LCEDA_PRO_EXE(Windows 路径已转 WSL)"
148
+ });
149
+ else pushCandidate(list, seen, {
150
+ path: fromEnv,
151
+ source: "env:LCEDA_PRO_EXE"
152
+ });
153
+ }
154
+ switch (environment.kind) {
155
+ case "windows-native": return [...list, ...windowsNativeCandidates(env)];
156
+ case "wsl2": return [...list, ...location === "vm" ? posixCandidates(env, env.HOME, { excludeWindowsMounts: true }) : wslHostCandidates(env)];
157
+ case "linux-native": return [...list, ...posixCandidates(env, env.HOME)];
158
+ case "macos-native": return [...list, ...macosCandidates(env)];
159
+ default: return list;
160
+ }
161
+ }
162
+ /** 显式路径归一:Windows 路径在 WSL2 + host 下自动转换;macOS .app 包展开为内部可执行文件。 */
163
+ function normalizeExplicitExe(value, environment, location = environment.clientLocations[0] ?? "host") {
164
+ const cleaned = stripQuotes(value);
165
+ if (!cleaned) return {
166
+ candidates: [],
167
+ hostPath: null
168
+ };
169
+ let hostPath = null;
170
+ const candidates = [];
171
+ if (environment.kind === "wsl2" && location === "host" && isWindowsDrivePath(cleaned)) {
172
+ candidates.push(windowsPathToWslPath(cleaned));
173
+ hostPath = cleaned;
174
+ } else if (environment.kind === "wsl2") {
175
+ candidates.push(cleaned);
176
+ hostPath = wslPathToWindowsPath(cleaned);
177
+ } else {
178
+ candidates.push(cleaned);
179
+ if (environment.kind === "windows-native") hostPath = cleaned;
180
+ }
181
+ if (environment.kind === "macos-native") for (const inner of expandMacAppBundle(cleaned)) candidates.push(inner);
182
+ return {
183
+ candidates: [...new Set(candidates)],
184
+ hostPath
185
+ };
186
+ }
187
+ function defaultRunText(command, args, timeoutMs) {
188
+ try {
189
+ const result = spawnSync(command, [...args], {
190
+ timeout: timeoutMs,
191
+ encoding: "utf8",
192
+ windowsHide: true
193
+ });
194
+ if (result.status !== 0) return null;
195
+ return `${result.stdout ?? ""}`.trim() || null;
196
+ } catch {
197
+ return null;
198
+ }
199
+ }
200
+ function deepProbeCandidates(environment, location, runText) {
201
+ const runner = runText ?? defaultRunText;
202
+ const list = [];
203
+ const seen = /* @__PURE__ */ new Set();
204
+ if (environment.kind === "wsl2" && location === "host") {
205
+ const output = runner("cmd.exe", [
206
+ "/c",
207
+ "where",
208
+ "lceda-pro.exe"
209
+ ], 8e3);
210
+ if (output) for (const line of output.split(/\r?\n/)) {
211
+ const value = line.trim();
212
+ if (!value) continue;
213
+ pushCandidate(list, seen, {
214
+ path: isWindowsDrivePath(value) ? windowsPathToWslPath(value) : value,
215
+ source: "Windows where.exe"
216
+ });
217
+ }
218
+ return list;
219
+ }
220
+ if (environment.kind === "windows-native") {
221
+ const output = runner("where.exe", ["lceda-pro.exe"], 5e3);
222
+ if (output) for (const line of output.split(/\r?\n/)) {
223
+ const value = line.trim();
224
+ if (value) pushCandidate(list, seen, {
225
+ path: value,
226
+ source: "where.exe"
227
+ });
228
+ }
229
+ return list;
230
+ }
231
+ if (environment.kind === "macos-native" || environment.kind === "linux-native" || environment.kind === "wsl2" && location === "vm") {
232
+ const output = runner("sh", ["-lc", "command -v lceda-pro || command -v easyeda-pro"], 5e3);
233
+ if (output) for (const line of output.split(/\r?\n/)) {
234
+ const value = line.trim();
235
+ if (value) pushCandidate(list, seen, {
236
+ path: value,
237
+ source: "command -v"
238
+ });
239
+ }
240
+ }
241
+ return list;
242
+ }
243
+ /**
244
+ * 发现客户端可执行文件。
245
+ *
246
+ * 显式路径优先且**不回退**:用户说了用哪个就必须用哪个,找不到要明确报错,
247
+ * 不能悄悄换成自动发现的结果(否则排查时会以为显式参数生效了)。
248
+ */
249
+ function discoverClientExe(options) {
250
+ const { environment } = options;
251
+ const env = options.env ?? process.env;
252
+ const location = options.location ?? environment.clientLocations[0] ?? "host";
253
+ const exists = options.exists ?? ((file) => fs.existsSync(file));
254
+ const warnings = [];
255
+ const candidates = [];
256
+ if (options.explicitPath && options.explicitPath.trim()) {
257
+ const explicit = normalizeExplicitExe(options.explicitPath, environment, location);
258
+ for (const candidate of explicit.candidates) {
259
+ const present = exists(candidate);
260
+ candidates.push({
261
+ path: candidate,
262
+ source: "显式指定",
263
+ exists: present
264
+ });
265
+ if (present) return {
266
+ status: "explicit",
267
+ path: candidate,
268
+ hostPath: explicit.hostPath,
269
+ source: "显式指定",
270
+ location,
271
+ candidates,
272
+ warnings,
273
+ environment: environment.kind
274
+ };
275
+ }
276
+ warnings.push(`显式指定的客户端路径不存在:${options.explicitPath}`);
277
+ return {
278
+ status: "explicit_missing",
279
+ path: null,
280
+ hostPath: explicit.hostPath,
281
+ source: "显式指定",
282
+ location,
283
+ candidates,
284
+ warnings,
285
+ environment: environment.kind
286
+ };
287
+ }
288
+ const list = clientExeCandidates(environment, env, location);
289
+ for (const candidate of list) {
290
+ const present = exists(candidate.path);
291
+ candidates.push({
292
+ ...candidate,
293
+ exists: present
294
+ });
295
+ if (present) return {
296
+ status: "discovered",
297
+ path: candidate.path,
298
+ hostPath: wslPathToWindowsPath(candidate.path) ?? candidate.path,
299
+ source: candidate.source,
300
+ location,
301
+ candidates,
302
+ warnings,
303
+ environment: environment.kind
304
+ };
305
+ }
306
+ if (options.deep) for (const candidate of deepProbeCandidates(environment, location, options.runText)) {
307
+ const present = exists(candidate.path);
308
+ candidates.push({
309
+ ...candidate,
310
+ exists: present
311
+ });
312
+ if (present) return {
313
+ status: "discovered",
314
+ path: candidate.path,
315
+ hostPath: wslPathToWindowsPath(candidate.path) ?? candidate.path,
316
+ source: candidate.source,
317
+ location,
318
+ candidates,
319
+ warnings,
320
+ environment: environment.kind
321
+ };
322
+ }
323
+ if (environment.kind === "unknown") warnings.push("环境未知:无法生成客户端候选路径,请用 --exe 显式指定");
324
+ return {
325
+ status: "not_found",
326
+ path: null,
327
+ hostPath: null,
328
+ source: "",
329
+ location,
330
+ candidates,
331
+ warnings,
332
+ environment: environment.kind
333
+ };
334
+ }
335
+ /**
336
+ * 发现结果缓存:loadConfig 在 MCP 进程里会被多次调用(每个 server / 每次自检),
337
+ * 而候选路径的 stat 在 WSL 的 /mnt 上并不便宜。文件系统探测结果按环境 + PATH 缓存;
338
+ * 注入探针(测试)不缓存。
339
+ */
340
+ const planCache = /* @__PURE__ */ new Map();
341
+ function planCacheKey(options) {
342
+ if (options.exists || options.runText) return null;
343
+ const env = options.env ?? process.env;
344
+ return [
345
+ options.environment.kind,
346
+ options.environment.clientLocations.join(","),
347
+ options.explicitPath ?? "",
348
+ env.PATH ?? "",
349
+ env.LCEDA_PRO_EXE ?? "",
350
+ env.HOME ?? "",
351
+ env.ProgramFiles ?? "",
352
+ env["ProgramFiles(x86)"] ?? "",
353
+ env.LOCALAPPDATA ?? "",
354
+ options.deep ? "deep" : ""
355
+ ].join("\0");
356
+ }
357
+ /**
358
+ * 按客户端位置优先级依次发现。显式路径在所有位置下都会尝试,
359
+ * 命中即返回该位置;显式路径全都不存在时,整体按 explicit_missing 处理,不回退。
360
+ */
361
+ function discoverClientExePlan(options) {
362
+ const cacheKey = planCacheKey(options);
363
+ if (cacheKey) {
364
+ const hit = planCache.get(cacheKey);
365
+ if (hit) return structuredClone(hit);
366
+ }
367
+ const warnings = [];
368
+ const locations = [];
369
+ const targets = options.environment.clientLocations.length ? options.environment.clientLocations : ["host"];
370
+ for (const location of targets) {
371
+ const result = discoverClientExe({
372
+ ...options,
373
+ location
374
+ });
375
+ locations.push(result);
376
+ warnings.push(...result.warnings);
377
+ if (result.status === "explicit" || result.status === "discovered") return rememberPlan(cacheKey, {
378
+ locations,
379
+ primary: result,
380
+ warnings
381
+ });
382
+ if (result.status === "explicit_missing") return rememberPlan(cacheKey, {
383
+ locations,
384
+ primary: null,
385
+ warnings
386
+ });
387
+ }
388
+ return rememberPlan(cacheKey, {
389
+ locations,
390
+ primary: null,
391
+ warnings
392
+ });
393
+ }
394
+ function rememberPlan(cacheKey, plan) {
395
+ if (cacheKey) planCache.set(cacheKey, structuredClone(plan));
396
+ return plan;
397
+ }
398
+ // src/core/config.ts
399
+ /** 立创EDA专业版历史默认安装路径(仅 Windows 原生环境;可用 `--exe` / LCEDA_PRO_EXE 覆盖)。 */
400
+ const DEFAULT_PRO_EXE = "D:\\Program Files\\lceda-pro\\lceda-pro.exe";
401
+ const DEFAULT_CDP_PORT = 9222;
402
+ const LOOPBACK = "127.0.0.1";
403
+ /** bridge-server.mjs 的固定契约,改动前请同步核对服务端。 */
404
+ const BRIDGE_PORT_FIRST = 49620;
405
+ const BRIDGE_PORT_LAST = 49629;
406
+ const BRIDGE_SERVICE_ID = "easyeda-bridge";
407
+ const BRIDGE_MAX_LOG_BYTES = 5242880;
408
+ const BRIDGE_START_WAIT_MS = 2e4;
409
+ const BRIDGE_RESTART_DELAY_MS = 3e3;
410
+ /** CDP 启动后等待端点就绪的上限(旧脚本为 60 秒)。 */
411
+ const CDP_START_WAIT_MS = 6e4;
412
+ const KIND_SOURCES = [
413
+ "argv",
414
+ "config-file",
415
+ "env",
416
+ "auto-detected"
417
+ ];
418
+ function kindSourceOf(raw, fallback) {
419
+ return KIND_SOURCES.includes(raw) ? raw : fallback;
420
+ }
421
+ function locationSourceOf(raw) {
422
+ if (raw === "argv" || raw === "config-file" || raw === "env" || raw === "default") return raw;
423
+ }
424
+ function intFromEnv(raw, fallback) {
425
+ if (!raw) return fallback;
426
+ const parsed = Number.parseInt(raw, 10);
427
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
428
+ }
429
+ function scanFromEnv(raw) {
430
+ if (!raw) return void 0;
431
+ const match = /^(\d+)\s*[-:]\s*(\d+)$/.exec(raw.trim());
432
+ if (!match) return void 0;
433
+ const start = Number.parseInt(match[1] ?? "", 10);
434
+ const end = Number.parseInt(match[2] ?? "", 10);
435
+ if (!Number.isFinite(start) || !Number.isFinite(end) || start <= 0 || end < start) return void 0;
436
+ return [start, end];
437
+ }
438
+ function homeDir(env = process.env) {
439
+ return env.USERPROFILE || env.HOME || homedir();
440
+ }
441
+ /**
442
+ * 配置优先级:命令行参数 > 配置文件 > .env > 环境变量 > 内置默认值
443
+ * (前四级由 core/runtimeConfig 合并成 env,这里只处理 env + 显式 overrides)。
444
+ *
445
+ * 目录约定:
446
+ * - CDP 运行态放在 ~/.lceda-connect/cdp(可用 LCEDA_CONNECT_HOME 改根)
447
+ * - Bridge 运行态沿用 %LOCALAPPDATA%\lceda-bridge,保证老的 state.json / bridge.log 继续可用
448
+ */
449
+ function loadConfig(env = process.env, overrides = {}, options = {}) {
450
+ const home = homeDir(env);
451
+ const connectHome = env.LCEDA_CONNECT_HOME || path.join(home, ".lceda-connect");
452
+ const cdpDir = path.join(connectHome, "cdp");
453
+ const localAppData = env.LOCALAPPDATA || path.join(home, "AppData", "Local");
454
+ const bridgeDir = env.LCEDA_BRIDGE_RUNTIME || path.join(localAppData, "lceda-bridge");
455
+ const detectOptions = {
456
+ ...options.detect,
457
+ kind: options.detect?.kind ?? env.LCEDA_ENV ?? null,
458
+ windowsHost: options.detect?.windowsHost ?? env.LCEDA_WINDOWS_HOST ?? null,
459
+ clientLocations: options.detect?.clientLocations ?? env.LCEDA_LEDA_IN ?? null,
460
+ cache: options.detect?.probes === void 0
461
+ };
462
+ if (options.detect?.kindSource === void 0 && env.LCEDA_ENV_SOURCE) detectOptions.kindSource = kindSourceOf(env.LCEDA_ENV_SOURCE, "env");
463
+ if (options.detect?.windowsHostSource === void 0 && env.LCEDA_WINDOWS_HOST_SOURCE) detectOptions.windowsHostSource = kindSourceOf(env.LCEDA_WINDOWS_HOST_SOURCE, "env");
464
+ if (options.detect?.clientLocationSource === void 0 && env.LCEDA_LEDA_IN_SOURCE) detectOptions.clientLocationSource = locationSourceOf(env.LCEDA_LEDA_IN_SOURCE);
465
+ const environment = detectEnvironment(detectOptions);
466
+ for (const [value, label] of [[env.LCEDA_CDP_HOST, "CDP 探测地址"], [env.LCEDA_BRIDGE_HOST, "bridge 探测地址"]]) {
467
+ const warning = hostOverrideWarning(environment, value, label);
468
+ if (warning) environment.warnings.push(warning);
469
+ }
470
+ const cdpHosts = cdpProbeHosts(environment, env.LCEDA_CDP_HOST);
471
+ const bridgeHost = bridgeProbeHost(environment, env.LCEDA_BRIDGE_HOST);
472
+ const explicitExe = overrides.cdp?.exePath ?? env.LCEDA_PRO_EXE;
473
+ const exePlan = discoverClientExePlan({
474
+ environment,
475
+ env,
476
+ explicitPath: explicitExe,
477
+ deep: options.deepExe ?? false,
478
+ exists: options.exists
479
+ });
480
+ return {
481
+ cdp: {
482
+ exePath: explicitExe ?? "D:\\Program Files\\lceda-pro\\lceda-pro.exe",
483
+ port: overrides.cdp?.port ?? intFromEnv(env.LCEDA_CDP_PORT, 9222),
484
+ bindHost: LOOPBACK,
485
+ host: cdpHosts[0] ?? "127.0.0.1",
486
+ hosts: cdpHosts,
487
+ runtimeDir: cdpDir,
488
+ pidFile: path.join(cdpDir, "lceda-cdp.pid"),
489
+ logFile: path.join(cdpDir, "lceda-cdp.log"),
490
+ legacyPidFile: path.join(home, ".lceda-cdp", "lceda-cdp.pid"),
491
+ startWaitMs: CDP_START_WAIT_MS,
492
+ scan: scanFromEnv(env.LCEDA_CDP_SCAN)
493
+ },
494
+ bridge: {
495
+ portFirst: intFromEnv(env.LCEDA_BRIDGE_PORT_FIRST, BRIDGE_PORT_FIRST),
496
+ portLast: intFromEnv(env.LCEDA_BRIDGE_PORT_LAST, BRIDGE_PORT_LAST),
497
+ bindHost: LOOPBACK,
498
+ host: bridgeHost,
499
+ serviceId: BRIDGE_SERVICE_ID,
500
+ runtimeDir: bridgeDir,
501
+ logFile: path.join(bridgeDir, "bridge.log"),
502
+ stateFile: path.join(bridgeDir, "state.json"),
503
+ maxLogBytes: BRIDGE_MAX_LOG_BYTES,
504
+ startWaitMs: BRIDGE_START_WAIT_MS,
505
+ restartDelayMs: BRIDGE_RESTART_DELAY_MS,
506
+ nodePath: overrides.bridge?.nodePath ?? env.LCEDA_NODE,
507
+ bridgeScript: overrides.bridge?.bridgeScript ?? env.LCEDA_BRIDGE_SCRIPT
508
+ },
509
+ environment,
510
+ client: {
511
+ plan: exePlan,
512
+ exe: exePlan.primary
513
+ }
514
+ };
515
+ }
516
+ export { loadConfig as i, DEFAULT_PRO_EXE as n, homeDir as r, DEFAULT_CDP_PORT as t };
@@ -1,67 +1,6 @@
1
- import { homedir } from "node:os";
1
+ import { r as homeDir } from "./config-BKbwyvsG.mjs";
2
2
  import path from "node:path";
3
3
  import fs from "node:fs";
4
- // src/core/config.ts
5
- /** 立创EDA专业版默认安装路径(与旧脚本保持一致,可用 LCEDA_PRO_EXE / --exe 覆盖)。 */
6
- const DEFAULT_PRO_EXE = "D:\\Program Files\\lceda-pro\\lceda-pro.exe";
7
- const DEFAULT_CDP_PORT$1 = 9222;
8
- const LOOPBACK = "127.0.0.1";
9
- /** bridge-server.mjs 的固定契约,改动前请同步核对服务端。 */
10
- const BRIDGE_PORT_FIRST = 49620;
11
- const BRIDGE_PORT_LAST = 49629;
12
- const BRIDGE_SERVICE_ID = "easyeda-bridge";
13
- const BRIDGE_MAX_LOG_BYTES = 5242880;
14
- const BRIDGE_START_WAIT_MS = 2e4;
15
- const BRIDGE_RESTART_DELAY_MS = 3e3;
16
- /** CDP 启动后等待端点就绪的上限(旧脚本为 60 秒)。 */
17
- const CDP_START_WAIT_MS = 6e4;
18
- function intFromEnv(raw, fallback) {
19
- if (!raw) return fallback;
20
- const parsed = Number.parseInt(raw, 10);
21
- return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
22
- }
23
- function homeDir(env = process.env) {
24
- return env.USERPROFILE || env.HOME || homedir();
25
- }
26
- /**
27
- * 配置优先级:命令行参数 > 环境变量 > 内置默认值。
28
- * 目录约定:
29
- * - CDP 运行态放在 ~/.lceda-connect/cdp(可用 LCEDA_CONNECT_HOME 改根)
30
- * - Bridge 运行态沿用 %LOCALAPPDATA%\lceda-bridge,保证老的 state.json / bridge.log 继续可用
31
- */
32
- function loadConfig(env = process.env, overrides = {}) {
33
- const home = homeDir(env);
34
- const connectHome = env.LCEDA_CONNECT_HOME || path.join(home, ".lceda-connect");
35
- const cdpDir = path.join(connectHome, "cdp");
36
- const localAppData = env.LOCALAPPDATA || path.join(home, "AppData", "Local");
37
- const bridgeDir = env.LCEDA_BRIDGE_RUNTIME || path.join(localAppData, "lceda-bridge");
38
- return {
39
- cdp: {
40
- exePath: overrides.cdp?.exePath ?? env.LCEDA_PRO_EXE ?? "D:\\Program Files\\lceda-pro\\lceda-pro.exe",
41
- port: overrides.cdp?.port ?? intFromEnv(env.LCEDA_CDP_PORT, 9222),
42
- bindHost: LOOPBACK,
43
- runtimeDir: cdpDir,
44
- pidFile: path.join(cdpDir, "lceda-cdp.pid"),
45
- logFile: path.join(cdpDir, "lceda-cdp.log"),
46
- legacyPidFile: path.join(home, ".lceda-cdp", "lceda-cdp.pid"),
47
- startWaitMs: CDP_START_WAIT_MS
48
- },
49
- bridge: {
50
- portFirst: BRIDGE_PORT_FIRST,
51
- portLast: BRIDGE_PORT_LAST,
52
- bindHost: LOOPBACK,
53
- serviceId: BRIDGE_SERVICE_ID,
54
- runtimeDir: bridgeDir,
55
- logFile: path.join(bridgeDir, "bridge.log"),
56
- stateFile: path.join(bridgeDir, "state.json"),
57
- maxLogBytes: BRIDGE_MAX_LOG_BYTES,
58
- startWaitMs: BRIDGE_START_WAIT_MS,
59
- restartDelayMs: BRIDGE_RESTART_DELAY_MS,
60
- nodePath: overrides.bridge?.nodePath ?? env.LCEDA_NODE,
61
- bridgeScript: overrides.bridge?.bridgeScript ?? env.LCEDA_BRIDGE_SCRIPT
62
- }
63
- };
64
- }
65
4
  // src/cdp/errors.ts
66
5
  /**
67
6
  * CDP 客户端的领域错误。
@@ -137,6 +76,16 @@ function requireLoopbackHost(host) {
137
76
  if (!isLoopbackHost(host)) throw new CdpError("LOOPBACK_ONLY", `只允许回环地址,拒绝连接 ${host}`, { exitCode: 3 });
138
77
  }
139
78
  /**
79
+ * 允许回环地址,或显式列出的「当前环境的宿主地址」(WSL2 → Windows 宿主)。
80
+ * 边界仍然是本机:列表只能由环境发现或用户显式配置产生,不接受任意远端主机。
81
+ */
82
+ function requireAllowedHost(host, allowedHosts = []) {
83
+ if (isLoopbackHost(host)) return;
84
+ const target = host.trim().toLowerCase();
85
+ if (allowedHosts.some((allowed) => allowed.trim().toLowerCase() === target)) return;
86
+ throw new CdpError("LOOPBACK_ONLY", `只允许回环地址或当前环境的宿主地址,拒绝连接 ${host}`, { exitCode: 3 });
87
+ }
88
+ /**
140
89
  * 候选 profile 根目录:Windows 扫 %LOCALAPPDATA% 下以 LCEDA / EasyEDA 开头的目录;
141
90
  * 其他平台对应 `~/.config`。返回排序后的绝对路径。
142
91
  */
@@ -267,7 +216,7 @@ function attributionOf(item) {
267
216
  * 的那份才是当前实例;没有匹配项时才退回「活但未记录 uuid」的候选(cli / scan)。
268
217
  */
269
218
  async function discover(host = DEFAULT_CDP_HOST, options = {}) {
270
- requireLoopbackHost(host);
219
+ requireAllowedHost(host, options.allowedHosts);
271
220
  const candidates = (options.records ?? readDevToolsActivePorts(profileRoots(options.env ?? process.env, options.platform ?? process.platform))).map((record) => ({
272
221
  port: record.port,
273
222
  source: "DevToolsActivePort",
@@ -342,4 +291,4 @@ function parsePortRange(text) {
342
291
  if (low <= 0 || low > 65535 || high < low || high > 65535) return null;
343
292
  return [low, high];
344
293
  }
345
- export { loadConfig as C, homeDir as S, asCdpError as _, defaultPortFromEnv as a, DEFAULT_CDP_PORT$1 as b, fetchJson as c, profileLabel as d, requireLoopbackHost as f, CdpError as g, versionWebSocketUrl as h, browserUuid as i, parseBrowserUuidLine as l, versionBrowser as m, DEFAULT_CDP_PORT as n, discover as o, resolveEndpoint as p, attributionOf as r, endpointUrl as s, DEFAULT_CDP_HOST as t, parsePortRange as u, clipText as v, DEFAULT_PRO_EXE as x, coerceText as y };
294
+ export { CdpError as _, defaultPortFromEnv as a, coerceText as b, fetchJson as c, profileLabel as d, requireAllowedHost as f, versionWebSocketUrl as g, versionBrowser as h, browserUuid as i, parseBrowserUuidLine as l, resolveEndpoint as m, DEFAULT_CDP_PORT as n, discover as o, requireLoopbackHost as p, attributionOf as r, endpointUrl as s, DEFAULT_CDP_HOST as t, parsePortRange as u, asCdpError as v, clipText as y };