dsh-mobile-plugin 0.5.39 → 0.5.41

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 (293) hide show
  1. package/README.md +3 -4
  2. package/docs/extension-authoring.md +215 -42
  3. package/docs/extension-sample/README.md +2 -3
  4. package/extensions/README.md +7 -3
  5. package/extensions/catalog.json +1 -8
  6. package/lib/client.js +1 -40
  7. package/lib/mobile/extension-catalog.js +95 -14
  8. package/lib/mobile/extension-data.js +8 -82
  9. package/lib/mobile/extension-dev.js +56 -0
  10. package/lib/mobile/host-bridge/shims.js +10 -2
  11. package/lib/mobile/permissions.js +5 -3
  12. package/lib/mobile/plugin-catalog.js +501 -0
  13. package/lib/mobile/third-party-extensions.js +49 -2
  14. package/lib/mobile/uploads.js +3 -31
  15. package/lib/panel/index.html +11 -11
  16. package/lib/routes/control-plane.js +0 -41
  17. package/lib/routes/mobile-api.js +13 -19
  18. package/package.json +3 -1
  19. package/extensions/magpie/agentcompact.js +0 -104
  20. package/extensions/magpie/app.css +0 -4902
  21. package/extensions/magpie/app.js +0 -24474
  22. package/extensions/magpie/autobackup.js +0 -185
  23. package/extensions/magpie/autoresetown.js +0 -151
  24. package/extensions/magpie/balfound.js +0 -38
  25. package/extensions/magpie/boot.js +0 -23
  26. package/extensions/magpie/cfgblocked.js +0 -39
  27. package/extensions/magpie/checkinnow.js +0 -111
  28. package/extensions/magpie/claudemode.js +0 -92
  29. package/extensions/magpie/compat.js +0 -41
  30. package/extensions/magpie/context.css +0 -433
  31. package/extensions/magpie/context.js +0 -1576
  32. package/extensions/magpie/ctxtext.js +0 -223
  33. package/extensions/magpie/fonts.js +0 -42
  34. package/extensions/magpie/i18n.js +0 -18942
  35. package/extensions/magpie/icons/ai21.svg +0 -1
  36. package/extensions/magpie/icons/ai302-color.svg +0 -1
  37. package/extensions/magpie/icons/ai360-color.svg +0 -1
  38. package/extensions/magpie/icons/aihubmix-color.svg +0 -1
  39. package/extensions/magpie/icons/aionui.svg +0 -1
  40. package/extensions/magpie/icons/air.svg +0 -4
  41. package/extensions/magpie/icons/alibaba-color.svg +0 -1
  42. package/extensions/magpie/icons/alibabacloud-color.svg +0 -1
  43. package/extensions/magpie/icons/alma.png +0 -0
  44. package/extensions/magpie/icons/anthropic.svg +0 -1
  45. package/extensions/magpie/icons/antigravity-color.svg +0 -1
  46. package/extensions/magpie/icons/anyscale-color.svg +0 -1
  47. package/extensions/magpie/icons/app-alma.png +0 -0
  48. package/extensions/magpie/icons/app-cc-switch.png +0 -0
  49. package/extensions/magpie/icons/apple-touch-icon.png +0 -0
  50. package/extensions/magpie/icons/aside.svg +0 -28
  51. package/extensions/magpie/icons/astrbot.svg +0 -1
  52. package/extensions/magpie/icons/atomcode.png +0 -0
  53. package/extensions/magpie/icons/aws.svg +0 -1
  54. package/extensions/magpie/icons/azure-color.svg +0 -1
  55. package/extensions/magpie/icons/azureai-color.svg +0 -1
  56. package/extensions/magpie/icons/baichuan-color.svg +0 -1
  57. package/extensions/magpie/icons/baidu-color.svg +0 -1
  58. package/extensions/magpie/icons/baiducloud-color.svg +0 -1
  59. package/extensions/magpie/icons/bailian-color.svg +0 -1
  60. package/extensions/magpie/icons/baseten.svg +0 -1
  61. package/extensions/magpie/icons/bedrock-color.svg +0 -1
  62. package/extensions/magpie/icons/burncloud-color.svg +0 -1
  63. package/extensions/magpie/icons/bytedance-color.svg +0 -1
  64. package/extensions/magpie/icons/cerebras.svg +0 -1
  65. package/extensions/magpie/icons/chatglm-color.svg +0 -1
  66. package/extensions/magpie/icons/cherryin-color.svg +0 -1
  67. package/extensions/magpie/icons/chutes.svg +0 -1
  68. package/extensions/magpie/icons/cindy.png +0 -0
  69. package/extensions/magpie/icons/claude-color.svg +0 -1
  70. package/extensions/magpie/icons/claudecode-color.svg +0 -1
  71. package/extensions/magpie/icons/cline.svg +0 -1
  72. package/extensions/magpie/icons/cloudflare-color.svg +0 -1
  73. package/extensions/magpie/icons/codebuddy-color.svg +0 -1
  74. package/extensions/magpie/icons/codex-color.svg +0 -1
  75. package/extensions/magpie/icons/cohere-color.svg +0 -1
  76. package/extensions/magpie/icons/cometapi-color.svg +0 -1
  77. package/extensions/magpie/icons/commandcode.svg +0 -1
  78. package/extensions/magpie/icons/copilot-color.svg +0 -1
  79. package/extensions/magpie/icons/coze.svg +0 -1
  80. package/extensions/magpie/icons/crush.png +0 -0
  81. package/extensions/magpie/icons/curl.svg +0 -1
  82. package/extensions/magpie/icons/cursor.svg +0 -1
  83. package/extensions/magpie/icons/deepinfra-color.svg +0 -1
  84. package/extensions/magpie/icons/deepmind-color.svg +0 -1
  85. package/extensions/magpie/icons/deepseek-color.svg +0 -1
  86. package/extensions/magpie/icons/deveco-color.svg +0 -12
  87. package/extensions/magpie/icons/devin.svg +0 -1
  88. package/extensions/magpie/icons/doubao-color.svg +0 -1
  89. package/extensions/magpie/icons/empryo-color.svg +0 -1
  90. package/extensions/magpie/icons/factory.svg +0 -1
  91. package/extensions/magpie/icons/favicon.svg +0 -1
  92. package/extensions/magpie/icons/featherless-color.svg +0 -1
  93. package/extensions/magpie/icons/fireworks-color.svg +0 -1
  94. package/extensions/magpie/icons/friendli.svg +0 -1
  95. package/extensions/magpie/icons/fx.svg +0 -1
  96. package/extensions/magpie/icons/gemini-color.svg +0 -1
  97. package/extensions/magpie/icons/geminicli-color.svg +0 -1
  98. package/extensions/magpie/icons/gemma-color.svg +0 -1
  99. package/extensions/magpie/icons/giteeai.svg +0 -1
  100. package/extensions/magpie/icons/github.svg +0 -1
  101. package/extensions/magpie/icons/githubcopilot.svg +0 -1
  102. package/extensions/magpie/icons/glama.svg +0 -1
  103. package/extensions/magpie/icons/gmicloud.svg +0 -1
  104. package/extensions/magpie/icons/google-color.svg +0 -1
  105. package/extensions/magpie/icons/googlecloud-color.svg +0 -1
  106. package/extensions/magpie/icons/goose.svg +0 -1
  107. package/extensions/magpie/icons/grok.svg +0 -1
  108. package/extensions/magpie/icons/groq.svg +0 -1
  109. package/extensions/magpie/icons/hanako.png +0 -0
  110. package/extensions/magpie/icons/hermes.svg +0 -1
  111. package/extensions/magpie/icons/huawei-color.svg +0 -1
  112. package/extensions/magpie/icons/huaweicloud-color.svg +0 -1
  113. package/extensions/magpie/icons/huggingface-color.svg +0 -1
  114. package/extensions/magpie/icons/hunyuan-color.svg +0 -1
  115. package/extensions/magpie/icons/hyperbolic-color.svg +0 -1
  116. package/extensions/magpie/icons/icon-512.png +0 -0
  117. package/extensions/magpie/icons/iflytekcloud-color.svg +0 -1
  118. package/extensions/magpie/icons/inference.svg +0 -1
  119. package/extensions/magpie/icons/infinigence-color.svg +0 -1
  120. package/extensions/magpie/icons/internlm-color.svg +0 -1
  121. package/extensions/magpie/icons/jcode.svg +0 -1
  122. package/extensions/magpie/icons/jina.svg +0 -1
  123. package/extensions/magpie/icons/kilo.svg +0 -1
  124. package/extensions/magpie/icons/kilocode.svg +0 -1
  125. package/extensions/magpie/icons/kimi-code-desktop.png +0 -0
  126. package/extensions/magpie/icons/kimi.svg +0 -1
  127. package/extensions/magpie/icons/kiro-color.svg +0 -1
  128. package/extensions/magpie/icons/kling-color.svg +0 -1
  129. package/extensions/magpie/icons/lambda.svg +0 -1
  130. package/extensions/magpie/icons/leptonai-color.svg +0 -1
  131. package/extensions/magpie/icons/liquid.svg +0 -1
  132. package/extensions/magpie/icons/liveagent-color.svg +0 -1
  133. package/extensions/magpie/icons/llmapi.svg +0 -1
  134. package/extensions/magpie/icons/lmstudio.svg +0 -1
  135. package/extensions/magpie/icons/lody.png +0 -0
  136. package/extensions/magpie/icons/longcat.svg +0 -1
  137. package/extensions/magpie/icons/magpie.svg +0 -1
  138. package/extensions/magpie/icons/meta-color.svg +0 -1
  139. package/extensions/magpie/icons/metaai-color.svg +0 -1
  140. package/extensions/magpie/icons/microsoft-color.svg +0 -1
  141. package/extensions/magpie/icons/mimocode.svg +0 -1
  142. package/extensions/magpie/icons/minimax-color.svg +0 -1
  143. package/extensions/magpie/icons/mistermorph-color.svg +0 -1
  144. package/extensions/magpie/icons/mistral-color.svg +0 -1
  145. package/extensions/magpie/icons/mlx-serve.png +0 -0
  146. package/extensions/magpie/icons/modelscope-color.svg +0 -1
  147. package/extensions/magpie/icons/monica-color.svg +0 -1
  148. package/extensions/magpie/icons/moonshot.svg +0 -1
  149. package/extensions/magpie/icons/morph-color.svg +0 -1
  150. package/extensions/magpie/icons/mothx.png +0 -0
  151. package/extensions/magpie/icons/nebius.svg +0 -1
  152. package/extensions/magpie/icons/newapi-color.svg +0 -1
  153. package/extensions/magpie/icons/nousresearch.svg +0 -1
  154. package/extensions/magpie/icons/nova-color.svg +0 -1
  155. package/extensions/magpie/icons/novita-color.svg +0 -1
  156. package/extensions/magpie/icons/nvidia-color.svg +0 -1
  157. package/extensions/magpie/icons/ollama.svg +0 -1
  158. package/extensions/magpie/icons/omlx.svg +0 -1
  159. package/extensions/magpie/icons/omo.svg +0 -1
  160. package/extensions/magpie/icons/omp.svg +0 -1
  161. package/extensions/magpie/icons/openai.svg +0 -1
  162. package/extensions/magpie/icons/openchamber.svg +0 -11
  163. package/extensions/magpie/icons/openclaw.svg +0 -1
  164. package/extensions/magpie/icons/opencode.svg +0 -1
  165. package/extensions/magpie/icons/openmausbot.svg +0 -1
  166. package/extensions/magpie/icons/openrouter.svg +0 -1
  167. package/extensions/magpie/icons/openwebui.svg +0 -1
  168. package/extensions/magpie/icons/parasail.svg +0 -1
  169. package/extensions/magpie/icons/penguin-color.svg +0 -1
  170. package/extensions/magpie/icons/perplexity-color.svg +0 -1
  171. package/extensions/magpie/icons/phala-color.svg +0 -1
  172. package/extensions/magpie/icons/pi.svg +0 -1
  173. package/extensions/magpie/icons/pipellm-color.svg +0 -5
  174. package/extensions/magpie/icons/poe.svg +0 -1
  175. package/extensions/magpie/icons/ppio-color.svg +0 -1
  176. package/extensions/magpie/icons/prime-agent.svg +0 -1
  177. package/extensions/magpie/icons/qiniu-color.svg +0 -1
  178. package/extensions/magpie/icons/qoder.svg +0 -1
  179. package/extensions/magpie/icons/qwen-color.svg +0 -1
  180. package/extensions/magpie/icons/qwenpaw.svg +0 -1
  181. package/extensions/magpie/icons/raycast.svg +0 -1
  182. package/extensions/magpie/icons/reasonix-color.svg +0 -48
  183. package/extensions/magpie/icons/reka.svg +0 -1
  184. package/extensions/magpie/icons/replicate.svg +0 -1
  185. package/extensions/magpie/icons/roocode.svg +0 -1
  186. package/extensions/magpie/icons/rtk.png +0 -0
  187. package/extensions/magpie/icons/sambanova-color.svg +0 -1
  188. package/extensions/magpie/icons/sensenova-color.svg +0 -1
  189. package/extensions/magpie/icons/siliconcloud-color.svg +0 -1
  190. package/extensions/magpie/icons/snow.png +0 -0
  191. package/extensions/magpie/icons/snowflake-color.svg +0 -1
  192. package/extensions/magpie/icons/sophnet-color.svg +0 -1
  193. package/extensions/magpie/icons/spark-color.svg +0 -1
  194. package/extensions/magpie/icons/stepfun-color.svg +0 -1
  195. package/extensions/magpie/icons/straico-color.svg +0 -1
  196. package/extensions/magpie/icons/submodel-color.svg +0 -1
  197. package/extensions/magpie/icons/t3code.svg +0 -4
  198. package/extensions/magpie/icons/targon-color.svg +0 -1
  199. package/extensions/magpie/icons/tencent-color.svg +0 -1
  200. package/extensions/magpie/icons/tencentcloud-color.svg +0 -1
  201. package/extensions/magpie/icons/tiangong-color.svg +0 -1
  202. package/extensions/magpie/icons/together-color.svg +0 -1
  203. package/extensions/magpie/icons/trae.svg +0 -1
  204. package/extensions/magpie/icons/typesafe.png +0 -0
  205. package/extensions/magpie/icons/upstage-color.svg +0 -1
  206. package/extensions/magpie/icons/venice-color.svg +0 -1
  207. package/extensions/magpie/icons/vercel.svg +0 -1
  208. package/extensions/magpie/icons/vertexai-color.svg +0 -1
  209. package/extensions/magpie/icons/vllm-color.svg +0 -1
  210. package/extensions/magpie/icons/volcengine-color.svg +0 -1
  211. package/extensions/magpie/icons/voyage-color.svg +0 -1
  212. package/extensions/magpie/icons/vscode.svg +0 -1
  213. package/extensions/magpie/icons/wenxin-color.svg +0 -1
  214. package/extensions/magpie/icons/windsurf.svg +0 -1
  215. package/extensions/magpie/icons/workbuddy-color.svg +0 -1
  216. package/extensions/magpie/icons/workersai-color.svg +0 -1
  217. package/extensions/magpie/icons/xai.svg +0 -1
  218. package/extensions/magpie/icons/xiaomimimo.svg +0 -1
  219. package/extensions/magpie/icons/xinference-color.svg +0 -1
  220. package/extensions/magpie/icons/yi.svg +0 -1
  221. package/extensions/magpie/icons/yuanbao-color.svg +0 -1
  222. package/extensions/magpie/icons/yylx.svg +0 -5
  223. package/extensions/magpie/icons/zai.svg +0 -1
  224. package/extensions/magpie/icons/zcode.png +0 -0
  225. package/extensions/magpie/icons/zed.svg +0 -1
  226. package/extensions/magpie/icons/zenmux.svg +0 -1
  227. package/extensions/magpie/icons/zeroone.svg +0 -1
  228. package/extensions/magpie/icons/zhipu-color.svg +0 -1
  229. package/extensions/magpie/icons/zoo-code.png +0 -0
  230. package/extensions/magpie/index.html +0 -572
  231. package/extensions/magpie/kernel.js +0 -274
  232. package/extensions/magpie/keyspend.js +0 -63
  233. package/extensions/magpie/layout.js +0 -26
  234. package/extensions/magpie/manifest.json +0 -37
  235. package/extensions/magpie/navlamp.js +0 -47
  236. package/extensions/magpie/omarchy.css +0 -72
  237. package/extensions/magpie/omarchy.js +0 -46
  238. package/extensions/magpie/passerrors.js +0 -21
  239. package/extensions/magpie/pluginpages.js +0 -405
  240. package/extensions/magpie/plugins/gateway/client.js +0 -1194
  241. package/extensions/magpie/plugins/gateway/keys.js +0 -972
  242. package/extensions/magpie/plugins/gateway/settings.js +0 -459
  243. package/extensions/magpie/plugins/library/client.js +0 -6788
  244. package/extensions/magpie/plugins/library/i18n.js +0 -3228
  245. package/extensions/magpie/plugins/library/library.css +0 -848
  246. package/extensions/magpie/plugins/otel/i18n.js +0 -48
  247. package/extensions/magpie/plugins/otel/page.js +0 -189
  248. package/extensions/magpie/plugins/redact/client.js +0 -46
  249. package/extensions/magpie/plugins/redact/demo.js +0 -461
  250. package/extensions/magpie/plugins/redact/i18n.js +0 -432
  251. package/extensions/magpie/plugins/redact/page.js +0 -985
  252. package/extensions/magpie/plugins/redact/redact.css +0 -385
  253. package/extensions/magpie/plugins/redact/settings.js +0 -248
  254. package/extensions/magpie/plugins/redact/xq_dark.jpg +0 -0
  255. package/extensions/magpie/plugins/redact/xq_dark.webm +0 -0
  256. package/extensions/magpie/plugins/redact/xq_light.jpg +0 -0
  257. package/extensions/magpie/plugins/redact/xq_light.webm +0 -0
  258. package/extensions/magpie/plugins/routing/client.js +0 -8593
  259. package/extensions/magpie/plugins/routing/i18n.js +0 -2696
  260. package/extensions/magpie/plugins/routing/routing.css +0 -1111
  261. package/extensions/magpie/plugins/search/settings.js +0 -460
  262. package/extensions/magpie/plugins/sessions/client.js +0 -1622
  263. package/extensions/magpie/plugins/sessions/i18n.js +0 -360
  264. package/extensions/magpie/plugins/sessions/sessions.css +0 -181
  265. package/extensions/magpie/plugins/sessions/settings.js +0 -100
  266. package/extensions/magpie/plugins/usage/client.js +0 -150
  267. package/extensions/magpie/plugins/usage/i18n.js +0 -984
  268. package/extensions/magpie/plugins/usage/page.js +0 -4307
  269. package/extensions/magpie/plugins.css +0 -319
  270. package/extensions/magpie/plugins.js +0 -1926
  271. package/extensions/magpie/profilerename.js +0 -27
  272. package/extensions/magpie/purposes.js +0 -2
  273. package/extensions/magpie/quitask.js +0 -79
  274. package/extensions/magpie/quotaall.js +0 -142
  275. package/extensions/magpie/ratewindows.js +0 -123
  276. package/extensions/magpie/registry.js +0 -87
  277. package/extensions/magpie/resetask.js +0 -137
  278. package/extensions/magpie/ruleedit.js +0 -53
  279. package/extensions/magpie/scrollends.js +0 -91
  280. package/extensions/magpie/settingshints.js +0 -75
  281. package/extensions/magpie/signsheets.js +0 -86
  282. package/extensions/magpie/start.js +0 -26
  283. package/extensions/magpie/syncfiles.js +0 -216
  284. package/extensions/magpie/theme.js +0 -29
  285. package/extensions/magpie/traywindows.js +0 -41
  286. package/extensions/magpie/webfonts.js +0 -130
  287. package/extensions/magpie/whatsnew.js +0 -252
  288. package/extensions/magpie.zip +0 -0
  289. package/lib/canvas-app.html +0 -442
  290. package/lib/mobile/canvas-store.js +0 -365
  291. package/lib/mobile/magpie-web.js +0 -425
  292. package/lib/mobile/promo-receiver.cjs +0 -77
  293. package/lib/routes/canvas-desktop.js +0 -196
