@capyclaw/offline-ddr0 0.0.0-stage → 1.9.1

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 (300) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +441 -2
  3. package/client/client.js +603 -0
  4. package/cordis.patch.yml +13 -0
  5. package/docs/STORAGE-MIGRATION.md +136 -0
  6. package/lib/atomic.mjs +40 -0
  7. package/lib/http-cache.mjs +42 -0
  8. package/lib/inbox.mjs +64 -0
  9. package/lib/media.mjs +242 -0
  10. package/lib/migrate-json.mjs +101 -0
  11. package/lib/mirror-index.mjs +105 -0
  12. package/lib/mirror.mjs +123 -0
  13. package/lib/proxy.mjs +188 -0
  14. package/lib/remote.mjs +158 -0
  15. package/lib/render.mjs +93 -0
  16. package/lib/repository.mjs +71 -0
  17. package/lib/store.mjs +610 -0
  18. package/lib/surface.mjs +74 -0
  19. package/lib/sync-ui.mjs +108 -0
  20. package/lib/sync.mjs +433 -0
  21. package/lib/wire.mjs +211 -0
  22. package/lib/workstore-repository.mjs +316 -0
  23. package/lib/workstore.mjs +348 -0
  24. package/mirror/files/00001.html +2045 -0
  25. package/mirror/files/00002.json +1 -0
  26. package/mirror/files/00003.json +1 -0
  27. package/mirror/files/00004.json +1 -0
  28. package/mirror/files/00005.json +1 -0
  29. package/mirror/files/00006.json +1 -0
  30. package/mirror/files/00007.json +1 -0
  31. package/mirror/files/00008.json +1 -0
  32. package/mirror/files/00009.json +1 -0
  33. package/mirror/files/00010.json +1 -0
  34. package/mirror/files/00011.json +1 -0
  35. package/mirror/files/00012.json +1 -0
  36. package/mirror/files/00013.json +1 -0
  37. package/mirror/files/00014.json +1 -0
  38. package/mirror/files/00015.json +1 -0
  39. package/mirror/files/00016.json +1 -0
  40. package/mirror/files/00017.json +1 -0
  41. package/mirror/files/00018.json +1 -0
  42. package/mirror/files/00019.json +1 -0
  43. package/mirror/files/00020.json +1 -0
  44. package/mirror/files/00021.json +1 -0
  45. package/mirror/files/00022.json +1 -0
  46. package/mirror/files/00023.js +205 -0
  47. package/mirror/files/00024.json +1 -0
  48. package/mirror/files/00025.json +1 -0
  49. package/mirror/files/00026.json +1 -0
  50. package/mirror/files/00027.js +51 -0
  51. package/mirror/files/00028.json +1 -0
  52. package/mirror/files/00029.json +1 -0
  53. package/mirror/files/00030.json +1 -0
  54. package/mirror/files/00031.json +1 -0
  55. package/mirror/files/00032.json +1 -0
  56. package/mirror/files/00033.json +1 -0
  57. package/mirror/files/00034.json +1 -0
  58. package/mirror/files/00035.json +1 -0
  59. package/mirror/files/00036.json +1 -0
  60. package/mirror/files/00037.json +1 -0
  61. package/mirror/files/00038.json +1 -0
  62. package/mirror/files/00039.json +1 -0
  63. package/mirror/files/00040.js +2311 -0
  64. package/mirror/files/00041.json +1 -0
  65. package/mirror/files/00042.json +1 -0
  66. package/mirror/files/00043.json +1 -0
  67. package/mirror/files/00044.json +1 -0
  68. package/mirror/files/00045.js +575 -0
  69. package/mirror/files/00046.json +1 -0
  70. package/mirror/files/00047.js +575 -0
  71. package/mirror/files/00048.js +575 -0
  72. package/mirror/files/00049.json +1 -0
  73. package/mirror/files/00050.json +1 -0
  74. package/mirror/files/00051.json +1 -0
  75. package/mirror/files/00052.json +1 -0
  76. package/mirror/files/00053.js +575 -0
  77. package/mirror/files/00054.json +1 -0
  78. package/mirror/files/00055.json +1 -0
  79. package/mirror/files/00056.js +575 -0
  80. package/mirror/files/00057.json +1 -0
  81. package/mirror/files/00058.json +1 -0
  82. package/mirror/files/00059.js +360 -0
  83. package/mirror/files/00060.json +1 -0
  84. package/mirror/files/00061.json +1 -0
  85. package/mirror/files/00062.json +1 -0
  86. package/mirror/files/00063.json +1 -0
  87. package/mirror/files/00064.json +1 -0
  88. package/mirror/files/00065.js +144 -0
  89. package/mirror/files/00066.js +7 -0
  90. package/mirror/files/00067.json +1 -0
  91. package/mirror/files/00068.json +1 -0
  92. package/mirror/files/00069.json +1 -0
  93. package/mirror/files/00070.json +1 -0
  94. package/mirror/files/00071.json +1 -0
  95. package/mirror/files/00072.json +1 -0
  96. package/mirror/files/00073.js +1 -0
  97. package/mirror/files/00074.json +1 -0
  98. package/mirror/files/00075.json +1 -0
  99. package/mirror/files/00076.json +1 -0
  100. package/mirror/files/00077.js +45 -0
  101. package/mirror/files/00078.js +1348 -0
  102. package/mirror/files/00079.json +1 -0
  103. package/mirror/files/00080.json +1 -0
  104. package/mirror/files/00081.js +1 -0
  105. package/mirror/files/00082.json +1 -0
  106. package/mirror/files/00083.json +1 -0
  107. package/mirror/files/00084.js +7 -0
  108. package/mirror/files/00085.json +1 -0
  109. package/mirror/files/00086.json +1 -0
  110. package/mirror/files/00087.js +7 -0
  111. package/mirror/files/00088.json +1 -0
  112. package/mirror/files/00089.json +1 -0
  113. package/mirror/files/00090.js +7 -0
  114. package/mirror/files/00091.json +1 -0
  115. package/mirror/files/00092.json +1 -0
  116. package/mirror/files/00093.js +102 -0
  117. package/mirror/files/00094.json +1 -0
  118. package/mirror/files/00095.json +1 -0
  119. package/mirror/files/00096.json +1 -0
  120. package/mirror/files/00097.json +1 -0
  121. package/mirror/files/00098.json +1 -0
  122. package/mirror/files/00099.json +1 -0
  123. package/mirror/files/00100.json +1 -0
  124. package/mirror/files/00101.js +102 -0
  125. package/mirror/files/00102.json +1 -0
  126. package/mirror/files/00103.json +1 -0
  127. package/mirror/files/00104.json +1 -0
  128. package/mirror/files/00105.json +1 -0
  129. package/mirror/files/00106.json +1 -0
  130. package/mirror/files/00107.js +110 -0
  131. package/mirror/files/00108.json +1 -0
  132. package/mirror/files/00109.json +1 -0
  133. package/mirror/files/00110.json +1 -0
  134. package/mirror/files/00111.json +1 -0
  135. package/mirror/files/00112.json +1 -0
  136. package/mirror/files/00113.json +1 -0
  137. package/mirror/files/00114.json +1 -0
  138. package/mirror/files/00115.json +1 -0
  139. package/mirror/files/00116.json +1 -0
  140. package/mirror/files/00117.json +1 -0
  141. package/mirror/files/00118.json +1 -0
  142. package/mirror/files/00119.json +1 -0
  143. package/mirror/files/00120.json +1 -0
  144. package/mirror/files/00121.json +1 -0
  145. package/mirror/files/00122.json +1 -0
  146. package/mirror/files/00123.json +1 -0
  147. package/mirror/files/00124.json +1 -0
  148. package/mirror/files/00125.json +1 -0
  149. package/mirror/files/00126.json +1 -0
  150. package/mirror/files/00127.json +1 -0
  151. package/mirror/files/00128.json +1 -0
  152. package/mirror/files/00129.json +1 -0
  153. package/mirror/files/00130.json +1 -0
  154. package/mirror/files/00131.json +1 -0
  155. package/mirror/files/00132.js +110 -0
  156. package/mirror/files/00133.json +1 -0
  157. package/mirror/files/00134.json +1 -0
  158. package/mirror/files/00135.json +1 -0
  159. package/mirror/files/00136.json +1 -0
  160. package/mirror/files/00137.js +26 -0
  161. package/mirror/files/00138.json +1 -0
  162. package/mirror/files/00139.json +1 -0
  163. package/mirror/files/00140.json +1 -0
  164. package/mirror/files/00141.json +1 -0
  165. package/mirror/files/00142.json +1 -0
  166. package/mirror/files/00143.json +1 -0
  167. package/mirror/files/00144.js +26 -0
  168. package/mirror/files/00145.json +1 -0
  169. package/mirror/files/00146.json +1 -0
  170. package/mirror/files/00147.js +29 -0
  171. package/mirror/files/00148.json +1 -0
  172. package/mirror/files/00149.json +1 -0
  173. package/mirror/files/00150.js +23 -0
  174. package/mirror/files/00151.json +1 -0
  175. package/mirror/files/00152.json +1 -0
  176. package/mirror/files/00153.js +719 -0
  177. package/mirror/files/00154.js +9575 -0
  178. package/mirror/files/00155.js +108 -0
  179. package/mirror/files/00156.js +379 -0
  180. package/mirror/files/00157.js +48 -0
  181. package/mirror/files/00158.json +16 -0
  182. package/mirror/manifest.json +1582 -0
  183. package/node_modules/@capyclaw/asset-service/README.md +48 -0
  184. package/node_modules/@capyclaw/asset-service/cordis.patch.yml +4 -0
  185. package/node_modules/@capyclaw/asset-service/dsh-suite.json +29 -0
  186. package/node_modules/@capyclaw/asset-service/lib/host.mjs +14 -0
  187. package/node_modules/@capyclaw/asset-service/package.json +59 -0
  188. package/node_modules/@capyclaw/asset-service/src/index.mjs +310 -0
  189. package/node_modules/@capyclaw/foundation/README.md +51 -0
  190. package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
  191. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
  192. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  193. package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
  194. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  195. package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
  196. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  197. package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
  198. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  199. package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
  200. package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
  201. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  202. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  203. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  204. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  205. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  206. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
  207. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  208. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  209. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  210. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  211. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  212. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  213. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  214. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  215. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
  216. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
  217. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
  218. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
  219. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  220. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
  221. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  222. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  223. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  224. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
  225. package/node_modules/@capyclaw/foundation/package.json +53 -0
  226. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  227. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  228. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  229. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  230. package/node_modules/@capyclaw/plugin-kit/package.json +71 -0
  231. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  232. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  233. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +132 -0
  234. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
  235. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
  236. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  237. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  238. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  239. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  240. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  241. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  242. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  243. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  244. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  245. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  246. package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
  247. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  248. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  249. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  250. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  251. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  252. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  253. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  254. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  255. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  256. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +664 -0
  257. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  258. package/node_modules/@capyclaw/service-host/README.md +86 -0
  259. package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
  260. package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
  261. package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
  262. package/node_modules/@capyclaw/service-host/package.json +60 -0
  263. package/node_modules/@capyclaw/service-host/src/index.mjs +570 -0
  264. package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
  265. package/node_modules/@capyclaw/suite-kit/README.md +289 -0
  266. package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
  267. package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
  268. package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
  269. package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
  270. package/node_modules/@capyclaw/suite-kit/package.json +58 -0
  271. package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
  272. package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
  273. package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
  274. package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
  275. package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
  276. package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
  277. package/node_modules/@capyclaw/suite-kit/src/host.mjs +230 -0
  278. package/node_modules/@capyclaw/suite-kit/src/index.mjs +24 -0
  279. package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
  280. package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
  281. package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
  282. package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
  283. package/node_modules/@capyclaw/suite-kit/suite.schema.json +128 -0
  284. package/node_modules/@capyclaw/work-store/README.md +83 -0
  285. package/node_modules/@capyclaw/work-store/STANDARD.md +263 -0
  286. package/node_modules/@capyclaw/work-store/cordis.patch.yml +3 -0
  287. package/node_modules/@capyclaw/work-store/dsh-suite.json +33 -0
  288. package/node_modules/@capyclaw/work-store/icon.svg +7 -0
  289. package/node_modules/@capyclaw/work-store/lib/host.mjs +21 -0
  290. package/node_modules/@capyclaw/work-store/package.json +68 -0
  291. package/node_modules/@capyclaw/work-store/src/backend.mjs +90 -0
  292. package/node_modules/@capyclaw/work-store/src/db.mjs +199 -0
  293. package/node_modules/@capyclaw/work-store/src/envelope.mjs +132 -0
  294. package/node_modules/@capyclaw/work-store/src/index.mjs +571 -0
  295. package/node_modules/@capyclaw/work-store/src/interop.mjs +70 -0
  296. package/node_modules/@capyclaw/work-store/src/refs.mjs +99 -0
  297. package/node_modules/@capyclaw/work-store/src/routes.mjs +135 -0
  298. package/package.json +82 -4
  299. package/plugin.mjs +644 -0
  300. package/static/index.html +2068 -0
