-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
421 lines (377 loc) · 13.6 KB
/
Copy pathserver.mjs
File metadata and controls
421 lines (377 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { userInfo } from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import {
closeDatabase,
createLabel,
deleteArticle,
exportData,
getLlmSettings,
getNetworkSettings,
importArticles,
listArticles,
listLabels,
updateArticle,
updateLabel,
updateLlmSettings,
updateNetworkSettings,
} from "./lib/database.mjs";
import {
getApiKeyInfo,
getSubscriptionKeyInfo,
setApiKey,
setSubscriptionKey,
} from "./lib/api-key-store.mjs";
import {
configureMetadataFetch,
fetchLinkMetadata,
fetchMetadataBatch,
normalizeUrl,
parseLinks,
} from "./lib/link-metadata.mjs";
import {
queueArticleClassifications,
queueImportedArticles,
queueUnreadArticles,
testLlmConnection,
} from "./lib/llm-labeler.mjs";
const root = path.dirname(fileURLToPath(import.meta.url));
const publicDir = path.join(root, "public");
const port = Number.parseInt(process.env.PORT || "8999", 10);
const mimeTypes = {
".css": "text/css; charset=utf-8",
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".svg": "image/svg+xml",
};
const validStatuses = new Set(["all", "unread", "reading", "completed"]);
function systemUsername() {
try {
return userInfo().username || process.env.USERNAME || process.env.USER || "";
} catch {
return process.env.USERNAME || process.env.USER || "";
}
}
function sendJson(response, statusCode, value, extraHeaders = {}) {
response.writeHead(statusCode, {
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "no-store",
"X-Content-Type-Options": "nosniff",
...extraHeaders,
});
response.end(JSON.stringify(value));
}
async function readJsonBody(request) {
const chunks = [];
let size = 0;
for await (const chunk of request) {
size += chunk.length;
if (size > 1024 * 1024) throw new Error("请求内容过大");
chunks.push(chunk);
}
if (!chunks.length) return {};
try {
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
} catch {
throw new Error("JSON 格式无效");
}
}
function routeId(pathname) {
const match = pathname.match(/^\/api\/articles\/(\d+)$/u);
return match ? Number(match[1]) : null;
}
function routeArticleAction(pathname, action) {
const match = pathname.match(new RegExp(`^/api/articles/(\\d+)/${action}$`, "u"));
return match ? Number(match[1]) : null;
}
function routeLabelId(pathname) {
const match = pathname.match(/^\/api\/labels\/(\d+)$/u);
return match ? Number(match[1]) : null;
}
async function handleApi(request, response, url) {
if (request.method === "GET" && url.pathname === "/api/articles") {
const status = validStatuses.has(url.searchParams.get("status")) ? url.searchParams.get("status") : "all";
const parsedLabelId = Number(url.searchParams.get("label"));
const labelId = Number.isInteger(parsedLabelId) && parsedLabelId > 0 ? parsedLabelId : null;
sendJson(response, 200, listArticles({ search: url.searchParams.get("search") || "", status, labelId }));
return true;
}
if (request.method === "GET" && url.pathname === "/api/labels") {
sendJson(response, 200, {
labels: listLabels({ includeDisabled: url.searchParams.get("includeDisabled") === "true" }),
});
return true;
}
if (request.method === "POST" && url.pathname === "/api/labels") {
const body = await readJsonBody(request);
sendJson(response, 201, { label: createLabel(body) });
return true;
}
const labelId = routeLabelId(url.pathname);
if (labelId && request.method === "PATCH") {
const body = await readJsonBody(request);
const label = updateLabel(labelId, body);
if (!label) sendJson(response, 404, { error: "标签不存在" });
else sendJson(response, 200, { label });
return true;
}
if (labelId && request.method === "DELETE") {
const label = updateLabel(labelId, { enabled: false });
if (!label) sendJson(response, 404, { error: "标签不存在" });
else sendJson(response, 200, { label });
return true;
}
if (request.method === "GET" && url.pathname === "/api/settings/llm") {
sendJson(response, 200, {
settings: getLlmSettings(),
apiKey: await getApiKeyInfo(),
subscriptionKey: await getSubscriptionKeyInfo(),
systemUsername: systemUsername(),
});
return true;
}
if (request.method === "PATCH" && url.pathname === "/api/settings/llm") {
const body = await readJsonBody(request);
const settings = updateLlmSettings(body);
if (typeof body.apiKey === "string" && body.apiKey.trim()) await setApiKey(body.apiKey);
if (body.clearApiKey === true) await setApiKey("");
if (typeof body.subscriptionKey === "string" && body.subscriptionKey.trim()) {
await setSubscriptionKey(body.subscriptionKey);
}
if (body.clearSubscriptionKey === true) await setSubscriptionKey("");
sendJson(response, 200, {
settings,
apiKey: await getApiKeyInfo(),
subscriptionKey: await getSubscriptionKeyInfo(),
});
return true;
}
if (request.method === "POST" && url.pathname === "/api/settings/llm/test") {
const result = await testLlmConnection();
sendJson(response, 200, result);
return true;
}
if (request.method === "GET" && url.pathname === "/api/settings/network") {
sendJson(response, 200, {
settings: getNetworkSettings(),
capabilities: {
socks5: process.env.LEARNING_TRACKER_RUNTIME === "desktop",
},
});
return true;
}
if (request.method === "PATCH" && url.pathname === "/api/settings/network") {
const body = await readJsonBody(request);
sendJson(response, 200, {
settings: updateNetworkSettings(body),
capabilities: {
socks5: process.env.LEARNING_TRACKER_RUNTIME === "desktop",
},
});
return true;
}
if (request.method === "POST" && url.pathname === "/api/settings/network/test") {
const body = await readJsonBody(request);
const testUrl = normalizeUrl(String(body.url || "https://example.com/"));
const result = await fetchLinkMetadata({ url: testUrl, suppliedTitle: "" });
const ok = result.fetched || result.errorType === "title_missing";
sendJson(response, 200, { ok, result });
return true;
}
if (request.method === "POST" && url.pathname === "/api/classification/unread") {
const queued = queueUnreadArticles();
sendJson(response, 202, { queued });
return true;
}
if (request.method === "POST" && url.pathname === "/api/links/preview") {
const body = await readJsonBody(request);
const links = parseLinks(body.text || "");
if (!links.length) {
sendJson(response, 400, { error: "没有识别到有效的 HTTP 或 HTTPS 链接" });
return true;
}
const items = await fetchMetadataBatch(links);
sendJson(response, 200, { items, truncated: links.length >= 50 });
return true;
}
if (request.method === "POST" && url.pathname === "/api/articles/import") {
const body = await readJsonBody(request);
if (!Array.isArray(body.items) || !body.items.length || body.items.length > 50) {
sendJson(response, 400, { error: "请提交 1 至 50 篇文章" });
return true;
}
const seen = new Set();
const items = [];
let repeatedInBatch = 0;
for (const item of body.items) {
try {
const urlValue = normalizeUrl(String(item.url || ""));
if (seen.has(urlValue)) {
repeatedInBatch += 1;
continue;
}
seen.add(urlValue);
const title = String(item.title || "").trim().slice(0, 500);
if (!title) throw new Error("文章标题不能为空");
items.push({
title,
url: urlValue,
domain: new URL(urlValue).hostname.replace(/^www\./u, ""),
description: String(item.description || "").trim().slice(0, 2_000),
labelIds: Object.hasOwn(item, "labelIds") ? item.labelIds : [],
});
} catch (error) {
sendJson(response, 400, { error: error instanceof Error ? error.message : "文章数据无效" });
return true;
}
}
const result = importArticles(items);
result.duplicates += repeatedInBatch;
result.classificationQueued = queueImportedArticles(
result.inserted.filter((article) => article.labels.length === 0).map((article) => article.id),
);
sendJson(response, 201, result);
return true;
}
const classifyArticleId = routeArticleAction(url.pathname, "classify");
if (classifyArticleId && request.method === "POST") {
const queued = queueArticleClassifications([classifyArticleId]);
if (!queued) sendJson(response, 409, { error: "文章正在分类,或文章不存在" });
else sendJson(response, 202, { queued });
return true;
}
const articleId = routeId(url.pathname);
if (articleId && request.method === "PATCH") {
const body = await readJsonBody(request);
const changes = {};
if (Object.hasOwn(body, "title")) changes.title = body.title;
if (Object.hasOwn(body, "status")) changes.status = body.status;
if (Object.hasOwn(body, "labelIds")) changes.labelIds = body.labelIds;
if (Object.hasOwn(body, "url")) {
const articleUrl = normalizeUrl(String(body.url || ""));
changes.url = articleUrl;
changes.domain = new URL(articleUrl).hostname.replace(/^www\./u, "");
}
const article = updateArticle(articleId, changes);
if (!article) sendJson(response, 404, { error: "文章不存在" });
else sendJson(response, 200, { article });
return true;
}
if (articleId && request.method === "DELETE") {
if (!deleteArticle(articleId)) sendJson(response, 404, { error: "文章不存在" });
else sendJson(response, 200, { ok: true });
return true;
}
if (request.method === "GET" && url.pathname === "/api/export") {
const timestamp = new Date().toISOString().replace(/[:.]/gu, "-");
sendJson(
response,
200,
exportData(),
{ "Content-Disposition": `attachment; filename="reading-tracker-${timestamp}.json"` },
);
return true;
}
return false;
}
async function serveStatic(urlPath, response) {
const requestedPath = urlPath === "/" ? "/index.html" : urlPath;
const filePath = path.resolve(publicDir, `.${requestedPath}`);
if (!filePath.startsWith(`${publicDir}${path.sep}`)) {
return false;
}
try {
const content = await readFile(filePath);
response.writeHead(200, {
"Content-Type": mimeTypes[path.extname(filePath)] || "application/octet-stream",
"Cache-Control": "no-cache",
});
response.end(content);
return true;
} catch {
return false;
}
}
export const server = createServer(async (request, response) => {
const url = new URL(request.url || "/", `http://${request.headers.host || "localhost"}`);
response.setHeader("Referrer-Policy", "no-referrer");
response.setHeader("X-Frame-Options", "DENY");
try {
if (url.pathname.startsWith("/api/") && (await handleApi(request, response, url))) {
return;
}
if (request.method === "GET" && url.pathname === "/healthz") {
sendJson(response, 200, { ok: true });
return;
}
if (request.method === "GET" && (await serveStatic(url.pathname, response))) {
return;
}
sendJson(response, 404, { error: "Not found" });
} catch (error) {
const message = error instanceof Error ? error.message : "服务器错误";
const statusCode = error?.code === "DUPLICATE_URL"
? 409
: /不能为空|无效|过大|HTTP|最多|至少|请选择|应在|已存在/u.test(message)
? 400
: 500;
sendJson(response, statusCode, {
error: message,
});
}
});
function shutdown() {
server.close(() => {
closeDatabase();
process.exit(0);
});
}
const isDirectRun = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
if (isDirectRun) {
process.env.LEARNING_TRACKER_RUNTIME = "web";
const { ProxyAgent, fetch: undiciFetch } = await import("undici");
const proxyDispatchers = new Map();
function dispatcherFor(proxyUrl) {
if (!proxyDispatchers.has(proxyUrl)) proxyDispatchers.set(proxyUrl, new ProxyAgent(proxyUrl));
return proxyDispatchers.get(proxyUrl);
}
configureMetadataFetch(async (input, init = {}) => {
const settings = getNetworkSettings();
const routes = settings.useProxy
? [
...(settings.httpProxy ? [{ label: `HTTP 代理 ${settings.httpProxy}`, proxy: settings.httpProxy }] : []),
...(settings.socksProxy ? [{ label: `SOCKS5 代理 ${settings.socksProxy}`, unsupported: true }] : []),
...(settings.fallbackToDirect ? [{ label: "直连回退", proxy: null }] : []),
]
: [{ label: "直连", proxy: null }];
const errors = [];
for (const route of routes) {
if (route.unsupported) {
errors.push(`${route.label}:浏览器运行模式暂不支持 SOCKS5,请使用桌面版或配置 HTTP 代理`);
continue;
}
try {
const response = await undiciFetch(input, {
...init,
...(route.proxy ? { dispatcher: dispatcherFor(route.proxy) } : {}),
});
if ([502, 503, 504].includes(response.status)) {
errors.push(`${route.label}返回 HTTP ${response.status}`);
continue;
}
return response;
} catch (error) {
const cause = error?.cause?.message || error?.message || "连接失败";
errors.push(`${route.label}:${cause}`);
}
}
throw new Error(`网页抓取请求失败;${errors.join(";")}`);
});
server.listen(port, "127.0.0.1", () => {
console.log(`Learning Tracker running at http://127.0.0.1:${port}`);
});
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}