@celljs/ta-relay 4.10.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.
- package/README.en.md +13 -1
- package/README.md +13 -1
- package/esm/common/index.d.ts +11 -0
- package/esm/common/index.js +11 -0
- package/esm/common/relay-blobs.d.ts +54 -0
- package/esm/common/relay-blobs.js +153 -0
- package/esm/common/relay-card.d.ts +23 -0
- package/esm/common/relay-card.js +30 -0
- package/esm/common/relay-content.d.ts +104 -0
- package/esm/common/relay-content.js +316 -0
- package/esm/common/relay-conversation.d.ts +59 -0
- package/esm/common/relay-conversation.js +9 -0
- package/esm/common/relay-faces.d.ts +32 -0
- package/esm/common/relay-faces.js +208 -0
- package/esm/common/relay-hub-util.d.ts +91 -0
- package/esm/common/relay-hub-util.js +164 -0
- package/esm/common/relay-hub.d.ts +125 -0
- package/esm/common/relay-hub.js +438 -0
- package/esm/common/relay-hubs-connection.d.ts +98 -0
- package/esm/common/relay-hubs-connection.js +505 -0
- package/esm/common/relay-hubs-protocol.d.ts +130 -0
- package/esm/common/relay-hubs-protocol.js +2 -0
- package/esm/common/relay-hubs.d.ts +61 -0
- package/esm/common/relay-hubs.js +351 -0
- package/esm/common/relay-identity.d.ts +46 -0
- package/esm/common/relay-identity.js +126 -0
- package/esm/common/relay-protocol.d.ts +211 -2
- package/esm/common/relay-protocol.js +53 -0
- package/esm/common/relay-room-chat.d.ts +109 -0
- package/esm/common/relay-room-chat.js +176 -0
- package/esm/common/relay-socket.d.ts +8 -0
- package/esm/common/relay-socket.js +23 -2
- package/esm/guest/guest-i18n.d.ts +50 -0
- package/esm/guest/guest-i18n.js +103 -0
- package/esm/guest/guest-style.d.ts +1 -0
- package/esm/guest/guest-style.js +85 -0
- package/esm/guest/guest.d.ts +1 -0
- package/esm/guest/guest.js +384 -0
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/relay-host.d.ts +25 -3
- package/esm/node/relay-host.js +115 -4
- package/esm/node/relay-hub-files.d.ts +12 -0
- package/esm/node/relay-hub-files.js +39 -0
- package/esm/node/relay-server.d.ts +46 -1
- package/esm/node/relay-server.js +227 -1
- package/esm/node/relay-store.d.ts +72 -0
- package/esm/node/relay-store.js +123 -2
- package/esm/node/relay-viewer.d.ts +7 -0
- package/esm/node/relay-viewer.js +33 -0
- package/esm/viewer/viewer-i18n.d.ts +56 -0
- package/esm/viewer/viewer-i18n.js +117 -0
- package/esm/viewer/viewer-markdown.d.ts +7 -0
- package/esm/viewer/viewer-markdown.js +198 -0
- package/esm/viewer/viewer-style.d.ts +1 -0
- package/esm/viewer/viewer-style.js +72 -0
- package/esm/viewer/viewer.d.ts +8 -0
- package/esm/viewer/viewer.js +378 -0
- package/lib/common/index.d.ts +11 -0
- package/lib/common/index.js +11 -0
- package/lib/common/relay-blobs.d.ts +55 -0
- package/lib/common/relay-blobs.js +157 -0
- package/lib/common/relay-card.d.ts +24 -0
- package/lib/common/relay-card.js +33 -0
- package/lib/common/relay-content.d.ts +105 -0
- package/lib/common/relay-content.js +320 -0
- package/lib/common/relay-conversation.d.ts +60 -0
- package/lib/common/relay-conversation.js +12 -0
- package/lib/common/relay-faces.d.ts +33 -0
- package/lib/common/relay-faces.js +211 -0
- package/lib/common/relay-hub-util.d.ts +92 -0
- package/lib/common/relay-hub-util.js +167 -0
- package/lib/common/relay-hub.d.ts +126 -0
- package/lib/common/relay-hub.js +442 -0
- package/lib/common/relay-hubs-connection.d.ts +99 -0
- package/lib/common/relay-hubs-connection.js +510 -0
- package/lib/common/relay-hubs-protocol.d.ts +131 -0
- package/lib/common/relay-hubs-protocol.js +3 -0
- package/lib/common/relay-hubs.d.ts +62 -0
- package/lib/common/relay-hubs.js +355 -0
- package/lib/common/relay-identity.d.ts +47 -0
- package/lib/common/relay-identity.js +129 -0
- package/lib/common/relay-protocol.d.ts +211 -2
- package/lib/common/relay-protocol.js +54 -1
- package/lib/common/relay-room-chat.d.ts +110 -0
- package/lib/common/relay-room-chat.js +180 -0
- package/lib/common/relay-socket.d.ts +8 -0
- package/lib/common/relay-socket.js +23 -2
- package/lib/guest/guest-i18n.d.ts +51 -0
- package/lib/guest/guest-i18n.js +106 -0
- package/lib/guest/guest-style.d.ts +2 -0
- package/lib/guest/guest-style.js +88 -0
- package/lib/guest/guest.d.ts +2 -0
- package/lib/guest/guest.js +386 -0
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/relay-host.d.ts +25 -3
- package/lib/node/relay-host.js +115 -4
- package/lib/node/relay-hub-files.d.ts +13 -0
- package/lib/node/relay-hub-files.js +43 -0
- package/lib/node/relay-server.d.ts +46 -1
- package/lib/node/relay-server.js +226 -0
- package/lib/node/relay-store.d.ts +72 -0
- package/lib/node/relay-store.js +121 -0
- package/lib/node/relay-viewer.d.ts +8 -0
- package/lib/node/relay-viewer.js +32 -0
- package/lib/viewer/viewer-i18n.d.ts +57 -0
- package/lib/viewer/viewer-i18n.js +120 -0
- package/lib/viewer/viewer-markdown.d.ts +8 -0
- package/lib/viewer/viewer-markdown.js +201 -0
- package/lib/viewer/viewer-style.d.ts +2 -0
- package/lib/viewer/viewer-style.js +75 -0
- package/lib/viewer/viewer.d.ts +9 -0
- package/lib/viewer/viewer.js +381 -0
- package/package.json +5 -4
- package/viewer/frame.html +1 -0
- package/viewer/page.html +80 -0
- package/viewer/room.html +94 -0
package/esm/node/relay-host.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { RelayCipher } from '../common/relay-cipher.js';
|
|
|
2
2
|
import { type RelayFilesFrame } from '../common/relay-files.js';
|
|
3
3
|
import { RELAY_PING, type RelayHostMessage, type RelayWelcome } from '../common/relay-protocol.js';
|
|
4
4
|
import { RelayFilesHost } from './relay-files-host.js';
|
|
5
|
-
import type { RelayShare, RelayStore } from './relay-store.js';
|
|
5
|
+
import type { RelayContentShare, RelayShare, RelayStore } from './relay-store.js';
|
|
6
6
|
export interface RelayHostOptions {
|
|
7
7
|
store: RelayStore;
|
|
8
8
|
/** The WebSocket of this ta serve, with its token: every device gets a connection to it of its own. */
|
|
@@ -13,6 +13,8 @@ export interface RelayHostOptions {
|
|
|
13
13
|
pingMs?: number;
|
|
14
14
|
/** The bytes of a package handed on (a share of the store), for one who has its link; without it, shares are refused. */
|
|
15
15
|
sharePackage?(share: RelayShare): Promise<Uint8Array>;
|
|
16
|
+
/** Hands on the content shared in the store (RelayStore.contents) to whoever has its link; without it, such links are refused. */
|
|
17
|
+
shareContent?: boolean;
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
18
20
|
* A device on a channel: the handshake, then its connection to ta serve — or, for a channel that asked
|
|
@@ -25,10 +27,17 @@ declare class RelayChannel {
|
|
|
25
27
|
protected readonly options: RelayHostOptions;
|
|
26
28
|
protected cipher?: RelayCipher;
|
|
27
29
|
protected local?: WebSocket;
|
|
28
|
-
/** What this channel does, once its first sealed message said it. */
|
|
29
|
-
protected mode?: 'rpc' | 'files';
|
|
30
|
+
/** What this channel does, once its first sealed message said it; `share` from the handshake, for a link of a package. */
|
|
31
|
+
protected mode?: 'rpc' | 'files' | 'share' | 'content';
|
|
32
|
+
/** Content shared, until the viewer proved it has its key (RelayContentUtil). */
|
|
33
|
+
protected content?: RelayContentShare;
|
|
34
|
+
/** Content shared with Ta friends: a cipher with the key of each, till the proof of one opens. */
|
|
35
|
+
protected candidates?: RelayCipher[];
|
|
30
36
|
protected files?: RelayFilesHost;
|
|
31
37
|
protected readonly early: string[];
|
|
38
|
+
/** Agreed in the hello of a viewer of content, set once it proved. */
|
|
39
|
+
protected compress: boolean;
|
|
40
|
+
protected proofTimer?: ReturnType<typeof setTimeout>;
|
|
32
41
|
protected readonly timer: NodeJS.Timeout;
|
|
33
42
|
constructor(id: string, host: RelayHost, options: RelayHostOptions);
|
|
34
43
|
receive(text: string): Promise<void>;
|
|
@@ -37,6 +46,10 @@ declare class RelayChannel {
|
|
|
37
46
|
/** A frame of the files of the workspace, sealed as the rest. */
|
|
38
47
|
protected sendFrame(frame: RelayFilesFrame): void;
|
|
39
48
|
protected hello(text: string): Promise<void>;
|
|
49
|
+
/** The first sealed message of a viewer of content: its proof, then the content; a wrong one is counted, and refused. */
|
|
50
|
+
protected proven(cipher: RelayCipher, content: RelayContentShare, data: string): Promise<void>;
|
|
51
|
+
/** The key did not open: told in the clear (the viewer cannot open what is sealed), and counted. */
|
|
52
|
+
protected wrongKey(content: RelayContentShare): void;
|
|
40
53
|
/** A package handed on: what it is, its bytes in chunks, the end; then the channel closes. It never reaches ta serve's socket. */
|
|
41
54
|
protected sendShare(cipher: RelayCipher, share: RelayShare): Promise<void>;
|
|
42
55
|
protected connectLocal(cipher: RelayCipher): void;
|
|
@@ -58,7 +71,16 @@ export declare class RelayHost {
|
|
|
58
71
|
protected timer?: ReturnType<typeof setTimeout>;
|
|
59
72
|
protected pingTimer?: ReturnType<typeof setInterval>;
|
|
60
73
|
protected readonly channels: Map<string, RelayChannel>;
|
|
74
|
+
/** Wrong keys of each share of content, of late. */
|
|
75
|
+
protected readonly wrong: Map<string, {
|
|
76
|
+
count: number;
|
|
77
|
+
since: number;
|
|
78
|
+
}>;
|
|
61
79
|
constructor(options: RelayHostOptions);
|
|
80
|
+
/** @internal Whether a share of content had too many wrong keys of late. */
|
|
81
|
+
blocked(contentId: string, now?: number): boolean;
|
|
82
|
+
/** @internal */
|
|
83
|
+
wrongKey(contentId: string, now?: number): void;
|
|
62
84
|
/** Whether the connection to the relay is open. */
|
|
63
85
|
get online(): boolean;
|
|
64
86
|
/** Connects when relay.json has a relay and a device, and keeps connected; checks again on {@link refresh}. */
|
package/esm/node/relay-host.js
CHANGED
|
@@ -4,6 +4,9 @@ import { RelayFilesUtil } from '../common/relay-files.js';
|
|
|
4
4
|
import { RelayPairingUtil } from '../common/relay-pairing.js';
|
|
5
5
|
import { RELAY_HOST_PATH, RELAY_PING, RELAY_PING_MS, RELAY_PONG, RELAY_PROOF, RELAY_SHARE_CHUNK_BYTES, RELAY_SHARE_MAX_BYTES, RelayCloseCode, } from '../common/relay-protocol.js';
|
|
6
6
|
import { RelayFilesHost } from './relay-files-host.js';
|
|
7
|
+
/** Wrong keys (passwords) for a share before it is refused a while, and for how long. */
|
|
8
|
+
const MAX_WRONG_KEYS = 10;
|
|
9
|
+
const WRONG_KEY_WINDOW_MS = 10 * 60 * 1000;
|
|
7
10
|
const MAX_RECONNECT_DELAY_MS = 30000;
|
|
8
11
|
const HELLO_TIMEOUT_MS = 10000;
|
|
9
12
|
/**
|
|
@@ -17,6 +20,8 @@ class RelayChannel {
|
|
|
17
20
|
this.host = host;
|
|
18
21
|
this.options = options;
|
|
19
22
|
this.early = [];
|
|
23
|
+
/** Agreed in the hello of a viewer of content, set once it proved. */
|
|
24
|
+
this.compress = false;
|
|
20
25
|
this.timer = setTimeout(() => this.close(), HELLO_TIMEOUT_MS);
|
|
21
26
|
}
|
|
22
27
|
async receive(text) {
|
|
@@ -25,14 +30,42 @@ class RelayChannel {
|
|
|
25
30
|
await this.hello(text);
|
|
26
31
|
return;
|
|
27
32
|
}
|
|
33
|
+
if (this.content && this.candidates) {
|
|
34
|
+
// Whose key it is, the proof says: the relay and the others do not learn who looks.
|
|
35
|
+
const content = this.content;
|
|
36
|
+
for (const candidate of this.candidates) {
|
|
37
|
+
const opened = await candidate.open(text).catch(() => undefined);
|
|
38
|
+
if (opened !== undefined) {
|
|
39
|
+
this.cipher = candidate;
|
|
40
|
+
this.candidates = undefined;
|
|
41
|
+
await this.proven(candidate, content, opened);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
this.candidates = undefined;
|
|
46
|
+
this.wrongKey(content);
|
|
47
|
+
this.close();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
28
50
|
let data;
|
|
29
51
|
try {
|
|
30
52
|
data = await this.cipher.open(text);
|
|
31
53
|
}
|
|
32
54
|
catch (_c) {
|
|
55
|
+
if (this.content) {
|
|
56
|
+
this.wrongKey(this.content);
|
|
57
|
+
}
|
|
33
58
|
this.close();
|
|
34
59
|
return;
|
|
35
60
|
}
|
|
61
|
+
if (this.content) {
|
|
62
|
+
await this.proven(this.cipher, this.content, data);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (this.mode === 'share' || this.mode === 'content') {
|
|
66
|
+
// A link hands on what it shares and nothing else: what its holder sends never opens ta serve's socket or files.
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
36
69
|
if (!this.mode) {
|
|
37
70
|
this.mode = RelayFilesUtil.isHello(data) ? 'files' : 'rpc';
|
|
38
71
|
if (this.mode === 'files') {
|
|
@@ -57,6 +90,7 @@ class RelayChannel {
|
|
|
57
90
|
closed() {
|
|
58
91
|
var _a, _b;
|
|
59
92
|
clearTimeout(this.timer);
|
|
93
|
+
clearTimeout(this.proofTimer);
|
|
60
94
|
(_a = this.files) === null || _a === void 0 ? void 0 : _a.close();
|
|
61
95
|
(_b = this.local) === null || _b === void 0 ? void 0 : _b.close();
|
|
62
96
|
}
|
|
@@ -66,19 +100,26 @@ class RelayChannel {
|
|
|
66
100
|
(_a = this.cipher) === null || _a === void 0 ? void 0 : _a.seal(JSON.stringify(frame)).then(sealed => this.host.send({ t: 'data', ch: this.id, d: sealed }));
|
|
67
101
|
}
|
|
68
102
|
async hello(text) {
|
|
69
|
-
var _a;
|
|
103
|
+
var _a, _b, _c;
|
|
70
104
|
let hello;
|
|
71
105
|
try {
|
|
72
106
|
hello = JSON.parse(text);
|
|
73
107
|
}
|
|
74
|
-
catch (
|
|
108
|
+
catch (_d) {
|
|
75
109
|
this.close();
|
|
76
110
|
return;
|
|
77
111
|
}
|
|
78
112
|
const valid = hello.t === 'hello' && hello.v === 1 && typeof hello.nonce === 'string';
|
|
79
113
|
const device = valid ? this.options.store.device(hello.device) : undefined;
|
|
80
114
|
const share = valid && !device && this.options.sharePackage ? this.options.store.share(hello.device) : undefined;
|
|
81
|
-
const
|
|
115
|
+
const content = valid && !device && !share && this.options.shareContent ? this.options.store.content(hello.device) : undefined;
|
|
116
|
+
if (content && this.host.blocked(content.id)) {
|
|
117
|
+
this.answer({ t: 'refused', reason: 'busy' });
|
|
118
|
+
this.close();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const people = (content === null || content === void 0 ? void 0 : content.access) === 'people' ? ((_a = content.people) !== null && _a !== void 0 ? _a : []).map(person => person.key) : undefined;
|
|
122
|
+
const key = (_c = (_b = device === null || device === void 0 ? void 0 : device.key) !== null && _b !== void 0 ? _b : share === null || share === void 0 ? void 0 : share.key) !== null && _c !== void 0 ? _c : ((content === null || content === void 0 ? void 0 : content.access) === 'self' ? undefined : people ? people[0] : content === null || content === void 0 ? void 0 : content.key);
|
|
82
123
|
if (!key) {
|
|
83
124
|
this.answer({ t: 'refused', reason: 'This device is not paired.' });
|
|
84
125
|
this.close();
|
|
@@ -88,7 +129,25 @@ class RelayChannel {
|
|
|
88
129
|
const nonce = RelayBytes.random(16);
|
|
89
130
|
const cipher = await RelayCipher.derive(RelayBytes.fromBase64(key), RelayBytes.fromBase64(hello.nonce), nonce, 'host');
|
|
90
131
|
this.cipher = cipher;
|
|
132
|
+
if (people) {
|
|
133
|
+
this.candidates = [
|
|
134
|
+
cipher,
|
|
135
|
+
...(await Promise.all(people.slice(1).map(other => RelayCipher.derive(RelayBytes.fromBase64(other), RelayBytes.fromBase64(hello.nonce), nonce, 'host')))),
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
if (share) {
|
|
139
|
+
this.mode = 'share';
|
|
140
|
+
}
|
|
91
141
|
const compress = hello.z === 1 && RelayDeflate.supported();
|
|
142
|
+
if (content) {
|
|
143
|
+
// The viewer proves first: what this side seals would let one with the link but not the password try passwords offline.
|
|
144
|
+
this.content = content;
|
|
145
|
+
this.compress = compress;
|
|
146
|
+
// A viewer that never proves would hold one of the places of the devices.
|
|
147
|
+
this.proofTimer = setTimeout(() => this.close(), HELLO_TIMEOUT_MS);
|
|
148
|
+
this.answer({ t: 'ready', nonce: RelayBytes.toBase64(nonce), ...(compress ? { z: 1 } : {}) });
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
92
151
|
const proof = await cipher.seal(RELAY_PROOF);
|
|
93
152
|
// The messages after the proof, both ways: the device sets it once it opened the proof.
|
|
94
153
|
cipher.compress = compress;
|
|
@@ -97,6 +156,44 @@ class RelayChannel {
|
|
|
97
156
|
await this.sendShare(cipher, share);
|
|
98
157
|
}
|
|
99
158
|
}
|
|
159
|
+
/** The first sealed message of a viewer of content: its proof, then the content; a wrong one is counted, and refused. */
|
|
160
|
+
async proven(cipher, content, data) {
|
|
161
|
+
this.content = undefined;
|
|
162
|
+
clearTimeout(this.proofTimer);
|
|
163
|
+
if (data !== RELAY_PROOF) {
|
|
164
|
+
this.wrongKey(content);
|
|
165
|
+
this.close();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
cipher.compress = this.compress;
|
|
169
|
+
this.mode = 'content';
|
|
170
|
+
const send = async (message) => this.host.send({ t: 'data', ch: this.id, d: await cipher.seal(JSON.stringify(message)) });
|
|
171
|
+
const bytes = this.options.store.content(content.id) ? this.options.store.contentData(content) : undefined;
|
|
172
|
+
if (!bytes) {
|
|
173
|
+
await send({ t: 'error', message: 'This link no longer works: the share was stopped or ran out.' });
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
await send({
|
|
177
|
+
t: 'content',
|
|
178
|
+
kind: content.kind,
|
|
179
|
+
title: content.title,
|
|
180
|
+
size: bytes.length,
|
|
181
|
+
createdAt: content.createdAt,
|
|
182
|
+
...(content.type ? { type: content.type } : {}),
|
|
183
|
+
});
|
|
184
|
+
for (let at = 0; at < bytes.length; at += RELAY_SHARE_CHUNK_BYTES) {
|
|
185
|
+
await send({ t: 'chunk', d: RelayBytes.toBase64(bytes.subarray(at, at + RELAY_SHARE_CHUNK_BYTES)) });
|
|
186
|
+
}
|
|
187
|
+
await send({ t: 'end' });
|
|
188
|
+
}
|
|
189
|
+
this.close();
|
|
190
|
+
}
|
|
191
|
+
/** The key did not open: told in the clear (the viewer cannot open what is sealed), and counted. */
|
|
192
|
+
wrongKey(content) {
|
|
193
|
+
this.content = undefined;
|
|
194
|
+
this.answer({ t: 'refused', reason: 'key' });
|
|
195
|
+
this.host.wrongKey(content.id);
|
|
196
|
+
}
|
|
100
197
|
/** A package handed on: what it is, its bytes in chunks, the end; then the channel closes. It never reaches ta serve's socket. */
|
|
101
198
|
async sendShare(cipher, share) {
|
|
102
199
|
const send = async (message) => this.host.send({ t: 'data', ch: this.id, d: await cipher.seal(JSON.stringify(message)) });
|
|
@@ -134,6 +231,7 @@ class RelayChannel {
|
|
|
134
231
|
}
|
|
135
232
|
close() {
|
|
136
233
|
clearTimeout(this.timer);
|
|
234
|
+
clearTimeout(this.proofTimer);
|
|
137
235
|
this.host.closeChannel(this.id);
|
|
138
236
|
}
|
|
139
237
|
}
|
|
@@ -148,6 +246,18 @@ export class RelayHost {
|
|
|
148
246
|
this.stopped = false;
|
|
149
247
|
this.attempts = 0;
|
|
150
248
|
this.channels = new Map();
|
|
249
|
+
/** Wrong keys of each share of content, of late. */
|
|
250
|
+
this.wrong = new Map();
|
|
251
|
+
}
|
|
252
|
+
/** @internal Whether a share of content had too many wrong keys of late. */
|
|
253
|
+
blocked(contentId, now = Date.now()) {
|
|
254
|
+
const wrong = this.wrong.get(contentId);
|
|
255
|
+
return !!wrong && now - wrong.since < WRONG_KEY_WINDOW_MS && wrong.count >= MAX_WRONG_KEYS;
|
|
256
|
+
}
|
|
257
|
+
/** @internal */
|
|
258
|
+
wrongKey(contentId, now = Date.now()) {
|
|
259
|
+
const wrong = this.wrong.get(contentId);
|
|
260
|
+
this.wrong.set(contentId, wrong && now - wrong.since < WRONG_KEY_WINDOW_MS ? { count: wrong.count + 1, since: wrong.since } : { count: 1, since: now });
|
|
151
261
|
}
|
|
152
262
|
/** Whether the connection to the relay is open. */
|
|
153
263
|
get online() {
|
|
@@ -162,7 +272,8 @@ export class RelayHost {
|
|
|
162
272
|
/** relay.json may have changed: connect if it now has devices, or leave if it has none. */
|
|
163
273
|
refresh() {
|
|
164
274
|
const { url, host, devices } = this.options.store.read();
|
|
165
|
-
const shared = !!this.options.sharePackage && this.options.store.shares().length > 0
|
|
275
|
+
const shared = (!!this.options.sharePackage && this.options.store.shares().length > 0) ||
|
|
276
|
+
(!!this.options.shareContent && this.options.store.contents().some(content => content.access !== 'self'));
|
|
166
277
|
const wanted = !this.stopped && url !== undefined && host !== undefined && (devices.length > 0 || shared);
|
|
167
278
|
if (!wanted) {
|
|
168
279
|
this.disconnect();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RelayHubsStore, RelayHubsStoreName } from '../common/relay-hubs-protocol.js';
|
|
2
|
+
/** What the rooms of a Ta keep, as files of its agent directory readable by the user alone: `identity.json`, `rooms.json`, `friends.json`. */
|
|
3
|
+
export declare class RelayHubFiles implements RelayHubsStore {
|
|
4
|
+
readonly agentDir: string;
|
|
5
|
+
constructor(agentDir: string);
|
|
6
|
+
file(name: RelayHubsStoreName): string;
|
|
7
|
+
read<T>(name: RelayHubsStoreName): T | undefined;
|
|
8
|
+
/** Written whole, then renamed over the file, readable by the user alone: it holds keys. */
|
|
9
|
+
write(name: RelayHubsStoreName, value: unknown): void;
|
|
10
|
+
/** The name of a new identity: the user's on this system. */
|
|
11
|
+
static userName(): string;
|
|
12
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { chmodSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { userInfo } from 'node:os';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
/** What the rooms of a Ta keep, as files of its agent directory readable by the user alone: `identity.json`, `rooms.json`, `friends.json`. */
|
|
5
|
+
export class RelayHubFiles {
|
|
6
|
+
constructor(agentDir) {
|
|
7
|
+
this.agentDir = agentDir;
|
|
8
|
+
}
|
|
9
|
+
file(name) {
|
|
10
|
+
return join(this.agentDir, `${name}.json`);
|
|
11
|
+
}
|
|
12
|
+
read(name) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(readFileSync(this.file(name), 'utf8'));
|
|
15
|
+
}
|
|
16
|
+
catch (_a) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Written whole, then renamed over the file, readable by the user alone: it holds keys. */
|
|
21
|
+
write(name, value) {
|
|
22
|
+
const file = this.file(name);
|
|
23
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
24
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
25
|
+
writeFileSync(temporary, `${JSON.stringify(value, undefined, 2)}\n`, { mode: 0o600 });
|
|
26
|
+
chmodSync(temporary, 0o600);
|
|
27
|
+
renameSync(temporary, file);
|
|
28
|
+
}
|
|
29
|
+
/** The name of a new identity: the user's on this system. */
|
|
30
|
+
static userName() {
|
|
31
|
+
try {
|
|
32
|
+
return userInfo().username || 'Ta';
|
|
33
|
+
}
|
|
34
|
+
catch (_a) {
|
|
35
|
+
return 'Ta';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=relay-hub-files.js.map
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { type IncomingMessage, type Server } from 'node:http';
|
|
1
|
+
import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
|
2
2
|
import type { Duplex } from 'node:stream';
|
|
3
3
|
import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
|
|
4
|
+
import { type RelayBlobStorage, RelayBlobs } from '../common/relay-blobs.js';
|
|
5
|
+
import { RelayHub, type RelayHubStorage, type RelayHubWho } from '../common/relay-hub.js';
|
|
4
6
|
import { RelayRoom } from '../common/relay-room.js';
|
|
5
7
|
export interface RelayServerOptions {
|
|
6
8
|
host?: string;
|
|
@@ -12,6 +14,37 @@ interface Room {
|
|
|
12
14
|
clients: Map<string, WebSocketConnection>;
|
|
13
15
|
relay: RelayRoom;
|
|
14
16
|
}
|
|
17
|
+
/** A copy of content in memory, deleted when it runs out. */
|
|
18
|
+
declare class MemoryBlobStorage implements RelayBlobStorage {
|
|
19
|
+
protected readonly values: Map<string, unknown>;
|
|
20
|
+
protected timer?: ReturnType<typeof setTimeout>;
|
|
21
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
22
|
+
put(key: string, value: unknown): Promise<void>;
|
|
23
|
+
deleteAll(): Promise<void>;
|
|
24
|
+
/** No copy is kept: nothing of it need stay in the relay's memory. */
|
|
25
|
+
get empty(): boolean;
|
|
26
|
+
setAlarm(at: number): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/** A room in memory, as a Durable Object keeps it. */
|
|
29
|
+
declare class MemoryHubStorage implements RelayHubStorage {
|
|
30
|
+
protected readonly values: Map<string, unknown>;
|
|
31
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
32
|
+
put(key: string, value: unknown): Promise<void>;
|
|
33
|
+
delete(key: string): Promise<boolean>;
|
|
34
|
+
list<T>(options: {
|
|
35
|
+
prefix: string;
|
|
36
|
+
}): Promise<Map<string, T>>;
|
|
37
|
+
deleteAll(): Promise<void>;
|
|
38
|
+
/** No room was made, or it was closed. */
|
|
39
|
+
get empty(): boolean;
|
|
40
|
+
}
|
|
41
|
+
interface Hub {
|
|
42
|
+
hub: RelayHub;
|
|
43
|
+
storage: MemoryHubStorage;
|
|
44
|
+
sockets: Map<WebSocketConnection, {
|
|
45
|
+
who?: RelayHubWho;
|
|
46
|
+
}>;
|
|
47
|
+
}
|
|
15
48
|
/**
|
|
16
49
|
* `ta relay`: the relay on a machine of one's own, as the Cloudflare Worker is in the cloud. It forwards,
|
|
17
50
|
* and keeps the hash of each host's secret in memory, for as long as it runs.
|
|
@@ -19,14 +52,26 @@ interface Room {
|
|
|
19
52
|
export declare class RelayServer {
|
|
20
53
|
protected readonly options: RelayServerOptions;
|
|
21
54
|
protected readonly rooms: Map<string, Room>;
|
|
55
|
+
protected readonly blobs: Map<string, {
|
|
56
|
+
blobs: RelayBlobs;
|
|
57
|
+
storage: MemoryBlobStorage;
|
|
58
|
+
pending: number;
|
|
59
|
+
}>;
|
|
60
|
+
protected readonly hubs: Map<string, Hub>;
|
|
22
61
|
protected server?: Server;
|
|
23
62
|
constructor(options?: RelayServerOptions);
|
|
24
63
|
listen(): Promise<string>;
|
|
25
64
|
close(): Promise<void>;
|
|
65
|
+
/** A copy of content (RelayBlobs), kept in memory for as long as the relay runs. */
|
|
66
|
+
protected blob(id: string, request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
67
|
+
/** The body of a request, or undefined past `max` bytes: the rest is read and dropped. */
|
|
68
|
+
protected static body(request: IncomingMessage, max: number): Promise<Buffer<ArrayBuffer> | undefined>;
|
|
26
69
|
protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
|
|
27
70
|
/** The first host to come with an id keeps it, for as long as the relay runs. */
|
|
28
71
|
protected openHost(id: string, secret: string, connection: WebSocketConnection): void;
|
|
29
72
|
protected openClient(id: string, connection: WebSocketConnection): void;
|
|
73
|
+
/** A connection to the hub of a room (RelayHub), kept in memory for as long as the relay runs. */
|
|
74
|
+
protected openHub(id: string, connection: WebSocketConnection): void;
|
|
30
75
|
protected createRoom(id: string, secretHash: string): Room;
|
|
31
76
|
protected static same(a: string, b: string): boolean;
|
|
32
77
|
}
|
package/esm/node/relay-server.js
CHANGED
|
@@ -1,9 +1,71 @@
|
|
|
1
1
|
import { createHash, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
2
|
import { createServer } from 'node:http';
|
|
3
3
|
import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
|
|
4
|
-
import {
|
|
4
|
+
import { RelayBlobs } from '../common/relay-blobs.js';
|
|
5
|
+
import { RelayHub } from '../common/relay-hub.js';
|
|
6
|
+
import { RELAY_BLOB_PATH, RELAY_CONNECT_PATH, RELAY_CONTENT_PATH, RELAY_FRAME_HEADER_CSP, RELAY_FRAME_PATH, RELAY_HOST_PATH, RELAY_HUB_PATH, RELAY_ID, RELAY_PAIR_PAGE, RELAY_PAIR_PATH, RELAY_ROOM_PATH, RELAY_SHARE_PAGE, RELAY_SHARE_PATH, RELAY_VIEWER_HEADERS, RelayCloseCode, } from '../common/relay-protocol.js';
|
|
5
7
|
import { RelayRoom } from '../common/relay-room.js';
|
|
8
|
+
import { RelayViewer } from './relay-viewer.js';
|
|
6
9
|
const WEBSOCKET = { maxPayload: 32 * 1024 * 1024, heartbeat: 30000 };
|
|
10
|
+
/** A copy of content in memory, deleted when it runs out. */
|
|
11
|
+
class MemoryBlobStorage {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.values = new Map();
|
|
14
|
+
}
|
|
15
|
+
async get(key) {
|
|
16
|
+
return this.values.get(key);
|
|
17
|
+
}
|
|
18
|
+
async put(key, value) {
|
|
19
|
+
this.values.set(key, value);
|
|
20
|
+
}
|
|
21
|
+
async deleteAll() {
|
|
22
|
+
this.values.clear();
|
|
23
|
+
clearTimeout(this.timer);
|
|
24
|
+
}
|
|
25
|
+
/** No copy is kept: nothing of it need stay in the relay's memory. */
|
|
26
|
+
get empty() {
|
|
27
|
+
return this.values.size === 0;
|
|
28
|
+
}
|
|
29
|
+
async setAlarm(at) {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
clearTimeout(this.timer);
|
|
32
|
+
// Past the 24 days of a timer, it is found run out on the next read.
|
|
33
|
+
this.timer = setTimeout(() => this.values.clear(), Math.min(Math.max(at - Date.now(), 0), 2 ** 31 - 1));
|
|
34
|
+
(_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** A room in memory, as a Durable Object keeps it. */
|
|
38
|
+
class MemoryHubStorage {
|
|
39
|
+
constructor() {
|
|
40
|
+
this.values = new Map();
|
|
41
|
+
}
|
|
42
|
+
async get(key) {
|
|
43
|
+
const value = this.values.get(key);
|
|
44
|
+
return value === undefined ? undefined : structuredClone(value);
|
|
45
|
+
}
|
|
46
|
+
async put(key, value) {
|
|
47
|
+
this.values.set(key, structuredClone(value));
|
|
48
|
+
}
|
|
49
|
+
async delete(key) {
|
|
50
|
+
return this.values.delete(key);
|
|
51
|
+
}
|
|
52
|
+
async list(options) {
|
|
53
|
+
const found = new Map();
|
|
54
|
+
for (const [key, value] of [...this.values].sort(([a], [b]) => a.localeCompare(b))) {
|
|
55
|
+
if (key.startsWith(options.prefix)) {
|
|
56
|
+
found.set(key, structuredClone(value));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return found;
|
|
60
|
+
}
|
|
61
|
+
async deleteAll() {
|
|
62
|
+
this.values.clear();
|
|
63
|
+
}
|
|
64
|
+
/** No room was made, or it was closed. */
|
|
65
|
+
get empty() {
|
|
66
|
+
return this.values.size === 0;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
7
69
|
const peer = (connection) => ({ send: text => connection.send(text), close: (code, reason) => connection.close(code, reason) });
|
|
8
70
|
/**
|
|
9
71
|
* `ta relay`: the relay on a machine of one's own, as the Cloudflare Worker is in the cloud. It forwards,
|
|
@@ -13,11 +75,37 @@ export class RelayServer {
|
|
|
13
75
|
constructor(options = {}) {
|
|
14
76
|
this.options = options;
|
|
15
77
|
this.rooms = new Map();
|
|
78
|
+
this.blobs = new Map();
|
|
79
|
+
this.hubs = new Map();
|
|
16
80
|
}
|
|
17
81
|
listen() {
|
|
18
82
|
const server = createServer((request, response) => {
|
|
19
83
|
var _a;
|
|
20
84
|
const path = ((_a = request.url) !== null && _a !== void 0 ? _a : '/').split(/[?#]/)[0];
|
|
85
|
+
if (path.startsWith(RELAY_BLOB_PATH)) {
|
|
86
|
+
this.blob(path.slice(RELAY_BLOB_PATH.length), request, response).catch(() => {
|
|
87
|
+
// A request aborted or that failed: an unhandled rejection would end the relay.
|
|
88
|
+
if (!response.headersSent) {
|
|
89
|
+
response.writeHead(400);
|
|
90
|
+
}
|
|
91
|
+
response.end();
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (path === RELAY_ROOM_PATH) {
|
|
96
|
+
response.writeHead(200, { ...RELAY_VIEWER_HEADERS, 'x-frame-options': 'DENY' });
|
|
97
|
+
response.end(RelayViewer.room());
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (path === RELAY_CONTENT_PATH || path === RELAY_FRAME_PATH) {
|
|
101
|
+
const frame = path === RELAY_FRAME_PATH;
|
|
102
|
+
response.writeHead(200, {
|
|
103
|
+
...RELAY_VIEWER_HEADERS,
|
|
104
|
+
...(frame ? { 'content-security-policy': RELAY_FRAME_HEADER_CSP } : { 'x-frame-options': 'DENY' }),
|
|
105
|
+
});
|
|
106
|
+
response.end(frame ? RelayViewer.frame() : RelayViewer.page());
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
21
109
|
const page = path === RELAY_PAIR_PATH ? RELAY_PAIR_PAGE : path === RELAY_SHARE_PATH ? RELAY_SHARE_PAGE : undefined;
|
|
22
110
|
response.writeHead(200, { 'content-type': page ? 'text/html; charset=utf-8' : 'text/plain' });
|
|
23
111
|
response.end(page !== null && page !== void 0 ? page : 'Ta relay\n');
|
|
@@ -35,6 +123,12 @@ export class RelayServer {
|
|
|
35
123
|
}
|
|
36
124
|
close() {
|
|
37
125
|
var _a;
|
|
126
|
+
for (const { sockets } of this.hubs.values()) {
|
|
127
|
+
for (const connection of sockets.keys()) {
|
|
128
|
+
connection.close(1001, 'The relay is stopping.');
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
this.hubs.clear();
|
|
38
132
|
for (const room of this.rooms.values()) {
|
|
39
133
|
(_a = room.host) === null || _a === void 0 ? void 0 : _a.close(1001, 'The relay is stopping.');
|
|
40
134
|
for (const client of room.clients.values()) {
|
|
@@ -44,6 +138,70 @@ export class RelayServer {
|
|
|
44
138
|
this.rooms.clear();
|
|
45
139
|
return new Promise(resolve => (this.server ? this.server.close(() => resolve()) : resolve()));
|
|
46
140
|
}
|
|
141
|
+
/** A copy of content (RelayBlobs), kept in memory for as long as the relay runs. */
|
|
142
|
+
async blob(id, request, response) {
|
|
143
|
+
var _a;
|
|
144
|
+
if (!RELAY_ID.test(id)) {
|
|
145
|
+
response.writeHead(404).end();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const read = await RelayServer.body(request, RelayBlobs.MAX_BYTES);
|
|
149
|
+
if (!read) {
|
|
150
|
+
// Refused before it is all in memory, whatever its content-length said.
|
|
151
|
+
response.writeHead(413, { connection: 'close' }).end('Too large.\n');
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
// Kept only while it holds a copy: requests for ids of no copy leave nothing behind.
|
|
155
|
+
let copy = this.blobs.get(id);
|
|
156
|
+
if (!copy) {
|
|
157
|
+
const storage = new MemoryBlobStorage();
|
|
158
|
+
copy = { blobs: new RelayBlobs(storage), storage, pending: 0 };
|
|
159
|
+
this.blobs.set(id, copy);
|
|
160
|
+
}
|
|
161
|
+
const headers = new Headers();
|
|
162
|
+
for (const [name, value] of Object.entries(request.headers)) {
|
|
163
|
+
if (typeof value === 'string') {
|
|
164
|
+
headers.set(name, value);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const method = (_a = request.method) !== null && _a !== void 0 ? _a : 'GET';
|
|
168
|
+
const body = read.length && method !== 'GET' && method !== 'HEAD' ? read : undefined;
|
|
169
|
+
copy.pending++;
|
|
170
|
+
let answer;
|
|
171
|
+
try {
|
|
172
|
+
answer = await copy.blobs.handle(new Request(`http://relay${request.url}`, { method, headers, body }));
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
copy.pending--;
|
|
176
|
+
if (copy.pending === 0 && copy.storage.empty && this.blobs.get(id) === copy) {
|
|
177
|
+
this.blobs.delete(id);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
response.writeHead(answer.status, Object.fromEntries(answer.headers));
|
|
181
|
+
response.end(Buffer.from(await answer.arrayBuffer()));
|
|
182
|
+
}
|
|
183
|
+
/** The body of a request, or undefined past `max` bytes: the rest is read and dropped. */
|
|
184
|
+
static body(request, max) {
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
const chunks = [];
|
|
187
|
+
let size = 0;
|
|
188
|
+
request.on('data', (chunk) => {
|
|
189
|
+
if (size < 0) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
size += chunk.length;
|
|
193
|
+
if (size > max) {
|
|
194
|
+
size = -1;
|
|
195
|
+
chunks.length = 0;
|
|
196
|
+
resolve(undefined);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
chunks.push(chunk);
|
|
200
|
+
});
|
|
201
|
+
request.on('end', () => size >= 0 && resolve(Buffer.concat(chunks)));
|
|
202
|
+
request.on('error', reject);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
47
205
|
upgrade(request, socket, head) {
|
|
48
206
|
var _a, _b;
|
|
49
207
|
const url = new URL((_a = request.url) !== null && _a !== void 0 ? _a : '/', 'http://relay');
|
|
@@ -52,6 +210,15 @@ export class RelayServer {
|
|
|
52
210
|
WebSocketConnection.refuse(socket, checked.status, checked.message);
|
|
53
211
|
return;
|
|
54
212
|
}
|
|
213
|
+
if (url.pathname.startsWith(RELAY_HUB_PATH)) {
|
|
214
|
+
const id = url.pathname.slice(RELAY_HUB_PATH.length);
|
|
215
|
+
if (!RELAY_ID.test(id)) {
|
|
216
|
+
WebSocketConnection.refuse(socket, 404, 'Not found.');
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
this.openHub(id, WebSocketConnection.open(socket, checked.key, head, WEBSOCKET));
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
55
222
|
const [path, id] = url.pathname.startsWith(RELAY_HOST_PATH)
|
|
56
223
|
? [RELAY_HOST_PATH, url.pathname.slice(RELAY_HOST_PATH.length)]
|
|
57
224
|
: [RELAY_CONNECT_PATH, url.pathname.slice(RELAY_CONNECT_PATH.length)];
|
|
@@ -107,6 +274,65 @@ export class RelayServer {
|
|
|
107
274
|
}
|
|
108
275
|
});
|
|
109
276
|
}
|
|
277
|
+
/** A connection to the hub of a room (RelayHub), kept in memory for as long as the relay runs. */
|
|
278
|
+
openHub(id, connection) {
|
|
279
|
+
let hub = this.hubs.get(id);
|
|
280
|
+
if (!hub) {
|
|
281
|
+
const sockets = new Map();
|
|
282
|
+
const socketOf = (other) => {
|
|
283
|
+
var _a;
|
|
284
|
+
const state = (_a = sockets.get(other)) !== null && _a !== void 0 ? _a : {};
|
|
285
|
+
return {
|
|
286
|
+
key: other,
|
|
287
|
+
get who() {
|
|
288
|
+
return state.who;
|
|
289
|
+
},
|
|
290
|
+
set who(who) {
|
|
291
|
+
state.who = who;
|
|
292
|
+
},
|
|
293
|
+
send: text => other.send(text),
|
|
294
|
+
close: (code, reason) => other.close(code, reason),
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
const storage = new MemoryHubStorage();
|
|
298
|
+
hub = { sockets, storage, hub: new RelayHub(storage, () => [...sockets.keys()].map(socketOf)) };
|
|
299
|
+
this.hubs.set(id, hub);
|
|
300
|
+
}
|
|
301
|
+
const current = hub;
|
|
302
|
+
const { sockets, hub: relay } = current;
|
|
303
|
+
sockets.set(connection, {});
|
|
304
|
+
let chain = Promise.resolve();
|
|
305
|
+
const socket = {
|
|
306
|
+
key: connection,
|
|
307
|
+
get who() {
|
|
308
|
+
var _a;
|
|
309
|
+
return (_a = sockets.get(connection)) === null || _a === void 0 ? void 0 : _a.who;
|
|
310
|
+
},
|
|
311
|
+
set who(who) {
|
|
312
|
+
const state = sockets.get(connection);
|
|
313
|
+
if (state) {
|
|
314
|
+
state.who = who;
|
|
315
|
+
}
|
|
316
|
+
},
|
|
317
|
+
send: text => connection.send(text),
|
|
318
|
+
close: (code, reason) => connection.close(code, reason),
|
|
319
|
+
};
|
|
320
|
+
connection.onMessage(text => {
|
|
321
|
+
chain = chain.then(() => relay.receive(socket, text)).catch(() => undefined);
|
|
322
|
+
});
|
|
323
|
+
connection.onClose(() => {
|
|
324
|
+
void chain.then(() => {
|
|
325
|
+
if (sockets.has(connection)) {
|
|
326
|
+
relay.closed(socket);
|
|
327
|
+
sockets.delete(connection);
|
|
328
|
+
}
|
|
329
|
+
// Connections to ids of no room (refused, or the room closed) leave nothing behind.
|
|
330
|
+
if (sockets.size === 0 && current.storage.empty && this.hubs.get(id) === current) {
|
|
331
|
+
this.hubs.delete(id);
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
}
|
|
110
336
|
createRoom(id, secretHash) {
|
|
111
337
|
const room = {
|
|
112
338
|
secretHash,
|