Files
.pouch/skills/previewer/references/contract.md
T

54 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Previewer 调用契约
## 输入
```text
node scripts/serve_file.js FILE [--mount-path PATH] [--port PORT] [--local-port PORT] [--ttl SECONDS]
```
- `FILE`:必须是存在的普通文件,建议使用绝对路径。
- `--mount-path`:以 `/` 开头的 URL 路径,不能包含 `?`、`#`、空段或 `..`;不提供时按父目录名生成。
- `--port`:Tailscale 对外 HTTP 端口,默认 `80`。
- `--local-port`:Node 监听的 localhost 端口,默认自动选择空闲端口。
- `--ttl`:服务存活时间,单位为秒,默认 `3600`(1 小时);到期同时关闭 Node 服务和对应
Tailscale Serve 路由。
Node 服务 `FILE` 的父目录,而不是把文件复制到其它地方。这样 HTML 可以加载同目录的 CSS、
图片和其它资源,但也意味着同目录其它文件会对 tailnet 可见。Node 只监听 `127.0.0.1`,
Tailscale Serve 负责把指定路径代理到这个本地端口。
## 输出
成功时输出单个 JSON 对象:
```json
{
"file": "/workspace/docs/prd-v002.html",
"directory": "/workspace/docs",
"mountPath": "/preview/billing",
"ip": "100.101.102.103",
"port": 80,
"localPort": 43721,
"pid": 12345,
"ttl": 3600,
"expiresAt": "2026-10-11T13:00:00.000Z",
"url": "http://100.101.102.103/preview/billing/prd-v002.html",
"stopCommand": "kill 12345 2>/dev/null || true; tailscale serve --http=80 --set-path=/preview/billing off"
}
```
调用方应把 `ip` 字段和 `url` 一起展示给用户:`ip` 是访问这次预览时应使用的 Tailscale
IPv4,`url` 是可直接打开的完整 HTTP 地址。不要把 `127.0.0.1` 当成远程访问地址,也不
要求用户必须使用 Tailscale hostname。
失败时输出到 stderr,退出码非 0;调用方不能把部分 stdout 当成有效 URL。
## 状态和冲突
脚本先读取 `tailscale serve status --json`,确认当前 Tailscale Serve 可用,然后启动 Node 本地
服务并追加指定 `--set-path` 路由。应为不同预览使用不同 mount path;脚本不会执行全量重置。
Serve 配置由 Tailscale 持久保存,但 Node 服务默认在 1 小时后自动关闭并清理对应路由。需要
提前停止时使用输出的命令,不能使用 `tailscale serve reset`,因为 reset 会清除该节点的全部
Serve 配置。