@celljs/ta-relay 4.10.0 → 4.12.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.
Files changed (118) hide show
  1. package/README.en.md +13 -1
  2. package/README.md +13 -1
  3. package/esm/common/index.d.ts +11 -0
  4. package/esm/common/index.js +11 -0
  5. package/esm/common/relay-blobs.d.ts +54 -0
  6. package/esm/common/relay-blobs.js +153 -0
  7. package/esm/common/relay-card.d.ts +23 -0
  8. package/esm/common/relay-card.js +30 -0
  9. package/esm/common/relay-content.d.ts +104 -0
  10. package/esm/common/relay-content.js +316 -0
  11. package/esm/common/relay-conversation.d.ts +59 -0
  12. package/esm/common/relay-conversation.js +9 -0
  13. package/esm/common/relay-faces.d.ts +32 -0
  14. package/esm/common/relay-faces.js +208 -0
  15. package/esm/common/relay-hub-util.d.ts +91 -0
  16. package/esm/common/relay-hub-util.js +164 -0
  17. package/esm/common/relay-hub.d.ts +125 -0
  18. package/esm/common/relay-hub.js +438 -0
  19. package/esm/common/relay-hubs-connection.d.ts +98 -0
  20. package/esm/common/relay-hubs-connection.js +505 -0
  21. package/esm/common/relay-hubs-protocol.d.ts +130 -0
  22. package/esm/common/relay-hubs-protocol.js +2 -0
  23. package/esm/common/relay-hubs.d.ts +61 -0
  24. package/esm/common/relay-hubs.js +351 -0
  25. package/esm/common/relay-identity.d.ts +46 -0
  26. package/esm/common/relay-identity.js +126 -0
  27. package/esm/common/relay-protocol.d.ts +211 -2
  28. package/esm/common/relay-protocol.js +53 -0
  29. package/esm/common/relay-room-chat.d.ts +109 -0
  30. package/esm/common/relay-room-chat.js +176 -0
  31. package/esm/common/relay-socket.d.ts +8 -0
  32. package/esm/common/relay-socket.js +23 -2
  33. package/esm/guest/guest-i18n.d.ts +50 -0
  34. package/esm/guest/guest-i18n.js +103 -0
  35. package/esm/guest/guest-style.d.ts +1 -0
  36. package/esm/guest/guest-style.js +85 -0
  37. package/esm/guest/guest.d.ts +1 -0
  38. package/esm/guest/guest.js +384 -0
  39. package/esm/node/index.d.ts +2 -0
  40. package/esm/node/index.js +2 -0
  41. package/esm/node/relay-host.d.ts +25 -3
  42. package/esm/node/relay-host.js +115 -4
  43. package/esm/node/relay-hub-files.d.ts +12 -0
  44. package/esm/node/relay-hub-files.js +39 -0
  45. package/esm/node/relay-server.d.ts +46 -1
  46. package/esm/node/relay-server.js +227 -1
  47. package/esm/node/relay-store.d.ts +72 -0
  48. package/esm/node/relay-store.js +123 -2
  49. package/esm/node/relay-viewer.d.ts +7 -0
  50. package/esm/node/relay-viewer.js +33 -0
  51. package/esm/viewer/viewer-i18n.d.ts +56 -0
  52. package/esm/viewer/viewer-i18n.js +117 -0
  53. package/esm/viewer/viewer-markdown.d.ts +7 -0
  54. package/esm/viewer/viewer-markdown.js +198 -0
  55. package/esm/viewer/viewer-style.d.ts +1 -0
  56. package/esm/viewer/viewer-style.js +72 -0
  57. package/esm/viewer/viewer.d.ts +8 -0
  58. package/esm/viewer/viewer.js +378 -0
  59. package/lib/common/index.d.ts +11 -0
  60. package/lib/common/index.js +11 -0
  61. package/lib/common/relay-blobs.d.ts +55 -0
  62. package/lib/common/relay-blobs.js +157 -0
  63. package/lib/common/relay-card.d.ts +24 -0
  64. package/lib/common/relay-card.js +33 -0
  65. package/lib/common/relay-content.d.ts +105 -0
  66. package/lib/common/relay-content.js +320 -0
  67. package/lib/common/relay-conversation.d.ts +60 -0
  68. package/lib/common/relay-conversation.js +12 -0
  69. package/lib/common/relay-faces.d.ts +33 -0
  70. package/lib/common/relay-faces.js +211 -0
  71. package/lib/common/relay-hub-util.d.ts +92 -0
  72. package/lib/common/relay-hub-util.js +167 -0
  73. package/lib/common/relay-hub.d.ts +126 -0
  74. package/lib/common/relay-hub.js +442 -0
  75. package/lib/common/relay-hubs-connection.d.ts +99 -0
  76. package/lib/common/relay-hubs-connection.js +510 -0
  77. package/lib/common/relay-hubs-protocol.d.ts +131 -0
  78. package/lib/common/relay-hubs-protocol.js +3 -0
  79. package/lib/common/relay-hubs.d.ts +62 -0
  80. package/lib/common/relay-hubs.js +355 -0
  81. package/lib/common/relay-identity.d.ts +47 -0
  82. package/lib/common/relay-identity.js +129 -0
  83. package/lib/common/relay-protocol.d.ts +211 -2
  84. package/lib/common/relay-protocol.js +54 -1
  85. package/lib/common/relay-room-chat.d.ts +110 -0
  86. package/lib/common/relay-room-chat.js +180 -0
  87. package/lib/common/relay-socket.d.ts +8 -0
  88. package/lib/common/relay-socket.js +23 -2
  89. package/lib/guest/guest-i18n.d.ts +51 -0
  90. package/lib/guest/guest-i18n.js +106 -0
  91. package/lib/guest/guest-style.d.ts +2 -0
  92. package/lib/guest/guest-style.js +88 -0
  93. package/lib/guest/guest.d.ts +2 -0
  94. package/lib/guest/guest.js +386 -0
  95. package/lib/node/index.d.ts +2 -0
  96. package/lib/node/index.js +2 -0
  97. package/lib/node/relay-host.d.ts +25 -3
  98. package/lib/node/relay-host.js +115 -4
  99. package/lib/node/relay-hub-files.d.ts +13 -0
  100. package/lib/node/relay-hub-files.js +43 -0
  101. package/lib/node/relay-server.d.ts +46 -1
  102. package/lib/node/relay-server.js +226 -0
  103. package/lib/node/relay-store.d.ts +72 -0
  104. package/lib/node/relay-store.js +121 -0
  105. package/lib/node/relay-viewer.d.ts +8 -0
  106. package/lib/node/relay-viewer.js +32 -0
  107. package/lib/viewer/viewer-i18n.d.ts +57 -0
  108. package/lib/viewer/viewer-i18n.js +120 -0
  109. package/lib/viewer/viewer-markdown.d.ts +8 -0
  110. package/lib/viewer/viewer-markdown.js +201 -0
  111. package/lib/viewer/viewer-style.d.ts +2 -0
  112. package/lib/viewer/viewer-style.js +75 -0
  113. package/lib/viewer/viewer.d.ts +9 -0
  114. package/lib/viewer/viewer.js +381 -0
  115. package/package.json +5 -4
  116. package/viewer/frame.html +1 -0
  117. package/viewer/page.html +80 -0
  118. package/viewer/room.html +94 -0
