weaver-work-cli 0.1.53 → 0.1.56
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/dist/chunks/{archive-ZQ4A4TV6.js → archive-4SQAVTIB.js} +3 -2
- package/dist/chunks/{archive-oa-57R2FYHV.js → archive-oa-W2UEJ67L.js} +137 -8
- package/dist/chunks/{asset-ZOJVBWZH.js → asset-EGZEABBI.js} +3 -2
- package/dist/chunks/{asset-idle-XGB5IPGT.js → asset-idle-EBUZBB6M.js} +3 -2
- package/dist/chunks/{attachment-content-W3TSJC3Y.js → attachment-content-QGM5MLQE.js} +4 -3
- package/dist/chunks/{blog-QSHLZSFW.js → blog-2YKMWOWI.js} +3 -2
- package/dist/chunks/{caizhilian-KCQYFH72.js → caizhilian-QJ4FLSQJ.js} +5 -4
- package/dist/chunks/{calendar-DLGMRJQA.js → calendar-H4QTJCUY.js} +3 -2
- package/dist/chunks/{chunk-XPQN4E5O.js → chunk-44KJ7WFA.js} +300 -55
- package/dist/chunks/{chunk-CZNZKOHR.js → chunk-4GG72JAT.js} +2 -2
- package/dist/chunks/{chunk-YIKSL36S.js → chunk-5N222HK6.js} +2 -2
- package/dist/chunks/{chunk-6GCXWKG6.js → chunk-6DVL5IN5.js} +4 -1
- package/dist/chunks/{chunk-WTLQGS6D.js → chunk-6MS3RLPO.js} +5 -334
- package/dist/chunks/{chunk-Z4G7LLDD.js → chunk-7EBV7Q3R.js} +124 -27
- package/dist/chunks/{chunk-76PRVYH4.js → chunk-AHJZIMCX.js} +1 -1
- package/dist/chunks/{chunk-TGAGQV4I.js → chunk-CQ4OVCKG.js} +2 -2
- package/dist/chunks/{chunk-2BQESR2U.js → chunk-DVR2EDUU.js} +99 -34
- package/dist/chunks/{chunk-QQXMHM3J.js → chunk-FDAJC7R4.js} +4 -4
- package/dist/chunks/{chunk-GR76ZDDX.js → chunk-FMNCSYIG.js} +2 -2
- package/dist/chunks/{chunk-IF33CG4V.js → chunk-L2LMBFG5.js} +1 -1
- package/dist/chunks/{chunk-DLY5GUXX.js → chunk-PH7N4UFT.js} +1 -1
- package/dist/chunks/{chunk-U6R6FZHD.js → chunk-SFM3M2L3.js} +286 -93
- package/dist/chunks/chunk-SHYQUBY3.js +48 -0
- package/dist/chunks/{chunk-HFXK54M4.js → chunk-TGM7BWEH.js} +2 -2
- package/dist/chunks/{chunk-2J3XY4ED.js → chunk-TJUAGZU7.js} +11 -11
- package/dist/chunks/{chunk-PE334WL3.js → chunk-UI66EMOR.js} +4 -4
- package/dist/chunks/{chunk-762M5XJS.js → chunk-VMCCRWFO.js} +2 -2
- package/dist/chunks/{chunk-U7ROML5E.js → chunk-WHJIUKJW.js} +681 -48
- package/dist/chunks/{chunk-PGO4ILMQ.js → chunk-WMB3SMXI.js} +1 -1
- package/dist/chunks/chunk-WMRNO6IU.js +337 -0
- package/dist/chunks/{chunk-6NXT4ICB.js → chunk-WVH5ESDA.js} +1 -1
- package/dist/chunks/{chunk-DEHJ7PMA.js → chunk-XU3FL3AW.js} +4 -4
- package/dist/chunks/{chunk-Z6UUBY37.js → chunk-Z4IPY5GV.js} +2 -2
- package/dist/chunks/{conflict-57WDJWL7.js → conflict-VAFFPUY3.js} +2 -2
- package/dist/chunks/{contract-LJRAXVZU.js → contract-LLRKMLQQ.js} +2655 -1319
- package/dist/chunks/{cowork-3ACICHDQ.js → cowork-2RDFBZWB.js} +3 -2
- package/dist/chunks/{create-flow-3FMMZBCA.js → create-flow-TVYE3IOH.js} +6 -6
- package/dist/chunks/{data-WK5CQXNF.js → data-WVICO3DT.js} +57 -11
- package/dist/chunks/{ebuilder-form-CBYI6CQ6.js → ebuilder-form-TWSXS5NA.js} +7 -6
- package/dist/chunks/{ehr-XI4BIVR2.js → ehr-52GBKNJR.js} +3 -2
- package/dist/chunks/{env-JYZE3BZB.js → env-BVVFIN4P.js} +2 -2
- package/dist/chunks/{esb-4BQHNP5C.js → esb-HJSXCEOM.js} +5 -4
- package/dist/chunks/{factory-66SCIKW3.js → factory-RCQ36LLE.js} +1 -1
- package/dist/chunks/{fna-ECOYBQYL.js → fna-OBACRT3J.js} +72 -3
- package/dist/chunks/{group-chat-MNRYMLNG.js → group-chat-YFZDUUY3.js} +3 -3
- package/dist/chunks/{host-TUOIOEMC.js → host-2BKMPWDH.js} +4 -3
- package/dist/chunks/{host-3TNBMDTR.js → host-KJT3G5D2.js} +4 -3
- package/dist/chunks/{host-7OWWPLM4.js → host-KOWUMNAY.js} +4 -3
- package/dist/chunks/{host-M4XP5URR.js → host-MFXRA2MY.js} +4 -3
- package/dist/chunks/{host-7NTIBSPK.js → host-TTSGY5NZ.js} +4 -3
- package/dist/chunks/{host-G3VU7SDW.js → host-W4JCNRFS.js} +4 -3
- package/dist/chunks/{hrm-YGIR3HGF.js → hrm-AK3WGBMN.js} +4 -3
- package/dist/chunks/{im-TN2JYVGJ.js → im-JZSBPO4Z.js} +4 -3
- package/dist/chunks/{invoice-V7ZTITUO.js → invoice-V6KD4TZE.js} +4 -3
- package/dist/chunks/{jiuchuanhui-ASFZBMPU.js → jiuchuanhui-K4EQU6GK.js} +3 -2
- package/dist/chunks/{lazy-dispatch-XSAHDK6Z.js → lazy-dispatch-JHGFCKJN.js} +5 -4
- package/dist/chunks/{mail-M3HJF5ER.js → mail-IKVCQCVZ.js} +3 -2
- package/dist/chunks/{meeting-IIY4UAJL.js → meeting-JOHV4RH3.js} +14 -12
- package/dist/chunks/minute-doc-JLS4KMFZ.js +156 -0
- package/dist/chunks/{okr-EVX7FB5G.js → okr-KV43FPPX.js} +3 -2
- package/dist/chunks/{operations-J7PUIVWZ.js → operations-ZPWBMYKE.js} +5 -4
- package/dist/chunks/{plan-2QJQEPBO.js → plan-WOYR4WEG.js} +3 -2
- package/dist/chunks/{project-XELELW2T.js → project-CQJKBUIL.js} +3 -2
- package/dist/chunks/{reception-QGPNTGHG.js → reception-MPBU3JXC.js} +3 -2
- package/dist/chunks/{recruit-stage-XCMVEQA2.js → recruit-stage-USTTRHOX.js} +3 -2
- package/dist/chunks/{registry-CJOVECZD.js → registry-GURQG4ZV.js} +3 -2
- package/dist/chunks/{registry-METZ2LWE.js → registry-LITYOH2W.js} +12 -10
- package/dist/chunks/{registry-QJCZ2GVG.js → registry-SS5J2FZQ.js} +5 -4
- package/dist/chunks/{room-recommend-YFY7HTOF.js → room-recommend-62MECG4J.js} +4 -4
- package/dist/chunks/{schedule-6I63TERA.js → schedule-6QRXIBGU.js} +4 -4
- package/dist/chunks/{smart-extract-FOUYNIG3.js → smart-extract-APX2PNIN.js} +3 -2
- package/dist/chunks/{workflow-YHXFIU63.js → workflow-WMPW3WSB.js} +5 -4
- package/dist/index.js +33 -32
- package/dist/internal/e10/auth/agent-host-key.js +68 -0
- package/dist/internal/e10/auth/agent-hosts.js +250 -0
- package/dist/internal/e10/auth/commands.js +99 -30
- package/dist/internal/e10/auth/paths.js +9 -0
- package/dist/internal/e10/auth/session.js +17 -0
- package/dist/internal/e10/auth/xiaoe.js +37 -3
- package/dist/shortcuts/archive-oa/operations/contract-modules.js +106 -0
- package/dist/shortcuts/archive-oa/operations/contract.js +38 -4
- package/dist/shortcuts/archive-oa/operations/registry.js +2 -1
- package/dist/shortcuts/contract/index.js +6 -7
- package/dist/shortcuts/contract/manifest.js +101 -24
- package/dist/shortcuts/contract/ooxml/docx-table.js +75 -3
- package/dist/shortcuts/contract/ooxml/xlsx.js +49 -13
- package/dist/shortcuts/contract/operations/card-action.js +113 -12
- package/dist/shortcuts/contract/operations/card-ebdf-list.js +29 -7
- package/dist/shortcuts/contract/operations/card-plan.js +75 -9
- package/dist/shortcuts/contract/operations/card-risk-report.js +2 -2
- package/dist/shortcuts/contract/operations/card.js +1 -1
- package/dist/shortcuts/contract/operations/clause-library.js +88 -16
- package/dist/shortcuts/contract/operations/compare-screen.js +378 -0
- package/dist/shortcuts/contract/operations/compare.js +1 -1
- package/dist/shortcuts/contract/operations/credit-page.js +1 -1
- package/dist/shortcuts/contract/operations/docx-slots.js +1000 -0
- package/dist/shortcuts/contract/operations/flow.js +2 -2
- package/dist/shortcuts/contract/operations/quote-to-contract.js +495 -0
- package/dist/shortcuts/contract/operations/quote.js +2 -229
- package/dist/shortcuts/contract/operations/recommend.js +1 -1
- package/dist/shortcuts/contract/operations/registry.js +6 -2
- package/dist/shortcuts/contract/operations/review.js +230 -4
- package/dist/shortcuts/contract/operations/save-recognize.js +122 -3
- package/dist/shortcuts/contract/operations/template.js +11 -10
- package/dist/shortcuts/data/operations/ds.js +5 -5
- package/dist/shortcuts/data/operations/shared.js +76 -4
- package/dist/shortcuts/fna/manifest.js +7 -2
- package/dist/shortcuts/fna/operations/budget.js +68 -0
- package/dist/shortcuts/meeting/minute-doc.js +153 -0
- package/dist/shortcuts/meeting/operations/compose-outcome.js +176 -0
- package/dist/shortcuts/meeting/operations/eb/catalog.js +1 -1
- package/dist/shortcuts/meeting/operations/eb/create-flow.js +47 -0
- package/dist/shortcuts/meeting/operations/eb/decision-task-batch.js +144 -0
- package/dist/shortcuts/meeting/operations/eb/decision.js +92 -4
- package/dist/shortcuts/meeting/operations/eb/index.js +133 -4
- package/dist/shortcuts/meeting/operations/eb/meeting-link.js +90 -0
- package/dist/shortcuts/meeting/operations/from-group-chat.js +46 -0
- package/dist/shortcuts/meeting/operations/minute-content.js +61 -0
- package/dist/shortcuts/meeting/operations/registry.js +14 -1
- package/dist/shortcuts/meeting/operations/self-check.js +49 -0
- package/dist/shortcuts/workflow/endpoints.js +2 -0
- package/dist/shortcuts/workflow/operations/read.js +114 -8
- package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.collect.js +72 -46
- package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.core.js +41 -0
- package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.js +46 -11
- package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.smartGroup.js +18 -1
- package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.smartGroupAdd.js +51 -0
- package/docs/e10-auth.md +78 -0
- package/docs/fna.md +8 -3
- package/package.json +4 -2
- package/scripts/pack-offline.mjs +228 -0
- package/scripts/package-skill.mjs +14 -5
- package/skill-template/business-info.json +3 -0
- package/skill-template/domains/qiyecheng.md +4 -3
- package/skills/weaver-e10-archive-oa/SKILL.md +6 -1
- package/skills/weaver-e10-archive-oa/references/contract-search.md +91 -4
- package/skills/weaver-e10-jinchengda/SKILL.md +100 -81
- package/skills/weaver-e10-jinchengda/references/contract-action-card-template.html +11 -6
- package/skills/weaver-e10-jinchengda/references/contract-action-card.md +5 -4
- package/skills/weaver-e10-jinchengda/references/contract-action-engine.md +38 -12
- package/skills/weaver-e10-jinchengda/references/contract-agent-entry.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-ai-assistant.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-ai-review.md +20 -6
- package/skills/weaver-e10-jinchengda/references/contract-card-render-internals.md +8 -3
- package/skills/weaver-e10-jinchengda/references/contract-clause-library-crud.md +56 -13
- package/skills/weaver-e10-jinchengda/references/contract-clause-recommender.md +7 -0
- package/skills/weaver-e10-jinchengda/references/contract-command-index.md +4 -4
- package/skills/weaver-e10-jinchengda/references/contract-compare-report.md +11 -5
- package/skills/weaver-e10-jinchengda/references/contract-credit-page.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-disabled-capabilities.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-doc-compare.md +38 -7
- package/skills/weaver-e10-jinchengda/references/contract-doc-triage.md +13 -25
- package/skills/weaver-e10-jinchengda/references/contract-element-save-record.md +9 -8
- package/skills/weaver-e10-jinchengda/references/contract-execution-rules.md +8 -6
- package/skills/weaver-e10-jinchengda/references/contract-field-extract-new-customer.md +10 -7
- package/skills/weaver-e10-jinchengda/references/contract-flow-launch.md +2 -2
- package/skills/weaver-e10-jinchengda/references/contract-ledger-catalog.md +3 -1
- package/skills/weaver-e10-jinchengda/references/contract-ledger-query-playbook.md +26 -18
- package/skills/weaver-e10-jinchengda/references/contract-modeltext-library.md +4 -4
- package/skills/weaver-e10-jinchengda/references/contract-multi-file.md +18 -13
- package/skills/weaver-e10-jinchengda/references/contract-performance-query.md +5 -3
- package/skills/weaver-e10-jinchengda/references/contract-quote-to-contract.md +105 -49
- package/skills/weaver-e10-jinchengda/references/contract-template-recommend.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-template-selector.md +3 -6
- package/skills/weaver-e10-meeting/SKILL.md +48 -9
- package/skills/weaver-e10-qiyecheng/SKILL.md +16 -7
- package/skills/weaver-e10-qiyecheng/references/budget.md +77 -3
- package/skills/weaver-e10-qiyecheng/references/catalog.md +6 -4
- package/skills/weaver-e10-qiyecheng/references/getEnabledTemplate-api.md +255 -0
- package/skills/weaver-e10-qiyecheng/references/source-manifest.json +70 -25
- package/skills/weaver-e10-workflow/SKILL.md +54 -40
- package/skills/weaver-e10-workflow/references/todo-dialogue.md +134 -0
- package/skills/weaver-e10-workflow/references/todo-smart-convert.md +102 -80
- package/skills/weaver-e10-workflow/references/workflow-agent-entry.md +23 -13
- package/skills/weaver-e10-workflow/references/workflow-query.md +68 -46
- package/skills/weaver-e10-workflow/references/workflow-view.md +21 -13
- package/skills/weaver-e10-workflow/tools/todo-summary.cjs +317 -332
- package/dist/shortcuts/contract/ooxml/docx.js +0 -295
- package/dist/shortcuts/contract/operations/docx-fillfromquote.js +0 -532
- package/skills/weaver-e10-jinchengda/references/contract-risk-report-template.html +0 -178
- package/skills/weaver-e10-jinchengda/references/source-manifest.json +0 -1270
- package/skills/weaver-e10-jinchengda/scripts/fill_plan.py +0 -998
- package/skills/weaver-e10-workflow/tools/todo-serve.cjs +0 -327
|
@@ -1,327 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* todo-serve.cjs —— 待办汇总载荷的本地 GET 数据源(v4:内存优先,2026-10-30)。
|
|
4
|
-
*
|
|
5
|
-
* v4 变化(为什么必须改):
|
|
6
|
-
* 宿主沙箱对「脱离父命令的常驻进程」的文件读授权,只在其启动命令存活期间有效——
|
|
7
|
-
* 命令一结束,服务就读不了任何快照文件(能 stat、不能 read ⇒ GET 404 snapshot_empty),
|
|
8
|
-
* 用户过几分钟再打开链接必 404。所以 v4 改为:**快照正文由 CLI 通过 POST 推进本进程内存,
|
|
9
|
-
* GET 从内存返回,服务端不再读任何磁盘文件**。
|
|
10
|
-
*
|
|
11
|
-
* 数据通路:
|
|
12
|
-
* POST /s/<token>/todo.json -> CLI 发布:body 即三键 JSON 原文,存入内存(须带 x-publish-key)
|
|
13
|
-
* GET /s/<token>/todo.json -> 内存优先,miss 再回落读磁盘快照(兼容旧文件)
|
|
14
|
-
* GET /todo.json?token=<t> -> 同上(兼容旧链接形态)
|
|
15
|
-
* GET /health -> 存活信息(inMemory = 内存快照数)
|
|
16
|
-
*
|
|
17
|
-
* 兼容与安全:
|
|
18
|
-
* - x-publish-key:启动时随机生成、写入 current.json 给 CLI;发布必须携带,防止拿到 URL 的人伪造发布;
|
|
19
|
-
* - 内存快照上限 20 份(LRU 淘汰)、单份 ≤ 8MB;
|
|
20
|
-
* - 服务收到正文后仍 best-effort 写一份磁盘副本(只写不读,排错用);
|
|
21
|
-
* - 旧版「CLI 写文件、服务读文件」的路径保留为回落,但不再是主通路。
|
|
22
|
-
*/
|
|
23
|
-
const http = require('node:http');
|
|
24
|
-
const crypto = require('node:crypto');
|
|
25
|
-
const fs = require('node:fs');
|
|
26
|
-
const os = require('node:os');
|
|
27
|
-
const path = require('node:path');
|
|
28
|
-
|
|
29
|
-
function parseArgs(argv) {
|
|
30
|
-
const out = {};
|
|
31
|
-
for (let i = 0; i < argv.length; i += 1) {
|
|
32
|
-
const token = argv[i];
|
|
33
|
-
if (!token.startsWith('--')) continue;
|
|
34
|
-
const key = token.slice(2);
|
|
35
|
-
const next = argv[i + 1];
|
|
36
|
-
if (next === undefined || next.startsWith('--')) out[key] = true;
|
|
37
|
-
else { out[key] = next; i += 1; }
|
|
38
|
-
}
|
|
39
|
-
return out;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** 与服务端约定一致的版本号:客户端只复用同版本实例。 */
|
|
43
|
-
const SERVE_VERSION = '4';
|
|
44
|
-
|
|
45
|
-
const args = parseArgs(process.argv.slice(2));
|
|
46
|
-
// 默认放系统临时目录:快照只有 30 分钟寿命,本就不该写进家目录;
|
|
47
|
-
// 而且宿主沙箱对家目录的删除操作更严(unlink/rmdir 会被 EPERM 拦),临时目录不受影响。
|
|
48
|
-
const root = path.resolve(String(
|
|
49
|
-
args.root && args.root !== true ? args.root : path.join(os.tmpdir(), 'weaver-work-cli-serve'),
|
|
50
|
-
));
|
|
51
|
-
const snapshotsDir = path.join(root, 'snapshots');
|
|
52
|
-
const registryFile = path.join(root, 'current.json');
|
|
53
|
-
const idleMinutes = Number(args.idle && args.idle !== true ? args.idle : 30);
|
|
54
|
-
const port = args.port !== undefined && args.port !== true ? Number(args.port) : 0;
|
|
55
|
-
const allowOrigin = args['allow-origin'] && args['allow-origin'] !== true ? String(args['allow-origin']) : '';
|
|
56
|
-
|
|
57
|
-
// token 只允许十六进制,避免 `../` 之类的目录穿越
|
|
58
|
-
const TOKEN_RE = /^[a-f0-9]{8,64}$/;
|
|
59
|
-
const IDLE_MS = idleMinutes * 60000;
|
|
60
|
-
/** 发布密钥:启动时随机生成,写进 current.json 给 CLI;发布请求必须带它(防伪造发布)。 */
|
|
61
|
-
const publishKey = crypto.randomBytes(8).toString('hex');
|
|
62
|
-
/** 内存快照:token -> { body, createdAt, bytes }。GET 的主来源,服务端不再读磁盘。 */
|
|
63
|
-
const memorySnapshots = new Map();
|
|
64
|
-
const MAX_MEMORY_SNAPSHOTS = 20;
|
|
65
|
-
const MAX_BODY_BYTES = 8 * 1024 * 1024;
|
|
66
|
-
|
|
67
|
-
fs.mkdirSync(snapshotsDir, { recursive: true });
|
|
68
|
-
|
|
69
|
-
let hits = 0;
|
|
70
|
-
let lastHit = Date.now();
|
|
71
|
-
|
|
72
|
-
function touchDir(dir) {
|
|
73
|
-
try {
|
|
74
|
-
const now = new Date();
|
|
75
|
-
fs.utimesSync(dir, now, now);
|
|
76
|
-
} catch { /* 目录已被删,忽略 */ }
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function dropSnapshot(dir) {
|
|
80
|
-
// 不用 rmSync(recursive):宿主沙箱会拦后台进程的批量删除(EPERM),
|
|
81
|
-
// 逐文件 unlink 再 rmdir 可以通过;任一步失败都不影响「过期即 404」的安全性。
|
|
82
|
-
try {
|
|
83
|
-
for (const name of fs.readdirSync(dir)) {
|
|
84
|
-
try { fs.unlinkSync(path.join(dir, name)); } catch { /* 忽略 */ }
|
|
85
|
-
}
|
|
86
|
-
fs.rmdirSync(dir);
|
|
87
|
-
} catch { /* 忽略 */ }
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** 读一份快照:**内存优先**,miss 再回落磁盘文件(兼容旧路径)。过期都返回 404。 */
|
|
91
|
-
function readSnapshot(token) {
|
|
92
|
-
if (!TOKEN_RE.test(String(token || ''))) return { status: 400, error: 'invalid_token' };
|
|
93
|
-
const mem = memorySnapshots.get(token);
|
|
94
|
-
if (mem) {
|
|
95
|
-
mem.createdAt = Date.now(); // 命中即续期
|
|
96
|
-
return { status: 200, body: mem.body };
|
|
97
|
-
}
|
|
98
|
-
const dir = path.join(snapshotsDir, token);
|
|
99
|
-
const file = path.join(dir, 'todo.json');
|
|
100
|
-
let mtime = 0;
|
|
101
|
-
try {
|
|
102
|
-
mtime = fs.statSync(dir).mtimeMs;
|
|
103
|
-
} catch {
|
|
104
|
-
return { status: 404, error: 'snapshot_not_found' };
|
|
105
|
-
}
|
|
106
|
-
if (Date.now() - mtime > IDLE_MS) {
|
|
107
|
-
dropSnapshot(dir);
|
|
108
|
-
return { status: 404, error: 'snapshot_expired' };
|
|
109
|
-
}
|
|
110
|
-
let body = '';
|
|
111
|
-
try {
|
|
112
|
-
body = fs.readFileSync(file, 'utf8');
|
|
113
|
-
} catch {
|
|
114
|
-
return { status: 404, error: 'snapshot_empty' };
|
|
115
|
-
}
|
|
116
|
-
touchDir(dir);
|
|
117
|
-
return { status: 200, body };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** 清掉过期快照(内存 + 磁盘),返回仍在有效期内的数量。 */
|
|
121
|
-
function sweepSnapshots() {
|
|
122
|
-
const now = Date.now();
|
|
123
|
-
for (const [token, mem] of memorySnapshots) {
|
|
124
|
-
if (now - mem.createdAt > IDLE_MS) memorySnapshots.delete(token);
|
|
125
|
-
}
|
|
126
|
-
let names = [];
|
|
127
|
-
try {
|
|
128
|
-
names = fs.readdirSync(snapshotsDir);
|
|
129
|
-
} catch {
|
|
130
|
-
names = [];
|
|
131
|
-
}
|
|
132
|
-
let alive = 0;
|
|
133
|
-
for (const name of names) {
|
|
134
|
-
const dir = path.join(snapshotsDir, name);
|
|
135
|
-
let mtime = 0;
|
|
136
|
-
try {
|
|
137
|
-
mtime = fs.statSync(dir).mtimeMs;
|
|
138
|
-
} catch {
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
if (Date.now() - mtime > IDLE_MS) dropSnapshot(dir);
|
|
142
|
-
else alive += 1;
|
|
143
|
-
}
|
|
144
|
-
return alive + memorySnapshots.size;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function corsHeaders(req) {
|
|
148
|
-
const origin = allowOrigin || req.headers.origin || '*';
|
|
149
|
-
return {
|
|
150
|
-
'Access-Control-Allow-Origin': origin,
|
|
151
|
-
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
|
152
|
-
'Access-Control-Allow-Headers': req.headers['access-control-request-headers'] || '*',
|
|
153
|
-
'Access-Control-Allow-Private-Network': 'true',
|
|
154
|
-
'Access-Control-Max-Age': '600',
|
|
155
|
-
Vary: 'Origin',
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function send(res, status, headers, body) {
|
|
160
|
-
res.writeHead(status, { ...headers, 'Content-Type': 'application/json; charset=utf-8' });
|
|
161
|
-
res.end(body);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** 读取请求体(有 8MB 上限,超限返回 null)。 */
|
|
165
|
-
function readBody(req, onDone) {
|
|
166
|
-
const chunks = [];
|
|
167
|
-
let size = 0;
|
|
168
|
-
let aborted = false;
|
|
169
|
-
req.on('data', (chunk) => {
|
|
170
|
-
if (aborted) return;
|
|
171
|
-
size += chunk.length;
|
|
172
|
-
if (size > MAX_BODY_BYTES) {
|
|
173
|
-
aborted = true;
|
|
174
|
-
onDone(null);
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
chunks.push(chunk);
|
|
178
|
-
});
|
|
179
|
-
req.on('end', () => { if (!aborted) onDone(Buffer.concat(chunks).toString('utf8')); });
|
|
180
|
-
req.on('error', () => { if (!aborted) onDone(null); });
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/** 内存快照:存入并按 LRU 淘汰最旧的,返回是否成功。 */
|
|
184
|
-
function putMemorySnapshot(token, body) {
|
|
185
|
-
memorySnapshots.set(token, { body, createdAt: Date.now(), bytes: Buffer.byteLength(body) });
|
|
186
|
-
while (memorySnapshots.size > MAX_MEMORY_SNAPSHOTS) {
|
|
187
|
-
const oldest = [...memorySnapshots.entries()].sort((a, b) => a[1].createdAt - b[1].createdAt)[0];
|
|
188
|
-
if (!oldest) break;
|
|
189
|
-
memorySnapshots.delete(oldest[0]);
|
|
190
|
-
}
|
|
191
|
-
return true;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const server = http.createServer((req, res) => {
|
|
195
|
-
lastHit = Date.now();
|
|
196
|
-
const url = new URL(req.url || '/', 'http://127.0.0.1');
|
|
197
|
-
const headers = corsHeaders(req);
|
|
198
|
-
|
|
199
|
-
if (req.method === 'OPTIONS') {
|
|
200
|
-
res.writeHead(204, headers);
|
|
201
|
-
res.end();
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// 发布端点:CLI 把三键 JSON 原文 POST 进内存(必须带 x-publish-key)。
|
|
206
|
-
// 这是 v4 的主通路——服务端从此不依赖「读磁盘」就能服务 GET。
|
|
207
|
-
let publishMatch = null;
|
|
208
|
-
if (req.method === 'POST') {
|
|
209
|
-
publishMatch = url.pathname.match(/^\/s\/([^/]+)\/todo\.json$/);
|
|
210
|
-
if (!publishMatch) {
|
|
211
|
-
send(res, 404, headers, JSON.stringify({ ok: false, error: 'not_found' }));
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
const token = publishMatch[1];
|
|
215
|
-
if (!TOKEN_RE.test(token)) {
|
|
216
|
-
send(res, 400, headers, JSON.stringify({ ok: false, error: 'invalid_token' }));
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
if (String(req.headers['x-publish-key'] || '') !== publishKey) {
|
|
220
|
-
send(res, 403, headers, JSON.stringify({ ok: false, error: 'forbidden' }));
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
readBody(req, (body) => {
|
|
224
|
-
if (body === null) {
|
|
225
|
-
send(res, 413, headers, JSON.stringify({ ok: false, error: 'payload_too_large' }));
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
try { JSON.parse(body); } catch {
|
|
229
|
-
send(res, 400, headers, JSON.stringify({ ok: false, error: 'invalid_json' }));
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
putMemorySnapshot(token, body);
|
|
233
|
-
// best-effort 落一份磁盘副本:只写不读,排错/人工恢复用
|
|
234
|
-
try {
|
|
235
|
-
const dir = path.join(snapshotsDir, token);
|
|
236
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
237
|
-
fs.writeFileSync(path.join(dir, 'todo.json'), body);
|
|
238
|
-
} catch { /* 写不了也不影响内存通路 */ }
|
|
239
|
-
send(res, 200, { ...headers, 'Cache-Control': 'no-store' }, JSON.stringify({
|
|
240
|
-
ok: true, token, bytes: Buffer.byteLength(body), inMemory: memorySnapshots.size,
|
|
241
|
-
}));
|
|
242
|
-
});
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (req.method !== 'GET') {
|
|
247
|
-
send(res, 405, headers, JSON.stringify({ ok: false, error: 'method_not_allowed' }));
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
if (url.pathname === '/health') {
|
|
251
|
-
const alive = sweepSnapshots();
|
|
252
|
-
const address = server.address();
|
|
253
|
-
send(res, 200, { ...headers, 'Cache-Control': 'no-store' }, JSON.stringify({
|
|
254
|
-
ok: true,
|
|
255
|
-
version: SERVE_VERSION,
|
|
256
|
-
pid: process.pid,
|
|
257
|
-
port: address ? address.port : port,
|
|
258
|
-
snapshots: alive,
|
|
259
|
-
inMemory: memorySnapshots.size,
|
|
260
|
-
idleMinutes,
|
|
261
|
-
hits,
|
|
262
|
-
}));
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// 新形态 /s/<token>/todo.json,兼容旧形态 /todo.json?token=<token>
|
|
267
|
-
let token = '';
|
|
268
|
-
const matched = url.pathname.match(/^\/s\/([^/]+)\/todo\.json$/);
|
|
269
|
-
if (matched) token = matched[1];
|
|
270
|
-
else if (url.pathname === '/todo.json' || url.pathname === '/') token = url.searchParams.get('token') || '';
|
|
271
|
-
|
|
272
|
-
if (!token) {
|
|
273
|
-
send(res, 404, headers, JSON.stringify({ ok: false, error: 'not_found' }));
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
const result = readSnapshot(token);
|
|
277
|
-
if (result.status !== 200) {
|
|
278
|
-
send(res, result.status, { ...headers, 'Cache-Control': 'no-store' }, JSON.stringify({ ok: false, error: result.error }));
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
hits += 1;
|
|
282
|
-
send(res, 200, { ...headers, 'Cache-Control': 'no-store' }, result.body);
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
let retries = 0;
|
|
286
|
-
server.on('error', (error) => {
|
|
287
|
-
if (error && error.code === 'EADDRINUSE' && retries < 10) {
|
|
288
|
-
retries += 1;
|
|
289
|
-
console.error('[todo-serve] 端口 %d 被占用,换下一个', port + retries);
|
|
290
|
-
server.listen(port + retries, '127.0.0.1');
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
console.error('[todo-serve] 监听失败:%s', error && error.message);
|
|
294
|
-
cleanup(1);
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
server.listen(port, '127.0.0.1', () => {
|
|
298
|
-
const actualPort = server.address().port;
|
|
299
|
-
fs.writeFileSync(registryFile, JSON.stringify({
|
|
300
|
-
pid: process.pid,
|
|
301
|
-
port: actualPort,
|
|
302
|
-
version: SERVE_VERSION,
|
|
303
|
-
root,
|
|
304
|
-
startedAt: new Date().toISOString(),
|
|
305
|
-
idleMinutes,
|
|
306
|
-
publishKey,
|
|
307
|
-
}, null, 2));
|
|
308
|
-
console.log('[todo-serve] listening http://127.0.0.1:%d/s/<token>/todo.json|内存优先版 v%s|空闲 %d 分钟自动退出|pid=%d', actualPort, SERVE_VERSION, idleMinutes, process.pid);
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
function cleanup(code) {
|
|
312
|
-
try { fs.unlinkSync(registryFile); } catch { /* 已删 */ }
|
|
313
|
-
process.exit(code);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
const sweeper = setInterval(() => {
|
|
317
|
-
const alive = sweepSnapshots();
|
|
318
|
-
if (alive > 0) return;
|
|
319
|
-
if (Date.now() - lastHit < IDLE_MS) return;
|
|
320
|
-
console.log('[todo-serve] 无快照且连续 %d 分钟无访问,退出', idleMinutes);
|
|
321
|
-
cleanup(0);
|
|
322
|
-
}, 15000);
|
|
323
|
-
sweeper.unref();
|
|
324
|
-
|
|
325
|
-
for (const sig of ['SIGINT', 'SIGTERM']) {
|
|
326
|
-
process.on(sig, () => cleanup(0));
|
|
327
|
-
}
|