@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
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RelayHubEpochs } from './relay-hub-epochs.js';
|
|
1
2
|
/** How far a membership has come: one computer heard it end, it ended for both. */
|
|
2
3
|
const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
|
|
3
4
|
export var RelayHubsUtil;
|
|
@@ -34,7 +35,7 @@ export var RelayHubsUtil;
|
|
|
34
35
|
* key and the owner's keys once either has them, the invitations of both, made once either made it.
|
|
35
36
|
*/
|
|
36
37
|
function merge(local, other) {
|
|
37
|
-
var _a, _b, _c, _d, _e, _f;
|
|
38
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
38
39
|
const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
|
|
39
40
|
const behind = ahead === local ? other : local;
|
|
40
41
|
const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
|
|
@@ -51,7 +52,12 @@ export var RelayHubsUtil;
|
|
|
51
52
|
const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
|
|
52
53
|
const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
|
|
53
54
|
const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
|
|
55
|
+
// The keys of the epochs either has (docs/design/ta-link.md 8.7), the latest kept.
|
|
56
|
+
const epochs = RelayHubEpochs.kept({ ...behind.epochs, ...ahead.epochs, ...local.epochs, ...other.epochs });
|
|
57
|
+
const rosterV = Math.max((_g = local.rosterV) !== null && _g !== void 0 ? _g : 0, (_h = other.rosterV) !== null && _h !== void 0 ? _h : 0);
|
|
54
58
|
delete merged.key;
|
|
59
|
+
delete merged.epochs;
|
|
60
|
+
delete merged.rosterV;
|
|
55
61
|
delete merged.invites;
|
|
56
62
|
delete merged.joinedAt;
|
|
57
63
|
delete merged.identity;
|
|
@@ -63,6 +69,8 @@ export var RelayHubsUtil;
|
|
|
63
69
|
return {
|
|
64
70
|
...merged,
|
|
65
71
|
...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
|
|
72
|
+
...(key && merged.status === 'member' && Object.keys(epochs).length ? { epochs } : {}),
|
|
73
|
+
...(rosterV ? { rosterV } : {}),
|
|
66
74
|
...(invites.length ? { invites } : {}),
|
|
67
75
|
...(local.made || other.made ? { made: true } : {}),
|
|
68
76
|
...(joinedAt ? { joinedAt } : {}),
|
|
@@ -70,5 +78,19 @@ export var RelayHubsUtil;
|
|
|
70
78
|
};
|
|
71
79
|
}
|
|
72
80
|
RelayHubsUtil.merge = merge;
|
|
81
|
+
/**
|
|
82
|
+
* Whether a key of the state names another member than the one who set it (docs/design/ta-link.md 8.8): a key naming
|
|
83
|
+
* a member (`chat:<member>:<n>`, `agents:<member>`, a like of a post) is theirs alone; the owner of the room may only
|
|
84
|
+
* clear it. Keys naming nobody (a game's) are anyone's.
|
|
85
|
+
*/
|
|
86
|
+
function foreign(members, key, by, value) {
|
|
87
|
+
const parts = new Set(key.split(':'));
|
|
88
|
+
const named = members.find(member => parts.has(member.member));
|
|
89
|
+
if (!named || named.member === by) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return !(value === null && members.some(member => member.member === by && member.owner));
|
|
93
|
+
}
|
|
94
|
+
RelayHubsUtil.foreign = foreign;
|
|
73
95
|
})(RelayHubsUtil || (RelayHubsUtil = {}));
|
|
74
96
|
//# sourceMappingURL=relay-hubs-util.js.map
|
|
@@ -2,9 +2,9 @@ import { RelayFriendSends, type RelayFriendShare } from './relay-friend-sends.js
|
|
|
2
2
|
import { RelayFriends } from './relay-friends.js';
|
|
3
3
|
import { type RelayGroupInvite, RelayGroupInvites } from './relay-group-invites.js';
|
|
4
4
|
import { type RelayHubAskBody } from './relay-hub-util.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayRoomEvent, RelayRoomInfo, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
|
|
5
|
+
import { RelayHubsAdmins } from './relay-hubs-admins.js';
|
|
6
|
+
import { Live } from './relay-hubs-live.js';
|
|
7
|
+
import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayRoomEvent, RelayRoomInfo, RelayRoomRecord, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
|
|
8
8
|
import { type RelayPublicIdentity } from './relay-identity.js';
|
|
9
9
|
/**
|
|
10
10
|
* The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
|
|
@@ -12,7 +12,7 @@ import { type RelayPublicIdentity } from './relay-identity.js';
|
|
|
12
12
|
* the others; every message signed by its member and sealed with the key of the room. The friends are a room of
|
|
13
13
|
* their own (RelayFriends).
|
|
14
14
|
*/
|
|
15
|
-
export declare class RelayHubs extends
|
|
15
|
+
export declare class RelayHubs extends RelayHubsAdmins {
|
|
16
16
|
protected readonly book: RelayFriends;
|
|
17
17
|
protected readonly groups: RelayGroupInvites;
|
|
18
18
|
protected readonly sends: RelayFriendSends;
|
|
@@ -49,14 +49,7 @@ export declare class RelayHubs extends RelayHubsIdentity {
|
|
|
49
49
|
requests(): Promise<RelayRoomRequest[]>;
|
|
50
50
|
/** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
|
|
51
51
|
answer(roomId: string, askId: string, accept: boolean): Promise<void>;
|
|
52
|
-
/**
|
|
53
|
-
* The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
|
|
54
|
-
* RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
|
|
55
|
-
*/
|
|
56
|
-
configure(roomId: string, options: {
|
|
57
|
-
members?: number;
|
|
58
|
-
}): RelayRoomInfo;
|
|
59
|
-
/** The owner takes a member out: the relay closes it at once. */
|
|
52
|
+
/** The owner or an admin takes a member out: the relay closes it at once. An admin takes out neither the owner nor an admin. */
|
|
60
53
|
remove(roomId: string, member: string): Promise<void>;
|
|
61
54
|
/** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
|
|
62
55
|
leave(roomId: string): Promise<void>;
|
|
@@ -72,6 +65,12 @@ export declare class RelayHubs extends RelayHubsIdentity {
|
|
|
72
65
|
ok: boolean;
|
|
73
66
|
version: number;
|
|
74
67
|
}>;
|
|
68
|
+
/**
|
|
69
|
+
* Whether a key of the state is this Ta's own, which no other member may replace or clear at the relay: one that
|
|
70
|
+
* names its member id (`chat:<member>:<n>`, `agents:<member>`, a like of a post), and every key of a room of
|
|
71
|
+
* friends it owns (its posts, what it sends its friends). Any other is shared, as a game's.
|
|
72
|
+
*/
|
|
73
|
+
static owns(record: Pick<RelayRoomRecord, 'member' | 'kind' | 'own'>, name: string): boolean;
|
|
75
74
|
/** Friends of the user's other computer, with those of this Ta. */
|
|
76
75
|
mergeFriends(friends: RelayFriend[]): number;
|
|
77
76
|
/** The friends, with their keys: each in the room of the other (shares to friends are sealed for them). */
|
|
@@ -109,14 +108,12 @@ export declare class RelayHubs extends RelayHubsIdentity {
|
|
|
109
108
|
friendShares(): Promise<RelayFriendShare[]>;
|
|
110
109
|
/** The user opened one (`keep`) or let it go. */
|
|
111
110
|
answerFriendShare(id: string, keep: boolean): void;
|
|
112
|
-
protected isFriends(live: Live): boolean;
|
|
113
111
|
protected friendIds(): Set<string>;
|
|
114
112
|
protected heardAsk(live: Live, ask: RelayHubAskBody): void;
|
|
113
|
+
protected foreignSet(live: Live, key: string): void;
|
|
115
114
|
protected heardMembers(members: RelayPublicIdentity[]): void;
|
|
116
115
|
protected offered(live: Live, from: RelayPublicIdentity, link: string): void;
|
|
117
116
|
/** What changes the friends: rooms coming and going, members, the links friends leave to join back. */
|
|
118
117
|
protected after(event: RelayRoomEvent): void;
|
|
119
|
-
protected owned(roomId: string): Live;
|
|
120
|
-
protected member(roomId: string): Live;
|
|
121
118
|
protected info(live: Live): RelayRoomInfo;
|
|
122
119
|
}
|
package/esm/common/relay-hubs.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { RelayFriendSends } from './relay-friend-sends.js';
|
|
2
2
|
import { RELAY_FRIENDS_KIND, RelayFriends, RelayFriendsUtil } from './relay-friends.js';
|
|
3
3
|
import { RelayGroupInvites } from './relay-group-invites.js';
|
|
4
|
+
import { RelayHubCharterUtil } from './relay-hub-charter.js';
|
|
4
5
|
import { RelayHubUtil } from './relay-hub-util.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
6
|
+
import { RelayHubsAdmins } from './relay-hubs-admins.js';
|
|
7
|
+
import { Live, OPEN, READY_MS } from './relay-hubs-live.js';
|
|
7
8
|
import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
|
|
8
|
-
import {
|
|
9
|
+
import { RELAY_INVITE_MAX_DAYS, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
|
|
9
10
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
10
11
|
/**
|
|
11
12
|
* The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
|
|
@@ -13,7 +14,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
13
14
|
* the others; every message signed by its member and sealed with the key of the room. The friends are a room of
|
|
14
15
|
* their own (RelayFriends).
|
|
15
16
|
*/
|
|
16
|
-
export class RelayHubs extends
|
|
17
|
+
export class RelayHubs extends RelayHubsAdmins {
|
|
17
18
|
constructor() {
|
|
18
19
|
super(...arguments);
|
|
19
20
|
this.book = new RelayFriends({
|
|
@@ -40,6 +41,10 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
40
41
|
identity: () => this.identity(),
|
|
41
42
|
list: () => this.list(),
|
|
42
43
|
record: room => { var _a; return (_a = this.live.get(room)) === null || _a === void 0 ? void 0 : _a.record; },
|
|
44
|
+
manages: room => {
|
|
45
|
+
const live = this.live.get(room);
|
|
46
|
+
return !!live && this.manages(live);
|
|
47
|
+
},
|
|
43
48
|
save: () => this.save(),
|
|
44
49
|
friends: () => this.friends(),
|
|
45
50
|
friendRoom: () => this.friendRoom(),
|
|
@@ -142,14 +147,15 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
142
147
|
async invite(roomId, options = {}) {
|
|
143
148
|
var _a;
|
|
144
149
|
var _b, _c;
|
|
145
|
-
const live = this.
|
|
150
|
+
const live = this.managed(roomId);
|
|
146
151
|
const identity = await this.identityOf(live);
|
|
147
152
|
const linkOf = (invite) => ({
|
|
148
153
|
link: RelayHubUtil.link({
|
|
149
154
|
relay: live.record.relay,
|
|
150
155
|
room: live.record.id,
|
|
151
156
|
invite: invite.secret,
|
|
152
|
-
owner:
|
|
157
|
+
// The owner's, whoever invites: the one who asks checks the answer against it (an admin's by the charter).
|
|
158
|
+
owner: live.record.owner.id,
|
|
153
159
|
title: live.record.title,
|
|
154
160
|
by: identity.name,
|
|
155
161
|
}),
|
|
@@ -223,9 +229,9 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
223
229
|
}
|
|
224
230
|
/** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
|
|
225
231
|
async answer(roomId, askId, accept) {
|
|
226
|
-
var _a;
|
|
227
|
-
var
|
|
228
|
-
const live = this.
|
|
232
|
+
var _a, _b;
|
|
233
|
+
var _c;
|
|
234
|
+
const live = this.managed(roomId);
|
|
229
235
|
const asked = live.asks.get(askId);
|
|
230
236
|
if (!asked) {
|
|
231
237
|
throw new Error('Nobody asks that now.');
|
|
@@ -238,17 +244,21 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
238
244
|
const identity = await this.identityOf(live);
|
|
239
245
|
const body = {
|
|
240
246
|
key: live.record.key,
|
|
247
|
+
...(live.record.epochs && Object.keys(live.record.epochs).length ? { epochs: live.record.epochs } : {}),
|
|
241
248
|
kind: live.record.kind,
|
|
242
249
|
title: live.record.title,
|
|
243
250
|
...(live.record.view ? { view: live.record.view } : {}),
|
|
244
251
|
...(live.record.unlisted ? { unlisted: true } : {}),
|
|
245
252
|
};
|
|
246
253
|
const ask = asked.body;
|
|
247
|
-
|
|
248
|
-
if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= most) {
|
|
254
|
+
if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= this.capacityOf(live)) {
|
|
249
255
|
throw new Error('The room is full.');
|
|
250
256
|
}
|
|
251
|
-
const
|
|
257
|
+
const head = live.record.owner;
|
|
258
|
+
const proof = !live.record.own && ((_b = live.roster) === null || _b === void 0 ? void 0 : _b.charter) && head.sign && head.box
|
|
259
|
+
? RelayHubCharterUtil.answerProof({ ...head, sign: head.sign, box: head.box }, live.roster.charter)
|
|
260
|
+
: undefined;
|
|
261
|
+
const answer = await RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite, proof);
|
|
252
262
|
const member = { ...RelayIdentityUtil.publicOf(ask), member: ask.member, ...(ask.guest ? { guest: true } : {}), at: Date.now() };
|
|
253
263
|
const roster = await this.nextRoster(live, members => [...members.filter(other => other.member !== ask.member), member]);
|
|
254
264
|
this.request(live, { t: 'admit', ask: askId, answer, roster });
|
|
@@ -256,35 +266,20 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
256
266
|
live.asks.delete(askId);
|
|
257
267
|
this.emit({ type: 'rooms' });
|
|
258
268
|
}
|
|
259
|
-
/**
|
|
260
|
-
* The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
|
|
261
|
-
* RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
|
|
262
|
-
*/
|
|
263
|
-
configure(roomId, options) {
|
|
264
|
-
const live = this.owned(roomId);
|
|
265
|
-
if (options.members !== undefined) {
|
|
266
|
-
const members = Math.round(Number(options.members));
|
|
267
|
-
if (!(members >= 2 && members <= RELAY_ROOM_CAPACITY_MAX)) {
|
|
268
|
-
throw new Error(`A room takes 2 to ${RELAY_ROOM_CAPACITY_MAX} members.`);
|
|
269
|
-
}
|
|
270
|
-
if (members === RELAY_ROOM_MAX_MEMBERS) {
|
|
271
|
-
delete live.record.capacity;
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
274
|
-
live.record.capacity = members;
|
|
275
|
-
}
|
|
276
|
-
this.save();
|
|
277
|
-
}
|
|
278
|
-
return this.info(live);
|
|
279
|
-
}
|
|
280
|
-
/** The owner takes a member out: the relay closes it at once. */
|
|
269
|
+
/** The owner or an admin takes a member out: the relay closes it at once. An admin takes out neither the owner nor an admin. */
|
|
281
270
|
async remove(roomId, member) {
|
|
282
|
-
|
|
271
|
+
var _a, _b;
|
|
272
|
+
const live = this.managed(roomId);
|
|
283
273
|
if (member === live.record.member) {
|
|
284
274
|
throw new Error('The owner leaves by closing the room.');
|
|
285
275
|
}
|
|
276
|
+
const owner = (_b = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.find(other => other.owner)) === null || _b === void 0 ? void 0 : _b.member;
|
|
277
|
+
if (!live.record.own && (member === owner || this.isAdmin(live, member))) {
|
|
278
|
+
throw new Error('Only the owner of the room does that.');
|
|
279
|
+
}
|
|
286
280
|
await this.ready(live);
|
|
287
|
-
|
|
281
|
+
// A new key for those left: the one taken out holds those of before alone (docs/design/ta-link.md 8.7).
|
|
282
|
+
const roster = await this.nextRoster(live, members => members.filter(other => other.member !== member), undefined, true);
|
|
288
283
|
this.request(live, { t: 'remove', member, roster });
|
|
289
284
|
}
|
|
290
285
|
/** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
|
|
@@ -329,7 +324,7 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
329
324
|
await this.ready(live);
|
|
330
325
|
const identity = await this.identityOf(live);
|
|
331
326
|
const keys = live.keys;
|
|
332
|
-
const d = await RelayHubUtil.
|
|
327
|
+
const d = await RelayHubUtil.sealIn(keys, identity, live.record.member, { key: name, value: value !== null && value !== void 0 ? value : null });
|
|
333
328
|
const k = await RelayHubUtil.keyId(keys, name);
|
|
334
329
|
const id = this.nextId++;
|
|
335
330
|
const result = await new Promise((resolve, reject) => {
|
|
@@ -342,7 +337,14 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
342
337
|
resolve(answer);
|
|
343
338
|
});
|
|
344
339
|
try {
|
|
345
|
-
this.request(live, {
|
|
340
|
+
this.request(live, {
|
|
341
|
+
t: 'set',
|
|
342
|
+
id,
|
|
343
|
+
k,
|
|
344
|
+
d,
|
|
345
|
+
...(version === undefined ? {} : { v: version }),
|
|
346
|
+
...(RelayHubs.owns(live.record, name) ? { own: true } : {}),
|
|
347
|
+
});
|
|
346
348
|
}
|
|
347
349
|
catch (error) {
|
|
348
350
|
clearTimeout(timer);
|
|
@@ -355,7 +357,9 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
355
357
|
? 'The value is too large.'
|
|
356
358
|
: result.error === 'limit'
|
|
357
359
|
? 'The shared state of the room is full.'
|
|
358
|
-
:
|
|
360
|
+
: result.error === 'owned'
|
|
361
|
+
? 'Another member keeps this key: only they or the owner of the room change it.'
|
|
362
|
+
: 'The relay refused it.');
|
|
359
363
|
}
|
|
360
364
|
if (result.ok) {
|
|
361
365
|
live.state.set(name, { value: value !== null && value !== void 0 ? value : null, version: result.v, by: live.record.member });
|
|
@@ -363,6 +367,14 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
363
367
|
}
|
|
364
368
|
return { ok: result.ok, version: result.v };
|
|
365
369
|
}
|
|
370
|
+
/**
|
|
371
|
+
* Whether a key of the state is this Ta's own, which no other member may replace or clear at the relay: one that
|
|
372
|
+
* names its member id (`chat:<member>:<n>`, `agents:<member>`, a like of a post), and every key of a room of
|
|
373
|
+
* friends it owns (its posts, what it sends its friends). Any other is shared, as a game's.
|
|
374
|
+
*/
|
|
375
|
+
static owns(record, name) {
|
|
376
|
+
return name.split(':').includes(record.member) || (record.own && RelayFriendsUtil.isKind(record.kind));
|
|
377
|
+
}
|
|
366
378
|
/** Friends of the user's other computer, with those of this Ta. */
|
|
367
379
|
mergeFriends(friends) {
|
|
368
380
|
return this.book.merge(friends);
|
|
@@ -427,17 +439,15 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
427
439
|
this.sends.answer(id, keep);
|
|
428
440
|
this.emit({ type: 'rooms' });
|
|
429
441
|
}
|
|
430
|
-
isFriends(live) {
|
|
431
|
-
return live.record.own && live.record.kind === RELAY_FRIENDS_KIND;
|
|
432
|
-
}
|
|
433
442
|
friendIds() {
|
|
434
443
|
return this.book.ids();
|
|
435
444
|
}
|
|
436
445
|
heardAsk(live, ask) {
|
|
446
|
+
var _a;
|
|
437
447
|
if (this.isFriends(live)) {
|
|
438
448
|
this.book.asked(ask);
|
|
439
449
|
}
|
|
440
|
-
else if (this.groups.admits(live.record, ask)) {
|
|
450
|
+
else if (!((_a = live.charter) === null || _a === void 0 ? void 0 : _a.rules.approve) && this.groups.admits(live.record, ask)) {
|
|
441
451
|
// A friend the user invited: in at once, as the user chose it; asked as any other should that fail.
|
|
442
452
|
void this.answer(live.record.id, ask.member, true).then(() => this.groups.admitted(live.record, ask.id), () => super.heardAsk(live, ask));
|
|
443
453
|
}
|
|
@@ -445,6 +455,10 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
445
455
|
super.heardAsk(live, ask);
|
|
446
456
|
}
|
|
447
457
|
}
|
|
458
|
+
foreignSet(live, key) {
|
|
459
|
+
// Not awaited: the owner clears what one set under another's name, as soon as it is in the room.
|
|
460
|
+
void this.setState(live.record.id, key, null).catch(() => undefined);
|
|
461
|
+
}
|
|
448
462
|
heardMembers(members) {
|
|
449
463
|
if (!this.options.guest) {
|
|
450
464
|
this.book.heard(members);
|
|
@@ -467,20 +481,6 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
467
481
|
this.emit({ type: 'rooms' });
|
|
468
482
|
}
|
|
469
483
|
}
|
|
470
|
-
owned(roomId) {
|
|
471
|
-
const live = this.member(roomId);
|
|
472
|
-
if (!live.record.own) {
|
|
473
|
-
throw new Error('Only the owner of the room does that.');
|
|
474
|
-
}
|
|
475
|
-
return live;
|
|
476
|
-
}
|
|
477
|
-
member(roomId) {
|
|
478
|
-
const live = this.live.get(roomId);
|
|
479
|
-
if ((live === null || live === void 0 ? void 0 : live.record.status) !== 'member') {
|
|
480
|
-
throw new Error('Not in that room.');
|
|
481
|
-
}
|
|
482
|
-
return live;
|
|
483
|
-
}
|
|
484
484
|
info(live) {
|
|
485
485
|
var _a, _b;
|
|
486
486
|
var _c, _d;
|
|
@@ -491,6 +491,7 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
491
491
|
name: member.name,
|
|
492
492
|
...(RelayIdentityUtil.isLook(member.look) ? { look: member.look } : {}),
|
|
493
493
|
owner: !!member.owner,
|
|
494
|
+
...(this.isAdmin(live, member.member) ? { admin: true } : {}),
|
|
494
495
|
online: member.member === record.member ? ((_a = live.socket) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN && live.synced : live.online.has(member.member),
|
|
495
496
|
self: member.member === record.member,
|
|
496
497
|
...(member.guest ? { guest: true } : {}),
|
|
@@ -503,7 +504,9 @@ export class RelayHubs extends RelayHubsIdentity {
|
|
|
503
504
|
...(record.unlisted ? { unlisted: true } : {}),
|
|
504
505
|
relay: record.relay,
|
|
505
506
|
owner: record.own,
|
|
507
|
+
...(!record.own && this.isAdmin(live, record.member) ? { admin: true } : {}),
|
|
506
508
|
...(record.own ? { capacity: (_d = record.capacity) !== null && _d !== void 0 ? _d : RELAY_ROOM_MAX_MEMBERS } : {}),
|
|
509
|
+
...(live.charter && Object.keys(live.charter.rules).length ? { rules: { ...live.charter.rules } } : {}),
|
|
507
510
|
ownerName: record.owner.name,
|
|
508
511
|
status: record.status,
|
|
509
512
|
...(record.reason ? { reason: record.reason } : {}),
|
|
@@ -186,7 +186,7 @@ export declare const RELAY_HUB_MAX_VALUE: number;
|
|
|
186
186
|
export declare const RELAY_HUB_MAX_KEYS = 4096;
|
|
187
187
|
/** Characters of all of the state, sealed. */
|
|
188
188
|
export declare const RELAY_HUB_MAX_STATE: number;
|
|
189
|
-
/** Asks to join waiting for the owner, and for how long. */
|
|
189
|
+
/** Asks to join waiting for the owner or an admin, and for how long. */
|
|
190
190
|
export declare const RELAY_HUB_MAX_ASKS = 20;
|
|
191
191
|
/**
|
|
192
192
|
* The key of the state of a room a member keeps its picture under (RelayIdentity.picture), its member id after it: each
|
|
@@ -200,10 +200,15 @@ export declare const RELAY_HUB_ASK_DAYS = 7;
|
|
|
200
200
|
export declare const RELAY_HUB_IDLE_DAYS = 90;
|
|
201
201
|
/** Messages of a connection in a second. */
|
|
202
202
|
export declare const RELAY_HUB_RATE = 40;
|
|
203
|
-
/**
|
|
203
|
+
/**
|
|
204
|
+
* The sealed list of the members of a room, signed by its owner (or an admin); `v` grows with each change. After a
|
|
205
|
+
* member was taken out or left, `k` holds the keys of the room's epochs sealed for each member left
|
|
206
|
+
* (docs/design/ta-link.md 8.7), by member id.
|
|
207
|
+
*/
|
|
204
208
|
export interface RelayHubRoster {
|
|
205
209
|
v: number;
|
|
206
210
|
d: string;
|
|
211
|
+
k?: Record<string, string>;
|
|
207
212
|
}
|
|
208
213
|
/** A key of the shared state: `k` the HMAC of its name, `v` its version (from 1), `d` sealed, `by` the member who wrote it last. */
|
|
209
214
|
export interface RelayHubEntry {
|
|
@@ -211,6 +216,8 @@ export interface RelayHubEntry {
|
|
|
211
216
|
v: number;
|
|
212
217
|
d: string;
|
|
213
218
|
by: string;
|
|
219
|
+
/** Its member's own: only they, or the owner of the room, set it again or clear it while they are a member. */
|
|
220
|
+
own?: true;
|
|
214
221
|
}
|
|
215
222
|
/** An ask to join, for the owner: `d` sealed with the key of the invitation. */
|
|
216
223
|
export interface RelayHubAsk {
|
|
@@ -251,10 +258,14 @@ export type RelayHubRequest = {
|
|
|
251
258
|
k: string;
|
|
252
259
|
d: string;
|
|
253
260
|
v?: number;
|
|
261
|
+
own?: true;
|
|
254
262
|
} | {
|
|
255
263
|
t: 'invite';
|
|
256
264
|
hash: string;
|
|
257
265
|
expiresAt: number;
|
|
266
|
+
} | {
|
|
267
|
+
t: 'admins';
|
|
268
|
+
admins: string[];
|
|
258
269
|
} | {
|
|
259
270
|
t: 'admit';
|
|
260
271
|
ask: string;
|
|
@@ -276,7 +287,7 @@ export type RelayHubRequest = {
|
|
|
276
287
|
t: 'close';
|
|
277
288
|
};
|
|
278
289
|
/** Why the hub refused: `auth` not a member, `gone` no such room, `invite` the invitation is wrong or ran out. */
|
|
279
|
-
export type RelayHubFailure = 'auth' | 'gone' | 'invite' | 'full' | 'large' | 'limit' | 'rate' | 'owner' | 'exists' | 'invalid';
|
|
290
|
+
export type RelayHubFailure = 'auth' | 'gone' | 'invite' | 'full' | 'large' | 'limit' | 'rate' | 'owner' | 'exists' | 'invalid' | 'owned';
|
|
280
291
|
/**
|
|
281
292
|
* What a hub sends. A member is first `welcome`d (who is online, the roster, asks for the owner), then given the
|
|
282
293
|
* state in `entries` and `synced`.
|
|
@@ -119,7 +119,7 @@ export const RELAY_HUB_MAX_VALUE = 96 * 1024;
|
|
|
119
119
|
export const RELAY_HUB_MAX_KEYS = 4096;
|
|
120
120
|
/** Characters of all of the state, sealed. */
|
|
121
121
|
export const RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
|
|
122
|
-
/** Asks to join waiting for the owner, and for how long. */
|
|
122
|
+
/** Asks to join waiting for the owner or an admin, and for how long. */
|
|
123
123
|
export const RELAY_HUB_MAX_ASKS = 20;
|
|
124
124
|
/**
|
|
125
125
|
* The key of the state of a room a member keeps its picture under (RelayIdentity.picture), its member id after it: each
|
|
@@ -36,6 +36,11 @@ export interface RelayChatLine {
|
|
|
36
36
|
* by `<member>/<agent id>`, their member's Ta answering.
|
|
37
37
|
*/
|
|
38
38
|
to?: string[];
|
|
39
|
+
/**
|
|
40
|
+
* `to` names the members themselves with @, not their doubles (docs/design/ta-link.md 7): each one's Ta tells them, and
|
|
41
|
+
* answers for them as far as they set it to (their double at once, once they are away, a draft for them to send).
|
|
42
|
+
*/
|
|
43
|
+
person?: boolean;
|
|
39
44
|
/** The id of the line it answers. */
|
|
40
45
|
re?: string;
|
|
41
46
|
/**
|
|
@@ -50,7 +55,7 @@ export interface RelayChatAgent {
|
|
|
50
55
|
name: string;
|
|
51
56
|
}
|
|
52
57
|
/** What a line adds to its text: who said it and to whom (RelayRoomChat.say). */
|
|
53
|
-
export type RelayChatLineExtra = Pick<RelayChatLine, 'double' | 'agent' | 'to' | 're' | 'task'>;
|
|
58
|
+
export type RelayChatLineExtra = Pick<RelayChatLine, 'double' | 'agent' | 'to' | 'person' | 're' | 'task'>;
|
|
54
59
|
/** What is kept of a line: `from` is the key's. */
|
|
55
60
|
type StoredLine = Omit<RelayChatLine, 'from'>;
|
|
56
61
|
/** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
|
|
@@ -41,6 +41,7 @@ export var RelayRoomChatUtil;
|
|
|
41
41
|
? { agent: { id: agent.id.slice(0, 64), name: agent.name.slice(0, 64) } }
|
|
42
42
|
: {}),
|
|
43
43
|
...(to.length ? { to } : {}),
|
|
44
|
+
...(line.person === true && to.length && line.double !== true && !line.agent ? { person: true } : {}),
|
|
44
45
|
...(typeof line.re === 'string' ? { re: line.re } : {}),
|
|
45
46
|
...(line.task === true && to.length ? { task: true } : {}),
|
|
46
47
|
};
|
|
@@ -7,7 +7,7 @@ import { type RelayAccountProvider, RelayAccounts, type RelayAccountsOptions } f
|
|
|
7
7
|
export declare class RelayServerAccounts {
|
|
8
8
|
protected readonly accounts: RelayAccounts;
|
|
9
9
|
/** `authenticate`: a gateway's, whose sign-in gives the tokens (RelayAccountsOptions.authenticate). */
|
|
10
|
-
constructor(data: string | undefined, options?: Pick<RelayAccountsOptions, 'authenticate'> & {
|
|
10
|
+
constructor(data: string | undefined, options?: Pick<RelayAccountsOptions, 'authenticate' | 'issue'> & {
|
|
11
11
|
providers?: RelayAccountProvider[];
|
|
12
12
|
});
|
|
13
13
|
/** Answers a request of RELAY_ACCOUNT_PATH; never throws, an unhandled rejection would end the relay. */
|
|
@@ -15,7 +15,7 @@ export class RelayServerAccounts {
|
|
|
15
15
|
var _a;
|
|
16
16
|
const books = new RelayAccountBooks(account => data ? new FileRelayStorage(join(data, 'accounts', RelayBytes.toBase64(new TextEncoder().encode(account)))) : new MemoryHubStorage());
|
|
17
17
|
const providers = (_a = options.providers) !== null && _a !== void 0 ? _a : (options.authenticate ? [] : [new RelayGitHubAccounts()]);
|
|
18
|
-
this.accounts = new RelayAccounts({ book: account => books.book(account), providers, authenticate: options.authenticate });
|
|
18
|
+
this.accounts = new RelayAccounts({ book: account => books.book(account), providers, authenticate: options.authenticate, issue: options.issue });
|
|
19
19
|
}
|
|
20
20
|
/** Answers a request of RELAY_ACCOUNT_PATH; never throws, an unhandled rejection would end the relay. */
|
|
21
21
|
serve(request, response) {
|
package/lib/common/index.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export * from './relay-friends';
|
|
|
14
14
|
export * from './relay-group-invites';
|
|
15
15
|
export * from './relay-host-calls';
|
|
16
16
|
export * from './relay-hub';
|
|
17
|
+
export * from './relay-hub-charter';
|
|
17
18
|
export * from './relay-hub-util';
|
|
18
19
|
export * from './relay-hubs';
|
|
19
20
|
export * from './relay-hubs-protocol';
|
package/lib/common/index.js
CHANGED
|
@@ -30,6 +30,7 @@ __exportStar(require("./relay-friends"), exports);
|
|
|
30
30
|
__exportStar(require("./relay-group-invites"), exports);
|
|
31
31
|
__exportStar(require("./relay-host-calls"), exports);
|
|
32
32
|
__exportStar(require("./relay-hub"), exports);
|
|
33
|
+
__exportStar(require("./relay-hub-charter"), exports);
|
|
33
34
|
__exportStar(require("./relay-hub-util"), exports);
|
|
34
35
|
__exportStar(require("./relay-hubs"), exports);
|
|
35
36
|
__exportStar(require("./relay-hubs-protocol"), exports);
|
|
@@ -133,6 +133,10 @@ export declare class AccountAgent {
|
|
|
133
133
|
approve(id: string): Promise<AccountView>;
|
|
134
134
|
/** Removes a device (one waiting that is not the user's, or one lost): every device drops it. */
|
|
135
135
|
remove(id: string): Promise<AccountView>;
|
|
136
|
+
/** A token of the account for a device the user brings in by hand (a computer whose code they scanned). */
|
|
137
|
+
invite(): Promise<{
|
|
138
|
+
token: string;
|
|
139
|
+
}>;
|
|
136
140
|
/** Leaves the account: removed from it, its token dropped. */
|
|
137
141
|
signOut(): Promise<void>;
|
|
138
142
|
}
|
|
@@ -129,7 +129,7 @@ class AccountAgent {
|
|
|
129
129
|
/** A new device of `account`: its own keys, trusting itself alone. */
|
|
130
130
|
static async create(service, token, account, kind, name) {
|
|
131
131
|
const self = await relay_identity_1.RelayIdentityUtil.create(name);
|
|
132
|
-
return { service: service.replace(/\/+$/, ''), token, account, kind, self, circle: relay_account_1.AccountCircle.alone(self), pairings: {} };
|
|
132
|
+
return { service: service.replace(/\/+$/, ''), token, account, kind, self, circle: relay_account_1.AccountCircle.alone(self, kind), pairings: {} };
|
|
133
133
|
}
|
|
134
134
|
get state() {
|
|
135
135
|
return this.kept;
|
|
@@ -213,12 +213,14 @@ class AccountAgent {
|
|
|
213
213
|
if (this.hooks.pair && this.hooks.paired) {
|
|
214
214
|
const paired = new Set(this.hooks.paired());
|
|
215
215
|
for (const id of paired) {
|
|
216
|
-
|
|
216
|
+
const device = trusted.find(one => one.id === id);
|
|
217
|
+
if (!ids.has(id) || (device && relay_account_1.AccountCircle.cloud(this.kept.circle, device))) {
|
|
217
218
|
await ((_b = (_a = this.hooks).unpair) === null || _b === void 0 ? void 0 : _b.call(_a, id));
|
|
218
219
|
}
|
|
219
220
|
}
|
|
220
221
|
for (const device of trusted) {
|
|
221
|
-
|
|
222
|
+
// A Ta in the cloud gets no way into a computer: devices open it, it does not reach them.
|
|
223
|
+
if (!paired.has(device.id) && !relay_account_1.AccountCircle.cloud(this.kept.circle, device)) {
|
|
222
224
|
const link = await this.hooks.pair(device);
|
|
223
225
|
await this.calls.send(await relay_account_1.AccountCircle.seal(this.kept.self, device, 'pairing', { link }));
|
|
224
226
|
}
|
|
@@ -312,6 +314,10 @@ class AccountAgent {
|
|
|
312
314
|
/** Lets a waiting device in: signs it, and sends it whom this device trusts. */
|
|
313
315
|
async approve(id) {
|
|
314
316
|
return this.guard(async () => {
|
|
317
|
+
if (this.kept.kind === 'cloud') {
|
|
318
|
+
// Not 403: that would sign this device out (guard).
|
|
319
|
+
throw new AccountCallError(409, 'A Ta in the cloud lets no device in.');
|
|
320
|
+
}
|
|
315
321
|
const roster = await this.calls.roster(this.kept.self.id);
|
|
316
322
|
const device = roster.devices.find(one => one.id === id);
|
|
317
323
|
if (!device || !relay_account_1.AccountCircle.waiting(device)) {
|
|
@@ -330,6 +336,10 @@ class AccountAgent {
|
|
|
330
336
|
return this.sync();
|
|
331
337
|
});
|
|
332
338
|
}
|
|
339
|
+
/** A token of the account for a device the user brings in by hand (a computer whose code they scanned). */
|
|
340
|
+
invite() {
|
|
341
|
+
return this.guard(() => this.calls.invite());
|
|
342
|
+
}
|
|
333
343
|
/** Leaves the account: removed from it, its token dropped. */
|
|
334
344
|
async signOut() {
|
|
335
345
|
const { self, account } = this.kept;
|
|
@@ -5,7 +5,11 @@ export declare const RELAY_ACCOUNT_PATH = "/v1/account/";
|
|
|
5
5
|
export declare const RELAY_ACCOUNT_MAX_DEVICES = 20;
|
|
6
6
|
/** A device seen within this is online: computers say they are there every minute. */
|
|
7
7
|
export declare const RELAY_ACCOUNT_ONLINE_MS: number;
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* `cloud`: a Ta in the cloud, in a sandbox of the gateway (docs/design/ta-me.md 2). The devices of the account open it,
|
|
10
|
+
* but it lets no device in and gets no way into a computer: whoever runs the gateway runs it.
|
|
11
|
+
*/
|
|
12
|
+
export type AccountDeviceKind = 'computer' | 'phone' | 'browser' | 'cloud';
|
|
9
13
|
export interface AccountName {
|
|
10
14
|
/** `github:<id>`, `iam:<user>`: the provider and who the user is there. */
|
|
11
15
|
id: string;
|
|
@@ -73,6 +77,13 @@ export interface AccountCalls {
|
|
|
73
77
|
send(mail: Omit<AccountMail, 'at'>): Promise<void>;
|
|
74
78
|
mail(to: string): Promise<AccountMail[]>;
|
|
75
79
|
signOut(): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* A token of the account for another device the user brings in by hand (a computer whose code they scanned): it
|
|
82
|
+
* joins with it and waits to be let in, as any device signing in.
|
|
83
|
+
*/
|
|
84
|
+
invite(): Promise<{
|
|
85
|
+
token: string;
|
|
86
|
+
}>;
|
|
76
87
|
}
|
|
77
88
|
export type AccountCallName = keyof AccountCalls;
|
|
78
89
|
export declare const ACCOUNT_CALLS: readonly AccountCallName[];
|
|
@@ -96,9 +107,11 @@ export type AccountSignInPoll = {
|
|
|
96
107
|
};
|
|
97
108
|
/** What a device keeps of the devices it trusts (docs/design/ta-account.md 5). */
|
|
98
109
|
export interface AccountCircleState {
|
|
110
|
+
/** `kind` as it was let in with (signed): a Ta in the cloud lets no one in. None from a device of before. */
|
|
99
111
|
trusted: Record<string, {
|
|
100
112
|
sign: string;
|
|
101
113
|
box: string;
|
|
114
|
+
kind?: AccountDeviceKind;
|
|
102
115
|
}>;
|
|
103
116
|
/** Removed once: never trusted again, whatever the service says later. */
|
|
104
117
|
removed: string[];
|
|
@@ -115,7 +128,9 @@ export declare namespace AccountCircle {
|
|
|
115
128
|
/** Waiting for a device of the account to let it in. */
|
|
116
129
|
function waiting(device: AccountDevice): boolean;
|
|
117
130
|
/** A device trusting itself alone: the first one, or one not let in yet. */
|
|
118
|
-
function alone(self: Pick<AccountDevice, 'id' | 'sign' | 'box'
|
|
131
|
+
function alone(self: Pick<AccountDevice, 'id' | 'sign' | 'box'>, kind?: AccountDeviceKind): AccountCircleState;
|
|
132
|
+
/** A Ta in the cloud, as trusted: its kind as it was let in with, else as the service says. */
|
|
133
|
+
function cloud(state: AccountCircleState, device: Pick<AccountDevice, 'id' | 'kind'>): boolean;
|
|
119
134
|
/**
|
|
120
135
|
* Who to trust now: those trusted before and not removed, and those a trusted device let in with a good signature,
|
|
121
136
|
* until no more. A device the service adds without a signature of one of them is never trusted.
|