@xiaobuyu/nodesign 0.0.1 → 0.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.
Files changed (282) hide show
  1. package/README.md +156 -106
  2. package/bin/nodesign.js +0 -0
  3. package/package.json +1 -1
  4. package/server/api/README.md +45 -45
  5. package/server/api/_guard.js +75 -75
  6. package/server/api/admin.js +187 -187
  7. package/server/api/assets/docx-page.js +103 -103
  8. package/server/api/assets/helpers.js +22 -22
  9. package/server/api/assets/notes.js +144 -144
  10. package/server/api/assets.js +896 -896
  11. package/server/api/board.js +57 -57
  12. package/server/api/browse.js +138 -138
  13. package/server/api/canvas.js +444 -444
  14. package/server/api/chatai.js +324 -324
  15. package/server/api/exports/build-standalone.js +980 -980
  16. package/server/api/exports/cards.js +118 -118
  17. package/server/api/exports/docx-pdf.js +46 -46
  18. package/server/api/exports/export-page.js +142 -142
  19. package/server/api/exports/handoff.js +147 -147
  20. package/server/api/exports.js +690 -690
  21. package/server/api/instruction.js +75 -75
  22. package/server/api/me.js +116 -116
  23. package/server/api/memory.js +185 -185
  24. package/server/api/pending-changes.js +366 -366
  25. package/server/api/plugins.js +164 -164
  26. package/server/api/projects.js +220 -220
  27. package/server/api/publish.js +64 -64
  28. package/server/api/recent.js +73 -73
  29. package/server/api/sessions.js +589 -589
  30. package/server/api/skills.js +77 -77
  31. package/server/api/standalone-fit.js +168 -168
  32. package/server/api/turn-compose.js +180 -180
  33. package/server/api/turn-inflight.js +44 -44
  34. package/server/auth/README.md +17 -17
  35. package/server/auth/middleware.js +138 -138
  36. package/server/auth/origin-guard.js +71 -71
  37. package/server/auth/session.js +108 -108
  38. package/server/auth/tier.js +113 -113
  39. package/server/auth/users-store.js +348 -348
  40. package/server/edit/README.md +29 -29
  41. package/server/engine/README.md +113 -113
  42. package/server/engine/agent/agent-shared.js +550 -550
  43. package/server/engine/agent/auto-mode-default-hard-deny.txt +4 -4
  44. package/server/engine/agent/auto-mode-rules.js +78 -78
  45. package/server/engine/agent/context.js +418 -418
  46. package/server/engine/agent/events.js +464 -464
  47. package/server/engine/agent/hooks/canvas-validate.js +199 -199
  48. package/server/engine/agent/hooks/failure.js +120 -120
  49. package/server/engine/agent/hooks/file-events.js +75 -75
  50. package/server/engine/agent/hooks/lifecycle.js +233 -233
  51. package/server/engine/agent/hooks/post-canvas-focus.js +80 -80
  52. package/server/engine/agent/hooks/post-guidance.js +206 -206
  53. package/server/engine/agent/hooks/post-subagent-report.js +104 -104
  54. package/server/engine/agent/hooks/post-trim.js +58 -58
  55. package/server/engine/agent/hooks/pre-board-neighborhood.js +34 -34
  56. package/server/engine/agent/hooks/pre-defaults.js +84 -84
  57. package/server/engine/agent/hooks/pre-injectors.js +277 -277
  58. package/server/engine/agent/hooks/pre-performance-log-guard.js +78 -78
  59. package/server/engine/agent/hooks/pre-starter-files.js +61 -61
  60. package/server/engine/agent/hooks/pre-workspace-scope-guard.js +78 -78
  61. package/server/engine/agent/hooks/site-validate.js +94 -94
  62. package/server/engine/agent/hooks/tool-prompts.js +31 -31
  63. package/server/engine/agent/hooks/turn-state-memory.js +56 -56
  64. package/server/engine/agent/hooks/user-prompt-submit.js +215 -215
  65. package/server/engine/agent/hooks.js +354 -354
  66. package/server/engine/agent/init-contract.js +69 -69
  67. package/server/engine/agent/isolation.js +159 -159
  68. package/server/engine/agent/model-table.js +292 -292
  69. package/server/engine/agent/plugin-loader.js +266 -266
  70. package/server/engine/agent/prompts/nodesign-prelude.md +336 -336
  71. package/server/engine/agent/prompts/tools/ask-user-question-protocol.md +71 -71
  72. package/server/engine/agent/prompts/tools/direct-edit-protocol.md +98 -98
  73. package/server/engine/agent/prompts/tools/generate-image-cookbook.gemini-gateway.md +583 -583
  74. package/server/engine/agent/prompts/tools/generate-image-cookbook.md +483 -483
  75. package/server/engine/agent/prompts/tools/hybrid-reference.md +100 -100
  76. package/server/engine/agent/prompts/tools/paint-still-cookbook.md +270 -270
  77. package/server/engine/agent/prompts/tools/roll-film-cookbook.md +70 -70
  78. package/server/engine/agent/prompts/tools/site-reference.md +106 -106
  79. package/server/engine/agent/prompts/tools/tweaks-syntax.md +125 -125
  80. package/server/engine/agent/prompts/tools/vision-checker-dispatch.md +48 -48
  81. package/server/engine/agent/session-loop.js +1024 -1024
  82. package/server/engine/agent/session-model.js +164 -164
  83. package/server/engine/agent/skill.js +204 -204
  84. package/server/engine/agent/task-events.js +78 -78
  85. package/server/engine/agents/ds-extractor.md +132 -132
  86. package/server/engine/agents/explorer.md +190 -190
  87. package/server/engine/agents/index.js +230 -230
  88. package/server/engine/agents/schemas/design-system.json +156 -156
  89. package/server/engine/agents/schemas/tweak-schema.json +113 -113
  90. package/server/engine/agents/tweak-proposer.md +127 -127
  91. package/server/engine/agents/vision-checker.md +254 -254
  92. package/server/engine/browse/capture.js +422 -422
  93. package/server/engine/browse/card.js +137 -137
  94. package/server/engine/browse/handover.js +59 -59
  95. package/server/engine/browse/page-digest.js +119 -119
  96. package/server/engine/browse/refs.js +196 -196
  97. package/server/engine/browse/registry.js +307 -307
  98. package/server/engine/browse/screencast.js +170 -170
  99. package/server/engine/browse/state.js +121 -121
  100. package/server/engine/chatai/chat-log.js +79 -79
  101. package/server/engine/chatai/index.js +193 -193
  102. package/server/engine/chatai/openai-compat.js +152 -152
  103. package/server/engine/chatai/orchestrate.js +309 -309
  104. package/server/engine/chatai/perform.js +40 -40
  105. package/server/engine/chatai/summarize.js +79 -79
  106. package/server/engine/mcp/capability-gate.js +52 -52
  107. package/server/engine/mcp/index.js +339 -339
  108. package/server/engine/mcp/param-sanitizer.js +142 -142
  109. package/server/engine/mcp/tools/arrange-on-board.js +124 -124
  110. package/server/engine/mcp/tools/artifact-session.js +278 -278
  111. package/server/engine/mcp/tools/browse-computer.js +421 -421
  112. package/server/engine/mcp/tools/browse-find-batch.js +183 -183
  113. package/server/engine/mcp/tools/browse-screenshot.js +157 -157
  114. package/server/engine/mcp/tools/browse.js +480 -480
  115. package/server/engine/mcp/tools/build-docx.js +101 -101
  116. package/server/engine/mcp/tools/clear-pending-changes.js +99 -99
  117. package/server/engine/mcp/tools/create-on-board.js +155 -155
  118. package/server/engine/mcp/tools/crystallize-skill.js +168 -168
  119. package/server/engine/mcp/tools/explain-style.js +237 -237
  120. package/server/engine/mcp/tools/export-handoff.js +133 -133
  121. package/server/engine/mcp/tools/expose-tweaks.js +149 -149
  122. package/server/engine/mcp/tools/generate-image.js +842 -842
  123. package/server/engine/mcp/tools/get-computed-styles.js +164 -164
  124. package/server/engine/mcp/tools/get-pending-changes.js +205 -205
  125. package/server/engine/mcp/tools/h3box-ssh.js +84 -84
  126. package/server/engine/mcp/tools/helpers/acquire-page.js +82 -82
  127. package/server/engine/mcp/tools/helpers/motion-lab.js +554 -554
  128. package/server/engine/mcp/tools/helpers/motion-scroll.js +112 -112
  129. package/server/engine/mcp/tools/helpers/perception-page.js +215 -215
  130. package/server/engine/mcp/tools/helpers/reference-download.js +103 -103
  131. package/server/engine/mcp/tools/helpers/rembg-bridge.py +80 -80
  132. package/server/engine/mcp/tools/helpers/shot-pipeline.js +280 -280
  133. package/server/engine/mcp/tools/highlight.js +68 -68
  134. package/server/engine/mcp/tools/list-pages.js +222 -222
  135. package/server/engine/mcp/tools/lookup-tags.js +59 -59
  136. package/server/engine/mcp/tools/navigate-to-page.js +60 -60
  137. package/server/engine/mcp/tools/organize-board.js +64 -64
  138. package/server/engine/mcp/tools/paint-still.js +434 -434
  139. package/server/engine/mcp/tools/pin-to-board.js +130 -130
  140. package/server/engine/mcp/tools/preview-deck.js +130 -130
  141. package/server/engine/mcp/tools/profile-scroll.js +305 -305
  142. package/server/engine/mcp/tools/publish-site.js +102 -102
  143. package/server/engine/mcp/tools/query-elements.js +195 -195
  144. package/server/engine/mcp/tools/read-board.js +103 -103
  145. package/server/engine/mcp/tools/read-document.js +93 -93
  146. package/server/engine/mcp/tools/read-page.js +264 -264
  147. package/server/engine/mcp/tools/read-tavern-json.js +143 -143
  148. package/server/engine/mcp/tools/record-decision.js +140 -140
  149. package/server/engine/mcp/tools/relate-on-board.js +110 -110
  150. package/server/engine/mcp/tools/report-issue.js +115 -115
  151. package/server/engine/mcp/tools/roll-film.js +266 -266
  152. package/server/engine/mcp/tools/screenshot-docx.js +138 -138
  153. package/server/engine/mcp/tools/screenshot-url.js +195 -195
  154. package/server/engine/mcp/tools/screenshot.js +583 -583
  155. package/server/engine/mcp/tools/tier-gate.js +96 -96
  156. package/server/engine/mcp/tools/trace-motion.js +215 -215
  157. package/server/engine/mcp/tools/web-search.js +548 -548
  158. package/server/engine/motion/inventory.js +349 -349
  159. package/server/engine/perception/session.js +235 -235
  160. package/server/engine/plugins/nodesign/.claude-plugin/plugin.json +5 -5
  161. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/SKILL.md +192 -192
  162. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/canvas.template.html +667 -667
  163. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/hybrid-grid.md +22 -22
  164. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/image-led-cover.md +25 -25
  165. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/portrait.md +23 -23
  166. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/quote-backdrop.md +21 -21
  167. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/section-divider.md +23 -23
  168. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/text-led.md +21 -21
  169. package/server/engine/plugins/nodesign/skills/docx-craft/SKILL.md +162 -162
  170. package/server/engine/plugins/nodesign/skills/docx-craft/references/token-schema.md +407 -407
  171. package/server/engine/plugins/nodesign/skills/docx-craft//346/226/207/346/241/243.template.json +80 -80
  172. package/server/engine/plugins/nodesign/skills/rp-craft/SKILL.md +228 -228
  173. package/server/engine/plugins/nodesign/skills/rp-craft/patterns//346/274/224/345/207/272/351/241/265-/346/234/200/345/260/217/345/256/236/347/216/260.html +78 -78
  174. package/server/engine/plugins/nodesign/skills/rp-craft//346/274/224/345/207/272.template.js +268 -268
  175. package/server/engine/plugins/nodesign/skills/site-craft/SKILL.md +323 -323
  176. package/server/engine/plugins/nodesign/skills/site-craft/patterns/build-lane.md +134 -134
  177. package/server/engine/plugins/nodesign/skills/site-craft/patterns/cutout-collage.md +61 -61
  178. package/server/engine/plugins/nodesign/skills/site-craft/patterns/mock-app.md +163 -163
  179. package/server/engine/plugins/nodesign/skills/site-craft/patterns/page-transitions.md +86 -86
  180. package/server/engine/runs/active-runs.js +698 -698
  181. package/server/engine/runs/live-turn.js +252 -252
  182. package/server/engine/runs/store.js +364 -364
  183. package/server/engine/runs/turn-relay.js +217 -217
  184. package/server/engine/runtime/workspace.js +164 -164
  185. package/server/lib/artifact-file-path.js +80 -80
  186. package/server/lib/artifact-target.js +284 -284
  187. package/server/lib/asset-refs.js +239 -239
  188. package/server/lib/async-queue.js +104 -104
  189. package/server/lib/auto-relations.js +130 -130
  190. package/server/lib/binding-types.js +60 -60
  191. package/server/lib/board-kind-sizes.js +62 -62
  192. package/server/lib/board-relations.js +133 -133
  193. package/server/lib/browse-proxy.js +180 -180
  194. package/server/lib/canvas-id.js +36 -36
  195. package/server/lib/cover.js +227 -227
  196. package/server/lib/danbooru-tags.js +291 -291
  197. package/server/lib/doc-extract.js +156 -156
  198. package/server/lib/docx/build-from-source.js +260 -260
  199. package/server/lib/docx/build.js +490 -490
  200. package/server/lib/docx/dump-styles.js +287 -287
  201. package/server/lib/docx/fonts/nodesign-cjk.conf +164 -164
  202. package/server/lib/docx/merge-runs.js +129 -129
  203. package/server/lib/docx/numbering.js +218 -218
  204. package/server/lib/docx/order.js +152 -152
  205. package/server/lib/docx/rawzip.js +172 -172
  206. package/server/lib/docx/render.js +92 -92
  207. package/server/lib/docx/text-lint.js +225 -225
  208. package/server/lib/docx/tokens.js +335 -335
  209. package/server/lib/docx/units.js +29 -29
  210. package/server/lib/docx/xml.js +291 -291
  211. package/server/lib/docx-pages.js +187 -187
  212. package/server/lib/export-collect.js +289 -289
  213. package/server/lib/export-package.js +210 -210
  214. package/server/lib/html-srcset.js +145 -145
  215. package/server/lib/image-variant.js +460 -460
  216. package/server/lib/ingress/forward-openai-chat.js +305 -305
  217. package/server/lib/ingress/openai-chat.js +491 -491
  218. package/server/lib/ingress/session-notice.js +66 -66
  219. package/server/lib/ingress/session-routes.js +68 -68
  220. package/server/lib/ingress/slot-probe.js +130 -130
  221. package/server/lib/ingress/upstream-billing.js +52 -52
  222. package/server/lib/ingress/upstream-fail-streak.js +72 -72
  223. package/server/lib/ingress/upstream-truncation.js +48 -48
  224. package/server/lib/issues-store.js +182 -182
  225. package/server/lib/kinds/deck.js +72 -72
  226. package/server/lib/kinds/docx.js +283 -283
  227. package/server/lib/kinds/file-kinds.js +113 -113
  228. package/server/lib/kinds/index.js +202 -202
  229. package/server/lib/kinds/site.js +161 -161
  230. package/server/lib/model-ingress.js +543 -543
  231. package/server/lib/moderation.js +255 -255
  232. package/server/lib/notice-store.js +103 -103
  233. package/server/lib/plugin-install.js +104 -104
  234. package/server/lib/plugin-validator.js +721 -721
  235. package/server/lib/publish-store.js +115 -115
  236. package/server/lib/quick-summary.js +52 -52
  237. package/server/lib/quota.js +270 -270
  238. package/server/lib/rate-window.js +29 -29
  239. package/server/lib/reference-assets.js +92 -92
  240. package/server/lib/region-shot.js +135 -135
  241. package/server/lib/safe-path.js +73 -73
  242. package/server/lib/sdk-session.js +33 -33
  243. package/server/lib/showcase-store.js +92 -92
  244. package/server/lib/site-publish.js +465 -465
  245. package/server/lib/ssrf-guard.js +432 -432
  246. package/server/lib/task-scan.js +158 -158
  247. package/server/lib/tavern-json.js +154 -154
  248. package/server/lib/video-variant.js +237 -237
  249. package/server/lib/workspace-path.js +33 -33
  250. package/server/ops/fix-sdk-musl.mjs +40 -40
  251. package/server/ops/install-macos-fonts.sh +114 -114
  252. package/server/ops/macos-fonts.conf +336 -336
  253. package/server/ops/sandbox-shim/bwrap +57 -57
  254. package/server/projects/assets-summary.js +119 -119
  255. package/server/projects/auto-name.js +53 -53
  256. package/server/projects/board-store.js +640 -640
  257. package/server/projects/move-entry.js +103 -103
  258. package/server/projects/store.js +270 -270
  259. package/server/projects/ui-config.js +54 -54
  260. package/server/projects/workspace-templates.js +53 -53
  261. package/server/projects/workspace.js +1025 -1025
  262. package/server/runtime/local-config.js +177 -177
  263. package/server/runtime/profile.js +72 -72
  264. package/server/services/rembg-service.py +334 -334
  265. package/server/shared/README.md +25 -25
  266. package/server/shared/deck.js +54 -54
  267. package/server/shared/time.js +49 -49
  268. package/server/style-pipeline/README.md +53 -53
  269. package/server/ws/broker.js +36 -36
  270. package/server/ws/browse-channel.js +177 -177
  271. package/server/ws/index.js +386 -386
  272. package/web/README.md +89 -89
  273. package/web/dist/assets/BrowserWindow-BYCgizHw.js +11 -0
  274. package/web/dist/assets/{DeckWindow-4G0xX7P3.js → DeckWindow-pNydEM4L.js} +1 -1
  275. package/web/dist/assets/{DocxWindow-BXQsoPkh.js → DocxWindow-PPgzh3Gq.js} +1 -1
  276. package/web/dist/assets/{SiteWindow-D6eS-2gM.js → SiteWindow-DM0LQPCO.js} +1 -1
  277. package/web/dist/assets/{index-Cbl3ATvp.js → index-BHTBJlpY.js} +32 -32
  278. package/web/dist/assets/{pending-edit-apply-DTjo2geW.js → pending-edit-apply-C0I13CbD.js} +1 -1
  279. package/web/dist/assets/{rotate-cw-LMT3x1Zj.js → rotate-cw-B1UQ8Hld.js} +1 -1
  280. package/web/dist/assets/{square-dashed-mouse-pointer-CBoAkfhi.js → square-dashed-mouse-pointer-8lrgqmfv.js} +1 -1
  281. package/web/dist/index.html +17 -17
  282. package/web/dist/assets/BrowserWindow-Dc7J7CYc.js +0 -11
