dsh-message-actions 0.2.4 → 0.2.8
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 +104 -244
- package/lib/client.js +7307 -4483
- package/lib/http.cjs +37 -1
- package/lib/index.js +625 -83
- package/lib/session-manager.mjs +1164 -0
- package/lib/sessions.cjs +210 -0
- package/lib/shared.cjs +2873 -2357
- package/lib/store.cjs +288 -265
- package/locale/en.json +2 -2
- package/locale/zh.json +2 -2
- package/package.json +5 -2
- package/README.cn.md +0 -289
package/lib/sessions.cjs
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* dsh-message-actions — 会话管理的纯函数(无 DSH 依赖,可离线测试)。
|
|
5
|
+
*
|
|
6
|
+
* 这里只放不产生副作用的逻辑:参数校验、路径包含判断、会话目录命名
|
|
7
|
+
* (与宿主 session-persistence 的公开命名约定一致)、标题快照取值、
|
|
8
|
+
* 回收站元数据校验与排序。真正的文件移动 / 删除在 session-manager.mjs。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const path = require('node:path');
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// 标识符校验
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
/** 会话 id:宿主生成的 `session-<uuid>`;禁止任何路径分隔符与控制字符。 */
|
|
18
|
+
const SESSION_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,159}$/;
|
|
19
|
+
/** 工作区 id:宿主持久化的 UUID。 */
|
|
20
|
+
const WORKSPACE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,159}$/;
|
|
21
|
+
/** 回收站条目 id:本插件生成,带 `trash-` 前缀便于人工识别。 */
|
|
22
|
+
const TRASH_ID_RE = /^trash-[A-Za-z0-9-]{8,80}$/;
|
|
23
|
+
|
|
24
|
+
function isSessionId(value) {
|
|
25
|
+
return typeof value === 'string' && SESSION_ID_RE.test(value);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isWorkspaceId(value) {
|
|
29
|
+
return typeof value === 'string' && WORKSPACE_ID_RE.test(value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isTrashId(value) {
|
|
33
|
+
return typeof value === 'string' && TRASH_ID_RE.test(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// 路径
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
const IS_WINDOWS = process.platform === 'win32';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* child 是否等于 parent 或位于 parent 之内(只做字符串级规范化,不触碰磁盘)。
|
|
44
|
+
* Windows 下大小写不敏感,避免 `C:\A` 与 `c:\a` 被误判为目录穿越。
|
|
45
|
+
*/
|
|
46
|
+
function isContainedPath(parent, child) {
|
|
47
|
+
if (typeof parent !== 'string' || typeof child !== 'string') return false;
|
|
48
|
+
if (parent === '' || child === '') return false;
|
|
49
|
+
const base = path.resolve(parent);
|
|
50
|
+
const target = path.resolve(child);
|
|
51
|
+
const a = IS_WINDOWS ? base.toLowerCase() : base;
|
|
52
|
+
const b = IS_WINDOWS ? target.toLowerCase() : target;
|
|
53
|
+
if (a === b) return true;
|
|
54
|
+
return b.startsWith(a.endsWith(path.sep) ? a : a + path.sep);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 会话 id 的单段路径编码(宿主公开约定):
|
|
59
|
+
* 仅 [A-Za-z0-9._-] 原样保留,其余 UTF-16 码元写成 `~XXXX`。
|
|
60
|
+
* 这里为无法提供 `sessionPersistence.locate` 的旧宿主保留兜底实现。
|
|
61
|
+
*/
|
|
62
|
+
function encodeSegment(raw) {
|
|
63
|
+
const text = String(raw == null ? '' : raw);
|
|
64
|
+
if (text === '') throw new Error('cannot encode an empty path segment');
|
|
65
|
+
if (text === '.') return '~002E';
|
|
66
|
+
if (text === '..') return '~002E~002E';
|
|
67
|
+
let out = '';
|
|
68
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
69
|
+
const code = text.charCodeAt(i);
|
|
70
|
+
const ch = String.fromCharCode(code);
|
|
71
|
+
if (ch !== '~' && /^[A-Za-z0-9._-]$/.test(ch)) out += ch;
|
|
72
|
+
else out += '~' + code.toString(16).toUpperCase().padStart(4, '0');
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 会话所属项目目录的目录名(宿主公开约定):
|
|
79
|
+
* 路径分隔符与盘符收敛为单个 `-`,其余不安全码元按 `~XXXX` 转义,
|
|
80
|
+
* 首尾用 `--` 包裹并截断到 251 字符。
|
|
81
|
+
*/
|
|
82
|
+
function projectKeyOf(cwd) {
|
|
83
|
+
const text = String(cwd == null ? '' : cwd);
|
|
84
|
+
if (text === '') throw new Error('cannot encode an empty project path');
|
|
85
|
+
let readable = '';
|
|
86
|
+
let separated = false;
|
|
87
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
88
|
+
const code = text.charCodeAt(i);
|
|
89
|
+
const ch = String.fromCharCode(code);
|
|
90
|
+
if (ch === '/' || ch === '\\' || ch === ':') {
|
|
91
|
+
if (!separated) readable += '-';
|
|
92
|
+
separated = true;
|
|
93
|
+
} else if (ch !== '~' && /^[A-Za-z0-9._-]$/.test(ch)) {
|
|
94
|
+
readable += ch;
|
|
95
|
+
separated = false;
|
|
96
|
+
} else {
|
|
97
|
+
readable += '~' + code.toString(16).toUpperCase().padStart(4, '0');
|
|
98
|
+
separated = false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const head = readable.replace(/^-+/, '') || 'root';
|
|
102
|
+
return '--' + head.slice(0, 251) + '--';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 会话日志所在目录:`<root>/<projectKey(cwd)>/<encodeSegment(id)>`。 */
|
|
106
|
+
function sessionDirOf(root, cwd, id) {
|
|
107
|
+
if (cwd === undefined || cwd === null || cwd === '') return path.join(root, '_no-cwd', encodeSegment(id));
|
|
108
|
+
return path.join(root, projectKeyOf(cwd), encodeSegment(id));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
// 标题与元数据
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
|
|
115
|
+
/** 从宿主的标题快照(字符串 / {title} / {value:{title}})取出纯文本。 */
|
|
116
|
+
function titleTextOf(snapshot) {
|
|
117
|
+
if (typeof snapshot === 'string') return snapshot.trim();
|
|
118
|
+
if (!snapshot || typeof snapshot !== 'object') return '';
|
|
119
|
+
if (typeof snapshot.title === 'string') return snapshot.title.trim();
|
|
120
|
+
if (snapshot.value && typeof snapshot.value.title === 'string') return snapshot.value.title.trim();
|
|
121
|
+
return '';
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 回收站元数据的字段白名单与长度上限(防止被篡改的 meta.json 撑爆界面)。 */
|
|
125
|
+
const TRASH_META_VERSION = 1;
|
|
126
|
+
const MAX_TITLE_LENGTH = 300;
|
|
127
|
+
const MAX_PATH_LENGTH = 4096;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 校验并收敛一份回收站元数据。任何必需字段不合法都返回 null,
|
|
131
|
+
* 调用方据此跳过损坏条目,而不是把任意 JSON 当路径使用。
|
|
132
|
+
*/
|
|
133
|
+
function normalizeTrashMeta(raw) {
|
|
134
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
|
|
135
|
+
if (raw.version !== TRASH_META_VERSION) return null;
|
|
136
|
+
if (!isTrashId(raw.trashId)) return null;
|
|
137
|
+
if (!isSessionId(raw.id)) return null;
|
|
138
|
+
const header = raw.header;
|
|
139
|
+
if (!header || typeof header !== 'object' || Array.isArray(header)) return null;
|
|
140
|
+
if (header.id !== raw.id) return null;
|
|
141
|
+
if (typeof header.cwd !== 'string' || header.cwd.length > MAX_PATH_LENGTH) return null;
|
|
142
|
+
if (!Number.isFinite(raw.deletedAt) || raw.deletedAt <= 0) return null;
|
|
143
|
+
if (typeof raw.originalDir !== 'string' || raw.originalDir.length > MAX_PATH_LENGTH) return null;
|
|
144
|
+
const sizeBytes = Number.isFinite(raw.sizeBytes) && raw.sizeBytes >= 0 ? Math.trunc(raw.sizeBytes) : 0;
|
|
145
|
+
const title = typeof raw.title === 'string' ? raw.title.slice(0, MAX_TITLE_LENGTH) : '';
|
|
146
|
+
const workspaceId = typeof raw.workspaceId === 'string' && isWorkspaceId(raw.workspaceId) ? raw.workspaceId : '';
|
|
147
|
+
const workspaceTitle = typeof raw.workspaceTitle === 'string'
|
|
148
|
+
? raw.workspaceTitle.slice(0, MAX_TITLE_LENGTH)
|
|
149
|
+
: '';
|
|
150
|
+
return {
|
|
151
|
+
version: TRASH_META_VERSION,
|
|
152
|
+
trashId: raw.trashId,
|
|
153
|
+
id: raw.id,
|
|
154
|
+
deletedAt: Math.trunc(raw.deletedAt),
|
|
155
|
+
title,
|
|
156
|
+
header,
|
|
157
|
+
originalDir: raw.originalDir,
|
|
158
|
+
sizeBytes,
|
|
159
|
+
workspaceId,
|
|
160
|
+
workspaceTitle
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ---------------------------------------------------------------------------
|
|
165
|
+
// 排序
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
function timeOf(value) {
|
|
169
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
170
|
+
if (typeof value === 'string') {
|
|
171
|
+
const parsed = Date.parse(value);
|
|
172
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
173
|
+
}
|
|
174
|
+
return 0;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** 会话按创建时间倒序;时间相同按 id 稳定排序。 */
|
|
178
|
+
function sortSessionsNewestFirst(rows) {
|
|
179
|
+
return (Array.isArray(rows) ? rows.slice() : []).sort((left, right) => {
|
|
180
|
+
const delta = timeOf(right && right.createdAt) - timeOf(left && left.createdAt);
|
|
181
|
+
if (delta !== 0) return delta;
|
|
182
|
+
return String(left && left.id).localeCompare(String(right && right.id));
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** 回收站按删除时间倒序。 */
|
|
187
|
+
function sortTrashNewestFirst(entries) {
|
|
188
|
+
return (Array.isArray(entries) ? entries.slice() : []).sort((left, right) => {
|
|
189
|
+
return (right && right.deletedAt ? right.deletedAt : 0) - (left && left.deletedAt ? left.deletedAt : 0);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
module.exports = {
|
|
194
|
+
SESSION_ID_RE,
|
|
195
|
+
WORKSPACE_ID_RE,
|
|
196
|
+
TRASH_ID_RE,
|
|
197
|
+
TRASH_META_VERSION,
|
|
198
|
+
MAX_TITLE_LENGTH,
|
|
199
|
+
isSessionId,
|
|
200
|
+
isWorkspaceId,
|
|
201
|
+
isTrashId,
|
|
202
|
+
isContainedPath,
|
|
203
|
+
encodeSegment,
|
|
204
|
+
projectKeyOf,
|
|
205
|
+
sessionDirOf,
|
|
206
|
+
titleTextOf,
|
|
207
|
+
normalizeTrashMeta,
|
|
208
|
+
sortSessionsNewestFirst,
|
|
209
|
+
sortTrashNewestFirst
|
|
210
|
+
};
|