@celljs/ta-relay 4.11.0 → 4.12.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.
Files changed (38) hide show
  1. package/esm/common/index.d.ts +2 -0
  2. package/esm/common/index.js +2 -0
  3. package/esm/common/relay-faces.d.ts +32 -0
  4. package/esm/common/relay-faces.js +208 -0
  5. package/esm/common/relay-hubs-connection.d.ts +98 -0
  6. package/esm/common/relay-hubs-connection.js +505 -0
  7. package/esm/common/relay-hubs-protocol.d.ts +130 -0
  8. package/esm/common/relay-hubs-protocol.js +2 -0
  9. package/esm/common/relay-hubs.d.ts +4 -214
  10. package/esm/common/relay-hubs.js +2 -495
  11. package/esm/common/relay-room-chat.d.ts +31 -2
  12. package/esm/common/relay-room-chat.js +31 -7
  13. package/esm/guest/guest-i18n.d.ts +3 -0
  14. package/esm/guest/guest-i18n.js +6 -0
  15. package/esm/guest/guest-style.d.ts +1 -1
  16. package/esm/guest/guest-style.js +11 -4
  17. package/esm/guest/guest.js +31 -18
  18. package/esm/node/relay-hub-files.d.ts +1 -1
  19. package/lib/common/index.d.ts +2 -0
  20. package/lib/common/index.js +2 -0
  21. package/lib/common/relay-faces.d.ts +33 -0
  22. package/lib/common/relay-faces.js +211 -0
  23. package/lib/common/relay-hubs-connection.d.ts +99 -0
  24. package/lib/common/relay-hubs-connection.js +510 -0
  25. package/lib/common/relay-hubs-protocol.d.ts +131 -0
  26. package/lib/common/relay-hubs-protocol.js +3 -0
  27. package/lib/common/relay-hubs.d.ts +4 -214
  28. package/lib/common/relay-hubs.js +9 -502
  29. package/lib/common/relay-room-chat.d.ts +31 -2
  30. package/lib/common/relay-room-chat.js +31 -7
  31. package/lib/guest/guest-i18n.d.ts +3 -0
  32. package/lib/guest/guest-i18n.js +6 -0
  33. package/lib/guest/guest-style.d.ts +1 -1
  34. package/lib/guest/guest-style.js +11 -4
  35. package/lib/guest/guest.js +31 -18
  36. package/lib/node/relay-hub-files.d.ts +1 -1
  37. package/package.json +2 -2
  38. package/viewer/room.html +21 -14
@@ -5,10 +5,12 @@ export * from './relay-card.js';
5
5
  export * from './relay-cipher.js';
6
6
  export * from './relay-content.js';
7
7
  export * from './relay-conversation.js';
8
+ export * from './relay-faces.js';
8
9
  export * from './relay-files.js';
9
10
  export * from './relay-hub.js';
10
11
  export * from './relay-hub-util.js';
11
12
  export * from './relay-hubs.js';
13
+ export * from './relay-hubs-protocol.js';
12
14
  export * from './relay-identity.js';
13
15
  export * from './relay-pairing.js';
14
16
  export * from './relay-protocol.js';
@@ -5,10 +5,12 @@ export * from './relay-card.js';
5
5
  export * from './relay-cipher.js';
6
6
  export * from './relay-content.js';
7
7
  export * from './relay-conversation.js';
8
+ export * from './relay-faces.js';
8
9
  export * from './relay-files.js';
9
10
  export * from './relay-hub.js';
10
11
  export * from './relay-hub-util.js';
11
12
  export * from './relay-hubs.js';
13
+ export * from './relay-hubs-protocol.js';
12
14
  export * from './relay-identity.js';
13
15
  export * from './relay-pairing.js';
14
16
  export * from './relay-protocol.js';
