@celljs/ta-relay 4.20.0 → 4.21.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/esm/common/index.d.ts +1 -0
- package/esm/common/index.js +1 -0
- package/esm/common/relay-account-client.d.ts +4 -0
- package/esm/common/relay-account-client.js +13 -3
- package/esm/common/relay-account.d.ts +17 -2
- package/esm/common/relay-account.js +15 -6
- package/esm/common/relay-accounts.d.ts +2 -0
- package/esm/common/relay-accounts.js +16 -2
- package/esm/common/relay-group-invites.d.ts +6 -2
- package/esm/common/relay-group-invites.js +6 -5
- package/esm/common/relay-hub-charter.d.ts +74 -0
- package/esm/common/relay-hub-charter.js +149 -0
- package/esm/common/relay-hub-epochs.d.ts +43 -0
- package/esm/common/relay-hub-epochs.js +92 -0
- package/esm/common/relay-hub-util.d.ts +39 -7
- package/esm/common/relay-hub-util.js +46 -15
- package/esm/common/relay-hub.d.ts +13 -0
- package/esm/common/relay-hub.js +81 -18
- package/esm/common/relay-hubs-admins.d.ts +27 -0
- package/esm/common/relay-hubs-admins.js +91 -0
- package/esm/common/relay-hubs-connection.d.ts +19 -63
- package/esm/common/relay-hubs-connection.js +100 -87
- package/esm/common/relay-hubs-identity.d.ts +7 -1
- package/esm/common/relay-hubs-identity.js +28 -1
- package/esm/common/relay-hubs-live.d.ts +58 -0
- package/esm/common/relay-hubs-live.js +27 -0
- package/esm/common/relay-hubs-protocol.d.ts +10 -0
- package/esm/common/relay-hubs-util.d.ts +9 -0
- package/esm/common/relay-hubs-util.js +23 -1
- package/esm/common/relay-hubs.d.ts +12 -15
- package/esm/common/relay-hubs.js +60 -57
- package/esm/common/relay-protocol.d.ts +14 -3
- package/esm/common/relay-protocol.js +1 -1
- package/esm/common/relay-room-chat.d.ts +6 -1
- package/esm/common/relay-room-chat.js +1 -0
- package/esm/node/relay-server-accounts.d.ts +1 -1
- package/esm/node/relay-server-accounts.js +1 -1
- package/lib/common/index.d.ts +1 -0
- package/lib/common/index.js +1 -0
- package/lib/common/relay-account-client.d.ts +4 -0
- package/lib/common/relay-account-client.js +13 -3
- package/lib/common/relay-account.d.ts +17 -2
- package/lib/common/relay-account.js +15 -6
- package/lib/common/relay-accounts.d.ts +2 -0
- package/lib/common/relay-accounts.js +16 -2
- package/lib/common/relay-group-invites.d.ts +6 -2
- package/lib/common/relay-group-invites.js +6 -5
- package/lib/common/relay-hub-charter.d.ts +75 -0
- package/lib/common/relay-hub-charter.js +152 -0
- package/lib/common/relay-hub-epochs.d.ts +44 -0
- package/lib/common/relay-hub-epochs.js +95 -0
- package/lib/common/relay-hub-util.d.ts +39 -7
- package/lib/common/relay-hub-util.js +46 -15
- package/lib/common/relay-hub.d.ts +13 -0
- package/lib/common/relay-hub.js +81 -18
- package/lib/common/relay-hubs-admins.d.ts +28 -0
- package/lib/common/relay-hubs-admins.js +95 -0
- package/lib/common/relay-hubs-connection.d.ts +19 -63
- package/lib/common/relay-hubs-connection.js +106 -94
- package/lib/common/relay-hubs-identity.d.ts +7 -1
- package/lib/common/relay-hubs-identity.js +28 -1
- package/lib/common/relay-hubs-live.d.ts +59 -0
- package/lib/common/relay-hubs-live.js +31 -0
- package/lib/common/relay-hubs-protocol.d.ts +10 -0
- package/lib/common/relay-hubs-util.d.ts +9 -0
- package/lib/common/relay-hubs-util.js +23 -1
- package/lib/common/relay-hubs.d.ts +12 -15
- package/lib/common/relay-hubs.js +66 -63
- package/lib/common/relay-protocol.d.ts +14 -3
- package/lib/common/relay-protocol.js +1 -1
- package/lib/common/relay-room-chat.d.ts +6 -1
- package/lib/common/relay-room-chat.js +1 -0
- package/lib/node/relay-server-accounts.d.ts +1 -1
- package/lib/node/relay-server-accounts.js +1 -1
- package/package.json +3 -3
- package/viewer/room.html +15 -15
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RelayHubUtil = void 0;
|
|
4
4
|
const relay_bytes_1 = require("./relay-bytes");
|
|
5
|
+
const relay_hub_charter_1 = require("./relay-hub-charter");
|
|
5
6
|
const relay_identity_1 = require("./relay-identity");
|
|
6
7
|
const relay_protocol_1 = require("./relay-protocol");
|
|
7
8
|
/** Links, keys, sealing and signatures of rooms (docs/design/ta-link.md 3.2, 3.3). */
|
|
@@ -65,15 +66,45 @@ var RelayHubUtil;
|
|
|
65
66
|
return relay_bytes_1.RelayBytes.toBase64(relay_bytes_1.RelayBytes.random(32));
|
|
66
67
|
}
|
|
67
68
|
RelayHubUtil.randomKey = randomKey;
|
|
68
|
-
|
|
69
|
+
/** The keys of a room of key `roomKey` (epoch 0), and of the epochs after it this Ta has (RelayRoomRecord.epochs). */
|
|
70
|
+
async function keys(roomKey, epochs = {}) {
|
|
69
71
|
const key = relay_bytes_1.RelayBytes.fromBase64(roomKey);
|
|
70
72
|
const none = new Uint8Array(0);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
const sealOf = async (value) => relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayKeys.hkdf(value, none, 'ta-room/1 seal'));
|
|
74
|
+
const ring = new Map([[0, await sealOf(key)]]);
|
|
75
|
+
for (const [epoch, value] of Object.entries(epochs)) {
|
|
76
|
+
const n = Number(epoch);
|
|
77
|
+
if (Number.isInteger(n) && n > 0) {
|
|
78
|
+
ring.set(n, await sealOf(relay_bytes_1.RelayBytes.fromBase64(value)));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const epoch = Math.max(...ring.keys());
|
|
82
|
+
return { seal: ring.get(epoch), names: await relay_identity_1.RelayKeys.hkdf(key, none, 'ta-room/1 key'), epoch, ring };
|
|
75
83
|
}
|
|
76
84
|
RelayHubUtil.keys = keys;
|
|
85
|
+
/**
|
|
86
|
+
* Sealed with the key of the latest epoch: as {@link seal} in epoch 0, which a Ta of before reads; `<epoch>.<sealed>`
|
|
87
|
+
* after (base64url has no dot).
|
|
88
|
+
*/
|
|
89
|
+
async function sealIn(keys, identity, member, body, at = Date.now()) {
|
|
90
|
+
const sealed = await seal(keys.seal, identity, member, body, at);
|
|
91
|
+
return keys.epoch ? `${keys.epoch}.${sealed}` : sealed;
|
|
92
|
+
}
|
|
93
|
+
RelayHubUtil.sealIn = sealIn;
|
|
94
|
+
/** The key a text of {@link sealIn} was sealed with, and the text without its epoch; none for an epoch this Ta lacks. */
|
|
95
|
+
function keyFor(keys, sealed) {
|
|
96
|
+
const dot = sealed.indexOf('.');
|
|
97
|
+
const epoch = dot > 0 ? Number(sealed.slice(0, dot)) : 0;
|
|
98
|
+
const key = Number.isInteger(epoch) ? keys.ring.get(epoch) : undefined;
|
|
99
|
+
return key && { key, sealed: dot > 0 ? sealed.slice(dot + 1) : sealed, epoch };
|
|
100
|
+
}
|
|
101
|
+
RelayHubUtil.keyFor = keyFor;
|
|
102
|
+
/** What {@link sealIn} sealed, checked as {@link open}. */
|
|
103
|
+
async function openIn(keys, sealed, signOf, options = {}) {
|
|
104
|
+
const found = keyFor(keys, String(sealed));
|
|
105
|
+
return found ? open(found.key, found.sealed, signOf, options) : undefined;
|
|
106
|
+
}
|
|
107
|
+
RelayHubUtil.openIn = openIn;
|
|
77
108
|
/** What the relay knows a key of the state by: HMAC of its name, 22 characters. */
|
|
78
109
|
async function keyId(keys, name) {
|
|
79
110
|
return (await relay_identity_1.RelayKeys.hmac(keys.names, name)).slice(0, 22);
|
|
@@ -150,30 +181,30 @@ var RelayHubUtil;
|
|
|
150
181
|
}
|
|
151
182
|
RelayHubUtil.openAsk = openAsk;
|
|
152
183
|
/**
|
|
153
|
-
* The
|
|
154
|
-
* the owner; all of it sealed with the invitation the ask came with,
|
|
184
|
+
* The answer to an ask: what the room is and its key, for the one who asked alone (ECDH of the two), signed by the
|
|
185
|
+
* owner, or by an admin with `proof` that the owner named it; all of it sealed with the invitation the ask came with,
|
|
186
|
+
* so that the relay does not see who answered.
|
|
155
187
|
*/
|
|
156
|
-
async function sealAnswer(owner, room, ask, body, invite) {
|
|
188
|
+
async function sealAnswer(owner, room, ask, body, invite, proof) {
|
|
157
189
|
const key = await relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(owner, ask.box, room, 'ta-room/1 answer'));
|
|
158
190
|
const d = await relay_identity_1.RelayKeys.seal(key, JSON.stringify(body));
|
|
159
191
|
const signed = `${room}\n${ask.member}\n${d}`;
|
|
160
|
-
const answer = JSON.stringify({ owner: relay_identity_1.RelayIdentityUtil.publicOf(owner), d, s: await relay_identity_1.RelayIdentityUtil.sign(owner, signed) });
|
|
192
|
+
const answer = JSON.stringify({ owner: relay_identity_1.RelayIdentityUtil.publicOf(owner), d, s: await relay_identity_1.RelayIdentityUtil.sign(owner, signed), ...(proof !== null && proof !== void 0 ? proof : {}) });
|
|
161
193
|
return relay_identity_1.RelayKeys.seal(await inviteKey(invite), answer);
|
|
162
194
|
}
|
|
163
195
|
RelayHubUtil.sealAnswer = sealAnswer;
|
|
164
|
-
/** Undefined unless it is signed by the owner the link names, and opens for this identity. */
|
|
196
|
+
/** Undefined unless it is signed by the owner the link names (or an admin it named), and opens for this identity; `owner` is the owner. */
|
|
165
197
|
async function openAnswer(identity, room, member, ownerId, answer, invite) {
|
|
166
198
|
try {
|
|
167
|
-
const { owner, d, s } = JSON.parse(await relay_identity_1.RelayKeys.open(await inviteKey(invite), answer));
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
!(await relay_identity_1.RelayIdentityUtil.verify(owner.sign, `${room}\n${member}\n${d}`, s))) {
|
|
199
|
+
const { owner, d, s, head, charter } = JSON.parse(await relay_identity_1.RelayKeys.open(await inviteKey(invite), answer));
|
|
200
|
+
const named = await relay_hub_charter_1.RelayHubCharterUtil.answeredBy(owner, ownerId, room, { head, charter });
|
|
201
|
+
if (!named || !(await relay_identity_1.RelayIdentityUtil.verify(owner.sign, `${room}\n${member}\n${d}`, s))) {
|
|
171
202
|
return undefined;
|
|
172
203
|
}
|
|
173
204
|
const key = await relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(identity, owner.box, room, 'ta-room/1 answer'));
|
|
174
205
|
const body = JSON.parse(await relay_identity_1.RelayKeys.open(key, d));
|
|
175
206
|
relay_bytes_1.RelayBytes.fromBase64(body.key);
|
|
176
|
-
return { owner: { ...relay_identity_1.RelayIdentityUtil.publicOf(
|
|
207
|
+
return { owner: { ...relay_identity_1.RelayIdentityUtil.publicOf(named), name: String(named.name).slice(0, 100) }, body };
|
|
177
208
|
}
|
|
178
209
|
catch (_a) {
|
|
179
210
|
return undefined;
|
|
@@ -47,6 +47,13 @@ interface HubMeta {
|
|
|
47
47
|
hash: string;
|
|
48
48
|
expiresAt: number;
|
|
49
49
|
};
|
|
50
|
+
/** The members the owner made admins (docs/design/ta-link.md 8): they let in, take out and invite, as the owner. */
|
|
51
|
+
admins?: string[];
|
|
52
|
+
/** The invitation of each admin, by its member id: one of each, beside the owner's. */
|
|
53
|
+
invites?: Record<string, {
|
|
54
|
+
hash: string;
|
|
55
|
+
expiresAt: number;
|
|
56
|
+
}>;
|
|
50
57
|
keys: number;
|
|
51
58
|
size: number;
|
|
52
59
|
}
|
|
@@ -103,6 +110,12 @@ export declare class RelayHub {
|
|
|
103
110
|
t: 'admit';
|
|
104
111
|
}>): Promise<void>;
|
|
105
112
|
protected setRoster(socket: RelayHubSocket, roster: RelayHubRoster): Promise<void>;
|
|
113
|
+
/** The keys of a roster sealed for each member (RelayHubRoster.k): undefined for none, null for what is not that. */
|
|
114
|
+
protected static epochKeys(k: unknown): Record<string, string> | undefined | null;
|
|
115
|
+
/** Whether a member lets in, takes out and invites: the owner, or an admin it made. */
|
|
116
|
+
protected static manages(meta: HubMeta, member: string): boolean;
|
|
117
|
+
/** One gone is no admin, its invitation with it. */
|
|
118
|
+
protected unmake(meta: HubMeta, member: string): Promise<void>;
|
|
106
119
|
protected closeRoom(): Promise<void>;
|
|
107
120
|
/** The asks still waiting for the owner, those past their time dropped. */
|
|
108
121
|
protected asks(): Promise<HubAsks>;
|
package/lib/common/relay-hub.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RelayHub = exports.RelayHubCloseCode = void 0;
|
|
4
|
+
const relay_hub_charter_1 = require("./relay-hub-charter");
|
|
4
5
|
const relay_identity_1 = require("./relay-identity");
|
|
5
6
|
const relay_protocol_1 = require("./relay-protocol");
|
|
6
7
|
/** Close codes of a hub. */
|
|
@@ -129,8 +130,7 @@ class RelayHub {
|
|
|
129
130
|
const wasOnline = this.online(member, socket);
|
|
130
131
|
socket.who = { member };
|
|
131
132
|
await this.touch(meta);
|
|
132
|
-
const
|
|
133
|
-
const asks = owner ? await this.pendingAsks() : undefined;
|
|
133
|
+
const asks = RelayHub.manages(meta, member) ? await this.pendingAsks() : undefined;
|
|
134
134
|
const roster = await this.storage.get('roster');
|
|
135
135
|
const online = [...new Set(this.members().map(other => other.who.member))];
|
|
136
136
|
this.send(socket, { t: 'welcome', owner: meta.owner, online, ...(roster ? { roster } : {}), ...(asks ? { asks } : {}) });
|
|
@@ -154,7 +154,7 @@ class RelayHub {
|
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
async ask(socket, request) {
|
|
157
|
-
var _a;
|
|
157
|
+
var _a, _b, _c;
|
|
158
158
|
const meta = await this.storage.get('meta');
|
|
159
159
|
if (!meta) {
|
|
160
160
|
this.refuse(socket, 'gone', 'No such room.', RelayHubCloseCode.GONE);
|
|
@@ -165,12 +165,13 @@ class RelayHub {
|
|
|
165
165
|
return;
|
|
166
166
|
}
|
|
167
167
|
const now = this.now();
|
|
168
|
-
|
|
168
|
+
const proof = await relay_identity_1.RelayKeys.hash(String(request.proof));
|
|
169
|
+
if (![meta.invite, ...Object.values((_a = meta.invites) !== null && _a !== void 0 ? _a : {})].some(invite => invite && invite.expiresAt > now && invite.hash === proof)) {
|
|
169
170
|
this.refuse(socket, 'invite', 'This invitation no longer works.', RelayHubCloseCode.REFUSED);
|
|
170
171
|
return;
|
|
171
172
|
}
|
|
172
173
|
const asks = await this.asks();
|
|
173
|
-
const members = (
|
|
174
|
+
const members = (_b = (await this.storage.get('members'))) !== null && _b !== void 0 ? _b : {};
|
|
174
175
|
if (asks[request.ask] || members[request.ask]) {
|
|
175
176
|
this.refuse(socket, 'exists', 'Asked already.', RelayHubCloseCode.REFUSED);
|
|
176
177
|
return;
|
|
@@ -183,8 +184,10 @@ class RelayHub {
|
|
|
183
184
|
await this.storage.put('asks', asks);
|
|
184
185
|
socket.who = { ask: request.ask };
|
|
185
186
|
this.send(socket, { t: 'waiting' });
|
|
186
|
-
for (const
|
|
187
|
-
|
|
187
|
+
for (const manager of [meta.owner, ...((_c = meta.admins) !== null && _c !== void 0 ? _c : [])]) {
|
|
188
|
+
for (const socket of this.members(manager)) {
|
|
189
|
+
this.send(socket, { t: 'asked', ask: { ask: request.ask, d: request.d, at: now } });
|
|
190
|
+
}
|
|
188
191
|
}
|
|
189
192
|
}
|
|
190
193
|
async wait(socket, ask, token) {
|
|
@@ -219,9 +222,10 @@ class RelayHub {
|
|
|
219
222
|
socket.close(RelayHubCloseCode.ANSWERED, 'Answered.');
|
|
220
223
|
}
|
|
221
224
|
async fromMember(socket, member, request) {
|
|
222
|
-
var _a;
|
|
225
|
+
var _a, _b;
|
|
226
|
+
var _c, _d;
|
|
223
227
|
const meta = await this.storage.get('meta');
|
|
224
|
-
const members = (
|
|
228
|
+
const members = (_c = (await this.storage.get('members'))) !== null && _c !== void 0 ? _c : {};
|
|
225
229
|
if (!meta || !members[member]) {
|
|
226
230
|
this.refuse(socket, 'gone', 'Not in this room any more.', RelayHubCloseCode.REMOVED);
|
|
227
231
|
return;
|
|
@@ -238,25 +242,44 @@ class RelayHub {
|
|
|
238
242
|
}
|
|
239
243
|
delete members[member];
|
|
240
244
|
await this.storage.put('members', members);
|
|
245
|
+
await this.unmake(meta, member);
|
|
241
246
|
for (const other of this.members(member)) {
|
|
242
247
|
other.close(RelayHubCloseCode.REMOVED, 'Left the room.');
|
|
243
248
|
}
|
|
244
249
|
this.broadcast({ t: 'left', member });
|
|
245
250
|
return;
|
|
246
251
|
}
|
|
247
|
-
if (!owner) {
|
|
252
|
+
if (!RelayHub.manages(meta, member) || ((request.t === 'admins' || request.t === 'close') && !owner)) {
|
|
248
253
|
this.send(socket, { t: 'error', code: 'owner', message: 'Only the owner does that.' });
|
|
249
254
|
return;
|
|
250
255
|
}
|
|
251
256
|
switch (request.t) {
|
|
252
|
-
case '
|
|
257
|
+
case 'admins': {
|
|
258
|
+
const admins = Array.isArray(request.admins) ? request.admins : [];
|
|
259
|
+
meta.admins = [...new Set(admins.map(String))].filter(id => id !== meta.owner && !!members[id]).slice(0, relay_hub_charter_1.RELAY_HUB_MAX_ADMINS);
|
|
260
|
+
for (const id of Object.keys((_d = meta.invites) !== null && _d !== void 0 ? _d : {})) {
|
|
261
|
+
if (!meta.admins.includes(id)) {
|
|
262
|
+
(_a = meta.invites) === null || _a === void 0 ? true : delete _a[id];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
await this.storage.put('meta', meta);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
case 'invite': {
|
|
253
269
|
if (!/^[A-Za-z0-9_-]{43}$/.test(String(request.hash)) || !(request.expiresAt > this.now())) {
|
|
254
270
|
this.send(socket, { t: 'error', code: 'invalid', message: 'An invitation needs a hash and a time.' });
|
|
255
271
|
return;
|
|
256
272
|
}
|
|
257
|
-
|
|
273
|
+
const invite = { hash: request.hash, expiresAt: Math.min(request.expiresAt, this.now() + relay_protocol_1.RELAY_INVITE_MAX_DAYS * DAY_MS) };
|
|
274
|
+
if (owner) {
|
|
275
|
+
meta.invite = invite;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
meta.invites = { ...meta.invites, [member]: invite };
|
|
279
|
+
}
|
|
258
280
|
await this.storage.put('meta', meta);
|
|
259
281
|
return;
|
|
282
|
+
}
|
|
260
283
|
case 'admit':
|
|
261
284
|
return this.admit(socket, members, request);
|
|
262
285
|
case 'refuse': {
|
|
@@ -277,8 +300,13 @@ class RelayHub {
|
|
|
277
300
|
if (request.member === member || !members[request.member]) {
|
|
278
301
|
return;
|
|
279
302
|
}
|
|
303
|
+
if (request.member === meta.owner || (!owner && ((_b = meta.admins) === null || _b === void 0 ? void 0 : _b.includes(request.member)))) {
|
|
304
|
+
this.send(socket, { t: 'error', code: 'owner', message: 'Only the owner does that.' });
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
280
307
|
delete members[request.member];
|
|
281
308
|
await this.storage.put('members', members);
|
|
309
|
+
await this.unmake(meta, request.member);
|
|
282
310
|
for (const other of this.members(request.member)) {
|
|
283
311
|
this.refuse(other, 'auth', 'Removed by the owner.', RelayHubCloseCode.REMOVED);
|
|
284
312
|
}
|
|
@@ -301,7 +329,7 @@ class RelayHub {
|
|
|
301
329
|
}
|
|
302
330
|
}
|
|
303
331
|
async set(socket, member, meta, request) {
|
|
304
|
-
var _a, _b;
|
|
332
|
+
var _a, _b, _c;
|
|
305
333
|
const id = Number(request.id);
|
|
306
334
|
const done = (ok, v, error) => this.send(socket, { t: 'done', id, ok, v, ...(error ? { error } : {}) });
|
|
307
335
|
if (typeof request.k !== 'string' || !/^[A-Za-z0-9_-]{1,64}$/.test(request.k) || typeof request.d !== 'string') {
|
|
@@ -315,17 +343,23 @@ class RelayHub {
|
|
|
315
343
|
const key = STATE + request.k;
|
|
316
344
|
const before = await this.storage.get(key);
|
|
317
345
|
const version = (_a = before === null || before === void 0 ? void 0 : before.v) !== null && _a !== void 0 ? _a : 0;
|
|
346
|
+
// What a member keeps as its own (its lines, its posts) no other member replaces or clears: the owner alone may.
|
|
347
|
+
if ((before === null || before === void 0 ? void 0 : before.own) && before.by !== member && member !== meta.owner && ((_b = (await this.storage.get('members'))) !== null && _b !== void 0 ? _b : {})[before.by]) {
|
|
348
|
+
done(false, version, 'owned');
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
318
351
|
if (request.v !== undefined && request.v !== version) {
|
|
319
352
|
done(false, version);
|
|
320
353
|
return;
|
|
321
354
|
}
|
|
322
355
|
const keys = meta.keys + (before ? 0 : 1);
|
|
323
|
-
const size = meta.size - ((
|
|
356
|
+
const size = meta.size - ((_c = before === null || before === void 0 ? void 0 : before.d.length) !== null && _c !== void 0 ? _c : 0) + request.d.length;
|
|
324
357
|
if (keys > relay_protocol_1.RELAY_HUB_MAX_KEYS || size > relay_protocol_1.RELAY_HUB_MAX_STATE) {
|
|
325
358
|
done(false, version, 'limit');
|
|
326
359
|
return;
|
|
327
360
|
}
|
|
328
|
-
const
|
|
361
|
+
const own = request.own === true || ((before === null || before === void 0 ? void 0 : before.own) === true && before.by === member);
|
|
362
|
+
const entry = { k: request.k, v: version + 1, d: request.d, by: member, ...(own ? { own: true } : {}) };
|
|
329
363
|
await this.storage.put(key, entry);
|
|
330
364
|
meta.keys = keys;
|
|
331
365
|
meta.size = size;
|
|
@@ -357,12 +391,41 @@ class RelayHub {
|
|
|
357
391
|
async setRoster(socket, roster) {
|
|
358
392
|
var _a;
|
|
359
393
|
const before = await this.storage.get('roster');
|
|
360
|
-
|
|
394
|
+
const k = RelayHub.epochKeys(roster === null || roster === void 0 ? void 0 : roster.k);
|
|
395
|
+
if (!roster || typeof roster.d !== 'string' || roster.d.length > relay_protocol_1.RELAY_HUB_MAX_VALUE || k === null || !(roster.v > ((_a = before === null || before === void 0 ? void 0 : before.v) !== null && _a !== void 0 ? _a : 0))) {
|
|
361
396
|
this.send(socket, { t: 'error', code: 'invalid', message: 'A roster needs a newer version.' });
|
|
362
397
|
return;
|
|
363
398
|
}
|
|
364
|
-
|
|
365
|
-
this.
|
|
399
|
+
const kept = { v: roster.v, d: roster.d, ...(k ? { k } : {}) };
|
|
400
|
+
await this.storage.put('roster', kept);
|
|
401
|
+
this.broadcast({ t: 'roster', roster: kept });
|
|
402
|
+
}
|
|
403
|
+
/** The keys of a roster sealed for each member (RelayHubRoster.k): undefined for none, null for what is not that. */
|
|
404
|
+
static epochKeys(k) {
|
|
405
|
+
if (k === undefined) {
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
408
|
+
if (!k || typeof k !== 'object' || Array.isArray(k)) {
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
const entries = Object.entries(k);
|
|
412
|
+
const ok = entries.length <= relay_protocol_1.RELAY_HUB_MAX_MEMBERS &&
|
|
413
|
+
entries.every(([member, value]) => RelayHub.id(member) && typeof value === 'string' && value.length < 4096);
|
|
414
|
+
return ok ? Object.fromEntries(entries) : null;
|
|
415
|
+
}
|
|
416
|
+
/** Whether a member lets in, takes out and invites: the owner, or an admin it made. */
|
|
417
|
+
static manages(meta, member) {
|
|
418
|
+
var _a;
|
|
419
|
+
return member === meta.owner || !!((_a = meta.admins) === null || _a === void 0 ? void 0 : _a.includes(member));
|
|
420
|
+
}
|
|
421
|
+
/** One gone is no admin, its invitation with it. */
|
|
422
|
+
async unmake(meta, member) {
|
|
423
|
+
var _a, _b, _c, _d;
|
|
424
|
+
if (((_a = meta.admins) === null || _a === void 0 ? void 0 : _a.includes(member)) || ((_b = meta.invites) === null || _b === void 0 ? void 0 : _b[member])) {
|
|
425
|
+
meta.admins = (_c = meta.admins) === null || _c === void 0 ? void 0 : _c.filter(id => id !== member);
|
|
426
|
+
(_d = meta.invites) === null || _d === void 0 ? true : delete _d[member];
|
|
427
|
+
await this.storage.put('meta', meta);
|
|
428
|
+
}
|
|
366
429
|
}
|
|
367
430
|
async closeRoom() {
|
|
368
431
|
await this.storage.deleteAll();
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type RelayHubRules } from './relay-hub-charter';
|
|
2
|
+
import { RelayHubsIdentity } from './relay-hubs-identity';
|
|
3
|
+
import type { Live } from './relay-hubs-live';
|
|
4
|
+
import type { RelayRoomInfo } from './relay-hubs-protocol';
|
|
5
|
+
/**
|
|
6
|
+
* The part of {@link RelayHubs} that says who may do what in a room (docs/design/ta-link.md 8): the owner's choices,
|
|
7
|
+
* signed in the charter of the roster (its admins, its rules, how many it lets in), and the checks of the calls.
|
|
8
|
+
*/
|
|
9
|
+
export declare abstract class RelayHubsAdmins extends RelayHubsIdentity {
|
|
10
|
+
/**
|
|
11
|
+
* The owner's choices for a room (docs/design/ta-link.md 8): how many members it lets in at most
|
|
12
|
+
* (RELAY_ROOM_MAX_MEMBERS by default, 2 to RELAY_ROOM_CAPACITY_MAX; those in already stay), its admins (member ids,
|
|
13
|
+
* Ta of the room, not guests) and its rules, for all; the last two signed in the charter of the roster.
|
|
14
|
+
*/
|
|
15
|
+
configure(roomId: string, options: {
|
|
16
|
+
members?: number;
|
|
17
|
+
admins?: string[];
|
|
18
|
+
rules?: RelayHubRules;
|
|
19
|
+
}): Promise<RelayRoomInfo>;
|
|
20
|
+
/** The members it lets in at most: the owner's own choice, else the one its charter says. */
|
|
21
|
+
protected capacityOf(live: Live): number;
|
|
22
|
+
protected managed(roomId: string): Live;
|
|
23
|
+
protected owned(roomId: string): Live;
|
|
24
|
+
protected member(roomId: string): Live;
|
|
25
|
+
protected isFriends(live: Live): boolean;
|
|
26
|
+
protected abstract info(live: Live): RelayRoomInfo;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=relay-hubs-admins.d.ts.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayHubsAdmins = void 0;
|
|
4
|
+
const relay_friends_1 = require("./relay-friends");
|
|
5
|
+
const relay_hub_charter_1 = require("./relay-hub-charter");
|
|
6
|
+
const relay_hubs_identity_1 = require("./relay-hubs-identity");
|
|
7
|
+
const relay_protocol_1 = require("./relay-protocol");
|
|
8
|
+
/**
|
|
9
|
+
* The part of {@link RelayHubs} that says who may do what in a room (docs/design/ta-link.md 8): the owner's choices,
|
|
10
|
+
* signed in the charter of the roster (its admins, its rules, how many it lets in), and the checks of the calls.
|
|
11
|
+
*/
|
|
12
|
+
class RelayHubsAdmins extends relay_hubs_identity_1.RelayHubsIdentity {
|
|
13
|
+
/**
|
|
14
|
+
* The owner's choices for a room (docs/design/ta-link.md 8): how many members it lets in at most
|
|
15
|
+
* (RELAY_ROOM_MAX_MEMBERS by default, 2 to RELAY_ROOM_CAPACITY_MAX; those in already stay), its admins (member ids,
|
|
16
|
+
* Ta of the room, not guests) and its rules, for all; the last two signed in the charter of the roster.
|
|
17
|
+
*/
|
|
18
|
+
async configure(roomId, options) {
|
|
19
|
+
var _a;
|
|
20
|
+
var _b, _c;
|
|
21
|
+
const live = this.owned(roomId);
|
|
22
|
+
let capacity = live.record.capacity;
|
|
23
|
+
if (options.members !== undefined) {
|
|
24
|
+
const members = Math.round(Number(options.members));
|
|
25
|
+
if (!(members >= 2 && members <= relay_protocol_1.RELAY_ROOM_CAPACITY_MAX)) {
|
|
26
|
+
throw new Error(`A room takes 2 to ${relay_protocol_1.RELAY_ROOM_CAPACITY_MAX} members.`);
|
|
27
|
+
}
|
|
28
|
+
capacity = members === relay_protocol_1.RELAY_ROOM_MAX_MEMBERS ? undefined : members;
|
|
29
|
+
live.record.capacity = capacity;
|
|
30
|
+
if (capacity === undefined) {
|
|
31
|
+
delete live.record.capacity;
|
|
32
|
+
}
|
|
33
|
+
this.save();
|
|
34
|
+
}
|
|
35
|
+
if (options.admins === undefined && options.rules === undefined && (options.members === undefined || !live.charter)) {
|
|
36
|
+
return this.info(live);
|
|
37
|
+
}
|
|
38
|
+
const before = (_b = live.charter) !== null && _b !== void 0 ? _b : { room: live.record.id, v: 0, admins: [], rules: {} };
|
|
39
|
+
const roster = (_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : [];
|
|
40
|
+
const admins = options.admins === undefined
|
|
41
|
+
? before.admins
|
|
42
|
+
: [...new Set(options.admins.map(String))]
|
|
43
|
+
.map(id => roster.find(member => member.member === id && !member.owner && !member.guest))
|
|
44
|
+
.filter(member => !!member)
|
|
45
|
+
.slice(0, relay_hub_charter_1.RELAY_HUB_MAX_ADMINS)
|
|
46
|
+
.map(member => ({ member: member.member, id: member.id }));
|
|
47
|
+
const charter = {
|
|
48
|
+
room: live.record.id,
|
|
49
|
+
v: before.v,
|
|
50
|
+
admins,
|
|
51
|
+
rules: options.rules === undefined ? before.rules : relay_hub_charter_1.RelayHubCharterUtil.rules(options.rules),
|
|
52
|
+
...(capacity ? { capacity } : {}),
|
|
53
|
+
};
|
|
54
|
+
await this.ready(live);
|
|
55
|
+
await this.writeRoster(live, same => same, charter);
|
|
56
|
+
this.request(live, { t: 'admins', admins: admins.map(admin => admin.member) });
|
|
57
|
+
this.emit({ type: 'rooms' });
|
|
58
|
+
return this.info(live);
|
|
59
|
+
}
|
|
60
|
+
/** The members it lets in at most: the owner's own choice, else the one its charter says. */
|
|
61
|
+
capacityOf(live) {
|
|
62
|
+
var _a;
|
|
63
|
+
var _b;
|
|
64
|
+
if (this.isFriends(live)) {
|
|
65
|
+
return relay_protocol_1.RELAY_FRIENDS_MAX + 1;
|
|
66
|
+
}
|
|
67
|
+
return (_b = (live.record.own ? live.record.capacity : (_a = live.charter) === null || _a === void 0 ? void 0 : _a.capacity)) !== null && _b !== void 0 ? _b : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS;
|
|
68
|
+
}
|
|
69
|
+
managed(roomId) {
|
|
70
|
+
const live = this.member(roomId);
|
|
71
|
+
if (!this.manages(live)) {
|
|
72
|
+
throw new Error('Only the owner or an admin of the room does that.');
|
|
73
|
+
}
|
|
74
|
+
return live;
|
|
75
|
+
}
|
|
76
|
+
owned(roomId) {
|
|
77
|
+
const live = this.member(roomId);
|
|
78
|
+
if (!live.record.own) {
|
|
79
|
+
throw new Error('Only the owner of the room does that.');
|
|
80
|
+
}
|
|
81
|
+
return live;
|
|
82
|
+
}
|
|
83
|
+
member(roomId) {
|
|
84
|
+
const live = this.live.get(roomId);
|
|
85
|
+
if ((live === null || live === void 0 ? void 0 : live.record.status) !== 'member') {
|
|
86
|
+
throw new Error('Not in that room.');
|
|
87
|
+
}
|
|
88
|
+
return live;
|
|
89
|
+
}
|
|
90
|
+
isFriends(live) {
|
|
91
|
+
return live.record.own && live.record.kind === relay_friends_1.RELAY_FRIENDS_KIND;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.RelayHubsAdmins = RelayHubsAdmins;
|
|
95
|
+
//# sourceMappingURL=relay-hubs-admins.js.map
|
|
@@ -1,55 +1,9 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import
|
|
1
|
+
import { type RelayHubCharterBody } from './relay-hub-charter';
|
|
2
|
+
import { type RelayHubAskBody, type RelayHubMember } from './relay-hub-util';
|
|
3
|
+
import { type Live, type MessageBody } from './relay-hubs-live';
|
|
4
|
+
import type { RelayHubsOptions, RelayRoomEvent, RelayRoomRecord, RelayRoomRequest, RelayRoomStatus } from './relay-hubs-protocol';
|
|
3
5
|
import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
|
|
4
|
-
import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest } from './relay-protocol';
|
|
5
|
-
/** What a member sends in a message, signed and sealed: data of an extension, or a word of Ta itself. */
|
|
6
|
-
export interface MessageBody {
|
|
7
|
-
d?: unknown;
|
|
8
|
-
/** A member's new name, for the owner to put in the roster; with its look (RelayPublicIdentity.look), '' for none. */
|
|
9
|
-
name?: string;
|
|
10
|
-
look?: string;
|
|
11
|
-
/** Asks the one it is sent to to be friends: the link of the sender's room of friends (RelayFriends.offer). */
|
|
12
|
-
friend?: string;
|
|
13
|
-
}
|
|
14
|
-
export interface StateBody {
|
|
15
|
-
key: string;
|
|
16
|
-
value: unknown;
|
|
17
|
-
}
|
|
18
|
-
/** How long a call waits for the room to be connected, or for the relay to answer. */
|
|
19
|
-
export declare const READY_MS = 15000;
|
|
20
|
-
export declare const OPEN = 1;
|
|
21
|
-
/** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
|
|
22
|
-
export declare class Live {
|
|
23
|
-
record: RelayRoomRecord;
|
|
24
|
-
socket?: WebSocket;
|
|
25
|
-
keys?: RelayHubKeys;
|
|
26
|
-
synced: boolean;
|
|
27
|
-
online: Set<string>;
|
|
28
|
-
roster?: RelayHubRosterBody;
|
|
29
|
-
readonly state: Map<string, RelayRoomValue>;
|
|
30
|
-
/** Asks to join, with the invitation each came with. */
|
|
31
|
-
readonly asks: Map<string, {
|
|
32
|
-
body: RelayHubAskBody;
|
|
33
|
-
invite: string;
|
|
34
|
-
}>;
|
|
35
|
-
readonly done: Map<number, (result: {
|
|
36
|
-
ok: boolean;
|
|
37
|
-
v: number;
|
|
38
|
-
error?: RelayHubFailure;
|
|
39
|
-
}) => void>;
|
|
40
|
-
readonly ready: Array<() => void>;
|
|
41
|
-
attempts: number;
|
|
42
|
-
/** Connect again at once, whatever the close said. */
|
|
43
|
-
retry: boolean;
|
|
44
|
-
timer?: ReturnType<typeof setTimeout>;
|
|
45
|
-
pingTimer?: ReturnType<typeof setInterval>;
|
|
46
|
-
awaitingPong: boolean;
|
|
47
|
-
/** The look this Ta last sent the owner, for the roster to say: once, whatever the owner does with it. */
|
|
48
|
-
toldLook?: string;
|
|
49
|
-
/** Events are handled one after another: opening them is async. */
|
|
50
|
-
chain: Promise<void>;
|
|
51
|
-
constructor(record: RelayRoomRecord);
|
|
52
|
-
}
|
|
6
|
+
import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest, type RelayHubRoster } from './relay-protocol';
|
|
53
7
|
/**
|
|
54
8
|
* The half of {@link RelayHubs} that keeps a connection to the hub of each room and reads what comes over it:
|
|
55
9
|
* connecting again, the welcome, the roster, the state and the messages, each opened and checked against the
|
|
@@ -74,12 +28,15 @@ export declare abstract class RelayHubsConnection {
|
|
|
74
28
|
protected ready(live: Live): Promise<void>;
|
|
75
29
|
protected request(live: Live, request: RelayHubRequest): void;
|
|
76
30
|
protected sendBody(live: Live, body: MessageBody, to?: string): Promise<void>;
|
|
77
|
-
/**
|
|
78
|
-
protected
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
31
|
+
/** Whether this Ta lets in, takes out and invites: the owner, or an admin of the charter. */
|
|
32
|
+
protected manages(live: Live): boolean;
|
|
33
|
+
protected isAdmin(live: Live, member: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The roster with `change` made, its version one more, signed by this Ta (the owner, or an admin). The owner's
|
|
36
|
+
* carries `charter` when it changes it, else the charter of before, its admins still members; an admin's, the one it has.
|
|
37
|
+
*/
|
|
38
|
+
protected nextRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[], charter?: RelayHubCharterBody, rekey?: boolean): Promise<RelayHubRoster>;
|
|
39
|
+
protected writeRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[], charter?: RelayHubCharterBody, rekey?: boolean): Promise<void>;
|
|
83
40
|
protected connect(live: Live): void;
|
|
84
41
|
protected hello(live: Live, socket: WebSocket): Promise<void>;
|
|
85
42
|
protected closed(live: Live, code: number): void;
|
|
@@ -96,18 +53,17 @@ export declare abstract class RelayHubsConnection {
|
|
|
96
53
|
protected heardMembers(_members: RelayPublicIdentity[]): void;
|
|
97
54
|
/** A member asks the user to be friends, by the link of its room of friends. */
|
|
98
55
|
protected offered(_live: Live, _from: RelayPublicIdentity, _link: string): void;
|
|
99
|
-
protected readRoster(live: Live,
|
|
56
|
+
protected readRoster(live: Live, roster: RelayHubRoster): Promise<void>;
|
|
57
|
+
/** The roster says this Ta as it is now: its look (RelayHubsIdentity). */
|
|
58
|
+
protected keepLook(_live: Live): Promise<void>;
|
|
100
59
|
/** The state of the room has this Ta's picture as it is now (RelayHubs). */
|
|
101
60
|
protected keepPicture(_live: Live): Promise<void>;
|
|
102
|
-
/**
|
|
103
|
-
* The roster says this Ta as it is now: its look, set while the room was away or the owner offline. The owner writes it,
|
|
104
|
-
* a member tells the owner, once a connection. A room joined with another identity keeps that one's.
|
|
105
|
-
*/
|
|
106
|
-
protected keepLook(live: Live): Promise<void>;
|
|
107
61
|
protected guestOf(live: Live, member: string): boolean;
|
|
108
62
|
protected signOf(live: Live): (member: string) => string | undefined;
|
|
109
63
|
protected readEntry(live: Live, entry: RelayHubEntry, tell: boolean): Promise<void>;
|
|
110
64
|
protected readMessage(live: Live, from: string, sealed: string): Promise<void>;
|
|
65
|
+
/** A member set a key naming another: the owner clears it (RelayHubs). */
|
|
66
|
+
protected foreignSet(_live: Live, _key: string): void;
|
|
111
67
|
/** Ids of those the user chose as friends. */
|
|
112
68
|
protected friendIds(): Set<string>;
|
|
113
69
|
protected readRooms(): RelayRoomRecord[];
|