ework-web 0.6.0 → 0.7.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ework-web",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "description": "ework-web — standalone multi-project issue tracker. Local SQLite-backed, no external API dependency. Bun + TypeScript + SSR HTML.",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -88,6 +88,7 @@ import {
88
88
  import { classifyActor, type CommentView } from "./render/components";
89
89
  import { buildWebhooksPage } from "./views/webhooks";
90
90
  import { buildWebhookDeliveriesPage } from "./views/webhookDeliveries";
91
+ import { browseRemoteFile, proxyFileSince, RemoteFileError } from "./remote-file";
91
92
  import { buildProjectMembersPage } from "./views/projectMembers";
92
93
  import { buildProjectUpstreamsPage, trySetUpstreamUrls } from "./views/projectUpstreams";
93
94
  import { buildProjectModelPage } from "./views/projectModel";
@@ -599,6 +600,18 @@ async function handle(req: Request, url: URL, ip: string, ctx: { authed: boolean
599
600
 
600
601
  if (url.pathname === "/file") {
601
602
  const rawPath = url.searchParams.get("path") ?? "";
603
+ const daemonParam = url.searchParams.get("daemon");
604
+ if (daemonParam && !isLocalhost(daemonParam)) {
605
+ try {
606
+ const mode = url.searchParams.get("mode") ?? "tail";
607
+ const order = url.searchParams.get("order") ?? "desc";
608
+ const { html: body } = await browseRemoteFile(daemonParam, rawPath, mode, order, ctx.user?.login);
609
+ return html(body);
610
+ } catch (e) {
611
+ const status = e instanceof RemoteFileError ? e.status : 500;
612
+ return html(errorPage(status === 404 ? "文件不存在" : "远程访问失败", errMsg(e)), status);
613
+ }
614
+ }
602
615
  const mode = url.searchParams.get("mode") ?? undefined;
603
616
  const order = url.searchParams.get("order") ?? undefined;
604
617
  try {
@@ -628,6 +641,15 @@ async function handle(req: Request, url: URL, ip: string, ctx: { authed: boolean
628
641
  const rawPath = url.searchParams.get("path") ?? "";
629
642
  const after = Number(url.searchParams.get("after") ?? "0");
630
643
  if (!Number.isFinite(after) || after < 0) return json({ error: "bad after" }, 400);
644
+ const daemonParam = url.searchParams.get("daemon");
645
+ if (daemonParam && !isLocalhost(daemonParam)) {
646
+ try {
647
+ return json(await proxyFileSince(daemonParam, rawPath, after));
648
+ } catch (e) {
649
+ const status = e instanceof RemoteFileError ? e.status : 500;
650
+ return json({ error: errMsg(e) }, status);
651
+ }
652
+ }
631
653
  try {
632
654
  const delta = readFileSince(cfg, rawPath, after);
633
655
  return json(delta);
@@ -0,0 +1,206 @@
1
+ import { THEME_CSS, escapeHtml, tabNavHTML } from "./render/layout";
2
+
3
+ export class RemoteFileError extends Error {
4
+ status: number;
5
+ constructor(message: string, status: number) {
6
+ super(message);
7
+ this.name = "RemoteFileError";
8
+ this.status = status;
9
+ }
10
+ }
11
+
12
+ interface DirEntry {
13
+ name: string;
14
+ isDir: boolean;
15
+ size: number;
16
+ mtime: number;
17
+ }
18
+
19
+ interface DirListing {
20
+ path: string;
21
+ entries: DirEntry[];
22
+ }
23
+
24
+ interface FileContent {
25
+ path: string;
26
+ size: number;
27
+ rows: { n: number; t: string }[];
28
+ mode: string;
29
+ byteCapped: boolean;
30
+ note: string;
31
+ }
32
+
33
+ function fmtSize(n: number): string {
34
+ if (n < 1024) return `${n}B`;
35
+ if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)}KB`;
36
+ return `${(n / (1024 * 1024)).toFixed(1)}MB`;
37
+ }
38
+
39
+ function fmtMtime(ms: number): string {
40
+ if (!ms) return "";
41
+ const diff = Date.now() - ms;
42
+ if (diff < 60000) return "刚刚";
43
+ if (diff < 3600000) return `${Math.floor(diff / 60000)}分前`;
44
+ if (diff < 86400000) return `${Math.floor(diff / 3600000)}时前`;
45
+ return new Date(ms).toISOString().slice(5, 16);
46
+ }
47
+
48
+ async function fetchDaemon(
49
+ endpoint: string,
50
+ apiPath: string,
51
+ params: Record<string, string>
52
+ ): Promise<Response> {
53
+ const url = new URL(`${endpoint}${apiPath}`);
54
+ for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
55
+ const resp = await fetch(url.toString(), { signal: AbortSignal.timeout(10_000) });
56
+ return resp;
57
+ }
58
+
59
+ export async function browseRemoteFile(
60
+ endpoint: string,
61
+ rawPath: string,
62
+ mode: string,
63
+ order: string,
64
+ viewerLogin?: string
65
+ ): Promise<{ html: string }> {
66
+ const daemonLabel = escapeHtml(endpoint);
67
+
68
+ const listResp = await fetchDaemon(endpoint, "/api/files/list", { path: rawPath });
69
+ if (listResp.ok) {
70
+ const data = (await listResp.json()) as DirListing;
71
+ return { html: renderDirListing(data, endpoint, daemonLabel, viewerLogin) };
72
+ }
73
+ if (listResp.status !== 400) {
74
+ const err = await listResp.json().catch(() => ({ error: "unknown" })) as { error?: string };
75
+ throw new RemoteFileError(err.error ?? `daemon returned ${listResp.status}`, listResp.status);
76
+ }
77
+
78
+ const readResp = await fetchDaemon(endpoint, "/api/files/read", {
79
+ path: rawPath,
80
+ mode: mode === "head" ? "head" : "tail",
81
+ order: order === "asc" ? "asc" : "desc",
82
+ });
83
+ if (!readResp.ok) {
84
+ const err = await readResp.json().catch(() => ({ error: "unknown" })) as { error?: string };
85
+ throw new RemoteFileError(err.error ?? `daemon returned ${readResp.status}`, readResp.status);
86
+ }
87
+ const data = (await readResp.json()) as FileContent;
88
+ return { html: renderFileContent(data, endpoint, daemonLabel, viewerLogin) };
89
+ }
90
+
91
+ function renderDirListing(
92
+ data: DirListing,
93
+ endpoint: string,
94
+ daemonLabel: string,
95
+ viewerLogin?: string
96
+ ): string {
97
+ const enc = encodeURIComponent;
98
+ const daemonParam = `&daemon=${enc(endpoint)}`;
99
+ const parentPath = data.path.split("/").slice(0, -1).join("/") || "/";
100
+ const rows = data.entries.map((e) => {
101
+ const childPath = `${data.path.replace(/\/$/, "")}/${e.name}`;
102
+ const icon = e.isDir ? "📁" : "📄";
103
+ const size = e.isDir ? "—" : fmtSize(e.size);
104
+ return `<tr>
105
+ <td><a href="/file?path=${enc(childPath)}${daemonParam}">${icon} ${escapeHtml(e.name)}</a></td>
106
+ <td class="ms">${size}</td>
107
+ <td class="ts">${fmtMtime(e.mtime)}</td>
108
+ </tr>`;
109
+ }).join("") || `<tr><td colspan="3" class="empty">空目录</td></tr>`;
110
+
111
+ const user = viewerLogin ? { login: viewerLogin, is_admin: 0 } : undefined;
112
+ return `<!doctype html>
113
+ <html lang="zh"><head><meta charset="utf-8">
114
+ <meta name="viewport" content="width=device-width,initial-scale=1">
115
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
116
+ <title>ework-web · ${escapeHtml(data.path)}</title>
117
+ <style>${THEME_CSS}
118
+ .nav{display:flex;align-items:center;gap:.5rem;padding:.55rem 1rem;background:var(--header-bg);color:var(--header-text);font-size:13px}
119
+ .nav a{color:var(--header-text);opacity:.95}
120
+ .wrap{max-width:900px;margin:0 auto;padding:1rem}
121
+ h1{font-size:15px;margin:0 0 .3rem;font-family:ui-monospace,monospace;word-break:break-all}
122
+ .remote-badge{display:inline-block;background:#1f6feb33;color:#58a6ff;padding:2px 8px;border-radius:10px;font-size:11px;font-weight:600;margin-left:.5rem}
123
+ .dtable{width:100%;border-collapse:collapse;font-size:13px;margin-top:.6rem}
124
+ .dtable th,.dtable td{border:1px solid var(--border);padding:.3rem .5rem;text-align:left}
125
+ .dtable th{background:var(--bg);color:var(--text-muted);font-weight:600}
126
+ .dtable a{color:var(--accent);text-decoration:none}
127
+ .dtable a:hover{text-decoration:underline}
128
+ .dtable .ms,.dtable .ts{color:var(--text-muted);white-space:nowrap;font-size:12px}
129
+ .empty{color:var(--text-muted);text-align:center;padding:.6rem}
130
+ .parent-link{font-size:13px;margin-bottom:.4rem}
131
+ .parent-link a{color:var(--text-muted)}
132
+ .tabs{display:flex;gap:.3rem;padding:.5rem 1rem;border-bottom:1px solid var(--border);font-size:13px}
133
+ .tab{padding:.3rem .7rem;border-radius:6px 6px 0 0;text-decoration:none;color:var(--text-muted)}
134
+ .tab.active{background:var(--accent);color:#fff}
135
+ </style></head><body>
136
+ <header class="nav"><a href="/" style="color:var(--header-text)">🏠 ework-web</a></header>
137
+ ${tabNavHTML("sessions", user)}
138
+ <main class="wrap">
139
+ <div class="parent-link">📁 <a href="/file?path=${enc(parentPath)}${daemonParam}">..</a></div>
140
+ <h1>${escapeHtml(data.path)}<span class="remote-badge">远程 ${daemonLabel}</span></h1>
141
+ <table class="dtable"><thead><tr>
142
+ <th>名称</th><th>大小</th><th>修改时间</th>
143
+ </tr></thead><tbody>${rows}</tbody></table>
144
+ </main></body></html>`;
145
+ }
146
+
147
+ function renderFileContent(
148
+ data: FileContent,
149
+ endpoint: string,
150
+ daemonLabel: string,
151
+ viewerLogin?: string
152
+ ): string {
153
+ const enc = encodeURIComponent;
154
+ const daemonParam = `&daemon=${enc(endpoint)}`;
155
+ const user = viewerLogin ? { login: viewerLogin, is_admin: 0 } : undefined;
156
+ const lines = data.rows.map((r) =>
157
+ `<div class="l"><span class="ln">${r.n}</span><span class="lt">${escapeHtml(r.t)}</span></div>`
158
+ ).join("");
159
+
160
+ return `<!doctype html>
161
+ <html lang="zh"><head><meta charset="utf-8">
162
+ <meta name="viewport" content="width=device-width,initial-scale=1">
163
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
164
+ <title>ework-web · ${escapeHtml(data.path)}</title>
165
+ <style>${THEME_CSS}
166
+ .nav{display:flex;align-items:center;gap:.5rem;padding:.55rem 1rem;background:var(--header-bg);color:var(--header-text);font-size:13px}
167
+ .nav a{color:var(--header-text);opacity:.95}
168
+ .wrap{max-width:1000px;margin:0 auto;padding:1rem}
169
+ h1{font-size:14px;margin:0 0 .3rem;font-family:ui-monospace,monospace;word-break:break-all}
170
+ .remote-badge{display:inline-block;background:#1f6feb33;color:#58a6ff;padding:2px 8px;border-radius:10px;font-size:11px;font-weight:600;margin-left:.5rem}
171
+ .note{color:var(--text-muted);font-size:12px;margin:0 0 .5rem}
172
+ .parent-link{font-size:13px;margin-bottom:.4rem}
173
+ .parent-link a{color:var(--text-muted)}
174
+ .fview{background:var(--bg-elev);border:1px solid var(--border);border-radius:6px;overflow:auto;max-height:80vh;font-size:12.5px;font-family:ui-monospace,monospace}
175
+ .l{display:flex}
176
+ .ln{color:var(--text-muted);min-width:3.5rem;text-align:right;padding:0 .5rem;user-select:none;border-right:1px solid var(--border)}
177
+ .lt{padding:0 .5rem;white-space:pre-wrap;word-break:break-all;flex:1}
178
+ .tabs{display:flex;gap:.3rem;padding:.5rem 1rem;border-bottom:1px solid var(--border);font-size:13px}
179
+ .tab{padding:.3rem .7rem;border-radius:6px 6px 0 0;text-decoration:none;color:var(--text-muted)}
180
+ .tab.active{background:var(--accent);color:#fff}
181
+ </style></head><body>
182
+ <header class="nav"><a href="/" style="color:var(--header-text)">🏠 ework-web</a></header>
183
+ ${tabNavHTML("sessions", user)}
184
+ <main class="wrap">
185
+ <div class="parent-link">📁 <a href="/file?path=${enc(data.path.split("/").slice(0, -1).join("/") || "/")}${daemonParam}">..</a></div>
186
+ <h1>${escapeHtml(data.path)}<span class="remote-badge">远程 ${daemonLabel}</span></h1>
187
+ ${data.note ? `<p class="note">${escapeHtml(data.note)}</p>` : ""}
188
+ <div class="fview">${lines}</div>
189
+ </main></body></html>`;
190
+ }
191
+
192
+ export async function proxyFileSince(
193
+ endpoint: string,
194
+ rawPath: string,
195
+ after: number
196
+ ): Promise<{ rows: { n: number; t: string }[]; size: number; rotated: boolean; capped: boolean }> {
197
+ const resp = await fetchDaemon(endpoint, "/api/files/since", {
198
+ path: rawPath,
199
+ after: String(after),
200
+ });
201
+ if (!resp.ok) {
202
+ const err = await resp.json().catch(() => ({ error: "unknown" })) as { error?: string };
203
+ throw new RemoteFileError(err.error ?? `daemon returned ${resp.status}`, resp.status);
204
+ }
205
+ return await resp.json();
206
+ }