@@ -6,6 +6,34 @@ export declare const RELAY_CONNECT_PATH = "/v1/connect/";
6
6
  export declare const RELAY_PAIR_PATH = "/pair";
7
7
  /** A link of an extension handed on: its package comes from the computer of the one who shared it, through the relay. */
8
8
  export declare const RELAY_SHARE_PATH = "/share";
9
+ /**
10
+ * Links of content shared (RelayContentUtil): a conversation, a page, a file, which the page of this path
11
+ * shows in a browser (the viewer of the relay, `viewer/`), and Ta opens itself.
12
+ */
13
+ export declare const RELAY_CONTENT_PATH = "/s";
14
+ /** The frame the viewer runs a shared HTML page in: a page of its own, so that it has its own CSP. */
15
+ export declare const RELAY_FRAME_PATH = "/s/frame";
16
+ /**
17
+ * The policy of the frame a shared page runs in (sandboxed, of an opaque origin): its own scripts, styles and
18
+ * pictures, from itself or the web, but no request of its own (`connect-src`), no form sent.
19
+ */
20
+ export declare const RELAY_FRAME_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'";
21
+ /**
22
+ * The policy the relay answers the frame with: that of its document, framed by the viewer alone and sandboxed by
23
+ * the relay itself. The `sandbox` of the viewer's iframe is not enough: a page of another site framing it without
24
+ * one would run what it writes in under the relay's origin, where guests of rooms keep their keys. A meta element
25
+ * cannot say `sandbox` or `frame-ancestors`, so they are in the header only.
26
+ */
27
+ export declare const RELAY_FRAME_HEADER_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox";
28
+ /** Headers of the viewer and its frame: never framed elsewhere, never a referrer (the key is in the address). */
29
+ export declare const RELAY_VIEWER_HEADERS: {
30
+ readonly 'content-type': 'text/html; charset=utf-8';
31
+ readonly 'cache-control': 'no-cache';
32
+ readonly 'referrer-policy': 'no-referrer';
33
+ readonly 'x-content-type-options': 'nosniff';
34
+ };
35
+ /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
36
+ export declare const RELAY_BLOB_PATH = "/v1/blob/";
9
37
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
10
38
  export declare const RELAY_PAIR_PAGE = "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>Ta</title>\n<body style=\"font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em\">\n<h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>\n<p>\u6253\u5F00 Ta \u5E94\u7528\uFF0C\u7C98\u8D34\u8FD9\u4E2A\u94FE\u63A5\uFF0C\u5C31\u80FD\u4ECE\u8FD9\u53F0\u8BBE\u5907\u8FDE\u5230\u4F60\u7684 Ta\u3002</p></body>";
11
39
  /** The page a link of an extension opens in a browser. */
@@ -34,6 +62,35 @@ export declare const RELAY_SHARE_MAX_BYTES: number;
34
62
  export declare const RELAY_SHARE_CHUNK_BYTES: number;
35
63
  /** How long a link of a package works. */
