@celljs/ta-relay 4.9.1 → 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 +29 -2
- package/README.md +29 -2
- package/esm/common/index.d.ts +10 -0
- package/esm/common/index.js +10 -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-cipher.d.ts +15 -0
- package/esm/common/relay-cipher.js +62 -2
- 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-files.d.ts +108 -0
- package/esm/common/relay-files.js +170 -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 +217 -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 +33 -4
- 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 +3 -0
- package/esm/node/index.js +3 -0
- package/esm/node/relay-files-host.d.ts +27 -0
- package/esm/node/relay-files-host.js +130 -0
- package/esm/node/relay-host.d.ts +35 -2
- package/esm/node/relay-host.js +150 -13
- 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 +10 -0
- package/lib/common/index.js +10 -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-cipher.d.ts +15 -0
- package/lib/common/relay-cipher.js +63 -3
- 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-files.d.ts +109 -0
- package/lib/common/relay-files.js +174 -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 +217 -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 +32 -3
- 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 +3 -0
- package/lib/node/index.js +3 -0
- package/lib/node/relay-files-host.d.ts +28 -0
- package/lib/node/relay-files-host.js +134 -0
- package/lib/node/relay-host.d.ts +35 -2
- package/lib/node/relay-host.js +149 -12
- 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,5 +1,8 @@
|
|
|
1
1
|
export * from '../common';
|
|
2
|
+
export * from './relay-files-host';
|
|
2
3
|
export * from './relay-host';
|
|
4
|
+
export * from './relay-hub-files';
|
|
3
5
|
export * from './relay-server';
|
|
4
6
|
export * from './relay-store';
|
|
7
|
+
export * from './relay-viewer';
|
|
5
8
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/node/index.js
CHANGED
|
@@ -15,7 +15,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("../common"), exports);
|
|
18
|
+
__exportStar(require("./relay-files-host"), exports);
|
|
18
19
|
__exportStar(require("./relay-host"), exports);
|
|
20
|
+
__exportStar(require("./relay-hub-files"), exports);
|
|
19
21
|
__exportStar(require("./relay-server"), exports);
|
|
20
22
|
__exportStar(require("./relay-store"), exports);
|
|
23
|
+
__exportStar(require("./relay-viewer"), exports);
|
|
21
24
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type RelayFilesFrame } from '../common/relay-files';
|
|
2
|
+
export interface RelayFilesHostOptions {
|
|
3
|
+
/** The address of ta serve, always at loopback: the one of its WebSocket (RelayHostOptions.localUrl). */
|
|
4
|
+
localUrl: string;
|
|
5
|
+
/** Sends a frame of the sealed channel of this device. */
|
|
6
|
+
send(frame: RelayFilesFrame): void;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The files of the workspace on a channel of a device (RelayFiles): asks them of the ta serve of this
|
|
10
|
+
* machine and sends them back sealed. It asks for nothing but a path of {@link RELAY_FILES_PREFIX}, with
|
|
11
|
+
* GET, at the loopback port of ta serve and never at another host: a device has the whole of ta serve
|
|
12
|
+
* already, and what a device cannot be trusted with is not this.
|
|
13
|
+
*/
|
|
14
|
+
export declare class RelayFilesHost {
|
|
15
|
+
protected readonly options: RelayFilesHostOptions;
|
|
16
|
+
protected readonly base: string;
|
|
17
|
+
protected reading: number;
|
|
18
|
+
protected readonly running: Set<AbortController>;
|
|
19
|
+
constructor(options: RelayFilesHostOptions);
|
|
20
|
+
/** A frame of the device: a file to read, or something of a version it does not know. */
|
|
21
|
+
receive(text: string): Promise<void>;
|
|
22
|
+
/** The device went: what it asked for is not read to the end. */
|
|
23
|
+
close(): void;
|
|
24
|
+
/** What to fetch for what a device asked, or undefined: a path of {@link RELAY_FILES_PREFIX} of this ta serve alone. */
|
|
25
|
+
static target(base: string, url: string): string | undefined;
|
|
26
|
+
protected read(id: number, url: string): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=relay-files-host.d.ts.map
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayFilesHost = void 0;
|
|
4
|
+
const relay_bytes_1 = require("../common/relay-bytes");
|
|
5
|
+
const relay_files_1 = require("../common/relay-files");
|
|
6
|
+
/**
|
|
7
|
+
* The files of the workspace on a channel of a device (RelayFiles): asks them of the ta serve of this
|
|
8
|
+
* machine and sends them back sealed. It asks for nothing but a path of {@link RELAY_FILES_PREFIX}, with
|
|
9
|
+
* GET, at the loopback port of ta serve and never at another host: a device has the whole of ta serve
|
|
10
|
+
* already, and what a device cannot be trusted with is not this.
|
|
11
|
+
*/
|
|
12
|
+
class RelayFilesHost {
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.reading = 0;
|
|
16
|
+
this.running = new Set();
|
|
17
|
+
// Only ever loopback, whatever the address of the socket says: what this reaches is one server.
|
|
18
|
+
this.base = `http://127.0.0.1:${new URL(options.localUrl).port}`;
|
|
19
|
+
}
|
|
20
|
+
/** A frame of the device: a file to read, or something of a version it does not know. */
|
|
21
|
+
async receive(text) {
|
|
22
|
+
let frame;
|
|
23
|
+
try {
|
|
24
|
+
frame = JSON.parse(text);
|
|
25
|
+
}
|
|
26
|
+
catch (_a) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (!frame || typeof frame !== 'object' || frame.t !== 'get' || typeof frame.url !== 'string' || typeof frame.id !== 'number') {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
await this.read(frame.id, frame.url);
|
|
33
|
+
}
|
|
34
|
+
/** The device went: what it asked for is not read to the end. */
|
|
35
|
+
close() {
|
|
36
|
+
for (const controller of this.running) {
|
|
37
|
+
controller.abort();
|
|
38
|
+
}
|
|
39
|
+
this.running.clear();
|
|
40
|
+
}
|
|
41
|
+
/** What to fetch for what a device asked, or undefined: a path of {@link RELAY_FILES_PREFIX} of this ta serve alone. */
|
|
42
|
+
static target(base, url) {
|
|
43
|
+
let parsed;
|
|
44
|
+
try {
|
|
45
|
+
// Parsed, never taken as text: a device that sends another host or a path out of the prefix is refused.
|
|
46
|
+
parsed = new URL(url, base);
|
|
47
|
+
}
|
|
48
|
+
catch (_a) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
if (parsed.origin !== base || !parsed.pathname.startsWith(relay_files_1.RELAY_FILES_PREFIX)) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
return base + parsed.pathname + parsed.search;
|
|
55
|
+
}
|
|
56
|
+
async read(id, url) {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
const target = RelayFilesHost.target(this.base, url);
|
|
59
|
+
if (!target) {
|
|
60
|
+
this.options.send({ t: 'error', id, message: 'Ta reads only the files it offers by URL.' });
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (this.reading >= relay_files_1.RELAY_FILES_MAX_PARALLEL) {
|
|
64
|
+
this.options.send({ t: 'error', id, message: 'Too many files are loading at once.' });
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const controller = new AbortController();
|
|
68
|
+
this.running.add(controller);
|
|
69
|
+
this.reading++;
|
|
70
|
+
const timer = setTimeout(() => controller.abort(), relay_files_1.RELAY_FILES_TIMEOUT_MS);
|
|
71
|
+
try {
|
|
72
|
+
const response = await fetch(target, { signal: controller.signal, redirect: 'error', headers: { accept: '*/*' } });
|
|
73
|
+
if (!response.ok) {
|
|
74
|
+
this.options.send({ t: 'error', id, message: `The file is not there (${response.status}).` });
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const length = Number((_a = response.headers.get('content-length')) !== null && _a !== void 0 ? _a : 0);
|
|
78
|
+
if (length > relay_files_1.RELAY_FILE_MAX_BYTES) {
|
|
79
|
+
this.options.send({ t: 'error', id, message: 'The file is too large to show here.' });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this.options.send({
|
|
83
|
+
t: 'head',
|
|
84
|
+
id,
|
|
85
|
+
status: response.status,
|
|
86
|
+
type: (_b = response.headers.get('content-type')) !== null && _b !== void 0 ? _b : 'application/octet-stream',
|
|
87
|
+
size: length || undefined,
|
|
88
|
+
});
|
|
89
|
+
if (!response.body) {
|
|
90
|
+
this.options.send({ t: 'end', id });
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
let sent = 0;
|
|
94
|
+
let parts = [];
|
|
95
|
+
let held = 0;
|
|
96
|
+
const flush = () => {
|
|
97
|
+
if (held > 0) {
|
|
98
|
+
this.options.send({ t: 'chunk', id, d: relay_bytes_1.RelayBytes.toBase64(relay_bytes_1.RelayBytes.concat(...parts)) });
|
|
99
|
+
parts = [];
|
|
100
|
+
held = 0;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
for await (const piece of response.body) {
|
|
104
|
+
const chunk = piece;
|
|
105
|
+
sent += chunk.length;
|
|
106
|
+
if (sent > relay_files_1.RELAY_FILE_MAX_BYTES) {
|
|
107
|
+
this.options.send({ t: 'error', id, message: 'The file is too large to show here.' });
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
parts.push(chunk);
|
|
111
|
+
held += chunk.length;
|
|
112
|
+
if (held >= relay_files_1.RELAY_FILE_CHUNK_BYTES) {
|
|
113
|
+
flush();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
flush();
|
|
117
|
+
this.options.send({ t: 'end', id });
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
this.options.send({
|
|
121
|
+
t: 'error',
|
|
122
|
+
id,
|
|
123
|
+
message: controller.signal.aborted ? 'The file took too long to come.' : `The file could not be read: ${error.message}`,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
clearTimeout(timer);
|
|
128
|
+
this.running.delete(controller);
|
|
129
|
+
this.reading--;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
exports.RelayFilesHost = RelayFilesHost;
|
|
134
|
+
//# sourceMappingURL=relay-files-host.js.map
|
package/lib/node/relay-host.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { RelayCipher } from '../common/relay-cipher';
|
|
2
|
+
import { type RelayFilesFrame } from '../common/relay-files';
|
|
2
3
|
import { RELAY_PING, type RelayHostMessage, type RelayWelcome } from '../common/relay-protocol';
|
|
3
|
-
import
|
|
4
|
+
import { RelayFilesHost } from './relay-files-host';
|
|
5
|
+
import type { RelayContentShare, RelayShare, RelayStore } from './relay-store';
|
|
4
6
|
export interface RelayHostOptions {
|
|
5
7
|
store: RelayStore;
|
|
6
8
|
/** The WebSocket of this ta serve, with its token: every device gets a connection to it of its own. */
|
|
@@ -11,21 +13,43 @@ export interface RelayHostOptions {
|
|
|
11
13
|
pingMs?: number;
|
|
12
14
|
/** The bytes of a package handed on (a share of the store), for one who has its link; without it, shares are refused. */
|
|
13
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;
|
|
14
18
|
}
|
|
15
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* A device on a channel: the handshake, then its connection to ta serve — or, for a channel that asked
|
|
21
|
+
* for files (RelayFiles, RelayFilesHost), the file it asks for. What a channel is, it says in its first
|
|
22
|
+
* sealed message; until then nothing of ta serve is opened for it.
|
|
23
|
+
*/
|
|
16
24
|
declare class RelayChannel {
|
|
17
25
|
readonly id: string;
|
|
18
26
|
protected readonly host: RelayHost;
|
|
19
27
|
protected readonly options: RelayHostOptions;
|
|
20
28
|
protected cipher?: RelayCipher;
|
|
21
29
|
protected local?: WebSocket;
|
|
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[];
|
|
36
|
+
protected files?: RelayFilesHost;
|
|
22
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>;
|
|
23
41
|
protected readonly timer: NodeJS.Timeout;
|
|
24
42
|
constructor(id: string, host: RelayHost, options: RelayHostOptions);
|
|
25
43
|
receive(text: string): Promise<void>;
|
|
26
44
|
/** From the relay: the device is gone. */
|
|
27
45
|
closed(): void;
|
|
46
|
+
/** A frame of the files of the workspace, sealed as the rest. */
|
|
47
|
+
protected sendFrame(frame: RelayFilesFrame): void;
|
|
28
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;
|
|
29
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. */
|
|
30
54
|
protected sendShare(cipher: RelayCipher, share: RelayShare): Promise<void>;
|
|
31
55
|
protected connectLocal(cipher: RelayCipher): void;
|
|
@@ -47,7 +71,16 @@ export declare class RelayHost {
|
|
|
47
71
|
protected timer?: ReturnType<typeof setTimeout>;
|
|
48
72
|
protected pingTimer?: ReturnType<typeof setInterval>;
|
|
49
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
|
+
}>;
|
|
50
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;
|
|
51
84
|
/** Whether the connection to the relay is open. */
|
|
52
85
|
get online(): boolean;
|
|
53
86
|
/** Connects when relay.json has a relay and a device, and keeps connected; checks again on {@link refresh}. */
|