dsh-plugin-mesh 0.5.2 → 0.5.4

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.
@@ -37,6 +37,7 @@ import {
37
37
  __hasUncertainProbe,
38
38
  __probeFailCount,
39
39
  __isUncertain,
40
+ __gpuProbeSpecs,
40
41
  } from "./capabilities.js";
41
42
  import { MeshDiscovery, APP_TAG } from "./discovery.js";
42
43
 
@@ -107,6 +108,30 @@ test("parseWindowsGpu 取第一行(多显卡时)", () => {
107
108
  assert.equal(g.name, "Intel(R) UHD Graphics");
108
109
  });
109
110
 
111
+ test("parseWindowsGpu 取第一条**真卡**(虚拟适配器排在前面时)", () => {
112
+ // 现场复现(真机):MuMu / GameViewer / Parsec 这类虚拟显示驱动常常排在
113
+ // 真卡前面,原实现"取第一行"于是把 3060 的机器报成 MuMuVMM。
114
+ const g = parseWindowsGpu("MuMuVMM Display Adapter\r\nNVIDIA GeForce RTX 3060");
115
+ assert.equal(g.name, "NVIDIA GeForce RTX 3060");
116
+ });
117
+
118
+ test("parseWindowsGpu 全是虚拟适配器时退回第一行,而不是说「没有 GPU」", () => {
119
+ // 「只有虚拟显卡」是一台真机器的真实状态;报 null 等于"我们不知道",
120
+ // 会让 Placement 把一台能用的机器当成没探过。退回第一行至少是真话。
121
+ const g = parseWindowsGpu("Parsec Virtual Display Adapter\r\nBasic Display Adapter");
122
+ assert.equal(g.present, true);
123
+ assert.equal(g.name, "Parsec Virtual Display Adapter");
124
+ });
125
+
126
+ test("GPU 探测候选链:Windows 先问 nvidia-smi,再退回 powershell", () => {
127
+ const win = __gpuProbeSpecs("win32");
128
+ assert.equal(win.length, 2, "Windows 应该有两个候选");
129
+ assert.equal(win[0].cmd, "nvidia-smi", "先问最可信的 nvidia-smi(只报真 N 卡)");
130
+ assert.equal(win[1].cmd, "powershell");
131
+ assert.equal(__gpuProbeSpecs("darwin").length, 1);
132
+ assert.equal(__gpuProbeSpecs("plan9").length, 0, "不认识的平台不该凭空造命令");
133
+ });
134
+
110
135
  test("传入非字符串(对象)→ 返回 null「未知」,不是「确定没有」也不是乱码", () => {
111
136
  // ⚠️ 这就是真实踩到的回归:runProbe 的返回值从 string 改成 {output,error}
112
137
  // 之后 collectGpu 漏改,把**对象**交给了解析函数。旧实现里 String(对象) 变成
@@ -652,6 +677,24 @@ test("探测没探准 → 标出来,并说明不能据此断定「没有」",
652
677
  assert.ok(s.includes("不能据此断定"), s);
653
678
  });
654
679
 
680
+ test("sanitizeCapabilities:保留并清洗 WSL 运行时", () => {
681
+ const caps = sanitizeCapabilities({
682
+ runtimes: { node: "20.19" },
683
+ wsl: [
684
+ { name: "Ubuntu", runtimes: { node: "22.1", python: "3.12" }, error: "" },
685
+ { name: "", runtimes: { node: "1.0" } },
686
+ { name: "Debian", runtimes: { git: "2.43" }, error: "x".repeat(100) },
687
+ ],
688
+ });
689
+ assert.equal(caps.wsl.length, 2, "空名字的项应被丢掉");
690
+ assert.equal(caps.wsl[0].name, "Ubuntu");
691
+ assert.equal(caps.wsl[0].runtimes.node, "22.1");
692
+ assert.equal(caps.wsl[1].runtimes.git, "2.43");
693
+ assert.ok(caps.wsl[1].error.length <= 80);
694
+ });
695
+
696
+ console.log("\n── 指纹认领的边界:同机多实例不该互相顶掉 ──");
697
+
655
698
  console.log("\n── 指纹认领的边界:同机多实例不该互相顶掉 ──");
656
699
 
657
700
  test("同指纹 + 同名(重装场景)→ 认领,旧记录被顶掉", () => {
@@ -0,0 +1,92 @@
1
+ /**
2
+ * 诊断块自测(`lib/diagnostics.js`)。
3
+ *
4
+ * 这组断言盯着一个反复踩的坑:**"进程版本 ≠ 磁盘版本"肉眼看不出来**。
5
+ * 磁盘已经升到 0.5.3、宿主进程还在跑 0.5.2 时,界面必须能自己说清楚,
6
+ * 否则用户会把旧行为当成新版本的 bug 报上来。
7
+ *
8
+ * node lib/_selftest-diagnostics.mjs
9
+ */
10
+ import { strict as assert } from "node:assert";
11
+ import { fileURLToPath } from "node:url";
12
+ import { dirname, join } from "node:path";
13
+ import { buildDiagnostics, entryInfo, readPackageVersion } from "./diagnostics.js";
14
+
15
+ const HERE = dirname(fileURLToPath(import.meta.url));
16
+ const ENTRY = new URL("index.js", new URL(`file://${HERE}/`)).href;
17
+
18
+ let passed = 0;
19
+ let failed = 0;
20
+ function test(name, fn) {
21
+ try {
22
+ fn();
23
+ passed += 1;
24
+ console.log(` ✓ ${name}`);
25
+ } catch (err) {
26
+ failed += 1;
27
+ console.log(` ✗ ${name}`);
28
+ console.log(` ${err?.message ?? err}`);
29
+ }
30
+ }
31
+
32
+ console.log("\n── 版本漂移:进程 vs 磁盘 ──");
33
+
34
+ test("版本一致时 versionMatches=true,且不报磁盘更新", () => {
35
+ const d = buildDiagnostics({ version: "0.5.3", packageVersion: "0.5.3", entryMtimeMs: 0, startedAt: Date.now() });
36
+ assert.equal(d.versionMatches, true);
37
+ assert.equal(d.diskNewerThanProcess, false);
38
+ });
39
+
40
+ test("升级了没重启:versionMatches=false,能看出磁盘比进程新", () => {
41
+ const startedAt = Date.now() - 60_000;
42
+ const d = buildDiagnostics({ version: "0.5.2", packageVersion: "0.5.3", entryMtimeMs: Date.now(), startedAt });
43
+ assert.equal(d.versionMatches, false);
44
+ assert.equal(d.diskNewerThanProcess, true, "入口文件比进程启动还新 → 就是没重启");
45
+ });
46
+
47
+ test("入口 mtime 拿不到时不能诬告「磁盘更新」", () => {
48
+ const d = buildDiagnostics({ version: "0.5.3", packageVersion: "0.5.3", entryMtimeMs: 0, startedAt: Date.now() });
49
+ assert.equal(d.diskNewerThanProcess, false, "mtime 未知 ≠ 磁盘更新");
50
+ });
51
+
52
+ test("空版本号不算匹配(宁可说不知道)", () => {
53
+ const d = buildDiagnostics({ version: "", packageVersion: "0.5.3" });
54
+ assert.equal(d.versionMatches, false);
55
+ });
56
+
57
+ console.log("\n── 不泄露密钥 ──");
58
+
59
+ test("config 只挑安全字段,tsnetAuthKey / secret 一律不出现", () => {
60
+ const d = buildDiagnostics({
61
+ version: "0.5.3",
62
+ packageVersion: "0.5.3",
63
+ config: { port: 45917, requirePairing: true, tsnetAuthKey: "tskey-auth-SECRET", secret: "s2", apiToken: "tok" },
64
+ });
65
+ assert.equal(d.config.port, 45917);
66
+ assert.equal(d.config.requirePairing, true);
67
+ const text = JSON.stringify(d);
68
+ assert.ok(!text.includes("SECRET"), "tsnet 授权码泄进了诊断块");
69
+ assert.ok(!text.includes("\"tok\""), "apiToken 泄进了诊断块");
70
+ assert.ok(!("secret" in d.config));
71
+ });
72
+
73
+ console.log("\n── 真实磁盘读取 ──");
74
+
75
+ test("readPackageVersion 读得到本仓库版本号", () => {
76
+ const v = readPackageVersion(ENTRY);
77
+ assert.match(v, /^\d+\.\d+\.\d+/, `读到的版本号是 ${JSON.stringify(v)}`);
78
+ });
79
+
80
+ test("entryInfo 给出真实路径与 mtime", () => {
81
+ const info = entryInfo(ENTRY);
82
+ assert.ok(info.path.endsWith(join("lib", "index.js")), info.path);
83
+ assert.ok(info.mtimeMs > 0);
84
+ });
85
+
86
+ test("入口 URL 是垃圾时安静返回空,不抛", () => {
87
+ assert.equal(readPackageVersion("not-a-url"), "");
88
+ assert.deepEqual(entryInfo("not-a-url"), { path: "", mtimeMs: 0 });
89
+ });
90
+
91
+ console.log(`\n结果:${passed} 通过,${failed} 失败`);
92
+ if (failed > 0) process.exit(1);
@@ -0,0 +1,379 @@
1
+ /**
2
+ * osInfo 的自测:纯 fixture,不碰真实系统。
3
+ *
4
+ * 为什么不用真实采集:macOS/Android 的分支在开发机上跑不到;这些平台差异
5
+ * 正是最容易出 bug 的地方(sw_vers 输出、os-release 引号、getprop 换行)。
6
+ */
7
+ import assert from "node:assert/strict";
8
+ import { detectOsInfo, sanitizeOsInfo, detectSystems, sanitizeSystems } from "./osinfo.js";
9
+ import { MeshDiscovery } from "./discovery.js";
10
+
11
+ let passed = 0;
12
+ let failed = 0;
13
+ function check(label, fn) {
14
+ try {
15
+ fn();
16
+ passed += 1;
17
+ console.log(` ✓ ${label}`);
18
+ } catch (err) {
19
+ failed += 1;
20
+ console.log(` ✗ ${label}`);
21
+ console.log(` ${err?.message ?? err}`);
22
+ }
23
+ }
24
+
25
+ console.log("\n── 平台分支:四个系统都要给出可读名字/版本 ──");
26
+
27
+ check("win32:os.version() 是产品名,release 是版本", () => {
28
+ const info = detectOsInfo({
29
+ platform: "win32",
30
+ release: "10.0.26200",
31
+ arch: "x64",
32
+ osVersion: () => "Windows 11 Home China",
33
+ });
34
+ assert.equal(info.platform, "win32");
35
+ assert.equal(info.name, "Windows 11 Home China");
36
+ assert.equal(info.version, "10.0.26200");
37
+ assert.equal(info.kernel, "10.0.26200");
38
+ assert.equal(info.arch, "x64");
39
+ assert.equal(info.distro, "");
40
+ });
41
+
42
+ check("win32:os.version() 拿不到时退回 Windows,不抛", () => {
43
+ const info = detectOsInfo({ platform: "win32", release: "10.0.19045", arch: "x64", osVersion: () => "" });
44
+ assert.equal(info.name, "Windows");
45
+ assert.equal(info.version, "10.0.19045");
46
+ });
47
+
48
+ check("darwin:sw_vers 的 productName/productVersion", () => {
49
+ const info = detectOsInfo({
50
+ platform: "darwin",
51
+ release: "24.1.0",
52
+ arch: "arm64",
53
+ run: (_cmd, args) => (args[0] === "-productName" ? "macOS" : "15.1"),
54
+ });
55
+ assert.equal(info.platform, "darwin");
56
+ assert.equal(info.name, "macOS");
57
+ assert.equal(info.version, "15.1");
58
+ assert.equal(info.kernel, "24.1.0");
59
+ assert.equal(info.arch, "arm64");
60
+ });
61
+
62
+ check("darwin:sw_vers 不可用时仍有 macOS 名字", () => {
63
+ const info = detectOsInfo({
64
+ platform: "darwin",
65
+ release: "24.1.0",
66
+ arch: "arm64",
67
+ run: () => {
68
+ throw new Error("ENOENT");
69
+ },
70
+ });
71
+ assert.equal(info.name, "macOS");
72
+ assert.equal(info.version, "");
73
+ assert.equal(info.kernel, "24.1.0");
74
+ });
75
+
76
+ check("linux:解析 /etc/os-release 的 NAME/VERSION_ID/ID", () => {
77
+ const fixture = [
78
+ 'PRETTY_NAME="Ubuntu 26.04 LTS"',
79
+ 'NAME="Ubuntu"',
80
+ 'VERSION_ID="26.04"',
81
+ "ID=ubuntu",
82
+ "",
83
+ ].join("\n");
84
+ const info = detectOsInfo({
85
+ platform: "linux",
86
+ release: "6.6.114.1-microsoft-standard-WSL2",
87
+ arch: "x64",
88
+ env: { XDG_CURRENT_DESKTOP: "ubuntu:GNOME" },
89
+ readFile: () => fixture,
90
+ });
91
+ assert.equal(info.platform, "linux");
92
+ assert.equal(info.name, "Ubuntu");
93
+ assert.equal(info.version, "26.04");
94
+ assert.equal(info.distro, "ubuntu");
95
+ assert.equal(info.desktop, "GNOME");
96
+ assert.equal(info.kernel, "6.6.114.1-microsoft-standard-WSL2");
97
+ });
98
+
99
+ check("linux:os-release 缺失时退回 Linux,desktop 可为空", () => {
100
+ const info = detectOsInfo({
101
+ platform: "linux",
102
+ release: "6.1.0",
103
+ arch: "x64",
104
+ env: {},
105
+ readFile: () => {
106
+ throw new Error("ENOENT");
107
+ },
108
+ });
109
+ assert.equal(info.name, "Linux");
110
+ assert.equal(info.version, "");
111
+ assert.equal(info.desktop, "");
112
+ });
113
+
114
+ check("android:getprop 的版本号,带换行也能取对", () => {
115
+ const info = detectOsInfo({
116
+ platform: "android",
117
+ release: "5.10.101-android13",
118
+ arch: "arm64",
119
+ run: () => "14\n",
120
+ });
121
+ assert.equal(info.platform, "android");
122
+ assert.equal(info.name, "Android");
123
+ assert.equal(info.version, "14");
124
+ assert.equal(info.arch, "arm64");
125
+ });
126
+
127
+ check("未知平台:给可读名字,platform 留空(UI 走回退徽章)", () => {
128
+ const info = detectOsInfo({ platform: "freebsd", release: "14.0", arch: "x64" });
129
+ assert.equal(info.platform, "");
130
+ assert.equal(info.name, "freebsd");
131
+ assert.equal(info.version, "14.0");
132
+ });
133
+
134
+ console.log("\n── sanitizeOsInfo:对端名片是网络输入 ──");
135
+
136
+ check("platform 白名单:未知值被清空", () => {
137
+ const info = sanitizeOsInfo({ platform: "plan9", name: "Plan 9" });
138
+ assert.equal(info.platform, "");
139
+ assert.equal(info.name, "Plan 9");
140
+ });
141
+
142
+ check("控制字符剥掉、空白折叠、字段截断", () => {
143
+ const info = sanitizeOsInfo({
144
+ platform: "linux",
145
+ name: ` Ubuntu\u0000\t${"x".repeat(80)} `,
146
+ version: "24.04\n",
147
+ kernel: "k".repeat(100),
148
+ desktop: "GNOME",
149
+ arch: "x64",
150
+ });
151
+ assert.ok(!info.name.includes("\u0000"));
152
+ assert.ok(info.name.length <= 48, `name 未截断: ${info.name.length}`);
153
+ assert.equal(info.version, "24.04");
154
+ assert.ok(info.kernel.length <= 64);
155
+ });
156
+
157
+ check("空对象 / 垃圾输入 → null(调用方回退老 os 字段)", () => {
158
+ assert.equal(sanitizeOsInfo(null), null);
159
+ assert.equal(sanitizeOsInfo(undefined), null);
160
+ assert.equal(sanitizeOsInfo("win32"), null);
161
+ assert.equal(sanitizeOsInfo([]), null);
162
+ assert.equal(sanitizeOsInfo({}), null);
163
+ assert.equal(sanitizeOsInfo({ platform: "", name: "" }), null);
164
+ });
165
+
166
+ check("老对端只报 os 字符串:没有 osInfo 时不伪造", () => {
167
+ assert.equal(sanitizeOsInfo(undefined), null);
168
+ });
169
+
170
+ console.log("\n── systems:宿主机 + WSL 发行版(只读注册表,不启动发行版)──");
171
+
172
+ const WSL_REG = [
173
+ "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss",
174
+ " DefaultVersion REG_DWORD 0x2",
175
+ "",
176
+ "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss\\{1111}",
177
+ " State REG_DWORD 0x1",
178
+ " DistributionName REG_SZ Ubuntu",
179
+ " Flavor REG_SZ ubuntu",
180
+ " OsVersion REG_SZ 26.04",
181
+ "",
182
+ "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss\\{2222}",
183
+ " State REG_DWORD 0x1",
184
+ " DistributionName REG_SZ docker-desktop",
185
+ ].join("\n");
186
+
187
+ const winOpts = (run, readFile) => ({
188
+ platform: "win32",
189
+ release: "10.0.26200",
190
+ arch: "x64",
191
+ osVersion: () => "Windows 11 Home China",
192
+ run,
193
+ readFile,
194
+ });
195
+
196
+ check("win32:宿主机 + Ubuntu,docker-desktop 被过滤,且有版本时不该读 UNC", () => {
197
+ const list = detectSystems(
198
+ winOpts(
199
+ () => WSL_REG,
200
+ () => {
201
+ throw new Error("不该在读不到的情况下访问 UNC");
202
+ },
203
+ ),
204
+ );
205
+ assert.equal(list.length, 2);
206
+ assert.equal(list[0].kind, "host");
207
+ assert.equal(list[0].name, "Windows 11 Home China");
208
+ assert.equal(list[1].kind, "wsl");
209
+ assert.equal(list[1].platform, "linux");
210
+ assert.equal(list[1].name, "Ubuntu");
211
+ assert.equal(list[1].version, "26.04");
212
+ assert.equal(list[1].distro, "ubuntu");
213
+ });
214
+
215
+ check("WSL 注册表缺 OsVersion 时,运行中的发行版读 /etc/os-release 补版本", () => {
216
+ const reg = [
217
+ "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss\\{3333}",
218
+ " State REG_DWORD 0x1",
219
+ " DistributionName REG_SZ Debian",
220
+ ].join("\n");
221
+ const list = detectSystems(
222
+ winOpts(
223
+ () => reg,
224
+ (path) => {
225
+ assert.match(path, /Debian/);
226
+ return 'NAME="Debian"\nVERSION_ID="12"\nID=debian\n';
227
+ },
228
+ ),
229
+ );
230
+ assert.equal(list.length, 2);
231
+ assert.equal(list[1].name, "Debian");
232
+ assert.equal(list[1].version, "12");
233
+ assert.equal(list[1].distro, "debian");
234
+ });
235
+
236
+ check("停着的 WSL 发行版不读 UNC(不启动它),版本留空", () => {
237
+ const reg = [
238
+ "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss\\{4444}",
239
+ " State REG_DWORD 0x0",
240
+ " DistributionName REG_SZ openSUSE",
241
+ ].join("\n");
242
+ const list = detectSystems(
243
+ winOpts(
244
+ () => reg,
245
+ () => {
246
+ throw new Error("停着的发行版不该被访问");
247
+ },
248
+ ),
249
+ );
250
+ assert.equal(list.length, 2);
251
+ assert.equal(list[1].name, "openSUSE");
252
+ assert.equal(list[1].version, "");
253
+ });
254
+
255
+ check("没有 WSL 注册表 → 只报宿主机", () => {
256
+ const list = detectSystems(
257
+ winOpts(
258
+ () => {
259
+ throw new Error("ERROR: The system was unable to find the specified registry key");
260
+ },
261
+ () => "",
262
+ ),
263
+ );
264
+ assert.equal(list.length, 1);
265
+ assert.equal(list[0].kind, "host");
266
+ });
267
+
268
+ check("非 win32:不探测 WSL,只报宿主机", () => {
269
+ const list = detectSystems({
270
+ platform: "linux",
271
+ release: "6.1.0",
272
+ arch: "x64",
273
+ env: {},
274
+ readFile: () => 'NAME="Debian"\nVERSION_ID="12"\nID=debian\n',
275
+ run: () => {
276
+ throw new Error("非 Windows 不该跑 reg");
277
+ },
278
+ });
279
+ assert.equal(list.length, 1);
280
+ assert.equal(list[0].kind, "host");
281
+ assert.equal(list[0].name, "Debian");
282
+ });
283
+
284
+ check("sanitizeSystems:去重、kind 白名单、最多 4 项、宿主机排第一", () => {
285
+ const list = sanitizeSystems([
286
+ { kind: "wsl", platform: "linux", name: "Ubuntu", version: "24.04" },
287
+ { kind: "host", platform: "win32", name: "Win", version: "11" },
288
+ { kind: "wsl", platform: "linux", name: "Ubuntu", version: "24.04" },
289
+ { kind: "vm", platform: "linux", name: "Debian", version: "12" },
290
+ { kind: "container", platform: "linux", name: "Alpine", version: "3.20" },
291
+ { kind: "nope", platform: "linux", name: "Other", version: "1" },
292
+ ]);
293
+ assert.equal(list.length, 4);
294
+ assert.equal(list[0].kind, "host");
295
+ assert.equal(list[0].name, "Win");
296
+ assert.ok(!list.some((s) => s.name === "Other"), "第 5 个及以后的系统应被截断");
297
+ });
298
+
299
+ console.log("\n── 名片收发:selfRecord 带 osInfo,对端 osInfo 会被清洗 ──");
300
+
301
+ check("selfRecord 带本机 osInfo", () => {
302
+ const d = new MeshDiscovery({ id: "self-os", port: 0, meta: { machine: "me" } });
303
+ const info = d.selfRecord().osInfo;
304
+ assert.ok(info, "selfRecord 应带 osInfo");
305
+ assert.ok(info.name, "至少要有可读的系统名");
306
+ assert.equal(info.platform, process.platform === "win32" ? "win32" : info.platform);
307
+ const list = d.selfRecord().systems;
308
+ assert.ok(Array.isArray(list) && list.length >= 1, "selfRecord 应带 systems[]");
309
+ assert.equal(list[0].kind, "host");
310
+ });
311
+
312
+ check("对端 osInfo 被保存;旧对端没有该字段时为 null", () => {
313
+ const d = new MeshDiscovery({ id: "self-os", port: 0, meta: { machine: "me" } });
314
+ d.note(
315
+ {
316
+ app: "dsh-mesh",
317
+ id: "peer-mac",
318
+ machine: "mac",
319
+ os: "darwin 24.1.0",
320
+ osInfo: {
321
+ platform: "darwin",
322
+ name: "macOS",
323
+ version: "15.1",
324
+ distro: "",
325
+ kernel: "24.1.0",
326
+ desktop: "",
327
+ arch: "arm64",
328
+ },
329
+ },
330
+ "10.0.0.9",
331
+ );
332
+ assert.equal(d.peers()[0].osInfo.platform, "darwin");
333
+ assert.equal(d.peers()[0].osInfo.version, "15.1");
334
+
335
+ d.note({ app: "dsh-mesh", id: "peer-old", machine: "old", os: "linux 6.1.0" }, "10.0.0.10");
336
+ const old = d.peers().find((p) => p.machine === "old");
337
+ assert.equal(old.osInfo, null, "旧对端不该被伪造出一个 osInfo");
338
+ });
339
+
340
+ check("对端 systems[] 被清洗并保存;宿主机排第一", () => {
341
+ const d = new MeshDiscovery({ id: "self-sys", port: 0, meta: { machine: "me" } });
342
+ d.note(
343
+ {
344
+ app: "dsh-mesh",
345
+ id: "peer-sys",
346
+ machine: "multi",
347
+ systems: [
348
+ { kind: "wsl", platform: "linux", name: "Ubuntu", version: "26.04", distro: "ubuntu" },
349
+ { kind: "host", platform: "win32", name: "Windows 11", version: "10.0.26200", arch: "x64" },
350
+ { kind: "wsl", platform: "linux", name: "Ubuntu", version: "26.04", distro: "ubuntu" },
351
+ ],
352
+ },
353
+ "10.0.0.12",
354
+ );
355
+ const list = d.peers()[0].systems;
356
+ assert.equal(list.length, 2, "重复项应被去掉");
357
+ assert.equal(list[0].kind, "host");
358
+ assert.equal(list[1].kind, "wsl");
359
+ });
360
+
361
+ check("对端送垃圾 osInfo:platform 白名单 + 文本截断", () => {
362
+ const d = new MeshDiscovery({ id: "self-os", port: 0, meta: { machine: "me" } });
363
+ d.note(
364
+ {
365
+ app: "dsh-mesh",
366
+ id: "peer-evil",
367
+ machine: "evil",
368
+ osInfo: { platform: "evil", name: "<script>".repeat(20), version: "x".repeat(100) },
369
+ },
370
+ "10.0.0.11",
371
+ );
372
+ const info = d.peers()[0].osInfo;
373
+ assert.equal(info.platform, "");
374
+ assert.ok(info.name.length <= 48);
375
+ assert.ok(info.version.length <= 48);
376
+ });
377
+
378
+ console.log(`\n结果:${passed} 通过,${failed} 失败`);
379
+ process.exit(failed === 0 ? 0 : 1);