@@ -0,0 +1,501 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 手机侧「DSH 插件市场」的清单服务:读社区精选清单,收窄成 App 要的那一页。
5
+ *
6
+ * 为什么是读社区清单,而不是我们自己的分发系统:这一页装的是**电脑上 DSH 的插件**
7
+ * (npm 包 / GitHub 仓库),社区那份清单(awesome-dsh-plugin.com,另有 jsDelivr 上的镜像
8
+ * 一份)已经把「有哪些、是谁的、多少 star / 下载、图标、截图、能力声明」维护好了——那是
9
+ * 别人的一份公开索引,我们照读即可,没有自己的审核流水线要养。
10
+ *
11
+ * 为什么不在手机上装包、也不自己跑 pnpm:装插件是宿主的活。DSH 里本来就有插件管理器
12
+ * (桌面端 设置 → 插件 那个页面背后的同一个服务),版本兼容检查、依赖脚本授权、失败回滚、
13
+ * 要不要重启才生效,全由它判断。插件侧只把清单削干净、把 `installSpec` 算好,安装动作交给
14
+ * 已有的 `POST /plugin/market/install`。能力面对手机只多「读一份公开 JSON」(外网 GET),
15
+ * 仍然不碰 shell、不碰系统设置、不写任意文件。
16
+ *
17
+ * 三个取舍:
18
+ * 1. **缓存收窄后的结果,不缓存原文**:源清单 14MB / 4MB,缓存原文等于在宿主进程里常驻
19
+ * 十几 MB;收窄后通常是几百 KB 到一两 MB。10 分钟有效,过期重拉。
20
+ * 2. **过期重拉失败不回旧缓存**:清单过期是错误答案,不是降级答案(与 dshmarket 自己的
21
+ * 判据一致)。宁可让手机说「读不到清单」,也不要让它按一周前的数据装一个已经下架的包。
22
+ * 3. **并发只允许一个在飞**:手机切搜索词、翻页会连续打这条接口,每次各拉 14MB 毫无意义。
23
+ * 其余请求等同一个 Promise。
24
+ *
25
+ * 接口:`GET /api/mobile/v1/plugin/catalog`(走 token 守卫),query 见 A3-101。
26
+ * 失败回 `{ ok:false, error:{ code:'catalog-unavailable', message } }`,message 里带上每个源
27
+ * 各自的失败原因(排查时最想知道的是「哪一个源、哪一步坏了」)。
28
+ *
29
+ * 清单里认不出的条目**整条丢弃**:`name` / `owner` / `repo` 缺一、repo 不是
30
+ * `https://github.com/<owner>/<repo>`、或与 owner 不一致,都不是能安装的东西,App 侧不再兜。
31
+ */
32
+
33
+ const { sendJson } = require('../shared/http-util');
34
+
35
+ /**
36
+ * 数据源,按顺序试,前一个失败才试下一个。两份清单是社区同一批数据的两条腿:
37
+ * 主源字段全(star / 下载 / 图标 / 截图 / 收录时间),镜像源只剩名字、说明、仓库与版本。
38
+ */
39
+ const SOURCES = [
40
+ {
41
+ id: 'awesome',
42
+ url: 'https://awesome-dsh-plugin.com/plugins.json',
43
+ // { categories: { id: {en,zh} }, plugins: [...] }
44
+ format: 'plugins',
45
+ },
46
+ {
47
+ id: 'index',
48
+ url: 'https://cdn.jsdelivr.net/npm/dsh-plugin-catalog/index.json',
49
+ // { format: 1, metadata: { categories: { id: { title } } }, plugins: [...] }
50
+ format: 'index',
51
+ },
52
+ ];
53
+
54
+ /** 缓存有效期:清单一天更新不了几次,10 分钟足够新,也不至于每次翻页都拉 14MB。 */
55
+ const CACHE_TTL_MS = 10 * 60 * 1000;
56
+ /** 每个源最多试两次:14MB 的 CDN 偶发断流很常见,一次失败不代表源坏了。 */
57
+ const ATTEMPTS_PER_SOURCE = 2;
58
+ /** 单次拉取的等待上限。14MB 在慢网络上也要留够时间,但绝不能无限等。 */
59
+ const FETCH_TIMEOUT_MS = 30 * 1000;
60
+
61
+ /** 文案每门语言的字数上限(契约:600)。 */
62
+ const TEXT_MAX_CHARS = 600;
63
+ /** 图标只认 https 绝对地址;截图最多 4 张;能力最多 12 项(契约)。 */
64
+ const SCREENSHOTS_MAX = 4;
65
+ const CAPABILITIES_MAX = 12;
66
+ /** 搜索词上限 100 字,超了截断而不是报错:手机输入框不会让用户数到 100。 */
67
+ const Q_MAX_CHARS = 100;
68
+ /** 分页:缺省 20 条,最多 60 条一页(手机一屏装不下更多,再大只是白算)。 */
69
+ const LIMIT_DEFAULT = 20;
70
+ const LIMIT_MAX = 60;
71
+
72
+ const SORTS = ['downloads', 'stars', 'added', 'name'];
73
+ const DIRS = ['asc', 'desc'];
74
+ /** 条目没有分类时的落点:它仍会出现在「全部」里,只是不属于任何一颗胶囊。 */
75
+ const FALLBACK_CATEGORY = 'other';
76
+
77
+ /** 一份 `https://github.com/<owner>/<repo>`;允许尾部 `.git` 与多余斜杠,输出统一成干净形状。 */
78
+ const GITHUB_REPO = /^https:\/\/github\.com\/([A-Za-z0-9-]+)\/([A-Za-z0-9._-]+?)(?:\.git)?\/?$/;
79
+ /** npm 包名(含 scope)。只有合法名字才当安装目标,否则退回 github: 形态。 */
80
+ const NPM_NAME = /^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/;
81
+ const NPM_NAME_MAX = 214;
82
+ /** 收录日期只认 `YYYY-MM-DD`。 */
83
+ const DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
84
+
85
+ /** 收窄后的缓存:{ source, fetchedAt, fetchedAtWall, items, categories }。 */
86
+ let cache = null;
87
+ /** 正在飞的那一次拉取;并发请求等它。 */
88
+ let inflight = null;
89
+ /** 测试注入点:假 fetch 与假时钟。真跑时就是全局 fetch / Date.now。 */
90
+ let fetchImpl = null;
91
+ let clock = null;
92
+
93
+ function nowMs() {
94
+ return clock ? clock() : Date.now();
95
+ }
96
+
97
+ function errorText(error) {
98
+ if (!error) return '未知错误';
99
+ if (typeof error.message === 'string' && error.message) return error.message;
100
+ return String(error);
101
+ }
102
+
103
+ /** 非空字符串才收,其余 null;`max` 用来挡掉离谱长的字段。 */
104
+ function textOrNull(value, max = 300) {
105
+ if (typeof value !== 'string') return null;
106
+ const text = value.trim();
107
+ return text ? (max ? text.slice(0, max) : text) : null;
108
+ }
109
+
110
+ /** 只认 https 绝对地址:http 会混合内容、其它协议 App 也画不出来。 */
111
+ function httpsOrNull(value, max = 1000) {
112
+ const text = textOrNull(value, max);
113
+ if (!text) return null;
114
+ try {
115
+ return new URL(text).protocol === 'https:' ? text : null;
116
+ } catch {
117
+ return null;
118
+ }
119
+ }
120
+
121
+ /**
122
+ * 双语表:只留「语言 → 非空字符串」的项,每门语言截到 600 字;一项都不剩时是 `null`
123
+ * (不是空对象 —— App 侧据此判断「这条没有文案」)。
124
+ */
125
+ function localizedOf(value) {
126
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
127
+ const out = {};
128
+ for (const [language, text] of Object.entries(value)) {
129
+ const clean = textOrNull(text, TEXT_MAX_CHARS);
130
+ if (clean) out[language] = clean;
131
+ }
132
+ return Object.keys(out).length ? out : null;
133
+ }
134
+
135
+ /** 非负安全整数,否则 null(`stars` 那里再由调用方退成 0)。 */
136
+ function countOf(value) {
137
+ if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) return null;
138
+ return value;
139
+ }
140
+
141
+ /** 分类字段可能是字符串或字符串数组,取第一个可用值。 */
142
+ function firstString(value) {
143
+ if (typeof value === 'string') return textOrNull(value, 64);
144
+ if (Array.isArray(value)) {
145
+ for (const entry of value) {
146
+ const clean = textOrNull(entry, 64);
147
+ if (clean) return clean;
148
+ }
149
+ }
150
+ return null;
151
+ }
152
+
153
+ /** `https://` 截图数组,最多 4 张。 */
154
+ function screenshotsOf(value) {
155
+ if (!Array.isArray(value)) return [];
156
+ const out = [];
157
+ for (const entry of value) {
158
+ const url = httpsOrNull(entry);
159
+ if (url && out.length < SCREENSHOTS_MAX) out.push(url);
160
+ }
161
+ return out;
162
+ }
163
+
164
+ /** 能力声明:只留字符串,最多 12 项,重复的不给第二份。 */
165
+ function capabilitiesOf(value) {
166
+ if (!Array.isArray(value)) return [];
167
+ const out = [];
168
+ for (const entry of value) {
169
+ const name = textOrNull(entry, 64);
170
+ if (name && !out.includes(name) && out.length < CAPABILITIES_MAX) out.push(name);
171
+ }
172
+ return out;
173
+ }
174
+
175
+ /**
176
+ * 仓库地址解析:只认 `https://github.com/<owner>/<repo>`,统一成不带 `.git` / 尾斜杠的形状。
177
+ * 返回 null 表示这条不该进清单。
178
+ */
179
+ function parseRepo(value) {
180
+ const text = textOrNull(value, 300);
181
+ if (!text) return null;
182
+ const match = GITHUB_REPO.exec(text);
183
+ if (!match) return null;
184
+ return { owner: match[1], repo: match[2], url: `https://github.com/${match[1]}/${match[2]}` };
185
+ }
186
+
187
+ /**
188
+ * 一条原始记录 → 契约里的 Item;认不出就 null。
189
+ *
190
+ * `raw.owner` 有值时必须与 repo 里的 owner 一致(大小写不敏感):清单里这两格打架的条目,
191
+ * 装起来只会给人装错仓库。镜像源没有 owner 字段,从仓库地址里解析。
192
+ */
193
+ function normalizeItem(raw, sourceId) {
194
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
195
+ const name = textOrNull(raw.name, 200);
196
+ if (!name) return null;
197
+ const repoUrl = sourceId === 'index'
198
+ ? (raw.repository && raw.repository.url)
199
+ : (raw.url || (raw.repository && raw.repository.url));
200
+ const parsed = parseRepo(repoUrl);
201
+ if (!parsed) return null;
202
+ const declaredOwner = textOrNull(raw.owner, 100);
203
+ if (declaredOwner && declaredOwner.toLowerCase() !== parsed.owner.toLowerCase()) return null;
204
+
205
+ // 镜像源没有 npm 字段:它的 `name` 就是 npm 包名(契约这么写的)。
206
+ const npm = textOrNull(sourceId === 'index' ? raw.name : raw.npm, NPM_NAME_MAX);
207
+ // 分类:主源在顶层 `category`,镜像源在 `metadata.category`(都可能是字符串或数组)。
208
+ const rawCategory = sourceId === 'index' && raw.metadata ? raw.metadata.category : raw.category;
209
+ const npmValid = npm && NPM_NAME.test(npm) ? npm : null;
210
+
211
+ // 版本:主源给 `version`,镜像源给 `versions: [{ version }]` 取第一条。
212
+ const version = textOrNull(
213
+ raw.version || (Array.isArray(raw.versions) && raw.versions[0] && raw.versions[0].version),
214
+ 64,
215
+ );
216
+
217
+ const stars = countOf(raw.stars);
218
+ const downloads = countOf(raw.downloads);
219
+ const added = textOrNull(raw.added, 32);
220
+
221
+ return {
222
+ name,
223
+ owner: parsed.owner,
224
+ repo: parsed.url,
225
+ category: firstString(rawCategory) || FALLBACK_CATEGORY,
226
+ title: localizedOf(raw.title),
227
+ description: localizedOf(raw.description),
228
+ npm: npmValid,
229
+ version,
230
+ stars: stars === null ? 0 : stars,
231
+ downloads,
232
+ icon: httpsOrNull(raw.icon),
233
+ screenshots: screenshotsOf(raw.screenshots),
234
+ capabilities: capabilitiesOf(raw.capabilities),
235
+ // 安装目标是宿主算好的:有合法 npm 名就用它,否则让插件管理器按 GitHub 仓库去装。
236
+ installSpec: npmValid || `github:${parsed.owner}/${parsed.repo}`,
237
+ added: added && DAY_PATTERN.test(added) ? added : null,
238
+ };
239
+ }
240
+
241
+ /**
242
+ * 两份清单的形状差异只在这里抹平:返回 { categories, items }。
243
+ * `items` 已去重(按 `owner/name`,同一份清单里同名同源的两条只留第一条)。
244
+ */
245
+ function narrowCatalog(document, source) {
246
+ if (!document || typeof document !== 'object') throw new Error('返回的不是清单 JSON');
247
+ if (source.format === 'index' && document.format !== 1) {
248
+ throw new Error(`清单格式不认识(format=${String(document.format)})`);
249
+ }
250
+ const plugins = document.plugins;
251
+ if (!Array.isArray(plugins) || plugins.length === 0) throw new Error('清单里没有 plugins 数组');
252
+
253
+ // 分类:主源是 `categories: { id: {en,zh} }`,镜像源是 `metadata.categories: { id: { title } }`。
254
+ const rawCategories = source.format === 'index'
255
+ ? (document.metadata && document.metadata.categories)
256
+ : document.categories;
257
+ const categories = [];
258
+ if (rawCategories && typeof rawCategories === 'object' && !Array.isArray(rawCategories)) {
259
+ for (const [id, value] of Object.entries(rawCategories)) {
260
+ const cleanId = textOrNull(id, 64);
261
+ if (!cleanId) continue;
262
+ const title = source.format === 'index'
263
+ ? (localizedOf(value && value.title) || localizedOf(value))
264
+ : localizedOf(value);
265
+ // 分类没写文案时退回 id 当名字:胶囊总得有个字,不能画成空白。
266
+ categories.push({ id: cleanId, title: title || { en: cleanId } });
267
+ }
268
+ }
269
+
270
+ const seen = new Set();
271
+ const items = [];
272
+ for (const raw of plugins) {
273
+ const item = normalizeItem(raw, source.id);
274
+ if (!item) continue;
275
+ const key = `${item.owner}/${item.name}`.toLowerCase();
276
+ if (seen.has(key)) continue;
277
+ seen.add(key);
278
+ items.push(item);
279
+ }
280
+ if (items.length === 0) throw new Error('清单里没有一条可用的插件(缺仓库地址或名字)');
281
+ return { categories, items };
282
+ }
283
+
284
+ /** 拉一个源:读 JSON、把表单收窄。抛出的错误由 `loadCatalog` 记进失败原因。 */
285
+ async function fetchCatalogDocument(source) {
286
+ const doFetch = fetchImpl || globalThis.fetch;
287
+ if (typeof doFetch !== 'function') throw new Error('这个运行时里没有 fetch,拉不了清单');
288
+ const controller = typeof AbortController === 'function' ? new AbortController() : null;
289
+ const timer = controller ? setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS) : null;
290
+ let response;
291
+ try {
292
+ response = await doFetch(source.url, {
293
+ headers: { accept: 'application/json' },
294
+ signal: controller ? controller.signal : undefined,
295
+ });
296
+ } catch (error) {
297
+ throw new Error(`连不上:${errorText(error)}`);
298
+ } finally {
299
+ if (timer) clearTimeout(timer);
300
+ }
301
+ if (!response || response.ok !== true) {
302
+ const status = response && Number.isFinite(response.status) ? response.status : 0;
303
+ throw new Error(`HTTP ${status || '未知'}`);
304
+ }
305
+ try {
306
+ return await response.json();
307
+ } catch {
308
+ throw new Error('返回的不是 JSON');
309
+ }
310
+ }
311
+
312
+ /**
313
+ * 按顺序试源,每个源最多两次;全挂了抛错,message 是每个源各自的失败原因拼接。
314
+ * 成功时把**收窄后**的结果塞进缓存(不是原文)。
315
+ */
316
+ async function fetchCatalog() {
317
+ const failures = [];
318
+ for (const source of SOURCES) {
319
+ for (let attempt = 1; attempt <= ATTEMPTS_PER_SOURCE; attempt += 1) {
320
+ try {
321
+ const document = await fetchCatalogDocument(source);
322
+ const { categories, items } = narrowCatalog(document, source);
323
+ const fetchedAt = nowMs();
324
+ cache = {
325
+ source: source.url,
326
+ fetchedAt,
327
+ // 墙钟另存一份:`fetchedAt` 走可注入的时钟(测试用),这一格永远是真实时间。
328
+ fetchedAtWall: Date.now(),
329
+ items,
330
+ categories,
331
+ };
332
+ return cache;
333
+ } catch (error) {
334
+ failures.push(`${source.url} 第 ${attempt} 次:${errorText(error)}`);
335
+ }
336
+ }
337
+ }
338
+ throw new Error(failures.join(';'));
339
+ }
340
+
341
+ /**
342
+ * 取一份可用的收窄结果。
343
+ *
344
+ * - 缓存有效期内直接返回;
345
+ * - 过期 / 没有缓存时拉一次,**失败就抛**(绝不拿旧缓存冒充成功);
346
+ * - 并发只允许一个在飞:后来者 await 同一个 Promise。
347
+ */
348
+ async function loadCatalog() {
349
+ const at = nowMs();
350
+ if (cache && at - cache.fetchedAt >= 0 && at - cache.fetchedAt < CACHE_TTL_MS) return cache;
351
+ if (!inflight) {
352
+ inflight = fetchCatalog().finally(() => { inflight = null; });
353
+ }
354
+ return inflight;
355
+ }
356
+
357
+ /** 搜索词:名字 / 作者 / 仓库名 / 双语标题与说明 / npm 名,大小写不敏感。 */
358
+ function matchesQuery(item, needle) {
359
+ if (!needle) return true;
360
+ const haystack = [
361
+ item.name,
362
+ item.owner,
363
+ item.repo,
364
+ item.npm || '',
365
+ ...Object.values(item.title || {}),
366
+ ...Object.values(item.description || {}),
367
+ ...item.capabilities,
368
+ ].join('\n').toLowerCase();
369
+ return haystack.includes(needle);
370
+ }
371
+
372
+ /** 排序值:缺一格时 `null`(排在最后,与 App 侧「缺哪格就不画」一致)。 */
373
+ function sortValue(item, sort) {
374
+ if (sort === 'downloads') return item.downloads;
375
+ if (sort === 'stars') return item.stars;
376
+ if (sort === 'added') return item.added;
377
+ return item.name.toLowerCase();
378
+ }
379
+
380
+ /**
381
+ * 比较:null 恒排最后(不管升序降序,都不该让「没有数据」冒到顶上——镜像源本来就没有
382
+ * 下载量,排在前面等于整页被一堆空数字占住)。
383
+ */
384
+ function compareItems(a, b, sort, dir) {
385
+ const left = sortValue(a, sort);
386
+ const right = sortValue(b, sort);
387
+ if (left === null && right === null) return compareNames(a, b);
388
+ if (left === null) return 1;
389
+ if (right === null) return -1;
390
+ if (left === right) return compareNames(a, b);
391
+ const order = left < right ? -1 : 1;
392
+ return dir === 'asc' ? order : -order;
393
+ }
394
+
395
+ /** 同值的兜底顺序:名字、再作者。翻页因此是确定性的,不会两页看到同一条。 */
396
+ function compareNames(a, b) {
397
+ const an = a.name.toLowerCase();
398
+ const bn = b.name.toLowerCase();
399
+ if (an !== bn) return an < bn ? -1 : 1;
400
+ const ao = a.owner.toLowerCase();
401
+ const bo = b.owner.toLowerCase();
402
+ if (ao !== bo) return ao < bo ? -1 : 1;
403
+ return 0;
404
+ }
405
+
406
+ /** 取整数 query 参数;不是整数就退回缺省。 */
407
+ function intParam(raw, fallback) {
408
+ if (typeof raw !== 'string' || !/^\d+$/.test(raw.trim())) return fallback;
409
+ return Number.parseInt(raw.trim(), 10);
410
+ }
411
+
412
+ /**
413
+ * query 清洗。排序 / 方向 / 分页的缺省与上限全部照契约;认不出的值退回缺省而不是报错
414
+ * (手机拼错一个参数不该让整页变红,界面上的胶囊本来就只会给合法值)。
415
+ */
416
+ function parseQuery(searchParams) {
417
+ const q = textOrNull(searchParams.get('q'), Q_MAX_CHARS) || '';
418
+ const rawCategory = textOrNull(searchParams.get('category'), 64);
419
+ const category = rawCategory && rawCategory !== 'all' ? rawCategory : 'all';
420
+ const rawSort = textOrNull(searchParams.get('sort'), 16);
421
+ const sort = SORTS.includes(rawSort) ? rawSort : 'downloads';
422
+ const rawDir = textOrNull(searchParams.get('dir'), 8);
423
+ const dir = DIRS.includes(rawDir) ? rawDir : (sort === 'name' ? 'asc' : 'desc');
424
+ const offset = intParam(searchParams.get('offset'), 0);
425
+ const limit = Math.min(Math.max(intParam(searchParams.get('limit'), LIMIT_DEFAULT), 1), LIMIT_MAX);
426
+ return { q: q.toLowerCase(), category, sort, dir, offset, limit };
427
+ }
428
+
429
+ /**
430
+ * 把一份收窄结果切成契约要的那一页。
431
+ *
432
+ * `categories` 的 `count` 只受搜索词影响、**不受 `category` 影响**:否则选中一颗胶囊之后
433
+ * 其余胶囊的计数全变 0,界面只能把它们藏起来,用户就再也切不回去了。
434
+ */
435
+ function pageOf(catalog, query) {
436
+ const searched = query.q ? catalog.items.filter((item) => matchesQuery(item, query.q)) : catalog.items;
437
+
438
+ const counts = new Map();
439
+ for (const item of searched) counts.set(item.category, (counts.get(item.category) || 0) + 1);
440
+ const categories = catalog.categories.map((category) => ({
441
+ id: category.id,
442
+ title: category.title,
443
+ count: counts.get(category.id) || 0,
444
+ }));
445
+
446
+ const filtered = query.category === 'all'
447
+ ? searched
448
+ : searched.filter((item) => item.category === query.category);
449
+
450
+ const sorted = filtered.slice().sort((a, b) => compareItems(a, b, query.sort, query.dir));
451
+
452
+ const items = sorted.slice(query.offset, query.offset + query.limit);
453
+ return {
454
+ source: catalog.source,
455
+ fetchedAt: catalog.fetchedAt,
456
+ total: sorted.length,
457
+ categories,
458
+ items,
459
+ hasMore: query.offset + items.length < sorted.length,
460
+ };
461
+ }
462
+
463
+ /** GET /api/mobile/v1/plugin/catalog */
464
+ async function handlePluginCatalog(ctx, req, res) {
465
+ let catalog;
466
+ try {
467
+ catalog = await loadCatalog();
468
+ } catch (error) {
469
+ return sendJson(res, 503, {
470
+ ok: false,
471
+ error: { code: 'catalog-unavailable', message: errorText(error) },
472
+ });
473
+ }
474
+ let query;
475
+ try {
476
+ query = parseQuery(new URL((req && req.url) || '/', 'http://x').searchParams);
477
+ } catch {
478
+ query = parseQuery(new URLSearchParams());
479
+ }
480
+ const page = pageOf(catalog, query);
481
+ sendJson(res, 200, { ok: true, ...page });
482
+ }
483
+
484
+ module.exports = {
485
+ handlePluginCatalog,
486
+ // 纯逻辑(守卫 / 测试直接加载)
487
+ parseQuery,
488
+ pageOf,
489
+ narrowCatalog,
490
+ normalizeItem,
491
+ loadCatalog,
492
+ SOURCES,
493
+ CACHE_TTL_MS,
494
+ LIMIT_DEFAULT,
495
+ LIMIT_MAX,
496
+ Q_MAX_CHARS,
497
+ // 测试注入:假 fetch / 假时钟 / 清缓存(真跑时一个都不该被调用)
498
+ __setFetchForTests(next) { fetchImpl = next || null; },
499
+ __setClockForTests(next) { clock = next || null; },
500
+ __resetForTests() { cache = null; inflight = null; fetchImpl = null; clock = null; },
501
+ };
@@ -66,6 +66,41 @@ const MAX_ALLOW = 32;
66
66
  /** 单条前缀长度上限。 */
