@twinsearth/timemachine-dsh-plugin 0.2.2 → 0.2.3
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/CHANGELOG.md +19 -0
- package/README.md +27 -1
- package/docs/ARCHITECTURE.md +21 -0
- package/docs/REQUIREMENTS.md +5 -1
- package/docs/TESTPLAN.md +11 -2
- package/lib/client.js +85 -3
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/src/plugin.mjs +23 -1
- package/src/service.mjs +2 -0
- package/src/telemetry.mjs +363 -0
- package/src/version.mjs +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.3 — 2026-12-03
|
|
4
|
+
|
|
5
|
+
### 版本与统计
|
|
6
|
+
|
|
7
|
+
- 面板右上角新增 `Time Machine v0.2.3` 版本胶囊,版本来自 `/status` 的 `version` 字段。
|
|
8
|
+
- 版本胶囊旁新增 `在线 X · 7日 Y · 安装 Z` 统计胶囊,数据来自本地 `/stats` 代理的独立租约服务。
|
|
9
|
+
- 新增 `src/version.mjs` 作为版本单一来源;`plugin.json`、`package.json`、README、CHANGELOG 同步到 `0.2.3`。
|
|
10
|
+
|
|
11
|
+
### 安装身份与租约
|
|
12
|
+
|
|
13
|
+
- 新增 `src/telemetry.mjs`:安装目录首次运行生成随机 `.timemachine-install-token`;同目录重启复用,换目录/重装换新 token 再计数。目录只读时退回 `sha256(路径 + 主机名)` 稳定摘要。
|
|
14
|
+
- 客户端启动随机等待 1–11 秒再领租约;399 分钟租约;服务端按 3 小时周期在 55%–95% 区间随机排回执时刻。
|
|
15
|
+
- 回执失败 2–6 分钟补投,连续 3 次失败后下一个周期再领;本地 400 分钟未续上按 1–60 秒、100 秒、300 秒、1000 秒重连,连续三次失败后等 400 分钟再续。
|
|
16
|
+
- 服务端 `POST /v1/lease`、`POST /v1/receipt`、`POST /v1/miss`、`GET /v1/stats`、`GET /health`;独立 SQLite,不修改既有 `fang-lease` 服务。
|
|
17
|
+
|
|
18
|
+
### 发布
|
|
19
|
+
|
|
20
|
+
- 四方发布 `0.2.3`:Git tag、GitHub Release、GitHub Package、npm。
|
|
21
|
+
|
|
3
22
|
## 0.2.2 — 2026-12-03
|
|
4
23
|
|
|
5
24
|
### 滚动与默认文件夹树
|
package/README.md
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
# @twinsearth/timemachine-dsh-plugin
|
|
2
2
|
|
|
3
|
+
[](https://github.com/TwinsEarth/TimeMachine-dsh-plugin/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@twinsearth/timemachine-dsh-plugin)
|
|
5
|
+
[](https://github.com/deepseek-ai/deepseek-harness)
|
|
6
|
+
|
|
3
7
|
DSH 一键还原点(Time Machine)。它把 DSH 客户端的会话、工作区、配置与插件清单打包成一个可迁移的还原点,并在需要时把整台机器上的 DSH 恢复成还原点里的样子。
|
|
4
8
|
|
|
5
|
-
- 版本:`0.2.
|
|
9
|
+
- 版本:`0.2.3`
|
|
6
10
|
- 宿主:DeepSeek Harness(DSH)桌面客户端 `0.2.x`
|
|
7
11
|
- 平台:Windows / macOS / Linux(Windows 默认目录,macOS/Linux 由用户选择目录)
|
|
8
12
|
- 许可:MIT
|
|
@@ -66,6 +70,28 @@ DSH 一键还原点:
|
|
|
66
70
|
|
|
67
71
|
接口为 `POST /api/twinsearth/timemachine/repair-guides`,省略 `file` 时修复全部还原点;传 `file` 时只修复该归档,且必须位于当前保存文件夹内。
|
|
68
72
|
|
|
73
|
+
### 1.8 版本号、在线/7日/安装统计与租约
|
|
74
|
+
|
|
75
|
+
完整面板右上角固定显示两枚胶囊:
|
|
76
|
+
|
|
77
|
+
- `Time Machine v0.2.3`:版本号来自服务端 `status.version`,不是客户端硬编码;插件更新后随包变化。
|
|
78
|
+
- `在线 X · 7日 Y · 安装 Z`:通过本地 `GET /api/twinsearth/timemachine/stats` 代理独立统计服务。
|
|
79
|
+
|
|
80
|
+
统计口径:
|
|
81
|
+
|
|
82
|
+
- **当前在线**:399 分钟租约未过期的去重安装数,即最近 399 分钟有过回执。
|
|
83
|
+
- **7 天内活跃**:`lastSeen` 落在最近 7 天内的安装数。
|
|
84
|
+
- **历史安装数**:安装事件累计,单调只增。每份安装首次运行生成随机 token,写入插件安装目录的 `.timemachine-install-token`;同一目录重启复用 token,只算一次;换目录/重装会生成新 token,再计一次。
|
|
85
|
+
- 安装目录只读时退回 `sha256(安装路径 + 主机名)` 摘要:重启稳定,但同一路径重装不再增加计数,这是唯一降级情况。
|
|
86
|
+
|
|
87
|
+
租约与回执:
|
|
88
|
+
|
|
89
|
+
- 服务端把 3 小时划为一个周期,在周期的 55%–95% 之间为每台在线机器随机排一个回执时刻,避免整点齐发。
|
|
90
|
+
- 客户端启动后随机等待 1–11 秒再领租约;到点回执成功后续 399 分钟。
|
|
91
|
+
- 回执失败每隔 2–6 分钟补投一次;连续 3 次失败放回队列,下一个周期再领。
|
|
92
|
+
- 本地 400 分钟没有续上:先随机 1–60 秒重连,再按 100 秒、300 秒、1000 秒各试一次;连续三次失败后等 400 分钟再进入下一轮。
|
|
93
|
+
- 统计服务地址可用 `TIMEMACHINE_STATS_URL` 覆盖;默认地址随插件发布。
|
|
94
|
+
|
|
69
95
|
### 2.1 用户插件:内置与实验性
|
|
70
96
|
|
|
71
97
|
还原点记录 `dsh.profile.bundles`、`dependencies` 与 `cordis.patch.yml` 的插件行,包含:
|
package/docs/ARCHITECTURE.md
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
lib/index.js Node 入口,re-export src/plugin.mjs
|
|
7
7
|
lib/client.js 浏览器 bundle,注册三个 UI 槽位 + Windows 标题栏备用按钮
|
|
8
8
|
src/plugin.mjs Cordis apply / HTTP 路由 / 单操作互斥
|
|
9
|
+
src/telemetry.mjs 安装 token、租约回执状态机、远程统计缓存
|
|
10
|
+
src/version.mjs 面板与 health 共用的版本单一来源
|
|
9
11
|
src/service.mjs 设置、状态、创建、计划、还原、导入、定时器
|
|
10
12
|
src/point.mjs 还原点归档格式、创建、读取、列表、清理、导入
|
|
11
13
|
src/guide-repair.mjs 已有还原点内的会话引导校验与重建
|
|
@@ -21,6 +23,25 @@ src/util/* 路径、哈希、原子写、安全文件名
|
|
|
21
23
|
|
|
22
24
|
服务端只依赖 Node 标准库;客户端只依赖宿主提供的 `react`。
|
|
23
25
|
|
|
26
|
+
## 1.1 安装身份与租约统计
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
插件安装目录/.timemachine-install-token 随机 token,首次运行生成
|
|
30
|
+
│
|
|
31
|
+
├─ 同目录重启:复用 token,不重复计安装
|
|
32
|
+
├─ 换目录/重装:新 token,再计一次
|
|
33
|
+
└─ 目录只读:sha256(路径 + 主机名) 回退(同路径重装不再计数)
|
|
34
|
+
|
|
35
|
+
客户端启动 1–11 s
|
|
36
|
+
-> POST /v1/lease(token/version/platform/arch)
|
|
37
|
+
-> 服务端返回 399 分钟租约 + 3 小时周期 55%–95% 的随机 next_receipt_at
|
|
38
|
+
-> 到点 POST /v1/receipt,续租并排下一周期
|
|
39
|
+
-> 失败 2–6 分钟补投,连续 3 次失败下一周期再 claim
|
|
40
|
+
-> 本地 400 分钟未续上:1–60 s、100 s、300 s、1000 s 重连;仍失败等 400 分钟
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
统计服务是独立 Debian 12 + Python 标准库 + SQLite 服务(`server/timemachine_lease.py`,默认 `:38081`),不读写既有 `fang-lease` 的库或进程。`GET /v1/stats` 返回 `online`(399 分钟租约内去重安装)、`sevenDay`、`installs`(累计安装)。
|
|
44
|
+
|
|
24
45
|
## 2. 数据布局
|
|
25
46
|
|
|
26
47
|
```
|
package/docs/REQUIREMENTS.md
CHANGED
|
@@ -92,7 +92,11 @@ node scripts/host-menu-patch.mjs --app-asar "D:\DSH\resources\app.asar" --restor
|
|
|
92
92
|
- [ ] 重启 DSH 后点击「应用 -> 开发者工具 -> DSH一键还原点」验收
|
|
93
93
|
- [ ] 第三方插件真实联网 latest 安装验收
|
|
94
94
|
- [x] npm 与 GitHub 发布 `0.2.1`
|
|
95
|
-
- [
|
|
95
|
+
- [x] npm 与 GitHub 发布 `0.2.2`
|
|
96
96
|
- [ ] real DSH 面板滚动/拖动条与默认最新树的点击验收
|
|
97
97
|
- [ ] 真实 DSH 面板绿色「一键修复会话引导」点击与结果验收
|
|
98
98
|
- [ ] 插件管理器标题/详情在真实 DSH 中的渲染验收
|
|
99
|
+
- [x] 版本号、在线/7日/安装统计胶囊(静态与协议测试通过)
|
|
100
|
+
- [x] 399 分钟租约、3 小时随机回执、补投与 400 分钟重连(服务端单测 + 公网端到端通过)
|
|
101
|
+
- [x] 独立租约服务部署到 Debian 12 `43.142.74.71:38081`(systemd `timemachine-lease`)
|
|
102
|
+
- [ ] 四方发布 `0.2.3`(Git tag、GitHub Release、GitHub Package、npm)
|
package/docs/TESTPLAN.md
CHANGED
|
@@ -174,6 +174,15 @@ npm run pack-check
|
|
|
174
174
|
- 路由用例传 `file` 只修复指定归档,返回 `checked_points=1`、`unchanged_points=1`;全量扫描与失败归档报告待真实 UI 点击验收。
|
|
175
175
|
- 真实 DSH 中点击绿色按钮、查看结果提示、选中归档被修复后结构树自动刷新仍待人工点击验收。
|
|
176
176
|
|
|
177
|
+
### 3.13 0.2.3 版本、统计与租约(协议通过)
|
|
178
|
+
|
|
179
|
+
- `npm test` 26/26、`npm run check` 27/27、`npm run pack-check` 33 个文件。
|
|
180
|
+
- 客户端 `HeaderBadges` 显示 `Time Machine v0.2.3` 与 `在线 X · 7日 Y · 安装 Z`;`/stats` 失败时显示 `—`,不阻塞主面板。
|
|
181
|
+
- `ensureInstallToken` 覆盖同目录稳定、不同目录换 token、只读路径回退 `sha256(path+hostname)`;`createTelemetry` 覆盖 stats 归一化、30 秒缓存和离线降级。
|
|
182
|
+
- Debian 12 服务端 5 个单测通过:首次计数、同 token 不重复、回执排到下一周期、连续 3 次 miss 重排、迟到 claim 排到下一周期。
|
|
183
|
+
- 公网 `43.142.74.71:38081` 已验证 `/health`、`/v1/lease`、`/v1/receipt`、`/v1/miss`、`/v1/stats`;测试 token 已清理,生产统计归零。
|
|
184
|
+
- 真实 DSH 面板右上角显示、真实安装 token 落盘与 399 分钟续租仍待真机点击/长跑验收。
|
|
185
|
+
|
|
177
186
|
## 4. 待人工验收
|
|
178
187
|
|
|
179
188
|
- 安装 tarball 到真实 DSH profile `desktop` 后:
|
|
@@ -203,7 +212,7 @@ npm run pack-check
|
|
|
203
212
|
|
|
204
213
|
## 5. 回归门槛
|
|
205
214
|
|
|
206
|
-
- `npm test`
|
|
215
|
+
- `npm test` 26/26 通过
|
|
207
216
|
- `npm run check` 27/27 通过
|
|
208
|
-
- `npm run pack-check` tarball 内容符合 `files`(
|
|
217
|
+
- `npm run pack-check` tarball 内容符合 `files`(33 个文件)
|
|
209
218
|
- 任何失败先定位到具体断言,不通过放宽断言“修复”
|
package/lib/client.js
CHANGED
|
@@ -165,6 +165,16 @@ window.__ModuleLoader__.load({
|
|
|
165
165
|
.tm-pageScroll::-webkit-scrollbar-thumb:hover{background:#8b93a1;background-clip:content-box}
|
|
166
166
|
.tm-pageBody{min-width:0}
|
|
167
167
|
.tm-head{display:flex;align-items:flex-start;justify-content:space-between;gap:12px;flex-wrap:wrap}
|
|
168
|
+
.tm-headRight{display:flex;align-items:center;justify-content:flex-end;gap:8px;flex-wrap:wrap;margin-left:auto;min-width:0}
|
|
169
|
+
.tm-headActions{flex-wrap:nowrap}
|
|
170
|
+
.tm-badges{display:flex;align-items:center;gap:6px;flex-wrap:wrap}
|
|
171
|
+
.tm-pill{display:inline-flex;align-items:center;gap:6px;border:1px solid var(--dsh-border,#e5e7eb);background:var(--dsh-bg-primary,#fff);border-radius:999px;padding:5px 10px;font-size:12px;line-height:1;white-space:nowrap;box-shadow:0 1px 2px rgba(0,0,0,.03)}
|
|
172
|
+
.tm-version{font-weight:650;color:var(--dsh-text-primary,#111827)}
|
|
173
|
+
.tm-metrics{color:var(--dsh-text-secondary,#4b5563);font-variant-numeric:tabular-nums}
|
|
174
|
+
.tm-metrics b{color:var(--dsh-text-primary,#111827);font-weight:650}
|
|
175
|
+
.tm-metricsSep{color:var(--dsh-text-tertiary,#9ca3af);margin:0 1px}
|
|
176
|
+
.tm-dot{width:7px;height:7px;border-radius:999px;background:#9ca3af;display:inline-block;flex:none}
|
|
177
|
+
.tm-dot.online{background:#22c55e;box-shadow:0 0 0 2px rgba(34,197,94,.16)}
|
|
168
178
|
.tm-title{font-size:17px;font-weight:650;letter-spacing:.01em;margin:0}
|
|
169
179
|
.tm-sub{color:var(--dsh-text-secondary,#6b7280);font-size:12px;margin-top:4px;word-break:break-all}
|
|
170
180
|
.tm-card{background:var(--dsh-bg-primary,#fff);border:1px solid var(--dsh-border,#e5e7eb);border-radius:12px;padding:14px;box-shadow:0 1px 2px rgba(0,0,0,.03)}
|
|
@@ -238,6 +248,7 @@ window.__ModuleLoader__.load({
|
|
|
238
248
|
.tm-sessionRow{display:flex;gap:8px;align-items:center;padding:4px 0;border-top:1px dashed var(--dsh-border,#e5e7eb);font-size:11px;flex-wrap:wrap}
|
|
239
249
|
.tm-sessionRow:first-child{border-top:0}
|
|
240
250
|
.tm-sessionId{font-family:ui-monospace,SFMono-Regular,Consolas,monospace;word-break:break-all}
|
|
251
|
+
@media (max-width:760px){.tm-headRight{width:100%;justify-content:flex-start;margin-left:0}.tm-headActions{flex-wrap:wrap}}
|
|
241
252
|
@media (max-width:640px){.tm-pointActions{margin-left:0}.tm-card{padding:12px}.tm-treeHead{display:none}.tm-treeRow{grid-template-columns:14px minmax(0,1fr);row-gap:2px}.tm-treeDesc{grid-column:2;font-size:11px}}
|
|
242
253
|
`;
|
|
243
254
|
(doc.head ?? doc.documentElement).appendChild(style);
|
|
@@ -283,6 +294,72 @@ window.__ModuleLoader__.load({
|
|
|
283
294
|
return { status, loading, error, refresh, setError };
|
|
284
295
|
}
|
|
285
296
|
|
|
297
|
+
function useStats() {
|
|
298
|
+
const [stats, setStats] = useState(null);
|
|
299
|
+
const mounted = useRef(true);
|
|
300
|
+
const refresh = useCallback(async () => {
|
|
301
|
+
try {
|
|
302
|
+
const next = await request('/stats');
|
|
303
|
+
if (mounted.current) setStats(next);
|
|
304
|
+
return next;
|
|
305
|
+
} catch (caught) {
|
|
306
|
+
if (mounted.current) {
|
|
307
|
+
setStats(caught.payload ?? {
|
|
308
|
+
ok: false,
|
|
309
|
+
online: null,
|
|
310
|
+
sevenDay: null,
|
|
311
|
+
installs: null,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
}, []);
|
|
317
|
+
useEffect(() => {
|
|
318
|
+
mounted.current = true;
|
|
319
|
+
void refresh();
|
|
320
|
+
const timer = setInterval(() => { void refresh(); }, 60_000);
|
|
321
|
+
return () => {
|
|
322
|
+
mounted.current = false;
|
|
323
|
+
clearInterval(timer);
|
|
324
|
+
};
|
|
325
|
+
}, [refresh]);
|
|
326
|
+
return stats;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function HeaderBadges({ status, stats }) {
|
|
330
|
+
const version = typeof status?.version === 'string' && status.version !== '' ? status.version : '…';
|
|
331
|
+
const numeric = (value) => (value === null || value === undefined || value === ''
|
|
332
|
+
? null
|
|
333
|
+
: (Number.isFinite(Number(value)) ? Number(value) : null));
|
|
334
|
+
const online = numeric(stats?.online);
|
|
335
|
+
const sevenDay = numeric(stats?.sevenDay);
|
|
336
|
+
const installs = numeric(stats?.installs);
|
|
337
|
+
const live = stats?.ok !== false && online !== null;
|
|
338
|
+
return h('div', { className: 'tm-badges', 'data-tm-badges': 'true' }, [
|
|
339
|
+
h('span', {
|
|
340
|
+
key: 'version',
|
|
341
|
+
className: 'tm-pill tm-version',
|
|
342
|
+
title: 'Time Machine DSH 插件版本',
|
|
343
|
+
'data-tm-version': version,
|
|
344
|
+
}, [
|
|
345
|
+
h('span', { key: 'dot', className: `tm-dot${live ? ' online' : ''}`, 'aria-hidden': 'true' }),
|
|
346
|
+
h('span', { key: 'label' }, `Time Machine v${version}`),
|
|
347
|
+
]),
|
|
348
|
+
h('span', {
|
|
349
|
+
key: 'metrics',
|
|
350
|
+
className: 'tm-pill tm-metrics',
|
|
351
|
+
title: '当前在线:399 分钟租约内 · 7 天活跃:最近 7 天内有回执 · 安装:累计安装次数',
|
|
352
|
+
'data-tm-metrics': 'true',
|
|
353
|
+
}, [
|
|
354
|
+
h('span', { key: 'o' }, ['在线 ', h('b', { key: 'ov' }, online === null ? '—' : String(online))]),
|
|
355
|
+
h('span', { key: 's', className: 'tm-metricsSep' }, '·'),
|
|
356
|
+
h('span', { key: 'w' }, ['7日 ', h('b', { key: 'wv' }, sevenDay === null ? '—' : String(sevenDay))]),
|
|
357
|
+
h('span', { key: 's2', className: 'tm-metricsSep' }, '·'),
|
|
358
|
+
h('span', { key: 'i' }, ['安装 ', h('b', { key: 'iv' }, installs === null ? '—' : String(installs))]),
|
|
359
|
+
]),
|
|
360
|
+
]);
|
|
361
|
+
}
|
|
362
|
+
|
|
286
363
|
function PointRow({ point, busy, onRestore, onDetails, onFolder, selected = false }) {
|
|
287
364
|
const p = point.point ?? {};
|
|
288
365
|
return h('div', { className: `tm-point${selected ? ' selected' : ''}`, 'data-tm-point': p.kind ?? '' },
|
|
@@ -493,6 +570,7 @@ window.__ModuleLoader__.load({
|
|
|
493
570
|
|
|
494
571
|
function RestorePointBody({ compact = false, onDone = null }) {
|
|
495
572
|
const { status, loading, error, refresh, setError } = useStatus();
|
|
573
|
+
const stats = useStats();
|
|
496
574
|
const [tab, setTab] = useState('manual');
|
|
497
575
|
const [options, setOptions] = useState({ includeConfig: true, includePlugins: true, includeSessions: true, includePluginPackages: false, sessionScope: 'active' });
|
|
498
576
|
const [busy, setBusy] = useState('');
|
|
@@ -716,6 +794,7 @@ window.__ModuleLoader__.load({
|
|
|
716
794
|
return h('div', { className: 'tm-root' },
|
|
717
795
|
h('div', { className: 'tm-head' }, [
|
|
718
796
|
h('div', { key: 't' }, [h('h2', { className: 'tm-title' }, panelTitle()), h('div', { className: 'tm-sub' }, '先选择保存文件夹,之后所有还原点都放在这里。')]),
|
|
797
|
+
h('div', { key: 'b', className: 'tm-headRight' }, h(HeaderBadges, { status, stats })),
|
|
719
798
|
]),
|
|
720
799
|
h('div', { className: 'tm-card' }, [
|
|
721
800
|
h('div', { key: 'h', className: 'tm-cardTitle' }, '选择还原点保存文件夹'),
|
|
@@ -739,9 +818,12 @@ window.__ModuleLoader__.load({
|
|
|
739
818
|
h('h2', { className: 'tm-title' }, panelTitle()),
|
|
740
819
|
h('div', { key: 's', className: 'tm-sub' }, `保存到 ${status?.root ?? ''} · DSH ${status?.dsh_home ?? ''} · ${counts.plugins ?? 0} 个第三方插件`),
|
|
741
820
|
]),
|
|
742
|
-
h('div', { key: 'a', className: 'tm-
|
|
743
|
-
h(
|
|
744
|
-
h('
|
|
821
|
+
h('div', { key: 'a', className: 'tm-headRight' }, [
|
|
822
|
+
h(HeaderBadges, { key: 'badges', status, stats }),
|
|
823
|
+
h('div', { key: 'actions', className: 'tm-row tm-headActions' }, [
|
|
824
|
+
h('button', { key: 'pick', type: 'button', className: 'tm-btn ghost', disabled: busy !== '', onClick: changeRoot }, '更改文件夹'),
|
|
825
|
+
h('button', { key: 'refresh', type: 'button', className: 'tm-btn ghost', disabled: busy !== '', onClick: () => void refresh() }, '刷新'),
|
|
826
|
+
]),
|
|
745
827
|
]),
|
|
746
828
|
]),
|
|
747
829
|
notice ? h(Banner, { kind: notice.kind, title: notice.title, detail: notice.detail, onClose: () => setNotice(null) }) : null,
|
package/package.json
CHANGED
package/plugin.json
CHANGED
package/src/plugin.mjs
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* GET /api/twinsearth/timemachine/health
|
|
9
9
|
* GET /api/twinsearth/timemachine/status
|
|
10
|
+
* GET /api/twinsearth/timemachine/stats
|
|
10
11
|
* GET /api/twinsearth/timemachine/estimate
|
|
11
12
|
* POST /api/twinsearth/timemachine/create
|
|
12
13
|
* POST /api/twinsearth/timemachine/plan
|
|
@@ -32,6 +33,8 @@ import {
|
|
|
32
33
|
saveSettings,
|
|
33
34
|
startAutoScheduler,
|
|
34
35
|
} from './service.mjs';
|
|
36
|
+
import { startTelemetry } from './telemetry.mjs';
|
|
37
|
+
import { PLUGIN_VERSION } from './version.mjs';
|
|
35
38
|
|
|
36
39
|
export const name = 'timemachine';
|
|
37
40
|
export const inject = [];
|
|
@@ -40,6 +43,7 @@ export const ROUTE_PREFIX = '/api/twinsearth/timemachine';
|
|
|
40
43
|
export const ROUTE_PATHS = Object.freeze({
|
|
41
44
|
health: `${ROUTE_PREFIX}/health`,
|
|
42
45
|
status: `${ROUTE_PREFIX}/status`,
|
|
46
|
+
stats: `${ROUTE_PREFIX}/stats`,
|
|
43
47
|
estimate: `${ROUTE_PREFIX}/estimate`,
|
|
44
48
|
create: `${ROUTE_PREFIX}/create`,
|
|
45
49
|
plan: `${ROUTE_PREFIX}/plan`,
|
|
@@ -111,8 +115,16 @@ export function registerRoutes(ctx, rawConfig, deps = {}) {
|
|
|
111
115
|
});
|
|
112
116
|
};
|
|
113
117
|
|
|
114
|
-
register('GET', ROUTE_PATHS.health, async () => json({ ok: true, plugin: name, mounted: true }));
|
|
118
|
+
register('GET', ROUTE_PATHS.health, async () => json({ ok: true, plugin: name, version: PLUGIN_VERSION, mounted: true }));
|
|
115
119
|
register('GET', ROUTE_PATHS.status, async () => json(getStatus(rawConfig, deps)));
|
|
120
|
+
register('GET', ROUTE_PATHS.stats, async () => json(await (deps.telemetry?.getStats?.() ?? {
|
|
121
|
+
ok: false,
|
|
122
|
+
version: PLUGIN_VERSION,
|
|
123
|
+
online: null,
|
|
124
|
+
sevenDay: null,
|
|
125
|
+
installs: null,
|
|
126
|
+
error: 'telemetry unavailable',
|
|
127
|
+
})));
|
|
116
128
|
register('GET', ROUTE_PATHS.estimate, async () => json({ ok: true, ...getEstimate(rawConfig, deps) }));
|
|
117
129
|
register('POST', ROUTE_PATHS.create, async (request) => guarded('create', async () => {
|
|
118
130
|
const body = await readBody(request);
|
|
@@ -186,6 +198,14 @@ export function apply(ctx, config = {}, deps = {}) {
|
|
|
186
198
|
}
|
|
187
199
|
},
|
|
188
200
|
};
|
|
201
|
+
if (runtimeDeps.telemetry === undefined) {
|
|
202
|
+
try {
|
|
203
|
+
runtimeDeps.telemetry = startTelemetry({ logger: ctx.logger });
|
|
204
|
+
} catch (error) {
|
|
205
|
+
runtimeDeps.telemetry = null;
|
|
206
|
+
ctx.logger?.warn?.(`${name}: telemetry did not start: ${error instanceof Error ? error.message : String(error)}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
189
209
|
const routes = registerRoutes(ctx, config, runtimeDeps);
|
|
190
210
|
ctx.logger?.info?.(`${name}: routes ${routes.mounted ? 'mounted' : `not mounted (${routes.reason})`}`);
|
|
191
211
|
const stopScheduler = startAutoScheduler({
|
|
@@ -200,10 +220,12 @@ export function apply(ctx, config = {}, deps = {}) {
|
|
|
200
220
|
ctx.effect?.(() => () => {
|
|
201
221
|
disposed = true;
|
|
202
222
|
stopScheduler();
|
|
223
|
+
runtimeDeps.telemetry?.stop?.();
|
|
203
224
|
});
|
|
204
225
|
const dispose = () => {
|
|
205
226
|
disposed = true;
|
|
206
227
|
stopScheduler();
|
|
228
|
+
runtimeDeps.telemetry?.stop?.();
|
|
207
229
|
};
|
|
208
230
|
for (const [key, value] of Object.entries({
|
|
209
231
|
ok: true,
|
package/src/service.mjs
CHANGED
|
@@ -29,6 +29,7 @@ import { planRestore, restorePoint } from './restore.mjs';
|
|
|
29
29
|
import { normalizeRegistry } from './scan.mjs';
|
|
30
30
|
import { atomicWriteJson, ensureDir, readJson } from './util/fs.mjs';
|
|
31
31
|
import { profileDir, resolveDshHome } from './util/paths.mjs';
|
|
32
|
+
import { PLUGIN_VERSION } from './version.mjs';
|
|
32
33
|
|
|
33
34
|
export const DEFAULT_SETTINGS = Object.freeze({
|
|
34
35
|
root: '',
|
|
@@ -198,6 +199,7 @@ export function getStatus(rawConfig, options = {}) {
|
|
|
198
199
|
const registry = normalizeRegistry(readJson(path.join(dshHome, 'storages', 'workspace.json'), null));
|
|
199
200
|
return {
|
|
200
201
|
ok: true,
|
|
202
|
+
version: PLUGIN_VERSION,
|
|
201
203
|
platform: process.platform,
|
|
202
204
|
platform_label: process.platform === 'win32' ? 'Windows' : process.platform === 'darwin' ? 'macOS' : 'Linux',
|
|
203
205
|
dsh_home: dshHome,
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Install identity, lease heartbeat and remote statistics for Time Machine.
|
|
3
|
+
*
|
|
4
|
+
* The install token is the only identity: a fresh directory (or a fresh
|
|
5
|
+
* install) creates a new token and is counted once. Restarting the same
|
|
6
|
+
* directory reuses the token. If the install directory is read-only, a stable
|
|
7
|
+
* path + hostname digest is used instead, which keeps restarts stable but
|
|
8
|
+
* cannot distinguish a reinstall in the same path.
|
|
9
|
+
*/
|
|
10
|
+
import crypto from 'node:crypto';
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import os from 'node:os';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
|
|
16
|
+
import { PLUGIN_VERSION } from './version.mjs';
|
|
17
|
+
|
|
18
|
+
export const DEFAULT_TELEMETRY_URL = 'http://43.142.74.71:38081';
|
|
19
|
+
export const TOKEN_FILE = '.timemachine-install-token';
|
|
20
|
+
export const LEASE_MS = 399 * 60 * 1000;
|
|
21
|
+
export const CYCLE_MS = 3 * 60 * 60 * 1000;
|
|
22
|
+
export const STATS_CACHE_MS = 30 * 1000;
|
|
23
|
+
export const RECONNECT_COOLDOWN_MS = 400 * 60 * 1000;
|
|
24
|
+
|
|
25
|
+
const TOKEN_RE = /^[A-Za-z0-9_-]{16,128}$/u;
|
|
26
|
+
const START_DELAY_MIN_MS = 1000;
|
|
27
|
+
const START_DELAY_MAX_MS = 11000;
|
|
28
|
+
const CLAIM_RETRY_MIN_MS = 2 * 60 * 1000;
|
|
29
|
+
const CLAIM_RETRY_MAX_MS = 6 * 60 * 1000;
|
|
30
|
+
const RECEIPT_RETRY_MIN_MS = 2 * 60 * 1000;
|
|
31
|
+
const RECEIPT_RETRY_MAX_MS = 6 * 60 * 1000;
|
|
32
|
+
const RECONNECT_FIRST_MIN_MS = 1000;
|
|
33
|
+
const RECONNECT_FIRST_MAX_MS = 60 * 1000;
|
|
34
|
+
const RECONNECT_STEPS_MS = [100 * 1000, 300 * 1000, 1000 * 1000];
|
|
35
|
+
|
|
36
|
+
function randomBetween(min, max) {
|
|
37
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function iso(value) {
|
|
41
|
+
return Number.isFinite(value) && value > 0 ? new Date(value).toISOString() : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function parseTime(value) {
|
|
45
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
46
|
+
if (typeof value !== 'string' || value === '') return 0;
|
|
47
|
+
const parsed = Date.parse(value);
|
|
48
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Package root, i.e. the directory containing package.json and plugin.json. */
|
|
52
|
+
export function packageRoot() {
|
|
53
|
+
return path.resolve(fileURLToPath(new URL('..', import.meta.url)));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function installTokenPath(root) {
|
|
57
|
+
return path.join(path.resolve(root), TOKEN_FILE);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function readToken(file) {
|
|
61
|
+
try {
|
|
62
|
+
const value = fs.readFileSync(file, 'utf8').trim();
|
|
63
|
+
return TOKEN_RE.test(value) ? value : null;
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Read or create the install token in the plugin directory.
|
|
71
|
+
* Never throws: a read-only directory degrades to a stable digest.
|
|
72
|
+
*/
|
|
73
|
+
export function ensureInstallToken(root, { hostname = os.hostname() } = {}) {
|
|
74
|
+
const dir = path.resolve(root);
|
|
75
|
+
const file = installTokenPath(dir);
|
|
76
|
+
const existing = readToken(file);
|
|
77
|
+
if (existing) return { token: existing, source: 'file', path: file };
|
|
78
|
+
|
|
79
|
+
const token = crypto.randomBytes(24).toString('hex');
|
|
80
|
+
try {
|
|
81
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
82
|
+
const fd = fs.openSync(file, 'wx', 0o600);
|
|
83
|
+
try {
|
|
84
|
+
fs.writeFileSync(fd, `${token}\n`, 'utf8');
|
|
85
|
+
fs.fsyncSync(fd);
|
|
86
|
+
} finally {
|
|
87
|
+
fs.closeSync(fd);
|
|
88
|
+
}
|
|
89
|
+
try { fs.chmodSync(file, 0o600); } catch { /* best effort on Windows */ }
|
|
90
|
+
return { token, source: 'file', path: file };
|
|
91
|
+
} catch (error) {
|
|
92
|
+
// Another process may have won the O_EXCL race.
|
|
93
|
+
const raced = readToken(file);
|
|
94
|
+
if (raced) return { token: raced, source: 'file', path: file };
|
|
95
|
+
const digest = crypto
|
|
96
|
+
.createHash('sha256')
|
|
97
|
+
.update(`timemachine-install\0${dir}\0${hostname}`)
|
|
98
|
+
.digest('hex');
|
|
99
|
+
return { token: digest, source: 'fallback', path: file, error: error instanceof Error ? error.message : String(error) };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function normalizeStats(payload) {
|
|
104
|
+
if (!payload || payload.ok === false) throw new Error(payload?.error ?? 'stats unavailable');
|
|
105
|
+
const online = Number(payload.online);
|
|
106
|
+
const sevenDay = Number(payload.sevenDay ?? payload.seven_day ?? payload.active7d);
|
|
107
|
+
const installs = Number(payload.installs ?? payload.history);
|
|
108
|
+
if (![online, sevenDay, installs].every((value) => Number.isFinite(value))) {
|
|
109
|
+
throw new Error('stats payload is incomplete');
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
ok: true,
|
|
113
|
+
version: payload.version ?? PLUGIN_VERSION,
|
|
114
|
+
online,
|
|
115
|
+
sevenDay,
|
|
116
|
+
installs,
|
|
117
|
+
history: Number(payload.history ?? installs),
|
|
118
|
+
updated_at: payload.updated_at ?? payload.updatedAt ?? null,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Create the telemetry state machine. `start()` is deliberately delayed by
|
|
124
|
+
* 1–11 s so a fleet restart does not stampede the service.
|
|
125
|
+
*/
|
|
126
|
+
export function createTelemetry({
|
|
127
|
+
root = packageRoot(),
|
|
128
|
+
url = process.env.TIMEMACHINE_STATS_URL || DEFAULT_TELEMETRY_URL,
|
|
129
|
+
logger = null,
|
|
130
|
+
fetchImpl = globalThis.fetch,
|
|
131
|
+
} = {}) {
|
|
132
|
+
const baseUrl = String(url).replace(/\/+$/u, '');
|
|
133
|
+
const tokenInfo = ensureInstallToken(root);
|
|
134
|
+
let token = tokenInfo.token;
|
|
135
|
+
let stopped = false;
|
|
136
|
+
let timer = null;
|
|
137
|
+
let lastOkAt = Date.now();
|
|
138
|
+
let leaseUntil = 0;
|
|
139
|
+
let nextReceiptAt = 0;
|
|
140
|
+
let missed = 0;
|
|
141
|
+
let statsCache = null;
|
|
142
|
+
let statsCacheAt = 0;
|
|
143
|
+
let statsInFlight = null;
|
|
144
|
+
let lastError = '';
|
|
145
|
+
let reconnecting = false;
|
|
146
|
+
let reconnectStep = 0;
|
|
147
|
+
|
|
148
|
+
function warn(message) {
|
|
149
|
+
if (message === lastError) return;
|
|
150
|
+
lastError = message;
|
|
151
|
+
try {
|
|
152
|
+
logger?.warn?.(`timemachine: telemetry ${message}`);
|
|
153
|
+
} catch { /* logging must never break the plugin */ }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function request(route, body, method = 'POST') {
|
|
157
|
+
if (typeof fetchImpl !== 'function') throw new Error('fetch is unavailable');
|
|
158
|
+
const controller = new AbortController();
|
|
159
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
160
|
+
timeout.unref?.();
|
|
161
|
+
try {
|
|
162
|
+
const response = await fetchImpl(`${baseUrl}${route}`, {
|
|
163
|
+
method,
|
|
164
|
+
headers: body === undefined ? { accept: 'application/json' } : { accept: 'application/json', 'content-type': 'application/json' },
|
|
165
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
166
|
+
signal: controller.signal,
|
|
167
|
+
});
|
|
168
|
+
let payload = null;
|
|
169
|
+
try { payload = await response.json(); } catch { payload = null; }
|
|
170
|
+
if (!response.ok) throw new Error(payload?.error ?? `HTTP ${response.status}`);
|
|
171
|
+
if (!payload || payload.ok === false) throw new Error(payload?.error ?? 'bad response');
|
|
172
|
+
return payload;
|
|
173
|
+
} finally {
|
|
174
|
+
clearTimeout(timeout);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function schedule(delay, work, label) {
|
|
179
|
+
if (stopped) return;
|
|
180
|
+
if (timer) clearTimeout(timer);
|
|
181
|
+
const ms = Math.max(0, Math.floor(Number(delay) || 0));
|
|
182
|
+
timer = setTimeout(() => {
|
|
183
|
+
timer = null;
|
|
184
|
+
Promise.resolve()
|
|
185
|
+
.then(work)
|
|
186
|
+
.catch((error) => warn(`${label}: ${error instanceof Error ? error.message : String(error)}`));
|
|
187
|
+
}, ms);
|
|
188
|
+
timer.unref?.();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function applySchedule(payload) {
|
|
192
|
+
const lease = parseTime(payload.lease_until ?? payload.leaseUntil);
|
|
193
|
+
const next = parseTime(payload.next_receipt_at ?? payload.nextReceiptAt);
|
|
194
|
+
if (lease > 0) leaseUntil = lease;
|
|
195
|
+
if (next > 0) nextReceiptAt = next;
|
|
196
|
+
lastOkAt = Date.now();
|
|
197
|
+
lastError = '';
|
|
198
|
+
missed = 0;
|
|
199
|
+
reconnecting = false;
|
|
200
|
+
reconnectStep = 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function scheduleReceipt() {
|
|
204
|
+
const delay = nextReceiptAt > 0 ? nextReceiptAt - Date.now() : 0;
|
|
205
|
+
schedule(Math.max(1000, delay), receipt, 'receipt');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function leaseOnce() {
|
|
209
|
+
const payload = await request('/v1/lease', {
|
|
210
|
+
token,
|
|
211
|
+
version: PLUGIN_VERSION,
|
|
212
|
+
platform: process.platform,
|
|
213
|
+
arch: process.arch,
|
|
214
|
+
});
|
|
215
|
+
applySchedule(payload);
|
|
216
|
+
return payload;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function scheduleReconnect() {
|
|
220
|
+
if (stopped || reconnecting) return;
|
|
221
|
+
reconnecting = true;
|
|
222
|
+
reconnectStep = 0;
|
|
223
|
+
const delay = randomBetween(RECONNECT_FIRST_MIN_MS, RECONNECT_FIRST_MAX_MS);
|
|
224
|
+
schedule(delay, reconnectTick, 'reconnect-first');
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function reconnectTick() {
|
|
228
|
+
if (stopped) return;
|
|
229
|
+
try {
|
|
230
|
+
await leaseOnce();
|
|
231
|
+
scheduleReceipt();
|
|
232
|
+
return;
|
|
233
|
+
} catch (error) {
|
|
234
|
+
warn(`reconnect failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
235
|
+
if (reconnectStep >= RECONNECT_STEPS_MS.length) {
|
|
236
|
+
reconnecting = false;
|
|
237
|
+
reconnectStep = 0;
|
|
238
|
+
schedule(RECONNECT_COOLDOWN_MS, claim, 'reconnect-cooldown');
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const delay = RECONNECT_STEPS_MS[reconnectStep];
|
|
242
|
+
reconnectStep += 1;
|
|
243
|
+
schedule(delay, reconnectTick, `reconnect-${reconnectStep}`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function onClaimFailure() {
|
|
248
|
+
if (Date.now() - lastOkAt >= LEASE_MS || (leaseUntil > 0 && Date.now() >= leaseUntil)) {
|
|
249
|
+
scheduleReconnect();
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
schedule(randomBetween(CLAIM_RETRY_MIN_MS, CLAIM_RETRY_MAX_MS), claim, 'claim-retry');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async function claim() {
|
|
256
|
+
if (stopped) return;
|
|
257
|
+
try {
|
|
258
|
+
await leaseOnce();
|
|
259
|
+
scheduleReceipt();
|
|
260
|
+
} catch (error) {
|
|
261
|
+
warn(`claim failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
262
|
+
onClaimFailure();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async function receipt() {
|
|
267
|
+
if (stopped) return;
|
|
268
|
+
try {
|
|
269
|
+
const payload = await request('/v1/receipt', {
|
|
270
|
+
token,
|
|
271
|
+
scheduled_for: iso(nextReceiptAt),
|
|
272
|
+
});
|
|
273
|
+
applySchedule(payload);
|
|
274
|
+
scheduleReceipt();
|
|
275
|
+
} catch (error) {
|
|
276
|
+
warnedReceipt(error);
|
|
277
|
+
missed += 1;
|
|
278
|
+
if (missed < 3) {
|
|
279
|
+
schedule(randomBetween(RECEIPT_RETRY_MIN_MS, RECEIPT_RETRY_MAX_MS), receipt, 'receipt-retry');
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
missed = 0;
|
|
283
|
+
// Three misses: put this install back into the next cycle and claim again.
|
|
284
|
+
const now = Date.now();
|
|
285
|
+
const nextCycle = now - (now % CYCLE_MS) + CYCLE_MS;
|
|
286
|
+
schedule(Math.max(1000, nextCycle - now + randomBetween(START_DELAY_MIN_MS, START_DELAY_MAX_MS)), claim, 'next-cycle');
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function warnedReceipt(error) {
|
|
291
|
+
warn(`receipt failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async function getStats() {
|
|
295
|
+
const now = Date.now();
|
|
296
|
+
if (statsCache && now - statsCacheAt < STATS_CACHE_MS) return statsCache;
|
|
297
|
+
if (statsInFlight) return statsInFlight;
|
|
298
|
+
statsInFlight = (async () => {
|
|
299
|
+
try {
|
|
300
|
+
const payload = await request('/v1/stats', undefined, 'GET');
|
|
301
|
+
const normalized = normalizeStats(payload);
|
|
302
|
+
statsCache = normalized;
|
|
303
|
+
statsCacheAt = Date.now();
|
|
304
|
+
return normalized;
|
|
305
|
+
} catch (error) {
|
|
306
|
+
warn(`stats failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
307
|
+
if (statsCache) return { ...statsCache, stale: true };
|
|
308
|
+
return {
|
|
309
|
+
ok: false,
|
|
310
|
+
version: PLUGIN_VERSION,
|
|
311
|
+
online: null,
|
|
312
|
+
sevenDay: null,
|
|
313
|
+
installs: null,
|
|
314
|
+
history: null,
|
|
315
|
+
updated_at: null,
|
|
316
|
+
error: 'stats unavailable',
|
|
317
|
+
};
|
|
318
|
+
} finally {
|
|
319
|
+
statsInFlight = null;
|
|
320
|
+
}
|
|
321
|
+
})();
|
|
322
|
+
return statsInFlight;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function snapshot() {
|
|
326
|
+
return {
|
|
327
|
+
version: PLUGIN_VERSION,
|
|
328
|
+
token_source: tokenInfo.source,
|
|
329
|
+
connected: leaseUntil > Date.now(),
|
|
330
|
+
lease_until: iso(leaseUntil),
|
|
331
|
+
next_receipt_at: iso(nextReceiptAt),
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function start() {
|
|
336
|
+
if (!stopped) {
|
|
337
|
+
schedule(randomBetween(START_DELAY_MIN_MS, START_DELAY_MAX_MS), claim, 'startup');
|
|
338
|
+
}
|
|
339
|
+
return api;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function stop() {
|
|
343
|
+
stopped = true;
|
|
344
|
+
if (timer) clearTimeout(timer);
|
|
345
|
+
timer = null;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const api = {
|
|
349
|
+
start,
|
|
350
|
+
stop,
|
|
351
|
+
getStats,
|
|
352
|
+
snapshot,
|
|
353
|
+
get token() { return token; },
|
|
354
|
+
get installRoot() { return path.resolve(root); },
|
|
355
|
+
};
|
|
356
|
+
return api;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Convenience entry used by the Cordis plugin. */
|
|
360
|
+
export function startTelemetry(options = {}) {
|
|
361
|
+
if (process.env.TIMEMACHINE_TELEMETRY_DISABLED === '1') return null;
|
|
362
|
+
return createTelemetry(options).start();
|
|
363
|
+
}
|
package/src/version.mjs
ADDED