36
64
  export declare const RELAY_SHARE_DAYS = 7;
65
+ /**
66
+ * Content handed on through the relay (RelayContentUtil): the viewer proves first that it has the key (a sealed
67
+ * RELAY_PROOF, the host's `ready` having none), then the host sends what it is, its bytes in chunks and the end.
68
+ */
69
+ export type RelayContentMessage = {
70
+ t: 'content';
71
+ kind: string;
72
+ title: string;
73
+ size: number;
74
+ createdAt: number;
75
+ type?: string;
76
+ } | {
77
+ t: 'chunk';
78
+ d: string;
79
+ } | {
80
+ t: 'end';
81
+ } | {
82
+ t: 'error';
83
+ message: string;
84
+ };
85
+ /** The largest content shared: a conversation without its images, a page, a file. */
86
+ export declare const RELAY_CONTENT_MAX_BYTES: number;
87
+ /** How long a copy stays on the relay at most. */
88
+ export declare const RELAY_BLOB_MAX_DAYS = 30;
89
+ /**
90
+ * Why a host refused a viewer, in the clear `refused` it sends before it closes: `key` the key (a password)
91
+ * is wrong, `busy` too many wrong tries of late, `gone` no such share (stopped, or past its time).
92
+ */
93
+ export type RelayRefusal = 'key' | 'busy' | 'gone';
37
94
  /** Hosts and devices: what the relay knows of them, and nothing it could read from. */
38
95
  export declare const RELAY_ID: RegExp;
39
96
  /** Close codes of the relay. */
@@ -89,15 +146,167 @@ export interface RelayHello {
89
146
  }
90
147
  /**
91
148
  * The host's answer: its nonce, and a first sealed message that shows it has the device's key; `z` when
92
- * both compress what they seal after the proof (the device offered it, and the host can).
149
+ * both compress what they seal after the proof (the device offered it, and the host can). Without `proof`
150
+ * (content shared, RelayContentUtil), the device proves first, with a sealed RELAY_PROOF.
93
151
  */
94
152
  export type RelayWelcome = {
95
153
  t: 'ready';
96
154
  nonce: string;
97
- proof: string;
155
+ proof?: string;
98
156
  z?: 1;
99
157
  } | {
100
158
  t: 'refused';
101
159
  reason: string;
102
160
  };
103
161
  export declare const RELAY_PROOF = "ta-relay ready";
