@celljs/ta-relay 4.16.0 → 4.17.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-content.d.ts +13 -0
- package/esm/common/relay-content.js +42 -0
- package/esm/common/relay-conversation.d.ts +14 -0
- package/esm/common/relay-hub.js +2 -2
- package/esm/common/relay-hubs-identity.d.ts +36 -0
- package/esm/common/relay-hubs-identity.js +156 -0
- package/esm/common/relay-hubs-protocol.d.ts +4 -0
- package/esm/common/relay-hubs-util.d.ts +11 -0
- package/esm/common/relay-hubs-util.js +74 -0
- package/esm/common/relay-hubs.d.ts +17 -44
- package/esm/common/relay-hubs.js +37 -235
- package/esm/common/relay-protocol.d.ts +9 -0
- package/esm/common/relay-protocol.js +13 -0
- package/esm/common/relay-spot.d.ts +15 -3
- package/esm/common/relay-spot.js +17 -5
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/relay-memory-storage.d.ts +26 -0
- package/esm/node/relay-memory-storage.js +60 -0
- package/esm/node/relay-server.d.ts +14 -26
- package/esm/node/relay-server.js +33 -60
- package/esm/node/relay-store.js +4 -4
- package/esm/node/relays-file.d.ts +27 -0
- package/esm/node/relays-file.js +59 -4
- package/esm/viewer/viewer.js +33 -1
- package/lib/common/index.d.ts +1 -0
- package/lib/common/index.js +1 -0
- package/lib/common/relay-content.d.ts +13 -0
- package/lib/common/relay-content.js +43 -1
- package/lib/common/relay-conversation.d.ts +14 -0
- package/lib/common/relay-hub.js +1 -1
- package/lib/common/relay-hubs-identity.d.ts +37 -0
- package/lib/common/relay-hubs-identity.js +160 -0
- package/lib/common/relay-hubs-protocol.d.ts +4 -0
- package/lib/common/relay-hubs-util.d.ts +12 -0
- package/lib/common/relay-hubs-util.js +77 -0
- package/lib/common/relay-hubs.d.ts +17 -44
- package/lib/common/relay-hubs.js +36 -234
- package/lib/common/relay-protocol.d.ts +9 -0
- package/lib/common/relay-protocol.js +15 -1
- package/lib/common/relay-spot.d.ts +15 -3
- package/lib/common/relay-spot.js +17 -5
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/relay-memory-storage.d.ts +27 -0
- package/lib/node/relay-memory-storage.js +65 -0
- package/lib/node/relay-server.d.ts +14 -26
- package/lib/node/relay-server.js +34 -61
- package/lib/node/relay-store.js +3 -3
- package/lib/node/relays-file.d.ts +27 -0
- package/lib/node/relays-file.js +58 -3
- package/lib/viewer/viewer.js +32 -0
- package/package.json +3 -2
- package/viewer/join.html +3 -3
- package/viewer/page.html +9 -9
- package/viewer/room.html +10 -10
package/esm/common/relay-hubs.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { RELAY_FRIENDS_KIND, RelayFriends, RelayFriendsUtil } from './relay-friends.js';
|
|
2
2
|
import { RelayHubUtil } from './relay-hub-util.js';
|
|
3
|
-
import { Live, OPEN, READY_MS
|
|
3
|
+
import { Live, OPEN, READY_MS } from './relay-hubs-connection.js';
|
|
4
|
+
import { RelayHubsIdentity } from './relay-hubs-identity.js';
|
|
4
5
|
import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
|
|
5
|
-
import { RELAY_FRIENDS_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
|
|
6
|
+
import { RELAY_FRIENDS_MAX, RELAY_INVITE_MAX_DAYS, RELAY_ROOM_CAPACITY_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
|
|
6
7
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
7
8
|
/**
|
|
8
9
|
* The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
|
|
@@ -10,7 +11,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
10
11
|
* the others; every message signed by its member and sealed with the key of the room. The friends are a room of
|
|
11
12
|
* their own (RelayFriends).
|
|
12
13
|
*/
|
|
13
|
-
export class RelayHubs extends
|
|
14
|
+
export class RelayHubs extends RelayHubsIdentity {
|
|
14
15
|
constructor() {
|
|
15
16
|
super(...arguments);
|
|
16
17
|
this.book = new RelayFriends({
|
|
@@ -65,48 +66,6 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
65
66
|
this.listeners.add(listener);
|
|
66
67
|
return () => this.listeners.delete(listener);
|
|
67
68
|
}
|
|
68
|
-
/** The name others see; the owners of rooms this Ta is in hear it. */
|
|
69
|
-
async setName(name) {
|
|
70
|
-
const trimmed = String(name).trim().slice(0, 40);
|
|
71
|
-
if (!trimmed) {
|
|
72
|
-
throw new Error('A name is needed.');
|
|
73
|
-
}
|
|
74
|
-
return this.changeSelf({ ...(await this.identity()), name: trimmed });
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
|
|
78
|
-
* look that is none. The owners of rooms this Ta is in hear it, as a name.
|
|
79
|
-
*/
|
|
80
|
-
async setLook(look) {
|
|
81
|
-
const identity = await this.identity();
|
|
82
|
-
const next = RelayIdentityUtil.withLook(identity, look);
|
|
83
|
-
return next.look === identity.look ? RelayIdentityUtil.publicOf(identity) : this.changeSelf(next);
|
|
84
|
-
}
|
|
85
|
-
/** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
|
|
86
|
-
async changeSelf(identity) {
|
|
87
|
-
var _a;
|
|
88
|
-
this.options.store.write('identity', identity);
|
|
89
|
-
this.own = identity;
|
|
90
|
-
for (const live of this.live.values()) {
|
|
91
|
-
// A room joined with another identity keeps that one's name.
|
|
92
|
-
if (live.record.status !== 'member' || live.record.identity) {
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
if (live.record.own) {
|
|
96
|
-
await this.writeRoster(live, members => members.map(member => (member.id === identity.id ? RelayIdentityUtil.withLook({ ...member, name: identity.name }, identity.look) : member))).catch(() => undefined);
|
|
97
|
-
}
|
|
98
|
-
else if (live.roster) {
|
|
99
|
-
const owner = live.roster.members.find(member => member.owner);
|
|
100
|
-
if (owner) {
|
|
101
|
-
const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
|
|
102
|
-
await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
|
|
103
|
-
live.toldLook = look;
|
|
104
|
-
}, () => undefined);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
return RelayIdentityUtil.publicOf(identity);
|
|
109
|
-
}
|
|
110
69
|
list() {
|
|
111
70
|
return [...this.live.values()].map(live => this.info(live)).sort((a, b) => { var _a, _b; return ((_a = b.joinedAt) !== null && _a !== void 0 ? _a : b.createdAt) - ((_b = a.joinedAt) !== null && _b !== void 0 ? _b : a.createdAt); });
|
|
112
71
|
}
|
|
@@ -155,7 +114,7 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
155
114
|
}
|
|
156
115
|
/**
|
|
157
116
|
* The link that lets others ask to join: the one of before while it has a day left, else (or with `renew`) a new
|
|
158
|
-
* one for `days` (7 by default,
|
|
117
|
+
* one for `days` (7 by default, RELAY_INVITE_MAX_DAYS at most), the links of before no longer working.
|
|
159
118
|
*/
|
|
160
119
|
async invite(roomId, options = {}) {
|
|
161
120
|
var _a;
|
|
@@ -178,7 +137,7 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
178
137
|
return linkOf(latest);
|
|
179
138
|
}
|
|
180
139
|
const secret = RelayHubUtil.randomKey();
|
|
181
|
-
const expiresAt = Date.now() + Math.min(Math.max((_b = options.days) !== null && _b !== void 0 ? _b : 7, 1),
|
|
140
|
+
const expiresAt = Date.now() + Math.min(Math.max((_b = options.days) !== null && _b !== void 0 ? _b : 7, 1), RELAY_INVITE_MAX_DAYS) * DAY_MS;
|
|
182
141
|
await this.ready(live);
|
|
183
142
|
this.request(live, { t: 'invite', hash: await RelayKeys.hash(await RelayHubUtil.inviteProof(secret)), expiresAt });
|
|
184
143
|
// Asks sealed with an invitation of before are still read, while they wait.
|
|
@@ -186,15 +145,17 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
186
145
|
this.save();
|
|
187
146
|
return linkOf({ secret, expiresAt });
|
|
188
147
|
}
|
|
189
|
-
/**
|
|
148
|
+
/**
|
|
149
|
+
* Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it.
|
|
150
|
+
* A room the identity owns on the user's other computer (an identity shared or moved) is joined as any other.
|
|
151
|
+
*/
|
|
190
152
|
async join(text) {
|
|
191
153
|
const link = RelayHubUtil.parse(text);
|
|
192
|
-
const identity = await this.identity();
|
|
193
154
|
const known = this.live.get(link.room);
|
|
194
155
|
if (known && (known.record.status === 'member' || known.record.status === 'waiting')) {
|
|
195
156
|
return this.info(known);
|
|
196
157
|
}
|
|
197
|
-
if (
|
|
158
|
+
if (known === null || known === void 0 ? void 0 : known.record.own) {
|
|
198
159
|
throw new Error('This is a room of your own.');
|
|
199
160
|
}
|
|
200
161
|
const friends = RelayFriendsUtil.isKind(link.kind);
|
|
@@ -240,7 +201,7 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
240
201
|
/** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
|
|
241
202
|
async answer(roomId, askId, accept) {
|
|
242
203
|
var _a;
|
|
243
|
-
var _b;
|
|
204
|
+
var _b, _c;
|
|
244
205
|
const live = this.owned(roomId);
|
|
245
206
|
const asked = live.asks.get(askId);
|
|
246
207
|
if (!asked) {
|
|
@@ -260,8 +221,8 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
260
221
|
...(live.record.unlisted ? { unlisted: true } : {}),
|
|
261
222
|
};
|
|
262
223
|
const ask = asked.body;
|
|
263
|
-
const most = this.isFriends(live) ? RELAY_FRIENDS_MAX + 1 : RELAY_ROOM_MAX_MEMBERS;
|
|
264
|
-
if (((
|
|
224
|
+
const most = this.isFriends(live) ? RELAY_FRIENDS_MAX + 1 : ((_b = live.record.capacity) !== null && _b !== void 0 ? _b : RELAY_ROOM_MAX_MEMBERS);
|
|
225
|
+
if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= most) {
|
|
265
226
|
throw new Error('The room is full.');
|
|
266
227
|
}
|
|
267
228
|
const answer = await RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite);
|
|
@@ -272,6 +233,27 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
272
233
|
live.asks.delete(askId);
|
|
273
234
|
this.emit({ type: 'rooms' });
|
|
274
235
|
}
|
|
236
|
+
/**
|
|
237
|
+
* The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
|
|
238
|
+
* RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
|
|
239
|
+
*/
|
|
240
|
+
configure(roomId, options) {
|
|
241
|
+
const live = this.owned(roomId);
|
|
242
|
+
if (options.members !== undefined) {
|
|
243
|
+
const members = Math.round(Number(options.members));
|
|
244
|
+
if (!(members >= 2 && members <= RELAY_ROOM_CAPACITY_MAX)) {
|
|
245
|
+
throw new Error(`A room takes 2 to ${RELAY_ROOM_CAPACITY_MAX} members.`);
|
|
246
|
+
}
|
|
247
|
+
if (members === RELAY_ROOM_MAX_MEMBERS) {
|
|
248
|
+
delete live.record.capacity;
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
live.record.capacity = members;
|
|
252
|
+
}
|
|
253
|
+
this.save();
|
|
254
|
+
}
|
|
255
|
+
return this.info(live);
|
|
256
|
+
}
|
|
275
257
|
/** The owner takes a member out: the relay closes it at once. */
|
|
276
258
|
async remove(roomId, member) {
|
|
277
259
|
const live = this.owned(roomId);
|
|
@@ -358,114 +340,6 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
358
340
|
}
|
|
359
341
|
return { ok: result.ok, version: result.v };
|
|
360
342
|
}
|
|
361
|
-
/** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
|
|
362
|
-
async exportIdentity() {
|
|
363
|
-
return { identity: await this.identity(), rooms: [...this.live.values()].map(live => live.record), friends: this.book.chosen() };
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
|
|
367
|
-
* sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
|
|
368
|
-
* (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
|
|
369
|
-
*/
|
|
370
|
-
async importIdentity(bundle, options = {}) {
|
|
371
|
-
const incoming = bundle === null || bundle === void 0 ? void 0 : bundle.identity;
|
|
372
|
-
if (!RelayHubsUtil.isPrivate(incoming) || !(await RelayIdentityUtil.check(incoming))) {
|
|
373
|
-
throw new Error('Not an identity of Ta.');
|
|
374
|
-
}
|
|
375
|
-
const own = await this.identity();
|
|
376
|
-
const switched = incoming.id !== own.id;
|
|
377
|
-
if (switched) {
|
|
378
|
-
for (const live of this.live.values()) {
|
|
379
|
-
if (!live.record.identity) {
|
|
380
|
-
live.record = { ...live.record, identity: own };
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
const shared = options.shared || own.shared;
|
|
385
|
-
const next = { ...(switched ? incoming : own), ...(shared ? { shared: true } : {}) };
|
|
386
|
-
if (!shared) {
|
|
387
|
-
delete next.shared;
|
|
388
|
-
}
|
|
389
|
-
this.own = next;
|
|
390
|
-
this.options.store.write('identity', next);
|
|
391
|
-
let rooms = 0;
|
|
392
|
-
for (const record of Array.isArray(bundle.rooms) ? bundle.rooms : []) {
|
|
393
|
-
if (this.upsert(record)) {
|
|
394
|
-
rooms++;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
const friends = this.book.merge(Array.isArray(bundle.friends) ? bundle.friends : []);
|
|
398
|
-
this.save();
|
|
399
|
-
this.emit({ type: 'rooms' });
|
|
400
|
-
return { switched, rooms, friends };
|
|
401
|
-
}
|
|
402
|
-
/** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
|
|
403
|
-
async setShared(shared) {
|
|
404
|
-
const identity = { ...(await this.identity()) };
|
|
405
|
-
if (!!identity.shared === shared) {
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
if (shared) {
|
|
409
|
-
identity.shared = true;
|
|
410
|
-
}
|
|
411
|
-
else {
|
|
412
|
-
delete identity.shared;
|
|
413
|
-
}
|
|
414
|
-
this.own = identity;
|
|
415
|
-
this.options.store.write('identity', identity);
|
|
416
|
-
}
|
|
417
|
-
/**
|
|
418
|
-
* A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
|
|
419
|
-
* (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
|
|
420
|
-
* as another member.
|
|
421
|
-
*/
|
|
422
|
-
upsert(record) {
|
|
423
|
-
if (!RelayHubsUtil.isRecord(record)) {
|
|
424
|
-
return false;
|
|
425
|
-
}
|
|
426
|
-
const live = this.live.get(record.id);
|
|
427
|
-
if (!live) {
|
|
428
|
-
const added = new Live({ ...record });
|
|
429
|
-
this.live.set(record.id, added);
|
|
430
|
-
if (record.status === 'member' || record.status === 'waiting') {
|
|
431
|
-
this.connect(added);
|
|
432
|
-
}
|
|
433
|
-
this.save();
|
|
434
|
-
this.emit({ type: 'rooms' });
|
|
435
|
-
return true;
|
|
436
|
-
}
|
|
437
|
-
if (live.record.member !== record.member) {
|
|
438
|
-
return false;
|
|
439
|
-
}
|
|
440
|
-
const merged = RelayHubsUtil.merge(live.record, record);
|
|
441
|
-
if (JSON.stringify(merged) === JSON.stringify(live.record)) {
|
|
442
|
-
return false;
|
|
443
|
-
}
|
|
444
|
-
const was = live.record.status;
|
|
445
|
-
live.record = merged;
|
|
446
|
-
if (merged.status !== 'member' && merged.status !== 'waiting') {
|
|
447
|
-
this.disconnect(live);
|
|
448
|
-
}
|
|
449
|
-
else if (was !== merged.status || !live.socket) {
|
|
450
|
-
this.disconnect(live);
|
|
451
|
-
this.connect(live);
|
|
452
|
-
}
|
|
453
|
-
this.save();
|
|
454
|
-
this.emit({ type: 'rooms' });
|
|
455
|
-
return true;
|
|
456
|
-
}
|
|
457
|
-
/** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
|
|
458
|
-
forget(roomId, member) {
|
|
459
|
-
const live = this.live.get(roomId);
|
|
460
|
-
if (!live || live.record.member !== member) {
|
|
461
|
-
return false;
|
|
462
|
-
}
|
|
463
|
-
this.disconnect(live);
|
|
464
|
-
this.live.delete(roomId);
|
|
465
|
-
this.save();
|
|
466
|
-
this.emit({ type: 'rooms' });
|
|
467
|
-
return true;
|
|
468
|
-
}
|
|
469
343
|
/** Friends of the user's other computer, with those of this Ta. */
|
|
470
344
|
mergeFriends(friends) {
|
|
471
345
|
return this.book.merge(friends);
|
|
@@ -551,7 +425,7 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
551
425
|
}
|
|
552
426
|
info(live) {
|
|
553
427
|
var _a, _b;
|
|
554
|
-
var _c;
|
|
428
|
+
var _c, _d;
|
|
555
429
|
const { record } = live;
|
|
556
430
|
const members = ((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : []).map(member => { var _a; return ({
|
|
557
431
|
member: member.member,
|
|
@@ -571,6 +445,7 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
571
445
|
...(record.unlisted ? { unlisted: true } : {}),
|
|
572
446
|
relay: record.relay,
|
|
573
447
|
owner: record.own,
|
|
448
|
+
...(record.own ? { capacity: (_d = record.capacity) !== null && _d !== void 0 ? _d : RELAY_ROOM_MAX_MEMBERS } : {}),
|
|
574
449
|
ownerName: record.owner.name,
|
|
575
450
|
status: record.status,
|
|
576
451
|
...(record.reason ? { reason: record.reason } : {}),
|
|
@@ -581,77 +456,4 @@ export class RelayHubs extends RelayHubsConnection {
|
|
|
581
456
|
};
|
|
582
457
|
}
|
|
583
458
|
}
|
|
584
|
-
/** How far a membership has come: one computer heard it end, it ended for both. */
|
|
585
|
-
const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
|
|
586
|
-
export var RelayHubsUtil;
|
|
587
|
-
(function (RelayHubsUtil) {
|
|
588
|
-
function isPrivate(identity) {
|
|
589
|
-
const value = identity;
|
|
590
|
-
return (!!value &&
|
|
591
|
-
typeof value.id === 'string' &&
|
|
592
|
-
typeof value.name === 'string' &&
|
|
593
|
-
typeof value.sign === 'string' &&
|
|
594
|
-
typeof value.box === 'string' &&
|
|
595
|
-
!!value.signKey &&
|
|
596
|
-
typeof value.signKey === 'object' &&
|
|
597
|
-
!!value.boxKey &&
|
|
598
|
-
typeof value.boxKey === 'object');
|
|
599
|
-
}
|
|
600
|
-
RelayHubsUtil.isPrivate = isPrivate;
|
|
601
|
-
function isRecord(record) {
|
|
602
|
-
const value = record;
|
|
603
|
-
return (!!value &&
|
|
604
|
-
typeof value.id === 'string' &&
|
|
605
|
-
typeof value.relay === 'string' &&
|
|
606
|
-
typeof value.member === 'string' &&
|
|
607
|
-
typeof value.token === 'string' &&
|
|
608
|
-
typeof value.status === 'string' &&
|
|
609
|
-
value.status in STATUS_RANK &&
|
|
610
|
-
!!value.owner &&
|
|
611
|
-
typeof value.owner.id === 'string' &&
|
|
612
|
-
(value.identity === undefined || isPrivate(value.identity)));
|
|
613
|
-
}
|
|
614
|
-
RelayHubsUtil.isRecord = isRecord;
|
|
615
|
-
/**
|
|
616
|
-
* Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
|
|
617
|
-
* key and the owner's keys once either has them, the invitations of both, made once either made it.
|
|
618
|
-
*/
|
|
619
|
-
function merge(local, other) {
|
|
620
|
-
var _a, _b, _c, _d, _e, _f;
|
|
621
|
-
const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
|
|
622
|
-
const behind = ahead === local ? other : local;
|
|
623
|
-
const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
|
|
624
|
-
.filter((invite, index, all) => all.findIndex(each => each.secret === invite.secret) === index)
|
|
625
|
-
.sort((a, b) => a.expiresAt - b.expiresAt)
|
|
626
|
-
.slice(-3);
|
|
627
|
-
const merged = {
|
|
628
|
-
...behind,
|
|
629
|
-
...ahead,
|
|
630
|
-
kind: ahead.kind || behind.kind,
|
|
631
|
-
owner: ahead.owner.sign ? ahead.owner : behind.owner.sign ? behind.owner : ahead.owner,
|
|
632
|
-
createdAt: Math.min(local.createdAt, other.createdAt),
|
|
633
|
-
};
|
|
634
|
-
const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
|
|
635
|
-
const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
|
|
636
|
-
const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
|
|
637
|
-
delete merged.key;
|
|
638
|
-
delete merged.invites;
|
|
639
|
-
delete merged.joinedAt;
|
|
640
|
-
delete merged.identity;
|
|
641
|
-
delete merged.made;
|
|
642
|
-
if (merged.status !== 'waiting') {
|
|
643
|
-
delete merged.invite;
|
|
644
|
-
delete merged.asked;
|
|
645
|
-
}
|
|
646
|
-
return {
|
|
647
|
-
...merged,
|
|
648
|
-
...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
|
|
649
|
-
...(invites.length ? { invites } : {}),
|
|
650
|
-
...(local.made || other.made ? { made: true } : {}),
|
|
651
|
-
...(joinedAt ? { joinedAt } : {}),
|
|
652
|
-
...(identity ? { identity } : {}),
|
|
653
|
-
};
|
|
654
|
-
}
|
|
655
|
-
RelayHubsUtil.merge = merge;
|
|
656
|
-
})(RelayHubsUtil || (RelayHubsUtil = {}));
|
|
657
459
|
//# sourceMappingURL=relay-hubs.js.map
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
/** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
|
|
2
2
|
export declare const RELAY_DEFAULT_URL = "https://relay.cellbang.com";
|
|
3
|
+
/**
|
|
4
|
+
* The relay of this deployment (docs/design/ta-deploy.md 3): TA_RELAY_URL, which a distribution of Ta sets for its own
|
|
5
|
+
* people, else the official one. New pairings, rooms and links go there unless relays.json says otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export declare function relayDefaultUrl(env?: Record<string, string | undefined> | undefined): string;
|
|
3
8
|
/** What the relay serves: hosts connect to the first, devices to the second. */
|
|
4
9
|
export declare const RELAY_HOST_PATH = "/v1/host/";
|
|
5
10
|
export declare const RELAY_CONNECT_PATH = "/v1/connect/";
|
|
@@ -170,6 +175,10 @@ export declare const RELAY_ROOM_PATH = "/r";
|
|
|
170
175
|
export declare const RELAY_HUB_MAX_MEMBERS = 101;
|
|
171
176
|
/** Members of a room the user made, its owner among them: the owner's Ta lets in no more (RelayHubs.answer). */
|
|
172
177
|
export declare const RELAY_ROOM_MAX_MEMBERS = 16;
|
|
178
|
+
/** The members a room may be let take at most (RelayRoomRecord.capacity): under what the hub takes. */
|
|
179
|
+
export declare const RELAY_ROOM_CAPACITY_MAX = 100;
|
|
180
|
+
/** How long a link that invites works at most, in days. */
|
|
181
|
+
export declare const RELAY_INVITE_MAX_DAYS = 365;
|
|
173
182
|
/** Friends of a Ta at most: the members of its room of friends (docs/design/ta-link.md 3.1). */
|
|
174
183
|
export declare const RELAY_FRIENDS_MAX = 100;
|
|
175
184
|
/** Characters of a sealed message or value of the state. */
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
/** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
|
|
2
2
|
export const RELAY_DEFAULT_URL = 'https://relay.cellbang.com';
|
|
3
|
+
/**
|
|
4
|
+
* The relay of this deployment (docs/design/ta-deploy.md 3): TA_RELAY_URL, which a distribution of Ta sets for its own
|
|
5
|
+
* people, else the official one. New pairings, rooms and links go there unless relays.json says otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export function relayDefaultUrl(env) {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
if (env === void 0) { env = (_a = globalThis.process) === null || _a === void 0 ? void 0 : _a.env; }
|
|
10
|
+
return ((_b = env === null || env === void 0 ? void 0 : env.TA_RELAY_URL) === null || _b === void 0 ? void 0 : _b.trim().replace(/\/+$/, '')) || RELAY_DEFAULT_URL;
|
|
11
|
+
}
|
|
3
12
|
/** What the relay serves: hosts connect to the first, devices to the second. */
|
|
4
13
|
export const RELAY_HOST_PATH = '/v1/host/';
|
|
5
14
|
export const RELAY_CONNECT_PATH = '/v1/connect/';
|
|
@@ -96,6 +105,10 @@ export const RELAY_ROOM_PATH = '/r';
|
|
|
96
105
|
export const RELAY_HUB_MAX_MEMBERS = 101;
|
|
97
106
|
/** Members of a room the user made, its owner among them: the owner's Ta lets in no more (RelayHubs.answer). */
|
|
98
107
|
export const RELAY_ROOM_MAX_MEMBERS = 16;
|
|
108
|
+
/** The members a room may be let take at most (RelayRoomRecord.capacity): under what the hub takes. */
|
|
109
|
+
export const RELAY_ROOM_CAPACITY_MAX = 100;
|
|
110
|
+
/** How long a link that invites works at most, in days. */
|
|
111
|
+
export const RELAY_INVITE_MAX_DAYS = 365;
|
|
99
112
|
/** Friends of a Ta at most: the members of its room of friends (docs/design/ta-link.md 3.1). */
|
|
100
113
|
export const RELAY_FRIENDS_MAX = 100;
|
|
101
114
|
/** Characters of a sealed message or value of the state. */
|
|
@@ -22,7 +22,9 @@ export declare enum RelaySpotCloseCode {
|
|
|
22
22
|
LATE = 4410,
|
|
23
23
|
FULL = 4429,
|
|
24
24
|
/** This address tried too many codes nobody opened: it waits (RelaySpotTries). */
|
|
25
|
-
TRIES = 4428
|
|
25
|
+
TRIES = 4428,
|
|
26
|
+
/** Opened already, by one whose key this one does not hold: a new code is needed. Counted as a failure. */
|
|
27
|
+
TAKEN = 4409
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
28
30
|
* The codes nobody opened that one address tried lately (docs/design/ta-nearby.md 2.2): six digits are a million
|
|
@@ -44,12 +46,19 @@ export interface RelaySpotWho {
|
|
|
44
46
|
/** Its id of before, for what was sealed for it just before it changed. */
|
|
45
47
|
was?: string;
|
|
46
48
|
opened: number;
|
|
49
|
+
/** The key of the spot, given to who opened it alone: with it, it comes back in (RelaySpotIn `open`). */
|
|
50
|
+
k?: string;
|
|
47
51
|
}
|
|
48
52
|
/** What one sends to its spot. */
|
|
49
53
|
export type RelaySpotIn =
|
|
50
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* First: opens it. A spot open already lets in only who holds its key (`k`, of its `ready`), back after its connection
|
|
56
|
+
* dropped, the time to come in over or not; anyone else is refused and counted as a code tried (RelaySpotTries), or
|
|
57
|
+
* `open` would join any code untried.
|
|
58
|
+
*/
|
|
51
59
|
{
|
|
52
60
|
t: 'open';
|
|
61
|
+
k?: string;
|
|
53
62
|
}
|
|
54
63
|
/** First: comes in; refused when nobody opened it. */
|
|
55
64
|
| {
|
|
@@ -73,9 +82,12 @@ export type RelaySpotIn =
|
|
|
73
82
|
t: 'bye';
|
|
74
83
|
};
|
|
75
84
|
/** What the relay sends to one in a spot. */
|
|
76
|
-
export type RelaySpotOut =
|
|
85
|
+
export type RelaySpotOut =
|
|
86
|
+
/** `k`: the key of the spot, to who opened it. */
|
|
87
|
+
{
|
|
77
88
|
t: 'ready';
|
|
78
89
|
opened: number;
|
|
90
|
+
k?: string;
|
|
79
91
|
} | {
|
|
80
92
|
t: 'here';
|
|
81
93
|
b: {
|
package/esm/common/relay-spot.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RelayBytes } from './relay-bytes.js';
|
|
1
2
|
import { RELAY_PING, RELAY_PONG } from './relay-protocol.js';
|
|
2
3
|
/**
|
|
3
4
|
* A spot (docs/design/ta-nearby.md 2.2): the Ta that typed the same code of six digits, on networks that keep them
|
|
@@ -25,6 +26,8 @@ export var RelaySpotCloseCode;
|
|
|
25
26
|
RelaySpotCloseCode[RelaySpotCloseCode["FULL"] = 4429] = "FULL";
|
|
26
27
|
/** This address tried too many codes nobody opened: it waits (RelaySpotTries). */
|
|
27
28
|
RelaySpotCloseCode[RelaySpotCloseCode["TRIES"] = 4428] = "TRIES";
|
|
29
|
+
/** Opened already, by one whose key this one does not hold: a new code is needed. Counted as a failure. */
|
|
30
|
+
RelaySpotCloseCode[RelaySpotCloseCode["TAKEN"] = 4409] = "TAKEN";
|
|
28
31
|
})(RelaySpotCloseCode || (RelaySpotCloseCode = {}));
|
|
29
32
|
/**
|
|
30
33
|
* The codes nobody opened that one address tried lately (docs/design/ta-nearby.md 2.2): six digits are a million
|
|
@@ -123,7 +126,7 @@ export class RelaySpot {
|
|
|
123
126
|
}
|
|
124
127
|
}
|
|
125
128
|
enter(socket, message) {
|
|
126
|
-
var _a;
|
|
129
|
+
var _a, _b, _c, _d;
|
|
127
130
|
if (message.t !== 'open' && message.t !== 'join') {
|
|
128
131
|
socket.close(RelaySpotCloseCode.BAD, 'Open or join first.');
|
|
129
132
|
return;
|
|
@@ -131,19 +134,27 @@ export class RelaySpot {
|
|
|
131
134
|
const others = this.others(socket);
|
|
132
135
|
const now = this.now();
|
|
133
136
|
const opened = others.length ? Math.min(...others.map(other => { var _a; var _b; return (_b = (_a = other.who) === null || _a === void 0 ? void 0 : _a.opened) !== null && _b !== void 0 ? _b : now; })) : now;
|
|
137
|
+
const key = (_b = (_a = others.find(other => { var _a; return (_a = other.who) === null || _a === void 0 ? void 0 : _a.k; })) === null || _a === void 0 ? void 0 : _a.who) === null || _b === void 0 ? void 0 : _b.k;
|
|
138
|
+
const back = message.t === 'open' && !!key && !!message.k && message.k === key;
|
|
134
139
|
if (!others.length && message.t === 'join') {
|
|
135
|
-
(
|
|
140
|
+
(_c = this.missed) === null || _c === void 0 ? void 0 : _c.call(this, socket);
|
|
136
141
|
socket.close(RelaySpotCloseCode.NONE, 'No such spot.');
|
|
137
142
|
}
|
|
138
|
-
else if (
|
|
143
|
+
else if (others.length && message.t === 'open' && !back) {
|
|
144
|
+
(_d = this.missed) === null || _d === void 0 ? void 0 : _d.call(this, socket);
|
|
145
|
+
socket.close(RelaySpotCloseCode.TAKEN, 'Opened already.');
|
|
146
|
+
}
|
|
147
|
+
else if (!back && now - opened > RELAY_SPOT_JOIN_MS) {
|
|
139
148
|
socket.close(RelaySpotCloseCode.LATE, 'Too late to come in.');
|
|
140
149
|
}
|
|
141
150
|
else if (others.length >= RELAY_SPOT_MAX) {
|
|
142
151
|
socket.close(RelaySpotCloseCode.FULL, 'Full.');
|
|
143
152
|
}
|
|
144
153
|
else {
|
|
145
|
-
|
|
146
|
-
socket.
|
|
154
|
+
const k = key !== null && key !== void 0 ? key : RelayBytes.toBase64(RelayBytes.random(18));
|
|
155
|
+
socket.who = { opened, k };
|
|
156
|
+
const mine = message.t === 'open';
|
|
157
|
+
socket.send(JSON.stringify({ t: 'ready', opened, ...(mine ? { k } : {}) }));
|
|
147
158
|
}
|
|
148
159
|
}
|
|
149
160
|
/** The others in the spot. */
|
|
@@ -156,6 +167,7 @@ export class RelaySpot {
|
|
|
156
167
|
const id = (value) => typeof value === 'string' && value.length > 0 && value.length <= 64;
|
|
157
168
|
switch (data.t) {
|
|
158
169
|
case 'open':
|
|
170
|
+
return id(data.k) ? { t: 'open', k: data.k } : { t: 'open' };
|
|
159
171
|
case 'join':
|
|
160
172
|
case 'bye':
|
|
161
173
|
return { t: data.t };
|
package/esm/node/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './relay-file-storage.js';
|
|
|
3
3
|
export * from './relay-files-host.js';
|
|
4
4
|
export * from './relay-host.js';
|
|
5
5
|
export * from './relay-hub-files.js';
|
|
6
|
+
export * from './relay-memory-storage.js';
|
|
6
7
|
export * from './relay-server.js';
|
|
7
8
|
export * from './relay-store.js';
|
|
8
9
|
export * from './relay-viewer.js';
|
package/esm/node/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from './relay-file-storage.js';
|
|
|
3
3
|
export * from './relay-files-host.js';
|
|
4
4
|
export * from './relay-host.js';
|
|
5
5
|
export * from './relay-hub-files.js';
|
|
6
|
+
export * from './relay-memory-storage.js';
|
|
6
7
|
export * from './relay-server.js';
|
|
7
8
|
export * from './relay-store.js';
|
|
8
9
|
export * from './relay-viewer.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { RelayBlobStorage } from '../common/relay-blobs.js';
|
|
2
|
+
import type { RelayHubStorage } from '../common/relay-hub.js';
|
|
3
|
+
/** A copy of content in memory, deleted when it runs out. */
|
|
4
|
+
export declare class MemoryBlobStorage implements RelayBlobStorage {
|
|
5
|
+
protected readonly values: Map<string, unknown>;
|
|
6
|
+
protected timer?: ReturnType<typeof setTimeout>;
|
|
7
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
8
|
+
put(key: string, value: unknown): Promise<void>;
|
|
9
|
+
deleteAll(): Promise<void>;
|
|
10
|
+
/** No copy is kept: nothing of it need stay in the relay's memory. */
|
|
11
|
+
get empty(): boolean;
|
|
12
|
+
setAlarm(at: number): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
/** A room in memory, as a Durable Object keeps it. */
|
|
15
|
+
export declare class MemoryHubStorage implements RelayHubStorage {
|
|
16
|
+
protected readonly values: Map<string, unknown>;
|
|
17
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
18
|
+
put(key: string, value: unknown): Promise<void>;
|
|
19
|
+
delete(key: string): Promise<boolean>;
|
|
20
|
+
list<T>(options: {
|
|
21
|
+
prefix: string;
|
|
22
|
+
}): Promise<Map<string, T>>;
|
|
23
|
+
deleteAll(): Promise<void>;
|
|
24
|
+
/** No room was made, or it was closed. */
|
|
25
|
+
get empty(): boolean;
|
|
26
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** A copy of content in memory, deleted when it runs out. */
|
|
2
|
+
export class MemoryBlobStorage {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.values = new Map();
|
|
5
|
+
}
|
|
6
|
+
async get(key) {
|
|
7
|
+
return this.values.get(key);
|
|
8
|
+
}
|
|
9
|
+
async put(key, value) {
|
|
10
|
+
this.values.set(key, value);
|
|
11
|
+
}
|
|
12
|
+
async deleteAll() {
|
|
13
|
+
this.values.clear();
|
|
14
|
+
clearTimeout(this.timer);
|
|
15
|
+
}
|
|
16
|
+
/** No copy is kept: nothing of it need stay in the relay's memory. */
|
|
17
|
+
get empty() {
|
|
18
|
+
return this.values.size === 0;
|
|
19
|
+
}
|
|
20
|
+
async setAlarm(at) {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
clearTimeout(this.timer);
|
|
23
|
+
// Past the 24 days of a timer, it is found run out on the next read.
|
|
24
|
+
this.timer = setTimeout(() => this.values.clear(), Math.min(Math.max(at - Date.now(), 0), 2 ** 31 - 1));
|
|
25
|
+
(_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** A room in memory, as a Durable Object keeps it. */
|
|
29
|
+
export class MemoryHubStorage {
|
|
30
|
+
constructor() {
|
|
31
|
+
this.values = new Map();
|
|
32
|
+
}
|
|
33
|
+
async get(key) {
|
|
34
|
+
const value = this.values.get(key);
|
|
35
|
+
return value === undefined ? undefined : structuredClone(value);
|
|
36
|
+
}
|
|
37
|
+
async put(key, value) {
|
|
38
|
+
this.values.set(key, structuredClone(value));
|
|
39
|
+
}
|
|
40
|
+
async delete(key) {
|
|
41
|
+
return this.values.delete(key);
|
|
42
|
+
}
|
|
43
|
+
async list(options) {
|
|
44
|
+
const found = new Map();
|
|
45
|
+
for (const [key, value] of [...this.values].sort(([a], [b]) => a.localeCompare(b))) {
|
|
46
|
+
if (key.startsWith(options.prefix)) {
|
|
47
|
+
found.set(key, structuredClone(value));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return found;
|
|
51
|
+
}
|
|
52
|
+
async deleteAll() {
|
|
53
|
+
this.values.clear();
|
|
54
|
+
}
|
|
55
|
+
/** No room was made, or it was closed. */
|
|
56
|
+
get empty() {
|
|
57
|
+
return this.values.size === 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=relay-memory-storage.js.map
|