@@ -0,0 +1,32 @@
1
+ /** How many faces there are. */
2
+ export declare const FACE_COUNT = 16;
3
+ /** An element of SVG: its tag, its attributes as written in SVG (`stroke-width`, `class`), what is in it. */
4
+ export interface FaceNode {
5
+ tag: string;
6
+ attrs: Record<string, string | number>;
7
+ children?: FaceNode[];
8
+ }
9
+ export interface Look {
10
+ color: string;
11
+ /** Eyes and mouth. */
12
+ eye: string;
13
+ cheek: string;
14
+ /** A second colour of the shape: ears, a stem, an antenna. */
15
+ accent?: string;
16
+ /** The height of the eyes and how far apart they are. */
17
+ eyeY: number;
18
+ gap: number;
19
+ mouth?: string;
20
+ /** Brows pressed in, a grin. */
21
+ brows?: boolean;
22
+ }
23
+ export declare namespace FaceUtil {
24
+ /** The face of an id: the same for the same id, on every device. */
25
+ function of(seed: string): number;
26
+ /** The face given, if it is one, else that of `seed`. */
27
+ function pick(face: number | undefined, seed?: string): number;
28
+ /** Its colour, for what draws no faces. */
29
+ function color(face: number): string;
30
+ /** What is in the `<svg viewBox="0 0 40 40">` of a face: its shape, then its cheeks, brows, eyes and mouth. */
31
+ function art(face: number): FaceNode;
32
+ }
@@ -0,0 +1,208 @@
1
+ /*
2
+ * The faces of those who are not Ta (agents, the people of rooms), framework-free so that Ta Web (ui.Face, React) and
3
+ * the page of web guests of the relay (src/guest, the DOM) draw the same: a small coloured shape with two eyes, cheeks
4
+ * and a mouth, one of FACE_COUNT, on a 40 × 40 grid. Their styles (`ta-face-eyes` blinks, `ta-face-look` looks
5
+ * about while at work) are those of the page that draws them.
6
+ */
7
+ /** How many faces there are. */
8
+ export const FACE_COUNT = 16;
9
+ const BLUE = '#4285F4';
10
+ const RED = '#EA4335';
11
+ const YELLOW = '#FBBC05';
12
+ const GREEN = '#34A853';
13
+ const MINT = '#5CC27F';
14
+ const ORANGE = '#EE7B3B';
15
+ const VIOLET = '#8C5CF0';
16
+ const CREAM = '#F1DFC0';
17
+ const TEAL = '#26B0A9';
18
+ const PINK = '#F27BA6';
19
+ const LILAC = '#B49CF5';
20
+ const SKY = '#45B5E8';
21
+ const INDIGO = '#5C6BC0';
22
+ const MIST = '#DCE3F0';
23
+ const INK = '#202124';
24
+ const WHITE = '#FFFFFF';
25
+ const CHEEK = 'rgba(255, 160, 185, 0.55)';
26
+ /** The edge of a light face. */
27
+ const EDGE = 'rgba(0, 0, 0, 0.14)';
28
+ const CHEEK_DEEP = 'rgba(120, 10, 30, 0.35)';
29
+ const CHEEK_WARM = 'rgba(234, 67, 53, 0.4)';
30
+ /** By face: the order is that of the shapes drawn in {@link shape}. */
31
+ const LOOKS = [
32
+ { color: MINT, eye: '#1D3B27', cheek: CHEEK, eyeY: 21, gap: 8 }, // round
33
+ { color: BLUE, eye: WHITE, cheek: CHEEK, eyeY: 23, gap: 8 }, // cat
34
+ { color: RED, eye: WHITE, cheek: CHEEK_DEEP, eyeY: 22, gap: 8 }, // pentagon
35
+ { color: CREAM, eye: '#4A3420', cheek: 'rgba(240, 120, 120, 0.5)', accent: '#DDBF8E', eyeY: 22, gap: 7.6 }, // bear
36
+ { color: GREEN, eye: WHITE, cheek: CHEEK, eyeY: 19, gap: 7.2 }, // capsule
37
+ { color: INDIGO, eye: WHITE, cheek: CHEEK, eyeY: 20, gap: 7.4 }, // diamond
38
+ { color: YELLOW, eye: INK, cheek: CHEEK_WARM, accent: '#F29900', eyeY: 24, gap: 8.4 }, // bun
39
+ { color: VIOLET, eye: '#22134A', cheek: CHEEK, eyeY: 20, gap: 8 }, // hexagon
40
+ { color: MIST, eye: '#2B3245', cheek: CHEEK, eyeY: 19, gap: 7.6 }, // ghost
41
+ { color: SKY, eye: WHITE, cheek: CHEEK, eyeY: 21, gap: 6.4 }, // star
42
+ { color: ORANGE, eye: '#3D1C08', cheek: CHEEK, accent: GREEN, eyeY: 22, gap: 8.6, brows: true }, // pumpkin
43
+ { color: WHITE, eye: WHITE, cheek: CHEEK, accent: INK, mouth: INK, eyeY: 22, gap: 10 }, // panda
44
+ { color: TEAL, eye: WHITE, cheek: CHEEK, accent: '#1B8A84', eyeY: 22.5, gap: 8.4 }, // screen
45
+ { color: PINK, eye: '#4A1428', cheek: 'rgba(160, 20, 70, 0.3)', eyeY: 24, gap: 7.4 }, // drop
46
+ { color: LILAC, eye: '#2A1A5E', cheek: CHEEK, eyeY: 25, gap: 8 }, // rabbit
47
+ { color: YELLOW, eye: INK, cheek: CHEEK_WARM, eyeY: 25, gap: 7.4 }, // triangle
48
+ ];
49
+ /** The corners of a polygon of `count` sides around (cx, cy), the first straight up; `inner` makes it a star. */
50
+ function corners(count, radius, cx, cy, inner) {
51
+ const points = [];
52
+ const steps = inner ? count * 2 : count;
53
+ for (let index = 0; index < steps; index++) {
54
+ const r = inner && index % 2 === 1 ? inner : radius;
55
+ const angle = -Math.PI / 2 + (index * 2 * Math.PI) / steps;
56
+ points.push(`${(cx + r * Math.cos(angle)).toFixed(2)},${(cy + r * Math.sin(angle)).toFixed(2)}`);
57
+ }
58
+ return points.join(' ');
59
+ }
60
+ const PENTAGON = corners(5, 13.5, 20, 21.5);
61
+ const HEXAGON = corners(6, 14, 20, 20);
62
+ const STAR = corners(5, 15.5, 20, 21.5, 8.6);
63
+ function node(tag, attrs, children) {
64
+ return children ? { tag, attrs, children } : { tag, attrs };
65
+ }
66
+ /** A polygon with round corners: filled and stroked in the same colour. */
67
+ function rounded(points, color, width = 5) {
68
+ return node('polygon', { points, fill: color, stroke: color, 'stroke-width': width, 'stroke-linejoin': 'round' });
69
+ }
70
+ function shape(face, look) {
71
+ const { color, accent = color } = look;
72
+ switch (face) {
73
+ case 1:
74
+ return node('g', {}, [
75
+ rounded('7.5,6.5 17,12 8.5,18', color, 4),
76
+ rounded('32.5,6.5 23,12 31.5,18', color, 4),
77
+ node('ellipse', { cx: 20, cy: 23, rx: 14.5, ry: 12.5, fill: color }),
78
+ ]);
79
+ case 2:
80
+ return rounded(PENTAGON, color);
81
+ case 3:
82
+ return node('g', {}, [
83
+ node('circle', { cx: 9.5, cy: 10.5, r: 5.5, fill: color }),
84
+ node('circle', { cx: 30.5, cy: 10.5, r: 5.5, fill: color }),
85
+ node('circle', { cx: 9.5, cy: 10.5, r: 2.6, fill: accent }),
86
+ node('circle', { cx: 30.5, cy: 10.5, r: 2.6, fill: accent }),
87
+ node('ellipse', { cx: 20, cy: 22.5, rx: 14.5, ry: 13, fill: color }),
88
+ ]);
89
+ case 4:
90
+ return node('rect', { x: 8.5, y: 4, width: 23, height: 32, rx: 11.5, fill: color });
91
+ case 5:
92
+ return rounded('20,5.5 34.5,20 20,34.5 5.5,20', color, 5.5);
93
+ case 6:
94
+ return node('g', {}, [
95
+ node('ellipse', { cx: 20, cy: 11, rx: 3.2, ry: 3.6, fill: accent }),
96
+ node('path', { d: 'M20 12.5c9.2 0 15.5 5.6 15.5 12.4 0 6.4-6.9 10.1-15.5 10.1S4.5 31.3 4.5 24.9C4.5 18.1 10.8 12.5 20 12.5Z', fill: color }),
97
+ ]);
98
+ case 7:
99
+ return rounded(HEXAGON, color);
100
+ case 8:
101
+ return node('path', {
102
+ d: 'M8 19a12 12 0 0 1 24 0v14l-4-3-4 3-4-3-4 3-4-3-4 3Z',
103
+ fill: color,
104
+ stroke: EDGE,
105
+ 'stroke-width': 0.8,
106
+ 'stroke-linejoin': 'round',
107
+ });
108
+ case 9:
109
+ return rounded(STAR, color, 4.5);
110
+ case 10:
111
+ return node('g', {}, [
112
+ node('rect', { x: 18.6, y: 4.5, width: 2.8, height: 7, rx: 1.4, fill: accent }),
113
+ node('ellipse', { cx: 12.5, cy: 23, rx: 8.5, ry: 11, fill: color }),
114
+ node('ellipse', { cx: 27.5, cy: 23, rx: 8.5, ry: 11, fill: color }),
115
+ node('ellipse', { cx: 20, cy: 22.5, rx: 9.5, ry: 12, fill: color }),
116
+ ]);
117
+ case 11:
118
+ return node('g', {}, [
119
+ node('circle', { cx: 9.5, cy: 10, r: 5, fill: accent }),
120
+ node('circle', { cx: 30.5, cy: 10, r: 5, fill: accent }),
121
+ node('ellipse', { cx: 20, cy: 22.5, rx: 14.5, ry: 13, fill: color, stroke: EDGE, 'stroke-width': 0.8 }),
122
+ node('ellipse', { cx: 14.6, cy: 22, rx: 3.6, ry: 4.4, transform: 'rotate(20 14.6 22)', fill: accent }),
123
+ node('ellipse', { cx: 25.4, cy: 22, rx: 3.6, ry: 4.4, transform: 'rotate(-20 25.4 22)', fill: accent }),
124
+ ]);
125
+ case 12:
126
+ return node('g', {}, [
127
+ node('rect', { x: 19.1, y: 6, width: 1.8, height: 7, fill: accent }),
128
+ node('circle', { cx: 20, cy: 5, r: 2.2, fill: accent }),
129
+ node('rect', { x: 5.5, y: 12, width: 29, height: 22.5, rx: 6.5, fill: color }),
130
+ ]);
131
+ case 13:
132
+ return node('path', { d: 'M20 4.5c2 0 13 11.6 13 20.2 0 6.4-5.8 10.8-13 10.8S7 31.1 7 24.7C7 16.1 18 4.5 20 4.5Z', fill: color });
133
+ case 14:
134
+ return node('g', {}, [
135
+ node('ellipse', { cx: 14, cy: 10.5, rx: 3.6, ry: 8, transform: 'rotate(-8 14 10.5)', fill: color }),
136
+ node('ellipse', { cx: 26, cy: 10.5, rx: 3.6, ry: 8, transform: 'rotate(8 26 10.5)', fill: color }),
137
+ node('ellipse', { cx: 20, cy: 25, rx: 14, ry: 11, fill: color }),
138
+ ]);
139
+ case 15:
140
+ return rounded('20,7 35,33 5,33', color, 6);
141
+ default:
142
+ return node('circle', { cx: 20, cy: 21, r: 14.5, fill: color });
143
+ }
144
+ }
145
+ export var FaceUtil;
146
+ (function (FaceUtil) {
147
+ /** The face of an id: the same for the same id, on every device. */
148
+ function of(seed) {
149
+ let hash = 0;
150
+ for (const char of seed) {
151
+ hash = (hash * 31 + char.charCodeAt(0)) | 0;
152
+ }
153
+ return Math.abs(hash) % FACE_COUNT;
154
+ }
155
+ FaceUtil.of = of;
156
+ /** The face given, if it is one, else that of `seed`. */
157
+ function pick(face, seed = '') {
158
+ return face !== undefined && face >= 0 && face < FACE_COUNT ? Math.floor(face) : of(seed);
159
+ }
160
+ FaceUtil.pick = pick;
161
+ /** Its colour, for what draws no faces. */
162
+ function color(face) {
163
+ var _a;
164
+ return ((_a = LOOKS[face]) !== null && _a !== void 0 ? _a : LOOKS[0]).color;
165
+ }
166
+ FaceUtil.color = color;
167
+ /** What is in the `<svg viewBox="0 0 40 40">` of a face: its shape, then its cheeks, brows, eyes and mouth. */
168
+ function art(face) {
169
+ var _a, _b;
170
+ const look = (_a = LOOKS[face]) !== null && _a !== void 0 ? _a : LOOKS[0];
171
+ const half = look.gap / 2;
172
+ const eyeW = 4.1;
173
+ const eyeH = 4.9;
174
+ const top = look.eyeY - eyeH / 2;
175
+ const cheekY = look.eyeY + 3.6;
176
+ const cheekX = half + 2.2;
177
+ const mouthY = look.eyeY + 3.4;
178
+ const features = [
179
+ node('g', { fill: look.cheek }, [
180
+ node('ellipse', { cx: 20 - cheekX, cy: cheekY, rx: 2.2, ry: 1.25 }),
181
+ node('ellipse', { cx: 20 + cheekX, cy: cheekY, rx: 2.2, ry: 1.25 }),
182
+ ]),
183
+ ];
184
+ if (look.brows) {
185
+ features.push(node('path', {
186
+ d: `M${20 - half - 3.4} ${top - 2.6}l3.6 1.6M${20 + half + 3.4} ${top - 2.6}l-3.6 1.6`,
187
+ stroke: look.eye,
188
+ 'stroke-width': 1.4,
189
+ 'stroke-linecap': 'round',
190
+ fill: 'none',
191
+ }));
192
+ }
193
+ features.push(node('g', { class: 'ta-face-eyes', fill: look.eye }, [
194
+ node('rect', { x: 20 - half - eyeW / 2, y: top, width: eyeW, height: eyeH, rx: eyeW / 2 }),
195
+ node('rect', { x: 20 + half - eyeW / 2, y: top, width: eyeW, height: eyeH, rx: eyeW / 2 }),
196
+ ]), node('path', {
197
+ d: `M18.1 ${mouthY}q.95 1.25 1.9 0q.95 1.25 1.9 0`,
198
+ fill: 'none',
199
+ stroke: (_b = look.mouth) !== null && _b !== void 0 ? _b : look.eye,
200
+ 'stroke-width': 1.1,
201
+ 'stroke-linecap': 'round',
202
+ 'stroke-linejoin': 'round',
203
+ }));
204
+ return node('g', { class: 'ta-face-body' }, [shape(face, look), node('g', { class: 'ta-face-look' }, features)]);
205
+ }
206
+ FaceUtil.art = art;
207
+ })(FaceUtil || (FaceUtil = {}));
208
+ //# sourceMappingURL=relay-faces.js.map
@@ -0,0 +1,98 @@
1
+ import { type RelayHubAskBody, type RelayHubKeys, type RelayHubMember, type RelayHubRosterBody } from './relay-hub-util.js';
2
+ import type { RelayFriend, RelayHubsOptions, RelayRoomEvent, RelayRoomRecord, RelayRoomRequest, RelayRoomStatus, RelayRoomValue } from './relay-hubs-protocol.js';
3
+ import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
4
+ import { type RelayHubEntry, type RelayHubFailure, type RelayHubRequest } from './relay-protocol.js';
5
+ /** What a member sends in a message, signed and sealed: data of an extension, or a word of Ta itself. */
6
+ export interface MessageBody {
7
+ d?: unknown;
8
+ /** A member's new name, for the owner to put in the roster. */
9
+ name?: string;
10
+ }
11
+ export interface StateBody {
12
+ key: string;
13
+ value: unknown;
14
+ }
15
+ /** How long a call waits for the room to be connected, or for the relay to answer. */
16
+ export declare const READY_MS = 15000;
17
+ export declare const OPEN = 1;
18
+ /** A room as this Ta holds it while it runs: its connection and what it knows of the room. */
19
+ export declare class Live {
20
+ record: RelayRoomRecord;
21
+ socket?: WebSocket;
22
+ keys?: RelayHubKeys;
23
+ synced: boolean;
24
+ online: Set<string>;
25
+ roster?: RelayHubRosterBody;
26
+ readonly state: Map<string, RelayRoomValue>;
27
+ /** Asks to join, with the invitation each came with. */
28
+ readonly asks: Map<string, {
29
+ body: RelayHubAskBody;
30
+ invite: string;
31
+ }>;
32
+ readonly done: Map<number, (result: {
33
+ ok: boolean;
34
+ v: number;
35
+ error?: RelayHubFailure;
36
+ }) => void>;
37
+ readonly ready: Array<() => void>;
38
+ attempts: number;
39
+ /** Connect again at once, whatever the close said. */
40
+ retry: boolean;
41
+ timer?: ReturnType<typeof setTimeout>;
42
+ pingTimer?: ReturnType<typeof setInterval>;
43
+ awaitingPong: boolean;
44
+ /** Events are handled one after another: opening them is async. */
45
+ chain: Promise<void>;
46
+ constructor(record: RelayRoomRecord);
47
+ }
48
+ /**
49
+ * The half of {@link RelayHubs} that keeps a connection to the hub of each room and reads what comes over it:
50
+ * connecting again, the welcome, the roster, the state and the messages, each opened and checked against the
51
+ * signatures of the roster; what is kept of the rooms and the friends.
52
+ */
53
+ export declare abstract class RelayHubsConnection {
54
+ protected readonly options: RelayHubsOptions;
55
+ protected readonly live: Map<string, Live>;
56
+ protected readonly listeners: Set<(event: RelayRoomEvent) => void>;
57
+ protected own?: RelayIdentity;
58
+ /** Asks the user was told of while this runs: those the owner finds waiting when it connects are told once. */
59
+ protected readonly told: Set<string>;
60
+ protected stopped: boolean;
61
+ protected nextId: number;
62
+ constructor(options: RelayHubsOptions);
63
+ /** This Ta's identity, made the first time. */
64
+ identity(): Promise<RelayIdentity>;
65
+ protected requestOf(live: Live, ask: RelayHubAskBody, friends: Set<string>): RelayRoomRequest;
66
+ /** Resolves once the room is connected and has its state; rejects after a while. */
67
+ protected ready(live: Live): Promise<void>;
68
+ protected request(live: Live, request: RelayHubRequest): void;
69
+ protected sendBody(live: Live, body: MessageBody, to?: string): Promise<void>;
70
+ /** The roster with `change` made, its version one more, signed by the owner. */
71
+ protected nextRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[]): Promise<{
72
+ v: number;
73
+ d: string;
74
+ }>;
75
+ protected writeRoster(live: Live, change: (members: RelayHubMember[]) => RelayHubMember[]): Promise<void>;
76
+ protected connect(live: Live): void;
77
+ protected hello(live: Live, socket: WebSocket): Promise<void>;
78
+ protected closed(live: Live, code: number): void;
79
+ protected disconnect(live: Live): void;
80
+ protected receive(live: Live, text: string): Promise<void>;
81
+ protected failed(live: Live, code: RelayHubFailure): void;
82
+ protected setStatus(live: Live, status: RelayRoomStatus, reason?: RelayRoomRecord['reason']): void;
83
+ protected admitted(live: Live, answer: string): Promise<void>;
84
+ /** An ask to join: told as a request the first time this Ta hears it; `fresh` when it came now, not with the welcome. */
85
+ protected asked(live: Live, sealed: string, fresh: boolean): Promise<void>;
86
+ protected readRoster(live: Live, sealed: string): Promise<void>;
87
+ protected guestOf(live: Live, member: string): boolean;
88
+ protected signOf(live: Live): (member: string) => string | undefined;
89
+ protected readEntry(live: Live, entry: RelayHubEntry, tell: boolean): Promise<void>;
90
+ protected readMessage(live: Live, from: string, sealed: string): Promise<void>;
91
+ /** Those of a room are friends: kept with their keys and the rooms shared. */
92
+ protected meet(title: string, members: RelayPublicIdentity[]): void;
93
+ protected friendIds(): Set<string>;
94
+ protected readFriends(): RelayFriend[];
95
+ protected readRooms(): RelayRoomRecord[];
96
+ protected save(): void;
97
+ protected emit(event: RelayRoomEvent): void;
98
+ }