162
+ /**
163
+ * Rooms of Ta (docs/design/ta-link.md 3): a hub at the relay for each, through which its members send to each
164
+ * other and keep a shared state, all sealed with the key of the room, which the relay never has (RelayHub).
165
+ */
166
+ export declare const RELAY_HUB_PATH = "/v1/hub/";
167
+ /** Links that invite into a room (RelayHubLink): Ta opens them; a browser gets the page of guests (src/guest). */
168
+ export declare const RELAY_ROOM_PATH = "/r";
169
+ /** Members of a room, its owner among them. */
170
+ export declare const RELAY_HUB_MAX_MEMBERS = 16;
171
+ /** Characters of a sealed message or value of the state. */
172
+ export declare const RELAY_HUB_MAX_VALUE: number;
173
+ export declare const RELAY_HUB_MAX_KEYS = 512;
174
+ /** Characters of all of the state, sealed. */
175
+ export declare const RELAY_HUB_MAX_STATE: number;
176
+ /** Asks to join waiting for the owner, and for how long. */
177
+ export declare const RELAY_HUB_MAX_ASKS = 20;
178
+ export declare const RELAY_HUB_ASK_DAYS = 7;
179
+ /** A room nobody used for so long is deleted. */
180
+ export declare const RELAY_HUB_IDLE_DAYS = 90;
181
+ /** Messages of a connection in a second. */
182
+ export declare const RELAY_HUB_RATE = 40;
183
+ /** The sealed list of the members of a room, signed by its owner; `v` grows with each change. */
184
+ export interface RelayHubRoster {
185
+ v: number;
186
+ d: string;
187
+ }
188
+ /** A key of the shared state: `k` the HMAC of its name, `v` its version (from 1), `d` sealed, `by` the member who wrote it last. */
189
+ export interface RelayHubEntry {
190
+ k: string;
191
+ v: number;
192
+ d: string;
193
+ by: string;
194
+ }
195
+ /** An ask to join, for the owner: `d` sealed with the key of the invitation. */
196
+ export interface RelayHubAsk {
197
+ ask: string;
198
+ d: string;
199
+ at: number;
200
+ }
201
+ /**
202
+ * What a connection to a hub sends. The first message says who it is: the owner making the room (`create`), a
203
+ * member (`auth`), one asking to join (`ask`) or come back for the answer (`wait`); `member` and `ask` are ids
204
+ * of the room alone, `token` what the relay knows them by (it keeps its SHA-256).
205
+ */
206
+ export type RelayHubRequest = {
207
+ t: 'create';
208
+ member: string;
209
+ token: string;
210
+ } | {
211
+ t: 'auth';
212
+ member: string;
213
+ token: string;
214
+ } | {
215
+ t: 'ask';
216
+ ask: string;
217
+ token: string;
218
+ proof: string;
219
+ d: string;
220
+ } | {
221
+ t: 'wait';
222
+ ask: string;
223
+ token: string;
224
+ } | {
225
+ t: 'send';
226
+ d: string;
227
+ to?: string;
228
+ } | {
229
+ t: 'set';
230
+ id: number;
231
+ k: string;
232
+ d: string;
233
+ v?: number;
234
+ } | {
235
+ t: 'invite';
236
+ hash: string;
237
+ expiresAt: number;
238
+ } | {
239
+ t: 'admit';
240
+ ask: string;
241
+ answer: string;
242
+ roster: RelayHubRoster;
243
+ } | {
244
+ t: 'refuse';
245
+ ask: string;
246
+ } | {
247
+ t: 'roster';
248
+ roster: RelayHubRoster;
249
+ } | {
250
+ t: 'remove';
251
+ member: string;
252
+ roster: RelayHubRoster;
253
+ } | {
254
+ t: 'leave';
255
+ } | {
256
+ t: 'close';
257
+ };
258
+ /** Why the hub refused: `auth` not a member, `gone` no such room, `invite` the invitation is wrong or ran out. */
259
+ export type RelayHubFailure = 'auth' | 'gone' | 'invite' | 'full' | 'large' | 'limit' | 'rate' | 'owner' | 'exists' | 'invalid';
260
+ /**
261
+ * What a hub sends. A member is first `welcome`d (who is online, the roster, asks for the owner), then given the
262
+ * state in `entries` and `synced`.
263
+ */
264
+ export type RelayHubEvent = {
265
+ t: 'welcome';
266
+ owner: string;
267
+ online: string[];
268
+ roster?: RelayHubRoster;
269
+ asks?: RelayHubAsk[];
270
+ } | {
271
+ t: 'entries';
272
+ entries: RelayHubEntry[];
273
+ } | {
274
+ t: 'synced';
275
+ } | {
276
+ t: 'msg';
277
+ from: string;
278
+ d: string;
279
+ } | {
280
+ t: 'state';
281
+ entry: RelayHubEntry;
282
+ } | {
283
+ t: 'done';
284
+ id: number;
285
+ ok: boolean;
286
+ v: number;
287
+ error?: RelayHubFailure;
288
+ } | {
289
+ t: 'presence';
290
+ member: string;
291
+ online: boolean;
292
+ } | {
293
+ t: 'roster';
294
+ roster: RelayHubRoster;
295
+ } | {
296
+ t: 'asked';
297
+ ask: RelayHubAsk;
298
+ } | {
299
+ t: 'waiting';
300
+ } | {
301
+ t: 'admitted';
302
+ answer: string;
303
+ } | {
304
+ t: 'refused';
305
+ } | {
306
+ t: 'left';
307
+ member: string;
308
+ } | {
309
+ t: 'error';
310
+ code: RelayHubFailure;
311
+ message: string;
312
+ };
@@ -6,6 +6,34 @@ export const RELAY_CONNECT_PATH = '/v1/connect/';
6
6
  export const RELAY_PAIR_PATH = '/pair';
7
7
  /** A link of an extension handed on: its package comes from the computer of the one who shared it, through the relay. */
8
8
  export const RELAY_SHARE_PATH = '/share';
9
+ /**
10
+ * Links of content shared (RelayContentUtil): a conversation, a page, a file, which the page of this path
11
+ * shows in a browser (the viewer of the relay, `viewer/`), and Ta opens itself.
12
+ */
13
+ export const RELAY_CONTENT_PATH = '/s';
14
+ /** The frame the viewer runs a shared HTML page in: a page of its own, so that it has its own CSP. */
15
+ export const RELAY_FRAME_PATH = '/s/frame';
16
+ /**
17
+ * The policy of the frame a shared page runs in (sandboxed, of an opaque origin): its own scripts, styles and
18
+ * pictures, from itself or the web, but no request of its own (`connect-src`), no form sent.
19
+ */
20
+ export const RELAY_FRAME_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'";
21
+ /**
22
+ * The policy the relay answers the frame with: that of its document, framed by the viewer alone and sandboxed by
23
+ * the relay itself. The `sandbox` of the viewer's iframe is not enough: a page of another site framing it without
24
+ * one would run what it writes in under the relay's origin, where guests of rooms keep their keys. A meta element
25
+ * cannot say `sandbox` or `frame-ancestors`, so they are in the header only.
26
+ */
27
+ export const RELAY_FRAME_HEADER_CSP = `${RELAY_FRAME_CSP}; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox`;
28
+ /** Headers of the viewer and its frame: never framed elsewhere, never a referrer (the key is in the address). */
29
+ export const RELAY_VIEWER_HEADERS = {
30
+ 'content-type': 'text/html; charset=utf-8',
31
+ 'cache-control': 'no-cache',
32
+ 'referrer-policy': 'no-referrer',
33
+ 'x-content-type-options': 'nosniff',
34
+ };
35
+ /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
36
+ export const RELAY_BLOB_PATH = '/v1/blob/';
9
37
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
10
38
  export const RELAY_PAIR_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
11
39
  <body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
@@ -22,6 +50,10 @@ export const RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
22
50
  export const RELAY_SHARE_CHUNK_BYTES = 192 * 1024;
