@luosuqwq/dsh-diagram 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -98,7 +98,7 @@ A minimal `diagram_render` call:
98
98
  }
99
99
  ```
100
100
 
101
- The result carries `path`, `url`, `width`, `height`, `bytes` and `markdown`; the Markdown link points at `/plugins/dsh-diagram/asset?file=...` on the DSH web server, so it renders inline as long as the value is copied unchanged.
101
+ The result carries `path`, `url`, `width`, `height`, `bytes` and `markdown`. `markdown` uses the form DSH renders natively — `![alt](<percent-encoded absolute path>)` — which the chat client rewrites to its authenticated `/api/file?path=...` route, so it works in the browser and in the desktop shell alike; copy that line unchanged and the picture appears in the answer. `url` is the plugin's own `/plugins/dsh-diagram/asset?file=...` route, kept for anyone that wants the raw bytes over HTTP (it answers only an admitted DSH client).
102
102
 
103
103
  Defaults worth knowing: `diagram_render` uses width 1200, theme `dark` and scale 2 (output is twice the canvas size); `page_screenshot` uses a 1440×900 viewport, scale 2 and a 400 ms wait after load; `image_annotate` uses scale 2.
104
104
 
@@ -121,7 +121,7 @@ Annotation operations use these fields (defaults in parentheses):
121
121
  - The browser is driven over the DevTools protocol on `--remote-debugging-pipe` (fd 3 and fd 4, NUL-separated JSON) instead of a TCP port, so proxy environment variables that intercept localhost connections cannot break it.
122
122
  - Content height is measured from the body subtree rather than `scrollHeight`, because both `scrollHeight` and `Page.getLayoutMetrics` refuse to report less than the viewport height.
123
123
  - `image_annotate` renders the source image in a page and draws an SVG overlay on top; `blur` regions re-draw the image inside a CSS-filtered box.
124
- - PNGs are written by the DSH server process and served by the bundle's own route (`GET`/`HEAD`, known image MIME types only, `cache-control: no-store`).
124
+ - PNGs are written by the DSH server process and served by the bundle's own route (`GET`/`HEAD`, known image MIME types only, `cache-control: no-store`). The route runs the Host admission check first (`ctx.connection.requestRejection`), so it answers only an admitted DSH client and refuses a cross-site caller, and the Markdown link stays page-relative so both the web page and the desktop shell reach it through that same check.
125
125
 
126
126
  ## Limitations
127
127
 
package/README.zh-CN.md CHANGED
@@ -96,7 +96,7 @@ plugin_manager: { action: "install_bundle", target: "<path-to-checkout>" }
96
96
  }
97
97
  ```
98
98
 
99
- 返回结果里有 `path`、`url`、`width`、`height`、`bytes` 和 `markdown`;`markdown` 指向 DSH 服务的 `/plugins/dsh-diagram/asset?file=...`,只要原样复制就能内联显示。
99
+ 返回结果里有 `path`、`url`、`width`、`height`、`bytes` 和 `markdown`。`markdown` 用的是 DSH 原生渲染的写法——`![alt](<百分号编码的绝对路径>)`,聊天客户端会把它重写成自己的 `/api/file?path=...` 鉴权路由,浏览器和桌面壳都能显示;原样复制这一行就能在回答里出图。`url` 是插件自己的 `/plugins/dsh-diagram/asset?file=...`,留给需要走 HTTP 取原始字节的场合(只答已认证的 DSH 客户端)。
100
100
 
101
101
  几个值得记住的默认值:`diagram_render` 宽度 1200、主题 `dark`、倍率 2(输出是画布的 2 倍像素);`page_screenshot` 视口 1440×900、倍率 2、页面载入后额外等 400 ms;`image_annotate` 倍率 2。
102
102
 
@@ -119,7 +119,7 @@ plugin_manager: { action: "install_bundle", target: "<path-to-checkout>" }
119
119
  - 浏览器走 DevTools 协议的 `--remote-debugging-pipe`(fd 3 写、fd 4 读,NUL 分隔的 JSON),不开 TCP 端口,所以拦截 localhost 的代理环境变量影响不到它。
120
120
  - 内容高度不是读 `scrollHeight`:`scrollHeight` 和 `Page.getLayoutMetrics` 都不会小于视口高度,所以改为量 body 子树里最靠下的底边。
121
121
  - `image_annotate` 先把原图放进一个页面,再在上面叠一层 SVG;`blur` 区域则是在被裁剪的盒子里用 CSS 滤镜重绘原图。
122
- - PNG 由 DSH 服务进程写盘,并由本 bundle 自己的路由提供(只接受 `GET`/`HEAD`、只发已知图片类型、`cache-control: no-store`)。
122
+ - PNG 由 DSH 服务进程写盘,并由本 bundle 自己的路由提供(只接受 `GET`/`HEAD`、只发已知图片类型、`cache-control: no-store`)。路由先过 Host 的准入检查(`ctx.connection.requestRejection`):只答已认证的 DSH 客户端,跨站调用方拿不到字节;Markdown 链接保持页面相对,Web 页面和桌面壳都走同一条检查。
123
123
 
124
124
  ## 已知限制
125
125
 
package/index.js CHANGED
@@ -80,29 +80,35 @@ function resolveOut(raw, cwd, seed) {
80
80
  return path.join(dir, `${slugify(seed)}-${stamp()}.png`)
81
81
  }
82
82
 
