@iyowei/sweep-node-modules 0.5.0 → 1.0.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/README.md +32 -29
- package/README.zh-CN.md +164 -161
- package/dist/codes.d.ts +4 -2
- package/dist/config.d.ts +1 -1
- package/dist/delete.d.ts +3 -11
- package/dist/display.d.ts +1 -1
- package/dist/errors.d.ts +39 -24
- package/dist/guard.d.ts +10 -8
- package/dist/index.d.ts +3 -3
- package/dist/index.js +747 -324
- package/dist/manifest.json +3 -3
- package/dist/scan.d.ts +2 -6
- package/dist/skip.d.ts +1 -1
- package/dist/summary.d.ts +4 -1
- package/dist/types.d.ts +17 -11
- package/package.json +8 -5
- package/dist/runtime.d.ts +0 -26
- package/dist/scan-native.d.ts +0 -2
- package/dist/scan-parallel.d.ts +0 -2
- package/dist/scan-prune.d.ts +0 -2
- package/dist/size-du.d.ts +0 -33
- package/dist/size-js.d.ts +0 -2
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { homedir } from "node:os";
|
|
|
7
7
|
import { posix, win32 } from "node:path";
|
|
8
8
|
|
|
9
9
|
// src/errors.ts
|
|
10
|
-
var SWEEP_ERROR_CODES = new Set([
|
|
10
|
+
var SWEEP_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
11
11
|
"CONFIG_READ_FAILED",
|
|
12
12
|
"CONFIG_CORRUPT_JSON",
|
|
13
13
|
"CONFIG_CORRUPT_SHAPE",
|
|
@@ -20,8 +20,8 @@ class SweepError extends Error {
|
|
|
20
20
|
name = "SweepError";
|
|
21
21
|
code;
|
|
22
22
|
details;
|
|
23
|
-
constructor(code, message, details) {
|
|
24
|
-
super(message);
|
|
23
|
+
constructor(code, message, details, options) {
|
|
24
|
+
super(message, options);
|
|
25
25
|
this.code = code;
|
|
26
26
|
this.details = details;
|
|
27
27
|
}
|
|
@@ -30,11 +30,14 @@ function isSweepError(value) {
|
|
|
30
30
|
if (typeof value !== "object" || value === null)
|
|
31
31
|
return false;
|
|
32
32
|
const candidate = value;
|
|
33
|
-
return candidate.name === "SweepError" && typeof candidate.code === "string" && SWEEP_ERROR_CODES.has(candidate.code);
|
|
33
|
+
return candidate.name === "SweepError" && typeof candidate.message === "string" && typeof candidate.code === "string" && SWEEP_ERROR_CODES.has(candidate.code);
|
|
34
|
+
}
|
|
35
|
+
function isSweepErrorCode(value, code) {
|
|
36
|
+
return isSweepError(value) && value.code === code;
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
// src/config.ts
|
|
37
|
-
var DEFAULT_EXCLUDE = [
|
|
40
|
+
var DEFAULT_EXCLUDE = Object.freeze([
|
|
38
41
|
".bun",
|
|
39
42
|
".npm",
|
|
40
43
|
".pnpm-store",
|
|
@@ -55,7 +58,7 @@ var DEFAULT_EXCLUDE = [
|
|
|
55
58
|
".config",
|
|
56
59
|
".cache",
|
|
57
60
|
".claude"
|
|
58
|
-
];
|
|
61
|
+
]);
|
|
59
62
|
function platformDefaultPath(platform, env, home) {
|
|
60
63
|
if (platform === "win32") {
|
|
61
64
|
const roaming = env.APPDATA || win32.join(env.USERPROFILE || home, "AppData", "Roaming");
|
|
@@ -105,6 +108,15 @@ function fieldError(container, field, optional) {
|
|
|
105
108
|
message: `${field} 第 ${badIndex + 1} 项应为字符串 (实际: ${describeActual(value[badIndex])})`
|
|
106
109
|
};
|
|
107
110
|
}
|
|
111
|
+
if (field === "roots") {
|
|
112
|
+
const emptyIndex = value.findIndex((item) => item === "");
|
|
113
|
+
if (emptyIndex !== -1) {
|
|
114
|
+
return {
|
|
115
|
+
field,
|
|
116
|
+
message: `${field} 第 ${emptyIndex + 1} 项不能为空字符串`
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
108
120
|
return null;
|
|
109
121
|
}
|
|
110
122
|
function shapeError(value) {
|
|
@@ -130,13 +142,13 @@ async function loadConfig(path) {
|
|
|
130
142
|
} catch (error) {
|
|
131
143
|
if (error.code === "ENOENT")
|
|
132
144
|
return { state: "absent" };
|
|
133
|
-
throw new SweepError("CONFIG_READ_FAILED", `配置读取失败 (${path}): ${error.message}`, { path, errno: error.code });
|
|
145
|
+
throw new SweepError("CONFIG_READ_FAILED", `配置读取失败 (${path}): ${error.message}`, { path, errno: error.code }, { cause: error });
|
|
134
146
|
}
|
|
135
147
|
let data;
|
|
136
148
|
try {
|
|
137
149
|
data = JSON.parse(text);
|
|
138
150
|
} catch (error) {
|
|
139
|
-
throw new SweepError("CONFIG_CORRUPT_JSON", `配置损坏 (${path}): JSON 解析失败: ${error.message}`, { path });
|
|
151
|
+
throw new SweepError("CONFIG_CORRUPT_JSON", `配置损坏 (${path}): JSON 解析失败: ${error.message}`, { path }, { cause: error });
|
|
140
152
|
}
|
|
141
153
|
const problem = shapeError(data);
|
|
142
154
|
if (problem !== null) {
|
|
@@ -167,16 +179,16 @@ function mergeNames(configNames, cliNames) {
|
|
|
167
179
|
import { lstat, realpath, stat } from "node:fs/promises";
|
|
168
180
|
import { homedir as homedir2 } from "node:os";
|
|
169
181
|
import { posix as posix2, win32 as win322 } from "node:path";
|
|
170
|
-
var POSIX_STYLE = {
|
|
182
|
+
var POSIX_STYLE = Object.freeze({
|
|
171
183
|
name: "posix",
|
|
172
184
|
ops: posix2,
|
|
173
185
|
caseInsensitive: false
|
|
174
|
-
};
|
|
175
|
-
var WIN32_STYLE = {
|
|
186
|
+
});
|
|
187
|
+
var WIN32_STYLE = Object.freeze({
|
|
176
188
|
name: "win32",
|
|
177
189
|
ops: win322,
|
|
178
190
|
caseInsensitive: true
|
|
179
|
-
};
|
|
191
|
+
});
|
|
180
192
|
function nativeStyle() {
|
|
181
193
|
return process.platform === "win32" ? WIN32_STYLE : POSIX_STYLE;
|
|
182
194
|
}
|
|
@@ -190,7 +202,11 @@ function hasNodeModulesLeaf(target, style) {
|
|
|
190
202
|
return fold(style.ops.basename(target), style) === "node_modules";
|
|
191
203
|
}
|
|
192
204
|
function insideAnyRoot(realPath, roots, style) {
|
|
205
|
+
if (realPath === "")
|
|
206
|
+
return false;
|
|
193
207
|
return roots.some((root) => {
|
|
208
|
+
if (root === "")
|
|
209
|
+
return false;
|
|
194
210
|
const rel = style.ops.relative(fold(root, style), fold(realPath, style));
|
|
195
211
|
if (rel === "" || style.ops.isAbsolute(rel))
|
|
196
212
|
return false;
|
|
@@ -198,10 +214,16 @@ function insideAnyRoot(realPath, roots, style) {
|
|
|
198
214
|
});
|
|
199
215
|
}
|
|
200
216
|
function isFilesystemRootBody(realPath, style) {
|
|
201
|
-
|
|
217
|
+
if (realPath === "")
|
|
218
|
+
return false;
|
|
219
|
+
const normalized = style.ops.normalize(realPath);
|
|
220
|
+
const root = style.ops.parse(normalized).root;
|
|
221
|
+
return root !== "" && fold(root, style) === fold(normalized, style);
|
|
202
222
|
}
|
|
203
223
|
function isHomeBody(realPath, home, style) {
|
|
204
|
-
|
|
224
|
+
if (home === null || home === "" || realPath === "")
|
|
225
|
+
return false;
|
|
226
|
+
return style.ops.relative(fold(home, style), fold(realPath, style)) === "";
|
|
205
227
|
}
|
|
206
228
|
function firstSymlinkOnAnchor(links) {
|
|
207
229
|
for (const link of links) {
|
|
@@ -211,11 +233,13 @@ function firstSymlinkOnAnchor(links) {
|
|
|
211
233
|
return null;
|
|
212
234
|
}
|
|
213
235
|
function anchorChainPaths(root, style) {
|
|
214
|
-
const absolute = style.ops.isAbsolute(root) ? root : style.ops.resolve(root);
|
|
236
|
+
const absolute = style.ops.isAbsolute(root) ? style.ops.normalize(root) : style.ops.resolve(root);
|
|
215
237
|
const prefix = style.ops.parse(absolute).root;
|
|
238
|
+
const tail = absolute.slice(prefix.length);
|
|
239
|
+
const parts = style.name === "win32" ? tail.split(/[\\/]+/) : tail.split(style.ops.sep);
|
|
216
240
|
const chain = [];
|
|
217
241
|
let cursor = prefix;
|
|
218
|
-
for (const part of
|
|
242
|
+
for (const part of parts) {
|
|
219
243
|
if (part === "")
|
|
220
244
|
continue;
|
|
221
245
|
cursor = style.ops.join(cursor, part);
|
|
@@ -246,6 +270,11 @@ async function firstSymlinkOnRoot(root) {
|
|
|
246
270
|
return firstSymlinkOnTarget(root);
|
|
247
271
|
}
|
|
248
272
|
async function validateTargets(targets, options) {
|
|
273
|
+
if (options.signal?.aborted) {
|
|
274
|
+
throw new SweepError("CANCELLED", "安全闸校验在逐目标检查点被取消", {
|
|
275
|
+
phase: "validate"
|
|
276
|
+
});
|
|
277
|
+
}
|
|
249
278
|
const style = options.style ?? nativeStyle();
|
|
250
279
|
const homeOption = options.home === undefined ? homedir2() : options.home;
|
|
251
280
|
const anchors = [];
|
|
@@ -372,7 +401,7 @@ function owningRoot(target, roots, style) {
|
|
|
372
401
|
for (const root of roots) {
|
|
373
402
|
if (!insideAnyRoot(target, [root], style))
|
|
374
403
|
continue;
|
|
375
|
-
if (best === null || root
|
|
404
|
+
if (best === null || insideAnyRoot(root, [best], style))
|
|
376
405
|
best = root;
|
|
377
406
|
}
|
|
378
407
|
return best;
|
|
@@ -407,11 +436,12 @@ function crossDeviceIndex(entries) {
|
|
|
407
436
|
|
|
408
437
|
// src/classify.ts
|
|
409
438
|
var fold2 = (text, style) => style.caseInsensitive ? text.toLowerCase() : text;
|
|
410
|
-
var INSTALL_TREE_SEGMENTS = new Set([...DEFAULT_EXCLUDE, "extensions", "_npx"].map((name) => name.toLowerCase()));
|
|
439
|
+
var INSTALL_TREE_SEGMENTS = /* @__PURE__ */ new Set([...DEFAULT_EXCLUDE, "extensions", "_npx"].map((name) => name.toLowerCase()));
|
|
411
440
|
function classifyTarget(target, options = {}) {
|
|
412
441
|
const style = options.style ?? nativeStyle();
|
|
413
442
|
const homeOption = options.home === undefined ? homedir3() : options.home;
|
|
414
|
-
const
|
|
443
|
+
const normalizedTarget = style.ops.normalize(target);
|
|
444
|
+
const parts = (style.name === "win32" ? normalizedTarget.split(/[\\/]+/) : normalizedTarget.split(style.ops.sep)).filter((part) => part !== "");
|
|
415
445
|
const ancestors = parts.slice(0, -1);
|
|
416
446
|
for (const segment of ancestors) {
|
|
417
447
|
if (INSTALL_TREE_SEGMENTS.has(segment.toLowerCase()))
|
|
@@ -426,9 +456,9 @@ function classifyTarget(target, options = {}) {
|
|
|
426
456
|
kind: "suspect-install-tree",
|
|
427
457
|
reason: "父目录为 lib, 形如版本管理器的 node 安装树"
|
|
428
458
|
};
|
|
429
|
-
if (homeOption !== null) {
|
|
430
|
-
const relative = style.ops.relative(fold2(homeOption, style), fold2(
|
|
431
|
-
const [first] = relative.split(style.ops.sep);
|
|
459
|
+
if (homeOption !== null && homeOption !== "") {
|
|
460
|
+
const relative = style.ops.relative(fold2(homeOption, style), fold2(normalizedTarget, style));
|
|
461
|
+
const [first] = style.name === "win32" ? relative.split(/[\\/]+/) : relative.split(style.ops.sep);
|
|
432
462
|
if (first !== undefined && first !== ".." && first !== "." && first.startsWith(".") && !style.ops.isAbsolute(relative))
|
|
433
463
|
return {
|
|
434
464
|
kind: "suspect-install-tree",
|
|
@@ -438,8 +468,10 @@ function classifyTarget(target, options = {}) {
|
|
|
438
468
|
return { kind: "project" };
|
|
439
469
|
}
|
|
440
470
|
// src/delete.ts
|
|
441
|
-
import { lstat as
|
|
442
|
-
|
|
471
|
+
import { lstat as lstat3, realpath as realpath2, rm } from "node:fs/promises";
|
|
472
|
+
|
|
473
|
+
// src/delete-review.ts
|
|
474
|
+
import { lstat as lstat2 } from "node:fs/promises";
|
|
443
475
|
var ERROR_HINTS = {
|
|
444
476
|
EACCES: "权限不足, 拒绝删除",
|
|
445
477
|
EPERM: "操作不被允许",
|
|
@@ -447,7 +479,6 @@ var ERROR_HINTS = {
|
|
|
447
479
|
ENOTEMPTY: "目录非空",
|
|
448
480
|
EROFS: "目标位于只读文件系统"
|
|
449
481
|
};
|
|
450
|
-
var PARTIAL_DELETION_HINT = "注意: 目录内容可能已被部分或全部删除, 请复查";
|
|
451
482
|
function describeCode(error) {
|
|
452
483
|
const fsError = error;
|
|
453
484
|
const code = fsError?.code;
|
|
@@ -456,48 +487,51 @@ function describeCode(error) {
|
|
|
456
487
|
}
|
|
457
488
|
return `${code}: ${ERROR_HINTS[code] ?? "删除未成功"}`;
|
|
458
489
|
}
|
|
459
|
-
function describeRemovalError(target, error) {
|
|
460
|
-
return `${describeCode(error)} (目标: ${target}; ${PARTIAL_DELETION_HINT})`;
|
|
461
|
-
}
|
|
462
490
|
function describeReviewError(target, path, error) {
|
|
463
491
|
return `安全复核未完成 (${describeCode(error)}) (目标: ${target}; 组件: ${path}; 未执行删除)`;
|
|
464
492
|
}
|
|
465
493
|
function describeVanishedReview(target, path) {
|
|
466
494
|
return `安全复核未完成 (ENOENT: 路径组件消失) (目标: ${target}; 组件: ${path}; 请复查目标是否仍存在; 未执行删除; 若目标确已不存在, 可忽略此条)`;
|
|
467
495
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
return "present";
|
|
472
|
-
} catch (error) {
|
|
473
|
-
return error?.code === "ENOENT" ? "gone" : "unknown";
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
function describeSurvivor(target, state) {
|
|
477
|
-
const review = state === "present" ? "复核确认目标仍存在" : "目标是否仍存在未能核验";
|
|
478
|
-
return `ENOENT: 删除中途失败 (${review}) (目标: ${target}; ${PARTIAL_DELETION_HINT})`;
|
|
479
|
-
}
|
|
480
|
-
function isUnder(root, target) {
|
|
481
|
-
const rel = relative(root, target);
|
|
482
|
-
if (rel === "" || isAbsolute(rel))
|
|
483
|
-
return false;
|
|
484
|
-
return rel !== ".." && !rel.startsWith(`..${sep}`);
|
|
485
|
-
}
|
|
486
|
-
function componentChain(root, target) {
|
|
496
|
+
function componentChain(root, target, style) {
|
|
497
|
+
const fold = (p) => style.caseInsensitive ? p.toLowerCase() : p;
|
|
498
|
+
const rel = style.ops.relative(fold(root), fold(target));
|
|
487
499
|
const chain = [root];
|
|
488
500
|
let cursor = root;
|
|
489
|
-
for (const part of
|
|
490
|
-
cursor = join(cursor, part);
|
|
501
|
+
for (const part of rel.split(style.ops.sep).slice(0, -1)) {
|
|
502
|
+
cursor = style.ops.join(cursor, part);
|
|
491
503
|
chain.push(cursor);
|
|
492
504
|
}
|
|
493
505
|
return chain;
|
|
494
506
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
return
|
|
498
|
-
}
|
|
499
|
-
|
|
507
|
+
function targetShapeViolation(target, style) {
|
|
508
|
+
if (target.endsWith("/") || target.endsWith("\\")) {
|
|
509
|
+
return `安全复核失败 (目标路径不得含尾分隔符): ${target}`;
|
|
510
|
+
}
|
|
511
|
+
const segments = style.name === "win32" ? target.split(/[\\/]+/) : target.split("/");
|
|
512
|
+
if (segments.some((segment) => segment === "." || segment === "..")) {
|
|
513
|
+
return `安全复核失败 (目标路径不得含相对段 . 或 ..): ${target}`;
|
|
500
514
|
}
|
|
515
|
+
if (!hasNodeModulesLeaf(target, style)) {
|
|
516
|
+
return `安全复核失败 (路径末段不是 node_modules): ${target}`;
|
|
517
|
+
}
|
|
518
|
+
return null;
|
|
519
|
+
}
|
|
520
|
+
function rootAncestorChain(root, style) {
|
|
521
|
+
const chain = [];
|
|
522
|
+
const seen = new Set;
|
|
523
|
+
const append = (raw) => {
|
|
524
|
+
for (const step of anchorChainPaths(raw, style)) {
|
|
525
|
+
const key = dedupeKey(step, style);
|
|
526
|
+
if (seen.has(key))
|
|
527
|
+
continue;
|
|
528
|
+
seen.add(key);
|
|
529
|
+
chain.push(step);
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
append(root.configured);
|
|
533
|
+
append(root.real);
|
|
534
|
+
return chain;
|
|
501
535
|
}
|
|
502
536
|
async function reviewComponents(chain) {
|
|
503
537
|
for (const path of chain) {
|
|
@@ -514,9 +548,183 @@ async function reviewComponents(chain) {
|
|
|
514
548
|
}
|
|
515
549
|
return null;
|
|
516
550
|
}
|
|
551
|
+
function toReviewFailure(target, finding) {
|
|
552
|
+
if (finding.kind === "vanished") {
|
|
553
|
+
return {
|
|
554
|
+
target,
|
|
555
|
+
code: "REVIEW_COMPONENT_VANISHED",
|
|
556
|
+
errno: "ENOENT",
|
|
557
|
+
message: describeVanishedReview(target, finding.path),
|
|
558
|
+
partialRisk: false
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
return {
|
|
562
|
+
target,
|
|
563
|
+
code: "REVIEW_UNVERIFIED",
|
|
564
|
+
errno: finding.error?.code,
|
|
565
|
+
message: describeReviewError(target, finding.path, finding.error),
|
|
566
|
+
partialRisk: false
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
function resolveTargetRootSync(target, roots, style) {
|
|
570
|
+
const violation = targetShapeViolation(target, style);
|
|
571
|
+
if (violation !== null) {
|
|
572
|
+
return {
|
|
573
|
+
kind: "aborted",
|
|
574
|
+
aborted: {
|
|
575
|
+
target,
|
|
576
|
+
code: "REVIEW_NOT_UNDER_ANY_ROOT",
|
|
577
|
+
message: violation,
|
|
578
|
+
path: target
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
const root = roots.find((candidate) => insideAnyRoot(target, [candidate.real], style));
|
|
583
|
+
if (root === undefined) {
|
|
584
|
+
return {
|
|
585
|
+
kind: "aborted",
|
|
586
|
+
aborted: {
|
|
587
|
+
target,
|
|
588
|
+
code: "REVIEW_NOT_UNDER_ANY_ROOT",
|
|
589
|
+
message: `安全复核失败 (目标不在任何 roots 之下): ${target}`,
|
|
590
|
+
path: target
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
return { kind: "ok", root };
|
|
595
|
+
}
|
|
596
|
+
async function reviewPathOnDisk(target, root, style) {
|
|
597
|
+
const headFinding = await reviewComponents(rootAncestorChain(root, style));
|
|
598
|
+
if (headFinding !== null) {
|
|
599
|
+
if (headFinding.kind === "unsafe") {
|
|
600
|
+
const location = headFinding.path === root.configured ? root.configured : `${headFinding.path} (根: ${root.configured})`;
|
|
601
|
+
return {
|
|
602
|
+
kind: "aborted",
|
|
603
|
+
aborted: {
|
|
604
|
+
target,
|
|
605
|
+
code: "REVIEW_HEAD_SYMLINK",
|
|
606
|
+
message: `安全复核失败 (根被替换为符号链接): ${location}`,
|
|
607
|
+
path: headFinding.path
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
return { kind: "failed", failure: toReviewFailure(target, headFinding) };
|
|
612
|
+
}
|
|
613
|
+
const finding = await reviewComponents(componentChain(root.real, target, style));
|
|
614
|
+
if (finding !== null) {
|
|
615
|
+
if (finding.kind === "unsafe") {
|
|
616
|
+
return {
|
|
617
|
+
kind: "aborted",
|
|
618
|
+
aborted: {
|
|
619
|
+
target,
|
|
620
|
+
code: "REVIEW_COMPONENT_REPLACED",
|
|
621
|
+
message: `安全复核失败 (路径组件被替换): ${finding.path}`,
|
|
622
|
+
path: finding.path
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
return { kind: "failed", failure: toReviewFailure(target, finding) };
|
|
627
|
+
}
|
|
628
|
+
return { kind: "ok" };
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// src/stale.ts
|
|
632
|
+
function splitStaleRejections(rejected, staleTargets) {
|
|
633
|
+
if (staleTargets !== "missing")
|
|
634
|
+
return { rejected, stale: [] };
|
|
635
|
+
const rest = [];
|
|
636
|
+
const stale = [];
|
|
637
|
+
for (const item of rejected) {
|
|
638
|
+
if (item.code === "GUARD_TARGET_MISSING")
|
|
639
|
+
stale.push(item.target);
|
|
640
|
+
else
|
|
641
|
+
rest.push(item);
|
|
642
|
+
}
|
|
643
|
+
return { rejected: rest, stale };
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// src/delete.ts
|
|
647
|
+
var PARTIAL_DELETION_HINT = "注意: 目录内容可能已被部分或全部删除, 请复查";
|
|
648
|
+
function describeRemovalError(target, error) {
|
|
649
|
+
return `${describeCode(error)} (目标: ${target}; ${PARTIAL_DELETION_HINT})`;
|
|
650
|
+
}
|
|
651
|
+
async function checkGone(path) {
|
|
652
|
+
try {
|
|
653
|
+
await lstat3(path);
|
|
654
|
+
return "present";
|
|
655
|
+
} catch (error) {
|
|
656
|
+
return error?.code === "ENOENT" ? "gone" : "unknown";
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
function describeSurvivor(target, state) {
|
|
660
|
+
const review = state === "present" ? "复核确认目标仍存在" : "目标是否仍存在未能核验";
|
|
661
|
+
return `ENOENT: 删除中途失败 (${review}) (目标: ${target}; ${PARTIAL_DELETION_HINT})`;
|
|
662
|
+
}
|
|
517
663
|
var REMOVAL_CONCURRENCY = 4;
|
|
664
|
+
async function executeRm(target) {
|
|
665
|
+
try {
|
|
666
|
+
await rm(target, { recursive: true, force: false });
|
|
667
|
+
return { kind: "removed", target };
|
|
668
|
+
} catch (error) {
|
|
669
|
+
if (error?.code === "ENOENT") {
|
|
670
|
+
const state = await checkGone(target);
|
|
671
|
+
if (state === "gone")
|
|
672
|
+
return { kind: "missing", target };
|
|
673
|
+
return {
|
|
674
|
+
kind: "failed",
|
|
675
|
+
failure: {
|
|
676
|
+
target,
|
|
677
|
+
code: "REMOVE_ENOENT_SURVIVOR",
|
|
678
|
+
errno: "ENOENT",
|
|
679
|
+
message: describeSurvivor(target, state),
|
|
680
|
+
partialRisk: true
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
return {
|
|
685
|
+
kind: "failed",
|
|
686
|
+
failure: {
|
|
687
|
+
target,
|
|
688
|
+
code: "REMOVE_FAILED",
|
|
689
|
+
errno: error?.code,
|
|
690
|
+
message: describeRemovalError(target, error),
|
|
691
|
+
partialRisk: true
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
function assembleRemovalResult(slots, aborted) {
|
|
697
|
+
const result = { removed: [], missing: [], failed: [] };
|
|
698
|
+
for (const slot of slots) {
|
|
699
|
+
if (slot === undefined)
|
|
700
|
+
continue;
|
|
701
|
+
switch (slot.kind) {
|
|
702
|
+
case "removed":
|
|
703
|
+
result.removed.push(slot.target);
|
|
704
|
+
break;
|
|
705
|
+
case "missing":
|
|
706
|
+
result.missing.push(slot.target);
|
|
707
|
+
break;
|
|
708
|
+
case "failed":
|
|
709
|
+
result.failed.push(slot.failure);
|
|
710
|
+
break;
|
|
711
|
+
case "aborted":
|
|
712
|
+
break;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
if (aborted !== undefined)
|
|
716
|
+
result.aborted = aborted;
|
|
717
|
+
return result;
|
|
718
|
+
}
|
|
518
719
|
async function removeTargets(targets, options) {
|
|
720
|
+
if (options?.signal?.aborted) {
|
|
721
|
+
throw new SweepError("CANCELLED", "删除在条目间检查点被取消", {
|
|
722
|
+
phase: "remove",
|
|
723
|
+
partial: { removed: [], missing: [], failed: [] }
|
|
724
|
+
});
|
|
725
|
+
}
|
|
519
726
|
const roots = options?.roots ?? [];
|
|
727
|
+
const style = options?.style ?? nativeStyle();
|
|
520
728
|
const signal = options?.signal;
|
|
521
729
|
const emit = options?.onProgress;
|
|
522
730
|
const slots = Array.from({
|
|
@@ -538,7 +746,7 @@ async function removeTargets(targets, options) {
|
|
|
538
746
|
const drained = new Promise((resolve) => {
|
|
539
747
|
resolveDrained = resolve;
|
|
540
748
|
});
|
|
541
|
-
const halted = () => cancelled || aborted !== undefined;
|
|
749
|
+
const halted = () => cancelled || aborted !== undefined || failures.length > 0;
|
|
542
750
|
function pump() {
|
|
543
751
|
while (active < REMOVAL_CONCURRENCY && next < targets.length && !halted()) {
|
|
544
752
|
const index = next;
|
|
@@ -561,98 +769,26 @@ async function removeTargets(targets, options) {
|
|
|
561
769
|
cancelled = true;
|
|
562
770
|
return;
|
|
563
771
|
}
|
|
564
|
-
const
|
|
565
|
-
if (
|
|
566
|
-
|
|
567
|
-
target,
|
|
568
|
-
code: "REVIEW_NOT_UNDER_ANY_ROOT",
|
|
569
|
-
message: `安全复核失败 (目标不在任何 roots 之下): ${target}`,
|
|
570
|
-
path: target
|
|
571
|
-
};
|
|
572
|
-
recordAbort(index, info);
|
|
772
|
+
const syncCheck = resolveTargetRootSync(target, roots, style);
|
|
773
|
+
if (syncCheck.kind === "aborted") {
|
|
774
|
+
recordAbort(index, syncCheck.aborted);
|
|
573
775
|
slots[index] = { kind: "aborted" };
|
|
574
|
-
emit?.({ kind: "aborted", aborted: info });
|
|
575
776
|
return;
|
|
576
777
|
}
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
code: "REVIEW_HEAD_SYMLINK",
|
|
581
|
-
message: `安全复核失败 (根被替换为符号链接): ${root.configured}`,
|
|
582
|
-
path: root.configured
|
|
583
|
-
};
|
|
584
|
-
recordAbort(index, info);
|
|
778
|
+
const review = await reviewPathOnDisk(target, syncCheck.root, style);
|
|
779
|
+
if (review.kind === "aborted") {
|
|
780
|
+
recordAbort(index, review.aborted);
|
|
585
781
|
slots[index] = { kind: "aborted" };
|
|
586
|
-
emit?.({ kind: "aborted", aborted: info });
|
|
587
782
|
return;
|
|
588
783
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
const vanished = {
|
|
593
|
-
target,
|
|
594
|
-
code: "REVIEW_COMPONENT_VANISHED",
|
|
595
|
-
message: describeVanishedReview(target, finding.path),
|
|
596
|
-
partialRisk: false
|
|
597
|
-
};
|
|
598
|
-
slots[index] = { kind: "failed", failure: vanished };
|
|
599
|
-
emit?.({ kind: "failed", failure: vanished });
|
|
600
|
-
return;
|
|
601
|
-
}
|
|
602
|
-
if (finding.kind === "unsafe") {
|
|
603
|
-
const info = {
|
|
604
|
-
target,
|
|
605
|
-
code: "REVIEW_COMPONENT_REPLACED",
|
|
606
|
-
message: `安全复核失败 (路径组件被替换): ${finding.path}`,
|
|
607
|
-
path: finding.path
|
|
608
|
-
};
|
|
609
|
-
recordAbort(index, info);
|
|
610
|
-
slots[index] = { kind: "aborted" };
|
|
611
|
-
emit?.({ kind: "aborted", aborted: info });
|
|
612
|
-
return;
|
|
613
|
-
}
|
|
614
|
-
const unverified = {
|
|
615
|
-
target,
|
|
616
|
-
code: "REVIEW_UNVERIFIED",
|
|
617
|
-
message: describeReviewError(target, finding.path, finding.error),
|
|
618
|
-
partialRisk: false
|
|
619
|
-
};
|
|
620
|
-
slots[index] = { kind: "failed", failure: unverified };
|
|
621
|
-
emit?.({ kind: "failed", failure: unverified });
|
|
784
|
+
if (review.kind === "failed") {
|
|
785
|
+
slots[index] = { kind: "failed", failure: review.failure };
|
|
786
|
+
emit?.({ kind: "failed", failure: review.failure });
|
|
622
787
|
return;
|
|
623
788
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
emit?.({ kind: "removed", target });
|
|
628
|
-
} catch (error) {
|
|
629
|
-
if (error?.code === "ENOENT") {
|
|
630
|
-
const state = await checkGone(target);
|
|
631
|
-
if (state === "gone") {
|
|
632
|
-
slots[index] = { kind: "missing", target };
|
|
633
|
-
emit?.({ kind: "missing", target });
|
|
634
|
-
return;
|
|
635
|
-
}
|
|
636
|
-
const survivor = {
|
|
637
|
-
target,
|
|
638
|
-
code: "REMOVE_ENOENT_SURVIVOR",
|
|
639
|
-
message: describeSurvivor(target, state),
|
|
640
|
-
partialRisk: true
|
|
641
|
-
};
|
|
642
|
-
slots[index] = { kind: "failed", failure: survivor };
|
|
643
|
-
emit?.({ kind: "failed", failure: survivor });
|
|
644
|
-
return;
|
|
645
|
-
}
|
|
646
|
-
const failure = {
|
|
647
|
-
target,
|
|
648
|
-
code: "REMOVE_FAILED",
|
|
649
|
-
errno: error?.code,
|
|
650
|
-
message: describeRemovalError(target, error),
|
|
651
|
-
partialRisk: true
|
|
652
|
-
};
|
|
653
|
-
slots[index] = { kind: "failed", failure };
|
|
654
|
-
emit?.({ kind: "failed", failure });
|
|
655
|
-
}
|
|
789
|
+
const outcome = await executeRm(target);
|
|
790
|
+
slots[index] = outcome;
|
|
791
|
+
emit?.(outcome);
|
|
656
792
|
} catch (error) {
|
|
657
793
|
if (failures.length === 0)
|
|
658
794
|
failures.push(error);
|
|
@@ -667,26 +803,9 @@ async function removeTargets(targets, options) {
|
|
|
667
803
|
await drained;
|
|
668
804
|
if (failures.length > 0)
|
|
669
805
|
throw failures[0];
|
|
670
|
-
const result =
|
|
671
|
-
for (const slot of slots) {
|
|
672
|
-
if (slot === undefined)
|
|
673
|
-
continue;
|
|
674
|
-
switch (slot.kind) {
|
|
675
|
-
case "removed":
|
|
676
|
-
result.removed.push(slot.target);
|
|
677
|
-
break;
|
|
678
|
-
case "missing":
|
|
679
|
-
result.missing.push(slot.target);
|
|
680
|
-
break;
|
|
681
|
-
case "failed":
|
|
682
|
-
result.failed.push(slot.failure);
|
|
683
|
-
break;
|
|
684
|
-
case "aborted":
|
|
685
|
-
break;
|
|
686
|
-
}
|
|
687
|
-
}
|
|
806
|
+
const result = assembleRemovalResult(slots, aborted);
|
|
688
807
|
if (aborted !== undefined)
|
|
689
|
-
|
|
808
|
+
emit?.({ kind: "aborted", aborted });
|
|
690
809
|
if (cancelled) {
|
|
691
810
|
throw new SweepError("CANCELLED", "删除在条目间检查点被取消", {
|
|
692
811
|
phase: "remove",
|
|
@@ -701,19 +820,6 @@ async function toTrustRoots(roots) {
|
|
|
701
820
|
real: await realpath2(root).catch(() => null) ?? root
|
|
702
821
|
})));
|
|
703
822
|
}
|
|
704
|
-
function splitStaleRejections(rejected, staleTargets) {
|
|
705
|
-
if (staleTargets !== "missing")
|
|
706
|
-
return { rejected, stale: [] };
|
|
707
|
-
const rest = [];
|
|
708
|
-
const stale = [];
|
|
709
|
-
for (const item of rejected) {
|
|
710
|
-
if (item.code === "GUARD_TARGET_MISSING")
|
|
711
|
-
stale.push(item.target);
|
|
712
|
-
else
|
|
713
|
-
rest.push(item);
|
|
714
|
-
}
|
|
715
|
-
return { rejected: rest, stale };
|
|
716
|
-
}
|
|
717
823
|
async function removeBatch(targets, options) {
|
|
718
824
|
const validation = await validateTargets(targets, {
|
|
719
825
|
roots: options.roots,
|
|
@@ -727,6 +833,7 @@ async function removeBatch(targets, options) {
|
|
|
727
833
|
}
|
|
728
834
|
const removal = await removeTargets(validation.accepted, {
|
|
729
835
|
roots: await toTrustRoots(options.roots),
|
|
836
|
+
style: options.style,
|
|
730
837
|
signal: options.signal,
|
|
731
838
|
onProgress: options.onProgress
|
|
732
839
|
});
|
|
@@ -758,7 +865,7 @@ var sanitizeOutputLine = (text) => {
|
|
|
758
865
|
};
|
|
759
866
|
// src/scan-parallel.ts
|
|
760
867
|
import { readdir, realpath as realpath3, stat as stat2 } from "node:fs/promises";
|
|
761
|
-
import { join
|
|
868
|
+
import { join } from "node:path";
|
|
762
869
|
var NODE_MODULES = "node_modules";
|
|
763
870
|
var GIT_DIR = ".git";
|
|
764
871
|
var CONCURRENCY = 32;
|
|
@@ -814,7 +921,6 @@ async function walkRoot(root, ctx) {
|
|
|
814
921
|
onProgress
|
|
815
922
|
} = ctx;
|
|
816
923
|
const stack = [{ dir: root, included: include.size === 0 }];
|
|
817
|
-
let outstanding = 1;
|
|
818
924
|
let active = 0;
|
|
819
925
|
const failures = [];
|
|
820
926
|
let resolveDrained = null;
|
|
@@ -822,12 +928,13 @@ async function walkRoot(root, ctx) {
|
|
|
822
928
|
resolveDrained = resolve;
|
|
823
929
|
});
|
|
824
930
|
function pump() {
|
|
825
|
-
while (active < CONCURRENCY && stack.length > 0) {
|
|
931
|
+
while (active < CONCURRENCY && stack.length > 0 && failures.length === 0 && !signal?.aborted) {
|
|
826
932
|
const next = stack.pop();
|
|
827
933
|
active += 1;
|
|
828
934
|
visit(next.dir, next.included);
|
|
829
935
|
}
|
|
830
|
-
|
|
936
|
+
const stopped = failures.length > 0 || signal?.aborted === true;
|
|
937
|
+
if (active === 0 && (stack.length === 0 || stopped) && resolveDrained !== null) {
|
|
831
938
|
const resolve = resolveDrained;
|
|
832
939
|
resolveDrained = null;
|
|
833
940
|
resolve();
|
|
@@ -843,11 +950,13 @@ async function walkRoot(root, ctx) {
|
|
|
843
950
|
let entries;
|
|
844
951
|
try {
|
|
845
952
|
entries = await readdir(dir, { withFileTypes: true });
|
|
846
|
-
} catch {
|
|
953
|
+
} catch (error) {
|
|
954
|
+
const errno = error?.code;
|
|
847
955
|
warnings.push({
|
|
848
956
|
code: "SCAN_DIR_UNREADABLE",
|
|
849
957
|
message: `目录不可读, 已跳过: ${dir}`,
|
|
850
|
-
path: dir
|
|
958
|
+
path: dir,
|
|
959
|
+
...typeof errno === "string" ? { errno } : {}
|
|
851
960
|
});
|
|
852
961
|
return;
|
|
853
962
|
}
|
|
@@ -858,7 +967,7 @@ async function walkRoot(root, ctx) {
|
|
|
858
967
|
if (name === NODE_MODULES) {
|
|
859
968
|
if (!included)
|
|
860
969
|
continue;
|
|
861
|
-
const target =
|
|
970
|
+
const target = join(dir, name);
|
|
862
971
|
const key = dedupeKey(await realpath3(target).catch(() => target), style);
|
|
863
972
|
if (!hits.has(key)) {
|
|
864
973
|
const hit = { project: dir, target, root };
|
|
@@ -877,15 +986,13 @@ async function walkRoot(root, ctx) {
|
|
|
877
986
|
excludeCounts.set(name, (excludeCounts.get(name) ?? 0) + 1);
|
|
878
987
|
continue;
|
|
879
988
|
}
|
|
880
|
-
stack.push({ dir:
|
|
881
|
-
outstanding += 1;
|
|
989
|
+
stack.push({ dir: join(dir, name), included: childIncluded });
|
|
882
990
|
}
|
|
883
991
|
} catch (error) {
|
|
884
992
|
if (failures.length === 0)
|
|
885
993
|
failures.push(error);
|
|
886
994
|
} finally {
|
|
887
995
|
active -= 1;
|
|
888
|
-
outstanding -= 1;
|
|
889
996
|
pump();
|
|
890
997
|
}
|
|
891
998
|
}
|
|
@@ -893,11 +1000,21 @@ async function walkRoot(root, ctx) {
|
|
|
893
1000
|
await drained;
|
|
894
1001
|
if (failures.length > 0)
|
|
895
1002
|
throw failures[0];
|
|
1003
|
+
if (signal?.aborted) {
|
|
1004
|
+
throw new SweepError("CANCELLED", "扫描在遍历任务检查点被取消", {
|
|
1005
|
+
phase: "scan"
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
896
1008
|
}
|
|
897
1009
|
function createParallelScanner() {
|
|
898
1010
|
return {
|
|
899
1011
|
name: "parallel",
|
|
900
1012
|
async scan(options) {
|
|
1013
|
+
if (options.signal?.aborted) {
|
|
1014
|
+
throw new SweepError("CANCELLED", "扫描在根预检阶段被取消", {
|
|
1015
|
+
phase: "scan"
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
901
1018
|
const warnings = [];
|
|
902
1019
|
const exclude = new Set(options.exclude);
|
|
903
1020
|
const excludeCounts = new Map;
|
|
@@ -916,6 +1033,11 @@ function createParallelScanner() {
|
|
|
916
1033
|
const seenRoots = new Set;
|
|
917
1034
|
const roots = [];
|
|
918
1035
|
for (const root of options.roots) {
|
|
1036
|
+
if (options.signal?.aborted) {
|
|
1037
|
+
throw new SweepError("CANCELLED", "扫描在根预检阶段被取消", {
|
|
1038
|
+
phase: "scan"
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
919
1041
|
let key;
|
|
920
1042
|
try {
|
|
921
1043
|
const resolved = await realpath3(root);
|
|
@@ -964,13 +1086,178 @@ function createParallelScanner() {
|
|
|
964
1086
|
}
|
|
965
1087
|
};
|
|
966
1088
|
}
|
|
1089
|
+
|
|
1090
|
+
// src/scan.ts
|
|
1091
|
+
function createScanner() {
|
|
1092
|
+
return createParallelScanner();
|
|
1093
|
+
}
|
|
967
1094
|
// src/size-du.ts
|
|
968
1095
|
import { spawn } from "node:child_process";
|
|
969
1096
|
import { accessSync, constants } from "node:fs";
|
|
1097
|
+
|
|
1098
|
+
// src/size-du-parse.ts
|
|
1099
|
+
var PROG_PREFIX_RE = /^(?:\/[^\s:]*)?[a-z][a-z0-9_-]*:\s+/;
|
|
1100
|
+
function describeDuFailure(detail) {
|
|
1101
|
+
if (/no such file or directory/i.test(detail))
|
|
1102
|
+
return { missing: true, reason: "不存在" };
|
|
1103
|
+
if (/permission denied/i.test(detail))
|
|
1104
|
+
return {
|
|
1105
|
+
missing: false,
|
|
1106
|
+
code: "SIZE_UNMEASURED_PERMISSION",
|
|
1107
|
+
reason: "权限不足, 无法读取"
|
|
1108
|
+
};
|
|
1109
|
+
if (/not a directory/i.test(detail))
|
|
1110
|
+
return {
|
|
1111
|
+
missing: false,
|
|
1112
|
+
code: "SIZE_UNMEASURED_NOT_DIR",
|
|
1113
|
+
reason: "不是目录"
|
|
1114
|
+
};
|
|
1115
|
+
if (/too many levels of symbolic links/i.test(detail)) {
|
|
1116
|
+
return {
|
|
1117
|
+
missing: false,
|
|
1118
|
+
code: "SIZE_UNMEASURED_LOOP",
|
|
1119
|
+
reason: "符号链接层级过深"
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
1122
|
+
return {
|
|
1123
|
+
missing: false,
|
|
1124
|
+
code: "SIZE_UNMEASURED_OTHER",
|
|
1125
|
+
reason: `读取失败 (${detail})`
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
var ANSI_C_ESCAPES = {
|
|
1129
|
+
"\\": 92,
|
|
1130
|
+
"'": 39,
|
|
1131
|
+
'"': 34,
|
|
1132
|
+
a: 7,
|
|
1133
|
+
b: 8,
|
|
1134
|
+
f: 12,
|
|
1135
|
+
n: 10,
|
|
1136
|
+
r: 13,
|
|
1137
|
+
t: 9,
|
|
1138
|
+
v: 11,
|
|
1139
|
+
e: 27,
|
|
1140
|
+
E: 27
|
|
1141
|
+
};
|
|
1142
|
+
function pushUtf8(bytes, text) {
|
|
1143
|
+
for (const byte of Buffer.from(text, "utf8"))
|
|
1144
|
+
bytes.push(byte);
|
|
1145
|
+
}
|
|
1146
|
+
function decodeAnsiCEscape(raw, index, bytes) {
|
|
1147
|
+
const ch = raw[index];
|
|
1148
|
+
if (ch === undefined)
|
|
1149
|
+
return index;
|
|
1150
|
+
const mapped = ANSI_C_ESCAPES[ch];
|
|
1151
|
+
if (mapped !== undefined) {
|
|
1152
|
+
bytes.push(mapped);
|
|
1153
|
+
return index + 1;
|
|
1154
|
+
}
|
|
1155
|
+
const oct = /^[0-7]{1,3}/.exec(raw.slice(index));
|
|
1156
|
+
if (oct !== null) {
|
|
1157
|
+
bytes.push(Number.parseInt(oct[0], 8) % 256);
|
|
1158
|
+
return index + oct[0].length;
|
|
1159
|
+
}
|
|
1160
|
+
if (ch === "x") {
|
|
1161
|
+
const hex = /^[0-9a-fA-F]{1,2}/.exec(raw.slice(index + 1));
|
|
1162
|
+
if (hex !== null) {
|
|
1163
|
+
bytes.push(Number.parseInt(hex[0], 16));
|
|
1164
|
+
return index + 1 + hex[0].length;
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
pushUtf8(bytes, ch);
|
|
1168
|
+
return index + 1;
|
|
1169
|
+
}
|
|
1170
|
+
function consumeAnsiCSegment(raw, start, bytes) {
|
|
1171
|
+
let index = start;
|
|
1172
|
+
while (index < raw.length) {
|
|
1173
|
+
const inner = raw[index];
|
|
1174
|
+
if (inner === "'")
|
|
1175
|
+
return index + 1;
|
|
1176
|
+
if (inner === "\\") {
|
|
1177
|
+
index = decodeAnsiCEscape(raw, index + 1, bytes);
|
|
1178
|
+
continue;
|
|
1179
|
+
}
|
|
1180
|
+
pushUtf8(bytes, inner ?? "");
|
|
1181
|
+
index += 1;
|
|
1182
|
+
}
|
|
1183
|
+
return -1;
|
|
1184
|
+
}
|
|
1185
|
+
function consumeDoubleQuoted(raw, start, bytes) {
|
|
1186
|
+
let index = start;
|
|
1187
|
+
while (index < raw.length) {
|
|
1188
|
+
const inner = raw[index];
|
|
1189
|
+
if (inner === '"')
|
|
1190
|
+
return index + 1;
|
|
1191
|
+
if (inner === "\\" && index + 1 < raw.length && '"\\$`'.includes(raw[index + 1] ?? "")) {
|
|
1192
|
+
pushUtf8(bytes, raw[index + 1] ?? "");
|
|
1193
|
+
index += 2;
|
|
1194
|
+
continue;
|
|
1195
|
+
}
|
|
1196
|
+
pushUtf8(bytes, inner ?? "");
|
|
1197
|
+
index += 1;
|
|
1198
|
+
}
|
|
1199
|
+
return -1;
|
|
1200
|
+
}
|
|
1201
|
+
function unescapeGnuShellQuoted(raw) {
|
|
1202
|
+
const bytes = [];
|
|
1203
|
+
let index = 0;
|
|
1204
|
+
while (index < raw.length) {
|
|
1205
|
+
const ch = raw[index];
|
|
1206
|
+
if (ch === "$" && raw[index + 1] === "'") {
|
|
1207
|
+
index = consumeAnsiCSegment(raw, index + 2, bytes);
|
|
1208
|
+
if (index === -1)
|
|
1209
|
+
return raw;
|
|
1210
|
+
continue;
|
|
1211
|
+
}
|
|
1212
|
+
if (ch === "'") {
|
|
1213
|
+
const end = raw.indexOf("'", index + 1);
|
|
1214
|
+
if (end === -1)
|
|
1215
|
+
return raw;
|
|
1216
|
+
pushUtf8(bytes, raw.slice(index + 1, end));
|
|
1217
|
+
index = end + 1;
|
|
1218
|
+
continue;
|
|
1219
|
+
}
|
|
1220
|
+
if (ch === '"') {
|
|
1221
|
+
index = consumeDoubleQuoted(raw, index + 1, bytes);
|
|
1222
|
+
if (index === -1)
|
|
1223
|
+
return raw;
|
|
1224
|
+
continue;
|
|
1225
|
+
}
|
|
1226
|
+
if (ch === "\\" && index + 1 < raw.length) {
|
|
1227
|
+
pushUtf8(bytes, raw[index + 1] ?? "");
|
|
1228
|
+
index += 2;
|
|
1229
|
+
continue;
|
|
1230
|
+
}
|
|
1231
|
+
pushUtf8(bytes, ch ?? "");
|
|
1232
|
+
index += 1;
|
|
1233
|
+
}
|
|
1234
|
+
return Buffer.from(bytes).toString("utf8");
|
|
1235
|
+
}
|
|
1236
|
+
function parseDuErrorLine(line) {
|
|
1237
|
+
const body = line.replace(PROG_PREFIX_RE, "");
|
|
1238
|
+
const gnu = /^cannot\s+(?:access|read directory|stat|open)\s+(.+):\s+(.+)$/.exec(body);
|
|
1239
|
+
if (gnu !== null) {
|
|
1240
|
+
const rawPath = gnu[1];
|
|
1241
|
+
const detail = gnu[2];
|
|
1242
|
+
if (rawPath !== undefined && detail !== undefined) {
|
|
1243
|
+
const path = rawPath.startsWith("'") || rawPath.startsWith('"') || rawPath.startsWith("$'") ? unescapeGnuShellQuoted(rawPath) : rawPath;
|
|
1244
|
+
return { path, detail };
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
const match = /^(.+):\s+(.+)$/.exec(body);
|
|
1248
|
+
if (match === null)
|
|
1249
|
+
return null;
|
|
1250
|
+
const path = match[1];
|
|
1251
|
+
const detail = match[2];
|
|
1252
|
+
if (path === undefined || detail === undefined)
|
|
1253
|
+
return null;
|
|
1254
|
+
return { path, detail };
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
// src/size-du.ts
|
|
970
1258
|
var DU_PROBES = ["/usr/bin/du", "/bin/du"];
|
|
971
1259
|
var hasControlChar = (target) => target.includes(`
|
|
972
1260
|
`) || target.includes("\r");
|
|
973
|
-
var PROG_PREFIX_RE = /^(?:\/[^\s:]*)?[a-z][a-z0-9_-]*:\s+/;
|
|
974
1261
|
function findDu(platform = process.platform) {
|
|
975
1262
|
if (platform === "win32")
|
|
976
1263
|
return null;
|
|
@@ -982,13 +1269,45 @@ function findDu(platform = process.platform) {
|
|
|
982
1269
|
}
|
|
983
1270
|
return null;
|
|
984
1271
|
}
|
|
1272
|
+
var DU_BATCH_MAX_BYTES = 128 * 1024;
|
|
1273
|
+
function chunkTargetsByBytes(targets, maxBytes = DU_BATCH_MAX_BYTES) {
|
|
1274
|
+
const batches = [];
|
|
1275
|
+
let current = [];
|
|
1276
|
+
let currentBytes = 0;
|
|
1277
|
+
for (const target of targets) {
|
|
1278
|
+
const size = Buffer.byteLength(target, "utf8") + 1;
|
|
1279
|
+
if (current.length > 0 && currentBytes + size > maxBytes) {
|
|
1280
|
+
batches.push(current);
|
|
1281
|
+
current = [];
|
|
1282
|
+
currentBytes = 0;
|
|
1283
|
+
}
|
|
1284
|
+
current.push(target);
|
|
1285
|
+
currentBytes += size;
|
|
1286
|
+
}
|
|
1287
|
+
if (current.length > 0)
|
|
1288
|
+
batches.push(current);
|
|
1289
|
+
return batches;
|
|
1290
|
+
}
|
|
985
1291
|
function runDu(bin, targets) {
|
|
986
1292
|
return new Promise((resolve) => {
|
|
1293
|
+
const env = { ...process.env, LC_ALL: "C", LANG: "C" };
|
|
1294
|
+
delete env.QUOTING_STYLE;
|
|
1295
|
+
delete env.LANGUAGE;
|
|
987
1296
|
const child = spawn(bin, ["-sk", "--", ...targets], {
|
|
988
|
-
stdio: ["ignore", "pipe", "pipe"]
|
|
1297
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
1298
|
+
env
|
|
989
1299
|
});
|
|
1300
|
+
child.stdout.setEncoding("utf8");
|
|
1301
|
+
child.stderr.setEncoding("utf8");
|
|
990
1302
|
let stdout = "";
|
|
991
1303
|
let stderr = "";
|
|
1304
|
+
let settled = false;
|
|
1305
|
+
const finish = (result) => {
|
|
1306
|
+
if (settled)
|
|
1307
|
+
return;
|
|
1308
|
+
settled = true;
|
|
1309
|
+
resolve(result);
|
|
1310
|
+
};
|
|
992
1311
|
child.stdout.on("data", (chunk) => {
|
|
993
1312
|
stdout += chunk;
|
|
994
1313
|
});
|
|
@@ -996,9 +1315,17 @@ function runDu(bin, targets) {
|
|
|
996
1315
|
stderr += chunk;
|
|
997
1316
|
});
|
|
998
1317
|
child.on("error", (error) => {
|
|
999
|
-
|
|
1318
|
+
finish({
|
|
1319
|
+
stdout,
|
|
1320
|
+
stderr,
|
|
1321
|
+
exitCode: null,
|
|
1322
|
+
signal: null,
|
|
1323
|
+
spawnError: error
|
|
1324
|
+
});
|
|
1325
|
+
});
|
|
1326
|
+
child.on("close", (exitCode, signal) => {
|
|
1327
|
+
finish({ stdout, stderr, exitCode, signal, spawnError: null });
|
|
1000
1328
|
});
|
|
1001
|
-
child.on("close", () => resolve({ stdout, stderr }));
|
|
1002
1329
|
});
|
|
1003
1330
|
}
|
|
1004
1331
|
function parseDuSizes(stdout) {
|
|
@@ -1045,58 +1372,105 @@ function describeAnomaly(assessment) {
|
|
|
1045
1372
|
shapes.push("行数与输入不符");
|
|
1046
1373
|
return shapes.join(" / ");
|
|
1047
1374
|
}
|
|
1048
|
-
function
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1375
|
+
async function measureDuBatches(bin, safeTargets, maxBatchBytes, signal) {
|
|
1376
|
+
const sizes = new Map;
|
|
1377
|
+
const failures = new Map;
|
|
1378
|
+
const degraded = new Set;
|
|
1379
|
+
const warnings = [];
|
|
1380
|
+
for (const batch of chunkTargetsByBytes(safeTargets, maxBatchBytes)) {
|
|
1381
|
+
if (signal?.aborted) {
|
|
1382
|
+
throw new SweepError("CANCELLED", "体积统计在批量统计前被取消", {
|
|
1383
|
+
phase: "measure"
|
|
1384
|
+
});
|
|
1385
|
+
}
|
|
1386
|
+
const run = await runDu(bin, batch);
|
|
1387
|
+
if (run.spawnError !== null || run.signal !== null) {
|
|
1388
|
+
for (const target of batch)
|
|
1389
|
+
degraded.add(target);
|
|
1390
|
+
const errno = run.spawnError?.code;
|
|
1391
|
+
const reason = run.spawnError !== null ? `du 执行失败 (${run.spawnError.message})` : `du 被信号 ${run.signal} 中止`;
|
|
1392
|
+
warnings.push({
|
|
1393
|
+
code: "SIZE_DU_UNAVAILABLE",
|
|
1394
|
+
message: `${reason}: 本批 ${batch.length} 个目标的体积均未采信`,
|
|
1395
|
+
...typeof errno === "string" ? { errno } : {}
|
|
1396
|
+
});
|
|
1397
|
+
continue;
|
|
1398
|
+
}
|
|
1399
|
+
const { stdout, stderr } = run;
|
|
1400
|
+
const batchSet = new Set(batch);
|
|
1401
|
+
const assessment = assessDuOutput(stdout, batch);
|
|
1402
|
+
if (!assessment.intact) {
|
|
1403
|
+
for (const target of batch)
|
|
1404
|
+
degraded.add(target);
|
|
1405
|
+
warnings.push({
|
|
1406
|
+
code: "SIZE_DU_OUTPUT_MISMATCH",
|
|
1407
|
+
message: `du 输出与输入集合不符 (${describeAnomaly(assessment)}): 本批 ${batch.length} 个目标的体积均未采信`
|
|
1408
|
+
});
|
|
1409
|
+
} else {
|
|
1410
|
+
for (const [path, bytes] of assessment.sizes)
|
|
1411
|
+
sizes.set(path, bytes);
|
|
1412
|
+
}
|
|
1413
|
+
for (const line of stderr.split(`
|
|
1414
|
+
`)) {
|
|
1415
|
+
if (line.trim() === "")
|
|
1416
|
+
continue;
|
|
1417
|
+
const errorLine = parseDuErrorLine(line);
|
|
1418
|
+
if (errorLine === null) {
|
|
1419
|
+
warnings.push({
|
|
1420
|
+
code: "SIZE_DU_LINE_UNATTRIBUTED",
|
|
1421
|
+
message: `体积统计失败 (未归因): ${line.replace(PROG_PREFIX_RE, "").trim()}`
|
|
1422
|
+
});
|
|
1423
|
+
continue;
|
|
1424
|
+
}
|
|
1425
|
+
const failure = describeDuFailure(errorLine.detail);
|
|
1426
|
+
if (!batchSet.has(errorLine.path)) {
|
|
1427
|
+
warnings.push({
|
|
1428
|
+
code: "SIZE_SUBPATH_FAILED",
|
|
1429
|
+
message: `体积统计失败 (${failure.reason}): ${errorLine.path}`,
|
|
1430
|
+
path: errorLine.path
|
|
1431
|
+
});
|
|
1432
|
+
continue;
|
|
1433
|
+
}
|
|
1434
|
+
failures.set(errorLine.path, failure);
|
|
1435
|
+
}
|
|
1084
1436
|
}
|
|
1085
|
-
const
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
const path = match[1];
|
|
1089
|
-
const detail = match[2];
|
|
1090
|
-
if (path === undefined || detail === undefined)
|
|
1091
|
-
return null;
|
|
1092
|
-
return { path, detail };
|
|
1437
|
+
for (const path of failures.keys())
|
|
1438
|
+
sizes.delete(path);
|
|
1439
|
+
return { sizes, failures, degraded, warnings };
|
|
1093
1440
|
}
|
|
1094
|
-
function createDuSizer(bin = findDu()) {
|
|
1441
|
+
function createDuSizer(bin = findDu(), maxBatchBytes = DU_BATCH_MAX_BYTES) {
|
|
1095
1442
|
return {
|
|
1096
1443
|
name: "du",
|
|
1097
1444
|
basis: "disk-usage",
|
|
1098
1445
|
async measure(targets, options) {
|
|
1446
|
+
if (options?.signal?.aborted) {
|
|
1447
|
+
throw new SweepError("CANCELLED", "体积统计在批量统计前被取消", {
|
|
1448
|
+
phase: "measure"
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1451
|
+
const sorted = [...new Set(targets)].sort();
|
|
1452
|
+
const emit = options?.onProgress;
|
|
1099
1453
|
if (bin === null) {
|
|
1454
|
+
const unmeasured = [];
|
|
1455
|
+
const reason = "du 候选不可用";
|
|
1456
|
+
for (const target of sorted) {
|
|
1457
|
+
if (options?.signal?.aborted) {
|
|
1458
|
+
throw new SweepError("CANCELLED", "体积统计在逐目标检查点被取消", {
|
|
1459
|
+
phase: "measure"
|
|
1460
|
+
});
|
|
1461
|
+
}
|
|
1462
|
+
unmeasured.push({
|
|
1463
|
+
target,
|
|
1464
|
+
code: "SIZE_UNMEASURED_OTHER",
|
|
1465
|
+
reason
|
|
1466
|
+
});
|
|
1467
|
+
emit?.({
|
|
1468
|
+
kind: "unmeasured",
|
|
1469
|
+
target,
|
|
1470
|
+
code: "SIZE_UNMEASURED_OTHER",
|
|
1471
|
+
reason
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1100
1474
|
return {
|
|
1101
1475
|
entries: [],
|
|
1102
1476
|
basis: "disk-usage",
|
|
@@ -1106,11 +1480,11 @@ function createDuSizer(bin = findDu()) {
|
|
|
1106
1480
|
message: "du 候选不可用: /usr/bin/du 与 /bin/du 均不存在, 本候选跳过体积统计"
|
|
1107
1481
|
}
|
|
1108
1482
|
],
|
|
1109
|
-
unmeasured
|
|
1483
|
+
unmeasured,
|
|
1110
1484
|
gone: []
|
|
1111
1485
|
};
|
|
1112
1486
|
}
|
|
1113
|
-
if (
|
|
1487
|
+
if (sorted.length === 0) {
|
|
1114
1488
|
return {
|
|
1115
1489
|
entries: [],
|
|
1116
1490
|
basis: "disk-usage",
|
|
@@ -1119,55 +1493,16 @@ function createDuSizer(bin = findDu()) {
|
|
|
1119
1493
|
gone: []
|
|
1120
1494
|
};
|
|
1121
1495
|
}
|
|
1122
|
-
const sorted = [...targets].sort();
|
|
1123
|
-
const warnings = [];
|
|
1124
1496
|
const entries = [];
|
|
1125
1497
|
const unmeasured = [];
|
|
1126
1498
|
const gone = [];
|
|
1127
|
-
const emit = options?.onProgress;
|
|
1128
1499
|
const safeTargets = sorted.filter((target) => !hasControlChar(target));
|
|
1129
|
-
const
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
const assessment = assessDuOutput(stdout, safeTargets);
|
|
1136
|
-
intact = assessment.intact;
|
|
1137
|
-
if (!intact) {
|
|
1138
|
-
warnings.push({
|
|
1139
|
-
code: "SIZE_DU_OUTPUT_MISMATCH",
|
|
1140
|
-
message: `du 输出与输入集合不符 (${describeAnomaly(assessment)}): 本批 ${safeTargets.length} 个目标的体积均未采信`
|
|
1141
|
-
});
|
|
1142
|
-
}
|
|
1143
|
-
for (const [path, bytes] of assessment.sizes)
|
|
1144
|
-
sizes.set(path, bytes);
|
|
1145
|
-
for (const line of stderr.split(`
|
|
1146
|
-
`)) {
|
|
1147
|
-
if (line.trim() === "")
|
|
1148
|
-
continue;
|
|
1149
|
-
const errorLine = parseDuErrorLine(line);
|
|
1150
|
-
if (errorLine === null) {
|
|
1151
|
-
warnings.push({
|
|
1152
|
-
code: "SIZE_DU_LINE_UNATTRIBUTED",
|
|
1153
|
-
message: `体积统计失败 (未归因): ${line.replace(PROG_PREFIX_RE, "").trim()}`
|
|
1154
|
-
});
|
|
1155
|
-
continue;
|
|
1156
|
-
}
|
|
1157
|
-
const failure = describeDuFailure(errorLine.detail);
|
|
1158
|
-
if (!safeSet.has(errorLine.path)) {
|
|
1159
|
-
warnings.push({
|
|
1160
|
-
code: "SIZE_SUBPATH_FAILED",
|
|
1161
|
-
message: `体积统计失败 (${failure.reason}): ${errorLine.path}`,
|
|
1162
|
-
path: errorLine.path
|
|
1163
|
-
});
|
|
1164
|
-
continue;
|
|
1165
|
-
}
|
|
1166
|
-
failures.set(errorLine.path, failure);
|
|
1167
|
-
}
|
|
1168
|
-
for (const path of failures.keys())
|
|
1169
|
-
sizes.delete(path);
|
|
1170
|
-
}
|
|
1500
|
+
const { sizes, failures, degraded, warnings } = safeTargets.length > 0 ? await measureDuBatches(bin, safeTargets, maxBatchBytes, options?.signal) : {
|
|
1501
|
+
sizes: new Map,
|
|
1502
|
+
failures: new Map,
|
|
1503
|
+
degraded: new Set,
|
|
1504
|
+
warnings: []
|
|
1505
|
+
};
|
|
1171
1506
|
const pushUnmeasured = (target, code, reason) => {
|
|
1172
1507
|
unmeasured.push({ target, code, reason });
|
|
1173
1508
|
emit?.({ kind: "unmeasured", target, code, reason });
|
|
@@ -1182,7 +1517,7 @@ function createDuSizer(bin = findDu()) {
|
|
|
1182
1517
|
pushUnmeasured(target, "SIZE_UNMEASURED_CONTROL_CHAR", "路径含控制字符");
|
|
1183
1518
|
continue;
|
|
1184
1519
|
}
|
|
1185
|
-
if (
|
|
1520
|
+
if (degraded.has(target)) {
|
|
1186
1521
|
pushUnmeasured(target, "SIZE_UNMEASURED_UNPARSEABLE", "输出不可解析");
|
|
1187
1522
|
continue;
|
|
1188
1523
|
}
|
|
@@ -1215,8 +1550,8 @@ function createDuSizer(bin = findDu()) {
|
|
|
1215
1550
|
}
|
|
1216
1551
|
|
|
1217
1552
|
// src/size-js.ts
|
|
1218
|
-
import {
|
|
1219
|
-
import { join as
|
|
1553
|
+
import { lstat as lstat4, readdir as readdir2 } from "node:fs/promises";
|
|
1554
|
+
import { join as join2 } from "node:path";
|
|
1220
1555
|
function describeFailure(code) {
|
|
1221
1556
|
switch (code) {
|
|
1222
1557
|
case "ENOENT":
|
|
@@ -1251,34 +1586,90 @@ function describeFailure(code) {
|
|
|
1251
1586
|
function errorCode(error) {
|
|
1252
1587
|
return error?.code;
|
|
1253
1588
|
}
|
|
1254
|
-
|
|
1255
|
-
|
|
1589
|
+
var SIZE_CONCURRENCY = 32;
|
|
1590
|
+
function pushSubpathWarning(warnings, path, error) {
|
|
1591
|
+
const errno = errorCode(error);
|
|
1592
|
+
warnings.push({
|
|
1593
|
+
code: "SIZE_SUBPATH_FAILED",
|
|
1594
|
+
message: `体积统计失败 (${describeFailure(errno).reason}): ${path}`,
|
|
1595
|
+
path,
|
|
1596
|
+
...errno !== undefined ? { errno } : {}
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
1599
|
+
function enqueueDirents(stack, dir, dirents) {
|
|
1256
1600
|
for (const dirent of dirents) {
|
|
1257
1601
|
if (dirent.isSymbolicLink())
|
|
1258
1602
|
continue;
|
|
1259
|
-
const path =
|
|
1260
|
-
if (dirent.isDirectory())
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1603
|
+
const path = join2(dir, dirent.name);
|
|
1604
|
+
if (dirent.isDirectory())
|
|
1605
|
+
stack.push({ kind: "dir", path });
|
|
1606
|
+
else if (dirent.isFile())
|
|
1607
|
+
stack.push({ kind: "file", path });
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
async function sumEntries(dirents, dir, warnings, signal) {
|
|
1611
|
+
const stack = [];
|
|
1612
|
+
enqueueDirents(stack, dir, dirents);
|
|
1613
|
+
let total = 0;
|
|
1614
|
+
let active = 0;
|
|
1615
|
+
const failures = [];
|
|
1616
|
+
let resolveDrained = null;
|
|
1617
|
+
const drained = new Promise((resolve) => {
|
|
1618
|
+
resolveDrained = resolve;
|
|
1619
|
+
});
|
|
1620
|
+
function pump() {
|
|
1621
|
+
while (active < SIZE_CONCURRENCY && stack.length > 0 && failures.length === 0 && !signal?.aborted) {
|
|
1622
|
+
const item = stack.pop();
|
|
1623
|
+
active += 1;
|
|
1624
|
+
processItem(item);
|
|
1625
|
+
}
|
|
1626
|
+
const stopped = failures.length > 0 || signal?.aborted === true;
|
|
1627
|
+
if (active === 0 && (stack.length === 0 || stopped) && resolveDrained !== null) {
|
|
1628
|
+
const resolve = resolveDrained;
|
|
1629
|
+
resolveDrained = null;
|
|
1630
|
+
resolve();
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
async function processItem(item) {
|
|
1634
|
+
try {
|
|
1635
|
+
if (signal?.aborted) {
|
|
1636
|
+
throw new SweepError("CANCELLED", "体积统计在目录遍历检查点被取消", {
|
|
1637
|
+
phase: "measure"
|
|
1268
1638
|
});
|
|
1269
1639
|
}
|
|
1270
|
-
|
|
1640
|
+
if (item.kind === "dir") {
|
|
1641
|
+
try {
|
|
1642
|
+
const children = await readdir2(item.path, { withFileTypes: true });
|
|
1643
|
+
enqueueDirents(stack, item.path, children);
|
|
1644
|
+
} catch (error) {
|
|
1645
|
+
pushSubpathWarning(warnings, item.path, error);
|
|
1646
|
+
}
|
|
1647
|
+
return;
|
|
1648
|
+
}
|
|
1271
1649
|
try {
|
|
1272
|
-
|
|
1650
|
+
const stats = await lstat4(item.path);
|
|
1651
|
+
if (stats.isFile())
|
|
1652
|
+
total += stats.size;
|
|
1273
1653
|
} catch (error) {
|
|
1274
|
-
warnings.
|
|
1275
|
-
code: "SIZE_SUBPATH_FAILED",
|
|
1276
|
-
message: `体积统计失败 (${describeFailure(errorCode(error)).reason}): ${path}`,
|
|
1277
|
-
path
|
|
1278
|
-
});
|
|
1654
|
+
pushSubpathWarning(warnings, item.path, error);
|
|
1279
1655
|
}
|
|
1656
|
+
} catch (error) {
|
|
1657
|
+
if (failures.length === 0)
|
|
1658
|
+
failures.push(error);
|
|
1659
|
+
} finally {
|
|
1660
|
+
active -= 1;
|
|
1661
|
+
pump();
|
|
1280
1662
|
}
|
|
1281
1663
|
}
|
|
1664
|
+
pump();
|
|
1665
|
+
await drained;
|
|
1666
|
+
if (failures.length > 0)
|
|
1667
|
+
throw failures[0];
|
|
1668
|
+
if (signal?.aborted) {
|
|
1669
|
+
throw new SweepError("CANCELLED", "体积统计在目录遍历检查点被取消", {
|
|
1670
|
+
phase: "measure"
|
|
1671
|
+
});
|
|
1672
|
+
}
|
|
1282
1673
|
return total;
|
|
1283
1674
|
}
|
|
1284
1675
|
function createJsSizer() {
|
|
@@ -1286,6 +1677,11 @@ function createJsSizer() {
|
|
|
1286
1677
|
name: "js",
|
|
1287
1678
|
basis: "logical-bytes",
|
|
1288
1679
|
async measure(targets, options) {
|
|
1680
|
+
if (options?.signal?.aborted) {
|
|
1681
|
+
throw new SweepError("CANCELLED", "体积统计在逐目标检查点被取消", {
|
|
1682
|
+
phase: "measure"
|
|
1683
|
+
});
|
|
1684
|
+
}
|
|
1289
1685
|
const warnings = [];
|
|
1290
1686
|
const entries = [];
|
|
1291
1687
|
const unmeasured = [];
|
|
@@ -1295,7 +1691,7 @@ function createJsSizer() {
|
|
|
1295
1691
|
unmeasured.push({ target, code, reason });
|
|
1296
1692
|
emit?.({ kind: "unmeasured", target, code, reason });
|
|
1297
1693
|
};
|
|
1298
|
-
for (const target of [...targets].sort()) {
|
|
1694
|
+
for (const target of [...new Set(targets)].sort()) {
|
|
1299
1695
|
if (options?.signal?.aborted) {
|
|
1300
1696
|
throw new SweepError("CANCELLED", "体积统计在逐目标检查点被取消", {
|
|
1301
1697
|
phase: "measure"
|
|
@@ -1303,6 +1699,11 @@ function createJsSizer() {
|
|
|
1303
1699
|
}
|
|
1304
1700
|
let dirents;
|
|
1305
1701
|
try {
|
|
1702
|
+
const st = await lstat4(target);
|
|
1703
|
+
if (st.isSymbolicLink()) {
|
|
1704
|
+
pushUnmeasured(target, "SIZE_UNMEASURED_NOT_DIR", "不是目录");
|
|
1705
|
+
continue;
|
|
1706
|
+
}
|
|
1306
1707
|
dirents = await readdir2(target, { withFileTypes: true });
|
|
1307
1708
|
} catch (error) {
|
|
1308
1709
|
const failure = describeFailure(errorCode(error));
|
|
@@ -1319,7 +1720,7 @@ function createJsSizer() {
|
|
|
1319
1720
|
}
|
|
1320
1721
|
continue;
|
|
1321
1722
|
}
|
|
1322
|
-
const bytes = await sumEntries(dirents, target, warnings);
|
|
1723
|
+
const bytes = await sumEntries(dirents, target, warnings, options?.signal);
|
|
1323
1724
|
entries.push({ target, bytes });
|
|
1324
1725
|
emit?.({ kind: "measured", target, bytes, basis: "logical-bytes" });
|
|
1325
1726
|
}
|
|
@@ -1330,7 +1731,8 @@ function createJsSizer() {
|
|
|
1330
1731
|
|
|
1331
1732
|
// src/size.ts
|
|
1332
1733
|
function createSizer() {
|
|
1333
|
-
|
|
1734
|
+
const bin = findDu();
|
|
1735
|
+
return bin !== null ? createDuSizer(bin) : createJsSizer();
|
|
1334
1736
|
}
|
|
1335
1737
|
// src/skip.ts
|
|
1336
1738
|
var SUSPECT_HINT = "已跳过 (加 --force 一并清理)";
|
|
@@ -1420,7 +1822,7 @@ function collectSkips(entries, crossDevice, policy) {
|
|
|
1420
1822
|
return { entries: book, trailer, hints };
|
|
1421
1823
|
}
|
|
1422
1824
|
// src/summary.ts
|
|
1423
|
-
var SUCCESS_KINDS = new Set([
|
|
1825
|
+
var SUCCESS_KINDS = /* @__PURE__ */ new Set([
|
|
1424
1826
|
"removed",
|
|
1425
1827
|
"missing",
|
|
1426
1828
|
"stale"
|
|
@@ -1463,23 +1865,30 @@ function assertNameArray(value, field, required) {
|
|
|
1463
1865
|
if (required && value.length === 0)
|
|
1464
1866
|
throw invalidArgument(field, "不能为空数组");
|
|
1465
1867
|
}
|
|
1868
|
+
var WIN32_ABS_ROOT_RE = /^(?:[A-Za-z]:[/\\]|[/\\]{2}[^/\\]+[/\\][^/\\]+)/;
|
|
1869
|
+
function isAbsoluteRoot(root, style) {
|
|
1870
|
+
return style.name === "win32" ? WIN32_ABS_ROOT_RE.test(root) : style.ops.isAbsolute(root);
|
|
1871
|
+
}
|
|
1466
1872
|
function assertShape(options) {
|
|
1467
1873
|
if (typeof options !== "object" || options === null)
|
|
1468
1874
|
throw invalidArgument("options", "构造选项必须是对象");
|
|
1469
1875
|
assertNameArray(options.roots, "roots", true);
|
|
1876
|
+
const style = options.style ?? nativeStyle();
|
|
1877
|
+
if (options.roots.some((root) => !isAbsoluteRoot(root, style)))
|
|
1878
|
+
throw invalidArgument("roots", "必须是绝对路径");
|
|
1470
1879
|
assertNameArray(options.exclude, "exclude", false);
|
|
1471
1880
|
assertNameArray(options.include, "include", false);
|
|
1472
|
-
if (options.home !== undefined && options.home !== null && typeof options.home !== "string")
|
|
1473
|
-
throw invalidArgument("home", "
|
|
1881
|
+
if (options.home !== undefined && options.home !== null && (typeof options.home !== "string" || options.home === ""))
|
|
1882
|
+
throw invalidArgument("home", "必须是非空字符串或 null");
|
|
1474
1883
|
if (options.policy !== undefined && (typeof options.policy !== "object" || options.policy === null || typeof options.policy.releaseSuspects !== "boolean"))
|
|
1475
1884
|
throw invalidArgument("policy", "releaseSuspects 必须是布尔值");
|
|
1476
1885
|
}
|
|
1477
1886
|
function resolveOptions(options) {
|
|
1478
1887
|
assertShape(options);
|
|
1479
1888
|
return Object.freeze({
|
|
1480
|
-
roots: [...options.roots],
|
|
1481
|
-
exclude: mergeNames(options.exclude ?? [...DEFAULT_EXCLUDE], []),
|
|
1482
|
-
include: mergeNames(options.include ?? [], []),
|
|
1889
|
+
roots: Object.freeze([...options.roots]),
|
|
1890
|
+
exclude: Object.freeze(mergeNames(options.exclude ?? [...DEFAULT_EXCLUDE], [])),
|
|
1891
|
+
include: Object.freeze(mergeNames(options.include ?? [], [])),
|
|
1483
1892
|
home: options.home === undefined ? homedir4() : options.home,
|
|
1484
1893
|
style: options.style ?? nativeStyle(),
|
|
1485
1894
|
policy: Object.freeze({
|
|
@@ -1504,12 +1913,12 @@ async function withPhase(phase, emit, signal, work) {
|
|
|
1504
1913
|
}
|
|
1505
1914
|
async function executePlan(resolved, options, signal) {
|
|
1506
1915
|
const emit = options?.onProgress;
|
|
1507
|
-
const roots = resolved.roots;
|
|
1916
|
+
const roots = [...resolved.roots];
|
|
1508
1917
|
const scanResult = await withPhase("scan", emit, signal, async () => {
|
|
1509
|
-
const result = await
|
|
1510
|
-
roots,
|
|
1511
|
-
exclude: resolved.exclude,
|
|
1512
|
-
include: resolved.include,
|
|
1918
|
+
const result = await createScanner().scan({
|
|
1919
|
+
roots: [...roots],
|
|
1920
|
+
exclude: [...resolved.exclude],
|
|
1921
|
+
include: [...resolved.include],
|
|
1513
1922
|
signal,
|
|
1514
1923
|
onProgress: (event) => emit?.(event)
|
|
1515
1924
|
});
|
|
@@ -1598,11 +2007,12 @@ async function executePlan(resolved, options, signal) {
|
|
|
1598
2007
|
roots: [...resolved.roots],
|
|
1599
2008
|
exclude: [...resolved.exclude],
|
|
1600
2009
|
include: [...resolved.include],
|
|
1601
|
-
policy,
|
|
2010
|
+
policy: { releaseSuspects: policy.releaseSuspects },
|
|
1602
2011
|
basis: entries.length > 0 ? sizeResult.basis : undefined,
|
|
1603
2012
|
entries,
|
|
1604
2013
|
batch,
|
|
1605
2014
|
skipped: [...book.entries],
|
|
2015
|
+
trailer: [...book.trailer],
|
|
1606
2016
|
warnings: [...scanResult.warnings, ...sizeResult.warnings],
|
|
1607
2017
|
nameMatches: {
|
|
1608
2018
|
exclude: scanResult.excludeMatches,
|
|
@@ -1611,9 +2021,17 @@ async function executePlan(resolved, options, signal) {
|
|
|
1611
2021
|
};
|
|
1612
2022
|
});
|
|
1613
2023
|
}
|
|
1614
|
-
function buildOutcomeEntries(plan, expectedSet, stale, rejected, removal) {
|
|
2024
|
+
function buildOutcomeEntries(plan, expectedSet, stale, rejected, mappings, removal) {
|
|
1615
2025
|
const staleSet = new Set(stale);
|
|
1616
|
-
|
|
2026
|
+
let rejectedMap;
|
|
2027
|
+
if (rejected.length > 0) {
|
|
2028
|
+
rejectedMap = new Map;
|
|
2029
|
+
for (const item of rejected) {
|
|
2030
|
+
if (!rejectedMap.has(item.target))
|
|
2031
|
+
rejectedMap.set(item.target, item);
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
const realOf = new Map(mappings.map((item) => [item.original, item.real]));
|
|
1617
2035
|
const removedSet = new Set(removal?.removed ?? []);
|
|
1618
2036
|
const missingSet = new Set(removal?.missing ?? []);
|
|
1619
2037
|
const failedMap = new Map((removal?.failed ?? []).map((item) => [
|
|
@@ -1627,13 +2045,16 @@ function buildOutcomeEntries(plan, expectedSet, stale, rejected, removal) {
|
|
|
1627
2045
|
return { kind: "not-expected" };
|
|
1628
2046
|
if (staleSet.has(entry.target))
|
|
1629
2047
|
return { kind: "stale" };
|
|
1630
|
-
if (rejectedMap !== undefined)
|
|
1631
|
-
|
|
1632
|
-
|
|
2048
|
+
if (rejectedMap !== undefined) {
|
|
2049
|
+
const rejection = rejectedMap.get(entry.target);
|
|
2050
|
+
return rejection !== undefined ? { kind: "rejected", rejection } : { kind: "not-attempted", code: "BATCH_REJECTED" };
|
|
2051
|
+
}
|
|
2052
|
+
const realTarget = realOf.get(entry.target) ?? entry.target;
|
|
2053
|
+
if (removedSet.has(realTarget))
|
|
1633
2054
|
return { kind: "removed" };
|
|
1634
|
-
if (missingSet.has(
|
|
2055
|
+
if (missingSet.has(realTarget))
|
|
1635
2056
|
return { kind: "missing" };
|
|
1636
|
-
const failure = failedMap.get(
|
|
2057
|
+
const failure = failedMap.get(realTarget);
|
|
1637
2058
|
if (failure !== undefined)
|
|
1638
2059
|
return { kind: "failed", failure };
|
|
1639
2060
|
return { kind: "not-attempted", code: removal.aborted.code };
|
|
@@ -1658,7 +2079,7 @@ async function executeRun(resolved, options, signal) {
|
|
|
1658
2079
|
let rejected = [];
|
|
1659
2080
|
if (face.length > 0) {
|
|
1660
2081
|
const raw = await withPhase("validate", emit, signal, () => validateTargets(face, {
|
|
1661
|
-
roots: resolved.roots,
|
|
2082
|
+
roots: [...resolved.roots],
|
|
1662
2083
|
home: resolved.home,
|
|
1663
2084
|
style: resolved.style,
|
|
1664
2085
|
signal
|
|
@@ -1673,7 +2094,8 @@ async function executeRun(resolved, options, signal) {
|
|
|
1673
2094
|
};
|
|
1674
2095
|
if (rejected.length === 0) {
|
|
1675
2096
|
removal = await withPhase("remove", emit, signal, async () => removeTargets(raw.accepted, {
|
|
1676
|
-
roots: await toTrustRoots(resolved.roots),
|
|
2097
|
+
roots: await toTrustRoots([...resolved.roots]),
|
|
2098
|
+
style: resolved.style,
|
|
1677
2099
|
signal,
|
|
1678
2100
|
onProgress: (event) => {
|
|
1679
2101
|
if (event.kind === "removed" || event.kind === "missing") {
|
|
@@ -1696,7 +2118,7 @@ async function executeRun(resolved, options, signal) {
|
|
|
1696
2118
|
status = "rejected";
|
|
1697
2119
|
else
|
|
1698
2120
|
status = "executed";
|
|
1699
|
-
const entries = buildOutcomeEntries(plan, expectedSet, stale, rejected, removal);
|
|
2121
|
+
const entries = buildOutcomeEntries(plan, expectedSet, stale, rejected, validation?.mappings ?? [], removal);
|
|
1700
2122
|
const releasedBytes = entries.reduce((sum, entry) => isSuccessOutcome(entry.outcome) ? sum + (entry.bytes ?? 0) : sum, 0);
|
|
1701
2123
|
const report = {
|
|
1702
2124
|
status,
|
|
@@ -1738,7 +2160,7 @@ export {
|
|
|
1738
2160
|
anchorChainPaths,
|
|
1739
2161
|
classifyTarget,
|
|
1740
2162
|
collectSkips,
|
|
1741
|
-
|
|
2163
|
+
createScanner,
|
|
1742
2164
|
createSizer,
|
|
1743
2165
|
createSweeper,
|
|
1744
2166
|
crossDeviceIndex,
|
|
@@ -1757,6 +2179,7 @@ export {
|
|
|
1757
2179
|
isHomeBody,
|
|
1758
2180
|
isSuccessOutcome,
|
|
1759
2181
|
isSweepError,
|
|
2182
|
+
isSweepErrorCode,
|
|
1760
2183
|
loadConfig,
|
|
1761
2184
|
loadResolvedConfig,
|
|
1762
2185
|
mergeNames,
|