@allbluecn/collab-server 0.1.6 → 0.1.8
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 +44 -54
- package/package.json +2 -2
- package/src/exporters.ts +6 -3
- package/src/index.ts +28 -2
- package/src/share-room-limit.ts +39 -0
package/README.md
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
# @allbluecn/collab-server
|
|
2
2
|
|
|
3
|
-
AllBlue
|
|
3
|
+
AllBlue 多人协作模块(Yjs + Hocuspocus 4.7),规格见协作计划 §3(服务结构)/§4.2(ticket 验签)。
|
|
4
4
|
|
|
5
|
-
-
|
|
6
|
-
|
|
5
|
+
> **服务形态(P4 服务形态统一,任务 30/31/32,2026-11-27 用户裁决)**:主形态为 **web 后端内嵌模块**——同端口 `/collab` 路径分流(HTTP/WS 同端口,单监听面,对标 DSH 零独立服务 webserver),无独立进程/独立端口。SaaS 由 Pro 仓 platform 容器内嵌(任务 31);桌面端随 nitro 后端内嵌(任务 32)。**独立启动形态已退役**:仅显式 `COLLAB_STANDALONE=1` 回退门放行(退役计划中;本 README「standalone 回退形态」章节保留仅供回退与历史追溯,不再维护)。
|
|
6
|
+
|
|
7
|
+
- **鉴权**:连接时校验 `@allbluecn/collab-core/ticket` 的 HMAC ticket(验签 + exp + `ticket.room === 连接 room`),通过后按 `role` 落 `readOnly`(view 连接的写入被服务端丢弃并 nack,不广播)。内嵌/回退形态同链路。
|
|
8
|
+
- **内部应用通道**(§5.3 写入路由协议,任务 17):`POST /internal/apply`(standalone 回退形态与 WS 同端口;内部 HMAC 信封见 `@allbluecn/collab-core/internal`,域 `collab-internal-apply:v1`,ts 偏移 >60s 拒)。web 写入门卫在 CollabDocument 存在时把旁路 content 写入转发:**内嵌形态为进程内直调 `handleInternalApply`**(免 loopback,embed-registry 注册表探测),standalone 回退仍走 `COLLAB_INTERNAL_URL` loopback HTTP。room 在线 → 系统 client(userId=actor)整文档 replace 应用到 Y 并广播,落库走 onStoreDocument 统一链路;room 离线 → 直接落库业务列(视为旁路写,重种子化兜底);实体已删 → 409。非内部路由的原 HTTP 行为(`Welcome to Hocuspocus!`)不受监听器置换影响。
|
|
7
9
|
- **share room 上限**:单 `share:{token}` room 连接上限 **50**(§9),超限拒绝新连接(拒绝语义同鉴权失败,客户端收 `authenticationFailed`,reason `room-limit`);`COLLAB_SHARE_ROOM_LIMIT` 可配置(0 = 不设上限),非 share room 不受限。
|
|
8
10
|
- **room 注册表**:复用 `@allbluecn/collab-core/rooms` 的 `parseRoomName`(本 app 是纯服务端,直接 import 全部子路径,无客户端隔离约束)。
|
|
9
11
|
- **指标**:`src/metrics.ts` 周期打印连接数 / room 数 / update 体积(`COLLAB_METRICS=off` 关闭)。
|
|
10
|
-
- ⚠️ **单实例边界**:room 文档与 awareness 存单进程内存,多实例部署不共享(横向扩容前须 extension-redis,见 `src/index.ts`
|
|
12
|
+
- ⚠️ **单实例边界**:room 文档与 awareness 存单进程内存,多实例部署不共享(横向扩容前须 extension-redis,见 `src/index.ts` 顶注释)。任何部署形态(内嵌/PM2/Docker/Fly/Railway)都必须保持 **`instances: 1`,禁 cluster 多实例**。
|
|
13
|
+
- **嵌入消费入口**:宿主统一走 `@allbluecn/collab-server/embed` 门面(crossws hooks + env 解析/导出器装配);`createCollabServer` 增 `externalServer`/`wsPath` 支持监听器置换挂宿主 HTTP server。
|
|
11
14
|
|
|
12
15
|
## 运行形态(依赖解析决策)
|
|
13
16
|
|
|
@@ -15,74 +18,57 @@ AllBlue 多人协作的独立 WebSocket 服务(Yjs + Hocuspocus 4.7),规
|
|
|
15
18
|
|
|
16
19
|
| 场景 | 命令 | 说明 |
|
|
17
20
|
|---|---|---|
|
|
18
|
-
|
|
|
21
|
+
| 内嵌主形态 | —(随 web 进程装配) | dev:web 侧 `collab-embed-dev-plugin` 置换 vite 监听器;prod nitro:`routes/collab.ts` crossws hooks 注入 |
|
|
19
22
|
| 构建 | `pnpm build`(= esbuild bundle → `dist/index.mjs`) | collab-core 源码随包打入;`@hocuspocus/*`、`yjs`、`@allbluecn/database` external |
|
|
20
|
-
|
|
|
23
|
+
| standalone 回退(退役) | `COLLAB_STANDALONE=1 pnpm dev`(= `tsx src/index.ts`) | 退役门禁:非显式 `COLLAB_STANDALONE=1` 拒绝启动 |
|
|
21
24
|
|
|
22
25
|
## 环境变量
|
|
23
26
|
|
|
27
|
+
内嵌主形态仅需 `COLLAB_SECRET`(缺失则内嵌跳过,协作 offline 降级、web 正常启动);下表 `COLLAB_PORT`/`COLLAB_WEB_URL` 等仅 standalone 回退形态消费。全量缺省语义见仓根 / `apps/web` / `apps/collab` 三处 `.env.example`(P4 任务 30 已改内嵌缺省口径)。
|
|
28
|
+
|
|
24
29
|
| 变量 | 必填 | 默认 | 说明 |
|
|
25
30
|
|---|---|---|---|
|
|
26
|
-
| `COLLAB_SECRET` | ✅ | — | 与 web 签发端共享的 HMAC
|
|
27
|
-
| `COLLAB_PORT` | — | `3010` | 监听端口(worktree 并行开发约定 3030) |
|
|
31
|
+
| `COLLAB_SECRET` | ✅ | — | 与 web 签发端共享的 HMAC 密钥;内嵌缺失则内嵌跳过,standalone 缺失拒绝启动 |
|
|
28
32
|
| `DATABASE_URL` | ✅* | — | 持久层落库连接串;仅 `COLLAB_PERSISTENCE=off` 时可缺 |
|
|
29
33
|
| `COLLAB_PERSISTENCE` | — | on | `off` 退回纯内存(重启丢协同快照);开启但缺 `DATABASE_URL` 显式失败 |
|
|
30
34
|
| `COLLAB_SHARE_ROOM_LIMIT` | — | `50` | 单 share room 连接上限(0 = 不设上限) |
|
|
31
35
|
| `COLLAB_METRICS` | — | on | `off` 关闭周期指标日志 |
|
|
32
36
|
| `COLLAB_METRICS_INTERVAL_MS` | — | `60000` | 指标打印周期 |
|
|
33
|
-
| `
|
|
37
|
+
| `COLLAB_STANDALONE` | 回退 | 不设 | 独立启动门禁(退役裁决):不设=拒绝独立启动;显式 `1` 才放行 |
|
|
38
|
+
| `COLLAB_PORT` | 回退 | `3010` | standalone 监听端口(worktree 并行开发约定 3030) |
|
|
39
|
+
| `COLLAB_INTERNAL_URL` | 回退 | — | standalone 形态 web 服务端 → collab 的 HTTP 内部通道(内嵌为进程内直调,无需配置) |
|
|
40
|
+
| `COLLAB_WEB_URL` | 回退 | — | standalone 形态 collab → web 的向量索引重建通知目标(内嵌为进程内直调 `indexDocumentById`);缺省时索引滞后直至配置,协作与内容回写不受影响 |
|
|
34
41
|
|
|
35
42
|
\* 生产必填:协作快照/审计是产品承诺,默认开启持久层。
|
|
36
43
|
|
|
37
|
-
|
|
44
|
+
⚠ tsx 直跑不自动加载 `.env`——本地 dev 统一走根 `scripts/dev.sh` 注入(web 进程消费 `COLLAB_SECRET`/`DATABASE_URL`);standalone 回退绕过 dev.sh 单跑需自行 export。
|
|
38
45
|
|
|
39
46
|
## 本地开发
|
|
40
47
|
|
|
41
48
|
```bash
|
|
42
|
-
#
|
|
49
|
+
# 根目录两进程起全功能(web 3000 + admin 3001;collab 内嵌 web 同端口 /collab)
|
|
43
50
|
pnpm dev
|
|
44
51
|
|
|
45
|
-
#
|
|
46
|
-
pnpm --filter @allbluecn/collab-server dev
|
|
47
|
-
|
|
48
|
-
# 真实 WS 集成测试(Server port 0 随机端口 + HocuspocusProvider,覆盖鉴权五场景 + share room 上限)
|
|
52
|
+
# 单独跑 collab 测试(嵌入冒烟 + 鉴权 + share room 上限等)
|
|
49
53
|
pnpm --filter @allbluecn/collab-server test:run
|
|
50
54
|
|
|
51
|
-
#
|
|
52
|
-
curl http://localhost:
|
|
55
|
+
# 验证内嵌面存活(同端口路径分流;WS 升级探测返回 426 Upgrade Required)
|
|
56
|
+
curl http://localhost:3000/collab # → 426(Hocuspocus 升级面在位)
|
|
57
|
+
|
|
58
|
+
# standalone 回退形态(退役;显式门禁放行后才监听 3010)
|
|
59
|
+
COLLAB_STANDALONE=1 pnpm --filter @allbluecn/collab-server dev
|
|
53
60
|
```
|
|
54
61
|
|
|
55
62
|
## 部署
|
|
56
63
|
|
|
57
|
-
###
|
|
58
|
-
|
|
59
|
-
同机与 web 部署、nginx 做反代。**WebSocket 升级必须透传**(`Upgrade`/`Connection` 头),且反代读超时要大于 awareness 心跳间隔(Hocuspocus 连接期每 <30s 有消息,nginx 默认 60s 够用,压长更稳):
|
|
64
|
+
### 自托管单进程(主形态,内嵌)
|
|
60
65
|
|
|
61
|
-
|
|
62
|
-
# collab 独立子域方案(推荐):wss://collab.example.com → 127.0.0.1:3010
|
|
63
|
-
server {
|
|
64
|
-
server_name collab.example.com;
|
|
65
|
-
listen 443 ssl;
|
|
66
|
-
# ssl_certificate /etc/letsencrypt/live/collab.example.com/fullchain.pem;
|
|
67
|
-
# ssl_certificate_key /etc/letsencrypt/live/collab.example.com/privkey.pem;
|
|
68
|
-
|
|
69
|
-
location / {
|
|
70
|
-
proxy_pass http://127.0.0.1:3010;
|
|
71
|
-
proxy_http_version 1.1;
|
|
72
|
-
proxy_set_header Upgrade $http_upgrade; # WS 升级必需
|
|
73
|
-
proxy_set_header Connection "upgrade"; # WS 升级必需
|
|
74
|
-
proxy_set_header Host $host;
|
|
75
|
-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
76
|
-
proxy_read_timeout 300s; # > awareness 心跳窗口(30s)
|
|
77
|
-
proxy_send_timeout 300s;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
```
|
|
66
|
+
web 以 nitro node server 单进程跑(协作随内嵌面在位),反代只需透传 `/collab` 的 WS 升级(`Upgrade`/`Connection` 头),读超时大于 awareness 心跳间隔(Hocuspocus 连接期每 <30s 有消息,nginx 默认 60s 够用,压长更稳):
|
|
81
67
|
|
|
82
68
|
```nginx
|
|
83
|
-
#
|
|
69
|
+
# 同域路径分流:location /collab → web 单进程(内嵌 collab 同端口,无独立 collab 部署)
|
|
84
70
|
location /collab {
|
|
85
|
-
proxy_pass http://127.0.0.1:
|
|
71
|
+
proxy_pass http://127.0.0.1:3000;
|
|
86
72
|
proxy_http_version 1.1;
|
|
87
73
|
proxy_set_header Upgrade $http_upgrade;
|
|
88
74
|
proxy_set_header Connection "upgrade";
|
|
@@ -90,28 +76,32 @@ location /collab {
|
|
|
90
76
|
}
|
|
91
77
|
```
|
|
92
78
|
|
|
93
|
-
|
|
79
|
+
`VITE_COLLAB_WS_URL` 缺省同源 `/collab` 无需配置;仅反代/拆分部署才显式覆盖。
|
|
94
80
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
81
|
+
### SaaS / 桌面(Pro 仓)
|
|
82
|
+
|
|
83
|
+
- **SaaS**:platform 容器内嵌(任务 31)——Harness 常驻 Worker + collab 同进程,ingress(7080)分流 `/collab`,compose `platform + db` 两服务;见 Pro 仓 README「SaaS platform」章节。
|
|
84
|
+
- **桌面**:desktop nitro 后端内嵌(任务 32),`COLLAB_EMBEDDED=1` + `COLLAB_SECRET` 由 Electron main 注入,协作快照落 PGlite。
|
|
85
|
+
|
|
86
|
+
### standalone 回退形态(已退役,仅供回退与历史追溯)
|
|
98
87
|
|
|
99
|
-
|
|
88
|
+
> ⚠ **退役章节**:以下独立部署内容自 P4 任务 30/31 起为**退役形态**(`COLLAB_STANDALONE=1` 显式回退门),不再维护;仅当内嵌形态不可用需要临时回退时参考。Vercel/拆分部署的协作面:web 在 Vercel(serverless 无 WS 升级面,内嵌不适用)时协作 offline 降级,或以本回退形态独立部署 collab 并用 `VITE_COLLAB_WS_URL`/`COLLAB_INTERNAL_URL` 覆盖。
|
|
100
89
|
|
|
101
|
-
collab 是有状态 WebSocket 长连接服务,**不能跑在 Vercel
|
|
90
|
+
collab 是有状态 WebSocket 长连接服务,**不能跑在 Vercel 函数上**;回退形态独立部署到自有 VPS / Fly.io / Railway,配独立子域:
|
|
102
91
|
|
|
103
92
|
- 域名:`collab.你的域名` → collab 实例;web 端 env 配 `VITE_COLLAB_WS_URL=wss://collab.你的域名`(**生产必须 wss**,https 页面下 ws 会被浏览器 mixed-content 拦截)。
|
|
104
|
-
- Fly.io:`fly launch --no-deploy` 生成 `fly.toml` 后改 `internal_port = 3010`,Fly 边缘自动终止 TLS
|
|
105
|
-
- Railway:Node service 用 `apps/collab/Dockerfile`(build context=仓库根)或 Nixpacks(start 命令 `pnpm --filter @allbluecn/collab-server start`),Variables 配 `COLLAB_SECRET`/`DATABASE_URL`,TCP Proxy 暴露 3010 并绑定 `collab.*` 自定义域(Railway 自动 TLS);**Horizontal Scale = 1**。
|
|
93
|
+
- Fly.io:`fly launch --no-deploy` 生成 `fly.toml` 后改 `internal_port = 3010`,Fly 边缘自动终止 TLS;region 固定、`min_machines_running = 1`,**不开多实例**(见单实例边界)。模板参考 Fly 官方 Node 入门(https://fly.io/docs/languages-and-frameworks/node/),可直接用其内置 node builder,也可用 `apps/collab/Dockerfile`(build context 仍为仓库根,见下「Docker 镜像」)。
|
|
94
|
+
- Railway:Node service 用 `apps/collab/Dockerfile`(build context=仓库根)或 Nixpacks(start 命令 `pnpm --filter @allbluecn/collab-server start`),Variables 配 `COLLAB_SECRET`/`DATABASE_URL`/`COLLAB_STANDALONE=1`,TCP Proxy 暴露 3010 并绑定 `collab.*` 自定义域(Railway 自动 TLS);**Horizontal Scale = 1**。
|
|
106
95
|
- web 服务端 → collab 的 HTTP 内部通道配 `COLLAB_INTERNAL_URL`(同机 `http://127.0.0.1:3010`;Fly/Railway 内网用 `http://<internal-host>:3010`,勿绕公网)。
|
|
96
|
+
- nginx 独立子域反代样例与 PM2 单实例命令同历史文档口径(WS 升级透传 + 300s 读超时;`pm2 start dist/index.mjs --name allblue-collab -i 1`)。
|
|
107
97
|
|
|
108
|
-
### Docker
|
|
98
|
+
### Docker 镜像(回退形态专用,不再维护)
|
|
109
99
|
|
|
110
100
|
Dockerfile 位于 `apps/collab/Dockerfile`(仓根无 Dockerfile),**build context 为仓库根**(workspace 级文件 COPY + `COPY . .`)。三阶段:deps → builder → runner,esbuild 产物 + tini 前台启动。构建(**在仓库根执行**,`-f` 指定文件):
|
|
111
101
|
|
|
112
102
|
```bash
|
|
113
103
|
docker build -t allblue-collab -f apps/collab/Dockerfile .
|
|
114
|
-
docker run -e COLLAB_SECRET=... -e DATABASE_URL=... -p 3010:3010 allblue-collab
|
|
104
|
+
docker run -e COLLAB_STANDALONE=1 -e COLLAB_SECRET=... -e DATABASE_URL=... -p 3010:3010 allblue-collab
|
|
115
105
|
```
|
|
116
106
|
|
|
117
107
|
> ✅ **三缺口已修复(followup,本机 docker build 实测通过)**:
|
|
@@ -119,9 +109,9 @@ docker run -e COLLAB_SECRET=... -e DATABASE_URL=... -p 3010:3010 allblue-collab
|
|
|
119
109
|
> 2. builder 阶段先 `pnpm --filter @allbluecn/database exec prisma generate` 再 typecheck/test/build(collab 消费 generated client 类型面)。
|
|
120
110
|
> 3. `@allbluecn/database` 移出 esbuild external(Prisma 7 prisma-client generator Rust-free,纯 TS 可打包)→ `dist/index.mjs` 自包含持久层,不再依赖 Node 运行时类型剥离。
|
|
121
111
|
|
|
122
|
-
###
|
|
112
|
+
### 部署后自检(主形态)
|
|
123
113
|
|
|
124
114
|
```bash
|
|
125
|
-
curl https
|
|
115
|
+
curl https://你的域名/collab # → 426 Upgrade Required(TLS + 反代 + 内嵌面链路通)
|
|
126
116
|
# web 端打开任一协同文档,浏览器 DevTools → Network → WS → 101 Switching Protocols
|
|
127
117
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allbluecn/collab-server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"license": "AGPL-3.0-or-later",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "AllBlue Hocuspocus WebSocket 协作服务(Yjs 多人协同;主形态为 web 后端内嵌模块,独立启动为退役回退)",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@allbluecn/collab-core": "0.2.1",
|
|
20
|
-
"@allbluecn/database": "0.14.
|
|
20
|
+
"@allbluecn/database": "0.14.12",
|
|
21
21
|
"@hocuspocus/server": "^4.7.0",
|
|
22
22
|
"@tiptap/core": "3.31.3",
|
|
23
23
|
"@tiptap/extension-emoji": "3.31.3",
|
package/src/exporters.ts
CHANGED
|
@@ -137,7 +137,7 @@ export function resetRoomGuards(): void {
|
|
|
137
137
|
|
|
138
138
|
/**
|
|
139
139
|
* 执行 room 守卫。未注册该 kind = 实体存在性不归 collab 管(如 presence/
|
|
140
|
-
* share room),一律放行;guard 抛错 fail-
|
|
140
|
+
* share room),一律放行;guard 抛错 fail-closed(拒绝 + 记日志)。
|
|
141
141
|
*/
|
|
142
142
|
export async function runRoomGuard(room: CollabRoomName): Promise<boolean> {
|
|
143
143
|
const guard = guardRegistry.get(room.kind);
|
|
@@ -145,8 +145,11 @@ export async function runRoomGuard(room: CollabRoomName): Promise<boolean> {
|
|
|
145
145
|
try {
|
|
146
146
|
return await guard(room);
|
|
147
147
|
} catch (err) {
|
|
148
|
-
|
|
149
|
-
|
|
148
|
+
// collab-revoke-001:DB 异常 fail-closed(fail-open 放行=被撤销成员的写回不被切断)。
|
|
149
|
+
// 误杀面仅「DB 抖动窗口的快照写」(存在性守卫语义,可接受);拒绝=快照写被拒,
|
|
150
|
+
// 客户端重试即可恢复。
|
|
151
|
+
console.error(`[collab-exporters] room 守卫查询失败(fail-closed 拒绝)kind=${room.kind}:`, err);
|
|
152
|
+
return false;
|
|
150
153
|
}
|
|
151
154
|
}
|
|
152
155
|
|
package/src/index.ts
CHANGED
|
@@ -41,8 +41,11 @@ import {
|
|
|
41
41
|
type CollabPersistenceOptions,
|
|
42
42
|
} from "./persistence";
|
|
43
43
|
import {
|
|
44
|
+
assertRoomCapacity,
|
|
44
45
|
assertShareRoomCapacity,
|
|
46
|
+
DEFAULT_ROOM_LIMIT,
|
|
45
47
|
DEFAULT_SHARE_ROOM_LIMIT,
|
|
48
|
+
parseRoomLimit,
|
|
46
49
|
parseShareRoomLimit,
|
|
47
50
|
} from "./share-room-limit";
|
|
48
51
|
import { attachInternalApplyChannel } from "./internal-apply";
|
|
@@ -85,6 +88,11 @@ export interface CollabServerConfig {
|
|
|
85
88
|
* DEFAULT_SHARE_ROOM_LIMIT(50),0 = 不设上限。
|
|
86
89
|
*/
|
|
87
90
|
shareRoomLimit?: number;
|
|
91
|
+
/**
|
|
92
|
+
* 非 share room(实体/文档/presence)连接上限(collab-p3s:防单房资源耗尽);
|
|
93
|
+
* 缺省 DEFAULT_ROOM_LIMIT(200),0 = 不设上限。
|
|
94
|
+
*/
|
|
95
|
+
roomLimit?: number;
|
|
88
96
|
/** onStoreDocument debounce(Hocuspocus 内置配置);缺省 2000(§3.2) */
|
|
89
97
|
debounceMs?: number;
|
|
90
98
|
}
|
|
@@ -126,6 +134,12 @@ export function createCollabServer(config: CollabServerConfig): CollabServerInst
|
|
|
126
134
|
documentName,
|
|
127
135
|
config.shareRoomLimit ?? DEFAULT_SHARE_ROOM_LIMIT,
|
|
128
136
|
);
|
|
137
|
+
// collab-p3s:非 share room 连接上限(防单房资源耗尽拖垮整个 web 进程)
|
|
138
|
+
assertRoomCapacity(
|
|
139
|
+
instance,
|
|
140
|
+
documentName,
|
|
141
|
+
config.roomLimit ?? DEFAULT_ROOM_LIMIT,
|
|
142
|
+
);
|
|
129
143
|
// readOnly 必须落在 connectionConfig 上才驱动服务端拒写
|
|
130
144
|
// (view 连接的 update 被丢弃并 nack,不广播、不踢线);
|
|
131
145
|
// context 携带同一事实,供 onLoadDocument/onStateless 等 hook 使用。
|
|
@@ -165,6 +179,7 @@ export interface CollabEnvSettings {
|
|
|
165
179
|
metricsIntervalMs: number;
|
|
166
180
|
persistenceEnabled: boolean;
|
|
167
181
|
shareRoomLimit: number;
|
|
182
|
+
roomLimit: number;
|
|
168
183
|
}
|
|
169
184
|
|
|
170
185
|
/**
|
|
@@ -194,6 +209,9 @@ export function parseCollabEnvSettings(
|
|
|
194
209
|
// §9 share room 连接上限(默认 50;0 = 不设上限),非法值显式失败
|
|
195
210
|
const shareRoomLimit = parseShareRoomLimit(env.COLLAB_SHARE_ROOM_LIMIT);
|
|
196
211
|
|
|
212
|
+
// collab-p3s:非 share room 连接上限(默认 200;0 = 不设上限),防单房资源耗尽
|
|
213
|
+
const roomLimit = parseRoomLimit(env.COLLAB_ROOM_LIMIT);
|
|
214
|
+
|
|
197
215
|
// 持久层缺省开启(协作服务的快照/审计是产品承诺);显式 off 退回纯内存。
|
|
198
216
|
// 开启但缺 DATABASE_URL 时显式失败——静默降级会无声丢快照(配置缺失必须显式失败)。
|
|
199
217
|
const persistenceEnabled = env.COLLAB_PERSISTENCE !== "off";
|
|
@@ -205,7 +223,14 @@ export function parseCollabEnvSettings(
|
|
|
205
223
|
);
|
|
206
224
|
}
|
|
207
225
|
|
|
208
|
-
return {
|
|
226
|
+
return {
|
|
227
|
+
secret,
|
|
228
|
+
metricsEnabled,
|
|
229
|
+
metricsIntervalMs,
|
|
230
|
+
persistenceEnabled,
|
|
231
|
+
shareRoomLimit,
|
|
232
|
+
roomLimit,
|
|
233
|
+
};
|
|
209
234
|
}
|
|
210
235
|
|
|
211
236
|
/**
|
|
@@ -253,6 +278,7 @@ export async function startStandalone(
|
|
|
253
278
|
metrics: settings.metricsEnabled ? { intervalMs: settings.metricsIntervalMs } : false,
|
|
254
279
|
persistence: settings.persistenceEnabled ? {} : false,
|
|
255
280
|
shareRoomLimit: settings.shareRoomLimit,
|
|
281
|
+
roomLimit: settings.roomLimit,
|
|
256
282
|
});
|
|
257
283
|
// 回写导出器 + room 守卫注册(§5.2 回写,任务 16/21):生产入口显式装配。
|
|
258
284
|
if (settings.persistenceEnabled) {
|
|
@@ -267,7 +293,7 @@ export async function startStandalone(
|
|
|
267
293
|
}
|
|
268
294
|
await instance.server.listen();
|
|
269
295
|
console.log(
|
|
270
|
-
`[collab] Hocuspocus 协作服务已启动(standalone 回退形态,退役计划中)port=${instance.server.address.port} metrics=${settings.metricsEnabled ? `on(${settings.metricsIntervalMs}ms)` : "off"} persistence=${settings.persistenceEnabled ? "on" : "off"} shareRoomLimit=${settings.shareRoomLimit <= 0 ? "off" : settings.shareRoomLimit}`,
|
|
296
|
+
`[collab] Hocuspocus 协作服务已启动(standalone 回退形态,退役计划中)port=${instance.server.address.port} metrics=${settings.metricsEnabled ? `on(${settings.metricsIntervalMs}ms)` : "off"} persistence=${settings.persistenceEnabled ? "on" : "off"} shareRoomLimit=${settings.shareRoomLimit <= 0 ? "off" : settings.shareRoomLimit} roomLimit=${settings.roomLimit <= 0 ? "off" : settings.roomLimit}`,
|
|
271
297
|
);
|
|
272
298
|
if (instance.metrics && settings.metricsEnabled) {
|
|
273
299
|
instance.metrics.start(instance.server.hocuspocus, settings.metricsIntervalMs);
|
package/src/share-room-limit.ts
CHANGED
|
@@ -37,6 +37,9 @@ import { resolveRoomName } from "./rooms";
|
|
|
37
37
|
/** 单 share room 连接上限默认值(§9:50) */
|
|
38
38
|
export const DEFAULT_SHARE_ROOM_LIMIT = 50;
|
|
39
39
|
|
|
40
|
+
/** 单非 share room(实体/文档/presence)连接上限默认值(collab-p3s:防单房资源耗尽拖垮整个 web 进程) */
|
|
41
|
+
export const DEFAULT_ROOM_LIMIT = 200;
|
|
42
|
+
|
|
40
43
|
/**
|
|
41
44
|
* 解析 COLLAB_SHARE_ROOM_LIMIT:缺省/空 → 默认 50;0 → 不设上限;
|
|
42
45
|
* 非法(负数/非整数/非数字)抛错——配置缺失必须显式失败。
|
|
@@ -52,6 +55,21 @@ export function parseShareRoomLimit(raw: string | undefined): number {
|
|
|
52
55
|
return value;
|
|
53
56
|
}
|
|
54
57
|
|
|
58
|
+
/**
|
|
59
|
+
* 解析 COLLAB_ROOM_LIMIT:缺省/空 → 默认 200;0 → 不设上限;
|
|
60
|
+
* 非法(负数/非整数/非数字)抛错——配置缺失必须显式失败。
|
|
61
|
+
*/
|
|
62
|
+
export function parseRoomLimit(raw: string | undefined): number {
|
|
63
|
+
if (raw === undefined || raw.trim() === "") return DEFAULT_ROOM_LIMIT;
|
|
64
|
+
const value = Number(raw);
|
|
65
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`COLLAB_ROOM_LIMIT 非法(${raw}):须为非负整数(0 表示不设上限)`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
|
|
55
73
|
/**
|
|
56
74
|
* share room 容量守卫:超限 throw CollabAuthError("room-limit")
|
|
57
75
|
* (Hocuspocus 转写 permissionDenied 下发,客户端退避 offline)。
|
|
@@ -71,3 +89,24 @@ export function assertShareRoomCapacity(
|
|
|
71
89
|
throw new CollabAuthError("room-limit");
|
|
72
90
|
}
|
|
73
91
|
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 非 share room 容量守卫(collab-p3s):实体/文档/presence room 连接数超限
|
|
95
|
+
* throw CollabAuthError("room-limit")。此前非 share 房无连接上限,任一登录用户
|
|
96
|
+
* 可对同一 room 开无限连接 → 单 Y.Doc + 连接数资源耗尽拖垮内嵌的整个 web 进程。
|
|
97
|
+
* share 房走 assertShareRoomCapacity(各自独立上限)。
|
|
98
|
+
*/
|
|
99
|
+
export function assertRoomCapacity(
|
|
100
|
+
hocuspocus: { documents: Map<string, { getConnectionsCount(): number }> },
|
|
101
|
+
documentName: string,
|
|
102
|
+
limit: number,
|
|
103
|
+
): void {
|
|
104
|
+
if (limit <= 0) return;
|
|
105
|
+
const room = resolveRoomName(documentName);
|
|
106
|
+
if (!room || room.kind === "share") return;
|
|
107
|
+
const count =
|
|
108
|
+
hocuspocus.documents.get(documentName)?.getConnectionsCount() ?? 0;
|
|
109
|
+
if (count >= limit) {
|
|
110
|
+
throw new CollabAuthError("room-limit");
|
|
111
|
+
}
|
|
112
|
+
}
|