@coda-console/friends 0.6.0 → 0.7.0
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 +65 -7
- package/lib/friends.mjs +7 -4
- package/lib/gateway.mjs +72 -9
- package/lib/outbox.mjs +10 -5
- package/lib/rpc-caller.mjs +474 -0
- package/lib/rpc-format.mjs +288 -0
- package/lib/rpc-holder.mjs +723 -0
- package/lib/rpc-store.mjs +556 -0
- package/main.mjs +62 -4
- package/package.json +15 -2
- package/view/app.mjs +375 -36
- package/view/style.css +43 -0
package/README.md
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
- **本机身份**:身份卡显示本机的展示名、确定性几何纹头像、公钥短码(点按复制),下方状态条给出已互认好友数(有待互认、冲突时提示)与身份、证书状态。没有建身份时显示「未建身份」并提示前往客户端「设置 › 身份」创建。
|
|
6
6
|
- **公钥派生的昵称与头像**:本机和好友都没有可编辑的昵称、照片、Emoji 或主题色,统一由 Accord 公钥推出。展示名是 `昵称 #尾号`:昵称按 coda#3040 的算法取公钥 256 位,从最高位起每 7 位一组(至多 36 组),`0–77` 为字母、`78–87` 为数字、`88–127` 为结束符(已有不足 2 位时跳过,够 2 位就结束,读完仍不足 2 位则没有昵称);尾号是公钥按大端整数转成十进制(补零到 78 位)后的末 4 位。解不出昵称时退回公钥短码(`ed25519:` 之后前 8 位),还没有公钥的待互认好友用节点名。头像是由公钥确定性生成的 5×5 几何纹,灰色,换设备也一样;没有合法公钥时取名字首字。算法在 `lib/nickname.mjs`,浏览器与 Node 通用。
|
|
7
|
-
- **添加好友**:点身份卡上的「添加好友」,粘贴对方给的邀请(JSON、`coda-friend://` 链接或多行 `key=value
|
|
8
|
-
-
|
|
7
|
+
- **添加好友**:点身份卡上的「添加好友」,粘贴对方给的邀请(JSON、`coda-friend://` 链接或多行 `key=value`),接受后自动尝试互认。邀请也可以在同一个弹层里生成,见下文「生成与接受邀请」。
|
|
8
|
+
- **好友列表**:实时跟随好友表变化,宽屏双列。点好友打开详情:昵称、几何纹头像、完整公钥(可复制)、节点名、连接状态、「共享」(见下文「把远程执行授给好友」)、重新互认、二次确认后移除。
|
|
9
9
|
- **社交网关 `friends/social@1`**:业务无关,别的插件(在 `optionalUses` 里声明)经它读好友与好友分享的记录、向好友发布与撤回记录,不必各自处理没有身份、证书过期和重试。
|
|
10
10
|
|
|
11
11
|
## 几台电脑
|
|
@@ -18,6 +18,63 @@
|
|
|
18
18
|
- **推荐用法**:好友只在一台电脑上加。好友关系登记在那台电脑上,别的电脑不会自动出现这个好友。邀请里的入网码只能用一次,不必在每台上重复加。
|
|
19
19
|
- **几台电脑绑定同一个身份、又在同一个空间里时**,其他电脑发布的记录由 Coda 在这几台之间同步,好友从加了他的那台读取(见 Coda 仓库 `docs/peers.md`)。这是 Coda 基座的行为,本插件不另外保证,也不检查同步是否已经完成。
|
|
20
20
|
|
|
21
|
+
## 把远程执行授给好友
|
|
22
|
+
|
|
23
|
+
好友之间可以把一台机器上的 `remote-exec`(协议 `remote-exec/machine@1`)授给对方:对方的 Agent 经本机的代理记录,在你的机器上执行命令。`remote-exec` 本身不感知好友,签名、验签与转发都在本插件里;没装 `remote-exec` 的机器照常使用好友功能(清单里是可选实现与可选依赖)。
|
|
24
|
+
|
|
25
|
+
### 用法
|
|
26
|
+
|
|
27
|
+
- **授予**:在好友详情的「共享」一节,「我授给 TA」点「授予…」,选本机的一条远程执行记录。前提是已互认(已绑定好友通道),并且在 `remote-exec` 里打开了「允许本空间的机器在这台上执行」。
|
|
28
|
+
- **撤销**:授权那一行的菜单选「撤销」。撤销以本机为准,立即生效:还没启动的调用不再启动,已经在跑的发取消,授权先显示「撤销中」,取消在后面跟上,不弹确认。
|
|
29
|
+
- **用好友授给我的**:「TA 授给我」里的每一条是一条代理记录,名字见下面的「代理记录的名字」,同步在你的空间里。会话里像用本机的远程执行一样,对这条记录调用 `exec`;你空间里的别的机器也能用,签名和收回答由持有这条记录的那台机器做。
|
|
30
|
+
- **看别人做了什么**:持有方在「共享」里能看到最近的操作记录(谁、哪条授权、命令摘要与结局)。
|
|
31
|
+
|
|
32
|
+
### 代理记录的名字
|
|
33
|
+
|
|
34
|
+
名字固定为 `<机器名>@<公钥昵称>#<尾号>`(coda#3040),只由好友的公钥算出,与好友页上的展示名同一套算法(`lib/nickname.mjs`)。
|
|
35
|
+
|
|
36
|
+
- **公钥**:好友表里这位好友存下的作者:绑了个人身份的是个人钥匙,没绑的是机器钥匙;证书过期时仍是存下的那把,名字不跟着变。
|
|
37
|
+
- **昵称**:与基座的 `AccordNickname` 同一套:32 字节公钥从最高位起每 7 位一组,至多 36 组;0–77 是字母(对 26 取余),78–87 是数字,88–127 是结束(不足 2 位时跳过)。解不出昵称的用公钥前 8 位十六进制。
|
|
38
|
+
- **尾号**:完整尾号是公钥按大端无符号整数写成的十进制,补零到 78 位,记在代理记录的 `tail` 标签里。名字里默认写最后 4 位;本机同一 `<机器名>@<昵称>` 的代理记录最后 4 位也相同时,这几条都写到能互相区分的最短位数。
|
|
39
|
+
- **会话里的 `target`**:可以写 `机器名@昵称`(不撞名时)或 `机器名@昵称#尾号`(尾号从最后一位往前数的任意位数),这要求基座含 coda#3040;更早的基座只认完整名字或记录地址。
|
|
40
|
+
- **机器名**:是持有方自己报的,要能放进标题:含控制字符(C0、DEL、C1)、换行、双向控制符,或首尾有空白的,调用方不登记这条授权(不替换、不裁剪:改完的名字可能与别的记录相同);持有方授予时本机那台机器的名字不合这条,授予当场失败。`@`、`#`、中间的空格、非 ASCII 文字照旧可以。
|
|
41
|
+
- **名字的范围**:名字只在当下在场的记录里判唯一。另一位好友可以专门挖一把昵称相同、末 4 位也相同的钥匙(昵称越短越容易),再授出一台同名机器:两条都在时会被要求带更长的尾号;前一条不在时,后一条就是唯一匹配。基座(含 coda#3040)的保护是:会话开场表里列出过的名字,在这个会话里始终指开场时那条记录,那条没了就报不可用。脚本、Skill、定时任务这类跨会话的引用写记录地址(`coda://…/<UUID>`),地址不会换主人。
|
|
42
|
+
|
|
43
|
+
### 授权的含义
|
|
44
|
+
|
|
45
|
+
持有方始终以**机器**出现:授权是那台机器上的本机的事,授权记录与回答都用它的机器钥匙签,与它有没有绑个人身份、证书是否过期无关。
|
|
46
|
+
|
|
47
|
+
- **授给个人身份的好友**:就是授给这个人名下的机器,以及这些机器上被好友插件放行的会话。
|
|
48
|
+
- **授给机器身份的好友**(对方没绑个人身份,只装了后端):只授给对方那一台机器。
|
|
49
|
+
- 授权记下授予那一刻核验到的调用方作者。调用方后来绑了个人身份、解绑或换了根,旧授权与代理记录立即停用,要重新授予;持有方这一侧只有机器钥匙变了才停用:绑定、解绑个人身份或换根都不影响它授出去的授权。另外,目标记录删掉了,授权也随之结束(`target_missing`),之后重建同 id 的记录也不会恢复,要重新授予。
|
|
50
|
+
|
|
51
|
+
### 生成与接受邀请
|
|
52
|
+
|
|
53
|
+
- **生成**:在「添加好友」弹层的上半点「生成邀请」(需在 Coda 客户端里打开好友页)。邀请带入网码和**一次性口令** `bindingToken`,24 小时内有效,只能用一次;它是凭据,只显示这一次,关掉弹层就清掉。
|
|
54
|
+
- **接受**:弹层下半粘贴对方的邀请,接受后自动互认。没有口令的邀请会被拒绝,请对方重新生成。
|
|
55
|
+
- 邀请的字段、口令的校验与互认的细节见 Coda 仓库的 `docs/peers.md`,这里不再另列。
|
|
56
|
+
|
|
57
|
+
### 已知限制
|
|
58
|
+
|
|
59
|
+
- **同一系统用户**:被执行的命令和后端是同一个系统用户。本机的授权文件、操作记录(好友插件记的和 `remote-exec` 自己的)挡不住同一用户下的别的程序,`0600` 也挡不住;它们防得住误操作,防不住有意抹痕迹。
|
|
60
|
+
- **命令全文**:`remote-exec` 自己的记录存命令全文;本插件的操作记录默认只存前 80 字摘要与 sha256,不存 `cwd`、`stdin`(留 90 天)。
|
|
61
|
+
- **清理会延后**:
|
|
62
|
+
- 持有方的回答:到终态后满 24 小时才撤回;持有方用机器钥匙,不受证书影响。
|
|
63
|
+
- 调用方的请求:拿到结局后撤回,拿不到就等过期后撤回;离线或证书不可用时推迟到恢复以后。但如果过了期限再满 24 小时仍签不了名,本地的发出项会被清掉,之后不再补撤。
|
|
64
|
+
- **只转发 `exec`**:`pull`、`push` 在第一个字节之前报不支持,请改用 `exec` 分段读写。
|
|
65
|
+
- **撤销会取消在跑的调用**:取消后拿不到确定结局的,记为中断、效果未知;命令可能仍在对方机器上执行,重跑之前先检查。
|
|
66
|
+
- **按目录停用好友插件不会停止共享**:插件模块属于机器,在目录里停用好友插件后,对方的调用照常执行。要停止共享,请撤销授权或卸载插件。
|
|
67
|
+
- **移除好友只在本机断交**:只在执行移除的这一台删掉绑定、终止授权和目录同步。空间里别的机器若也和这位好友互认着,要分别移除。如果这位好友是你发邀请加进来的,对方的节点仍留在你的网络里:到得了各台后端的好友入口,读得到后端公开的绑定信息,但调不了命令、读不到记录正文;用过的邀请绑不回来,拿到你另一份有效的邀请则可以。要彻底收回,到控制服务器(Tailscale 或 Headscale)上删掉那个节点。
|
|
68
|
+
- **好友换身份的后果**:好友原来以机器身份互认,之后绑上个人身份,好友关系保留(coda#3041)。
|
|
69
|
+
- 你授给它的授权钉在原作者上,会按「被授权的好友换了作者」失效,要重新授一次。
|
|
70
|
+
- 它授给你的不受影响,只是代理的名字会跟着新作者变。
|
|
71
|
+
- **机器钥匙没有时限,也不能原地轮换**:持有方带个人身份的,换了机器要重新互认并重新授予;没有个人身份的,机器丢了只能移除这条好友通道。
|
|
72
|
+
- **调用方的证书**:调用方绑了个人身份就要靠客户端按时续签证书,过期后调用当场失败。
|
|
73
|
+
- **时钟**:两边时钟差超过 2 分钟,或本机时钟大幅回拨,都会拒绝或暂停准入。
|
|
74
|
+
- **断线后变慢**:连接断过一次后,回答可能晚 12 到 45 秒;75 秒内没有「运行中」回执,调用按 `peer_offline`、效果未知失败。
|
|
75
|
+
- **同根的别台机器也能用**:几台电脑绑定同一个身份、又在同一个空间里时,好友授给你的代理,别台机器也看得到、调得动,不必在每台上各自登记。调用经持有这条代理记录的那台转发,那台要在线。能看到是因为空间会同步代理记录,与是否同根无关。
|
|
76
|
+
- 配额:每个好友至多 8 个在途、4 个同时执行、每分钟 30 个新请求;全机 32、8、120。超限的请求持有方只记账、不回答,调用方 75 秒内收不到回执,按 `peer_offline`、效果未知失败。
|
|
77
|
+
|
|
21
78
|
## 社交网关
|
|
22
79
|
|
|
23
80
|
签名写入与记录查询全部透传 `coda/accord@1`,网关自己没有第二份目录库。本机网关记录(`friends/social@1`,key `local`)的 `status` 给出作者、证书状态(`no_identity` / `needs_cert` / `active`)、好友数、待发数与暂停的 kind。
|
|
@@ -25,24 +82,25 @@
|
|
|
25
82
|
| 操作 | 作用 |
|
|
26
83
|
| --- | --- |
|
|
27
84
|
| `friends`(读) | 互认的好友(`includePending` 带上待互认与冲突的),只来自 Accord 的好友表:`{ selfAuthor, certState, friends }`,每行的 `title` 是公钥派生的展示名,不读名片记录 |
|
|
28
|
-
| `records`(读) | 按 `kind`、`author`、`id`、`cursor` 读好友分享的记录,不读 `binding` |
|
|
85
|
+
| `records`(读) | 按 `kind`、`author`、`id`、`cursor` 读好友分享的记录,不读 `binding` 与 `friends/rpc-` 前缀 |
|
|
29
86
|
| `publish` | 发布 `{ kind, id, body, audience? }`;同作者同内容不重发,换作者重发 |
|
|
30
87
|
| `retract` | 撤回一条,或省略 `id` 撤回本机在这个 `kind` 下的全部(含队列里还没发出去的) |
|
|
31
88
|
| `outbox`(读) | 待发与已同步条目的状态,从不给出 `body` |
|
|
32
89
|
|
|
33
|
-
- `publish`、`retract` 拒绝保留类型 `binding`、`profile`、`friends`、`friends/card`、`coda/profile`、`coda/friends
|
|
90
|
+
- `publish`、`retract` 拒绝保留类型 `binding`、`profile`、`friends`、`friends/card`、`coda/profile`、`coda/friends` 与整个 `friends/rpc-` 前缀;`kind` 须是 Accord 的格式,`id` 至多 200 个字符、256 字节,`body` 是对象且 JSON 至多 64 KiB,`audience` 每项是 `public` 或 `ed25519:` 加 64 位十六进制,至多 500 项。
|
|
34
91
|
- 没有身份、证书过期(或发布遇到 `invalid_state`)、`kind` 被暂停、发布失败时,待发的 `body` 暂存在本机 `outbox.json`;同步成功就释放 `body`,只留去重用的 `lastAuthor`、`lastSyncedHash`、`lastSyncedAt`;撤回完成就删掉条目。证书恢复后自动补发、补撤;待撤的条目记着排队时的作者(`targetAuthor`),换了作者不会替新作者撤,没有身份时的撤回只清本机未发出的待发,不排待撤。意图先落盘再签名,签名成功立即落盘确认;`outbox.json`、`policy.json` 读不出来(不是缺失或损坏)就报错,不当空表。
|
|
35
|
-
- 旧版留在 `outbox.json` 里的保留类型条目(如已下线的名片 `friends/card
|
|
92
|
+
- 旧版留在 `outbox.json` 里的保留类型条目(如已下线的名片 `friends/card`、升级前入队的 `friends/rpc-*`)读表时直接丢掉,随启动或下一次冲洗落盘清理,不补发、也不挡着别的待发条目。插件不会去撤回 Accord 里历史上已发布的旧名片记录。
|
|
36
93
|
- 暂停与恢复某个 `kind` 的外发:`coda self policy.setKind '{"kind":"…","paused":true}'`,`coda self policy.get` 读现状(`policy.json`);暂停只拦发布,不拦撤回。
|
|
37
94
|
|
|
38
95
|
## 已下线的功能
|
|
39
96
|
|
|
40
|
-
名片编辑(昵称、照片、Emoji、字标、主题色、裁剪上传)、本机备注(`notes.json`、`coda self notes.*`)、`set-card`、私信 `send-dm`、动态 `post-feed` 与 `dm`、`feed`
|
|
97
|
+
名片编辑(昵称、照片、Emoji、字标、主题色、裁剪上传)、本机备注(`notes.json`、`coda self notes.*`)、`set-card`、私信 `send-dm`、动态 `post-feed` 与 `dm`、`feed` 两条帧流都已移除。插件本地写 `outbox.json`、`policy.json` 与「共享」用的 `rpc/` 目录(授权、发出项、操作记录);旧版留下的 `notes.json` 不再被读取。
|
|
41
98
|
|
|
42
99
|
## 权限与数据边界
|
|
43
100
|
|
|
44
101
|
- `friends/social@1` 的权限组:`read` 管 `friends`、`records`;`publish` 管 `publish`、`retract`、`outbox`。
|
|
45
|
-
- `uses`:`coda/accord@1` 的 `read`(读好友表)、`publish`(网关代别的插件发布)、`manage
|
|
102
|
+
- `uses`:`coda/accord@1` 的 `read`(读好友表)、`publish`(网关代别的插件发布)、`manage`(接受邀请、互认、移除)。`optionalUses`:`remote-exec/machine@1` 的 `use`(转给本机的远程执行,没装就没有这部分)。
|
|
103
|
+
- 保留种类 `friends/rpc-grant`、`friends/rpc-call`、`friends/rpc-answer`:只有本插件的内部入口能签发与读取,通用网关的发布、撤回、`records`、`outbox` 都不给出。
|
|
46
104
|
- 入网码(`authKey`)是凭据:只在提交时用一次,提交后立即清空输入框,不写入任何文件、日志或页面状态。
|
|
47
105
|
- 节点名等外来数据只当文本渲染(`textContent`),不当指令或 HTML。
|
|
48
106
|
- 好友页的 Accord 读写在页面交互时发起;模块只在 `activate` 时监听本机 Accord 记录(证书、好友表),用来刷新网关记录、证书恢复后冲洗待发队列。
|
package/lib/friends.mjs
CHANGED
|
@@ -5,11 +5,11 @@ import { identityTitle, shortAuthor } from './nickname.mjs';
|
|
|
5
5
|
export { shortAuthor };
|
|
6
6
|
|
|
7
7
|
// 邀请各字段的上限,与 coda/accord@1 的 accept 输入一致。
|
|
8
|
-
const LIMITS = { control: 500, authKey: 500, linkId: 64, scope: 520, hostname: 100, peerNode: 100 };
|
|
9
|
-
const LABELS = { control: '入网地址 control', authKey: '入网码 authKey', linkId: 'linkId', scope: 'scope', hostname: 'hostname', peerNode: 'peerNode', peerPort: 'peerPort' };
|
|
8
|
+
const LIMITS = { control: 500, authKey: 500, linkId: 64, scope: 520, hostname: 100, peerNode: 100, bindingToken: 200 };
|
|
9
|
+
const LABELS = { control: '入网地址 control', authKey: '入网码 authKey', linkId: 'linkId', scope: 'scope', hostname: 'hostname', peerNode: 'peerNode', peerPort: 'peerPort', bindingToken: 'bindingToken' };
|
|
10
10
|
const LINK_ID = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
11
11
|
const ALIASES = { controlurl: 'control', control_url: 'control', authkey: 'authKey', auth_key: 'authKey', linkid: 'linkId', link_id: 'linkId',
|
|
12
|
-
link: 'linkId', peernode: 'peerNode', peer_node: 'peerNode', peerport: 'peerPort', peer_port: 'peerPort' };
|
|
12
|
+
link: 'linkId', peernode: 'peerNode', peer_node: 'peerNode', peerport: 'peerPort', peer_port: 'peerPort', bindingtoken: 'bindingToken', binding_token: 'bindingToken' };
|
|
13
13
|
const length = text => [...text].length;
|
|
14
14
|
|
|
15
15
|
// ---- 邀请 ----
|
|
@@ -22,6 +22,7 @@ function fromObject(object) {
|
|
|
22
22
|
for (const [key, value] of Object.entries(object)) {
|
|
23
23
|
const name = field(key);
|
|
24
24
|
if (!name || value === undefined || value === null) continue;
|
|
25
|
+
if (name === 'bindingToken' && typeof value !== 'string') fail('bindingToken 须是文本');
|
|
25
26
|
if (typeof value !== 'string' && typeof value !== 'number') fail(`${LABELS[name]} 须是文本`);
|
|
26
27
|
found[name] = String(value);
|
|
27
28
|
}
|
|
@@ -50,7 +51,7 @@ function fromLines(text) {
|
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
/**
|
|
53
|
-
* 把粘贴的邀请解析成 accept 的入参:`{ linkId, control, authKey, scope?, hostname?,
|
|
54
|
+
* 把粘贴的邀请解析成 accept 的入参:`{ linkId, control, authKey, bindingToken, peerNode, scope?, hostname?, peerPort? }`。
|
|
54
55
|
* 接受 JSON 对象、coda-friend:// 或带查询串的链接、多行 key=value;错误抛出清晰的中文提示。
|
|
55
56
|
*/
|
|
56
57
|
export function parseInvite(raw) {
|
|
@@ -86,6 +87,8 @@ export function checked(input) {
|
|
|
86
87
|
}
|
|
87
88
|
if (!invite.control) fail('邀请缺少 control(入网地址)');
|
|
88
89
|
if (!invite.authKey) fail('邀请缺少 authKey(入网码)');
|
|
90
|
+
if (!invite.bindingToken) fail('邀请缺少 bindingToken(一次性口令),请让对方重新生成邀请');
|
|
91
|
+
if (!invite.peerNode) fail('邀请缺少 peerNode:bindingToken 须与 peerNode 一起给,请让对方重新生成邀请');
|
|
89
92
|
for (const [name, max] of Object.entries(LIMITS)) if (invite[name] && length(invite[name]) > max) fail(`${LABELS[name]} 太长(最多 ${max} 字)`);
|
|
90
93
|
const raw = input?.peerPort;
|
|
91
94
|
if (raw !== undefined && raw !== null && typeof raw !== 'number' && typeof raw !== 'string') fail('peerPort 须是 1–65535 的整数');
|
package/lib/gateway.mjs
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// 自己不建第二份目录库,只在 outbox.json 里留还没发出去的 body(同步成功即释放)。所有会写盘、会外发的动作排成一条队,
|
|
3
3
|
// 发布、撤回、证书变化引起的冲洗互不交叉。
|
|
4
4
|
import { mergeFriendRows, statusOf } from './friends.mjs';
|
|
5
|
-
import { canSkip, canonicalHash, certStateOf, checkId, checkKind, checkPublish, entryKey, holdState, keyOf, retractKey, invalid, isPending, markRejected, markSynced,
|
|
5
|
+
import { MAX_BODY_BYTES, canSkip, canonicalHash, certStateOf, checkId, checkKind, checkPublish, entryKey, holdState, keyOf, retractKey, invalid, isPending, markRejected, markSynced,
|
|
6
6
|
nextSeq, queuePublish, queueRetract, readOutbox, withState, writeOutbox } from './outbox.mjs';
|
|
7
7
|
import { readPolicy, setKindPaused } from './policy.mjs';
|
|
8
|
+
import { KIND_CALL, RPC_KINDS, isAuthor, isRpcKind, machineOf, senderField, signerOf } from './rpc-format.mjs';
|
|
8
9
|
|
|
9
10
|
const ACCORD = { kind: 'coda/accord@1', id: 'local' };
|
|
10
11
|
const SOCIAL = 'friends/social@1';
|
|
@@ -15,7 +16,7 @@ const interrupted = effects => Object.assign(new Error('好友网关已停止'),
|
|
|
15
16
|
|
|
16
17
|
export function createGateway(coda, dir) {
|
|
17
18
|
let stopped = false, watching = null, chain = Promise.resolve();
|
|
18
|
-
const handles = [];
|
|
19
|
+
const handles = [], accordListeners = new Set();
|
|
19
20
|
const stateDir = () => {
|
|
20
21
|
if (!dir) throw new Error('这台机器没有插件状态目录,网关的待发队列存不下');
|
|
21
22
|
return dir;
|
|
@@ -37,19 +38,20 @@ export function createGateway(coda, dir) {
|
|
|
37
38
|
return { accord, author: accord?.spec?.author ?? null, certState: certStateOf(accord) };
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
// 翻页读 coda/accord@1 的记录,至多
|
|
41
|
-
|
|
41
|
+
// 翻页读 coda/accord@1 的记录,至多 `maxPages` 页;cursor 重复就停,不会原地打转。`input.cursor` 给了就从那一页续读。
|
|
42
|
+
// 页数用完还没读全时带 `nextCursor`,下次从那里接着读;游标缺失或重复(续不上)时没有。
|
|
43
|
+
async function pages(input, maxPages = MAX_PAGES) {
|
|
42
44
|
const records = [], seen = new Set();
|
|
43
|
-
let cursor;
|
|
44
|
-
for (let page = 0; page <
|
|
45
|
+
let cursor = input.cursor;
|
|
46
|
+
for (let page = 0; page < maxPages; page++) {
|
|
45
47
|
const read = await coda.query(ACCORD, 'records', { ...input, limit: 200, ...(cursor ? { cursor } : {}) });
|
|
46
48
|
records.push(...(read?.records ?? []));
|
|
47
49
|
cursor = read?.nextCursor;
|
|
48
50
|
if (!read?.truncated) return { records, truncated: false };
|
|
49
|
-
if (!cursor || seen.has(cursor))
|
|
51
|
+
if (!cursor || seen.has(cursor)) return { records, truncated: true };
|
|
50
52
|
seen.add(cursor);
|
|
51
53
|
}
|
|
52
|
-
return { records, truncated: true };
|
|
54
|
+
return { records, truncated: true, nextCursor: cursor };
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
// 本机在 Accord 里有没有这条记录。
|
|
@@ -73,7 +75,7 @@ export function createGateway(coda, dir) {
|
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
|
|
76
|
-
// 读出待发队列;旧版残留的保留类型条目(如 friends/card
|
|
78
|
+
// 读出待发队列;旧版残留的保留类型条目(如 friends/card、升级前入队的 friends/rpc-*)已被读表丢掉,这里把清理落盘,之后不再补发,也不挡着别的条目。
|
|
77
79
|
async function purgeReserved() {
|
|
78
80
|
const outbox = await readOutbox(stateDir());
|
|
79
81
|
if (outbox.dropped) await persist(outbox);
|
|
@@ -168,6 +170,7 @@ export function createGateway(coda, dir) {
|
|
|
168
170
|
|
|
169
171
|
function records({ kind, author, id, limit, cursor, includeAudience }) {
|
|
170
172
|
if (kind === 'binding') throw invalid('kind「binding」是保留类型,网关不读它');
|
|
173
|
+
if (isRpcKind(kind)) throw invalid(`kind「${kind}」是保留类型,网关不读它`);
|
|
171
174
|
if (id !== undefined) checkId(id, { query: true });
|
|
172
175
|
return coda.query(ACCORD, 'records', { kind, ...(author === undefined ? {} : { author }), ...(id === undefined ? {} : { id }),
|
|
173
176
|
...(limit === undefined ? {} : { limit }), ...(cursor === undefined ? {} : { cursor }), ...(includeAudience === undefined ? {} : { includeAudience }) });
|
|
@@ -308,6 +311,58 @@ export function createGateway(coda, dir) {
|
|
|
308
311
|
return { author, certState, pausedKinds, pendingCount: Object.values(entries).filter(isPending).length, items };
|
|
309
312
|
}
|
|
310
313
|
|
|
314
|
+
// ---- 内部签名入口 ----
|
|
315
|
+
// 只给好友插件自己的持有方、调用方用,不登记成 friends/social@1 的操作、也不经 coda self:签 friends/rpc-grant、rpc-call、rpc-answer。
|
|
316
|
+
// 受众必须是单人;发送方取自正文(grant、answer 是 holder,call 是 caller),先与本机此刻该署的作者核对,不符当场失败;符合就把它作为
|
|
317
|
+
// expectAuthor 传给 coda/accord@1.publish,实际署名的作者不符 Accord 拒签;不进待发队列。
|
|
318
|
+
// 该署谁按种类分(signingAs):
|
|
319
|
+
// - 授权记录与回答是持有方发的,持有方始终以机器出现:带 `signer: "machine"` 用本机机器钥匙签,不看有没有绑个人身份、证书是否有效;
|
|
320
|
+
// - 调用请求署本机此刻的作者(signerOf):机器作者(没绑个人身份)用机器钥匙签,不看证书;个人作者的证书不是 active(已过期、未生效)当场失败,什么也不发。
|
|
321
|
+
async function signingAs(kind) {
|
|
322
|
+
const { accord } = await accordState();
|
|
323
|
+
live();
|
|
324
|
+
if (kind === KIND_CALL) return signerOf(accord);
|
|
325
|
+
const machine = machineOf(accord);
|
|
326
|
+
return { author: machine, canSign: machine !== null, signer: 'machine' };
|
|
327
|
+
}
|
|
328
|
+
const missing = kind => kind === KIND_CALL ? '本机没有可用的签名证书' : '读不出本机的机器钥匙';
|
|
329
|
+
|
|
330
|
+
// `beforePublish`(可省):读完签名身份、真正发布之前最后调一次,它抛错就什么也不发(调用方在这里看取消与期限)。
|
|
331
|
+
async function signRpc({ kind, id, body, audience } = {}, { beforePublish } = {}) {
|
|
332
|
+
live();
|
|
333
|
+
if (!RPC_KINDS.includes(kind)) throw invalid(`只能签 ${RPC_KINDS.join('、')}`);
|
|
334
|
+
checkId(id);
|
|
335
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) throw invalid('body 须是对象');
|
|
336
|
+
if (Buffer.byteLength(JSON.stringify(body)) > MAX_BODY_BYTES) throw invalid(`body 最多 ${MAX_BODY_BYTES} 字节`);
|
|
337
|
+
if (!Array.isArray(audience) || audience.length !== 1 || !isAuthor(audience[0])) throw invalid('audience 须恰好是一位好友的公钥');
|
|
338
|
+
const sender = body[senderField(kind)];
|
|
339
|
+
if (!isAuthor(sender)) throw invalid(`body.${senderField(kind)} 须写明发送方的公钥`);
|
|
340
|
+
const { author, canSign, signer } = await signingAs(kind);
|
|
341
|
+
if (!canSign) throw coda.error('invalid_state', `${missing(kind)},好友调用的记录没有发出`, { effects: 'none' });
|
|
342
|
+
if (author !== sender) throw coda.error('invalid_state', '本机此刻该署的作者不是这条记录写的发送方,好友调用的记录没有发出', { effects: 'none' });
|
|
343
|
+
beforePublish?.();
|
|
344
|
+
return coda.call(ACCORD, 'publish', { kind, id, body, audience, ...(signer ? { signer } : {}), expectAuthor: sender });
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 撤回同一入口签过的一条:同一种类、同一 id,正文为空、受众为空。`author` 是这条该署的作者(授权记录与回答是本机机器钥匙,
|
|
348
|
+
// 调用请求是本机此刻的作者),作为 expectAuthor 传下去;只撤这个作者署名的、Accord 里还在的记录:没有就给 `{ state: 'absent' }`,
|
|
349
|
+
// 不为从没发过的 id 生成墓碑。签不了(见 signRpc)、或此刻该署的不是 `author`,当场失败(invalid_state),什么也不发。
|
|
350
|
+
async function retractRpc({ kind, id, author } = {}) {
|
|
351
|
+
live();
|
|
352
|
+
if (!RPC_KINDS.includes(kind)) throw invalid(`只能撤 ${RPC_KINDS.join('、')}`);
|
|
353
|
+
checkId(id);
|
|
354
|
+
if (!isAuthor(author)) throw invalid('author 须是本机此刻应署的作者');
|
|
355
|
+
const current = await signingAs(kind);
|
|
356
|
+
if (!current.canSign) throw coda.error('invalid_state', `${missing(kind)},好友调用的记录没有撤回`, { effects: 'none' });
|
|
357
|
+
if (current.author !== author) throw coda.error('invalid_state', '本机此刻该署的作者不是这条记录的作者,好友调用的记录没有撤回', { effects: 'none' });
|
|
358
|
+
const read = await coda.query(ACCORD, 'records', { kind, author, id, limit: 1 });
|
|
359
|
+
const held = (read?.records ?? []).some(record => record?.local === true && record.author === author && record.id === id && Object.keys(record.body ?? {}).length > 0);
|
|
360
|
+
live();
|
|
361
|
+
if (!held) return { state: 'absent' };
|
|
362
|
+
const result = await coda.call(ACCORD, 'publish', { kind, id, body: {}, audience: [], ...(current.signer ? { signer: current.signer } : {}), expectAuthor: author });
|
|
363
|
+
return { state: 'retracted', ...(Number.isSafeInteger(result?.version) ? { version: result.version } : {}) };
|
|
364
|
+
}
|
|
365
|
+
|
|
311
366
|
const protocolHandlers = { friends, records, publish, retract, outbox };
|
|
312
367
|
for (const [name, fn] of Object.entries(protocolHandlers)) handles.push(coda.handle(SOCIAL, name, input => fn(input ?? {})));
|
|
313
368
|
|
|
@@ -315,6 +370,7 @@ export function createGateway(coda, dir) {
|
|
|
315
370
|
const onChange = ({ type, entry } = {}) => {
|
|
316
371
|
const ready = type !== 'DELETED' && certStateOf(entry) === 'active';
|
|
317
372
|
serial(async () => { if (ready) await flushNow(); await refreshNow(); }).catch(() => {});
|
|
373
|
+
for (const listener of accordListeners) { try { listener({ type, entry }); } catch { /* 听的人出错不影响网关 */ } }
|
|
318
374
|
};
|
|
319
375
|
|
|
320
376
|
return {
|
|
@@ -330,6 +386,12 @@ export function createGateway(coda, dir) {
|
|
|
330
386
|
});
|
|
331
387
|
},
|
|
332
388
|
},
|
|
389
|
+
// 内部签名、撤回入口,见上;不对外登记。
|
|
390
|
+
signRpc, retractRpc,
|
|
391
|
+
// 翻页读 Accord 记录(见上),持有方扫请求用。
|
|
392
|
+
pages,
|
|
393
|
+
// 本机 Accord 记录的变化(网关已在 watch 它),转告给持有方等内部的听众;给回 dispose。
|
|
394
|
+
onAccord(listener) { accordListeners.add(listener); return { dispose: () => accordListeners.delete(listener) }; },
|
|
333
395
|
async start() {
|
|
334
396
|
if (dir) await serial(purgeReserved).catch(() => {});
|
|
335
397
|
try { watching = await coda.watch(ACCORD.kind, onChange, { id: ACCORD.id }); } catch { /* 没有 Accord:网关记录照样写 */ }
|
|
@@ -340,6 +402,7 @@ export function createGateway(coda, dir) {
|
|
|
340
402
|
for (const handle of handles.splice(0)) handle?.dispose?.();
|
|
341
403
|
watching?.dispose?.();
|
|
342
404
|
watching = null;
|
|
405
|
+
accordListeners.clear();
|
|
343
406
|
},
|
|
344
407
|
};
|
|
345
408
|
}
|
package/lib/outbox.mjs
CHANGED
|
@@ -3,9 +3,14 @@
|
|
|
3
3
|
// 同步成功就释放 body,只留去重用的轻量元数据;撤回完成就删掉条目。
|
|
4
4
|
import { createHash } from 'node:crypto';
|
|
5
5
|
import { readJson, writeJson } from './json-file.mjs';
|
|
6
|
+
import { isRpcKind } from './rpc-format.mjs';
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* 网关不替业务发布、撤回的类型:好友关系、名片等 Accord 自己的记录;friends/card 是已下线的旧名片,封住旧写路径。
|
|
10
|
+
* 另有整个 `friends/rpc-` 前缀(按前缀判,见 `isRpcKind`),只有网关的内部入口能签。
|
|
11
|
+
*/
|
|
8
12
|
export const RESERVED_KINDS = new Set(['binding', 'profile', 'friends', 'coda/profile', 'coda/friends', 'friends/card']);
|
|
13
|
+
const isReserved = kind => RESERVED_KINDS.has(kind) || isRpcKind(kind);
|
|
9
14
|
export const MAX_ID_CHARS = 200, MAX_ID_BYTES = 256, MAX_BODY_BYTES = 65536, MAX_AUDIENCE = 500;
|
|
10
15
|
const KIND = /^[a-z][a-z0-9-]{0,63}(?:\/[a-z][a-z0-9-]{0,63})?$/;
|
|
11
16
|
const AUDIENCE = /^(?:public|ed25519:[0-9a-f]{64})$/;
|
|
@@ -17,12 +22,12 @@ const isPlain = value => {
|
|
|
17
22
|
return proto === Object.prototype || proto === null;
|
|
18
23
|
};
|
|
19
24
|
|
|
20
|
-
export const validKind = kind => typeof kind === 'string' && KIND.test(kind) && !
|
|
25
|
+
export const validKind = kind => typeof kind === 'string' && KIND.test(kind) && !isReserved(kind);
|
|
21
26
|
|
|
22
27
|
/** kind 须符合 Accord 的格式,且不是保留类型。 */
|
|
23
28
|
export function checkKind(kind) {
|
|
24
29
|
if (typeof kind !== 'string' || !KIND.test(kind)) throw invalid('kind 须是小写字母开头的 name 或 ns/name(只含小写字母、数字、连字符,各段最长 64 位)');
|
|
25
|
-
if (
|
|
30
|
+
if (isReserved(kind)) throw invalid(`kind「${kind}」是保留类型,网关不发布也不撤回它`);
|
|
26
31
|
return kind;
|
|
27
32
|
}
|
|
28
33
|
|
|
@@ -115,7 +120,7 @@ export function holdState(entry, certState, pausedKinds) {
|
|
|
115
120
|
|
|
116
121
|
/**
|
|
117
122
|
* 读出整张表 `{ 键: 条目 }`(磁盘上是 `{ seq, entries }`,旧版是直接平铺的条目);键按条目内容重新规范(兼容旧键名),
|
|
118
|
-
* 缺失、损坏、条目不成样子的都丢掉;保留类型(含旧版残留的 friends/card
|
|
123
|
+
* 缺失、损坏、条目不成样子的都丢掉;保留类型(含旧版残留的 friends/card 与升级前入队的 friends/rpc-*)的条目也丢掉,丢了的个数记在表上不可枚举的 `dropped`,
|
|
119
124
|
* 由冲洗落盘清理。序号计数器取顶层 `seq` 与现存条目最大序号中较大的。
|
|
120
125
|
*/
|
|
121
126
|
export async function readOutbox(dir) {
|
|
@@ -126,7 +131,7 @@ export async function readOutbox(dir) {
|
|
|
126
131
|
for (const entry of Object.values(stored)) {
|
|
127
132
|
if (isPlain(entry) && typeof entry.kind === 'string' && typeof entry.id === 'string' && ['publish', 'retract'].includes(entry.desired) && typeof entry.state === 'string'
|
|
128
133
|
&& (entry.desired === 'publish' || typeof entry.targetAuthor === 'string')) {
|
|
129
|
-
if (
|
|
134
|
+
if (isReserved(entry.kind)) dropped++; else entries[keyOf(entry)] = entry;
|
|
130
135
|
}
|
|
131
136
|
}
|
|
132
137
|
Object.defineProperty(entries, 'dropped', { value: dropped });
|