@anycon/sdk 0.1.1 → 0.3.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 +52 -14
- package/README.zh-CN.md +50 -14
- package/dist/attachments.d.ts +62 -14
- package/dist/attachments.d.ts.map +1 -1
- package/dist/attachments.js +126 -15
- package/dist/attachments.js.map +1 -1
- package/dist/client.d.ts +53 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +205 -3
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +3 -3
- package/dist/errors.js +1 -1
- package/dist/hub.d.ts +10 -0
- package/dist/hub.d.ts.map +1 -1
- package/dist/hub.js +18 -1
- package/dist/hub.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +27 -7
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +41 -10
- package/dist/node.js.map +1 -1
- package/dist/pair.d.ts.map +1 -1
- package/dist/pair.js +6 -1
- package/dist/pair.js.map +1 -1
- package/dist/rotation.d.ts +33 -0
- package/dist/rotation.d.ts.map +1 -0
- package/dist/rotation.js +41 -0
- package/dist/rotation.js.map +1 -0
- package/dist/session.d.ts +5 -3
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js.map +1 -1
- package/dist/storage.d.ts +11 -0
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +16 -2
- package/dist/storage.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +6 -6
- package/src/attachments.ts +167 -22
- package/src/client.ts +292 -2
- package/src/errors.ts +3 -3
- package/src/hub.ts +23 -1
- package/src/index.ts +13 -1
- package/src/node.ts +49 -13
- package/src/pair.ts +9 -2
- package/src/rotation.ts +50 -0
- package/src/session.ts +5 -3
- package/src/storage.ts +24 -2
- package/src/version.ts +1 -1
package/src/rotation.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 设备 token 轮换的策略常量和纯函数(docs/spec/sdk.md § 2,hub 侧见 docs/spec/hub.md § 5)。
|
|
3
|
+
*
|
|
4
|
+
* 轮换的发起和落盘在 `Client`,这里只放「什么时候该换」「多久再试一次」的判断,方便单独测。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** token 超过这么久(30 天)就该轮换。 */
|
|
8
|
+
export const TOKEN_MAX_AGE_MS = 30 * 24 * 3600 * 1000
|
|
9
|
+
/**
|
|
10
|
+
* 发出 `rotate_token` 之后没等到 `token_rotated`,多久后再发一次。比 hub 的「同一设备 1 分钟最多轮换一次」窗口长:
|
|
11
|
+
* 请求被当作「太频繁」忽略的话,重试时窗口已经过去;回复在同一条连接上丢了的话,hub 记着这条连接用的已经是新 token,
|
|
12
|
+
* 重试同样能成。重连之后不行:用旧 token 认证的连接不能轮换(docs/spec/hub.md § 5)。
|
|
13
|
+
*/
|
|
14
|
+
export const ROTATE_RETRY_MS = 75_000
|
|
15
|
+
/** 一条连接上头几次发送(间隔 [`ROTATE_RETRY_MS`])的次数;之后只按 [`ROTATE_RECHECK_MS`] 的节奏复查。 */
|
|
16
|
+
export const ROTATE_QUICK_SENDS = 3
|
|
17
|
+
/** 复查间隔:长连接可能连续几十天不重连,不能只在 `auth_ok` 时看一次;hub 一直不回复(旧版本)时也不会刷屏。 */
|
|
18
|
+
export const ROTATE_RECHECK_MS = 3600 * 1000
|
|
19
|
+
/** `client.rotateToken()` 等 `token_rotated` 的默认时间。 */
|
|
20
|
+
export const ROTATE_TIMEOUT_MS = 10_000
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* token 该不该轮换:签发时间未知、签发时间在未来(时钟被拨回过,当作未知)、或已满 `maxAgeMs`。
|
|
24
|
+
* 时间未知的老记录会在下次连上时换一次,之后就有记录了。
|
|
25
|
+
*/
|
|
26
|
+
export function isTokenDue(
|
|
27
|
+
issuedAtMs: unknown,
|
|
28
|
+
nowMs: number,
|
|
29
|
+
maxAgeMs: number = TOKEN_MAX_AGE_MS,
|
|
30
|
+
): boolean {
|
|
31
|
+
if (typeof issuedAtMs !== 'number' || !Number.isFinite(issuedAtMs)) return true
|
|
32
|
+
if (issuedAtMs > nowMs) return true
|
|
33
|
+
return nowMs - issuedAtMs >= maxAgeMs
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 已经发了 `sends` 次请求之后,下一次检查在多久以后。 */
|
|
37
|
+
export function nextRotationDelay(
|
|
38
|
+
sends: number,
|
|
39
|
+
retryMs: number = ROTATE_RETRY_MS,
|
|
40
|
+
recheckMs: number = ROTATE_RECHECK_MS,
|
|
41
|
+
): number {
|
|
42
|
+
return sends < ROTATE_QUICK_SENDS ? retryMs : recheckMs
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* `client.close()` 最多再等多久(从发出 `rotate_token` 起算)让还在路上的 `token_rotated` 落盘:hub 在回复之前就已经换了 token,
|
|
47
|
+
* 刚连上就关的短脚本不等的话,新 token 会丢,旧 token 60 秒后失效。回复很快(一个来回),所以几乎不会真等这么久;
|
|
48
|
+
* hub 不回复(旧版本、或 1 分钟内的第二次请求被忽略)时,只有发出请求后的头 2 秒里调 `close()` 才会多等。
|
|
49
|
+
*/
|
|
50
|
+
export const ROTATE_CLOSE_WAIT_MS = 2000
|
package/src/session.ts
CHANGED
|
@@ -74,10 +74,12 @@ export interface SendOptions {
|
|
|
74
74
|
/** 幂等键,默认随机 uuid。重发同一条消息时传同一个(附件也要一样,否则 `invalid`)。 */
|
|
75
75
|
messageId?: string
|
|
76
76
|
/**
|
|
77
|
-
*
|
|
77
|
+
* 附件:`node.uploadImage()` / `node.uploadFile()` 的结果,原样放进来(每条至多 20 个、合计 ≤ 20 MiB)。有附件时 `content` 可以是空串。
|
|
78
78
|
* daemon 逐个核对,拒绝时 `send()` 的迭代以 `AttachmentError` 失败:blob 不存在 / 已回收 → `reason = 'blob'`(重新上传);
|
|
79
|
-
* 已经用在别的会话里 → `blob_in_use`;合计超限 → `too_large`。agent
|
|
80
|
-
*
|
|
79
|
+
* 已经用在别的会话里 → `blob_in_use`;合计超限 → `too_large`。agent 原生收的类型(`capabilities.attachments`)交给 adapter,其余的
|
|
80
|
+
* daemon 复制到会话工作目录、路径附在发给 agent 的文字末尾(ADR-44)——事件流里的 `user_message` 仍是你发的原文;复制失败
|
|
81
|
+
* (磁盘满、该目录被换成符号链接…)时这一轮以 `error { code: 'attachment_failed' }` 结束,重发即可。旧节点(`acceptsAttachment`
|
|
82
|
+
* 为 false)不收这种类型 → `RpcError`(`unsupported`)。
|
|
81
83
|
*/
|
|
82
84
|
attachments?: AttachmentRef[]
|
|
83
85
|
/** 中止**迭代**(不会取消 turn;要取消用 `session.cancel()`)。 */
|
package/src/storage.ts
CHANGED
|
@@ -40,6 +40,11 @@ export interface StoredIdentity {
|
|
|
40
40
|
deviceId: string
|
|
41
41
|
/** hub 签发的设备 token。敏感。 */
|
|
42
42
|
deviceToken: string
|
|
43
|
+
/**
|
|
44
|
+
* 这个 token 的签发时间(配对完成或上次轮换成功的时刻,Unix 毫秒),轮换策略据此判断是否到期(`rotation.ts`)。
|
|
45
|
+
* 没有这个字段的旧记录当作「很久以前」,下次连上 hub 时会轮换一次。
|
|
46
|
+
*/
|
|
47
|
+
tokenIssuedAtMs?: number
|
|
43
48
|
/** Noise 静态私钥,hex。敏感。 */
|
|
44
49
|
secretKey: string
|
|
45
50
|
/** 配对时给 hub 看的显示名。 */
|
|
@@ -77,12 +82,29 @@ export class StateStore {
|
|
|
77
82
|
}
|
|
78
83
|
|
|
79
84
|
setIdentity(identity: StoredIdentity): Promise<void> {
|
|
80
|
-
return this.storage.set(IDENTITY_KEY, JSON.stringify(identity))
|
|
85
|
+
return this.#serial(() => this.storage.set(IDENTITY_KEY, JSON.stringify(identity)))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 轮换成功之后换上新 token 和它的签发时间:读—改—写在同一个串行区里,**一次** `storage.set` 写完整条身份记录
|
|
90
|
+
* (token 和签发时间不会只落一半)。本地已经没有身份(被吊销清掉了、或没配对过)时不写——不能把作废的身份又写回去——
|
|
91
|
+
* 返回 `false`。
|
|
92
|
+
*/
|
|
93
|
+
rotateIdentityToken(deviceToken: string, issuedAtMs: number): Promise<boolean> {
|
|
94
|
+
return this.#serial(async () => {
|
|
95
|
+
const identity = await this.getIdentity()
|
|
96
|
+
if (!identity) return false
|
|
97
|
+
await this.storage.set(
|
|
98
|
+
IDENTITY_KEY,
|
|
99
|
+
JSON.stringify({ ...identity, deviceToken, tokenIssuedAtMs: issuedAtMs }),
|
|
100
|
+
)
|
|
101
|
+
return true
|
|
102
|
+
})
|
|
81
103
|
}
|
|
82
104
|
|
|
83
105
|
/** 设备被吊销后清掉本地凭据(私钥 + token);节点公钥记录留着,重新配对会覆盖。 */
|
|
84
106
|
clearIdentity(): Promise<void> {
|
|
85
|
-
return this.storage.delete(IDENTITY_KEY)
|
|
107
|
+
return this.#serial(() => this.storage.delete(IDENTITY_KEY))
|
|
86
108
|
}
|
|
87
109
|
|
|
88
110
|
async getNodes(): Promise<StoredNode[]> {
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** SDK 版本。与 package.json 一致(`tests/version.test.ts` 守着)。 */
|
|
2
|
-
export const SDK_VERSION = '0.
|
|
2
|
+
export const SDK_VERSION = '0.3.0'
|