@andy.ian/dsh-homing 2.0.0 → 2.0.2

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.
@@ -44,7 +44,21 @@ export function layout({ title, body }) {
44
44
  <html lang="zh-CN"><head><meta charset="utf-8">
45
45
  <meta name="viewport" content="width=device-width, initial-scale=1">
46
46
  <title>${escapeHtml(title)} · DSH 文件</title>
47
- <style>${STYLE}</style></head>
47
+ <style>${STYLE}.flist { display: flex; flex-direction: column; }
48
+ .sortbar { display: flex; align-items: center; gap: 6px; padding: 6px 2px 8px; color: #8b93a1; font-size: 12px; }
49
+ .sortlabel { margin-right: 2px; }
50
+ .sortbtn { padding: 4px 9px; border-radius: 6px; background: #222833; border: 1px solid #333c48; color: #b9c3d1; font-size: 12px; }
51
+ .sortbtn[data-active="1"] { background: #2f3a49; color: #e6edf5; border-color: #4a586b; }
52
+ .fitem { position: relative; display: grid; grid-template-columns: 22px auto 1fr auto; grid-template-areas: "ico size size menu" "name name name menu"; align-items: center; gap: 2px 6px; padding: 9px 2px 11px; border-bottom: 1px solid #232a35; }
53
+ .fico { grid-area: ico; font-size: 15px; line-height: 1; }
54
+ .fsize { grid-area: size; justify-self: start; color: #8b93a1; font-size: 12px; white-space: nowrap; pointer-events: none; }
55
+ .ftime { grid-area: 1 / 2 / 2 / 4; text-align: center; color: #8b93a1; font-size: 12px; white-space: nowrap; pointer-events: none; }
56
+ .fname { grid-area: name; color: #dfe6ef; text-decoration: none; font-size: 15px; word-break: break-all; }
57
+ .fname.dir { color: #cfe3ff; }
58
+ .fname.exe { color: #ffd479; }
59
+ .fempty { padding: 18px 4px; color: #8b93a1; }
60
+ .flist .mm { grid-area: menu; }
61
+ </style></head>
48
62
  <body><div class="wrap">${body}</div></body></html>`
49
63
  }
50
64
 
@@ -53,26 +67,187 @@ const messageHtml = (message, isError) => (message
53
67
  : '')
54
68
 
55
69
  /** 目录列表页 */
70
+ const MENU_JS = `<script>
71
+ (() => {
72
+ var openMenu = null;
73
+ function closeAll() {
74
+ document.querySelectorAll('.rowmenu').forEach(function (m) { m.hidden = true; });
75
+ openMenu = null;
76
+ }
77
+ function position(btn, menu) {
78
+ menu.style.left = '0px';
79
+ menu.style.top = '0px';
80
+ var r = btn.getBoundingClientRect();
81
+ var mw = menu.offsetWidth;
82
+ var mh = menu.offsetHeight;
83
+ var left = Math.max(8, Math.min(r.right - mw, window.innerWidth - mw - 8));
84
+ var up = (window.innerHeight - r.bottom) < (mh + 20);
85
+ var top = up ? (r.top - mh - 6) : (r.bottom + 6);
86
+ menu.style.left = left + 'px';
87
+ menu.style.top = Math.max(8, top) + 'px';
88
+ requestAnimationFrame(function () {
89
+ var w2 = menu.offsetWidth, h2 = menu.offsetHeight;
90
+ var l2 = Math.max(8, Math.min(r.right - w2, window.innerWidth - w2 - 8));
91
+ var t2 = ((window.innerHeight - r.bottom) < (h2 + 20)) ? (r.top - h2 - 6) : (r.bottom + 6);
92
+ menu.style.left = l2 + 'px';
93
+ menu.style.top = Math.max(8, t2) + 'px';
94
+ });
95
+ }
96
+ document.addEventListener('click', function (e) {
97
+ var btn = e.target.closest ? e.target.closest('.mm-btn') : null;
98
+ if (btn) {
99
+ e.preventDefault(); e.stopPropagation();
100
+ var menu = btn.parentNode.querySelector('.rowmenu');
101
+ var wasOpen = openMenu === menu;
102
+ closeAll();
103
+ if (!wasOpen) { menu.hidden = false; position(btn, menu); openMenu = menu; }
104
+ return;
105
+ }
106
+ var act = e.target.closest ? e.target.closest('.mi') : null;
107
+ if (act) {
108
+ e.preventDefault(); e.stopPropagation();
109
+ var kind = act.getAttribute('data-act');
110
+ var path = act.getAttribute('data-path');
111
+ closeAll();
112
+ if (kind === 'edit') {
113
+ location.href = '/files/edit?path=' + encodeURIComponent(path);
114
+ return;
115
+ }
116
+ if (kind === 'run') {
117
+ location.href = '/files/run?path=' + encodeURIComponent(path);
118
+ return;
119
+ }
120
+ if (kind === 'copy' || kind === 'cut') {
121
+ try { localStorage.setItem('dsh-files-clip', JSON.stringify({ path: path, op: kind === 'cut' ? 'cut' : 'copy' })); } catch (error) { /* 忽略 */ }
122
+ return;
123
+ }
124
+ if (kind === 'mkdir' || kind === 'newfile') {
125
+ var nm = window.prompt(kind === 'mkdir' ? '新文件夹名:' : '新文件名:', '');
126
+ if (!nm) return;
127
+ var api = kind === 'mkdir' ? '/files/mkdir' : '/files/newfile';
128
+ fetch(api, {
129
+ method: 'POST',
130
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
131
+ body: 'dir=' + encodeURIComponent(path) + '&name=' + encodeURIComponent(nm),
132
+ }).then(function () { location.reload(); }).catch(function () { location.reload(); });
133
+ return;
134
+ }
135
+ if (kind === 'paste') {
136
+ var raw = null;
137
+ try { raw = localStorage.getItem('dsh-files-clip'); } catch (error) { /* 忽略 */ }
138
+ if (!raw) { window.alert('剪贴板是空的,先点「复制」或「剪切」'); return; }
139
+ var clip = { path: raw, op: 'copy' };
140
+ try { var parsed = JSON.parse(raw); if (parsed && parsed.path) clip = parsed; } catch (error) { /* 兼容旧格式 */ }
141
+ fetch('/files/paste', {
142
+ method: 'POST',
143
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
144
+ body: 'src=' + encodeURIComponent(clip.path) + '&dir=' + encodeURIComponent(path) + '&op=' + encodeURIComponent(clip.op || 'copy'),
145
+ }).then(function () {
146
+ try { if ((clip.op || 'copy') === 'cut') localStorage.removeItem('dsh-files-clip'); } catch (error) { /* 忽略 */ }
147
+ location.reload();
148
+ }).catch(function () { location.reload(); });
149
+ return;
150
+ }
151
+ if (kind === 'copypath') {
152
+ if (navigator.clipboard) navigator.clipboard.writeText(path);
153
+ return;
154
+ }
155
+ if (kind === 'rename') {
156
+ var next = window.prompt('重命名为:', act.getAttribute('data-name'));
157
+ if (!next) return;
158
+ fetch('/files/rename', {
159
+ method: 'POST',
160
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
161
+ body: 'path=' + encodeURIComponent(path) + '&name=' + encodeURIComponent(next),
162
+ }).then(function () { location.reload(); }).catch(function () { location.reload(); });
163
+ return;
164
+ }
165
+ if (kind === 'delete') {
166
+ var isDir2 = act.getAttribute('data-dir') === '1';
167
+ if (!window.confirm(isDir2 ? '删除这个目录及其全部内容?' : '删除这个文件?')) return;
168
+ fetch('/files/delete', {
169
+ method: 'POST',
170
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
171
+ body: 'path=' + encodeURIComponent(path),
172
+ }).then(function () { location.reload(); }).catch(function () { location.reload(); });
173
+ return;
174
+ }
175
+ return;
176
+ }
177
+ closeAll();
178
+ }, true);
179
+ var sortState = { key: '', asc: true };
180
+ function applySort(key) {
181
+ var list = document.querySelector('.flist');
182
+ if (!list) return;
183
+ var asc = (sortState.key === key) ? !sortState.asc : true;
184
+ sortState = { key: key, asc: asc };
185
+ var items = Array.prototype.slice.call(list.querySelectorAll('.fitem'));
186
+ items.sort(function (a, b) {
187
+ var ad = a.getAttribute('data-dir') === '1', bd = b.getAttribute('data-dir') === '1';
188
+ if (ad !== bd) return ad ? -1 : 1;
189
+ var r = 0;
190
+ if (key === 'name') {
191
+ r = a.getAttribute('data-name').localeCompare(b.getAttribute('data-name'), 'zh');
192
+ } else if (key === 'size') {
193
+ r = Number(a.getAttribute('data-size')) - Number(b.getAttribute('data-size'));
194
+ } else {
195
+ r = Number(a.getAttribute('data-time')) - Number(b.getAttribute('data-time'));
196
+ }
197
+ return asc ? r : -r;
198
+ });
199
+ items.forEach(function (el) { list.appendChild(el); });
200
+ document.querySelectorAll('.sortbtn').forEach(function (btn) {
201
+ btn.setAttribute('data-active', btn.getAttribute('data-sort') === key ? '1' : '0');
202
+ btn.textContent = btn.getAttribute('data-sort') === 'name' ? '名称' : (btn.getAttribute('data-sort') === 'size' ? '大小' : '时间');
203
+ if (btn.getAttribute('data-sort') === key) btn.textContent += asc ? ' ↑' : ' ↓';
204
+ });
205
+ }
206
+ document.addEventListener('click', function (e) {
207
+ var btn = e.target.closest ? e.target.closest('.sortbtn') : null;
208
+ if (btn) { e.preventDefault(); applySort(btn.getAttribute('data-sort')); }
209
+ }, true);
210
+ document.addEventListener('keydown', function (e) { if (e.key === 'Escape') closeAll(); });
211
+ window.addEventListener('scroll', closeAll, { passive: true });
212
+ })();
213
+ </script>`;
214
+
56
215
  export function renderListing({ info, message, isError, home, dataDir, workspaces = [] }) {
216
+ const menuFor = (entry) => `<span class="mm">
217
+ <button type="button" class="mm-btn" aria-haspopup="menu" title="更多操作">\u22ef</button>
218
+ <span class="rowmenu" hidden role="menu">
219
+ ${entry.dir ? '' : `<a class="mi" role="menuitem" data-act="edit" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u270F\uFE0F</span>编辑</a>`}
220
+ ${entry.runnable ? `<a class="mi" role="menuitem" data-act="run" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u25B6\uFE0F</span>执行</a>` : ''}
221
+ <a class="mi" role="menuitem" data-act="rename" data-path="${escapeHtml(entry.path)}" data-name="${escapeHtml(entry.name)}" href="#"><span class="ico">\u{1F3F7}\uFE0F</span>重命名</a>
222
+ <a class="mi" role="menuitem" data-act="cut" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u2702\uFE0F</span>剪切</a>
223
+ <a class="mi" role="menuitem" data-act="copy" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4C4}</span>复制</a>
224
+ ${entry.dir ? `<a class="mi" role="menuitem" data-act="paste" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4E5}</span>粘贴到此处</a>
225
+ <a class="mi" role="menuitem" data-act="mkdir" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4C1}</span>新建文件夹</a>
226
+ <a class="mi" role="menuitem" data-act="newfile" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4C4}</span>新建文件</a>` : ''}
227
+ <a class="mi" role="menuitem" data-act="copypath" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4CB}</span>复制路径</a>
228
+ <a class="mi danger" role="menuitem" data-act="delete" data-path="${escapeHtml(entry.path)}" data-dir="${entry.dir ? 1 : 0}" href="#"><span class="ico">\u{1F5D1}\uFE0F</span>删除</a>
229
+ </span>
230
+ </span>`;
231
+
57
232
  const rows = info.entries.map((entry) => {
58
- const label = escapeHtml(entry.name)
59
- const nameCell = entry.dir
60
- ? `<a class="dir" href="/files?path=${encodeURIComponent(entry.path)}">📁 ${label}/</a>`
61
- : `<a class="${entry.runnable ? 'exe' : ''}" href="/files/view?path=${encodeURIComponent(entry.path)}">${entry.runnable ? '⚙️' : '📄'} ${label}</a>`
62
- + (entry.runnable ? '' : ` <a href="/files/edit?path=${encodeURIComponent(entry.path)}" title="编辑"
63
- style="font-size:12px;color:#9fc0ff;white-space:nowrap">✏️</a>`)
64
- // 运行改成链接:先进入运行页,可以填参数(很多脚本需要参数,比如提交信息)
65
- const ops = entry.runnable
66
- ? `<a class="small" href="/files/run?path=${encodeURIComponent(entry.path)}"
67
- style="display:inline-block;padding:6px 12px;border-radius:8px;background:#4f7cff;color:#fff;font-size:12px;font-weight:600;text-decoration:none">运行</a>`
68
- : ''
69
- return `<tr><td>${nameCell}</td><td class="size">${escapeHtml(entry.sizeText)}</td>`
70
- + `<td class="time">${escapeHtml(entry.mtime)}</td><td class="ops">${ops}</td></tr>`
71
- }).join('')
233
+ const label = escapeHtml(entry.name);
234
+ const icon = entry.dir ? '\u{1F4C1}' : (entry.runnable ? '\u2699\uFE0F' : '\u{1F4C4}');
235
+ const nameHtml = entry.dir
236
+ ? `<a class="fname dir" href="/files?path=${encodeURIComponent(entry.path)}">${label}</a>`
237
+ : `<a class="fname${entry.runnable ? ' exe' : ''}" href="/files/view?path=${encodeURIComponent(entry.path)}">${label}</a>`;
238
+ return `<div class="fitem" data-path="${escapeHtml(entry.path)}" data-name="${escapeHtml(entry.name)}" data-dir="${entry.dir ? 1 : 0}" data-size="${Number(entry.size) || 0}" data-time="${Number(entry.mtimeMs) || 0}">
239
+ <div class="fico">${icon}</div>
240
+ <div class="fsize">${escapeHtml(entry.sizeText ?? '')}</div>
241
+ <div class="ftime">${escapeHtml(entry.mtime ?? '')}</div>
242
+ ${nameHtml}
243
+ ${menuFor(entry)}
244
+ </div>`;
245
+ }).join('');
72
246
 
247
+ const navStyle = 'flex:0 0 auto;padding:4px 7px;border-radius:6px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:11px;line-height:1.4;white-space:nowrap'
73
248
  const parent = info.parent
74
- ? `<a class="btn ghost" href="/files?path=${encodeURIComponent(info.parent)}">上一级</a>`
75
- : ''
249
+ ? `<a class="btn ghost" href="/files?path=${encodeURIComponent(info.parent)}" style="${navStyle}">上一级</a>`
250
+ : `<span style="${navStyle};opacity:.3">上一级</span>`
76
251
 
77
252
  const chips = workspaces.length === 0 ? '' : `
78
253
  <div class="chips">
@@ -81,7 +256,17 @@ export function renderListing({ info, message, isError, home, dataDir, workspace
81
256
 
82
257
  const body = `
83
258
  <div class="bar">
84
- <h1>文件</h1>
259
+ <div style="display:flex;align-items:center;gap:8px;flex-wrap:nowrap;min-width:0">
260
+ <h1 style="margin:0;flex:0 0 auto;font-size:18px">文件</h1>
261
+ <div style="flex:1 1 auto;display:flex;align-items:center;justify-content:flex-end;gap:4px;flex-wrap:nowrap;overflow-x:auto;-webkit-overflow-scrolling:touch;scrollbar-width:none">
262
+ <a class="btn ghost" href="/" style="${navStyle}">DSH</a>
263
+ <a class="btn ghost" href="/term" style="${navStyle}">终端</a>
264
+ <a class="btn ghost" href="/git?path=${encodeURIComponent(info.path)}" style="${navStyle}">Git</a>
265
+ <a class="btn ghost" href="/files?path=${encodeURIComponent(home)}" style="${navStyle}">HOME</a>
266
+ <a class="btn ghost" href="/files?path=${encodeURIComponent(dataDir)}" style="${navStyle}">配置</a>
267
+ ${parent}
268
+ </div>
269
+ </div>
85
270
  <form method="get" action="/files" class="row">
86
271
  <input type="text" name="path" list="path-suggestions" value="${escapeHtml(info.path)}" placeholder="输入绝对路径">
87
272
  <datalist id="path-suggestions">
@@ -89,20 +274,32 @@ export function renderListing({ info, message, isError, home, dataDir, workspace
89
274
  </datalist>
90
275
  <button type="submit">前往</button>
91
276
  </form>
92
- <div class="row">
93
- <a class="btn ghost" href="/files?path=${encodeURIComponent(home)}" style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">主目录</a>
94
- <a class="btn ghost" href="/files?path=${encodeURIComponent(dataDir)}" style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">DSH 数据</a>
95
- ${parent}
96
- <a class="btn ghost" href="/git?path=${encodeURIComponent(info.path)}"
97
- style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">Git</a>
98
- </div>
99
277
  </div>
100
278
  ${chips}
101
279
  ${messageHtml(message, isError)}
102
280
  ${info.error ? `<div class="msg err">${escapeHtml(info.error)}</div>` : ''}
103
281
  ${info.truncated ? `<div class="msg">目录项过多,只显示前 2000 个</div>` : ''}
104
- <table>${rows || '<tr><td style="color:#8b93a1">空目录</td></tr>'}</table>
105
- <p class="crumbs">提示:点目录进入,点文件名查看内容;标 ⚙️ 的可直接运行(会在这台电脑上执行)。</p>`
282
+ <style>
283
+ .mm { position: relative; display: inline-block; }
284
+ .mm-btn { margin-left: 6px; padding: 6px 10px; border-radius: 8px; background: #2a2f3a; border: 1px solid #3a4351; color: #c7cdd8; font-size: 14px; line-height: 1; }
285
+ .mm-btn:active { background: #3a4351; }
286
+ .rowmenu { position: fixed; z-index: 9999; min-width: 0; padding: 4px 0; background: #20262f; border: 1px solid #3a4351; border-radius: 10px; box-shadow: 0 12px 32px rgba(0,0,0,.6); }
287
+ .rowmenu[hidden] { display: none; }
288
+ .rowmenu .mi { display: flex; align-items: center; gap: 9px; padding: 8px 14px 8px 10px; color: #e3e9f1; text-decoration: none; font-size: 13px; line-height: 1.35; white-space: nowrap; text-align: left; }
289
+ .rowmenu .ico { width: 18px; flex: 0 0 18px; text-align: center; opacity: .95; font-size: 13px; }
290
+ .rowmenu .mi.danger { color: #ff8f8f; }
291
+ .rowmenu .mi:first-child { border-radius: 8px 8px 0 0; }
292
+ .rowmenu .mi:last-child { border-radius: 0 0 8px 8px; }
293
+ .rowmenu .mi:active, .rowmenu .mi:hover { background: #333c48; }
294
+ </style>
295
+ <div class="sortbar">
296
+ <span class="sortlabel">排序</span>
297
+ <button type="button" class="sortbtn" data-sort="name">名称</button>
298
+ <button type="button" class="sortbtn" data-sort="size">大小</button>
299
+ <button type="button" class="sortbtn" data-sort="time">时间</button>
300
+ </div>
301
+ <div class="flist">${rows || '<div class="fempty">空目录</div>'}</div>
302
+ ${MENU_JS}\n <p class="crumbs">提示:点目录进入,点文件名查看内容;标 ⚙️ 的可直接运行(会在这台电脑上执行)。</p>`
106
303
 
107
304
  return layout({ title: info.path, body })
108
305
  }
@@ -202,6 +399,10 @@ export function renderGitPage({ repoPath, summary = null, args = '', result = nu
202
399
  style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">刷新</a>
203
400
  <a class="btn ghost" href="/files?path=${encodeURIComponent(repoPath)}"
204
401
  style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">看文件</a>
402
+ <a class="btn ghost" href="/term"
403
+ style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">终端</a>
404
+ <a class="btn ghost" href="/"
405
+ style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">DSH</a>
205
406
  </div>
206
407
  </div>
207
408
  ${message ? `<div class="msg">${escapeHtml(message)}</div>` : ''}
@@ -278,114 +479,3 @@ export function renderEditPage({ info, message = '', isError = false }) {
278
479
  }
279
480
 
280
481
  /** shell 页(手机端 Git Bash):命令 + 工作目录 + 输出 */
281
- export function renderShellPage({ cwd, command = '', result = null, history = [], shellName = '' }) {
282
- const failed = result !== null && (result.ok === false || (typeof result.code === 'number' && result.code !== 0))
283
- const body = `
284
- <div class="bar">
285
- <h1>终端</h1>
286
- <div class="path">${escapeHtml(cwd)}${shellName ? ' · ' + escapeHtml(shellName) : ''}</div>
287
- </div>
288
- <form method="post" action="/shell">
289
- <label style="display:block;margin-top:10px;font-size:13px;color:#9aa4b2">工作目录</label>
290
- <input type="text" name="cwd" list="shell-paths" value="${escapeHtml(cwd)}" style="width:100%;box-sizing:border-box">
291
- <label style="display:block;margin-top:10px;font-size:13px;color:#9aa4b2">命令</label>
292
- <input type="text" id="cmd" name="command" value="${escapeHtml(command)}" autocomplete="off" autocapitalize="off"
293
- autocorrect="off" spellcheck="false"
294
- placeholder='例如:git status / ls -la / npm test'
295
- style="width:100%;box-sizing:border-box">
296
- <div class="row">
297
- <button type="submit" style="flex:2">执行</button>
298
- <button type="button" id="tabkey" style="flex:1;background:#2a2f3a;color:#c7cdd8">Tab</button>
299
- <button type="submit" name="action" value="reset" formnovalidate style="flex:1;background:#3a2a2a;color:#f0b6bf">重置</button>
300
- </div>
301
- </form>
302
- <div id="cands" style="display:flex;flex-wrap:wrap;gap:6px;margin-top:8px"></div>
303
- <script id="shell-complete.js">
304
- (function () {
305
- var input = document.getElementById('cmd');
306
- var box = document.getElementById('cands');
307
- var cwdInput = document.querySelector('input[name=cwd]');
308
- if (!input || !box) return;
309
- var timer = null;
310
- function apply(value) {
311
- var text = input.value;
312
- var m = text.match(/(\\S+)$/);
313
- input.value = (m ? text.slice(0, m.index) : text) + value;
314
- input.focus();
315
- refresh();
316
- }
317
- function render(items) {
318
- box.innerHTML = '';
319
- items.slice(0, 12).forEach(function (item) {
320
- var btn = document.createElement('button');
321
- btn.type = 'button';
322
- btn.textContent = item.label;
323
- btn.style.cssText = 'padding:6px 10px;border-radius:999px;background:#1b2030;border:1px solid #2a3550;color:#9fc0ff;font-size:12px';
324
- btn.addEventListener('click', function () { apply(item.value); });
325
- box.appendChild(btn);
326
- });
327
- }
328
- function refresh() {
329
- var cwd = cwdInput ? cwdInput.value : '';
330
- var line = input.value;
331
- if (line.trim() === '') { box.innerHTML = ''; return; }
332
- fetch('/shell/complete?cwd=' + encodeURIComponent(cwd) + '&line=' + encodeURIComponent(line))
333
- .then(function (r) { return r.ok ? r.json() : []; })
334
- .then(function (items) { render(Array.isArray(items) ? items : []); })
335
- .catch(function () { /* 忽略补全失败 */ });
336
- }
337
- function completeNow() {
338
- var cwd = cwdInput ? cwdInput.value : '';
339
- var line = input.value;
340
- if (line.trim() === '') return;
341
- fetch('/shell/complete?cwd=' + encodeURIComponent(cwd) + '&line=' + encodeURIComponent(line))
342
- .then(function (r) { return r.ok ? r.json() : []; })
343
- .then(function (items) {
344
- if (!Array.isArray(items) || items.length === 0) {
345
- box.textContent = '无候选';
346
- setTimeout(function () { box.textContent = ''; }, 1200);
347
- return;
348
- }
349
- render(items);
350
- if (items.length === 1) { apply(items[0].value); return; }
351
- // 多个候选:把最后一段改成它们的公共前缀(像 bash 的 Tab 那样)
352
- var prefix = items[0].value;
353
- for (var i = 1; i < items.length; i += 1) {
354
- while (prefix.length > 0 && items[i].value.indexOf(prefix) !== 0) prefix = prefix.slice(0, -1);
355
- }
356
- if (prefix.length > 0) apply(prefix);
357
- })
358
- .catch(function () { /* 忽略 */ });
359
- }
360
- var tabBtn = document.getElementById('tabkey');
361
- if (tabBtn) tabBtn.addEventListener('click', completeNow);
362
-
363
- input.addEventListener('input', function () {
364
- clearTimeout(timer);
365
- timer = setTimeout(refresh, 250);
366
- });
367
- input.addEventListener('focus', refresh);
368
- })();
369
- </script>
370
- ${history.length === 0 ? '' : `<div style="display:flex;flex-wrap:wrap;gap:6px;margin-top:10px">
371
- ${history.slice(0, 8).map((cmd) => `<form method="post" action="/shell" style="display:inline">
372
- <input type="hidden" name="cwd" value="${escapeHtml(cwd)}">
373
- <input type="hidden" name="command" value="${escapeHtml(cmd)}">
374
- <button type="submit" style="padding:6px 10px;border-radius:999px;background:#1b2030;border:1px solid #2a3550;color:#9fc0ff;font-size:12px">${escapeHtml(cmd)}</button>
375
- </form>`).join('')}
376
- </div>`}
377
- ${result === null ? '' : `
378
- <div class="msg${failed ? ' err' : ''}" style="margin-top:14px">
379
- ${failed ? escapeHtml(result.error ?? (result.timeout ? '超时(30 秒),已终止' : `退出码 ${result.code}`)) : `退出码 ${result.code}`}
380
- </div>
381
- <pre>${escapeHtml(result.output ?? '')}</pre>
382
- `}
383
- <div class="row">
384
- <a class="btn ghost" href="/git?path=${encodeURIComponent(cwd)}"
385
- style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">Git 页</a>
386
- <a class="btn ghost" href="/files?path=${encodeURIComponent(cwd)}"
387
- style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">文件</a>
388
- </div>
389
- <p class="crumbs">命令直接在你电脑上执行,输出截断到 64KB,30 秒超时。可以用 cd 切换工作目录。</p>`
390
- return layout({ title: '终端', body })
391
- }
package/lib/files.mjs CHANGED
@@ -30,7 +30,8 @@ const fmtSize = (bytes) => {
30
30
  if (bytes < 1024) return `${bytes} B`
31
31
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
32
32
  if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(1)} MB`
33
- return `${(bytes / 1024 / 1024 / 1024).toFixed(1)} GB`
33
+ if (bytes < 1024 ** 4) return `${(bytes / 1024 ** 3).toFixed(1)} GB`
34
+ return `${(bytes / 1024 ** 4).toFixed(1)} TB`
34
35
  }
35
36
 
36
37
  const fmtTime = (date) => {
@@ -374,7 +375,7 @@ export function runFile(absPath, args = '', { timeoutMs = 15_000, cwd } = {}) {
374
375
  let command = target;
375
376
  let argv = argList;
376
377
  if (kind === 'ps1') {
377
- const wrapper = path.join(os.tmpdir(), 'dsh-mobile-run-ps1.ps1');
378
+ const wrapper = path.join(os.tmpdir(), 'dsh-homing-run-ps1.ps1');
378
379
  fs.writeFileSync(wrapper, [
379
380
  '[Console]::OutputEncoding = [System.Text.Encoding]::UTF8',
380
381
  '$target = $args[0]',
@@ -462,3 +463,89 @@ export function runFile(absPath, args = '', { timeoutMs = 15_000, cwd } = {}) {
462
463
  }
463
464
 
464
465
  export { fmtSize, fmtTime }
466
+
467
+ /**
468
+ * 重命名同一目录下的文件/目录。
469
+ * 只接受纯名字(不含路径分隔符),目标已存在则拒绝,绝不跨目录移动。
470
+ */
471
+ export function renameEntry(absPath, newName) {
472
+ const bad = /[\\/:*?"<>|\u0000-\u001f]/u;
473
+ const name = String(newName ?? '').trim();
474
+ if (!name) return { ok: false, error: '名字不能为空' };
475
+ if (name === '.' || name === '..') return { ok: false, error: '非法名字' };
476
+ if (bad.test(name)) return { ok: false, error: '名字里不能包含 \\ / : * ? " < > |' };
477
+ const dir = path.dirname(absPath);
478
+ const target = path.join(dir, name);
479
+ if (path.resolve(target) === path.resolve(absPath)) return { ok: true, path: absPath };
480
+ if (fs.existsSync(target)) return { ok: false, error: '同名文件已存在' };
481
+ fs.renameSync(absPath, target);
482
+ return { ok: true, path: target };
483
+ }
484
+
485
+ /** 删除文件或目录(目录递归)。调用方必须已做二次确认。 */
486
+ export function deleteEntry(absPath) {
487
+ const target = path.resolve(String(absPath ?? ''));
488
+ if (!target || !fs.existsSync(target)) return { ok: false, error: '不存在' };
489
+ const stat = fs.statSync(target);
490
+ if (stat.isDirectory()) fs.rmSync(target, { recursive: true, force: true });
491
+ else fs.unlinkSync(target);
492
+ return { ok: true };
493
+ }
494
+
495
+ /** 把 src 复制进 destDir;同名则自动加 " - 副本"/" - 副本2" 后缀,绝不覆盖。 */
496
+ export function copyInto(srcPath, destDir) {
497
+ const src = path.resolve(String(srcPath ?? ''));
498
+ const dir = path.resolve(String(destDir ?? ''));
499
+ if (!fs.existsSync(src)) return { ok: false, error: '源不存在' };
500
+ if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return { ok: false, error: '目标不是目录' };
501
+ const base = path.basename(src);
502
+ const ext = path.extname(base);
503
+ const stem = ext ? base.slice(0, -ext.length) : base;
504
+ let target = path.join(dir, base);
505
+ let n = 1;
506
+ while (fs.existsSync(target)) {
507
+ const name = ext ? `${stem} - 副本${n > 1 ? n : ''}${ext}` : `${stem} - 副本${n > 1 ? n : ''}`;
508
+ target = path.join(dir, name);
509
+ n += 1;
510
+ if (n > 100) return { ok: false, error: '同名文件过多' };
511
+ }
512
+ fs.cpSync(src, target, { recursive: true, errorOnExist: false, force: false });
513
+ return { ok: true, path: target };
514
+ }
515
+
516
+ const BAD_NAME = /[\\/:*?"<>|\u0000-\u001f]/u;
517
+
518
+ /** 在 dir 下新建文件或目录;名字非法或已存在则拒绝。 */
519
+ export function createEntry(dirPath, name, isDir) {
520
+ const dir = path.resolve(String(dirPath ?? ''));
521
+ const clean = String(name ?? '').trim();
522
+ if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return { ok: false, error: '目标不是目录' };
523
+ if (!clean) return { ok: false, error: '名字不能为空' };
524
+ if (clean === '.' || clean === '..' || BAD_NAME.test(clean)) return { ok: false, error: '名字不合法' };
525
+ const target = path.join(dir, clean);
526
+ if (fs.existsSync(target)) return { ok: false, error: '同名文件已存在' };
527
+ if (isDir) fs.mkdirSync(target);
528
+ else fs.writeFileSync(target, '');
529
+ return { ok: true, path: target };
530
+ }
531
+
532
+ /** 把 src 移动进 destDir;同名自动改名,绝不覆盖。 */
533
+ export function moveInto(srcPath, destDir) {
534
+ const src = path.resolve(String(srcPath ?? ''));
535
+ const dir = path.resolve(String(destDir ?? ''));
536
+ if (!fs.existsSync(src)) return { ok: false, error: '源不存在' };
537
+ if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return { ok: false, error: '目标不是目录' };
538
+ if (path.dirname(src) === dir) return { ok: false, error: '已经在目标目录里' };
539
+ const base = path.basename(src);
540
+ const ext = path.extname(base);
541
+ const stem = ext ? base.slice(0, -ext.length) : base;
542
+ let target = path.join(dir, base);
543
+ let n = 1;
544
+ while (fs.existsSync(target)) {
545
+ target = path.join(dir, ext ? `${stem} - 副本${n > 1 ? n : ''}${ext}` : `${stem} - 副本${n > 1 ? n : ''}`);
546
+ n += 1;
547
+ if (n > 100) return { ok: false, error: '同名文件过多' };
548
+ }
549
+ fs.renameSync(src, target);
550
+ return { ok: true, path: target };
551
+ }
package/lib/limits.mjs CHANGED
@@ -7,7 +7,7 @@
7
7
  * 把主人锁在门外。
8
8
  *
9
9
  * 因此分两层:
10
- * 1) 按「设备桶」计数(有 dsh-device 就按设备 id,没有就归到 unpaired),
10
+ * 1) 按「设备桶」计数(有 dsh-homing-device 就按设备 id,没有就归到 unpaired),
11
11
  * 桶内失败到阈值后只锁这一个桶 —— 攻击者锁不到别人的桶;
12
12
  * 2) 全局失败计数只做**延迟**(封顶 maxDelayMs),不做阻断 ——
13
13
  * 这样跨桶跳着猜也快不起来,但任何人都不至于被永久拒之门外。
package/lib/pty.mjs CHANGED
@@ -45,7 +45,7 @@ const KEYGEN_EXE = 'C:/Windows/System32/OpenSSH/ssh-keygen.exe'
45
45
 
46
46
  /**
47
47
  * DSH_HOME:优先用插件传进来的值。
48
- * 不能从代码位置推(仓库里是 D:\...\plugin\lib,部署后是 <DSH_HOME>\plugins\dsh-mobile\lib),
48
+ * 不能从代码位置推(仓库里是 D:\...\plugin\lib,部署后是 <DSH_HOME>\plugins\dsh-homing\lib),
49
49
  * 所以这里只在调用方没传时用环境变量兜底。
50
50
  */
51
51
  export const resolveDshHome = () => process.env.DSH_HOME ?? path.resolve(here, '..', '..', '..')
@@ -74,13 +74,20 @@ const waitFor = async (predicate, timeoutMs = 8000) => {
74
74
  return false
75
75
  }
76
76
 
77
- export const terminalDir = (dshHome = resolveDshHome()) => path.join(dshHome, 'integrations', 'dsh-mobile', 'terminal')
77
+ export const terminalDir = (dshHome = resolveDshHome()) => {
78
+ const dir = path.join(dshHome, 'integrations', 'dsh-homing', 'terminal')
79
+ // 老版本把 sshd 的密钥/配置放在 dsh-homing/terminal 下;新目录没有就沿用旧的,
80
+ // 避免改名后终端连不上(密钥变了 sshd 得重建,会话会断一下)。
81
+ const legacy = path.join(dshHome, 'integrations', 'dsh-homing', 'terminal')
82
+ if (!fs.existsSync(dir) && fs.existsSync(legacy)) return legacy
83
+ return dir
84
+ }
78
85
 
79
86
  export const ensureKeyPair = (dir) => {
80
87
  const key = path.join(dir, 'id_ed25519')
81
88
  if (fs.existsSync(key) && fs.existsSync(key + '.pub')) return key
82
89
  fs.mkdirSync(dir, { recursive: true })
83
- const result = spawnSync(KEYGEN_EXE, ['-t', 'ed25519', '-N', '', '-C', 'dsh-mobile-terminal', '-f', key], {
90
+ const result = spawnSync(KEYGEN_EXE, ['-t', 'ed25519', '-N', '', '-C', 'dsh-homing-terminal', '-f', key], {
84
91
  encoding: 'utf8',
85
92
  timeout: 20000,
86
93
  windowsHide: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andy.ian/dsh-homing",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "type": "module",
5
5
  "description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
6
6
  "keywords": [
@@ -29,7 +29,8 @@
29
29
  "dsh-homing-auth": "./bin/auth.mjs",
30
30
  "dsh-homing-backup": "./bin/backup.mjs",
31
31
  "dsh-homing-entry": "./bin/entry-link.mjs",
32
- "dsh-homing-watchdog": "./bin/watchdog.mjs"
32
+ "dsh-homing-watchdog": "./bin/watchdog.mjs",
33
+ "dsh-homing-supervisor": "./bin/supervisor.mjs"
33
34
  },
34
35
  "exports": {
35
36
  ".": "./index.js",
@@ -53,8 +54,7 @@
53
54
  "icon.svg",
54
55
  "README.md",
55
56
  "LICENSE",
56
- "lib/vendor/**",
57
- "ops/**"
57
+ "lib/vendor/**"
58
58
  ],
59
59
  "publishConfig": {
60
60
  "access": "public",