@celljs/ta-relay 4.22.0 → 4.23.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 (80) hide show
  1. package/esm/common/index.d.ts +2 -0
  2. package/esm/common/index.js +2 -0
  3. package/esm/common/relay-blobs.d.ts +36 -2
  4. package/esm/common/relay-blobs.js +33 -5
  5. package/esm/common/relay-chat-store.d.ts +59 -0
  6. package/esm/common/relay-chat-store.js +40 -0
  7. package/esm/common/relay-hub-charter.d.ts +2 -0
  8. package/esm/common/relay-hub-charter.js +3 -0
  9. package/esm/common/relay-hub-log.d.ts +65 -0
  10. package/esm/common/relay-hub-log.js +147 -0
  11. package/esm/common/relay-hub.d.ts +3 -0
  12. package/esm/common/relay-hub.js +18 -0
  13. package/esm/common/relay-hubs-admins.d.ts +1 -0
  14. package/esm/common/relay-hubs-admins.js +10 -6
  15. package/esm/common/relay-hubs-chats.d.ts +66 -0
  16. package/esm/common/relay-hubs-chats.js +342 -0
  17. package/esm/common/relay-hubs-connection.d.ts +3 -1
  18. package/esm/common/relay-hubs-connection.js +7 -0
  19. package/esm/common/relay-hubs-protocol.d.ts +14 -0
  20. package/esm/common/relay-hubs.d.ts +2 -2
  21. package/esm/common/relay-hubs.js +13 -11
  22. package/esm/common/relay-info.d.ts +23 -1
  23. package/esm/common/relay-info.js +25 -2
  24. package/esm/common/relay-protocol.d.ts +60 -1
  25. package/esm/common/relay-protocol.js +15 -0
  26. package/esm/common/relay-room-chat.d.ts +126 -4
  27. package/esm/common/relay-room-chat.js +230 -6
  28. package/esm/guest/guest-i18n.d.ts +2 -0
  29. package/esm/guest/guest-i18n.js +8 -0
  30. package/esm/guest/guest.js +8 -5
  31. package/esm/node/index.d.ts +1 -0
  32. package/esm/node/index.js +1 -0
  33. package/esm/node/relay-chat-files.d.ts +46 -0
  34. package/esm/node/relay-chat-files.js +199 -0
  35. package/esm/node/relay-hub-files.d.ts +11 -1
  36. package/esm/node/relay-hub-files.js +8 -1
  37. package/esm/node/relay-server.d.ts +7 -1
  38. package/esm/node/relay-server.js +23 -13
  39. package/lib/common/index.d.ts +2 -0
  40. package/lib/common/index.js +2 -0
  41. package/lib/common/relay-blobs.d.ts +36 -2
  42. package/lib/common/relay-blobs.js +34 -6
  43. package/lib/common/relay-chat-store.d.ts +60 -0
  44. package/lib/common/relay-chat-store.js +44 -0
  45. package/lib/common/relay-hub-charter.d.ts +2 -0
  46. package/lib/common/relay-hub-charter.js +3 -0
  47. package/lib/common/relay-hub-log.d.ts +66 -0
  48. package/lib/common/relay-hub-log.js +151 -0
  49. package/lib/common/relay-hub.d.ts +3 -0
  50. package/lib/common/relay-hub.js +18 -0
  51. package/lib/common/relay-hubs-admins.d.ts +1 -0
  52. package/lib/common/relay-hubs-admins.js +9 -5
  53. package/lib/common/relay-hubs-chats.d.ts +67 -0
  54. package/lib/common/relay-hubs-chats.js +346 -0
  55. package/lib/common/relay-hubs-connection.d.ts +3 -1
  56. package/lib/common/relay-hubs-connection.js +7 -0
  57. package/lib/common/relay-hubs-protocol.d.ts +14 -0
  58. package/lib/common/relay-hubs.d.ts +2 -2
  59. package/lib/common/relay-hubs.js +13 -11
  60. package/lib/common/relay-info.d.ts +23 -1
  61. package/lib/common/relay-info.js +26 -3
  62. package/lib/common/relay-protocol.d.ts +60 -1
  63. package/lib/common/relay-protocol.js +16 -1
  64. package/lib/common/relay-room-chat.d.ts +126 -4
  65. package/lib/common/relay-room-chat.js +231 -7
  66. package/lib/guest/guest-i18n.d.ts +2 -0
  67. package/lib/guest/guest-i18n.js +8 -0
  68. package/lib/guest/guest.js +7 -4
  69. package/lib/node/index.d.ts +1 -0
  70. package/lib/node/index.js +1 -0
  71. package/lib/node/relay-chat-files.d.ts +47 -0
  72. package/lib/node/relay-chat-files.js +203 -0
  73. package/lib/node/relay-hub-files.d.ts +11 -1
  74. package/lib/node/relay-hub-files.js +8 -1
  75. package/lib/node/relay-server.d.ts +7 -1
  76. package/lib/node/relay-server.js +21 -11
  77. package/package.json +3 -3
  78. package/viewer/join.html +4 -4
  79. package/viewer/page.html +10 -10
  80. package/viewer/room.html +15 -15
@@ -39,6 +39,11 @@ export declare const RELAY_VIEWER_HEADERS: {
39
39
  };
40
40
  /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
41
41
  export declare const RELAY_BLOB_PATH = "/v1/blob/";
