@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,16 +146,168 @@ 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
+ };
104
313
  //# sourceMappingURL=relay-protocol.d.ts.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RELAY_PROOF = exports.RELAY_STALE_MS = exports.RELAY_PING_MS = exports.RELAY_PONG = exports.RELAY_PING = exports.RelayCloseCode = exports.RELAY_ID = exports.RELAY_SHARE_DAYS = exports.RELAY_SHARE_CHUNK_BYTES = exports.RELAY_SHARE_MAX_BYTES = exports.RELAY_SHARE_PAGE = exports.RELAY_PAIR_PAGE = exports.RELAY_SHARE_PATH = exports.RELAY_PAIR_PATH = exports.RELAY_CONNECT_PATH = exports.RELAY_HOST_PATH = exports.RELAY_DEFAULT_URL = void 0;
3
+ exports.RELAY_HUB_RATE = exports.RELAY_HUB_IDLE_DAYS = exports.RELAY_HUB_ASK_DAYS = exports.RELAY_HUB_MAX_ASKS = exports.RELAY_HUB_MAX_STATE = exports.RELAY_HUB_MAX_KEYS = exports.RELAY_HUB_MAX_VALUE = exports.RELAY_HUB_MAX_MEMBERS = exports.RELAY_ROOM_PATH = exports.RELAY_HUB_PATH = exports.RELAY_PROOF = exports.RELAY_STALE_MS = exports.RELAY_PING_MS = exports.RELAY_PONG = exports.RELAY_PING = exports.RelayCloseCode = exports.RELAY_ID = exports.RELAY_BLOB_MAX_DAYS = exports.RELAY_CONTENT_MAX_BYTES = exports.RELAY_SHARE_DAYS = exports.RELAY_SHARE_CHUNK_BYTES = exports.RELAY_SHARE_MAX_BYTES = exports.RELAY_SHARE_PAGE = exports.RELAY_PAIR_PAGE = exports.RELAY_BLOB_PATH = exports.RELAY_VIEWER_HEADERS = exports.RELAY_FRAME_HEADER_CSP = exports.RELAY_FRAME_CSP = exports.RELAY_FRAME_PATH = exports.RELAY_CONTENT_PATH = exports.RELAY_SHARE_PATH = exports.RELAY_PAIR_PATH = exports.RELAY_CONNECT_PATH = exports.RELAY_HOST_PATH = exports.RELAY_DEFAULT_URL = void 0;
4
4
  /** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
5
5
  exports.RELAY_DEFAULT_URL = 'https://relay.cellbang.com';
6
6
  /** What the relay serves: hosts connect to the first, devices to the second. */
@@ -9,6 +9,34 @@ exports.RELAY_CONNECT_PATH = '/v1/connect/';
9
9
  exports.RELAY_PAIR_PATH = '/pair';
10
10
  /** A link of an extension handed on: its package comes from the computer of the one who shared it, through the relay. */
11
11
  exports.RELAY_SHARE_PATH = '/share';
12
+ /**
13
+ * Links of content shared (RelayContentUtil): a conversation, a page, a file, which the page of this path
14
+ * shows in a browser (the viewer of the relay, `viewer/`), and Ta opens itself.
15
+ */
16
+ exports.RELAY_CONTENT_PATH = '/s';
17
+ /** The frame the viewer runs a shared HTML page in: a page of its own, so that it has its own CSP. */
18
+ exports.RELAY_FRAME_PATH = '/s/frame';
19
+ /**
20
+ * The policy of the frame a shared page runs in (sandboxed, of an opaque origin): its own scripts, styles and
21
+ * pictures, from itself or the web, but no request of its own (`connect-src`), no form sent.
22
+ */
23
+ exports.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'";
24
+ /**
25
+ * The policy the relay answers the frame with: that of its document, framed by the viewer alone and sandboxed by
26
+ * the relay itself. The `sandbox` of the viewer's iframe is not enough: a page of another site framing it without
27
+ * one would run what it writes in under the relay's origin, where guests of rooms keep their keys. A meta element
28
+ * cannot say `sandbox` or `frame-ancestors`, so they are in the header only.
29
+ */
30
+ exports.RELAY_FRAME_HEADER_CSP = `${exports.RELAY_FRAME_CSP}; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox`;
31
+ /** Headers of the viewer and its frame: never framed elsewhere, never a referrer (the key is in the address). */
32
+ exports.RELAY_VIEWER_HEADERS = {
33
+ 'content-type': 'text/html; charset=utf-8',
34
+ 'cache-control': 'no-cache',
35
+ 'referrer-policy': 'no-referrer',
36
+ 'x-content-type-options': 'nosniff',
37
+ };
38
+ /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
39
+ exports.RELAY_BLOB_PATH = '/v1/blob/';
12
40
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
13
41
  exports.RELAY_PAIR_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