23
51
  /** How long a link of a package works. */
24
52
  export const RELAY_SHARE_DAYS = 7;
53
+ /** The largest content shared: a conversation without its images, a page, a file. */
54
+ export const RELAY_CONTENT_MAX_BYTES = 8 * 1024 * 1024;
55
+ /** How long a copy stays on the relay at most. */
56
+ export const RELAY_BLOB_MAX_DAYS = 30;
25
57
  /** Hosts and devices: what the relay knows of them, and nothing it could read from. */
26
58
  export const RELAY_ID = /^[A-Za-z0-9_-]{16,64}$/;
27
59
  /** Close codes of the relay. */
@@ -52,4 +84,25 @@ export const RELAY_PONG = 'pong';
52
84
  export const RELAY_PING_MS = 25000;
53
85
  export const RELAY_STALE_MS = 80000;
54
86
  export const RELAY_PROOF = 'ta-relay ready';
87
+ /**
88
+ * Rooms of Ta (docs/design/ta-link.md 3): a hub at the relay for each, through which its members send to each
89
+ * other and keep a shared state, all sealed with the key of the room, which the relay never has (RelayHub).
90
+ */
91
+ export const RELAY_HUB_PATH = '/v1/hub/';
92
+ /** Links that invite into a room (RelayHubLink): Ta opens them; a browser gets the page of guests (src/guest). */
93
+ export const RELAY_ROOM_PATH = '/r';
94
+ /** Members of a room, its owner among them. */
95
+ export const RELAY_HUB_MAX_MEMBERS = 16;
96
+ /** Characters of a sealed message or value of the state. */
97
+ export const RELAY_HUB_MAX_VALUE = 96 * 1024;
98
+ export const RELAY_HUB_MAX_KEYS = 512;
99
+ /** Characters of all of the state, sealed. */
100
+ export const RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
101
+ /** Asks to join waiting for the owner, and for how long. */
102
+ export const RELAY_HUB_MAX_ASKS = 20;
103
+ export const RELAY_HUB_ASK_DAYS = 7;
104
+ /** A room nobody used for so long is deleted. */
105
+ export const RELAY_HUB_IDLE_DAYS = 90;
106
+ /** Messages of a connection in a second. */
107
+ export const RELAY_HUB_RATE = 40;
55
108
  //# sourceMappingURL=relay-protocol.js.map