83
- function origin(ctx) {
84
- try {
85
- return ctx.webServer.getOrigin()
86
- } catch {
87
- return `http://127.0.0.1:${ctx.webServer.port}`
88
- }
89
- }
90
-
91
- function assetMarkdown(ctx, file) {
83
+ // 资源地址保持页面相对:Web 端页面本来就由本 Host 提供,桌面壳把
84
+ // `dsh-app://app/...` 转发到 Host 时会带上会话 cookie;写成绝对地址反而
85
+ // 会被浏览器当成跨站请求,连 cookie 都不会带上。相对地址两边都成立。
86
+ function assetMarkdown(file) {
92
87
  let version = '0'
93
88
  try { version = String(Math.round(statSync(file).mtimeMs)) } catch {}
94
- return `${origin(ctx)}${ROUTE}?file=${encodeURIComponent(file)}&v=${version}`
89
+ return `${ROUTE}?file=${encodeURIComponent(file)}&v=${version}`
90
+ }
91
+
92
+ /**
93
+ * 聊天里的图走 DSH 自己的通道:AssistantMarkdown 会把「百分号编码的绝对路径」
94
+ * 重写成 /api/file?path=...,那条路由挂在 connection.fetch 上、按会话鉴权。
95
+ * 插件自建的 /plugins/... 路由在桌面壳里是另一个源、拿不到会话 cookie,
96
+ * 用它做内联只会显示「图片无法预览」——所以 markdown 用原生写法,
97
+ * route 只留给需要直接取字节的外部消费者(例如复制 url 到别处)。
98
+ */
99
+ function localImageMarkdown(file, alt) {
100
+ const cleanAlt = String(alt ?? '').replace(/[[\]]/g, '').replace(/\s+/g, ' ').trim()
101
+ return `![${cleanAlt || 'image'}](<${encodeURIComponent(path.resolve(file))}>)`
95
102
  }
96
103
 
97
- function imageResult(ctx, info, alt) {
98
- const url = assetMarkdown(ctx, info.path)
104
+ function imageResult(info, alt) {
99
105
  return stripUndefined({
100
106
  path: info.path,
101
- url,
107
+ url: assetMarkdown(info.path),
102
108
  width: info.width,
103
109
  height: info.height,
104
110
  bytes: info.bytes,
105
- markdown: `![${alt}](<${url}>)`,
111
+ markdown: localImageMarkdown(info.path, alt),
106
112
  })
107
113
  }
108
114
 
@@ -118,7 +124,29 @@ function imageRender(_args, value) {
118
124
  ]
119
125
  }
120
126
 
121
- function serveAsset(req, res) {
127
+ /**
128
+ * 向 Host 的 connection 服务要一次准入判断:Host/Origin 围栏 + 已认证的会话。
129
+ * 拿不到这个服务就一律拒绝(503):宁可这条路不可用,也不能在服务缺席时
130
+ * 静默放行——那正好是这个洞原来的样子。
131
+ */
132
+ function requestRejection(ctx, req) {
133
+ const connection = ctx.get?.('connection')
134
+ if (typeof connection?.requestRejection !== 'function') return 503
135
+ try {
136
+ return connection.requestRejection(req)
137
+ } catch {
138
+ return 403
139
+ }
140
+ }
141
+
142
+ function serveAsset(ctx, req, res) {
143
+ // 图片路由同样要过准入:否则用户浏览器里任意网页都能借这个端口探本机文件。
144
+ const rejection = requestRejection(ctx, req)
145
+ if (rejection !== undefined) {
146
+ res.writeHead(rejection, { 'content-type': 'text/plain; charset=utf-8' })
147
+ res.end()
148
+ return
149
+ }
122
150
  if (req.method !== 'GET' && req.method !== 'HEAD') {
123
151
  res.writeHead(405, { allow: 'GET, HEAD' })
124
152
  res.end()
@@ -190,7 +218,7 @@ function diagramRenderTool(ctx) {
190
218
  scale: clamp(args.scale, 2, 1, 3),
191
219
  outPath,
192
220
  })
193
- return imageResult(ctx, info, args.title || '图示')
221
+ return imageResult(info, args.title || '图示')
194
222
  },
195
223
  })
196
224
  }
@@ -232,7 +260,7 @@ function pageScreenshotTool(ctx) {
232
260
  if (extra > 0) await wait(extra)
233
261
  },
234
262
  })
235
- return imageResult(ctx, info, '截图')
263
+ return imageResult(info, '截图')
236
264
  },
237
265
  })
238
266
  }
@@ -310,7 +338,7 @@ function imageAnnotateTool(ctx) {
310
338
  scale: clamp(args.scale, 2, 1, 3),
311
339
  outPath,
312
340
  })
313
- return imageResult(ctx, info, path.basename(imageFile, path.extname(imageFile)))
341
+ return imageResult(info, path.basename(imageFile, path.extname(imageFile)))
314
342
  },
315
343
  })
316
344
  }
@@ -340,7 +368,7 @@ function promptSection() {
340
368
  }
341
369
 
342
370
  export function apply(ctx) {
343
- ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: ROUTE, handler: serveAsset }), 'dsh-diagram: image route')
371
+ ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: ROUTE, handler: (req, res) => serveAsset(ctx, req, res) }), 'dsh-diagram: image route')
344
372
  ctx.effect(() => ctx.tools.register(diagramRenderTool(ctx)), 'dsh-diagram: diagram_render')
345
373
  ctx.effect(() => ctx.tools.register(pageScreenshotTool(ctx)), 'dsh-diagram: page_screenshot')
346
374
  ctx.effect(() => ctx.tools.register(imageAnnotateTool(ctx)), 'dsh-diagram: image_annotate')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luosuqwq/dsh-diagram",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Renders explanatory diagrams, web pages and annotated images to PNG, and shows them inline in answers.",
5
5
  "author": "LuoSu <2681153600@qq.com>",
6
6
  "keywords": [