14
42
  <body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
@@ -25,6 +53,10 @@ exports.RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
25
53
  exports.RELAY_SHARE_CHUNK_BYTES = 192 * 1024;
26
54
  /** How long a link of a package works. */
27
55
  exports.RELAY_SHARE_DAYS = 7;
56
+ /** The largest content shared: a conversation without its images, a page, a file. */
57
+ exports.RELAY_CONTENT_MAX_BYTES = 8 * 1024 * 1024;
58
+ /** How long a copy stays on the relay at most. */
59
+ exports.RELAY_BLOB_MAX_DAYS = 30;
28
60
  /** Hosts and devices: what the relay knows of them, and nothing it could read from. */
29
61
  exports.RELAY_ID = /^[A-Za-z0-9_-]{16,64}$/;
30
62
  /** Close codes of the relay. */
@@ -55,4 +87,25 @@ exports.RELAY_PONG = 'pong';
55
87
  exports.RELAY_PING_MS = 25000;
56
88
  exports.RELAY_STALE_MS = 80000;
57
89
  exports.RELAY_PROOF = 'ta-relay ready';
90
+ /**
91
+ * Rooms of Ta (docs/design/ta-link.md 3): a hub at the relay for each, through which its members send to each
92
+ * other and keep a shared state, all sealed with the key of the room, which the relay never has (RelayHub).
93
+ */
94
+ exports.RELAY_HUB_PATH = '/v1/hub/';
95
+ /** Links that invite into a room (RelayHubLink): Ta opens them; a browser gets the page of guests (src/guest). */
96
+ exports.RELAY_ROOM_PATH = '/r';
97
+ /** Members of a room, its owner among them. */
98
+ exports.RELAY_HUB_MAX_MEMBERS = 16;
99
+ /** Characters of a sealed message or value of the state. */
100
+ exports.RELAY_HUB_MAX_VALUE = 96 * 1024;
101
+ exports.RELAY_HUB_MAX_KEYS = 512;
102
+ /** Characters of all of the state, sealed. */
103
+ exports.RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
104
+ /** Asks to join waiting for the owner, and for how long. */
105
+ exports.RELAY_HUB_MAX_ASKS = 20;
106
+ exports.RELAY_HUB_ASK_DAYS = 7;
107
+ /** A room nobody used for so long is deleted. */
108
+ exports.RELAY_HUB_IDLE_DAYS = 90;
109
+ /** Messages of a connection in a second. */
110
+ exports.RELAY_HUB_RATE = 40;
58
111
  //# sourceMappingURL=relay-protocol.js.map
