@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/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export * from './relay-hub.js';
|
|
|
13
13
|
export * from './relay-hub-util.js';
|
|
14
14
|
export * from './relay-hubs.js';
|
|
15
15
|
export * from './relay-hubs-protocol.js';
|
|
16
|
+
export * from './relay-hubs-util.js';
|
|
16
17
|
export * from './relay-identity.js';
|
|
17
18
|
export * from './relay-info.js';
|
|
18
19
|
export * from './relay-pairing.js';
|
package/esm/common/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export * from './relay-hub.js';
|
|
|
13
13
|
export * from './relay-hub-util.js';
|
|
14
14
|
export * from './relay-hubs.js';
|
|
15
15
|
export * from './relay-hubs-protocol.js';
|
|
16
|
+
export * from './relay-hubs-util.js';
|
|
16
17
|
export * from './relay-identity.js';
|
|
17
18
|
export * from './relay-info.js';
|
|
18
19
|
export * from './relay-pairing.js';
|
|
@@ -102,3 +102,16 @@ export declare namespace RelayContentUtil {
|
|
|
102
102
|
*/
|
|
103
103
|
function fetch(text: string | RelayContentLink, options?: RelayContentFetchOptions): Promise<RelayContentItem>;
|
|
104
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* What the viewer of the relay opened, handed to Ta's web page of the same relay (RELAY_WEB_PATH), which draws a
|
|
107
|
+
* conversation as Ta does: kept in the session of the tab, by the share, for the page that comes next and for it
|
|
108
|
+
* loaded again; the password alone when the whole is too large to keep, so that it is not asked again.
|
|
109
|
+
*/
|
|
110
|
+
export declare namespace RelayContentHandoff {
|
|
111
|
+
function keep(share: string, item: RelayContentItem, password?: string): void;
|
|
112
|
+
/** What was kept for `share`: the content opened, or its password. */
|
|
113
|
+
function take(share: string): {
|
|
114
|
+
item?: RelayContentItem;
|
|
115
|
+
password?: string;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
@@ -313,4 +313,46 @@ export var RelayContentUtil;
|
|
|
313
313
|
return new Uint8Array(bits);
|
|
314
314
|
}
|
|
315
315
|
})(RelayContentUtil || (RelayContentUtil = {}));
|
|
316
|
+
/**
|
|
317
|
+
* What the viewer of the relay opened, handed to Ta's web page of the same relay (RELAY_WEB_PATH), which draws a
|
|
318
|
+
* conversation as Ta does: kept in the session of the tab, by the share, for the page that comes next and for it
|
|
319
|
+
* loaded again; the password alone when the whole is too large to keep, so that it is not asked again.
|
|
320
|
+
*/
|
|
321
|
+
export var RelayContentHandoff;
|
|
322
|
+
(function (RelayContentHandoff) {
|
|
323
|
+
const KEY = 'ta.shared';
|
|
324
|
+
function keep(share, item, password) {
|
|
325
|
+
const { data, ...header } = item;
|
|
326
|
+
const store = (kept) => { var _a; return (_a = globalThis.sessionStorage) === null || _a === void 0 ? void 0 : _a.setItem(KEY, JSON.stringify(kept)); };
|
|
327
|
+
try {
|
|
328
|
+
store({ share, ...(password === undefined ? {} : { password }), item: { ...header, data: RelayBytes.toBase64(data) } });
|
|
329
|
+
}
|
|
330
|
+
catch (_a) {
|
|
331
|
+
try {
|
|
332
|
+
store({ share, ...(password === undefined ? {} : { password }) });
|
|
333
|
+
}
|
|
334
|
+
catch (_b) {
|
|
335
|
+
// Kept nowhere: the page asks again.
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
RelayContentHandoff.keep = keep;
|
|
340
|
+
/** What was kept for `share`: the content opened, or its password. */
|
|
341
|
+
function take(share) {
|
|
342
|
+
var _a;
|
|
343
|
+
var _b;
|
|
344
|
+
try {
|
|
345
|
+
const kept = JSON.parse((_b = (_a = globalThis.sessionStorage) === null || _a === void 0 ? void 0 : _a.getItem(KEY)) !== null && _b !== void 0 ? _b : 'null');
|
|
346
|
+
if ((kept === null || kept === void 0 ? void 0 : kept.share) !== share) {
|
|
347
|
+
return {};
|
|
348
|
+
}
|
|
349
|
+
const item = kept.item && { ...kept.item, data: RelayBytes.fromBase64(kept.item.data) };
|
|
350
|
+
return { ...(item ? { item } : {}), ...(kept.password === undefined ? {} : { password: kept.password }) };
|
|
351
|
+
}
|
|
352
|
+
catch (_c) {
|
|
353
|
+
return {};
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
RelayContentHandoff.take = take;
|
|
357
|
+
})(RelayContentHandoff || (RelayContentHandoff = {}));
|
|
316
358
|
//# sourceMappingURL=relay-content.js.map
|
|
@@ -27,6 +27,15 @@ export type RelayConversationMessage = {
|
|
|
27
27
|
role: 'user';
|
|
28
28
|
content: string | RelayConversationPart[];
|
|
29
29
|
timestamp?: number;
|
|
30
|
+
}
|
|
31
|
+
/** What an extension showed in the conversation (pi's custom message with `display`): its card, drawn by its renderer. */
|
|
32
|
+
| {
|
|
33
|
+
role: 'custom';
|
|
34
|
+
customType: string;
|
|
35
|
+
content: string | RelayConversationPart[];
|
|
36
|
+
display: true;
|
|
37
|
+
details?: unknown;
|
|
38
|
+
timestamp?: number;
|
|
30
39
|
} | {
|
|
31
40
|
role: 'assistant';
|
|
32
41
|
content: RelayConversationPart[];
|
|
@@ -49,6 +58,11 @@ export interface RelayConversation {
|
|
|
49
58
|
/** The name of its directory, for a line under the title. */
|
|
50
59
|
place?: string;
|
|
51
60
|
messages: RelayConversationMessage[];
|
|
61
|
+
/**
|
|
62
|
+
* The pictures of the workspace its replies show (``), as data URLs by the path written there:
|
|
63
|
+
* the link shows them as the conversation did.
|
|
64
|
+
*/
|
|
65
|
+
files?: Record<string, string>;
|
|
52
66
|
/** What was left out when it was shared: the outputs of its tools, and how many images. */
|
|
53
67
|
omitted?: {
|
|
54
68
|
outputs?: boolean;
|
package/esm/common/relay-hub.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RelayKeys } from './relay-identity.js';
|
|
2
|
-
import { RELAY_HUB_ASK_DAYS, RELAY_HUB_IDLE_DAYS, RELAY_HUB_MAX_ASKS, RELAY_HUB_MAX_KEYS, RELAY_HUB_MAX_MEMBERS, RELAY_HUB_MAX_STATE, RELAY_HUB_MAX_VALUE, RELAY_HUB_RATE, RELAY_ID, RELAY_PING, RELAY_PONG, } from './relay-protocol.js';
|
|
2
|
+
import { RELAY_HUB_ASK_DAYS, RELAY_HUB_IDLE_DAYS, RELAY_HUB_MAX_ASKS, RELAY_HUB_MAX_KEYS, RELAY_HUB_MAX_MEMBERS, RELAY_HUB_MAX_STATE, RELAY_HUB_MAX_VALUE, RELAY_HUB_RATE, RELAY_ID, RELAY_INVITE_MAX_DAYS, RELAY_PING, RELAY_PONG, } from './relay-protocol.js';
|
|
3
3
|
/** Close codes of a hub. */
|
|
4
4
|
export var RelayHubCloseCode;
|
|
5
5
|
(function (RelayHubCloseCode) {
|
|
@@ -251,7 +251,7 @@ export class RelayHub {
|
|
|
251
251
|
this.send(socket, { t: 'error', code: 'invalid', message: 'An invitation needs a hash and a time.' });
|
|
252
252
|
return;
|
|
253
253
|
}
|
|
254
|
-
meta.invite = { hash: request.hash, expiresAt: Math.min(request.expiresAt, this.now() +
|
|
254
|
+
meta.invite = { hash: request.hash, expiresAt: Math.min(request.expiresAt, this.now() + RELAY_INVITE_MAX_DAYS * DAY_MS) };
|
|
255
255
|
await this.storage.put('meta', meta);
|
|
256
256
|
return;
|
|
257
257
|
case 'admit':
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { RelayFriends } from './relay-friends.js';
|
|
2
|
+
import { RelayHubsConnection } from './relay-hubs-connection.js';
|
|
3
|
+
import type { RelayIdentityBundle, RelayIdentityImport, RelayRoomRecord } from './relay-hubs-protocol.js';
|
|
4
|
+
import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
|
|
5
|
+
export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
|
|
6
|
+
protected abstract readonly book: RelayFriends;
|
|
7
|
+
/** The name others see; the owners of rooms this Ta is in hear it. */
|
|
8
|
+
setName(name: string): Promise<RelayPublicIdentity>;
|
|
9
|
+
/**
|
|
10
|
+
* The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
|
|
11
|
+
* look that is none. The owners of rooms this Ta is in hear it, as a name.
|
|
12
|
+
*/
|
|
13
|
+
setLook(look: string | undefined): Promise<RelayPublicIdentity>;
|
|
14
|
+
/** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
|
|
15
|
+
protected changeSelf(identity: RelayIdentity): Promise<RelayPublicIdentity>;
|
|
16
|
+
/** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
|
|
17
|
+
exportIdentity(): Promise<RelayIdentityBundle>;
|
|
18
|
+
/**
|
|
19
|
+
* Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
|
|
20
|
+
* sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
|
|
21
|
+
* (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
|
|
22
|
+
*/
|
|
23
|
+
importIdentity(bundle: RelayIdentityBundle, options?: {
|
|
24
|
+
shared?: boolean;
|
|
25
|
+
}): Promise<RelayIdentityImport>;
|
|
26
|
+
/** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
|
|
27
|
+
setShared(shared: boolean): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
|
|
30
|
+
* (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
|
|
31
|
+
* as another member.
|
|
32
|
+
*/
|
|
33
|
+
upsert(record: RelayRoomRecord): boolean;
|
|
34
|
+
/** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
|
|
35
|
+
forget(roomId: string, member: string): boolean;
|
|
36
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
|
|
2
|
+
import { RelayHubsUtil } from './relay-hubs-util.js';
|
|
3
|
+
import { RelayIdentityUtil } from './relay-identity.js';
|
|
4
|
+
export class RelayHubsIdentity extends RelayHubsConnection {
|
|
5
|
+
/** The name others see; the owners of rooms this Ta is in hear it. */
|
|
6
|
+
async setName(name) {
|
|
7
|
+
const trimmed = String(name).trim().slice(0, 40);
|
|
8
|
+
if (!trimmed) {
|
|
9
|
+
throw new Error('A name is needed.');
|
|
10
|
+
}
|
|
11
|
+
return this.changeSelf({ ...(await this.identity()), name: trimmed });
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
|
|
15
|
+
* look that is none. The owners of rooms this Ta is in hear it, as a name.
|
|
16
|
+
*/
|
|
17
|
+
async setLook(look) {
|
|
18
|
+
const identity = await this.identity();
|
|
19
|
+
const next = RelayIdentityUtil.withLook(identity, look);
|
|
20
|
+
return next.look === identity.look ? RelayIdentityUtil.publicOf(identity) : this.changeSelf(next);
|
|
21
|
+
}
|
|
22
|
+
/** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
|
|
23
|
+
async changeSelf(identity) {
|
|
24
|
+
var _a;
|
|
25
|
+
this.options.store.write('identity', identity);
|
|
26
|
+
this.own = identity;
|
|
27
|
+
for (const live of this.live.values()) {
|
|
28
|
+
// A room joined with another identity keeps that one's name.
|
|
29
|
+
if (live.record.status !== 'member' || live.record.identity) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (live.record.own) {
|
|
33
|
+
await this.writeRoster(live, members => members.map(member => (member.id === identity.id ? RelayIdentityUtil.withLook({ ...member, name: identity.name }, identity.look) : member))).catch(() => undefined);
|
|
34
|
+
}
|
|
35
|
+
else if (live.roster) {
|
|
36
|
+
const owner = live.roster.members.find(member => member.owner);
|
|
37
|
+
if (owner) {
|
|
38
|
+
const look = (_a = identity.look) !== null && _a !== void 0 ? _a : '';
|
|
39
|
+
await this.sendBody(live, { name: identity.name, look }, owner.member).then(() => {
|
|
40
|
+
live.toldLook = look;
|
|
41
|
+
}, () => undefined);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return RelayIdentityUtil.publicOf(identity);
|
|
46
|
+
}
|
|
47
|
+
/** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
|
|
48
|
+
async exportIdentity() {
|
|
49
|
+
return { identity: await this.identity(), rooms: [...this.live.values()].map(live => live.record), friends: this.book.chosen() };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
|
|
53
|
+
* sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
|
|
54
|
+
* (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
|
|
55
|
+
*/
|
|
56
|
+
async importIdentity(bundle, options = {}) {
|
|
57
|
+
const incoming = bundle === null || bundle === void 0 ? void 0 : bundle.identity;
|
|
58
|
+
if (!RelayHubsUtil.isPrivate(incoming) || !(await RelayIdentityUtil.check(incoming))) {
|
|
59
|
+
throw new Error('Not an identity of Ta.');
|
|
60
|
+
}
|
|
61
|
+
const own = await this.identity();
|
|
62
|
+
const switched = incoming.id !== own.id;
|
|
63
|
+
if (switched) {
|
|
64
|
+
for (const live of this.live.values()) {
|
|
65
|
+
if (!live.record.identity) {
|
|
66
|
+
live.record = { ...live.record, identity: own };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const shared = options.shared || own.shared;
|
|
71
|
+
const next = { ...(switched ? incoming : own), ...(shared ? { shared: true } : {}) };
|
|
72
|
+
if (!shared) {
|
|
73
|
+
delete next.shared;
|
|
74
|
+
}
|
|
75
|
+
this.own = next;
|
|
76
|
+
this.options.store.write('identity', next);
|
|
77
|
+
let rooms = 0;
|
|
78
|
+
for (const record of Array.isArray(bundle.rooms) ? bundle.rooms : []) {
|
|
79
|
+
if (this.upsert(record)) {
|
|
80
|
+
rooms++;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const friends = this.book.merge(Array.isArray(bundle.friends) ? bundle.friends : []);
|
|
84
|
+
this.save();
|
|
85
|
+
this.emit({ type: 'rooms' });
|
|
86
|
+
return { switched, rooms, friends };
|
|
87
|
+
}
|
|
88
|
+
/** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
|
|
89
|
+
async setShared(shared) {
|
|
90
|
+
const identity = { ...(await this.identity()) };
|
|
91
|
+
if (!!identity.shared === shared) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (shared) {
|
|
95
|
+
identity.shared = true;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
delete identity.shared;
|
|
99
|
+
}
|
|
100
|
+
this.own = identity;
|
|
101
|
+
this.options.store.write('identity', identity);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
|
|
105
|
+
* (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
|
|
106
|
+
* as another member.
|
|
107
|
+
*/
|
|
108
|
+
upsert(record) {
|
|
109
|
+
if (!RelayHubsUtil.isRecord(record)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const live = this.live.get(record.id);
|
|
113
|
+
if (!live) {
|
|
114
|
+
const added = new Live({ ...record });
|
|
115
|
+
this.live.set(record.id, added);
|
|
116
|
+
if (record.status === 'member' || record.status === 'waiting') {
|
|
117
|
+
this.connect(added);
|
|
118
|
+
}
|
|
119
|
+
this.save();
|
|
120
|
+
this.emit({ type: 'rooms' });
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
if (live.record.member !== record.member) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
const merged = RelayHubsUtil.merge(live.record, record);
|
|
127
|
+
if (JSON.stringify(merged) === JSON.stringify(live.record)) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
const was = live.record.status;
|
|
131
|
+
live.record = merged;
|
|
132
|
+
if (merged.status !== 'member' && merged.status !== 'waiting') {
|
|
133
|
+
this.disconnect(live);
|
|
134
|
+
}
|
|
135
|
+
else if (was !== merged.status || !live.socket) {
|
|
136
|
+
this.disconnect(live);
|
|
137
|
+
this.connect(live);
|
|
138
|
+
}
|
|
139
|
+
this.save();
|
|
140
|
+
this.emit({ type: 'rooms' });
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
/** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
|
|
144
|
+
forget(roomId, member) {
|
|
145
|
+
const live = this.live.get(roomId);
|
|
146
|
+
if (!live || live.record.member !== member) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
this.disconnect(live);
|
|
150
|
+
this.live.delete(roomId);
|
|
151
|
+
this.save();
|
|
152
|
+
this.emit({ type: 'rooms' });
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=relay-hubs-identity.js.map
|
|
@@ -28,6 +28,8 @@ export interface RelayRoomRecord {
|
|
|
28
28
|
}[];
|
|
29
29
|
/** Of the owner: the relay has the room. */
|
|
30
30
|
made?: boolean;
|
|
31
|
+
/** Of the owner: the members it lets in at most, when not RELAY_ROOM_MAX_MEMBERS (RelayHubs.configure). */
|
|
32
|
+
capacity?: number;
|
|
31
33
|
/** Of one asking: the invitation, till the relay has the ask. */
|
|
32
34
|
invite?: string;
|
|
33
35
|
asked?: boolean;
|
|
@@ -73,6 +75,8 @@ export interface RelayRoomInfo {
|
|
|
73
75
|
/** The relay it is on. */
|
|
74
76
|
relay: string;
|
|
75
77
|
owner: boolean;
|
|
78
|
+
/** Of a room this Ta made: the members it lets in at most. */
|
|
79
|
+
capacity?: number;
|
|
76
80
|
ownerName: string;
|
|
77
81
|
status: RelayRoomStatus;
|
|
78
82
|
reason?: RelayRoomRecord['reason'];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { RelayRoomRecord } from './relay-hubs-protocol.js';
|
|
2
|
+
import type { RelayIdentity } from './relay-identity.js';
|
|
3
|
+
export declare namespace RelayHubsUtil {
|
|
4
|
+
function isPrivate(identity: unknown): identity is RelayIdentity;
|
|
5
|
+
function isRecord(record: unknown): record is RelayRoomRecord;
|
|
6
|
+
/**
|
|
7
|
+
* Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
|
|
8
|
+
* key and the owner's keys once either has them, the invitations of both, made once either made it.
|
|
9
|
+
*/
|
|
10
|
+
function merge(local: RelayRoomRecord, other: RelayRoomRecord): RelayRoomRecord;
|
|
11
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** How far a membership has come: one computer heard it end, it ended for both. */
|
|
2
|
+
const STATUS_RANK = { waiting: 0, member: 1, refused: 2, gone: 3 };
|
|
3
|
+
export var RelayHubsUtil;
|
|
4
|
+
(function (RelayHubsUtil) {
|
|
5
|
+
function isPrivate(identity) {
|
|
6
|
+
const value = identity;
|
|
7
|
+
return (!!value &&
|
|
8
|
+
typeof value.id === 'string' &&
|
|
9
|
+
typeof value.name === 'string' &&
|
|
10
|
+
typeof value.sign === 'string' &&
|
|
11
|
+
typeof value.box === 'string' &&
|
|
12
|
+
!!value.signKey &&
|
|
13
|
+
typeof value.signKey === 'object' &&
|
|
14
|
+
!!value.boxKey &&
|
|
15
|
+
typeof value.boxKey === 'object');
|
|
16
|
+
}
|
|
17
|
+
RelayHubsUtil.isPrivate = isPrivate;
|
|
18
|
+
function isRecord(record) {
|
|
19
|
+
const value = record;
|
|
20
|
+
return (!!value &&
|
|
21
|
+
typeof value.id === 'string' &&
|
|
22
|
+
typeof value.relay === 'string' &&
|
|
23
|
+
typeof value.member === 'string' &&
|
|
24
|
+
typeof value.token === 'string' &&
|
|
25
|
+
typeof value.status === 'string' &&
|
|
26
|
+
value.status in STATUS_RANK &&
|
|
27
|
+
!!value.owner &&
|
|
28
|
+
typeof value.owner.id === 'string' &&
|
|
29
|
+
(value.identity === undefined || isPrivate(value.identity)));
|
|
30
|
+
}
|
|
31
|
+
RelayHubsUtil.isRecord = isRecord;
|
|
32
|
+
/**
|
|
33
|
+
* Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
|
|
34
|
+
* key and the owner's keys once either has them, the invitations of both, made once either made it.
|
|
35
|
+
*/
|
|
36
|
+
function merge(local, other) {
|
|
37
|
+
var _a, _b, _c, _d, _e, _f;
|
|
38
|
+
const ahead = STATUS_RANK[other.status] > STATUS_RANK[local.status] ? other : local;
|
|
39
|
+
const behind = ahead === local ? other : local;
|
|
40
|
+
const invites = [...((_a = local.invites) !== null && _a !== void 0 ? _a : []), ...((_b = other.invites) !== null && _b !== void 0 ? _b : [])]
|
|
41
|
+
.filter((invite, index, all) => all.findIndex(each => each.secret === invite.secret) === index)
|
|
42
|
+
.sort((a, b) => a.expiresAt - b.expiresAt)
|
|
43
|
+
.slice(-3);
|
|
44
|
+
const merged = {
|
|
45
|
+
...behind,
|
|
46
|
+
...ahead,
|
|
47
|
+
kind: ahead.kind || behind.kind,
|
|
48
|
+
owner: ahead.owner.sign ? ahead.owner : behind.owner.sign ? behind.owner : ahead.owner,
|
|
49
|
+
createdAt: Math.min(local.createdAt, other.createdAt),
|
|
50
|
+
};
|
|
51
|
+
const key = (_c = ahead.key) !== null && _c !== void 0 ? _c : behind.key;
|
|
52
|
+
const joinedAt = Math.max((_d = local.joinedAt) !== null && _d !== void 0 ? _d : 0, (_e = other.joinedAt) !== null && _e !== void 0 ? _e : 0);
|
|
53
|
+
const identity = (_f = local.identity) !== null && _f !== void 0 ? _f : other.identity;
|
|
54
|
+
delete merged.key;
|
|
55
|
+
delete merged.invites;
|
|
56
|
+
delete merged.joinedAt;
|
|
57
|
+
delete merged.identity;
|
|
58
|
+
delete merged.made;
|
|
59
|
+
if (merged.status !== 'waiting') {
|
|
60
|
+
delete merged.invite;
|
|
61
|
+
delete merged.asked;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
...merged,
|
|
65
|
+
...(key && (merged.status === 'member' || merged.status === 'waiting') ? { key } : {}),
|
|
66
|
+
...(invites.length ? { invites } : {}),
|
|
67
|
+
...(local.made || other.made ? { made: true } : {}),
|
|
68
|
+
...(joinedAt ? { joinedAt } : {}),
|
|
69
|
+
...(identity ? { identity } : {}),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
RelayHubsUtil.merge = merge;
|
|
73
|
+
})(RelayHubsUtil || (RelayHubsUtil = {}));
|
|
74
|
+
//# sourceMappingURL=relay-hubs-util.js.map
|
|
@@ -1,29 +1,21 @@
|
|
|
1
1
|
import { RelayFriends } from './relay-friends.js';
|
|
2
2
|
import { type RelayHubAskBody } from './relay-hub-util.js';
|
|
3
|
-
import { Live
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
3
|
+
import { Live } from './relay-hubs-connection.js';
|
|
4
|
+
import { RelayHubsIdentity } from './relay-hubs-identity.js';
|
|
5
|
+
import type { RelayFriend, RelayFriendInfo, RelayFriendRequest, RelayRoomEvent, RelayRoomInfo, RelayRoomRequest, RelayRoomValue } from './relay-hubs-protocol.js';
|
|
6
|
+
import { type RelayPublicIdentity } from './relay-identity.js';
|
|
6
7
|
/**
|
|
7
8
|
* The rooms of this Ta (docs/design/ta-link.md 3): its identity and friends, the rooms it made or asked to join,
|
|
8
9
|
* and a connection to the hub of each while it runs, through which it sends, sets the shared state and hears
|
|
9
10
|
* the others; every message signed by its member and sealed with the key of the room. The friends are a room of
|
|
10
11
|
* their own (RelayFriends).
|
|
11
12
|
*/
|
|
12
|
-
export declare class RelayHubs extends
|
|
13
|
+
export declare class RelayHubs extends RelayHubsIdentity {
|
|
13
14
|
protected readonly book: RelayFriends;
|
|
14
15
|
/** Connects to the rooms of rooms.json, and keeps connected. */
|
|
15
16
|
start(): void;
|
|
16
17
|
stop(): void;
|
|
17
18
|
onEvent(listener: (event: RelayRoomEvent) => void): () => void;
|
|
18
|
-
/** The name others see; the owners of rooms this Ta is in hear it. */
|
|
19
|
-
setName(name: string): Promise<RelayPublicIdentity>;
|
|
20
|
-
/**
|
|
21
|
-
* The kind of dinosaur the double of this Ta is (RelayPublicIdentity.look), which others draw it by; none for a
|
|
22
|
-
* look that is none. The owners of rooms this Ta is in hear it, as a name.
|
|
23
|
-
*/
|
|
24
|
-
setLook(look: string | undefined): Promise<RelayPublicIdentity>;
|
|
25
|
-
/** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
|
|
26
|
-
protected changeSelf(identity: RelayIdentity): Promise<RelayPublicIdentity>;
|
|
27
19
|
list(): RelayRoomInfo[];
|
|
28
20
|
room(id: string): RelayRoomInfo | undefined;
|
|
29
21
|
create(input: {
|
|
@@ -35,7 +27,7 @@ export declare class RelayHubs extends RelayHubsConnection {
|
|
|
35
27
|
}): Promise<RelayRoomInfo>;
|
|
36
28
|
/**
|
|
37
29
|
* The link that lets others ask to join: the one of before while it has a day left, else (or with `renew`) a new
|
|
38
|
-
* one for `days` (7 by default,
|
|
30
|
+
* one for `days` (7 by default, RELAY_INVITE_MAX_DAYS at most), the links of before no longer working.
|
|
39
31
|
*/
|
|
40
32
|
invite(roomId: string, options?: {
|
|
41
33
|
days?: number;
|
|
@@ -44,12 +36,22 @@ export declare class RelayHubs extends RelayHubsConnection {
|
|
|
44
36
|
link: string;
|
|
45
37
|
expiresAt: number;
|
|
46
38
|
}>;
|
|
47
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Asks to join the room of a link: the owner answers (`waiting` till then). The room of before, if this Ta has it.
|
|
41
|
+
* A room the identity owns on the user's other computer (an identity shared or moved) is joined as any other.
|
|
42
|
+
*/
|
|
48
43
|
join(text: string): Promise<RelayRoomInfo>;
|
|
49
44
|
/** Those asking to join the rooms of this Ta, the earliest first. */
|
|
50
45
|
requests(): Promise<RelayRoomRequest[]>;
|
|
51
46
|
/** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
|
|
52
47
|
answer(roomId: string, askId: string, accept: boolean): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* The owner's choices for a room: how many members it lets in at most (RELAY_ROOM_MAX_MEMBERS by default, 2 to
|
|
50
|
+
* RELAY_ROOM_CAPACITY_MAX), for one many come to (feedback from a browser). Those in already stay.
|
|
51
|
+
*/
|
|
52
|
+
configure(roomId: string, options: {
|
|
53
|
+
members?: number;
|
|
54
|
+
}): RelayRoomInfo;
|
|
53
55
|
/** The owner takes a member out: the relay closes it at once. */
|
|
54
56
|
remove(roomId: string, member: string): Promise<void>;
|
|
55
57
|
/** Leaves the room; the owner closes it, for everyone. A room asked to join is forgotten. */
|
|
@@ -66,26 +68,6 @@ export declare class RelayHubs extends RelayHubsConnection {
|
|
|
66
68
|
ok: boolean;
|
|
67
69
|
version: number;
|
|
68
70
|
}>;
|
|
69
|
-
/** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
|
|
70
|
-
exportIdentity(): Promise<RelayIdentityBundle>;
|
|
71
|
-
/**
|
|
72
|
-
* Takes on the identity of the user's other computer, with its rooms and friends (a move, or the identity shared by
|
|
73
|
-
* sync). Another identity than this Ta's: each room this Ta is in keeps the identity it joined with
|
|
74
|
-
* (RelayRoomRecord.identity), new rooms are the other's. A room this Ta is in as another member is left as it is.
|
|
75
|
-
*/
|
|
76
|
-
importIdentity(bundle: RelayIdentityBundle, options?: {
|
|
77
|
-
shared?: boolean;
|
|
78
|
-
}): Promise<RelayIdentityImport>;
|
|
79
|
-
/** Whether the user's other computers are this Ta too (RelayIdentity.shared). */
|
|
80
|
-
setShared(shared: boolean): Promise<void>;
|
|
81
|
-
/**
|
|
82
|
-
* A room as the user's other computer has it: added, or merged with this Ta's record of the same membership
|
|
83
|
-
* (RelayHubsUtil.merge), connected or not as its status says. False when nothing changed, or this Ta is in the room
|
|
84
|
-
* as another member.
|
|
85
|
-
*/
|
|
86
|
-
upsert(record: RelayRoomRecord): boolean;
|
|
87
|
-
/** A room the user's other computer left or forgot: forgotten here, without a word to the relay (that computer said it). */
|
|
88
|
-
forget(roomId: string, member: string): boolean;
|
|
89
71
|
/** Friends of the user's other computer, with those of this Ta. */
|
|
90
72
|
mergeFriends(friends: RelayFriend[]): number;
|
|
91
73
|
/** The friends, with their keys: each in the room of the other (shares to friends are sealed for them). */
|
|
@@ -119,12 +101,3 @@ export declare class RelayHubs extends RelayHubsConnection {
|
|
|
119
101
|
protected member(roomId: string): Live;
|
|
120
102
|
protected info(live: Live): RelayRoomInfo;
|
|
121
103
|
}
|
|
122
|
-
export declare namespace RelayHubsUtil {
|
|
123
|
-
function isPrivate(identity: unknown): identity is RelayIdentity;
|
|
124
|
-
function isRecord(record: unknown): record is RelayRoomRecord;
|
|
125
|
-
/**
|
|
126
|
-
* Two records of one membership, as two computers of the user hold it: the further status (one heard it end), the
|
|
127
|
-
* key and the owner's keys once either has them, the invitations of both, made once either made it.
|
|
128
|
-
*/
|
|
129
|
-
function merge(local: RelayRoomRecord, other: RelayRoomRecord): RelayRoomRecord;
|
|
130
|
-
}
|