67
67
  const MAX_ALLOW_PREFIX = 256;
68
68
 
69
+ /**
70
+ * 宿主自身的命名空间:第三方声明的 `allow` 前缀**不许与它们重叠**。
71
+ *
72
+ * 这条通道用的是宿主 owner 身份的浏览器会话 cookie(见 `extension-data.js` 的换 cookie
73
+ * 那一段),所以前缀一旦盖到宿主自己的接口,第三方插件就能以 owner 身份打到移动 API、
74
+ * 控制面那批 `localOnly` 端点、以及配对 / 会话这些地方。判据是**双向重叠**而不是字面相等:
75
+ * `allow: ['/api/']` 一条就盖住了 `/api/mobile/` 底下全部,必须与 `/api/mobile/v1/xxx`
76
+ * 一样被拒。命中的前缀丢掉,同一条声明里别的服务照旧生效。
77
+ */
78
+ const PROTECTED_PREFIXES = [
79
+ { namespace: '/api/mobile', reason: 'host-mobile-api' },
80
+ // 单列一条:插件市场那几个装 / 卸 / 启停端点是最该挡的,就算以后 `/api/mobile` 的
81
+ // 前缀口径变了,这一条也还在。
82
+ { namespace: '/api/mobile/v1/plugin', reason: 'host-mobile-api' },
83
+ { namespace: '/api/relay', reason: 'host-control-plane' },
84
+ { namespace: '/connect', reason: 'host-session' },
85
+ ];
86
+
87
+ /** 前缀去掉查询串 / 片段:`/connect?token=` 落到的路径就是 `/connect`。 */
88
+ function pathnameOf(prefix) {
89
+ return prefix.split(/[?#]/)[0];
90
+ }
91
+
92
+ /**
93
+ * 一个前缀是否盖住宿主命名空间。两个方向都算:声明的前缀罩住宿主那一段(`/api/`),
94
+ * 或者伸进宿主那一段里面(`/api/mobile/v1/session`)。边界按整段比,`/api/mobiles/`
95
+ * 这种只是同名前缀的第三方路径不该被误伤。
96
+ */
97
+ function overlapsProtected(prefix) {
98
+ const pathname = pathnameOf(prefix);
99
+ return PROTECTED_PREFIXES.find(({ namespace }) => (
100
+ pathname === namespace || pathname.startsWith(`${namespace}/`) || namespace.startsWith(pathname)
101
+ )) || null;
102
+ }
103
+
69
104
  /**
70
105
  * 读一个插件 `package.json` 里的数据服务声明。
71
106
  *
@@ -74,7 +109,9 @@ const MAX_ALLOW_PREFIX = 256;
74
109
  * 另一个本来能用的服务也一起消失。`service` 撞名的以后扫到的为准由调用方决定(见
75
110
  * `discoverDataServices` 的注释:先扫到的优先)。
76
111
  *
77
- * 返回 `[{ service, allow }]`;没声明 / 不是数组一律空数组。
112
+ * 返回 `[{ service, allow }]`;没声明 / 不是数组一律空数组。有前缀被禁区挡掉时,那一条
113
+ * 额外带 `rejected: [{ prefix, reason }]`,供作者排查与测试断言(没被挡就不带这一格,
114
+ * 形状与从前逐字一致)。
78
115
  */
79
116
  function parseDataServices(manifest) {
80
117
  if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest)) return [];
@@ -87,18 +124,26 @@ function parseDataServices(manifest) {
87
124
  if (!SERVICE_RE.test(service)) continue;
88
125
  if (!Array.isArray(item.allow)) continue;
89
126
  const allow = [];
127
+ const rejected = [];
90
128
  for (const prefix of item.allow) {
91
129
  if (typeof prefix !== 'string') continue;
92
130
  const text = prefix.trim();
93
131
  if (!text || text.length > MAX_ALLOW_PREFIX) continue;
94
132
  // 只认本源绝对路径前缀:整条 URL 或 `//host` 之类的写法会把它变成对外站的跳板。
95
133
  if (!text.startsWith('/') || text.startsWith('//')) continue;
134
+ // 宿主自身的命名空间一律不许盖:这条通道带的是 owner 身份的 cookie,盖到移动 API /
135
+ // 控制面就等于第三方插件拿到了宿主自己的接口。丢掉这一条,同一声明里的其余照旧。
136
+ const clash = overlapsProtected(text);
137
+ if (clash) {
138
+ if (!rejected.some((entry) => entry.prefix === text)) rejected.push({ prefix: text, reason: clash.reason });
139
+ continue;
140
+ }
96
141
  if (allow.includes(text)) continue;
97
142
  allow.push(text);
98
143
  if (allow.length >= MAX_ALLOW) break;
99
144
  }
100
145
  if (allow.length === 0) continue;
101
- out.push({ service, allow });
146
+ out.push(rejected.length > 0 ? { service, allow, rejected } : { service, allow });
102
147
  }
103
148
  return out;
104
149
  }