@@ -0,0 +1,181 @@
1
+ /**
2
+ * 极简跨平台 ZIP 读写(store + deflate 读,store 写;无外部依赖)——全仓单一真源。
3
+ *
4
+ * 之前三处实现各持一份(plugin-development 写 / tools-plugin-release 读 /
5
+ * @capyclaw/suite-kit zip-read 读),本模块合并之。旧位置保留兼容 shim。
6
+ *
7
+ * 写入:store 模式(内容不压缩,跨平台稳定,产物可被任何标准读取器读)。
8
+ * 读取:支持 store(0) 与 deflate(8);接受 Buffer 或文件路径——本地文件与
9
+ * 远端下载源共用同一条校验/解包路径。
10
+ */
11
+ import fs from 'node:fs'
12
+ import path from 'node:path'
13
+ import zlib from 'node:zlib'
14
+
15
+ /* ------------------------------------------------------------------ 写入 */
16
+
17
+ const CRC_TABLE = (() => {
18
+ const table = new Uint32Array(256)
19
+ for (let n = 0; n < 256; n++) {
20
+ let c = n
21
+ for (let k = 0; k < 8; k++) c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1)
22
+ table[n] = c >>> 0
23
+ }
24
+ return table
25
+ })()
26
+
27
+ function crc32(buffer) {
28
+ let crc = 0xffffffff
29
+ for (let i = 0; i < buffer.length; i++) crc = CRC_TABLE[(crc ^ buffer[i]) & 0xff] ^ (crc >>> 8)
30
+ return (crc ^ 0xffffffff) >>> 0
31
+ }
32
+
33
+ function dosDateTime(date = new Date()) {
34
+ const year = Math.max(1980, date.getFullYear())
35
+ const time = ((date.getHours() & 0x1f) << 11) | ((date.getMinutes() & 0x3f) << 5) | (Math.floor(date.getSeconds() / 2) & 0x1f)
36
+ const day = (((year - 1980) & 0x7f) << 9) | (((date.getMonth() + 1) & 0x0f) << 5) | (date.getDate() & 0x1f)
37
+ return { time, day }
38
+ }
39
+
40
+ function walkFiles(dir, base = dir) {
41
+ const out = []
42
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
43
+ const full = path.join(dir, entry.name)
44
+ if (entry.isDirectory()) out.push(...walkFiles(full, base))
45
+ else if (entry.isFile()) out.push(path.relative(base, full).replace(/\\/g, '/'))
46
+ }
47
+ return out.sort()
48
+ }
49
+
50
+ /** 把目录写成 store 模式 ZIP。返回打包的相对路径列表。 */
51
+ export function writeZipFromDir(sourceDir, outFile) {
52
+ const files = walkFiles(sourceDir)
53
+ const now = dosDateTime()
54
+ const localParts = []
55
+ const centralParts = []
56
+ let offset = 0
57
+
58
+ for (const name of files) {
59
+ const data = fs.readFileSync(path.join(sourceDir, name))
60
+ const nameBytes = Buffer.from(name, 'utf8')
61
+ const crc = crc32(data)
62
+
63
+ const local = Buffer.alloc(30)
64
+ local.writeUInt32LE(0x04034b50, 0)
65
+ local.writeUInt16LE(20, 4)
66
+ local.writeUInt16LE(0x0800, 6)
67
+ local.writeUInt16LE(0, 8)
68
+ local.writeUInt16LE(now.time, 10)
69
+ local.writeUInt16LE(now.day, 12)
70
+ local.writeUInt32LE(crc, 14)
71
+ local.writeUInt32LE(data.length, 18)
72
+ local.writeUInt32LE(data.length, 22)
73
+ local.writeUInt16LE(nameBytes.length, 26)
74
+ local.writeUInt16LE(0, 28)
75
+ localParts.push(local, nameBytes, data)
76
+
77
+ const central = Buffer.alloc(46)
78
+ central.writeUInt32LE(0x02014b50, 0)
79
+ central.writeUInt16LE(20, 4)
80
+ central.writeUInt16LE(20, 6)
81
+ central.writeUInt16LE(0x0800, 8)
82
+ central.writeUInt16LE(0, 10)
83
+ central.writeUInt16LE(now.time, 12)
84
+ central.writeUInt16LE(now.day, 14)
85
+ central.writeUInt32LE(crc, 16)
86
+ central.writeUInt32LE(data.length, 20)
87
+ central.writeUInt32LE(data.length, 24)
88
+ central.writeUInt16LE(nameBytes.length, 28)
89
+ central.writeUInt16LE(0, 30)
90
+ central.writeUInt16LE(0, 32)
91
+ central.writeUInt16LE(0, 34)
92
+ central.writeUInt16LE(0, 36)
93
+ central.writeUInt32LE(0, 38)
94
+ central.writeUInt32LE(offset, 42)
95
+ centralParts.push(central, nameBytes)
96
+
97
+ offset += local.length + nameBytes.length + data.length
98
+ }
99
+
100
+ const centralBuffer = Buffer.concat(centralParts)
101
+ const eocd = Buffer.alloc(22)
102
+ eocd.writeUInt32LE(0x06054b50, 0)
103
+ eocd.writeUInt16LE(0, 4)
104
+ eocd.writeUInt16LE(0, 6)
105
+ eocd.writeUInt16LE(files.length, 8)
106
+ eocd.writeUInt16LE(files.length, 10)
107
+ eocd.writeUInt32LE(centralBuffer.length, 12)
108
+ eocd.writeUInt32LE(offset, 16)
109
+ eocd.writeUInt16LE(0, 20)
110
+
111
+ fs.mkdirSync(path.dirname(outFile), { recursive: true })
112
+ fs.writeFileSync(outFile, Buffer.concat([...localParts, centralBuffer, eocd]))
113
+ return files
114
+ }
115
+
116
+ /* ------------------------------------------------------------------ 读取 */
117
+
118
+ function locateEocd(data, label) {
119
+ for (let i = data.length - 22; i >= 0; i--) {
120
+ if (data.readUInt32LE(i) === 0x06054b50) return i
121
+ }
122
+ throw new Error(`不是有效的 ZIP: ${label}`)
123
+ }
124
+
125
+ /**
126
+ * 列出 ZIP 全部条目名。
127
+ * @param {Buffer|string} input - ZIP 字节或文件路径
128
+ */
129
+ export function listZipEntryNames(input, label = 'zip') {
130
+ const data = Buffer.isBuffer(input) ? input : fs.readFileSync(input)
131
+ const eocd = locateEocd(data, label)
132
+ const count = data.readUInt16LE(eocd + 10)
133
+ let offset = data.readUInt32LE(eocd + 16)
134
+ const names = []
135
+ for (let i = 0; i < count; i++) {
136
+ if (data.readUInt32LE(offset) !== 0x02014b50) throw new Error(`ZIP 中央目录损坏: ${label}`)
137
+ const nameLength = data.readUInt16LE(offset + 28)
138
+ const extraLength = data.readUInt16LE(offset + 30)
139
+ const commentLength = data.readUInt16LE(offset + 32)
140
+ names.push(data.subarray(offset + 46, offset + 46 + nameLength).toString('utf8'))
141
+ offset += 46 + nameLength + extraLength + commentLength
142
+ }
143
+ return names
144
+ }
145
+
146
+ /**
147
+ * 从 ZIP 读单个条目的原始字节;不存在返回 undefined。
148
+ * @param {Buffer|string} input - ZIP 字节或文件路径
149
+ */
150
+ export function readZipEntryBuffer(input, entryName, label = 'zip') {
151
+ const data = Buffer.isBuffer(input) ? input : fs.readFileSync(input)
152
+ const eocd = locateEocd(data, label)
153
+ const count = data.readUInt16LE(eocd + 10)
154
+ let offset = data.readUInt32LE(eocd + 16)
155
+ for (let i = 0; i < count; i++) {
156
+ if (data.readUInt32LE(offset) !== 0x02014b50) throw new Error(`ZIP 中央目录损坏: ${label}`)
157
+ const method = data.readUInt16LE(offset + 10)
158
+ const compSize = data.readUInt32LE(offset + 20)
159
+ const nameLength = data.readUInt16LE(offset + 28)
160
+ const extraLength = data.readUInt16LE(offset + 30)
161
+ const commentLength = data.readUInt16LE(offset + 32)
162
+ const localOffset = data.readUInt32LE(offset + 42)
163
+ const name = data.subarray(offset + 46, offset + 46 + nameLength).toString('utf8')
164
+ if (name === entryName) {
165
+ const localNameLength = data.readUInt16LE(localOffset + 26)
166
+ const localExtraLength = data.readUInt16LE(localOffset + 28)
167
+ const start = localOffset + 30 + localNameLength + localExtraLength
168
+ const body = data.subarray(start, start + compSize)
169
+ if (method === 0) return body
170
+ if (method === 8) return zlib.inflateRawSync(body)
171
+ throw new Error(`不支持的 ZIP 压缩方式 ${method}: ${label}`)
172
+ }
173
+ offset += 46 + nameLength + extraLength + commentLength
174
+ }
175
+ return undefined
176
+ }
177
+
178
+ /** 读取 ZIP 中央目录条目名(文件路径版;打包自检/测试用)。 */
179
+ export function listZipEntries(file) {
180
+ return listZipEntryNames(fs.readFileSync(file), file)
181
+ }
@@ -0,0 +1,86 @@
1
+ # @capyclaw/service-host
2
+
3
+ DSH 公共服务宿主插件。功能插件通过 `ctx.serviceHost` 获得统一的 HTTP 运行时,不必再为每个插件复制 relay/路由/健康/指标/锁/静态资源代码。
4
+
5
+ ## 使用
6
+
7
+ ```js
8
+ export const inject = ['serviceHost']
9
+
10
+ export function apply(ctx, config) {
11
+ const app = ctx.serviceHost.createApp({
12
+ id: 'my-plugin',
13
+ title: 'My Plugin',
14
+ version: '1.0.0',
15
+ contractVersion: '1.0.0',
16
+ health: () => ({ ready: true }),
17
+ metrics: () => [['my_items', 3]],
18
+ })
19
+ app.use(myRoutes)
20
+ await app.listen({
21
+ port: config.port,
22
+ hostDir: config.hostDir,
23
+ strictPort: config.strictPort,
24
+ })
25
+ return () => app.close()
26
+ }
27
+ ```
28
+
29
+ 独立运行:
30
+
31
+ ```js
32
+ import { createServiceApp } from '@capyclaw/service-host/src'
33
+ const app = createServiceApp({ id: 'my-plugin' })
34
+ app.get('/ping', (req, res) => res.end('pong'))
35
+ const info = await app.listen({ port: 39500 })
36
+ ```
37
+
38
+ 嵌入 DSH Web:
39
+
40
+ ```js
41
+ await app.listen({ webServer: ctx.webServer, basePath: '/plugins/my-plugin' })
42
+ ```
43
+
44
+ ## 共享宿主(多插件一个端口)
45
+
46
+ ```js
47
+ const host = ctx.serviceHost.createSharedHost({ id: 'dsh-app' })
48
+ host.mount('/plugins/ppt', pptApp)
49
+ host.mount('/plugins/canvas', canvasApp)
50
+ await host.listen({ port: 3080 })
51
+ ```
52
+
53
+ 子 app 仍由 `createServiceApp()` 创建;共享宿主只做前缀分发和统一生命周期。
54
+
55
+ ## 每个会话一个地址(会话段)
56
+
57
+ 挂载前缀之后允许再挂一层可选的会话段,`dispatch()` 会把它剥掉再交给工作台自己的路由:
58
+
59
+ ```
60
+ /capyclaw/ppt/ # 全局入口(不带会话)
61
+ /capyclaw/ppt/s/<会话id>/ # 这个会话的入口:页面就是编辑器本身
62
+ /capyclaw/ppt/s/<会话id>/app.js # 相对资源照旧
63
+ /capyclaw/ppt/s/<会话id>/api/... # 相对 API 照旧
64
+ ```
65
+
66
+ - 剥段之后 `u.pathname` 与不带会话时完全一致,所以**工作台一行代码都不用改**;
67
+ 需要区分会话的路由读 `u.sessionId`(或 `req.sessionId`)。
68
+ - 形状与网关的 `/s/<会话id>/` 一致;sid 只放行 `[A-Za-z0-9._~-]`,不会引入路径分隔歧义。
69
+ - `<prefix>/s/<会话id>` 少了尾斜杠会 301 补上——否则相对地址会被解析成
70
+ `<prefix>/s/<文件名>`,页面里的 `./app.js` 直接 404。
71
+ - 拿不到会话 id 时就不带这一段,行为与以前完全相同。
72
+
73
+ ## 提供什么
74
+
75
+ - `createServiceApp(options)`:路由、静态、健康、指标、生命周期
76
+ - `createServiceHost(config)`:插件侧工厂
77
+ - 标准 `/healthz`:`{ ok, service, name, version, contractVersion, pid, port, uptimeSec, ...业务字段 }`
78
+ - 标准 `/metrics`:`<slug>_up`、`<slug>_uptime_seconds`、`<slug>_port` + 业务指标
79
+ - standalone:端口扫描、`strictPort`、复用探测、单实例锁、discovery 文件、优雅关闭
80
+ - embedded:注册到 `ctx.webServer` 的 `<basePath>` 前缀;每个会话自动获得 `<basePath>/s/<会话id>/` 子树
81
+
82
+ ## 边界
83
+
84
+ - 不拥有业务状态;状态持久化用 `@capyclaw/document-workbench`(store/file-backend)
85
+ - 不拥有资产;资产用 `@capyclaw/asset-service`
86
+ - 不拥有编辑器;编辑器用 `@capyclaw/editor-host`
@@ -0,0 +1,4 @@
1
+ # 由 @capyclaw/service-host 提供;功能插件通过 inject: ['serviceHost'] 或 ctx.get('serviceHost') 使用。
2
+ - insert:
3
+ - id: service-host
4
+ name: "@capyclaw/service-host"
@@ -0,0 +1,29 @@
1
+ {
2
+ "$schema": "../capyclaw-suite-kit/suite.schema.json",
3
+ "name": "dsh-service-host-suite",
4
+ "version": "0.1.0",
5
+ "title": "Service Host",
6
+ "description": "通用服务宿主:HTTP 路由、健康、指标、锁、复用、静态资源。",
7
+ "packages": {
8
+ "@capyclaw/service-host": {
9
+ "role": "provider",
10
+ "version": "^0.1.7",
11
+ "path": ".",
12
+ "provides": {
13
+ "services": [
14
+ "serviceHost"
15
+ ],
16
+ "capabilities": {
17
+ "host.http": "1.0.0"
18
+ }
19
+ }
20
+ }
21
+ },
22
+ "rows": [
23
+ {
24
+ "id": "service-host",
25
+ "name": "@capyclaw/service-host"
26
+ }
27
+ ],
28
+ "checks": []
29
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @capyclaw/service-host 宿主插件:向同进程插件提供 ctx.serviceHost。
3
+ *
4
+ * 功能插件:
5
+ * export const inject = ['serviceHost']
6
+ * const app = ctx.serviceHost.createApp({ id: 'my-plugin', ... })
7
+ * app.use(myRoutes)
8
+ * await app.listen({ port, hostDir })
9
+ */
10
+ import { defineDriver } from '@capyclaw/foundation/abi'
11
+ import { createServiceHost } from '../src/index.mjs'
12
+
13
+ export const { name, inject, apply } = defineDriver({
14
+ name: 'service-host',
15
+ service: 'serviceHost',
16
+ apply(ctx, config = {}) {
17
+ const service = createServiceHost(config)
18
+ const dispose = ctx.provide('serviceHost', service)
19
+ console.log('[service-host] ready')
20
+ return () => { if (typeof dispose === 'function') dispose() }
21
+ },
22
+ })
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@capyclaw/service-host",
3
+ "version": "0.1.7",
4
+ "description": "DSH 公共服务宿主插件:统一 HTTP 路由、健康检查、指标、单实例锁、端口复用与静态资源,向插件提供 ctx.serviceHost。",
5
+ "type": "module",
6
+ "main": "./lib/host.mjs",
7
+ "exports": {
8
+ ".": "./lib/host.mjs",
9
+ "./src": "./src/index.mjs",
10
+ "./package.json": "./package.json",
11
+ "./tuning": "./src/tuning.mjs"
12
+ },
13
+ "engines": {
14
+ "node": ">=18",
15
+ "dsh": "^0.2.0-rc.2"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "dsh": {
21
+ "bundle": {
22
+ "patch": "./cordis.patch.yml"
23
+ },
24
+ "suite": "./dsh-suite.json",
25
+ "category": "service",
26
+ "platform": "both",
27
+ "apiVersion": "1.0.0",
28
+ "driver": {
29
+ "service": "serviceHost"
30
+ }
31
+ },
32
+ "meta": {
33
+ "title": "公共服务宿主",
34
+ "description": "统一 HTTP 路由、健康检查、指标、单实例锁与端口复用,向插件提供 ctx.serviceHost"
35
+ },
36
+ "files": [
37
+ "lib/",
38
+ "src/",
39
+ "cordis.patch.yml",
40
+ "dsh-suite.json",
41
+ "README.md"
42
+ ],
43
+ "license": "MIT",
44
+ "scripts": {
45
+ "test": "node --test test/*.test.mjs"
46
+ },
47
+ "dependencies": {
48
+ "@capyclaw/plugin-kit": "^0.2.0",
49
+ "@capyclaw/foundation": "^0.1.1"
50
+ },
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "git+https://github.com/Maple496/capyclaw.git",
54
+ "directory": "C/packages/capyclaw-service-host"
55
+ },
56
+ "bugs": {
57
+ "url": "https://github.com/Maple496/capyclaw/issues"
58
+ },
59
+ "homepage": "https://github.com/Maple496/capyclaw/tree/main/C/packages/capyclaw-service-host#readme"
60
+ }