@celljs/ta-relay 4.10.0 → 4.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +13 -1
- package/README.md +13 -1
- package/esm/common/index.d.ts +9 -0
- package/esm/common/index.js +9 -0
- package/esm/common/relay-blobs.d.ts +54 -0
- package/esm/common/relay-blobs.js +153 -0
- package/esm/common/relay-card.d.ts +23 -0
- package/esm/common/relay-card.js +30 -0
- package/esm/common/relay-content.d.ts +104 -0
- package/esm/common/relay-content.js +316 -0
- package/esm/common/relay-conversation.d.ts +59 -0
- package/esm/common/relay-conversation.js +9 -0
- package/esm/common/relay-hub-util.d.ts +91 -0
- package/esm/common/relay-hub-util.js +164 -0
- package/esm/common/relay-hub.d.ts +125 -0
- package/esm/common/relay-hub.js +438 -0
- package/esm/common/relay-hubs.d.ts +271 -0
- package/esm/common/relay-hubs.js +844 -0
- package/esm/common/relay-identity.d.ts +46 -0
- package/esm/common/relay-identity.js +126 -0
- package/esm/common/relay-protocol.d.ts +211 -2
- package/esm/common/relay-protocol.js +53 -0
- package/esm/common/relay-room-chat.d.ts +80 -0
- package/esm/common/relay-room-chat.js +152 -0
- package/esm/common/relay-socket.d.ts +8 -0
- package/esm/common/relay-socket.js +23 -2
- package/esm/guest/guest-i18n.d.ts +47 -0
- package/esm/guest/guest-i18n.js +97 -0
- package/esm/guest/guest-style.d.ts +1 -0
- package/esm/guest/guest-style.js +78 -0
- package/esm/guest/guest.d.ts +1 -0
- package/esm/guest/guest.js +371 -0
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/relay-host.d.ts +25 -3
- package/esm/node/relay-host.js +115 -4
- package/esm/node/relay-hub-files.d.ts +12 -0
- package/esm/node/relay-hub-files.js +39 -0
- package/esm/node/relay-server.d.ts +46 -1
- package/esm/node/relay-server.js +227 -1
- package/esm/node/relay-store.d.ts +72 -0
- package/esm/node/relay-store.js +123 -2
- package/esm/node/relay-viewer.d.ts +7 -0
- package/esm/node/relay-viewer.js +33 -0
- package/esm/viewer/viewer-i18n.d.ts +56 -0
- package/esm/viewer/viewer-i18n.js +117 -0
- package/esm/viewer/viewer-markdown.d.ts +7 -0
- package/esm/viewer/viewer-markdown.js +198 -0
- package/esm/viewer/viewer-style.d.ts +1 -0
- package/esm/viewer/viewer-style.js +72 -0
- package/esm/viewer/viewer.d.ts +8 -0
- package/esm/viewer/viewer.js +378 -0
- package/lib/common/index.d.ts +9 -0
- package/lib/common/index.js +9 -0
- package/lib/common/relay-blobs.d.ts +55 -0
- package/lib/common/relay-blobs.js +157 -0
- package/lib/common/relay-card.d.ts +24 -0
- package/lib/common/relay-card.js +33 -0
- package/lib/common/relay-content.d.ts +105 -0
- package/lib/common/relay-content.js +320 -0
- package/lib/common/relay-conversation.d.ts +60 -0
- package/lib/common/relay-conversation.js +12 -0
- package/lib/common/relay-hub-util.d.ts +92 -0
- package/lib/common/relay-hub-util.js +167 -0
- package/lib/common/relay-hub.d.ts +126 -0
- package/lib/common/relay-hub.js +442 -0
- package/lib/common/relay-hubs.d.ts +272 -0
- package/lib/common/relay-hubs.js +848 -0
- package/lib/common/relay-identity.d.ts +47 -0
- package/lib/common/relay-identity.js +129 -0
- package/lib/common/relay-protocol.d.ts +211 -2
- package/lib/common/relay-protocol.js +54 -1
- package/lib/common/relay-room-chat.d.ts +81 -0
- package/lib/common/relay-room-chat.js +156 -0
- package/lib/common/relay-socket.d.ts +8 -0
- package/lib/common/relay-socket.js +23 -2
- package/lib/guest/guest-i18n.d.ts +48 -0
- package/lib/guest/guest-i18n.js +100 -0
- package/lib/guest/guest-style.d.ts +2 -0
- package/lib/guest/guest-style.js +81 -0
- package/lib/guest/guest.d.ts +2 -0
- package/lib/guest/guest.js +373 -0
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/relay-host.d.ts +25 -3
- package/lib/node/relay-host.js +115 -4
- package/lib/node/relay-hub-files.d.ts +13 -0
- package/lib/node/relay-hub-files.js +43 -0
- package/lib/node/relay-server.d.ts +46 -1
- package/lib/node/relay-server.js +226 -0
- package/lib/node/relay-store.d.ts +72 -0
- package/lib/node/relay-store.js +121 -0
- package/lib/node/relay-viewer.d.ts +8 -0
- package/lib/node/relay-viewer.js +32 -0
- package/lib/viewer/viewer-i18n.d.ts +57 -0
- package/lib/viewer/viewer-i18n.js +120 -0
- package/lib/viewer/viewer-markdown.d.ts +8 -0
- package/lib/viewer/viewer-markdown.js +201 -0
- package/lib/viewer/viewer-style.d.ts +2 -0
- package/lib/viewer/viewer-style.js +75 -0
- package/lib/viewer/viewer.d.ts +9 -0
- package/lib/viewer/viewer.js +381 -0
- package/package.json +5 -4
- package/viewer/frame.html +1 -0
- package/viewer/page.html +80 -0
- package/viewer/room.html +87 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const relay_hub_util_1 = require("../common/relay-hub-util");
|
|
4
|
+
const relay_hubs_1 = require("../common/relay-hubs");
|
|
5
|
+
const relay_room_chat_1 = require("../common/relay-room-chat");
|
|
6
|
+
const viewer_markdown_1 = require("../viewer/viewer-markdown");
|
|
7
|
+
const guest_i18n_1 = require("./guest-i18n");
|
|
8
|
+
const guest_style_1 = require("./guest-style");
|
|
9
|
+
/*
|
|
10
|
+
* The page a link inviting into a room opens in a browser (docs/design/ta-link.md 3.8): one without Ta asks to join
|
|
11
|
+
* as a guest, and once its owner lets it in, chats there. The keys are this browser's, kept for the relay's origin
|
|
12
|
+
* alone; the relay serves this page and never sees what is said.
|
|
13
|
+
*/
|
|
14
|
+
const { t } = guest_i18n_1.GuestI18n;
|
|
15
|
+
const el = viewer_markdown_1.ViewerMarkdown.el;
|
|
16
|
+
const NAMED = 'ta-guest:named';
|
|
17
|
+
/** A new run after this long without a word of the same member; a line with the time after a longer pause. */
|
|
18
|
+
const RUN_MS = 5 * 60 * 1000;
|
|
19
|
+
const PAUSE_MS = 15 * 60 * 1000;
|
|
20
|
+
/** What a guest keeps: the storage of this browser, for the origin of the relay alone. */
|
|
21
|
+
class BrowserStore {
|
|
22
|
+
read(name) {
|
|
23
|
+
try {
|
|
24
|
+
const text = localStorage.getItem(`ta-guest:${name}`);
|
|
25
|
+
return text ? JSON.parse(text) : undefined;
|
|
26
|
+
}
|
|
27
|
+
catch (_a) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
write(name, value) {
|
|
32
|
+
try {
|
|
33
|
+
localStorage.setItem(`ta-guest:${name}`, JSON.stringify(value));
|
|
34
|
+
}
|
|
35
|
+
catch (_a) {
|
|
36
|
+
// A browser that keeps nothing (a private window that is full): this visit alone.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function div(className, ...children) {
|
|
41
|
+
const node = el('div');
|
|
42
|
+
node.className = className;
|
|
43
|
+
node.append(...children);
|
|
44
|
+
return node;
|
|
45
|
+
}
|
|
46
|
+
function tint(id) {
|
|
47
|
+
let hash = 0;
|
|
48
|
+
for (const char of id) {
|
|
49
|
+
hash = (hash * 31 + char.charCodeAt(0)) | 0;
|
|
50
|
+
}
|
|
51
|
+
return Math.abs(hash) % 6;
|
|
52
|
+
}
|
|
53
|
+
function face(id, name, size, online) {
|
|
54
|
+
var _a;
|
|
55
|
+
var _b;
|
|
56
|
+
const node = el('span', (_b = (_a = Array.from(name.trim())[0]) === null || _a === void 0 ? void 0 : _a.toUpperCase()) !== null && _b !== void 0 ? _b : '?');
|
|
57
|
+
node.className = `face t${tint(id)}`;
|
|
58
|
+
node.style.cssText = `width:${size}px;height:${size}px;font-size:${Math.round(size * 0.42)}px`;
|
|
59
|
+
if (online !== undefined) {
|
|
60
|
+
const dot = el('i');
|
|
61
|
+
dot.className = online ? 'on' : '';
|
|
62
|
+
node.append(dot);
|
|
63
|
+
}
|
|
64
|
+
return node;
|
|
65
|
+
}
|
|
66
|
+
function button(className, text, onClick) {
|
|
67
|
+
const node = el('button', text);
|
|
68
|
+
node.type = 'button';
|
|
69
|
+
node.className = className;
|
|
70
|
+
node.addEventListener('click', onClick);
|
|
71
|
+
return node;
|
|
72
|
+
}
|
|
73
|
+
/** The page: who asks, waits, and chats, as the room the link names is for this browser. */
|
|
74
|
+
class GuestPage {
|
|
75
|
+
constructor(link) {
|
|
76
|
+
this.link = link;
|
|
77
|
+
this.hubs = new relay_hubs_1.RelayHubs({
|
|
78
|
+
store: new BrowserStore(),
|
|
79
|
+
relay: () => location.origin,
|
|
80
|
+
guest: true,
|
|
81
|
+
name: () => (guest_i18n_1.GuestI18n.chinese ? '访客' : 'Guest'),
|
|
82
|
+
});
|
|
83
|
+
this.screen = '';
|
|
84
|
+
this.stuck = true;
|
|
85
|
+
this.connected = false;
|
|
86
|
+
}
|
|
87
|
+
start() {
|
|
88
|
+
this.hubs.onEvent(event => {
|
|
89
|
+
var _a;
|
|
90
|
+
if (event.type === 'state' && event.room === this.link.room) {
|
|
91
|
+
(_a = this.chat) === null || _a === void 0 ? void 0 : _a.heard(event.key, event);
|
|
92
|
+
}
|
|
93
|
+
else if (event.type === 'rooms') {
|
|
94
|
+
this.update();
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
this.hubs.start();
|
|
98
|
+
this.update();
|
|
99
|
+
}
|
|
100
|
+
room() {
|
|
101
|
+
return this.hubs.room(this.link.room);
|
|
102
|
+
}
|
|
103
|
+
/** Shows what holds now: a screen of its own for each status, the chat kept as the room changes. */
|
|
104
|
+
update() {
|
|
105
|
+
var _a;
|
|
106
|
+
var _b;
|
|
107
|
+
const room = this.room();
|
|
108
|
+
const screen = !room ? 'join' : room.status === 'gone' ? `gone:${(_b = room.reason) !== null && _b !== void 0 ? _b : 'closed'}` : room.status;
|
|
109
|
+
if (screen === this.screen && room) {
|
|
110
|
+
if (room.status === 'member') {
|
|
111
|
+
this.refresh(room);
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
this.screen = screen;
|
|
116
|
+
(_a = this.sheet) === null || _a === void 0 ? void 0 : _a.remove();
|
|
117
|
+
this.sheet = undefined;
|
|
118
|
+
if (!room) {
|
|
119
|
+
this.join();
|
|
120
|
+
}
|
|
121
|
+
else if (room.status === 'waiting') {
|
|
122
|
+
this.waiting();
|
|
123
|
+
}
|
|
124
|
+
else if (room.status === 'member') {
|
|
125
|
+
this.chatScreen(room);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this.out(room);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
frame(...children) {
|
|
132
|
+
const app = div('app', ...children);
|
|
133
|
+
document.body.replaceChildren(app);
|
|
134
|
+
return app;
|
|
135
|
+
}
|
|
136
|
+
top() {
|
|
137
|
+
const logo = el('span', 'Ta');
|
|
138
|
+
logo.className = 'logo';
|
|
139
|
+
const heading = div('heading', Object.assign(el('b'), { textContent: t('title') }));
|
|
140
|
+
return div('top', logo, heading);
|
|
141
|
+
}
|
|
142
|
+
join(error) {
|
|
143
|
+
document.title = t('title');
|
|
144
|
+
const field = el('input');
|
|
145
|
+
field.className = 'text';
|
|
146
|
+
field.placeholder = t('namePlaceholder');
|
|
147
|
+
field.maxLength = 40;
|
|
148
|
+
field.enterKeyHint = 'go';
|
|
149
|
+
if (localStorage.getItem(NAMED)) {
|
|
150
|
+
void this.hubs.identity().then(identity => {
|
|
151
|
+
field.value || (field.value = identity.name);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
const label = el('label');
|
|
155
|
+
label.className = 'field';
|
|
156
|
+
label.append(t('name'), field);
|
|
157
|
+
const ask = button('primary', t('join'), () => void submit());
|
|
158
|
+
const shown = error ? [Object.assign(el('p', error), { className: 'error' })] : [];
|
|
159
|
+
const submit = async () => {
|
|
160
|
+
const name = field.value.trim();
|
|
161
|
+
if (!name) {
|
|
162
|
+
field.focus();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
ask.disabled = true;
|
|
166
|
+
ask.textContent = t('joining');
|
|
167
|
+
try {
|
|
168
|
+
await this.hubs.setName(name);
|
|
169
|
+
localStorage.setItem(NAMED, '1');
|
|
170
|
+
await this.hubs.join(location.href);
|
|
171
|
+
this.update();
|
|
172
|
+
}
|
|
173
|
+
catch (failure) {
|
|
174
|
+
this.screen = '';
|
|
175
|
+
this.join(failure instanceof Error && /own/.test(failure.message) ? t('own') : String(failure instanceof Error ? failure.message : failure));
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
field.addEventListener('keydown', event => {
|
|
179
|
+
if (event.key === 'Enter' && !event.isComposing) {
|
|
180
|
+
void submit();
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
this.frame(this.top(), div('center', Object.assign(face(this.link.owner, this.link.by || '?', 56), { className: `face card-face t${tint(this.link.owner)}` }), Object.assign(el('p', t('invited', { by: this.link.by || '…' })), { className: 'lead' }), Object.assign(el('h1', this.link.title || t('title')), { className: 'room-name' }), Object.assign(el('p', t('joinHint')), { className: 'hint' }), label, ...shown, ask, Object.assign(el('p', t('keep')), { className: 'small' }), Object.assign(el('p', t('haveTa')), { className: 'small' })));
|
|
184
|
+
}
|
|
185
|
+
waiting() {
|
|
186
|
+
this.frame(this.top(), div('center', Object.assign(el('div'), { className: 'spin' }), Object.assign(el('h1', t('waiting', { by: this.link.by || '…' })), { className: 'room-name' }), Object.assign(el('p', t('waitingHint')), { className: 'hint' }), button('plain', t('cancel'), () => void this.hubs.leave(this.link.room).then(() => this.update()))));
|
|
187
|
+
}
|
|
188
|
+
out(room) {
|
|
189
|
+
const text = room.status === 'refused'
|
|
190
|
+
? t('refused', { by: room.ownerName || this.link.by })
|
|
191
|
+
: room.reason === 'removed'
|
|
192
|
+
? t('removed')
|
|
193
|
+
: room.reason === 'invite'
|
|
194
|
+
? t('inviteGone')
|
|
195
|
+
: t('closed');
|
|
196
|
+
this.frame(this.top(), div('center', Object.assign(el('h1', text), { className: 'room-name' }), button('plain', t('again'), () => void this.hubs.leave(this.link.room).then(() => this.update()))));
|
|
197
|
+
}
|
|
198
|
+
chatScreen(room) {
|
|
199
|
+
document.title = room.title;
|
|
200
|
+
this.chat = new relay_room_chat_1.RelayRoomChat(this.hubs, room.id);
|
|
201
|
+
this.chat.subscribe(() => this.draw());
|
|
202
|
+
this.connected = room.connected;
|
|
203
|
+
void this.chat.load().catch(() => undefined);
|
|
204
|
+
const heading = div('heading');
|
|
205
|
+
this.heading = heading;
|
|
206
|
+
const more = button('icon', '⋯', () => this.members());
|
|
207
|
+
more.setAttribute('aria-label', t('members'));
|
|
208
|
+
const top = div('top', heading, more);
|
|
209
|
+
const list = div('list');
|
|
210
|
+
list.addEventListener('scroll', () => {
|
|
211
|
+
this.stuck = list.scrollHeight - list.scrollTop - list.clientHeight < 48;
|
|
212
|
+
});
|
|
213
|
+
this.list = list;
|
|
214
|
+
const field = el('textarea');
|
|
215
|
+
field.rows = 1;
|
|
216
|
+
field.placeholder = t('placeholder');
|
|
217
|
+
field.enterKeyHint = 'send';
|
|
218
|
+
const send = button('primary', t('send'), () => void say());
|
|
219
|
+
const fit = () => {
|
|
220
|
+
field.style.height = 'auto';
|
|
221
|
+
field.style.height = `${Math.min(field.scrollHeight + 2, 160)}px`;
|
|
222
|
+
};
|
|
223
|
+
const say = async () => {
|
|
224
|
+
var _a;
|
|
225
|
+
const text = field.value.trim();
|
|
226
|
+
const self = (_a = this.room()) === null || _a === void 0 ? void 0 : _a.members.find(member => member.self);
|
|
227
|
+
if (!text || !self || !this.chat) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
field.value = '';
|
|
231
|
+
fit();
|
|
232
|
+
this.stuck = true;
|
|
233
|
+
try {
|
|
234
|
+
await this.chat.say(text, self);
|
|
235
|
+
}
|
|
236
|
+
catch (failure) {
|
|
237
|
+
field.value || (field.value = text);
|
|
238
|
+
fit();
|
|
239
|
+
const note = Object.assign(el('p', t('failed', { reason: failure instanceof Error ? failure.message : String(failure) })), {
|
|
240
|
+
className: 'failed',
|
|
241
|
+
});
|
|
242
|
+
list.append(note);
|
|
243
|
+
setTimeout(() => note.remove(), 4000);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
field.addEventListener('input', fit);
|
|
247
|
+
field.addEventListener('keydown', event => {
|
|
248
|
+
if (event.key === 'Enter' && !event.shiftKey && !event.isComposing) {
|
|
249
|
+
event.preventDefault();
|
|
250
|
+
void say();
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
const banner = room.view ? [div('banner', t('game'))] : [];
|
|
254
|
+
this.frame(top, ...banner, list, div('box', field, send), div('foot', t('secure')));
|
|
255
|
+
this.refresh(room);
|
|
256
|
+
this.draw();
|
|
257
|
+
}
|
|
258
|
+
/** The head of the chat, and the members if shown, as the room changed (who is online, the roster). */
|
|
259
|
+
refresh(room) {
|
|
260
|
+
var _a, _b;
|
|
261
|
+
if (room.connected && !this.connected) {
|
|
262
|
+
// Connected again: what was said meanwhile.
|
|
263
|
+
void ((_a = this.chat) === null || _a === void 0 ? void 0 : _a.load().catch(() => undefined));
|
|
264
|
+
}
|
|
265
|
+
this.connected = room.connected;
|
|
266
|
+
const title = el('b', room.title);
|
|
267
|
+
title.append(Object.assign(el('span', `(${room.members.length})`)));
|
|
268
|
+
const online = room.members.filter(member => member.online && !member.self).length;
|
|
269
|
+
const sub = el('small', !room.connected ? t('connecting') : online ? t('online', { n: online }) : t('nobody'));
|
|
270
|
+
sub.className = room.connected && online ? '' : 'quiet';
|
|
271
|
+
(_b = this.heading) === null || _b === void 0 ? void 0 : _b.replaceChildren(title, sub);
|
|
272
|
+
if (this.sheet) {
|
|
273
|
+
this.members();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
draw() {
|
|
277
|
+
var _a;
|
|
278
|
+
const list = this.list;
|
|
279
|
+
const room = this.room();
|
|
280
|
+
if (!list || !room || !this.chat) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const lines = this.chat.lines;
|
|
284
|
+
const self = (_a = room.members.find(member => member.self)) === null || _a === void 0 ? void 0 : _a.member;
|
|
285
|
+
const nameOf = (line) => { var _a; var _b; return (_b = (_a = room.members.find(member => member.member === line.from)) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : line.name; };
|
|
286
|
+
const nodes = [];
|
|
287
|
+
if (!lines.length) {
|
|
288
|
+
nodes.push(div('empty', t('empty')));
|
|
289
|
+
}
|
|
290
|
+
lines.forEach((line, index) => {
|
|
291
|
+
var _a;
|
|
292
|
+
const before = lines[index - 1];
|
|
293
|
+
const separator = !before || line.at - before.at > PAUSE_MS;
|
|
294
|
+
const first = separator || before.from !== line.from || line.at - before.at > RUN_MS;
|
|
295
|
+
if (separator) {
|
|
296
|
+
nodes.push(div('when', guest_i18n_1.GuestI18n.time(line.at)));
|
|
297
|
+
}
|
|
298
|
+
const bubble = div('bubble', viewer_markdown_1.ViewerMarkdown.render(line.text));
|
|
299
|
+
bubble.title = guest_i18n_1.GuestI18n.time(line.at);
|
|
300
|
+
if (line.from === self) {
|
|
301
|
+
nodes.push(div('mine', bubble));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
const other = room.members.find(member => member.member === line.from);
|
|
305
|
+
const col = div('col', ...(first ? [div('who', nameOf(line))] : []), bubble);
|
|
306
|
+
nodes.push(div(`msg${first ? ' first' : ''}`, div('slot', ...(first ? [face((_a = other === null || other === void 0 ? void 0 : other.id) !== null && _a !== void 0 ? _a : line.from, nameOf(line), 32)] : [])), col));
|
|
307
|
+
});
|
|
308
|
+
list.replaceChildren(...nodes);
|
|
309
|
+
if (this.stuck) {
|
|
310
|
+
list.scrollTop = list.scrollHeight;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/** The members, over the chat: who is online, and the user's name and way out. */
|
|
314
|
+
members() {
|
|
315
|
+
var _a;
|
|
316
|
+
const room = this.room();
|
|
317
|
+
if (!room) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
const panel = div('panel', Object.assign(el('h3', `${t('members')} (${room.members.length})`)));
|
|
321
|
+
for (const member of room.members) {
|
|
322
|
+
const tags = [member.self ? t('you') : '', member.owner ? t('owner') : '', member.guest ? t('guest') : ''].filter(Boolean);
|
|
323
|
+
const name = div('nm', member.name, ...tags.map(tag => Object.assign(el('span', tag), { className: 'tag' })));
|
|
324
|
+
const status = Object.assign(el('span', member.online ? t('onlineNow') : ''), { className: 'st on' });
|
|
325
|
+
panel.append(div('member', face(member.id, member.name, 32, member.online), name, status));
|
|
326
|
+
}
|
|
327
|
+
panel.append(div('sep'), button('row', t('rename'), () => {
|
|
328
|
+
var _a, _b;
|
|
329
|
+
var _c;
|
|
330
|
+
const name = (_b = prompt(t('name'), (_c = (_a = room.members.find(member => member.self)) === null || _a === void 0 ? void 0 : _a.name) !== null && _c !== void 0 ? _c : '')) === null || _b === void 0 ? void 0 : _b.trim();
|
|
331
|
+
if (name) {
|
|
332
|
+
void this.hubs.setName(name).then(() => this.update());
|
|
333
|
+
}
|
|
334
|
+
}), button('row danger', t('leave'), () => {
|
|
335
|
+
if (confirm(t('leaveAsk', { title: room.title }))) {
|
|
336
|
+
void this.hubs.leave(room.id).then(() => this.update());
|
|
337
|
+
}
|
|
338
|
+
}), button('row', t('close'), () => this.closeSheet()));
|
|
339
|
+
const sheet = div('sheet', panel);
|
|
340
|
+
sheet.addEventListener('click', event => {
|
|
341
|
+
if (event.target === sheet) {
|
|
342
|
+
this.closeSheet();
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
(_a = this.sheet) === null || _a === void 0 ? void 0 : _a.replaceWith(sheet);
|
|
346
|
+
if (!this.sheet) {
|
|
347
|
+
document.body.append(sheet);
|
|
348
|
+
}
|
|
349
|
+
this.sheet = sheet;
|
|
350
|
+
}
|
|
351
|
+
closeSheet() {
|
|
352
|
+
var _a;
|
|
353
|
+
(_a = this.sheet) === null || _a === void 0 ? void 0 : _a.remove();
|
|
354
|
+
this.sheet = undefined;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
function main() {
|
|
358
|
+
const style = el('style', guest_style_1.GUEST_STYLE);
|
|
359
|
+
document.head.append(style);
|
|
360
|
+
let link;
|
|
361
|
+
try {
|
|
362
|
+
link = relay_hub_util_1.RelayHubUtil.parse(location.href);
|
|
363
|
+
}
|
|
364
|
+
catch (_a) {
|
|
365
|
+
const logo = Object.assign(el('span', 'Ta'), { className: 'logo' });
|
|
366
|
+
document.body.replaceChildren(div('app', div('top', logo), div('center', Object.assign(el('p', t('invalid')), { className: 'hint' }))));
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
new GuestPage(link).start();
|
|
370
|
+
}
|
|
371
|
+
addEventListener('hashchange', () => location.reload());
|
|
372
|
+
main();
|
|
373
|
+
//# sourceMappingURL=guest.js.map
|
package/lib/node/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from '../common';
|
|
2
2
|
export * from './relay-files-host';
|
|
3
3
|
export * from './relay-host';
|
|
4
|
+
export * from './relay-hub-files';
|
|
4
5
|
export * from './relay-server';
|
|
5
6
|
export * from './relay-store';
|
|
7
|
+
export * from './relay-viewer';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/node/index.js
CHANGED
|
@@ -17,6 +17,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("../common"), exports);
|
|
18
18
|
__exportStar(require("./relay-files-host"), exports);
|
|
19
19
|
__exportStar(require("./relay-host"), exports);
|
|
20
|
+
__exportStar(require("./relay-hub-files"), exports);
|
|
20
21
|
__exportStar(require("./relay-server"), exports);
|
|
21
22
|
__exportStar(require("./relay-store"), exports);
|
|
23
|
+
__exportStar(require("./relay-viewer"), exports);
|
|
22
24
|
//# sourceMappingURL=index.js.map
|
package/lib/node/relay-host.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { RelayCipher } from '../common/relay-cipher';
|
|
|
2
2
|
import { type RelayFilesFrame } from '../common/relay-files';
|
|
3
3
|
import { RELAY_PING, type RelayHostMessage, type RelayWelcome } from '../common/relay-protocol';
|
|
4
4
|
import { RelayFilesHost } from './relay-files-host';
|
|
5
|
-
import type { RelayShare, RelayStore } from './relay-store';
|
|
5
|
+
import type { RelayContentShare, RelayShare, RelayStore } from './relay-store';
|
|
6
6
|
export interface RelayHostOptions {
|
|
7
7
|
store: RelayStore;
|
|
8
8
|
/** The WebSocket of this ta serve, with its token: every device gets a connection to it of its own. */
|
|
@@ -13,6 +13,8 @@ export interface RelayHostOptions {
|
|
|
13
13
|
pingMs?: number;
|
|
14
14
|
/** The bytes of a package handed on (a share of the store), for one who has its link; without it, shares are refused. */
|
|
15
15
|
sharePackage?(share: RelayShare): Promise<Uint8Array>;
|
|
16
|
+
/** Hands on the content shared in the store (RelayStore.contents) to whoever has its link; without it, such links are refused. */
|
|
17
|
+
shareContent?: boolean;
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
18
20
|
* A device on a channel: the handshake, then its connection to ta serve — or, for a channel that asked
|
|
@@ -25,10 +27,17 @@ declare class RelayChannel {
|
|
|
25
27
|
protected readonly options: RelayHostOptions;
|
|
26
28
|
protected cipher?: RelayCipher;
|
|
27
29
|
protected local?: WebSocket;
|
|
28
|
-
/** What this channel does, once its first sealed message said it. */
|
|
29
|
-
protected mode?: 'rpc' | 'files';
|
|
30
|
+
/** What this channel does, once its first sealed message said it; `share` from the handshake, for a link of a package. */
|
|
31
|
+
protected mode?: 'rpc' | 'files' | 'share' | 'content';
|
|
32
|
+
/** Content shared, until the viewer proved it has its key (RelayContentUtil). */
|
|
33
|
+
protected content?: RelayContentShare;
|
|
34
|
+
/** Content shared with Ta friends: a cipher with the key of each, till the proof of one opens. */
|
|
35
|
+
protected candidates?: RelayCipher[];
|
|
30
36
|
protected files?: RelayFilesHost;
|
|
31
37
|
protected readonly early: string[];
|
|
38
|
+
/** Agreed in the hello of a viewer of content, set once it proved. */
|
|
39
|
+
protected compress: boolean;
|
|
40
|
+
protected proofTimer?: ReturnType<typeof setTimeout>;
|
|
32
41
|
protected readonly timer: NodeJS.Timeout;
|
|
33
42
|
constructor(id: string, host: RelayHost, options: RelayHostOptions);
|
|
34
43
|
receive(text: string): Promise<void>;
|
|
@@ -37,6 +46,10 @@ declare class RelayChannel {
|
|
|
37
46
|
/** A frame of the files of the workspace, sealed as the rest. */
|
|
38
47
|
protected sendFrame(frame: RelayFilesFrame): void;
|
|
39
48
|
protected hello(text: string): Promise<void>;
|
|
49
|
+
/** The first sealed message of a viewer of content: its proof, then the content; a wrong one is counted, and refused. */
|
|
50
|
+
protected proven(cipher: RelayCipher, content: RelayContentShare, data: string): Promise<void>;
|
|
51
|
+
/** The key did not open: told in the clear (the viewer cannot open what is sealed), and counted. */
|
|
52
|
+
protected wrongKey(content: RelayContentShare): void;
|
|
40
53
|
/** A package handed on: what it is, its bytes in chunks, the end; then the channel closes. It never reaches ta serve's socket. */
|
|
41
54
|
protected sendShare(cipher: RelayCipher, share: RelayShare): Promise<void>;
|
|
42
55
|
protected connectLocal(cipher: RelayCipher): void;
|
|
@@ -58,7 +71,16 @@ export declare class RelayHost {
|
|
|
58
71
|
protected timer?: ReturnType<typeof setTimeout>;
|
|
59
72
|
protected pingTimer?: ReturnType<typeof setInterval>;
|
|
60
73
|
protected readonly channels: Map<string, RelayChannel>;
|
|
74
|
+
/** Wrong keys of each share of content, of late. */
|
|
75
|
+
protected readonly wrong: Map<string, {
|
|
76
|
+
count: number;
|
|
77
|
+
since: number;
|
|
78
|
+
}>;
|
|
61
79
|
constructor(options: RelayHostOptions);
|
|
80
|
+
/** @internal Whether a share of content had too many wrong keys of late. */
|
|
81
|
+
blocked(contentId: string, now?: number): boolean;
|
|
82
|
+
/** @internal */
|
|
83
|
+
wrongKey(contentId: string, now?: number): void;
|
|
62
84
|
/** Whether the connection to the relay is open. */
|
|
63
85
|
get online(): boolean;
|
|
64
86
|
/** Connects when relay.json has a relay and a device, and keeps connected; checks again on {@link refresh}. */
|
package/lib/node/relay-host.js
CHANGED
|
@@ -7,6 +7,9 @@ const relay_files_1 = require("../common/relay-files");
|
|
|
7
7
|
const relay_pairing_1 = require("../common/relay-pairing");
|
|
8
8
|
const relay_protocol_1 = require("../common/relay-protocol");
|
|
9
9
|
const relay_files_host_1 = require("./relay-files-host");
|
|
10
|
+
/** Wrong keys (passwords) for a share before it is refused a while, and for how long. */
|
|
11
|
+
const MAX_WRONG_KEYS = 10;
|
|
12
|
+
const WRONG_KEY_WINDOW_MS = 10 * 60 * 1000;
|
|
10
13
|
const MAX_RECONNECT_DELAY_MS = 30000;
|
|
11
14
|
const HELLO_TIMEOUT_MS = 10000;
|
|
12
15
|
/**
|
|
@@ -20,6 +23,8 @@ class RelayChannel {
|
|
|
20
23
|
this.host = host;
|
|
21
24
|
this.options = options;
|
|
22
25
|
this.early = [];
|
|
26
|
+
/** Agreed in the hello of a viewer of content, set once it proved. */
|
|
27
|
+
this.compress = false;
|
|
23
28
|
this.timer = setTimeout(() => this.close(), HELLO_TIMEOUT_MS);
|
|
24
29
|
}
|
|
25
30
|
async receive(text) {
|
|
@@ -28,14 +33,42 @@ class RelayChannel {
|
|
|
28
33
|
await this.hello(text);
|
|
29
34
|
return;
|
|
30
35
|
}
|
|
36
|
+
if (this.content && this.candidates) {
|
|
37
|
+
// Whose key it is, the proof says: the relay and the others do not learn who looks.
|
|
38
|
+
const content = this.content;
|
|
39
|
+
for (const candidate of this.candidates) {
|
|
40
|
+
const opened = await candidate.open(text).catch(() => undefined);
|
|
41
|
+
if (opened !== undefined) {
|
|
42
|
+
this.cipher = candidate;
|
|
43
|
+
this.candidates = undefined;
|
|
44
|
+
await this.proven(candidate, content, opened);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
this.candidates = undefined;
|
|
49
|
+
this.wrongKey(content);
|
|
50
|
+
this.close();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
31
53
|
let data;
|
|
32
54
|
try {
|
|
33
55
|
data = await this.cipher.open(text);
|
|
34
56
|
}
|
|
35
57
|
catch (_c) {
|
|
58
|
+
if (this.content) {
|
|
59
|
+
this.wrongKey(this.content);
|
|
60
|
+
}
|
|
36
61
|
this.close();
|
|
37
62
|
return;
|
|
38
63
|
}
|
|
64
|
+
if (this.content) {
|
|
65
|
+
await this.proven(this.cipher, this.content, data);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (this.mode === 'share' || this.mode === 'content') {
|
|
69
|
+
// A link hands on what it shares and nothing else: what its holder sends never opens ta serve's socket or files.
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
39
72
|
if (!this.mode) {
|
|
40
73
|
this.mode = relay_files_1.RelayFilesUtil.isHello(data) ? 'files' : 'rpc';
|
|
41
74
|
if (this.mode === 'files') {
|
|
@@ -60,6 +93,7 @@ class RelayChannel {
|
|
|
60
93
|
closed() {
|
|
61
94
|
var _a, _b;
|
|
62
95
|
clearTimeout(this.timer);
|
|
96
|
+
clearTimeout(this.proofTimer);
|
|
63
97
|
(_a = this.files) === null || _a === void 0 ? void 0 : _a.close();
|
|
64
98
|
(_b = this.local) === null || _b === void 0 ? void 0 : _b.close();
|
|
65
99
|
}
|
|
@@ -69,19 +103,26 @@ class RelayChannel {
|
|
|
69
103
|
(_a = this.cipher) === null || _a === void 0 ? void 0 : _a.seal(JSON.stringify(frame)).then(sealed => this.host.send({ t: 'data', ch: this.id, d: sealed }));
|
|
70
104
|
}
|
|
71
105
|
async hello(text) {
|
|
72
|
-
var _a;
|
|
106
|
+
var _a, _b, _c;
|
|
73
107
|
let hello;
|
|
74
108
|
try {
|
|
75
109
|
hello = JSON.parse(text);
|
|
76
110
|
}
|
|
77
|
-
catch (
|
|
111
|
+
catch (_d) {
|
|
78
112
|
this.close();
|
|
79
113
|
return;
|
|
80
114
|
}
|
|
81
115
|
const valid = hello.t === 'hello' && hello.v === 1 && typeof hello.nonce === 'string';
|
|
82
116
|
const device = valid ? this.options.store.device(hello.device) : undefined;
|
|
83
117
|
const share = valid && !device && this.options.sharePackage ? this.options.store.share(hello.device) : undefined;
|
|
84
|
-
const
|
|
118
|
+
const content = valid && !device && !share && this.options.shareContent ? this.options.store.content(hello.device) : undefined;
|
|
119
|
+
if (content && this.host.blocked(content.id)) {
|
|
120
|
+
this.answer({ t: 'refused', reason: 'busy' });
|
|
121
|
+
this.close();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const people = (content === null || content === void 0 ? void 0 : content.access) === 'people' ? ((_a = content.people) !== null && _a !== void 0 ? _a : []).map(person => person.key) : undefined;
|
|
125
|
+
const key = (_c = (_b = device === null || device === void 0 ? void 0 : device.key) !== null && _b !== void 0 ? _b : share === null || share === void 0 ? void 0 : share.key) !== null && _c !== void 0 ? _c : ((content === null || content === void 0 ? void 0 : content.access) === 'self' ? undefined : people ? people[0] : content === null || content === void 0 ? void 0 : content.key);
|
|
85
126
|
if (!key) {
|
|
86
127
|
this.answer({ t: 'refused', reason: 'This device is not paired.' });
|
|
87
128
|
this.close();
|
|
@@ -91,7 +132,25 @@ class RelayChannel {
|
|
|
91
132
|
const nonce = relay_bytes_1.RelayBytes.random(16);
|
|
92
133
|
const cipher = await relay_cipher_1.RelayCipher.derive(relay_bytes_1.RelayBytes.fromBase64(key), relay_bytes_1.RelayBytes.fromBase64(hello.nonce), nonce, 'host');
|
|
93
134
|
this.cipher = cipher;
|
|
135
|
+
if (people) {
|
|
136
|
+
this.candidates = [
|
|
137
|
+
cipher,
|
|
138
|
+
...(await Promise.all(people.slice(1).map(other => relay_cipher_1.RelayCipher.derive(relay_bytes_1.RelayBytes.fromBase64(other), relay_bytes_1.RelayBytes.fromBase64(hello.nonce), nonce, 'host')))),
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
if (share) {
|
|
142
|
+
this.mode = 'share';
|
|
143
|
+
}
|
|
94
144
|
const compress = hello.z === 1 && relay_cipher_1.RelayDeflate.supported();
|
|
145
|
+
if (content) {
|
|
146
|
+
// The viewer proves first: what this side seals would let one with the link but not the password try passwords offline.
|
|
147
|
+
this.content = content;
|
|
148
|
+
this.compress = compress;
|
|
149
|
+
// A viewer that never proves would hold one of the places of the devices.
|
|
150
|
+
this.proofTimer = setTimeout(() => this.close(), HELLO_TIMEOUT_MS);
|
|
151
|
+
this.answer({ t: 'ready', nonce: relay_bytes_1.RelayBytes.toBase64(nonce), ...(compress ? { z: 1 } : {}) });
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
95
154
|
const proof = await cipher.seal(relay_protocol_1.RELAY_PROOF);
|
|
96
155
|
// The messages after the proof, both ways: the device sets it once it opened the proof.
|
|
97
156
|
cipher.compress = compress;
|
|
@@ -100,6 +159,44 @@ class RelayChannel {
|
|
|
100
159
|
await this.sendShare(cipher, share);
|
|
101
160
|
}
|
|
102
161
|
}
|
|
162
|
+
/** The first sealed message of a viewer of content: its proof, then the content; a wrong one is counted, and refused. */
|
|
163
|
+
async proven(cipher, content, data) {
|
|
164
|
+
this.content = undefined;
|
|
165
|
+
clearTimeout(this.proofTimer);
|
|
166
|
+
if (data !== relay_protocol_1.RELAY_PROOF) {
|
|
167
|
+
this.wrongKey(content);
|
|
168
|
+
this.close();
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
cipher.compress = this.compress;
|
|
172
|
+
this.mode = 'content';
|
|
173
|
+
const send = async (message) => this.host.send({ t: 'data', ch: this.id, d: await cipher.seal(JSON.stringify(message)) });
|
|
174
|
+
const bytes = this.options.store.content(content.id) ? this.options.store.contentData(content) : undefined;
|
|
175
|
+
if (!bytes) {
|
|
176
|
+
await send({ t: 'error', message: 'This link no longer works: the share was stopped or ran out.' });
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
await send({
|
|
180
|
+
t: 'content',
|
|
181
|
+
kind: content.kind,
|
|
182
|
+
title: content.title,
|
|
183
|
+
size: bytes.length,
|
|
184
|
+
createdAt: content.createdAt,
|
|
185
|
+
...(content.type ? { type: content.type } : {}),
|
|
186
|
+
});
|
|
187
|
+
for (let at = 0; at < bytes.length; at += relay_protocol_1.RELAY_SHARE_CHUNK_BYTES) {
|
|
188
|
+
await send({ t: 'chunk', d: relay_bytes_1.RelayBytes.toBase64(bytes.subarray(at, at + relay_protocol_1.RELAY_SHARE_CHUNK_BYTES)) });
|
|
189
|
+
}
|
|
190
|
+
await send({ t: 'end' });
|
|
191
|
+
}
|
|
192
|
+
this.close();
|
|
193
|
+
}
|
|
194
|
+
/** The key did not open: told in the clear (the viewer cannot open what is sealed), and counted. */
|
|
195
|
+
wrongKey(content) {
|
|
196
|
+
this.content = undefined;
|
|
197
|
+
this.answer({ t: 'refused', reason: 'key' });
|
|
198
|
+
this.host.wrongKey(content.id);
|
|
199
|
+
}
|
|
103
200
|
/** A package handed on: what it is, its bytes in chunks, the end; then the channel closes. It never reaches ta serve's socket. */
|
|
104
201
|
async sendShare(cipher, share) {
|
|
105
202
|
const send = async (message) => this.host.send({ t: 'data', ch: this.id, d: await cipher.seal(JSON.stringify(message)) });
|
|
@@ -137,6 +234,7 @@ class RelayChannel {
|
|
|
137
234
|
}
|
|
138
235
|
close() {
|
|
139
236
|
clearTimeout(this.timer);
|
|
237
|
+
clearTimeout(this.proofTimer);
|
|
140
238
|
this.host.closeChannel(this.id);
|
|
141
239
|
}
|
|
142
240
|
}
|
|
@@ -151,6 +249,18 @@ class RelayHost {
|
|
|
151
249
|
this.stopped = false;
|
|
152
250
|
this.attempts = 0;
|
|
153
251
|
this.channels = new Map();
|
|
252
|
+
/** Wrong keys of each share of content, of late. */
|
|
253
|
+
this.wrong = new Map();
|
|
254
|
+
}
|
|
255
|
+
/** @internal Whether a share of content had too many wrong keys of late. */
|
|
256
|
+
blocked(contentId, now = Date.now()) {
|
|
257
|
+
const wrong = this.wrong.get(contentId);
|
|
258
|
+
return !!wrong && now - wrong.since < WRONG_KEY_WINDOW_MS && wrong.count >= MAX_WRONG_KEYS;
|
|
259
|
+
}
|
|
260
|
+
/** @internal */
|
|
261
|
+
wrongKey(contentId, now = Date.now()) {
|
|
262
|
+
const wrong = this.wrong.get(contentId);
|
|
263
|
+
this.wrong.set(contentId, wrong && now - wrong.since < WRONG_KEY_WINDOW_MS ? { count: wrong.count + 1, since: wrong.since } : { count: 1, since: now });
|
|
154
264
|
}
|
|
155
265
|
/** Whether the connection to the relay is open. */
|
|
156
266
|
get online() {
|
|
@@ -165,7 +275,8 @@ class RelayHost {
|
|
|
165
275
|
/** relay.json may have changed: connect if it now has devices, or leave if it has none. */
|
|
166
276
|
refresh() {
|
|
167
277
|
const { url, host, devices } = this.options.store.read();
|
|
168
|
-
const shared = !!this.options.sharePackage && this.options.store.shares().length > 0
|
|
278
|
+
const shared = (!!this.options.sharePackage && this.options.store.shares().length > 0) ||
|
|
279
|
+
(!!this.options.shareContent && this.options.store.contents().some(content => content.access !== 'self'));
|
|
169
280
|
const wanted = !this.stopped && url !== undefined && host !== undefined && (devices.length > 0 || shared);
|
|
170
281
|
if (!wanted) {
|
|
171
282
|
this.disconnect();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RelayHubsStore, RelayHubsStoreName } from '../common/relay-hubs';
|
|
2
|
+
/** What the rooms of a Ta keep, as files of its agent directory readable by the user alone: `identity.json`, `rooms.json`, `friends.json`. */
|
|
3
|
+
export declare class RelayHubFiles implements RelayHubsStore {
|
|
4
|
+
readonly agentDir: string;
|
|
5
|
+
constructor(agentDir: string);
|
|
6
|
+
file(name: RelayHubsStoreName): string;
|
|
7
|
+
read<T>(name: RelayHubsStoreName): T | undefined;
|
|
8
|
+
/** Written whole, then renamed over the file, readable by the user alone: it holds keys. */
|
|
9
|
+
write(name: RelayHubsStoreName, value: unknown): void;
|
|
10
|
+
/** The name of a new identity: the user's on this system. */
|
|
11
|
+
static userName(): string;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=relay-hub-files.d.ts.map
|