@@ -234,8 +279,10 @@ module.exports = {
234
279
  DEFAULT_DIR,
235
280
  DATA_SERVICES_FIELD,
236
281
  SLUG_PATTERN,
282
+ PROTECTED_PREFIXES,
237
283
  declaredDir,
238
284
  parseDataServices,
285
+ overlapsProtected,
239
286
  ownerSlugOf,
240
287
  slugOf,
241
288
  discoverPackages,
@@ -44,7 +44,6 @@ const { sendJson, readJsonBody, readRawBody } = require('../shared/http-util');
44
44
  const { headerOf } = require('../shared/e2ee-request');
45
45
  const { allowedRoots, inAllowedZone } = require('./allowed-zone.js');
46
46
  const { withProjectWrite, queueKeyOf } = require('./project-write-queue.js');
47
- const canvasStore = require('./canvas-store.js');
48
47
 
49
48
  // 片大小的**起步值** 256KB(base64 后约 341KB):客户端一次请求的硬预算是 8s
50
49
  // (`app/src/api/core.ts` 的 REQUEST_TIMEOUT_MS),一上来就给大片的机器会在慢速上行
@@ -114,10 +113,9 @@ function rememberFinishedUpload(uploadId, record) {
114
113
  /**
115
114
  * 按 uploadId 取刚落盘的上传。过期 / 不存在返回 null。
116
115
  *
117
- * 记录有两种:收件箱那份带 `path`(图片内联要用它),画布素材那份带 `hash`(没有路径 ——
118
- * 画布库只按内容哈希认文件)。两种都要能原样回放给重收一次口的客户端。
116
+ * 记录带 `path`(图片内联要用它),能原样回放给重收一次口的客户端。
119
117
  *
120
- * @returns {{ path?: string, hash?: string, purpose?: string, name: string, bytes: number } | null}
118
+ * @returns {{ path?: string, name: string, bytes: number } | null}
121
119
  */
122
120
  function resolveFinishedUpload(uploadId) {
123
121
  const id = String(uploadId || '');
@@ -357,16 +355,8 @@ async function handleUploadBegin(ctx, req, res) {
357
355
  }
358
356
  const caps = resolveCaps(body);
359
357
  if (caps.status) return fail(res, caps.status, caps.code, caps.message);
360
- // 画布库的素材也走这条通道(`purpose: 'canvas'`):传完不落收件箱,而是按内容哈希收进
361
- // 画布库(见 `canvas-store.js`)。它与 `destPath` 互斥:一个是「收件箱里的一份文件」,
362
- // 一个是「库里的一份字节」,同时给说明调用方把两件事搞混了。
363
- const purpose = body.purpose === undefined || body.purpose === null ? '' : String(body.purpose);
364
- if (purpose && purpose !== 'canvas') return fail(res, 400, 'bad-purpose', 'purpose 只认 canvas');
365
- if (purpose && body.destPath !== undefined && body.destPath !== null) {
366
- return fail(res, 400, 'bad-request', 'purpose 与 destPath 不能同时给');
367
- }
368
358
  // 落点先判:越界 / 不是目录在这一步就回,不先建暂存再报错 —— 残片要躺满 7 天才清。
369
- const destination = purpose ? { dir: '' } : await resolveDestDir(ctx, body);
359
+ const destination = await resolveDestDir(ctx, body);
370
360
  if (destination.status) return fail(res, destination.status, destination.code, destination.message);
371
361
  const stagingDir = path.join(inboxRoot(), '.staging');
372
362
  try {
@@ -386,8 +376,6 @@ async function handleUploadBegin(ctx, req, res) {
386
376
  }
387
377
  const job = {
388
378
  name: sanitizeName(body.name),
389
- // 画布素材:finish 时按内容哈希收进画布库,不进收件箱(见 `canvas-store.js`)。
390
- purpose,
391
379
  // 指定落点就记在这里,finish 才用得上(中途不再判一次范围:begin 已经认过)。
392
380
  destDir: destination.dir,
393
381
  // begin 那一步算出来的允许区根:finish 落盘要拿它算写队列的键(见 `queueKeyOf`),
@@ -585,9 +573,6 @@ async function handleUploadFinish(_ctx, req, res) {
585
573
  const job = uploads.get(uploadId);
586
574
  if (!job) {
587
575
  const replay = resolveFinishedUpload(uploadId);
588
- if (replay && replay.purpose === 'canvas') {
589
- return sendJson(res, 200, { ok: true, hash: replay.hash, bytes: replay.bytes, name: replay.name, uploadId });
590
- }
591
576
  if (replay) return sendJson(res, 200, { ok: true, ...replay, uploadId });
592
577
  return fail(res, 404, 'unknown-upload', '上传会话不存在或已结束');
593
578
  }
@@ -602,19 +587,6 @@ async function handleUploadFinish(_ctx, req, res) {
602
587
  }
603
588
  removeUpload(uploadId, job);
604
589
 
605
- // 画布素材:不进收件箱,按内容哈希收进画布库,回哈希而不是路径(客户端拿不到也拼不出路径)。
606
- if (job.purpose === 'canvas') {
607
- try {
608
- const stored = await canvasStore.storeUploadedFile(job.stagingPath);
609
- // 与收件箱那条同一个理由:回包丢了,手机只会拿同一个 uploadId 再收一次口。
610
- rememberFinishedUpload(uploadId, { purpose: 'canvas', hash: stored.hash, bytes: stored.bytes, name: job.name });
611
- return sendJson(res, 200, { ok: true, hash: stored.hash, bytes: stored.bytes, name: job.name, uploadId });
612
- } catch (error) {
613
- try { await fsp.unlink(job.stagingPath); } catch { /* 已经不在了 */ }
614
- return fail(res, (error && error.status) || 500, (error && error.code) || 'canvas-store-unavailable', `收进画布库失败:${error.message}`);
615
- }
616
- }
617
-
618
590
  const dir = job.destDir || path.join(inboxRoot(), today());
619
591
  // 落盘这一段按项目串行,键与 `file-ops.js` 同口径(`queueKeyOf`:目标所在的那一个允许区
620
592
  // 根)。判候选名(`uniquePath`)与 rename 必须在同一个独占窗口里 —— 分开判的话两个并发