@@ -0,0 +1,110 @@
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 {};
110
+ //# sourceMappingURL=relay-room-chat.d.ts.map
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayRoomChat = exports.RelayRoomChatUtil = exports.RELAY_CHAT_TEXT_MAX = exports.RELAY_CHAT_PARTS = exports.RELAY_CHAT_PART_BYTES = exports.RELAY_CHAT_KEY = void 0;
4
+ /** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
5
+ exports.RELAY_CHAT_KEY = 'chat:';
6
+ /** The bytes of JSON of one part at most, well under what the relay takes of a value once sealed. */
7
+ exports.RELAY_CHAT_PART_BYTES = 8 * 1024;
8
+ /** The parts of each member kept: the oldest is cleared as a new one starts. */
9
+ exports.RELAY_CHAT_PARTS = 6;
10
+ exports.RELAY_CHAT_TEXT_MAX = 2000;
11
+ const TRIES = 6;
12
+ var RelayRoomChatUtil;
13
+ (function (RelayRoomChatUtil) {
14
+ function key(member, part) {
15
+ return `${exports.RELAY_CHAT_KEY}${member}:${part}`;
16
+ }
17
+ RelayRoomChatUtil.key = key;
18
+ function parse(key) {
19
+ const match = /^chat:([^:]+):(\d+)$/.exec(key);
20
+ return match ? { member: match[1], part: Number(match[2]) } : undefined;
21
+ }
22
+ RelayRoomChatUtil.parse = parse;
23
+ /** The lines of a part, if its value is one and was written by the member of its key. */
24
+ function lines(member, value, by) {
25
+ if (by !== member || !Array.isArray(value)) {
26
+ return undefined;
27
+ }
28
+ const lines = [];
29
+ for (const line of value) {
30
+ if (line && typeof line.id === 'string' && typeof line.text === 'string' && typeof line.at === 'number') {
31
+ lines.push({ id: line.id, from: member, name: typeof line.name === 'string' ? line.name : '', text: line.text, at: line.at, ...extra(line) });
32
+ }
33
+ }
34
+ return lines;
35
+ }
36
+ RelayRoomChatUtil.lines = lines;
37
+ /** What a stored line adds, as far as it is well formed. */
38
+ function extra(line) {
39
+ const to = Array.isArray(line.to) ? line.to.filter((each) => typeof each === 'string').slice(0, 16) : [];
40
+ const agent = line.agent;
41
+ return {
42
+ ...(line.double === true ? { double: true } : {}),
43
+ ...(agent && typeof agent.id === 'string' && agent.id && typeof agent.name === 'string' && line.double !== true
44
+ ? { agent: { id: agent.id.slice(0, 64), name: agent.name.slice(0, 64) } }
45
+ : {}),
46
+ ...(to.length ? { to } : {}),
47
+ ...(typeof line.re === 'string' ? { re: line.re } : {}),
48
+ };
49
+ }
50
+ RelayRoomChatUtil.extra = extra;
51
+ function stored(lines) {
52
+ return lines.map(({ from: _from, ...line }) => line);
53
+ }
54
+ RelayRoomChatUtil.stored = stored;
55
+ function bytes(lines) {
56
+ return new TextEncoder().encode(JSON.stringify(lines)).length;
57
+ }
58
+ RelayRoomChatUtil.bytes = bytes;
59
+ function id() {
60
+ return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
61
+ }
62
+ RelayRoomChatUtil.id = id;
63
+ })(RelayRoomChatUtil || (exports.RelayRoomChatUtil = RelayRoomChatUtil = {}));
64
+ /**
65
+ * 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
66
+ * own (`chat:<member>:<n>`), the latest added to on its version, a new one once it is full, the oldest cleared beyond
67
+ * {@link RELAY_CHAT_PARTS}. So it is there for whoever comes later, sealed as all of the state, and nobody writes as another.
68
+ */
69
+ class RelayRoomChat {
70
+ constructor(rooms, room) {
71
+ this.rooms = rooms;
72
+ this.room = room;
73
+ this.parts = new Map();
74
+ this.listeners = new Set();
75
+ this.all = [];
76
+ }
77
+ /** All that is said, the earliest first. */
78
+ get lines() {
79
+ return this.all;
80
+ }
81
+ subscribe(listener) {
82
+ this.listeners.add(listener);
83
+ return () => this.listeners.delete(listener);
84
+ }
85
+ async load() {
86
+ const state = await this.rooms.getState(this.room, exports.RELAY_CHAT_KEY);
87
+ this.parts.clear();
88
+ for (const [key, value] of Object.entries(state)) {
89
+ this.put(key, value);
90
+ }
91
+ this.changed();
92
+ }
93
+ /** A key of the room set, as its event tells: whether it was of the chat. */
94
+ heard(key, value) {
95
+ if (!RelayRoomChatUtil.parse(key)) {
96
+ return false;
97
+ }
98
+ const known = this.parts.get(key);
99
+ if (known && known.version >= value.version) {
100
+ return true;
101
+ }
102
+ this.put(key, value);
103
+ this.changed();
104
+ return true;
105
+ }
106
+ /**
107
+ * Says `text` as `self` (its member id and name), with what `extra` adds (its double says it, to whose doubles); set
108
+ * again on what another client of the same Ta wrote first. The line said, or undefined when there was nothing to say.
109
+ */
110
+ async say(text, self, extra = {}) {
111
+ var _a;
112
+ var _b, _c;
113
+ const said = text.trim().slice(0, exports.RELAY_CHAT_TEXT_MAX);
114
+ if (!said) {
115
+ return undefined;
116
+ }
117
+ for (let tries = 0; tries < TRIES; tries++) {
118
+ const latest = this.latest(self.member);
119
+ // After what it said last, even within the same millisecond: the lines of a member keep their order.
120
+ const last = latest === null || latest === void 0 ? void 0 : latest.lines[latest.lines.length - 1];
121
+ const at = Math.max(Date.now(), ((_b = last === null || last === void 0 ? void 0 : last.at) !== null && _b !== void 0 ? _b : 0) + 1);
122
+ const line = {
123
+ id: RelayRoomChatUtil.id(),
124
+ from: self.member,
125
+ name: self.name,
126
+ text: said,
127
+ at,
128
+ ...RelayRoomChatUtil.extra(extra),
129
+ };
130
+ const fits = latest && RelayRoomChatUtil.bytes(RelayRoomChatUtil.stored([...latest.lines, line])) <= exports.RELAY_CHAT_PART_BYTES;
131
+ const part = !latest ? 0 : fits ? latest.part : latest.part + 1;
132
+ const lines = fits && latest ? [...latest.lines, line] : [line];
133
+ const key = RelayRoomChatUtil.key(self.member, part);
134
+ const value = RelayRoomChatUtil.stored(lines);
135
+ 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);
136
+ if (result.ok) {
137
+ this.put(key, { value, version: result.version, by: self.member });
138
+ this.changed();
139
+ if (part >= exports.RELAY_CHAT_PARTS) {
140
+ void this.clear(RelayRoomChatUtil.key(self.member, part - exports.RELAY_CHAT_PARTS));
141
+ }
142
+ return line;
143
+ }
144
+ await this.load();
145
+ }
146
+ throw new Error('The room is busy: say it again.');
147
+ }
148
+ async clear(key) {
149
+ await this.rooms.setState(this.room, key, null).catch(() => undefined);
150
+ if (this.parts.delete(key)) {
151
+ this.changed();
152
+ }
153
+ }
154
+ latest(member) {
155
+ let latest;
156
+ for (const part of this.parts.values()) {
157
+ if (part.member === member && (!latest || part.part > latest.part)) {
158
+ latest = part;
159
+ }
160
+ }
161
+ return latest;
162
+ }
163
+ put(key, value) {
164
+ const parsed = RelayRoomChatUtil.parse(key);
165
+ const lines = parsed && value.value !== null ? RelayRoomChatUtil.lines(parsed.member, value.value, value.by) : undefined;
166
+ if (!parsed || !lines) {
167
+ this.parts.delete(key);
168
+ return;
169
+ }
170
+ this.parts.set(key, { ...parsed, lines, version: value.version });
171
+ }
172
+ changed() {
173
+ 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));
174
+ for (const listener of this.listeners) {
175
+ listener();
176
+ }
177
+ }
178
+ }
179
+ exports.RelayRoomChat = RelayRoomChat;
180
+ //# 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. */