42
+ /**
43
+ * Sealed copies of the pictures of chats (docs/design/ta-chat-history.md 7), only on a relay whose operator lets it
44
+ * (RelayInfo.media): none by default, what it keeps costing more the more people use it.
45
+ */
46
+ export declare const RELAY_MEDIA_PATH = "/v1/media/";
42
47
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
43
48
  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><p><a id=\"open\" style=\"display:inline-block;padding:10px 18px;border-radius:20px;background:#111;color:#fff;text-decoration:none;font-weight:600\">Open in Ta \u00B7 \u5728 Ta \u4E2D\u6253\u5F00</a></p>\n<script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script></body>";
44
49
  /** The page a link of an extension opens in a browser. */
@@ -200,6 +205,26 @@ export declare const RELAY_HUB_ASK_DAYS = 7;
200
205
  export declare const RELAY_HUB_IDLE_DAYS = 90;
201
206
  /** Messages of a connection in a second. */
202
207
  export declare const RELAY_HUB_RATE = 40;
208
+ /**
209
+ * One message of the log of a room (docs/design/ta-chat-history.md 5): its number in the room, the member who posted it,
210
+ * sealed and signed as a message is, and when the relay took it. What is said in a room goes here, not in the state.
211
+ */
212
+ export interface RelayHubLogItem {
213
+ s: number;
214
+ m: string;
215
+ d: string;
216
+ at: number;
217
+ }
218
+ /** The log of a room keeps so many messages, so many characters at most: past them the oldest go, read or not. */
219
+ export declare const RELAY_HUB_LOG_MAX = 5000;
220
+ export declare const RELAY_HUB_LOG_CHARS: number;
221
+ /** Days a message is kept once every member has it, and at most. */
222
+ export declare const RELAY_HUB_LOG_KEEP_DAYS = 7;
223
+ export declare const RELAY_HUB_LOG_MAX_DAYS = 30;
224
+ /** Messages of the log in one answer to `sync`. */
225
+ export declare const RELAY_HUB_SYNC_PAGE = 200;
226
+ /** Messages from before they joined a newcomer may read at most, as the charter of the room says (history, 0 by default). */
227
+ export declare const RELAY_HUB_HISTORY_MAX = 500;
203
228
  /**
204
229
  * The sealed list of the members of a room, signed by its owner (or an admin); `v` grows with each change. After a
205
230
  * member was taken out or left, `k` holds the keys of the room's epochs sealed for each member left
@@ -259,6 +284,24 @@ export type RelayHubRequest = {
259
284
  d: string;
260
285
  v?: number;
261
286
  own?: true;
287
+ }
288
+ /** Adds to the log (`mid` the id of the line: the same again is the same message), answered by `done` with its number in `v`. */
289
+ | {
290
+ t: 'post';
291
+ id: number;
292
+ mid: string;
293
+ d: string;
294
+ }
295
+ /** The log after the number `after`, a page of it (RELAY_HUB_SYNC_PAGE at most), answered by `log`. */
296
+ | {
297
+ t: 'sync';
298
+ after: number;
299
+ limit?: number;
300
+ }
301
+ /** This member has the log up to `seq`: the relay may let go of what all have, once old enough. */
302
+ | {
303
+ t: 'ack';
304
+ seq: number;
262
305
  } | {
263
306
  t: 'invite';
264
307
  hash: string;
@@ -266,11 +309,14 @@ export type RelayHubRequest = {
266
309
  } | {
267
310
  t: 'admins';
268
311
  admins: string[];
269
- } | {
312
+ }
313
+ /** `history`: messages from before they joined the newcomer may read (RELAY_HUB_HISTORY_MAX at most), 0 by default. */
314
+ | {
270
315
  t: 'admit';
271
316
  ask: string;
272
317
  answer: string;
273
318
  roster: RelayHubRoster;
319
+ history?: number;
274
320
  } | {
275
321
  t: 'refuse';
276
322
  ask: string;
@@ -307,6 +353,19 @@ export type RelayHubEvent = {
307
353
  t: 'msg';
308
354
  from: string;
309
355
  d: string;
356
+ }
357
+ /** A page of the log: `first` and `last` the numbers it holds now, `more` when there is more after the page. */
358
+ | {
359
+ t: 'log';
360
+ items: RelayHubLogItem[];
361
+ first: number;
362
+ last: number;
363
+ more: boolean;
364
+ }
365
+ /** Another member posted. */
366
+ | {
367
+ t: 'post';
368
+ item: RelayHubLogItem;
310
369
  } | {
311
370
  t: 'state';
312
371
  entry: RelayHubEntry;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RELAY_HUB_RATE = exports.RELAY_HUB_IDLE_DAYS = exports.RELAY_HUB_ASK_DAYS = exports.RELAY_PICTURE_MAX_CHARS = exports.RELAY_PICTURE_KEY = exports.RELAY_HUB_MAX_ASKS = exports.RELAY_HUB_MAX_STATE = exports.RELAY_HUB_MAX_KEYS = exports.RELAY_HUB_MAX_VALUE = exports.RELAY_FRIENDS_MAX = exports.RELAY_INVITE_MAX_DAYS = exports.RELAY_ROOM_CAPACITY_MAX = exports.RELAY_ROOM_MAX_MEMBERS = 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;
3
+ exports.RELAY_HUB_HISTORY_MAX = exports.RELAY_HUB_SYNC_PAGE = exports.RELAY_HUB_LOG_MAX_DAYS = exports.RELAY_HUB_LOG_KEEP_DAYS = exports.RELAY_HUB_LOG_CHARS = exports.RELAY_HUB_LOG_MAX = exports.RELAY_HUB_RATE = exports.RELAY_HUB_IDLE_DAYS = exports.RELAY_HUB_ASK_DAYS = exports.RELAY_PICTURE_MAX_CHARS = exports.RELAY_PICTURE_KEY = exports.RELAY_HUB_MAX_ASKS = exports.RELAY_HUB_MAX_STATE = exports.RELAY_HUB_MAX_KEYS = exports.RELAY_HUB_MAX_VALUE = exports.RELAY_FRIENDS_MAX = exports.RELAY_INVITE_MAX_DAYS = exports.RELAY_ROOM_CAPACITY_MAX = exports.RELAY_ROOM_MAX_MEMBERS = 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_MEDIA_PATH = 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
  exports.relayDefaultUrl = relayDefaultUrl;
5
5
  /** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
6
6
  exports.RELAY_DEFAULT_URL = 'https://relay.cellbang.com';
@@ -48,6 +48,11 @@ exports.RELAY_VIEWER_HEADERS = {
48
48
  };
49
49
  /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
50
50
  exports.RELAY_BLOB_PATH = '/v1/blob/';
51
+ /**
52
+ * Sealed copies of the pictures of chats (docs/design/ta-chat-history.md 7), only on a relay whose operator lets it
53
+ * (RelayInfo.media): none by default, what it keeps costing more the more people use it.
54
+ */
55
+ exports.RELAY_MEDIA_PATH = '/v1/media/';
51
56
  /** Open in Ta on the plain pages of the relay: the link of the page, its fragment too, to the Ta app (RelayOpenInTa). */
52
57
  const OPEN_IN_TA = `<p><a id="open" style="display:inline-block;padding:10px 18px;border-radius:20px;background:#111;color:#fff;text-decoration:none;font-weight:600">Open in Ta · 在 Ta 中打开</a></p>
53
58
  <script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script>`;
@@ -137,4 +142,14 @@ exports.RELAY_HUB_ASK_DAYS = 7;
137
142
  exports.RELAY_HUB_IDLE_DAYS = 90;
138
143
  /** Messages of a connection in a second. */
139
144
  exports.RELAY_HUB_RATE = 40;
145
+ /** The log of a room keeps so many messages, so many characters at most: past them the oldest go, read or not. */
146
+ exports.RELAY_HUB_LOG_MAX = 5000;
147
+ exports.RELAY_HUB_LOG_CHARS = 4 * 1024 * 1024;
148
+ /** Days a message is kept once every member has it, and at most. */
149
+ exports.RELAY_HUB_LOG_KEEP_DAYS = 7;
150
+ exports.RELAY_HUB_LOG_MAX_DAYS = 30;
151
+ /** Messages of the log in one answer to `sync`. */
152
+ exports.RELAY_HUB_SYNC_PAGE = 200;
153
+ /** Messages from before they joined a newcomer may read at most, as the charter of the room says (history, 0 by default). */
154
+ exports.RELAY_HUB_HISTORY_MAX = 500;
140
155
  //# sourceMappingURL=relay-protocol.js.map
@@ -4,13 +4,26 @@ export interface RelayRoomChatValue {
4
4
  version: number;
5
5
  by: string;
6
6
  }
7
- /** What a chat reads and sets: RelayHubs, or TaRoomServer of Ta. */
7
+ /**
8
+ * What a chat reads and sets: RelayHubs, or TaRoomServer of Ta. With `readChat` and `say`, what is said is the log of
9
+ * the room, kept on this computer (docs/design/ta-chat-history.md); without them (a Ta service of before), the state.
10
+ */
8
11
  export interface RelayRoomChatRooms {
9
12
  getState(room: string, prefix?: string): Promise<Record<string, RelayRoomChatValue>>;
10
13
  setState(room: string, key: string, value: unknown, version?: number): Promise<{
11
14
  ok: boolean;
12
15
  version: number;
13
16
  }>;
17
+ /** The latest lines kept, before the line `before` (its id) when given, the earliest first: `more` when there are older. */
18
+ readChat?(room: string, options?: {
19
+ before?: string;
20
+ limit?: number;
21
+ }): Promise<{
22
+ lines: RelayChatLine[];
23
+ more: boolean;
24
+ }>;
25
+ /** Says it as this Ta: the line, `pending` till the relay has it. */
26
+ say?(room: string, text: string, extra?: RelayChatLineExtra): Promise<RelayChatLine | undefined>;
14
27
  }
15
28
  /** One thing said in a room. */
16
29
  export interface RelayChatLine {
@@ -48,16 +61,85 @@ export interface RelayChatLine {
48
61
  * its own, says how it goes under `tasks:<member>` of the state, and answers with `re` when it ends.
49
62
  */
50
63
  task?: boolean;
64
+ /**
65
+ * Pictures sent with it (docs/design/ta-composer.md 3): what they are, not their bytes, which stay on the computers of
66
+ * the members (the sender's, and each one's that fetched them) and go from one to another through the room, sealed.
67
+ */
68
+ images?: RelayChatImage[];
69
+ /**
70
+ * Files, a voice message or a video sent with it (docs/design/ta-chat-history.md 7): what they are, not their
71
+ * bytes, which go from member to member through the room and are never kept on the relay.
72
+ */
73
+ files?: RelayChatFile[];
74
+ /** Its number in the log of the room; none for a line of before the log, or one not sent yet. */
75
+ seq?: number;
76
+ /** Said here, not yet taken by the relay: it goes once it can. */
77
+ pending?: true;
78
+ /** Said here, refused by the relay (too large): it never went. */
79
+ failed?: true;
80
+ }
81
+ /**
82
+ * A picture of a line (RelayChatLine.images): its id among the pictures of the sender, its type, size and the SHA-256 of
83
+ * its bytes, which whoever fetches it checks; a blurred thumbnail of a few hundred bytes to show until it comes.
84
+ */
85
+ export interface RelayChatImage {
86
+ id: string;
87
+ type: string;
88
+ w: number;
89
+ h: number;
90
+ bytes: number;
91
+ sha: string;
92
+ thumb?: string;
93
+ /**
94
+ * A copy on the relay of the room while it lasts (docs/design/ta-chat-history.md 7), for whoever comes when no
95
+ * member who has it is online: its id there, what reads it and what opens it (AES-256-GCM), till when.
96
+ */
97
+ blob?: RelayChatBlob;
98
+ }
99
+ /** A sealed copy of a picture on the relay (RelayChatImage.blob): the relay reads neither it nor what opens it. */
100
+ export interface RelayChatBlob {
101
+ id: string;
102
+ /** base64url, 32 bytes: the token that reads it (the relay keeps its SHA-256). */
103
+ read: string;
104
+ /** base64url, 32 bytes: the key it is sealed with. */
105
+ key: string;
106
+ until: number;
107
+ }
108
+ /** What a file of a line is: any file, a voice message (`ms` long), a video (`ms` long, `w` × `h`, a blurred frame). */
109
+ export type RelayChatFileKind = 'file' | 'voice' | 'video';
110
+ /**
111
+ * A file of a line (RelayChatLine.files): its id among the files of the sender, its name, type, size and the SHA-256
112
+ * of its bytes, which whoever fetches it checks.
113
+ */
114
+ export interface RelayChatFile {
115
+ id: string;
116
+ kind: RelayChatFileKind;
117
+ name: string;
118
+ type: string;
119
+ bytes: number;
120
+ sha: string;
121
+ ms?: number;
122
+ w?: number;
123
+ h?: number;
124
+ thumb?: string;
51
125
  }
126
+ /** The bytes of a file of each kind at most: what passes between members in a minute or two. */
127
+ export declare const RELAY_CHAT_FILE_BYTES: Record<RelayChatFileKind, number>;
128
+ /** How long a voice message and a video are at most. */
129
+ export declare const RELAY_CHAT_VOICE_MS = 60000;
130
+ export declare const RELAY_CHAT_VIDEO_MS: number;
131
+ /** The files of a line at most. */
132
+ export declare const RELAY_CHAT_FILES = 9;
52
133
  /** An agent of a member's Ta, in a room (RelayChatLine.agent). */
53
134
  export interface RelayChatAgent {
54
135
  id: string;
55
136
  name: string;
56
137
  }
57
138
  /** What a line adds to its text: who said it and to whom (RelayRoomChat.say). */
58
- export type RelayChatLineExtra = Pick<RelayChatLine, 'double' | 'agent' | 'to' | 'person' | 're' | 'task'>;
59
- /** What is kept of a line: `from` is the key's. */
60
- type StoredLine = Omit<RelayChatLine, 'from'>;
139
+ export type RelayChatLineExtra = Pick<RelayChatLine, 'double' | 'agent' | 'to' | 'person' | 're' | 'task' | 'images' | 'files'>;
140
+ /** What is kept of a line, and what a member signs of it in the log: `from` is the key's, or the signer's. */
141
+ export type RelayChatStored = Omit<RelayChatLine, 'from' | 'seq' | 'pending' | 'failed'>;
142
+ type StoredLine = RelayChatStored;
61
143
  /** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
62
144
  export declare const RELAY_CHAT_KEY = "chat:";
63
145
  /** The bytes of JSON of one part at most, well under what the relay takes of a value once sealed. */
@@ -65,6 +147,14 @@ export declare const RELAY_CHAT_PART_BYTES: number;
65
147
  /** The parts of each member kept: the oldest is cleared as a new one starts. */
66
148
  export declare const RELAY_CHAT_PARTS = 6;
67
149
  export declare const RELAY_CHAT_TEXT_MAX = 2000;
150
+ /** The pictures of a line at most, as WeChat sends nine at once. */
151
+ export declare const RELAY_CHAT_IMAGES = 9;
152
+ /** The bytes of a picture at most. */
153
+ export declare const RELAY_CHAT_IMAGE_BYTES: number;
154
+ /** The characters of the data URL of a thumbnail at most. */
155
+ export declare const RELAY_CHAT_THUMB_CHARS = 900;
156
+ /** Lines a chat holds at once: the latest; older ones are read as the page asks (RelayRoomChat.older). */
157
+ export declare const RELAY_CHAT_WINDOW = 500;
68
158
  export declare namespace RelayRoomChatUtil {
69
159
  function key(member: string, part: number): string;
70
160
  function parse(key: string): {
@@ -75,8 +165,16 @@ export declare namespace RelayRoomChatUtil {
75
165
  function lines(member: string, value: unknown, by: string): RelayChatLine[] | undefined;
76
166
  /** What a stored line adds, as far as it is well formed. */
77
167
  function extra(line: Record<string, unknown>): RelayChatLineExtra;
168
+ /** The files of a stored line, as far as each is well formed. */
169
+ function files(value: unknown): Pick<RelayChatLine, 'files'>;
170
+ /** What a line says in a word where only text shows (the list of chats, a notice, a page of guests). */
171
+ function preview(line: Pick<RelayChatLine, 'text' | 'images' | 'files'>, picture: string, words?: Record<RelayChatFileKind, string>): string;
78
172
  function stored(lines: RelayChatLine[]): StoredLine[];
173
+ /** A line of the log, as its member signed it: well formed, or none. */
174
+ function signed(member: string, value: unknown, seq?: number): RelayChatLine | undefined;
79
175
  function bytes(lines: StoredLine[]): number;
176
+ /** An error of a call a Ta service of before does not know (JSON-RPC's method not found, or its words). */
177
+ function unknownMethod(error: unknown): boolean;
80
178
  function id(): string;
81
179
  }
82
180
  interface Part {
@@ -96,11 +194,34 @@ export declare class RelayRoomChat {
96
194
  protected readonly parts: Map<string, Part>;
97
195
  protected readonly listeners: Set<() => void>;
98
196
  protected all: readonly RelayChatLine[];
197
+ /** Older lines are kept than those read (the log, RelayRoomChatRooms.readChat). */
198
+ protected olderKept: boolean;
199
+ /**
200
+ * What is said is the log (RelayRoomChatRooms.readChat): until a Ta service of before, whose proxy names every
201
+ * method all the same, fails it; then the state, as before.
202
+ */
203
+ protected log: boolean;
99
204
  constructor(rooms: RelayRoomChatRooms, room: string);
100
205
  /** All that is said, the earliest first. */
101
206
  get lines(): readonly RelayChatLine[];
102
207
  subscribe(listener: () => void): () => void;
208
+ /** Whether there are older lines than those read: `older` reads them. */
209
+ get more(): boolean;
103
210
  load(): Promise<void>;
211
+ /** Lines before those read, a page of them: how many came. */
212
+ older(limit?: number): Promise<number>;
213
+ /**
214
+ * What happened in the room, as its event tells: a line of the log (`chat`), or a key of the state set (`state`,
215
+ * the chat of a Ta service of before). Whether it was of the chat.
216
+ */
217
+ hear(event: {
218
+ type: string;
219
+ room?: string;
220
+ key?: string;
221
+ line?: RelayChatLine;
222
+ } & Partial<RelayRoomChatValue>): boolean;
223
+ /** A line said or taken: one of the same id is replaced (sent, refused), a new one comes last. */
224
+ heardLine(line: RelayChatLine): void;
104
225
  /** A key of the room set, as its event tells: whether it was of the chat. */
105
226
  heard(key: string, value: RelayRoomChatValue): boolean;
106
227
  /**
@@ -115,6 +236,7 @@ export declare class RelayRoomChat {
115
236
  protected latest(member: string): Part | undefined;
116
237
  protected put(key: string, value: RelayRoomChatValue): void;
117
238
  protected changed(): void;
239
+ protected tell(): void;
118
240
  }
119
241
  export {};
120
242
  //# sourceMappingURL=relay-room-chat.d.ts.map
@@ -1,6 +1,13 @@
1
1
  "use strict";
2
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;
3
+ exports.RelayRoomChat = exports.RelayRoomChatUtil = exports.RELAY_CHAT_WINDOW = exports.RELAY_CHAT_THUMB_CHARS = exports.RELAY_CHAT_IMAGE_BYTES = exports.RELAY_CHAT_IMAGES = exports.RELAY_CHAT_TEXT_MAX = exports.RELAY_CHAT_PARTS = exports.RELAY_CHAT_PART_BYTES = exports.RELAY_CHAT_KEY = exports.RELAY_CHAT_FILES = exports.RELAY_CHAT_VIDEO_MS = exports.RELAY_CHAT_VOICE_MS = exports.RELAY_CHAT_FILE_BYTES = void 0;
4
+ /** The bytes of a file of each kind at most: what passes between members in a minute or two. */
5
+ exports.RELAY_CHAT_FILE_BYTES = { file: 50 * 1024 * 1024, video: 30 * 1024 * 1024, voice: 2 * 1024 * 1024 };
6
+ /** How long a voice message and a video are at most. */
7
+ exports.RELAY_CHAT_VOICE_MS = 60000;
8
+ exports.RELAY_CHAT_VIDEO_MS = 3 * 60000;
9
+ /** The files of a line at most. */
10
+ exports.RELAY_CHAT_FILES = 9;
4
11
  /** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
5
12
  exports.RELAY_CHAT_KEY = 'chat:';
6
13
  /** The bytes of JSON of one part at most, well under what the relay takes of a value once sealed. */
@@ -8,6 +15,15 @@ exports.RELAY_CHAT_PART_BYTES = 8 * 1024;
8
15
  /** The parts of each member kept: the oldest is cleared as a new one starts. */
9
16
  exports.RELAY_CHAT_PARTS = 6;
10
17
  exports.RELAY_CHAT_TEXT_MAX = 2000;
18
+ /** The pictures of a line at most, as WeChat sends nine at once. */
19
+ exports.RELAY_CHAT_IMAGES = 9;
20
+ /** The bytes of a picture at most. */
21
+ exports.RELAY_CHAT_IMAGE_BYTES = 2 * 1024 * 1024;
22
+ /** The characters of the data URL of a thumbnail at most. */
23
+ exports.RELAY_CHAT_THUMB_CHARS = 900;
24
+ const IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
25
+ /** Lines a chat holds at once: the latest; older ones are read as the page asks (RelayRoomChat.older). */
26
+ exports.RELAY_CHAT_WINDOW = 500;
11
27
  const TRIES = 6;
12
28
  var RelayRoomChatUtil;
13
29
  (function (RelayRoomChatUtil) {
@@ -47,17 +63,141 @@ var RelayRoomChatUtil;
47
63
  ...(line.person === true && to.length && line.double !== true && !line.agent ? { person: true } : {}),
48
64
  ...(typeof line.re === 'string' ? { re: line.re } : {}),
49
65
  ...(line.task === true && to.length ? { task: true } : {}),
66
+ ...images(line.images),
67
+ ...files(line.files),
50
68
  };
51
69
  }
52
70
  RelayRoomChatUtil.extra = extra;
71
+ /** The files of a stored line, as far as each is well formed. */
72
+ function files(value) {
73
+ if (!Array.isArray(value)) {
74
+ return {};
75
+ }
76
+ const files = [];
77
+ for (const file of value.slice(0, exports.RELAY_CHAT_FILES)) {
78
+ const kind = file === null || file === void 0 ? void 0 : file.kind;
79
+ if (!file ||
80
+ (kind !== 'file' && kind !== 'voice' && kind !== 'video') ||
81
+ typeof file.id !== 'string' ||
82
+ !/^[a-f0-9]{24}$/.test(file.id) ||
83
+ typeof file.sha !== 'string' ||
84
+ !/^[0-9a-f]{64}$/.test(file.sha) ||
85
+ !Number.isInteger(file.bytes) ||
86
+ file.bytes <= 0 ||
87
+ file.bytes > exports.RELAY_CHAT_FILE_BYTES[kind]) {
88
+ continue;
89
+ }
90
+ const name = [...(typeof file.name === 'string' ? file.name : '')]
91
+ .filter(char => char.charCodeAt(0) >= 32 && char !== '/' && char !== '\\')
92
+ .join('')
93
+ .slice(0, 120) || kind;
94
+ const type = typeof file.type === 'string' && /^[\w.+-]+\/[\w.+-]+$/.test(file.type) ? file.type.slice(0, 100) : 'application/octet-stream';
95
+ const most = kind === 'voice' ? exports.RELAY_CHAT_VOICE_MS : exports.RELAY_CHAT_VIDEO_MS;
96
+ const ms = kind !== 'file' && Number.isFinite(file.ms) ? Math.min(Math.max(Math.round(file.ms), 0), most) : undefined;
97
+ const size = kind === 'video' && Number.isInteger(file.w) && Number.isInteger(file.h) && file.w > 0 && file.h > 0;
98
+ const thumb = kind === 'video' &&
99
+ typeof file.thumb === 'string' &&
100
+ file.thumb.length <= exports.RELAY_CHAT_THUMB_CHARS &&
101
+ /^data:image\/(jpeg|png|webp);base64,/.test(file.thumb)
102
+ ? file.thumb
103
+ : undefined;
104
+ files.push({
105
+ id: file.id,
106
+ kind,
107
+ name,
108
+ type,
109
+ bytes: file.bytes,
110
+ sha: file.sha,
111
+ ...(ms !== undefined ? { ms } : {}),
112
+ ...(size ? { w: file.w, h: file.h } : {}),
113
+ ...(thumb ? { thumb } : {}),
114
+ });
115
+ }
116
+ return files.length ? { files } : {};
117
+ }
118
+ RelayRoomChatUtil.files = files;
119
+ /** The pictures of a stored line, as far as each is well formed. */
120
+ function images(value) {
121
+ if (!Array.isArray(value)) {
122
+ return {};
123
+ }
124
+ const images = [];
125
+ for (const image of value.slice(0, exports.RELAY_CHAT_IMAGES)) {
126
+ if (image &&
127
+ typeof image.id === 'string' &&
128
+ /^[A-Za-z0-9-]{8,64}$/.test(image.id) &&
129
+ typeof image.type === 'string' &&
130
+ IMAGE_TYPES.includes(image.type) &&
131
+ typeof image.sha === 'string' &&
132
+ /^[0-9a-f]{64}$/.test(image.sha) &&
133
+ Number.isInteger(image.w) &&
134
+ Number.isInteger(image.h) &&
135
+ Number.isInteger(image.bytes) &&
136
+ image.w > 0 &&
137
+ image.h > 0 &&
138
+ image.bytes > 0 &&
139
+ image.bytes <= exports.RELAY_CHAT_IMAGE_BYTES) {
140
+ const thumb = typeof image.thumb === 'string' && image.thumb.length <= exports.RELAY_CHAT_THUMB_CHARS && /^data:image\/(jpeg|png|webp);base64,/.test(image.thumb)
141
+ ? image.thumb
142
+ : undefined;
143
+ const blob = image.blob;
144
+ const ok = !!blob &&
145
+ typeof blob.id === 'string' &&
146
+ /^[A-Za-z0-9_-]{16,64}$/.test(blob.id) &&
147
+ typeof blob.read === 'string' &&
148
+ /^[A-Za-z0-9_-]{43}$/.test(blob.read) &&
149
+ typeof blob.key === 'string' &&
150
+ /^[A-Za-z0-9_-]{43}$/.test(blob.key) &&
151
+ Number.isFinite(blob.until);
152
+ images.push({
153
+ id: image.id,
154
+ type: image.type,
155
+ w: image.w,
156
+ h: image.h,
157
+ bytes: image.bytes,
158
+ sha: image.sha,
159
+ ...(thumb ? { thumb } : {}),
160
+ ...(ok && blob ? { blob: { id: blob.id, read: blob.read, key: blob.key, until: blob.until } } : {}),
161
+ });
162
+ }
163
+ }
164
+ return images.length ? { images } : {};
165
+ }
166
+ /** What a line says in a word where only text shows (the list of chats, a notice, a page of guests). */
167
+ function preview(line, picture, words = { file: '[file]', voice: '[voice]', video: '[video]' }) {
168
+ var _a;
169
+ var _b;
170
+ const pictures = ((_a = line.images) === null || _a === void 0 ? void 0 : _a.length) ? (line.images.length > 1 ? `${picture}×${line.images.length}` : picture) : '';
171
+ const files = ((_b = line.files) !== null && _b !== void 0 ? _b : []).map(file => (file.kind === 'file' ? `${words.file} ${file.name}` : words[file.kind]));
172
+ return [pictures, ...files, line.text].filter(Boolean).join(' ');
173
+ }
174
+ RelayRoomChatUtil.preview = preview;
53
175
  function stored(lines) {
54
- return lines.map(({ from: _from, ...line }) => line);
176
+ return lines.map(({ from: _from, seq: _seq, pending: _pending, failed: _failed, ...line }) => line);
55
177
  }
56
178
  RelayRoomChatUtil.stored = stored;
179
+ /** A line of the log, as its member signed it: well formed, or none. */
180
+ function signed(member, value, seq) {
181
+ const line = value;
182
+ if (!line || typeof line.id !== 'string' || typeof line.text !== 'string' || typeof line.at !== 'number') {
183
+ return undefined;
184
+ }
185
+ const name = typeof line.name === 'string' ? line.name.slice(0, 64) : '';
186
+ const text = line.text.slice(0, exports.RELAY_CHAT_TEXT_MAX);
187
+ return { id: line.id.slice(0, 40), from: member, name, text, at: line.at, ...extra(line), ...(seq ? { seq } : {}) };
188
+ }
189
+ RelayRoomChatUtil.signed = signed;
57
190
  function bytes(lines) {
58
191
  return new TextEncoder().encode(JSON.stringify(lines)).length;
59
192
  }
60
193
  RelayRoomChatUtil.bytes = bytes;
194
+ /** An error of a call a Ta service of before does not know (JSON-RPC's method not found, or its words). */
195
+ function unknownMethod(error) {
196
+ var _a;
197
+ const found = error;
198
+ return (found === null || found === void 0 ? void 0 : found.code) === -32601 || /method not found|not a function|unknown method|no such method/i.test(String((_a = found === null || found === void 0 ? void 0 : found.message) !== null && _a !== void 0 ? _a : error));
199
+ }
200
+ RelayRoomChatUtil.unknownMethod = unknownMethod;
61
201
  function id() {
62
202
  return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
63
203
  }
@@ -75,6 +215,9 @@ class RelayRoomChat {
75
215
  this.parts = new Map();
76
216
  this.listeners = new Set();
77
217
  this.all = [];
218
+ /** Older lines are kept than those read (the log, RelayRoomChatRooms.readChat). */
219
+ this.olderKept = false;
220
+ this.log = typeof rooms.readChat === 'function' && typeof rooms.say === 'function';
78
221
  }
79
222
  /** All that is said, the earliest first. */
80
223
  get lines() {
@@ -84,7 +227,26 @@ class RelayRoomChat {
84
227
  this.listeners.add(listener);
85
228
  return () => this.listeners.delete(listener);
86
229
  }
230
+ /** Whether there are older lines than those read: `older` reads them. */
231
+ get more() {
232
+ return this.olderKept;
233
+ }
87
234
  async load() {
235
+ if (this.log && this.rooms.readChat) {
236
+ const read = await this.rooms.readChat(this.room, { limit: exports.RELAY_CHAT_WINDOW }).catch((error) => {
237
+ if (!RelayRoomChatUtil.unknownMethod(error)) {
238
+ throw error;
239
+ }
240
+ this.log = false;
241
+ return undefined;
242
+ });
243
+ if (read) {
244
+ this.olderKept = read.more;
245
+ this.all = Array.isArray(read.lines) ? read.lines : [];
246
+ this.tell();
247
+ return;
248
+ }
249
+ }
88
250
  const state = await this.rooms.getState(this.room, exports.RELAY_CHAT_KEY);
89
251
  this.parts.clear();
90
252
  for (const [key, value] of Object.entries(state)) {
@@ -92,8 +254,60 @@ class RelayRoomChat {
92
254
  }
93
255
  this.changed();
94
256
  }
257
+ /** Lines before those read, a page of them: how many came. */
258
+ async older(limit = 100) {
259
+ const first = this.all[0];
260
+ if (!this.log || !this.rooms.readChat || !first || !this.olderKept) {
261
+ return 0;
262
+ }
263
+ const read = await this.rooms.readChat(this.room, { before: first.id, limit });
264
+ const known = new Set(this.all.map(line => line.id));
265
+ const lines = read.lines.filter(line => !known.has(line.id));
266
+ this.olderKept = read.more;
267
+ this.all = [...lines, ...this.all];
268
+ this.tell();
269
+ return lines.length;
270
+ }
271
+ /**
272
+ * What happened in the room, as its event tells: a line of the log (`chat`), or a key of the state set (`state`,
273
+ * the chat of a Ta service of before). Whether it was of the chat.
274
+ */
275
+ hear(event) {
276
+ if (event.room !== undefined && event.room !== this.room) {
277
+ return false;
278
+ }
279
+ if (event.type === 'chat' && event.line) {
280
+ this.heardLine(event.line);
281
+ return true;
282
+ }
283
+ return event.type === 'state' && typeof event.key === 'string' && this.heard(event.key, event);
284
+ }
285
+ /** A line said or taken: one of the same id is replaced (sent, refused), a new one comes last. */
286
+ heardLine(line) {
287
+ const index = this.all.findIndex(each => each.id === line.id);
288
+ if (index >= 0) {
289
+ const next = [...this.all];
290
+ next[index] = line;
291
+ this.all = next;
292
+ }
293
+ else {
294
+ const next = [...this.all, line];
295
+ // A line taken while one of this Ta waits goes before it: the pending come last.
296
+ const pending = next.findIndex(each => each.pending);
297
+ if (pending >= 0 && !line.pending) {
298
+ next.splice(next.length - 1, 1);
299
+ next.splice(pending, 0, line);
300
+ }
301
+ this.all = next.length > exports.RELAY_CHAT_WINDOW * 2 ? next.slice(-exports.RELAY_CHAT_WINDOW) : next;
302
+ this.olderKept || (this.olderKept = next.length > this.all.length);
303
+ }
304
+ this.tell();
305
+ }
95
306
  /** A key of the room set, as its event tells: whether it was of the chat. */
96
307
  heard(key, value) {
308
+ if (this.log) {
309
+ return !!RelayRoomChatUtil.parse(key);
310
+ }
97
311
  if (!RelayRoomChatUtil.parse(key)) {
98
312
  return false;
99
313
  }
@@ -110,17 +324,24 @@ class RelayRoomChat {
110
324
  * again on what another client of the same Ta wrote first. The line said, or undefined when there was nothing to say.
111
325
  */
112
326
  async say(text, self, extra = {}) {
113
- var _a;
114
- var _b, _c;
327
+ var _a, _b, _c;
328
+ var _d, _e;
115
329
  const said = text.trim().slice(0, exports.RELAY_CHAT_TEXT_MAX);
116
- if (!said) {
330
+ if (!said && !((_a = extra.images) === null || _a === void 0 ? void 0 : _a.length) && !((_b = extra.files) === null || _b === void 0 ? void 0 : _b.length)) {
117
331
  return undefined;
118
332
  }
333
+ if (this.log && this.rooms.say) {
334
+ const line = await this.rooms.say(this.room, said, RelayRoomChatUtil.extra(extra));
335
+ if (line) {
336
+ this.heardLine(line);
337
+ }
338
+ return line;
339
+ }
119
340
  for (let tries = 0; tries < TRIES; tries++) {
120
341
  const latest = this.latest(self.member);
121
342
  // After what it said last, even within the same millisecond: the lines of a member keep their order.
122
343
  const last = latest === null || latest === void 0 ? void 0 : latest.lines[latest.lines.length - 1];
123
- const at = Math.max(Date.now(), ((_b = last === null || last === void 0 ? void 0 : last.at) !== null && _b !== void 0 ? _b : 0) + 1);
344
+ const at = Math.max(Date.now(), ((_d = last === null || last === void 0 ? void 0 : last.at) !== null && _d !== void 0 ? _d : 0) + 1);
124
345
  const line = {
125
346
  id: RelayRoomChatUtil.id(),
126
347
  from: self.member,
@@ -134,7 +355,7 @@ class RelayRoomChat {
134
355
  const lines = fits && latest ? [...latest.lines, line] : [line];
135
356
  const key = RelayRoomChatUtil.key(self.member, part);
136
357
  const value = RelayRoomChatUtil.stored(lines);
137
- 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);
358
+ const result = await this.rooms.setState(this.room, key, value, (_e = (_c = this.parts.get(key)) === null || _c === void 0 ? void 0 : _c.version) !== null && _e !== void 0 ? _e : 0);
138
359
  if (result.ok) {
139
360
  this.put(key, { value, version: result.version, by: self.member });
140
361
  this.changed();
@@ -173,6 +394,9 @@ class RelayRoomChat {
173
394
  }
174
395
  changed() {
175
396
  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));
397
+ this.tell();
398
+ }
399
+ tell() {
176
400
  for (const listener of this.listeners) {
177
401
  listener();
178
402
  }
@@ -10,6 +10,8 @@ declare const zh: {
10
10
  namePlaceholder: string;
11
11
  join: string;
12
12
  joining: string;
13
+ picture: string;
14
+ file: string;
13
15
  keep: string;
14
16
  haveTa: string;
15
17
  waiting: string;