dsh-aftermath 0.0.0-stage → 0.6.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.
package/lib/diff.js ADDED
@@ -0,0 +1,231 @@
1
+ /**
2
+ * 快照比较。纯函数,无 I/O,可单测。
3
+ *
4
+ * 语义边界:
5
+ * - `moved` 永远是启发式(同名 + 同大小 + mtime 相近),带 heuristic 标记,
6
+ * 报告里必须与 `deleted` 分开说,绝不合并成“被移动”这种断言。
7
+ * - mtime 变动严格比较:NTFS 是 100ns 分辨率,JS 侧已取整到 ms,不存在浮点噪音。
8
+ */
9
+
10
+ const MOVE_MTIME_TOLERANCE_MS = 2000;
11
+
12
+ export function indexEntries(entries) {
13
+ const map = new Map();
14
+ for (const entry of entries || []) map.set(entry.rel, entry);
15
+ return map;
16
+ }
17
+
18
+ export function diffRoot(beforeEntries, afterEntries) {
19
+ const before = indexEntries(beforeEntries);
20
+ const after = indexEntries(afterEntries);
21
+
22
+ const created = [];
23
+ const deleted = [];
24
+ const modified = [];
25
+
26
+ for (const [rel, entry] of after) {
27
+ const old = before.get(rel);
28
+ if (!old) {
29
+ created.push(entry);
30
+ continue;
31
+ }
32
+ // 类型互换(文件 ↔ 目录):当"改写"报出来,不当没发生
33
+ if (entry.dir !== old.dir) {
34
+ modified.push({ rel, before: old, after: entry, typeChanged: true });
35
+ continue;
36
+ }
37
+ if (entry.dir || old.dir) continue; // 目录只看存在性
38
+ if (old.missing || entry.missing) continue;
39
+ if (entry.link || old.link) continue;
40
+ if (entry.size !== old.size || entry.mtime !== old.mtime) {
41
+ modified.push({ rel, before: old, after: entry });
42
+ }
43
+ }
44
+
45
+ for (const [rel, entry] of before) {
46
+ if (!after.has(rel)) deleted.push(entry);
47
+ }
48
+
49
+ return { created, deleted, modified };
50
+ }
51
+
52
+ function basenameOf(rel) {
53
+ const idx = Math.max(rel.lastIndexOf('\\'), rel.lastIndexOf('/'));
54
+ return idx >= 0 ? rel.slice(idx + 1) : rel;
55
+ }
56
+
57
+ /**
58
+ * 把 {created, deleted} 里能配成一对的合成 moved。
59
+ * 只在同名 + 同大小 + mtime 差 ≤ 2s 时配对,剩余的原样保留。
60
+ */
61
+ export function pairMoves(created, deleted) {
62
+ const moved = [];
63
+ const usedCreated = new Set();
64
+ const usedDeleted = new Set();
65
+ const byName = new Map();
66
+
67
+ for (let i = 0; i < created.length; i += 1) {
68
+ const entry = created[i];
69
+ if (entry.dir || entry.missing || entry.link) continue;
70
+ const name = basenameOf(entry.rel).toLowerCase();
71
+ if (!byName.has(name)) byName.set(name, []);
72
+ byName.get(name).push(i);
73
+ }
74
+
75
+ for (let d = 0; d < deleted.length; d += 1) {
76
+ const gone = deleted[d];
77
+ if (gone.dir || gone.missing || gone.link) continue;
78
+ const name = basenameOf(gone.rel).toLowerCase();
79
+ const candidates = byName.get(name);
80
+ if (!candidates || candidates.length === 0) continue;
81
+ let bestIdx = -1;
82
+ let bestDelta = Infinity;
83
+ for (const idx of candidates) {
84
+ if (usedCreated.has(idx)) continue;
85
+ const added = created[idx];
86
+ if (added.size !== gone.size) continue;
87
+ const delta = Math.abs((added.mtime || 0) - (gone.mtime || 0));
88
+ if (delta <= MOVE_MTIME_TOLERANCE_MS && delta < bestDelta) {
89
+ bestDelta = delta;
90
+ bestIdx = idx;
91
+ }
92
+ }
93
+ if (bestIdx < 0) continue;
94
+ usedCreated.add(bestIdx);
95
+ usedDeleted.add(d);
96
+ moved.push({
97
+ from: gone,
98
+ to: created[bestIdx],
99
+ heuristic: true,
100
+ mtimeDeltaMs: bestDelta,
101
+ });
102
+ }
103
+
104
+ return {
105
+ moved,
106
+ created: created.filter((_, idx) => !usedCreated.has(idx)),
107
+ deleted: deleted.filter((_, idx) => !usedDeleted.has(idx)),
108
+ };
109
+ }
110
+
111
+ /**
112
+ * 一轮的完整变更集:{created, deleted, modified, moved},每项已带 rootId。
113
+ */
114
+ /** 根的身份 = id + 规范化路径。只按 id 会把 C:\A\Desktop 和 D:\B\Desktop 互相比对。 */
115
+ function rootKey(snap) {
116
+ const id = String(snap?.id ?? '');
117
+ const path = String(snap?.root ?? '')
118
+ .replace(/[\\/]+/g, '/')
119
+ .replace(/\/+$/, '')
120
+ .toLowerCase();
121
+ return `${id}|${path}`;
122
+ }
123
+
124
+ function incompleteReason(snap) {
125
+ if (snap.ok === false) return snap.error || 'unreadable';
126
+ if (snap.truncated) return 'truncated';
127
+ if (Number(snap.unreadable) > 0) return 'unreadable-subdir';
128
+ if (snap.complete === false) return 'incomplete';
129
+ return null;
130
+ }
131
+
132
+ export function diffSnapshotPair(beforeSnaps, afterSnaps) {
133
+ const beforeByRoot = new Map();
134
+ const beforeById = new Map();
135
+ for (const snap of beforeSnaps || []) {
136
+ beforeByRoot.set(rootKey(snap), snap);
137
+ const id = String(snap?.id ?? '');
138
+ if (!beforeById.has(id)) beforeById.set(id, []);
139
+ beforeById.get(id).push(snap);
140
+ }
141
+
142
+ const created = [];
143
+ const deleted = [];
144
+ const modified = [];
145
+ const roots = [];
146
+ const skipped = [];
147
+
148
+ for (const after of afterSnaps || []) {
149
+ const key = rootKey(after);
150
+ const before = beforeByRoot.get(key);
151
+ roots.push({
152
+ id: after.id,
153
+ path: after.root,
154
+ ok: after.ok,
155
+ complete: after.complete !== false,
156
+ count: after.count,
157
+ truncated: after.truncated === true,
158
+ unreadable: Number(after.unreadable) || 0,
159
+ });
160
+
161
+ if (!after.ok || !before || !before.ok) {
162
+ // 任一侧不可信就整根跳过:读不到 ≠ 全被删。
163
+ const reason = !after.ok
164
+ ? after.error || 'unreadable'
165
+ : before
166
+ ? before.error || 'unreadable'
167
+ : beforeById.has(String(after.id ?? '')) ? 'root-changed' : 'no-before';
168
+ skipped.push({ id: after.id, path: after.root, reason });
169
+ continue;
170
+ }
171
+
172
+ // 名单不全(截断 / 有子目录读不到)时做差会把"没扫到"说成"被删除",
173
+ // 所以整根跳过并写明原因,宁可少报也不误报。
174
+ const incomplete = incompleteReason(after) || incompleteReason(before);
175
+ if (incomplete) {
176
+ skipped.push({ id: after.id, path: after.root, reason: incomplete });
177
+ continue;
178
+ }
179
+
180
+ const delta = diffRoot(before.entries, after.entries);
181
+ const paired = pairMoves(delta.created, delta.deleted);
182
+
183
+ for (const entry of delta.modified) {
184
+ modified.push({ rel: entry.rel, before: entry.before, after: entry.after, rootId: after.id, root: after.root });
185
+ }
186
+ for (const entry of paired.created) {
187
+ created.push({ ...entry, rootId: after.id, root: after.root });
188
+ }
189
+ for (const entry of paired.deleted) {
190
+ deleted.push({ ...entry, rootId: after.id, root: after.root });
191
+ }
192
+ for (const m of paired.moved) {
193
+ modified.push({
194
+ rel: m.to.rel,
195
+ before: m.from,
196
+ after: m.to,
197
+ rootId: after.id,
198
+ root: after.root,
199
+ movedFrom: m.from.rel,
200
+ heuristic: true,
201
+ mtimeDeltaMs: m.mtimeDeltaMs,
202
+ });
203
+ }
204
+ }
205
+
206
+ return { created, deleted, modified, roots, skipped };
207
+ }
208
+
209
+ export function toChangeList(delta) {
210
+ const changes = [];
211
+ for (const entry of delta.deleted) {
212
+ changes.push({ kind: 'deleted', rootId: entry.rootId, root: entry.root, rel: entry.rel, before: entry, after: null });
213
+ }
214
+ for (const entry of delta.created) {
215
+ changes.push({ kind: 'created', rootId: entry.rootId, root: entry.root, rel: entry.rel, before: null, after: entry });
216
+ }
217
+ for (const entry of delta.modified) {
218
+ const kind = entry.movedFrom ? 'moved' : 'modified';
219
+ changes.push({
220
+ kind,
221
+ rootId: entry.rootId,
222
+ root: entry.root,
223
+ rel: entry.rel,
224
+ before: entry.before,
225
+ after: entry.after,
226
+ heuristic: entry.heuristic === true,
227
+ movedFrom: entry.movedFrom || null,
228
+ });
229
+ }
230
+ return changes;
231
+ }