@@ -0,0 +1,109 @@
1
+ /** A value of the shared state of a room, as RelayHubs and TaRoomServer read it. */
2
+ export interface RelayRoomChatValue {
3
+ value: unknown;
4
+ version: number;
5
+ by: string;
6
+ }
7
+ /** What a chat reads and sets: RelayHubs, or TaRoomServer of Ta. */
8
+ export interface RelayRoomChatRooms {
9
+ getState(room: string, prefix?: string): Promise<Record<string, RelayRoomChatValue>>;
10
+ setState(room: string, key: string, value: unknown, version?: number): Promise<{
11
+ ok: boolean;
12
+ version: number;
13
+ }>;
14
+ }
15
+ /** One thing said in a room. */
16
+ export interface RelayChatLine {
17
+ id: string;
18
+ /** The member who said it: that of the key it is under, never what the value says. */
19
+ from: string;
20
+ /** Its name as it said it, for one who left since. */
21
+ name: string;
22
+ text: string;
23
+ at: number;
24
+ /**
25
+ * Said by the double of the member, its Ta's agent, not by the member (docs/design/ta-link.md 4): as an answer to
26
+ * one who asked it, or for the member who sent it to say something.
27
+ */
28
+ double?: boolean;
29
+ /**
30
+ * Said by an agent of the member's Ta (one of ta-channels) that the member invited to the room: its id there and its
31
+ * name. Not with `double`.
32
+ */
33
+ agent?: RelayChatAgent;
34
+ /**
35
+ * Whom it asks: members' doubles by their member ids, each one's Ta answering if the member lets it; agents in the room
36
+ * by `<member>/<agent id>`, their member's Ta answering.
37
+ */
38
+ to?: string[];
39
+ /** The id of the line it answers. */
40
+ re?: string;
41
+ }
42
+ /** An agent of a member's Ta, in a room (RelayChatLine.agent). */
43
+ export interface RelayChatAgent {
44
+ id: string;
45
+ name: string;
46
+ }
47
+ /** What a line adds to its text: who said it and to whom (RelayRoomChat.say). */
48
+ export type RelayChatLineExtra = Pick<RelayChatLine, 'double' | 'agent' | 'to' | 're'>;
49
+ /** What is kept of a line: `from` is the key's. */
50
+ type StoredLine = Omit<RelayChatLine, 'from'>;
51
+ /** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
52
+ export declare const RELAY_CHAT_KEY = "chat:";
53
+ /** The bytes of JSON of one part at most, well under what the relay takes of a value once sealed. */
54
+ export declare const RELAY_CHAT_PART_BYTES: number;
55
+ /** The parts of each member kept: the oldest is cleared as a new one starts. */
56
+ export declare const RELAY_CHAT_PARTS = 6;
57
+ export declare const RELAY_CHAT_TEXT_MAX = 2000;
58
+ export declare namespace RelayRoomChatUtil {
59
+ function key(member: string, part: number): string;
60
+ function parse(key: string): {
61
+ member: string;
62
+ part: number;
63
+ } | undefined;
64
+ /** The lines of a part, if its value is one and was written by the member of its key. */
65
+ function lines(member: string, value: unknown, by: string): RelayChatLine[] | undefined;
66
+ /** What a stored line adds, as far as it is well formed. */
67
+ function extra(line: Record<string, unknown>): RelayChatLineExtra;
68
+ function stored(lines: RelayChatLine[]): StoredLine[];
69
+ function bytes(lines: StoredLine[]): number;
70
+ function id(): string;
71
+ }
72
+ interface Part {
73
+ member: string;
74
+ part: number;
75
+ lines: RelayChatLine[];
76
+ version: number;
77
+ }
78
+ /**
79
+ * The group chat of a room (docs/design/ta-link.md 3.7; the Rooms page of Ta and the page of guests), in its shared state: what each member said is in parts of its
80
+ * own (`chat:<member>:<n>`), the latest added to on its version, a new one once it is full, the oldest cleared beyond
81
+ * {@link RELAY_CHAT_PARTS}. So it is there for whoever comes later, sealed as all of the state, and nobody writes as another.
82
+ */
83
+ export declare class RelayRoomChat {
84
+ protected readonly rooms: RelayRoomChatRooms;
85
+ readonly room: string;
86
+ protected readonly parts: Map<string, Part>;
87
+ protected readonly listeners: Set<() => void>;
88
+ protected all: readonly RelayChatLine[];
89
+ constructor(rooms: RelayRoomChatRooms, room: string);
90
+ /** All that is said, the earliest first. */
91
+ get lines(): readonly RelayChatLine[];
92
+ subscribe(listener: () => void): () => void;
93
+ load(): Promise<void>;
94
+ /** A key of the room set, as its event tells: whether it was of the chat. */
95
+ heard(key: string, value: RelayRoomChatValue): boolean;
96
+ /**
97
+ * Says `text` as `self` (its member id and name), with what `extra` adds (its double says it, to whose doubles); set
98
+ * again on what another client of the same Ta wrote first. The line said, or undefined when there was nothing to say.
99
+ */
100
+ say(text: string, self: {
101
+ member: string;
102
+ name: string;
103
+ }, extra?: RelayChatLineExtra): Promise<RelayChatLine | undefined>;
104
+ protected clear(key: string): Promise<void>;
105
+ protected latest(member: string): Part | undefined;
106
+ protected put(key: string, value: RelayRoomChatValue): void;
107
+ protected changed(): void;
108
+ }
109
+ export {};
@@ -0,0 +1,176 @@
1
+ /** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
2
+ export const RELAY_CHAT_KEY = 'chat:';
3
+ /** The bytes of JSON of one part at most, well under what the relay takes of a value once sealed. */
4
+ export const RELAY_CHAT_PART_BYTES = 8 * 1024;
5
+ /** The parts of each member kept: the oldest is cleared as a new one starts. */
6
+ export const RELAY_CHAT_PARTS = 6;
7
+ export const RELAY_CHAT_TEXT_MAX = 2000;
8
+ const TRIES = 6;
9
+ export var RelayRoomChatUtil;
10
+ (function (RelayRoomChatUtil) {
11
+ function key(member, part) {
12
+ return `${RELAY_CHAT_KEY}${member}:${part}`;
13
+ }
14
+ RelayRoomChatUtil.key = key;
15
+ function parse(key) {
16
+ const match = /^chat:([^:]+):(\d+)$/.exec(key);
17
+ return match ? { member: match[1], part: Number(match[2]) } : undefined;
18
+ }
19
+ RelayRoomChatUtil.parse = parse;
20
+ /** The lines of a part, if its value is one and was written by the member of its key. */
21
+ function lines(member, value, by) {
22
+ if (by !== member || !Array.isArray(value)) {
23
+ return undefined;
24
+ }
25
+ const lines = [];
26
+ for (const line of value) {
27
+ if (line && typeof line.id === 'string' && typeof line.text === 'string' && typeof line.at === 'number') {
28
+ lines.push({ id: line.id, from: member, name: typeof line.name === 'string' ? line.name : '', text: line.text, at: line.at, ...extra(line) });
29
+ }
30
+ }
31
+ return lines;
32
+ }
33
+ RelayRoomChatUtil.lines = lines;
34
+ /** What a stored line adds, as far as it is well formed. */
35
+ function extra(line) {
36
+ const to = Array.isArray(line.to) ? line.to.filter((each) => typeof each === 'string').slice(0, 16) : [];
37
+ const agent = line.agent;
38
+ return {
39
+ ...(line.double === true ? { double: true } : {}),
40
+ ...(agent && typeof agent.id === 'string' && agent.id && typeof agent.name === 'string' && line.double !== true
41
+ ? { agent: { id: agent.id.slice(0, 64), name: agent.name.slice(0, 64) } }
42
+ : {}),
43
+ ...(to.length ? { to } : {}),
44
+ ...(typeof line.re === 'string' ? { re: line.re } : {}),
45
+ };
46
+ }
47
+ RelayRoomChatUtil.extra = extra;
48
+ function stored(lines) {
49
+ return lines.map(({ from: _from, ...line }) => line);
50
+ }
51
+ RelayRoomChatUtil.stored = stored;
52
+ function bytes(lines) {
53
+ return new TextEncoder().encode(JSON.stringify(lines)).length;
54
+ }
55
+ RelayRoomChatUtil.bytes = bytes;
56
+ function id() {
57
+ return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
58
+ }
59
+ RelayRoomChatUtil.id = id;
60
+ })(RelayRoomChatUtil || (RelayRoomChatUtil = {}));
61
+ /**
62
+ * The group chat of a room (docs/design/ta-link.md 3.7; the Rooms page of Ta and the page of guests), in its shared state: what each member said is in parts of its
63
+ * own (`chat:<member>:<n>`), the latest added to on its version, a new one once it is full, the oldest cleared beyond
64
+ * {@link RELAY_CHAT_PARTS}. So it is there for whoever comes later, sealed as all of the state, and nobody writes as another.
65
+ */
66
+ export class RelayRoomChat {
67
+ constructor(rooms, room) {
68
+ this.rooms = rooms;
69
+ this.room = room;
70
+ this.parts = new Map();
71
+ this.listeners = new Set();
72
+ this.all = [];
73
+ }
74
+ /** All that is said, the earliest first. */
75
+ get lines() {
76
+ return this.all;
77
+ }
78
+ subscribe(listener) {
79
+ this.listeners.add(listener);
80
+ return () => this.listeners.delete(listener);
81
+ }
82
+ async load() {
83
+ const state = await this.rooms.getState(this.room, RELAY_CHAT_KEY);
84
+ this.parts.clear();
85
+ for (const [key, value] of Object.entries(state)) {
86
+ this.put(key, value);
87
+ }
88
+ this.changed();
89
+ }
90
+ /** A key of the room set, as its event tells: whether it was of the chat. */
91
+ heard(key, value) {
92
+ if (!RelayRoomChatUtil.parse(key)) {
93
+ return false;
94
+ }
95
+ const known = this.parts.get(key);
96
+ if (known && known.version >= value.version) {
97
+ return true;
98
+ }
99
+ this.put(key, value);
100
+ this.changed();
101
+ return true;
102
+ }
103
+ /**
104
+ * Says `text` as `self` (its member id and name), with what `extra` adds (its double says it, to whose doubles); set
105
+ * again on what another client of the same Ta wrote first. The line said, or undefined when there was nothing to say.
106
+ */
107
+ async say(text, self, extra = {}) {
108
+ var _a;
109
+ var _b, _c;
110
+ const said = text.trim().slice(0, RELAY_CHAT_TEXT_MAX);
111
+ if (!said) {
112
+ return undefined;
113
+ }
114
+ for (let tries = 0; tries < TRIES; tries++) {
115
+ const latest = this.latest(self.member);
116
+ // After what it said last, even within the same millisecond: the lines of a member keep their order.
117
+ const last = latest === null || latest === void 0 ? void 0 : latest.lines[latest.lines.length - 1];
118
+ const at = Math.max(Date.now(), ((_b = last === null || last === void 0 ? void 0 : last.at) !== null && _b !== void 0 ? _b : 0) + 1);
119
+ const line = {
120
+ id: RelayRoomChatUtil.id(),
121
+ from: self.member,
122
+ name: self.name,
123
+ text: said,
124
+ at,
125
+ ...RelayRoomChatUtil.extra(extra),
126
+ };
127
+ const fits = latest && RelayRoomChatUtil.bytes(RelayRoomChatUtil.stored([...latest.lines, line])) <= RELAY_CHAT_PART_BYTES;
128
+ const part = !latest ? 0 : fits ? latest.part : latest.part + 1;
129
+ const lines = fits && latest ? [...latest.lines, line] : [line];
130
+ const key = RelayRoomChatUtil.key(self.member, part);
131
+ const value = RelayRoomChatUtil.stored(lines);
132
+ const result = await this.rooms.setState(this.room, key, value, (_c = (_a = this.parts.get(key)) === null || _a === void 0 ? void 0 : _a.version) !== null && _c !== void 0 ? _c : 0);
133
+ if (result.ok) {
134
+ this.put(key, { value, version: result.version, by: self.member });
135
+ this.changed();
136
+ if (part >= RELAY_CHAT_PARTS) {
137
+ void this.clear(RelayRoomChatUtil.key(self.member, part - RELAY_CHAT_PARTS));
138
+ }
139
+ return line;
140
+ }
141
+ await this.load();
142
+ }
143
+ throw new Error('The room is busy: say it again.');
144
+ }
145
+ async clear(key) {
146
+ await this.rooms.setState(this.room, key, null).catch(() => undefined);
147
+ if (this.parts.delete(key)) {
148
+ this.changed();
149
+ }
150
+ }
151
+ latest(member) {
152
+ let latest;
153
+ for (const part of this.parts.values()) {
154
+ if (part.member === member && (!latest || part.part > latest.part)) {
155
+ latest = part;
156
+ }
157
+ }
158
+ return latest;
159
+ }
160
+ put(key, value) {
161
+ const parsed = RelayRoomChatUtil.parse(key);
162
+ const lines = parsed && value.value !== null ? RelayRoomChatUtil.lines(parsed.member, value.value, value.by) : undefined;
163
+ if (!parsed || !lines) {
164
+ this.parts.delete(key);
165
+ return;
166
+ }
167
+ this.parts.set(key, { ...parsed, lines, version: value.version });
168
+ }
169
+ changed() {
170
+ this.all = [...this.parts.values()].flatMap(part => part.lines).sort((a, b) => a.at - b.at || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
171
+ for (const listener of this.listeners) {
172
+ listener();
173
+ }
174
+ }
175
+ }
176
+ //# sourceMappingURL=relay-room-chat.js.map
@@ -5,6 +5,11 @@ export interface RelaySocketOptions {
5
5
  pingMs?: number;
6
6
  /** Default 15 seconds. */
7
7
  helloMs?: number;
8
+ /**
9
+ * For content shared (RelayContentUtil): this side proves first that it has the key, and the host sends
10
+ * nothing sealed before; a host that refuses says why in the clear (RelayRefusal), as the close's reason.
11
+ */
12
+ proveFirst?: boolean;
8
13
  }
9
14
  /**
10
15
  * A WebSocket to ta serve through a relay, for a paired device: it says hello, checks that the host has
@@ -13,6 +18,7 @@ export interface RelaySocketOptions {
13
18
  */
14
19
  export declare class RelaySocket {
15
20
  protected readonly pairing: RelayPairing;
21
+ protected readonly options: RelaySocketOptions;
16
22
  static readonly CONNECTING = 0;
17
23
  static readonly OPEN = 1;
18
24
  static readonly CLOSING = 2;
@@ -47,6 +53,8 @@ export declare class RelaySocket {
47
53
  send(text: string): void;
48
54
  close(code?: number, reason?: string): void;
49
55
  protected receive(text: string): Promise<void>;
56
+ /** The reason of a clear `refused` of the host, as RelayRefusal says it; `gone` when it says none of them. */
57
+ protected static refusal(text: string): string;
50
58
  /** The relay stopped answering: the connection is gone without a word, and closing it would wait for one. */
51
59
  protected ping(): void;
52
60
  /** Closed at once, rather than once the close is answered. */
@@ -17,6 +17,7 @@ export class RelaySocket {
17
17
  constructor(pairing, options = {}) {
18
18
  var _a;
19
19
  this.pairing = pairing;
20
+ this.options = options;
20
21
  this.onopen = null;
21
22
  this.onmessage = null;
22
23
  this.onclose = null;
@@ -77,6 +78,11 @@ export class RelaySocket {
77
78
  async receive(text) {
78
79
  var _a, _b;
79
80
  if (this.cipher) {
81
+ if (this.options.proveFirst && text.startsWith('{')) {
82
+ // Sealed frames are base64url: this is the host's refusal, in the clear, after the proof did not open.
83
+ this.drop(RelaySocket.REFUSED, RelaySocket.refusal(text));
84
+ return;
85
+ }
80
86
  try {
81
87
  const data = await this.cipher.open(text);
82
88
  (_a = this.onmessage) === null || _a === void 0 ? void 0 : _a.call(this, { data });
@@ -89,10 +95,15 @@ export class RelaySocket {
89
95
  try {
90
96
  const welcome = JSON.parse(text);
91
97
  if (welcome.t !== 'ready') {
92
- throw new Error(welcome.reason);
98
+ this.drop(RelaySocket.REFUSED, this.options.proveFirst ? RelaySocket.refusal(text) : 'The host refused this device.');
99
+ return;
93
100
  }
94
101
  const cipher = await RelayCipher.derive(RelayBytes.fromBase64(this.pairing.key), this.nonce, RelayBytes.fromBase64(welcome.nonce), 'client');
95
- if ((await cipher.open(welcome.proof)) !== RELAY_PROOF) {
102
+ if (welcome.proof === undefined && this.options.proveFirst) {
103
+ // What the host sends next opens only if it has the key too.
104
+ this.socket.send(await cipher.seal(RELAY_PROOF));
105
+ }
106
+ else if (welcome.proof === undefined || (await cipher.open(welcome.proof)) !== RELAY_PROOF) {
96
107
  throw new Error('The host did not show the key.');
97
108
  }
98
109
  // The messages after the proof, both ways.
@@ -107,6 +118,16 @@ export class RelaySocket {
107
118
  this.drop(RelaySocket.REFUSED, 'The host refused this device.');
108
119
  }
109
120
  }
121
+ /** The reason of a clear `refused` of the host, as RelayRefusal says it; `gone` when it says none of them. */
122
+ static refusal(text) {
123
+ try {
124
+ const reason = JSON.parse(text).reason;
125
+ return reason === 'key' || reason === 'busy' ? reason : 'gone';
126
+ }
127
+ catch (_a) {
128
+ return 'gone';
129
+ }
130
+ }
110
131
  /** The relay stopped answering: the connection is gone without a word, and closing it would wait for one. */
111
132
  ping() {
112
133
  if (this.awaitingPong && this.ponged) {