@@ -1,172 +1,172 @@
1
- /**
2
- * rawzip.js — 面向 docx 的最小 ZIP 读写器,核心卖点是「保真」:
3
- * 没被替换的 entry,local header + 压缩数据 + data descriptor 原样字节拷贝;
4
- * 只有被替换/新增的 entry 走 zlib deflateRaw 重压。
5
- *
6
- * 覆盖范围 = OOXML 容器实际会遇到的:
7
- * - 方法 0 (store) / 8 (deflate)
8
- * - bit3 data descriptor(流式写出的 zip)
9
- * - 不支持 zip64 / 加密 / 分卷(docx 不会用;遇到即抛错,调用方退回只读)
10
- */
11
-
12
- import { deflateRawSync, inflateRawSync } from 'node:zlib';
13
- import { crc32 } from 'node:zlib';
14
-
15
- const EOCD_SIG = 0x06054b50;
16
- const CEN_SIG = 0x02014b50;
17
- const LOC_SIG = 0x04034b50;
18
-
19
- /**
20
- * @param {Buffer} buf
21
- * @returns {{ entries: Map<string, Entry>, order: string[] }}
22
- * Entry = { name, method, crc, csize, usize, flags, cenExtra..., locRaw:Buffer|null, replaced:Buffer|null }
23
- */
24
- export function readZip(buf) {
25
- // 找 EOCD(从尾部回扫,容忍 comment)
26
- let eocd = -1;
27
- for (let i = buf.length - 22; i >= Math.max(0, buf.length - 22 - 65535); i--) {
28
- if (buf.readUInt32LE(i) === EOCD_SIG) { eocd = i; break; }
29
- }
30
- if (eocd < 0) throw new Error('EOCD not found (not a zip)');
31
- const count = buf.readUInt16LE(eocd + 10);
32
- const cenOfs = buf.readUInt32LE(eocd + 16);
33
- if (cenOfs === 0xffffffff) throw new Error('zip64 not supported');
34
-
35
- const entries = new Map();
36
- const order = [];
37
- let p = cenOfs;
38
- for (let i = 0; i < count; i++) {
39
- if (buf.readUInt32LE(p) !== CEN_SIG) throw new Error(`bad central dir at ${p}`);
40
- const flags = buf.readUInt16LE(p + 8);
41
- const method = buf.readUInt16LE(p + 10);
42
- const mtime = buf.readUInt16LE(p + 12);
43
- const mdate = buf.readUInt16LE(p + 14);
44
- const crc = buf.readUInt32LE(p + 16);
45
- const csize = buf.readUInt32LE(p + 20);
46
- const usize = buf.readUInt32LE(p + 24);
47
- const nameLen = buf.readUInt16LE(p + 28);
48
- const extraLen = buf.readUInt16LE(p + 30);
49
- const commentLen = buf.readUInt16LE(p + 32);
50
- const attrs = buf.readUInt32LE(p + 38);
51
- const locOfs = buf.readUInt32LE(p + 42);
52
- const name = buf.toString('utf8', p + 46, p + 46 + nameLen);
53
- if ((flags & 0x1) !== 0) throw new Error('encrypted zip not supported');
54
- if (csize === 0xffffffff || usize === 0xffffffff) throw new Error('zip64 not supported');
55
-
56
- // local header:算出这个 entry 的完整原始片段 [local header + data (+ descriptor)]
57
- if (buf.readUInt32LE(locOfs) !== LOC_SIG) throw new Error(`bad local header for ${name}`);
58
- const locNameLen = buf.readUInt16LE(locOfs + 26);
59
- const locExtraLen = buf.readUInt16LE(locOfs + 28);
60
- const dataStart = locOfs + 30 + locNameLen + locExtraLen;
61
- let rawEnd = dataStart + csize;
62
- if (flags & 0x8) {
63
- // data descriptor: 可带可不带签名 0x08074b50
64
- if (buf.readUInt32LE(rawEnd) === 0x08074b50) rawEnd += 16; else rawEnd += 12;
65
- }
66
- const entry = {
67
- name, flags, method, mtime, mdate, crc, csize, usize, attrs,
68
- cenExtra: Buffer.from(buf.subarray(p + 46 + nameLen, p + 46 + nameLen + extraLen)),
69
- comment: Buffer.from(buf.subarray(p + 46 + nameLen + extraLen, p + 46 + nameLen + extraLen + commentLen)),
70
- locRaw: Buffer.from(buf.subarray(locOfs, rawEnd)),
71
- dataOfs: dataStart - locOfs,
72
- replaced: null,
73
- };
74
- entries.set(name, entry);
75
- order.push(name);
76
- p += 46 + nameLen + extraLen + commentLen;
77
- }
78
- return { entries, order };
79
- }
80
-
81
- export function entryData(zip, name) {
82
- const e = zip.entries.get(name);
83
- if (!e) return null;
84
- if (e.replaced) return e.replaced;
85
- const raw = e.locRaw.subarray(e.dataOfs, e.dataOfs + e.csize);
86
- if (e.method === 0) return Buffer.from(raw);
87
- if (e.method === 8) return inflateRawSync(raw);
88
- throw new Error(`unsupported method ${e.method} for ${name}`);
89
- }
90
-
91
- export function replaceEntry(zip, name, content) {
92
- const e = zip.entries.get(name);
93
- if (!e) throw new Error(`no such entry: ${name}`);
94
- e.replaced = Buffer.isBuffer(content) ? content : Buffer.from(content, 'utf8');
95
- }
96
-
97
- export function addEntry(zip, name, content) {
98
- if (zip.entries.has(name)) return replaceEntry(zip, name, content);
99
- const e = {
100
- name, flags: 0x800 /* utf8 name */, method: 8, mtime: 0, mdate: 0x2100 /* 1996-08-00 stable */,
101
- crc: 0, csize: 0, usize: 0, attrs: 0,
102
- cenExtra: Buffer.alloc(0), comment: Buffer.alloc(0),
103
- locRaw: null, dataOfs: 0,
104
- replaced: Buffer.isBuffer(content) ? content : Buffer.from(content, 'utf8'),
105
- };
106
- zip.entries.set(name, e);
107
- zip.order.push(name);
108
- }
109
-
110
- export function writeZip(zip) {
111
- const chunks = [];
112
- const cen = [];
113
- let offset = 0;
114
- for (const name of zip.order) {
115
- const e = zip.entries.get(name);
116
- let locBuf; let crc; let csize; let usize; let method; let flags;
117
- if (e.replaced == null) {
118
- locBuf = e.locRaw; // 原样拷贝(保真核心)
119
- crc = e.crc; csize = e.csize; usize = e.usize; method = e.method; flags = e.flags;
120
- } else {
121
- const data = e.replaced;
122
- const packed = deflateRawSync(data, { level: 9 });
123
- method = packed.length < data.length ? 8 : 0;
124
- const body = method === 8 ? packed : data;
125
- crc = crc32(data) >>> 0;
126
- csize = body.length; usize = data.length;
127
- flags = 0x800; // utf8 filename,不用 descriptor
128
- const nameBuf = Buffer.from(name, 'utf8');
129
- const head = Buffer.alloc(30);
130
- head.writeUInt32LE(LOC_SIG, 0);
131
- head.writeUInt16LE(20, 4); // version needed
132
- head.writeUInt16LE(flags, 6);
133
- head.writeUInt16LE(method, 8);
134
- head.writeUInt16LE(e.mtime, 10);
135
- head.writeUInt16LE(e.mdate, 12);
136
- head.writeUInt32LE(crc, 14);
137
- head.writeUInt32LE(csize, 18);
138
- head.writeUInt32LE(usize, 22);
139
- head.writeUInt16LE(nameBuf.length, 26);
140
- head.writeUInt16LE(0, 28);
141
- locBuf = Buffer.concat([head, nameBuf, body]);
142
- }
143
- const nameBuf = Buffer.from(name, 'utf8');
144
- const c = Buffer.alloc(46);
145
- c.writeUInt32LE(CEN_SIG, 0);
146
- c.writeUInt16LE(20, 4); c.writeUInt16LE(20, 6);
147
- c.writeUInt16LE(flags, 8);
148
- c.writeUInt16LE(method, 10);
149
- c.writeUInt16LE(e.mtime, 12); c.writeUInt16LE(e.mdate, 14);
150
- c.writeUInt32LE(crc, 16);
151
- c.writeUInt32LE(csize, 20); c.writeUInt32LE(usize, 24);
152
- c.writeUInt16LE(nameBuf.length, 28);
153
- c.writeUInt16LE(e.cenExtra.length, 30);
154
- c.writeUInt16LE(e.comment.length, 32);
155
- c.writeUInt32LE(e.attrs, 38);
156
- c.writeUInt32LE(offset, 42);
157
- cen.push(Buffer.concat([c, nameBuf, e.cenExtra, e.comment]));
158
- chunks.push(locBuf);
159
- offset += locBuf.length;
160
- }
161
- const cenStart = offset;
162
- let cenSize = 0;
163
- for (const c of cen) { chunks.push(c); cenSize += c.length; }
164
- const eocd = Buffer.alloc(22);
165
- eocd.writeUInt32LE(EOCD_SIG, 0);
166
- eocd.writeUInt16LE(zip.order.length, 8);
167
- eocd.writeUInt16LE(zip.order.length, 10);
168
- eocd.writeUInt32LE(cenSize, 12);
169
- eocd.writeUInt32LE(cenStart, 16);
170
- chunks.push(eocd);
171
- return Buffer.concat(chunks);
172
- }
1
+ /**
2
+ * rawzip.js — 面向 docx 的最小 ZIP 读写器,核心卖点是「保真」:
3
+ * 没被替换的 entry,local header + 压缩数据 + data descriptor 原样字节拷贝;
4
+ * 只有被替换/新增的 entry 走 zlib deflateRaw 重压。
5
+ *
6
+ * 覆盖范围 = OOXML 容器实际会遇到的:
7
+ * - 方法 0 (store) / 8 (deflate)
8
+ * - bit3 data descriptor(流式写出的 zip)
9
+ * - 不支持 zip64 / 加密 / 分卷(docx 不会用;遇到即抛错,调用方退回只读)
10
+ */
11
+
12
+ import { deflateRawSync, inflateRawSync } from 'node:zlib';
13
+ import { crc32 } from 'node:zlib';
14
+
15
+ const EOCD_SIG = 0x06054b50;
16
+ const CEN_SIG = 0x02014b50;
17
+ const LOC_SIG = 0x04034b50;
18
+
19
+ /**
20
+ * @param {Buffer} buf
21
+ * @returns {{ entries: Map<string, Entry>, order: string[] }}
22
+ * Entry = { name, method, crc, csize, usize, flags, cenExtra..., locRaw:Buffer|null, replaced:Buffer|null }
23
+ */
24
+ export function readZip(buf) {
25
+ // 找 EOCD(从尾部回扫,容忍 comment)
26
+ let eocd = -1;
27
+ for (let i = buf.length - 22; i >= Math.max(0, buf.length - 22 - 65535); i--) {
28
+ if (buf.readUInt32LE(i) === EOCD_SIG) { eocd = i; break; }
29
+ }
30
+ if (eocd < 0) throw new Error('EOCD not found (not a zip)');
31
+ const count = buf.readUInt16LE(eocd + 10);
32
+ const cenOfs = buf.readUInt32LE(eocd + 16);
33
+ if (cenOfs === 0xffffffff) throw new Error('zip64 not supported');
34
+
35
+ const entries = new Map();
36
+ const order = [];
37
+ let p = cenOfs;
38
+ for (let i = 0; i < count; i++) {
39
+ if (buf.readUInt32LE(p) !== CEN_SIG) throw new Error(`bad central dir at ${p}`);
40
+ const flags = buf.readUInt16LE(p + 8);
41
+ const method = buf.readUInt16LE(p + 10);
42
+ const mtime = buf.readUInt16LE(p + 12);
43
+ const mdate = buf.readUInt16LE(p + 14);
44
+ const crc = buf.readUInt32LE(p + 16);
45
+ const csize = buf.readUInt32LE(p + 20);
46
+ const usize = buf.readUInt32LE(p + 24);
47
+ const nameLen = buf.readUInt16LE(p + 28);
48
+ const extraLen = buf.readUInt16LE(p + 30);
49
+ const commentLen = buf.readUInt16LE(p + 32);
50
+ const attrs = buf.readUInt32LE(p + 38);
51
+ const locOfs = buf.readUInt32LE(p + 42);
52
+ const name = buf.toString('utf8', p + 46, p + 46 + nameLen);
53
+ if ((flags & 0x1) !== 0) throw new Error('encrypted zip not supported');
54
+ if (csize === 0xffffffff || usize === 0xffffffff) throw new Error('zip64 not supported');
55
+
56
+ // local header:算出这个 entry 的完整原始片段 [local header + data (+ descriptor)]
57
+ if (buf.readUInt32LE(locOfs) !== LOC_SIG) throw new Error(`bad local header for ${name}`);
58
+ const locNameLen = buf.readUInt16LE(locOfs + 26);
59
+ const locExtraLen = buf.readUInt16LE(locOfs + 28);
60
+ const dataStart = locOfs + 30 + locNameLen + locExtraLen;
61
+ let rawEnd = dataStart + csize;
62
+ if (flags & 0x8) {
63
+ // data descriptor: 可带可不带签名 0x08074b50
64
+ if (buf.readUInt32LE(rawEnd) === 0x08074b50) rawEnd += 16; else rawEnd += 12;
65
+ }
66
+ const entry = {
67
+ name, flags, method, mtime, mdate, crc, csize, usize, attrs,
68
+ cenExtra: Buffer.from(buf.subarray(p + 46 + nameLen, p + 46 + nameLen + extraLen)),
69
+ comment: Buffer.from(buf.subarray(p + 46 + nameLen + extraLen, p + 46 + nameLen + extraLen + commentLen)),
70
+ locRaw: Buffer.from(buf.subarray(locOfs, rawEnd)),
71
+ dataOfs: dataStart - locOfs,
72
+ replaced: null,
73
+ };
74
+ entries.set(name, entry);
75
+ order.push(name);
76
+ p += 46 + nameLen + extraLen + commentLen;
77
+ }
78
+ return { entries, order };
79
+ }
80
+
81
+ export function entryData(zip, name) {
82
+ const e = zip.entries.get(name);
83
+ if (!e) return null;
84
+ if (e.replaced) return e.replaced;
85
+ const raw = e.locRaw.subarray(e.dataOfs, e.dataOfs + e.csize);
86
+ if (e.method === 0) return Buffer.from(raw);
87
+ if (e.method === 8) return inflateRawSync(raw);
88
+ throw new Error(`unsupported method ${e.method} for ${name}`);
89
+ }
90
+
91
+ export function replaceEntry(zip, name, content) {
92
+ const e = zip.entries.get(name);
93
+ if (!e) throw new Error(`no such entry: ${name}`);
94
+ e.replaced = Buffer.isBuffer(content) ? content : Buffer.from(content, 'utf8');
95
+ }
96
+
97
+ export function addEntry(zip, name, content) {
98
+ if (zip.entries.has(name)) return replaceEntry(zip, name, content);
99
+ const e = {
100
+ name, flags: 0x800 /* utf8 name */, method: 8, mtime: 0, mdate: 0x2100 /* 1996-08-00 stable */,
101
+ crc: 0, csize: 0, usize: 0, attrs: 0,
102
+ cenExtra: Buffer.alloc(0), comment: Buffer.alloc(0),
103
+ locRaw: null, dataOfs: 0,
104
+ replaced: Buffer.isBuffer(content) ? content : Buffer.from(content, 'utf8'),
105
+ };
106
+ zip.entries.set(name, e);
107
+ zip.order.push(name);
108
+ }
109
+
110
+ export function writeZip(zip) {
111
+ const chunks = [];
112
+ const cen = [];
113
+ let offset = 0;
114
+ for (const name of zip.order) {
115
+ const e = zip.entries.get(name);
116
+ let locBuf; let crc; let csize; let usize; let method; let flags;
117
+ if (e.replaced == null) {
118
+ locBuf = e.locRaw; // 原样拷贝(保真核心)
119
+ crc = e.crc; csize = e.csize; usize = e.usize; method = e.method; flags = e.flags;
120
+ } else {
121
+ const data = e.replaced;
122
+ const packed = deflateRawSync(data, { level: 9 });
123
+ method = packed.length < data.length ? 8 : 0;
124
+ const body = method === 8 ? packed : data;
125
+ crc = crc32(data) >>> 0;
126
+ csize = body.length; usize = data.length;
127
+ flags = 0x800; // utf8 filename,不用 descriptor
128
+ const nameBuf = Buffer.from(name, 'utf8');
129
+ const head = Buffer.alloc(30);
130
+ head.writeUInt32LE(LOC_SIG, 0);
131
+ head.writeUInt16LE(20, 4); // version needed
132
+ head.writeUInt16LE(flags, 6);
133
+ head.writeUInt16LE(method, 8);
134
+ head.writeUInt16LE(e.mtime, 10);
135
+ head.writeUInt16LE(e.mdate, 12);
136
+ head.writeUInt32LE(crc, 14);
137
+ head.writeUInt32LE(csize, 18);
138
+ head.writeUInt32LE(usize, 22);
139
+ head.writeUInt16LE(nameBuf.length, 26);
140
+ head.writeUInt16LE(0, 28);
141
+ locBuf = Buffer.concat([head, nameBuf, body]);
142
+ }
143
+ const nameBuf = Buffer.from(name, 'utf8');
144
+ const c = Buffer.alloc(46);
145
+ c.writeUInt32LE(CEN_SIG, 0);
146
+ c.writeUInt16LE(20, 4); c.writeUInt16LE(20, 6);
147
+ c.writeUInt16LE(flags, 8);
148
+ c.writeUInt16LE(method, 10);
149
+ c.writeUInt16LE(e.mtime, 12); c.writeUInt16LE(e.mdate, 14);
150
+ c.writeUInt32LE(crc, 16);
151
+ c.writeUInt32LE(csize, 20); c.writeUInt32LE(usize, 24);
152
+ c.writeUInt16LE(nameBuf.length, 28);
153
+ c.writeUInt16LE(e.cenExtra.length, 30);
154
+ c.writeUInt16LE(e.comment.length, 32);
155
+ c.writeUInt32LE(e.attrs, 38);
156
+ c.writeUInt32LE(offset, 42);
157
+ cen.push(Buffer.concat([c, nameBuf, e.cenExtra, e.comment]));
158
+ chunks.push(locBuf);
159
+ offset += locBuf.length;
160
+ }
161
+ const cenStart = offset;
162
+ let cenSize = 0;
163
+ for (const c of cen) { chunks.push(c); cenSize += c.length; }
164
+ const eocd = Buffer.alloc(22);
165
+ eocd.writeUInt32LE(EOCD_SIG, 0);
166
+ eocd.writeUInt16LE(zip.order.length, 8);
167
+ eocd.writeUInt16LE(zip.order.length, 10);
168
+ eocd.writeUInt32LE(cenSize, 12);
169
+ eocd.writeUInt32LE(cenStart, 16);
170
+ chunks.push(eocd);
171
+ return Buffer.concat(chunks);
172
+ }
@@ -1,92 +1,92 @@
1
- /**
2
- * render.js — docx → PDF → PNG 的 QC 渲染链路。
3
- *
4
- * ⭐**必须是异步的。** 第一版写成 `execFileSync`,实测一份 55 页中文文档
5
- * 把事件循环**整整堵死 11.7 秒** —— 期间全站所有 HTTP、WebSocket 心跳、
6
- * agent 流式输出一起停摆。而触发它的不是"用户主动导出",是**一张卡片滚进
7
- * 视口**:画布上有人放了一份长文档,所有人的站就冻一次。
8
- * node 是单线程,凡是要等外部进程的地方一律不许用 *Sync。
9
- *
10
- * 三条已实证的军规:
11
- * 1. per-run 独立 LO profile + 可写 HOME(多会话并发抢默认 profile 是必然事件)
12
- * 2. soffice 吃不下中文文件名 → 进场先拷成 ASCII 名
13
- * 3. FONTCONFIG_FILE 指向 fonts/nodesign-cjk.conf(中文字体替身映射,
14
- * per-run 生效,不污染系统 fontconfig)
15
- *
16
- * ⚠️ 失败也要收摊:`mkdtemp` 一建出来就进 try,任何一步抛都在 finally 里删掉
17
- * scratch —— 第一版是"抛了就拿不到 res,调用方无从 cleanup",每失败一次漏
18
- * 一个约 870KB 的目录在 /tmp(跟数据库抢同一个根分区)。
19
- */
20
-
21
- import { promises as fs } from 'node:fs';
22
- import { execFile } from 'node:child_process';
23
- import { promisify } from 'node:util';
24
- import { tmpdir } from 'node:os';
25
- import { join, dirname } from 'node:path';
26
- import { fileURLToPath } from 'node:url';
27
- // 二进制路径走启动探测(Mac/Win 的 LibreOffice 不在 PATH;没探到就原名让 ENOENT 原样报)
28
- import { resolveBinary } from '../../runtime/capabilities.js';
29
-
30
- const run = promisify(execFile);
31
-
32
- const HERE = dirname(fileURLToPath(import.meta.url));
33
- export const FONTCONF = join(HERE, 'fonts', 'nodesign-cjk.conf');
34
-
35
- /** 单份文档的渲染上限。超时的天花板同时也是"最坏情况占用一个 soffice 槽多久" */
36
- const SOFFICE_TIMEOUT = 120_000;
37
- const PDFTOPPM_TIMEOUT = 60_000;
38
-
39
- async function exists(p) {
40
- try { await fs.access(p); return true; } catch { return false; }
41
- }
42
-
43
- /**
44
- * @param {string} docxPath
45
- * @param {object} opts { pngPages?: [from,to] | true, dpi?: number, timeoutMs?: number }
46
- * @returns {Promise<{ pdf: string, pngs: string[], scratch: string, ms: number }>}
47
- * 产物落在独立 scratch 目录里,**调用方成功拿到之后自己收**(cleanupRender)。
48
- * 失败的话这里已经收干净了,调用方不用管。
49
- */
50
- export async function renderDocx(docxPath, opts = {}) {
51
- const scratch = await fs.mkdtemp(join(tmpdir(), 'ndocx-'));
52
- const t0 = Date.now();
53
- try {
54
- const inFile = join(scratch, 'in.docx'); // 军规2:ASCII 文件名
55
- await fs.copyFile(docxPath, inFile);
56
- const env = {
57
- ...process.env,
58
- HOME: scratch, // 军规1:可写 HOME
59
- FONTCONFIG_FILE: FONTCONF, // 军规3:字体替身
60
- };
61
- await run(resolveBinary('libreoffice', 'soffice'), [
62
- `-env:UserInstallation=file://${scratch}/loprofile`, // 军规1:独立 profile
63
- '--headless', '--convert-to', 'pdf', '--outdir', scratch, inFile,
64
- ], { env, timeout: opts.timeoutMs ?? SOFFICE_TIMEOUT });
65
-
66
- const pdf = join(scratch, 'in.pdf');
67
- if (!await exists(pdf)) throw new Error('soffice produced no pdf');
68
-
69
- const pngs = [];
70
- if (opts.pngPages) {
71
- const args = ['-png', '-r', String(opts.dpi ?? 120)];
72
- if (Array.isArray(opts.pngPages)) {
73
- args.push('-f', String(opts.pngPages[0]), '-l', String(opts.pngPages[1]));
74
- }
75
- await run(resolveBinary('poppler', 'pdftoppm'), [...args, pdf, join(scratch, 'page')], { timeout: PDFTOPPM_TIMEOUT });
76
- // pdftoppm 按总页数补零(55 页 → page-01…page-55),宽度统一,
77
- // 所以字典序等于数字序 —— 这条核过,不是碰运气
78
- for (const f of (await fs.readdir(scratch)).sort()) {
79
- if (f.startsWith('page-') && f.endsWith('.png')) pngs.push(join(scratch, f));
80
- }
81
- }
82
- return { pdf, pngs, scratch, ms: Date.now() - t0 };
83
- } catch (err) {
84
- await fs.rm(scratch, { recursive: true, force: true }).catch(() => {});
85
- throw err;
86
- }
87
- }
88
-
89
- export async function cleanupRender(res) {
90
- if (!res?.scratch) return;
91
- await fs.rm(res.scratch, { recursive: true, force: true }).catch(() => {});
92
- }
1
+ /**
2
+ * render.js — docx → PDF → PNG 的 QC 渲染链路。
3
+ *
4
+ * ⭐**必须是异步的。** 第一版写成 `execFileSync`,实测一份 55 页中文文档
5
+ * 把事件循环**整整堵死 11.7 秒** —— 期间全站所有 HTTP、WebSocket 心跳、
6
+ * agent 流式输出一起停摆。而触发它的不是"用户主动导出",是**一张卡片滚进
7
+ * 视口**:画布上有人放了一份长文档,所有人的站就冻一次。
8
+ * node 是单线程,凡是要等外部进程的地方一律不许用 *Sync。
9
+ *
10
+ * 三条已实证的军规:
11
+ * 1. per-run 独立 LO profile + 可写 HOME(多会话并发抢默认 profile 是必然事件)
12
+ * 2. soffice 吃不下中文文件名 → 进场先拷成 ASCII 名
13
+ * 3. FONTCONFIG_FILE 指向 fonts/nodesign-cjk.conf(中文字体替身映射,
14
+ * per-run 生效,不污染系统 fontconfig)
15
+ *
16
+ * ⚠️ 失败也要收摊:`mkdtemp` 一建出来就进 try,任何一步抛都在 finally 里删掉
17
+ * scratch —— 第一版是"抛了就拿不到 res,调用方无从 cleanup",每失败一次漏
18
+ * 一个约 870KB 的目录在 /tmp(跟数据库抢同一个根分区)。
19
+ */
20
+
21
+ import { promises as fs } from 'node:fs';
22
+ import { execFile } from 'node:child_process';
23
+ import { promisify } from 'node:util';
24
+ import { tmpdir } from 'node:os';
25
+ import { join, dirname } from 'node:path';
26
+ import { fileURLToPath } from 'node:url';
27
+ // 二进制路径走启动探测(Mac/Win 的 LibreOffice 不在 PATH;没探到就原名让 ENOENT 原样报)
28
+ import { resolveBinary } from '../../runtime/capabilities.js';
29
+
30
+ const run = promisify(execFile);
31
+
32
+ const HERE = dirname(fileURLToPath(import.meta.url));
33
+ export const FONTCONF = join(HERE, 'fonts', 'nodesign-cjk.conf');
34
+
35
+ /** 单份文档的渲染上限。超时的天花板同时也是"最坏情况占用一个 soffice 槽多久" */
36
+ const SOFFICE_TIMEOUT = 120_000;
37
+ const PDFTOPPM_TIMEOUT = 60_000;
38
+
39
+ async function exists(p) {
40
+ try { await fs.access(p); return true; } catch { return false; }
41
+ }
42
+
43
+ /**
44
+ * @param {string} docxPath
45
+ * @param {object} opts { pngPages?: [from,to] | true, dpi?: number, timeoutMs?: number }
46
+ * @returns {Promise<{ pdf: string, pngs: string[], scratch: string, ms: number }>}
47
+ * 产物落在独立 scratch 目录里,**调用方成功拿到之后自己收**(cleanupRender)。
48
+ * 失败的话这里已经收干净了,调用方不用管。
49
+ */
50
+ export async function renderDocx(docxPath, opts = {}) {
51
+ const scratch = await fs.mkdtemp(join(tmpdir(), 'ndocx-'));
52
+ const t0 = Date.now();
53
+ try {
54
+ const inFile = join(scratch, 'in.docx'); // 军规2:ASCII 文件名
55
+ await fs.copyFile(docxPath, inFile);
56
+ const env = {
57
+ ...process.env,
58
+ HOME: scratch, // 军规1:可写 HOME
59
+ FONTCONFIG_FILE: FONTCONF, // 军规3:字体替身
60
+ };
61
+ await run(resolveBinary('libreoffice', 'soffice'), [
62
+ `-env:UserInstallation=file://${scratch}/loprofile`, // 军规1:独立 profile
63
+ '--headless', '--convert-to', 'pdf', '--outdir', scratch, inFile,
64
+ ], { env, timeout: opts.timeoutMs ?? SOFFICE_TIMEOUT });
65
+
66
+ const pdf = join(scratch, 'in.pdf');
67
+ if (!await exists(pdf)) throw new Error('soffice produced no pdf');
68
+
69
+ const pngs = [];
70
+ if (opts.pngPages) {
71
+ const args = ['-png', '-r', String(opts.dpi ?? 120)];
72
+ if (Array.isArray(opts.pngPages)) {
73
+ args.push('-f', String(opts.pngPages[0]), '-l', String(opts.pngPages[1]));
74
+ }
75
+ await run(resolveBinary('poppler', 'pdftoppm'), [...args, pdf, join(scratch, 'page')], { timeout: PDFTOPPM_TIMEOUT });
76
+ // pdftoppm 按总页数补零(55 页 → page-01…page-55),宽度统一,
77
+ // 所以字典序等于数字序 —— 这条核过,不是碰运气
78
+ for (const f of (await fs.readdir(scratch)).sort()) {
79
+ if (f.startsWith('page-') && f.endsWith('.png')) pngs.push(join(scratch, f));
80
+ }
81
+ }
82
+ return { pdf, pngs, scratch, ms: Date.now() - t0 };
83
+ } catch (err) {
84
+ await fs.rm(scratch, { recursive: true, force: true }).catch(() => {});
85
+ throw err;
86
+ }
87
+ }
88
+
89
+ export async function cleanupRender(res) {
90
+ if (!res?.scratch) return;
91
+ await fs.rm(res.scratch, { recursive: true, force: true }).catch(() => {});
92
+ }