dsh-mobile-plugin 0.5.52 → 0.5.53

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.
@@ -1,142 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * 操作记录回传:手机把「调试面板 → 操作记录」导出的一段正文交给宿主,宿主原样落到本机磁盘。
5
- *
6
- * **数据只落在用户自己这台电脑上,不出网。** 这条路由不转发给中转、不落任何远端存储:
7
- * 收到什么就写进 `$DSH_HOME/mobile-traces/` 里的一个 JSON 文件,仅此而已。检查「手机上滑得
8
- * 顺不顺」要的是那条设备自己记的时间线,留在本机就够了;把它送上云反而多一份没人要的副本。
9
- *
10
- * 正文(`text`)按原样存,不压成一行:这份文件是给人直接打开看的,转义过的 `\n` 一整片
11
- * 读不出任何东西。
12
- *
13
- * 同目录的 `index.jsonl` 是一份轻量目录(每条一行:文件名 / 收到时刻 / 字节数 / 设备),
14
- * 方便按时间翻;它写不进去不影响这次回传本身 —— 正文已经落盘了。
15
- */
16
-
17
- const fs = require('node:fs');
18
- const path = require('node:path');
19
-
20
- const { dshHome } = require('../shared/paths');
21
- const { sendJson, readJsonBody } = require('../shared/http-util');
22
-
23
- /** 正文上限:4MB(按 UTF-8 字节数算)。一次操作记录是文本时间线,到这个量已经是"记太久"。 */
24
- const MAX_TEXT_BYTES = 4 * 1024 * 1024;
25
- /**
26
- * 请求体上限:比正文宽一档。JSON 里换行 / 引号都要转义,4MB 正文的请求体可能大出几成;
27
- * 留到 12MB 是为了「正文合法但转义撑大了」不被误判成超限,真正的判据仍是 `text` 的字节数。
28
- */
29
- const MAX_BODY_BYTES = 12 * 1024 * 1024;
30
- /** label 进文件名的最长长度。 */
31
- const MAX_LABEL = 40;
32
- /** deviceId 短码取前几位。 */
33
- const DEVICE_SHORT = 8;
34
-
35
- /** 操作记录目录:`$DSH_HOME/mobile-traces`。 */
36
- function tracesDir() {
37
- return path.join(dshHome(), 'mobile-traces');
38
- }
39
-
40
- /** 文件名安全化:只留 `[a-zA-Z0-9_-]`,其它一律变 `_`。 */
41
- function safeSegment(value, fallback) {
42
- const raw = typeof value === 'string' ? value : '';
43
- const cleaned = raw.replace(/[^a-zA-Z0-9_-]/g, '_');
44
- return cleaned || fallback;
45
- }
46
-
47
- /** 本地时间戳,冒号换掉(`:` 在文件名里不该出现):`2026-11-08T23-47-45-123`。 */
48
- function localStamp(date) {
49
- const pad = (n, width = 2) => String(n).padStart(width, '0');
50
- return [
51
- `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`,
52
- `${pad(date.getHours())}-${pad(date.getMinutes())}-${pad(date.getSeconds())}-${pad(date.getMilliseconds(), 3)}`,
53
- ].join('T');
54
- }
55
-
56
- /**
57
- * 这次回传要落的文件名:`<本地时间戳>-<设备短码>-<label>.json`。
58
- *
59
- * @param {{ receivedAt: Date, deviceId?: string, label?: string }} input
60
- */
61
- function traceFileName({ receivedAt, deviceId, label }) {
62
- const device = safeSegment(deviceId, 'unknown').slice(0, DEVICE_SHORT);
63
- const tag = safeSegment(label, 'trace').slice(0, MAX_LABEL);
64
- return `${localStamp(receivedAt)}-${device || 'unknown'}-${tag}.json`;
65
- }
66
-
67
- /**
68
- * 处理 `POST /api/mobile/v1/perf-trace`。
69
- *
70
- * 请求体:`{ deviceId?, label?, startedAt: number, text: string, meta? }`。
71
- * 成功回 `{ ok: true, file, path }`。
72
- *
73
- * @param {object} _ctx 插件 ctx(这条路由用不到)
74
- * @param {object} req node:http IncomingMessage(信封请求的明文已在 `req.body`)
75
- * @param {object} res node:http ServerResponse
76
- */
77
- async function handlePerfTrace(_ctx, req, res) {
78
- const body = await readJsonBody(req, MAX_BODY_BYTES);
79
- if (!body || typeof body !== 'object' || Array.isArray(body)) {
80
- return sendJson(res, 400, { ok: false, error: '请求体不是合法 JSON(或超过 12MB 上限)' });
81
- }
82
-
83
- const text = body.text;
84
- if (typeof text !== 'string' || text.length === 0) {
85
- return sendJson(res, 400, { ok: false, error: 'text 必须是非空字符串' });
86
- }
87
- const bytes = Buffer.byteLength(text, 'utf8');
88
- if (bytes > MAX_TEXT_BYTES) {
89
- return sendJson(res, 413, { ok: false, error: `text 太大:${bytes} 字节,上限 ${MAX_TEXT_BYTES} 字节(4MB)` });
90
- }
91
-
92
- const startedAt = body.startedAt;
93
- if (typeof startedAt !== 'number' || !Number.isFinite(startedAt)) {
94
- return sendJson(res, 400, { ok: false, error: 'startedAt 必须是有限数字(毫秒时间戳)' });
95
- }
96
-
97
- const deviceId = typeof body.deviceId === 'string' ? body.deviceId : '';
98
- const label = typeof body.label === 'string' ? body.label : '';
99
- const meta = body.meta && typeof body.meta === 'object' ? body.meta : null;
100
-
101
- const receivedAt = new Date();
102
- const dir = tracesDir();
103
- const fileName = traceFileName({ receivedAt, deviceId, label });
104
- const filePath = path.join(dir, fileName);
105
-
106
- // 收到的原文 + 几格来源信息。2 空格缩进:这份文件是给人直接翻的。
107
- const payload = {
108
- receivedAt: receivedAt.toISOString(),
109
- deviceId,
110
- label,
111
- startedAt,
112
- meta,
113
- text,
114
- };
115
- const serialized = `${JSON.stringify(payload, null, 2)}\n`;
116
-
117
- try {
118
- fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
119
- fs.writeFileSync(filePath, serialized, { encoding: 'utf8', mode: 0o600 });
120
- } catch (error) {
121
- const reason = error && error.message ? error.message : String(error);
122
- return sendJson(res, 500, { ok: false, error: `写盘失败: ${reason}` });
123
- }
124
-
125
- // 目录索引:一条一行。它只是方便翻,写不进去不影响上面已经落好的正文。
126
- try {
127
- const entry = { file: fileName, receivedAt: receivedAt.toISOString(), bytes: Buffer.byteLength(serialized, 'utf8'), deviceId };
128
- fs.appendFileSync(path.join(dir, 'index.jsonl'), `${JSON.stringify(entry)}\n`, { encoding: 'utf8', mode: 0o600 });
129
- } catch { /* 索引可选,不影响主请求 */ }
130
-
131
- return sendJson(res, 200, { ok: true, file: fileName, path: filePath });
132
- }
133
-
134
- module.exports = {
135
- handlePerfTrace,
136
- tracesDir,
137
- traceFileName,
138
- safeSegment,
139
- MAX_TEXT_BYTES,
140
- MAX_LABEL,
141
- DEVICE_SHORT,
142
- };