@abraca/dabra 2.90.0 → 2.91.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/package.json +5 -11
- package/src/AbracadabraBaseProvider.ts +0 -963
- package/src/AbracadabraClient.ts +0 -1960
- package/src/AbracadabraProvider.ts +0 -915
- package/src/AbracadabraWS.ts +0 -695
- package/src/BackgroundSyncManager.ts +0 -856
- package/src/BackgroundSyncPersistence.ts +0 -140
- package/src/ChatClient.ts +0 -314
- package/src/CloseEvents.ts +0 -49
- package/src/ContentManager.ts +0 -246
- package/src/CoverReconcile.ts +0 -168
- package/src/CryptoIdentityKeystore.ts +0 -754
- package/src/DeviceRegistrationService.ts +0 -257
- package/src/DocConverters.ts +0 -1859
- package/src/DocKeyManager.ts +0 -163
- package/src/DocTypes.ts +0 -628
- package/src/DocUtils.ts +0 -251
- package/src/DocumentCache.ts +0 -210
- package/src/DocumentManager.ts +0 -410
- package/src/E2EAbracadabraProvider.ts +0 -426
- package/src/E2EOfflineStore.ts +0 -55
- package/src/EncryptedChatClient.ts +0 -173
- package/src/EncryptedY.ts +0 -145
- package/src/EventEmitter.ts +0 -59
- package/src/FileBlobStore.ts +0 -569
- package/src/IdentityDoc.ts +0 -711
- package/src/IncomingMessage.ts +0 -63
- package/src/MessageReceiver.ts +0 -151
- package/src/MessageSender.ts +0 -22
- package/src/MetaManager.ts +0 -194
- package/src/MnemonicKeyDerivation.ts +0 -171
- package/src/NodeConnect.ts +0 -63
- package/src/NodeIdentity.ts +0 -378
- package/src/NotificationsClient.ts +0 -207
- package/src/OfflineStore.ts +0 -294
- package/src/OutgoingMessage.ts +0 -25
- package/src/OutgoingMessages/AuthenticationMessage.ts +0 -23
- package/src/OutgoingMessages/AwarenessMessage.ts +0 -41
- package/src/OutgoingMessages/CloseMessage.ts +0 -17
- package/src/OutgoingMessages/QueryAwarenessMessage.ts +0 -17
- package/src/OutgoingMessages/StatelessMessage.ts +0 -18
- package/src/OutgoingMessages/SubdocMessage.ts +0 -35
- package/src/OutgoingMessages/SyncStepOneMessage.ts +0 -25
- package/src/OutgoingMessages/SyncStepTwoMessage.ts +0 -25
- package/src/OutgoingMessages/UpdateMessage.ts +0 -20
- package/src/QueryClient.ts +0 -209
- package/src/RpcClient.ts +0 -659
- package/src/SchemaTypes.ts +0 -179
- package/src/SearchIndex.ts +0 -250
- package/src/ServerRegistry.ts +0 -220
- package/src/ServerReset.ts +0 -222
- package/src/TokenManager.ts +0 -337
- package/src/TreeManager.ts +0 -894
- package/src/TreeTimestamps.ts +0 -101
- package/src/auth.ts +0 -62
- package/src/awarenessStatesToArray.ts +0 -10
- package/src/index.ts +0 -219
- package/src/messageRecord.ts +0 -121
- package/src/sync/BroadcastChannelSync.ts +0 -235
- package/src/types.ts +0 -790
- package/src/webrtc/AbracadabraWebRTC.ts +0 -683
- package/src/webrtc/DataChannelRouter.ts +0 -179
- package/src/webrtc/DevicePairingChannel.ts +0 -496
- package/src/webrtc/E2EEChannel.ts +0 -195
- package/src/webrtc/FileTransferChannel.ts +0 -367
- package/src/webrtc/ManualSignaling.ts +0 -204
- package/src/webrtc/PeerConnection.ts +0 -133
- package/src/webrtc/SignalingSocket.ts +0 -378
- package/src/webrtc/YjsDataChannel.ts +0 -203
- package/src/webrtc/index.ts +0 -26
- package/src/webrtc/types.ts +0 -177
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ManualSignaling
|
|
3
|
-
*
|
|
4
|
-
* Serverless signaling adapter for WebRTC peer-to-peer connections.
|
|
5
|
-
* Instead of a WebSocket server relaying SDP/ICE, peers exchange
|
|
6
|
-
* offer and answer "blobs" out-of-band (QR code, copy-paste, NFC, etc.).
|
|
7
|
-
*
|
|
8
|
-
* Designed as a drop-in replacement for `SignalingSocket` — emits the
|
|
9
|
-
* same events (`welcome`, `joined`, `offer`, `answer`, `ice`) so
|
|
10
|
-
* `AbracadabraWebRTC` can use it transparently.
|
|
11
|
-
*
|
|
12
|
-
* Flow:
|
|
13
|
-
* Device A (initiator):
|
|
14
|
-
* 1. `createOffer()` → gathers ICE candidates → returns offer blob
|
|
15
|
-
* 2. Share blob via QR/paste
|
|
16
|
-
* 3. Receive answer blob → `acceptAnswer(blob)`
|
|
17
|
-
*
|
|
18
|
-
* Device B (responder):
|
|
19
|
-
* 1. Receive offer blob → `acceptOffer(blob)` → returns answer blob
|
|
20
|
-
* 2. Share answer blob via QR/paste
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import EventEmitter from "../EventEmitter.ts";
|
|
24
|
-
|
|
25
|
-
export interface ManualSignalingBlob {
|
|
26
|
-
/** SDP offer or answer. */
|
|
27
|
-
sdp: string;
|
|
28
|
-
/** Gathered ICE candidates. */
|
|
29
|
-
candidates: string[];
|
|
30
|
-
/** Peer ID of the sender. */
|
|
31
|
-
peerId: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export class ManualSignaling extends EventEmitter {
|
|
35
|
-
public localPeerId: string;
|
|
36
|
-
public isConnected = false;
|
|
37
|
-
|
|
38
|
-
private pc: RTCPeerConnection | null = null;
|
|
39
|
-
private iceServers: RTCIceServer[];
|
|
40
|
-
|
|
41
|
-
constructor(iceServers?: RTCIceServer[]) {
|
|
42
|
-
super();
|
|
43
|
-
this.localPeerId = crypto.randomUUID();
|
|
44
|
-
this.iceServers = iceServers ?? [{ urls: "stun:stun.l.google.com:19302" }];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Initiator: create an offer blob with gathered ICE candidates.
|
|
49
|
-
* Returns a blob to share with the remote peer.
|
|
50
|
-
*/
|
|
51
|
-
async createOfferBlob(): Promise<ManualSignalingBlob> {
|
|
52
|
-
this.pc = new RTCPeerConnection({ iceServers: this.iceServers });
|
|
53
|
-
// A data channel is required for ICE gathering to produce candidates.
|
|
54
|
-
this.pc.createDataChannel("manual-signaling");
|
|
55
|
-
|
|
56
|
-
const candidates: string[] = [];
|
|
57
|
-
const gatheringComplete = new Promise<void>((resolve) => {
|
|
58
|
-
this.pc!.onicecandidate = (event) => {
|
|
59
|
-
if (event.candidate) {
|
|
60
|
-
candidates.push(JSON.stringify(event.candidate.toJSON()));
|
|
61
|
-
} else {
|
|
62
|
-
// Null candidate = gathering complete.
|
|
63
|
-
resolve();
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const offer = await this.pc.createOffer();
|
|
69
|
-
await this.pc.setLocalDescription(offer);
|
|
70
|
-
await gatheringComplete;
|
|
71
|
-
|
|
72
|
-
// Emit welcome (as if we connected to signaling).
|
|
73
|
-
this.isConnected = true;
|
|
74
|
-
this.emit("welcome", { peerId: this.localPeerId, peers: [] });
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
sdp: this.pc.localDescription!.sdp,
|
|
78
|
-
candidates,
|
|
79
|
-
peerId: this.localPeerId,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Responder: accept an offer blob and create an answer blob.
|
|
85
|
-
* The answer blob should be shared back to the initiator.
|
|
86
|
-
*/
|
|
87
|
-
async acceptOffer(offerBlob: ManualSignalingBlob): Promise<ManualSignalingBlob> {
|
|
88
|
-
this.pc = new RTCPeerConnection({ iceServers: this.iceServers });
|
|
89
|
-
this.pc.createDataChannel("manual-signaling");
|
|
90
|
-
|
|
91
|
-
const candidates: string[] = [];
|
|
92
|
-
const gatheringComplete = new Promise<void>((resolve) => {
|
|
93
|
-
this.pc!.onicecandidate = (event) => {
|
|
94
|
-
if (event.candidate) {
|
|
95
|
-
candidates.push(JSON.stringify(event.candidate.toJSON()));
|
|
96
|
-
} else {
|
|
97
|
-
resolve();
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
// Set remote offer.
|
|
103
|
-
await this.pc.setRemoteDescription(
|
|
104
|
-
new RTCSessionDescription({ type: "offer", sdp: offerBlob.sdp }),
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
// Add remote ICE candidates.
|
|
108
|
-
for (const c of offerBlob.candidates) {
|
|
109
|
-
try {
|
|
110
|
-
await this.pc.addIceCandidate(new RTCIceCandidate(JSON.parse(c)));
|
|
111
|
-
} catch {
|
|
112
|
-
// Skip malformed ICE candidate
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Create answer.
|
|
117
|
-
const answer = await this.pc.createAnswer();
|
|
118
|
-
await this.pc.setLocalDescription(answer);
|
|
119
|
-
await gatheringComplete;
|
|
120
|
-
|
|
121
|
-
// Emit welcome + joined (remote peer from the offer).
|
|
122
|
-
this.isConnected = true;
|
|
123
|
-
this.emit("welcome", { peerId: this.localPeerId, peers: [] });
|
|
124
|
-
this.emit("joined", {
|
|
125
|
-
peer_id: offerBlob.peerId,
|
|
126
|
-
user_id: offerBlob.peerId,
|
|
127
|
-
muted: false,
|
|
128
|
-
video: false,
|
|
129
|
-
screen: false,
|
|
130
|
-
name: null,
|
|
131
|
-
color: null,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// Emit the offer so AbracadabraWebRTC handles it.
|
|
135
|
-
this.emit("offer", { from: offerBlob.peerId, sdp: offerBlob.sdp });
|
|
136
|
-
|
|
137
|
-
// Feed ICE candidates.
|
|
138
|
-
for (const c of offerBlob.candidates) {
|
|
139
|
-
this.emit("ice", { from: offerBlob.peerId, candidate: c });
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return {
|
|
143
|
-
sdp: this.pc.localDescription!.sdp,
|
|
144
|
-
candidates,
|
|
145
|
-
peerId: this.localPeerId,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Initiator: accept the answer blob from the responder.
|
|
151
|
-
* Completes the connection.
|
|
152
|
-
*/
|
|
153
|
-
async acceptAnswer(answerBlob: ManualSignalingBlob): Promise<void> {
|
|
154
|
-
if (!this.pc) throw new Error("Call createOfferBlob() first");
|
|
155
|
-
|
|
156
|
-
// Emit joined for the remote peer.
|
|
157
|
-
this.emit("joined", {
|
|
158
|
-
peer_id: answerBlob.peerId,
|
|
159
|
-
user_id: answerBlob.peerId,
|
|
160
|
-
muted: false,
|
|
161
|
-
video: false,
|
|
162
|
-
screen: false,
|
|
163
|
-
name: null,
|
|
164
|
-
color: null,
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
// Emit the answer so AbracadabraWebRTC handles it.
|
|
168
|
-
this.emit("answer", { from: answerBlob.peerId, sdp: answerBlob.sdp });
|
|
169
|
-
|
|
170
|
-
// Feed ICE candidates.
|
|
171
|
-
for (const c of answerBlob.candidates) {
|
|
172
|
-
this.emit("ice", { from: answerBlob.peerId, candidate: c });
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// ── SignalingSocket-compatible stubs ─────────────────────────────────────
|
|
177
|
-
// These are no-ops since manual signaling doesn't relay through a server.
|
|
178
|
-
|
|
179
|
-
sendOffer(_to: string, _sdp: string): void {}
|
|
180
|
-
sendAnswer(_to: string, _sdp: string): void {}
|
|
181
|
-
sendIce(_to: string, _candidate: string): void {}
|
|
182
|
-
sendMute(_muted: boolean): void {}
|
|
183
|
-
sendMediaState(_video: boolean, _screen: boolean): void {}
|
|
184
|
-
sendProfile(_name: string, _color: string): void {}
|
|
185
|
-
sendLeave(): void {}
|
|
186
|
-
|
|
187
|
-
async connect(): Promise<void> {
|
|
188
|
-
// No-op — connection is initiated via createOfferBlob() or acceptOffer().
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
disconnect(): void {
|
|
192
|
-
this.isConnected = false;
|
|
193
|
-
if (this.pc) {
|
|
194
|
-
this.pc.close();
|
|
195
|
-
this.pc = null;
|
|
196
|
-
}
|
|
197
|
-
this.emit("disconnected");
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
destroy(): void {
|
|
201
|
-
this.disconnect();
|
|
202
|
-
this.removeAllListeners();
|
|
203
|
-
}
|
|
204
|
-
}
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import EventEmitter from "../EventEmitter.ts";
|
|
2
|
-
import { DataChannelRouter } from "./DataChannelRouter.ts";
|
|
3
|
-
|
|
4
|
-
export class PeerConnection extends EventEmitter {
|
|
5
|
-
public readonly connection: RTCPeerConnection;
|
|
6
|
-
public readonly router: DataChannelRouter;
|
|
7
|
-
public readonly peerId: string;
|
|
8
|
-
private pendingCandidates: RTCIceCandidateInit[] = [];
|
|
9
|
-
private hasRemoteDescription = false;
|
|
10
|
-
|
|
11
|
-
constructor(
|
|
12
|
-
peerId: string,
|
|
13
|
-
iceServers: RTCIceServer[],
|
|
14
|
-
) {
|
|
15
|
-
super();
|
|
16
|
-
|
|
17
|
-
this.peerId = peerId;
|
|
18
|
-
|
|
19
|
-
this.connection = new RTCPeerConnection({ iceServers });
|
|
20
|
-
this.router = new DataChannelRouter(this.connection);
|
|
21
|
-
|
|
22
|
-
this.connection.onicecandidate = (event) => {
|
|
23
|
-
if (event.candidate) {
|
|
24
|
-
this.emit("iceCandidate", {
|
|
25
|
-
peerId: this.peerId,
|
|
26
|
-
candidate: JSON.stringify(event.candidate.toJSON()),
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
this.connection.oniceconnectionstatechange = () => {
|
|
32
|
-
const state = this.connection.iceConnectionState;
|
|
33
|
-
this.emit("iceStateChange", { peerId: this.peerId, state });
|
|
34
|
-
|
|
35
|
-
if (state === "failed") {
|
|
36
|
-
this.emit("iceFailed", { peerId: this.peerId });
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
this.connection.onconnectionstatechange = () => {
|
|
41
|
-
this.emit("connectionStateChange", {
|
|
42
|
-
peerId: this.peerId,
|
|
43
|
-
state: this.connection.connectionState,
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
get connectionState(): RTCPeerConnectionState {
|
|
49
|
-
return this.connection.connectionState;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get iceConnectionState(): RTCIceConnectionState {
|
|
53
|
-
return this.connection.iceConnectionState;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** Create an SDP offer (initiator side). */
|
|
57
|
-
async createOffer(iceRestart = false): Promise<string> {
|
|
58
|
-
const offer = await this.connection.createOffer(
|
|
59
|
-
iceRestart ? { iceRestart: true } : undefined,
|
|
60
|
-
);
|
|
61
|
-
await this.connection.setLocalDescription(offer);
|
|
62
|
-
return JSON.stringify(this.connection.localDescription?.toJSON());
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Set a remote offer and create an answer (receiver side). Returns the SDP answer. */
|
|
66
|
-
async setRemoteOffer(sdp: string): Promise<string> {
|
|
67
|
-
const offer = JSON.parse(sdp) as RTCSessionDescriptionInit;
|
|
68
|
-
await this.connection.setRemoteDescription(
|
|
69
|
-
new RTCSessionDescription(offer),
|
|
70
|
-
);
|
|
71
|
-
this.hasRemoteDescription = true;
|
|
72
|
-
await this.flushPendingCandidates();
|
|
73
|
-
|
|
74
|
-
const answer = await this.connection.createAnswer();
|
|
75
|
-
await this.connection.setLocalDescription(answer);
|
|
76
|
-
return JSON.stringify(this.connection.localDescription?.toJSON());
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Set the remote answer (initiator side). */
|
|
80
|
-
async setRemoteAnswer(sdp: string): Promise<void> {
|
|
81
|
-
const answer = JSON.parse(sdp) as RTCSessionDescriptionInit;
|
|
82
|
-
await this.connection.setRemoteDescription(
|
|
83
|
-
new RTCSessionDescription(answer),
|
|
84
|
-
);
|
|
85
|
-
this.hasRemoteDescription = true;
|
|
86
|
-
await this.flushPendingCandidates();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** Add a remote ICE candidate. Queues if remote description not yet set. */
|
|
90
|
-
async addIceCandidate(candidateJson: string): Promise<void> {
|
|
91
|
-
const candidate = JSON.parse(candidateJson) as RTCIceCandidateInit;
|
|
92
|
-
|
|
93
|
-
if (!this.hasRemoteDescription) {
|
|
94
|
-
this.pendingCandidates.push(candidate);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
await this.connection.addIceCandidate(new RTCIceCandidate(candidate));
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
private async flushPendingCandidates(): Promise<void> {
|
|
102
|
-
for (const candidate of this.pendingCandidates) {
|
|
103
|
-
await this.connection.addIceCandidate(
|
|
104
|
-
new RTCIceCandidate(candidate),
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
this.pendingCandidates = [];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
close(): void {
|
|
111
|
-
this.router.close();
|
|
112
|
-
try {
|
|
113
|
-
this.connection.close();
|
|
114
|
-
} catch {
|
|
115
|
-
// Ignore
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
destroy(): void {
|
|
120
|
-
this.router.destroy();
|
|
121
|
-
this.connection.onicecandidate = null;
|
|
122
|
-
this.connection.oniceconnectionstatechange = null;
|
|
123
|
-
this.connection.onconnectionstatechange = null;
|
|
124
|
-
|
|
125
|
-
try {
|
|
126
|
-
this.connection.close();
|
|
127
|
-
} catch {
|
|
128
|
-
// Ignore
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
this.removeAllListeners();
|
|
132
|
-
}
|
|
133
|
-
}
|
|
@@ -1,378 +0,0 @@
|
|
|
1
|
-
import { retry } from "@lifeomic/attempt";
|
|
2
|
-
import EventEmitter from "../EventEmitter.ts";
|
|
3
|
-
import type {
|
|
4
|
-
PeerInfo,
|
|
5
|
-
SignalingIncoming,
|
|
6
|
-
SignalingOutgoing,
|
|
7
|
-
} from "./types.ts";
|
|
8
|
-
|
|
9
|
-
export interface SignalingSocketConfiguration {
|
|
10
|
-
/** WebSocket URL for the signaling endpoint. */
|
|
11
|
-
url: string;
|
|
12
|
-
|
|
13
|
-
/** JWT token or async token factory for auth. */
|
|
14
|
-
token: string | (() => string) | (() => Promise<string>);
|
|
15
|
-
|
|
16
|
-
/** Auto-connect on construction. Default: true. */
|
|
17
|
-
autoConnect?: boolean;
|
|
18
|
-
|
|
19
|
-
/** WebSocket polyfill (e.g. for Node.js). */
|
|
20
|
-
WebSocketPolyfill?: any;
|
|
21
|
-
|
|
22
|
-
/** Retry delay in ms. Default: 1000. */
|
|
23
|
-
delay?: number;
|
|
24
|
-
/** Retry factor. Default: 2. */
|
|
25
|
-
factor?: number;
|
|
26
|
-
/** Min retry delay. Default: 1000. */
|
|
27
|
-
minDelay?: number;
|
|
28
|
-
/** Max retry delay. Default: 30000. */
|
|
29
|
-
maxDelay?: number;
|
|
30
|
-
/** Randomize delay. Default: true. */
|
|
31
|
-
jitter?: boolean;
|
|
32
|
-
/** Max retry attempts (0 = unlimited). Default: 0. */
|
|
33
|
-
maxAttempts?: number;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class SignalingSocket extends EventEmitter {
|
|
37
|
-
private ws: WebSocket | null = null;
|
|
38
|
-
private wsHandlers: Record<string, (e: any) => void> = {};
|
|
39
|
-
private shouldConnect = true;
|
|
40
|
-
private cancelRetry?: () => void;
|
|
41
|
-
private connectionAttempt: {
|
|
42
|
-
resolve: (v?: any) => void;
|
|
43
|
-
reject: (r?: any) => void;
|
|
44
|
-
} | null = null;
|
|
45
|
-
|
|
46
|
-
private readonly config: Required<
|
|
47
|
-
Pick<
|
|
48
|
-
SignalingSocketConfiguration,
|
|
49
|
-
| "url"
|
|
50
|
-
| "token"
|
|
51
|
-
| "delay"
|
|
52
|
-
| "factor"
|
|
53
|
-
| "minDelay"
|
|
54
|
-
| "maxDelay"
|
|
55
|
-
| "jitter"
|
|
56
|
-
| "maxAttempts"
|
|
57
|
-
>
|
|
58
|
-
> & { WebSocketPolyfill: any };
|
|
59
|
-
|
|
60
|
-
public localPeerId: string | null = null;
|
|
61
|
-
public isConnected = false;
|
|
62
|
-
|
|
63
|
-
constructor(configuration: SignalingSocketConfiguration) {
|
|
64
|
-
super();
|
|
65
|
-
|
|
66
|
-
this.config = {
|
|
67
|
-
url: configuration.url,
|
|
68
|
-
token: configuration.token,
|
|
69
|
-
delay: configuration.delay ?? 1000,
|
|
70
|
-
factor: configuration.factor ?? 2,
|
|
71
|
-
minDelay: configuration.minDelay ?? 1000,
|
|
72
|
-
maxDelay: configuration.maxDelay ?? 30000,
|
|
73
|
-
jitter: configuration.jitter ?? true,
|
|
74
|
-
maxAttempts: configuration.maxAttempts ?? 0,
|
|
75
|
-
WebSocketPolyfill: configuration.WebSocketPolyfill ?? WebSocket,
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
if (configuration.autoConnect !== false) {
|
|
79
|
-
this.connect();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
private async getToken(): Promise<string> {
|
|
84
|
-
if (typeof this.config.token === "function") {
|
|
85
|
-
return await this.config.token();
|
|
86
|
-
}
|
|
87
|
-
return this.config.token;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
private _connectPromise: Promise<void> | null = null;
|
|
91
|
-
|
|
92
|
-
async connect(): Promise<void> {
|
|
93
|
-
if (this.isConnected) return;
|
|
94
|
-
if (this._connectPromise) return this._connectPromise;
|
|
95
|
-
|
|
96
|
-
this._connectPromise = this._doConnect();
|
|
97
|
-
try {
|
|
98
|
-
await this._connectPromise;
|
|
99
|
-
} finally {
|
|
100
|
-
this._connectPromise = null;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
private async _doConnect(): Promise<void> {
|
|
105
|
-
if (this.cancelRetry) {
|
|
106
|
-
this.cancelRetry();
|
|
107
|
-
this.cancelRetry = undefined;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
this.shouldConnect = true;
|
|
111
|
-
|
|
112
|
-
let cancelAttempt = false;
|
|
113
|
-
|
|
114
|
-
const retryPromise = retry(
|
|
115
|
-
() => this.createConnection(),
|
|
116
|
-
{
|
|
117
|
-
delay: this.config.delay,
|
|
118
|
-
initialDelay: 0,
|
|
119
|
-
factor: this.config.factor,
|
|
120
|
-
maxAttempts: this.config.maxAttempts,
|
|
121
|
-
minDelay: this.config.minDelay,
|
|
122
|
-
maxDelay: this.config.maxDelay,
|
|
123
|
-
jitter: this.config.jitter,
|
|
124
|
-
timeout: 0,
|
|
125
|
-
beforeAttempt: (context) => {
|
|
126
|
-
if (!this.shouldConnect || cancelAttempt) {
|
|
127
|
-
context.abort();
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
).catch((error: any) => {
|
|
132
|
-
if (error && error.code !== "ATTEMPT_ABORTED") {
|
|
133
|
-
throw error;
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
this.cancelRetry = () => {
|
|
138
|
-
cancelAttempt = true;
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
return retryPromise;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
private async createConnection(): Promise<void> {
|
|
145
|
-
this.cleanup();
|
|
146
|
-
|
|
147
|
-
const token = await this.getToken();
|
|
148
|
-
const separator = this.config.url.includes("?") ? "&" : "?";
|
|
149
|
-
const url = `${this.config.url}${separator}token=${encodeURIComponent(token)}`;
|
|
150
|
-
|
|
151
|
-
const ws = new this.config.WebSocketPolyfill(url);
|
|
152
|
-
|
|
153
|
-
return new Promise((resolve, reject) => {
|
|
154
|
-
const onOpen = () => {
|
|
155
|
-
this.isConnected = true;
|
|
156
|
-
// Server auto-sends Welcome on join; we send Join to register
|
|
157
|
-
this.sendRaw({ type: "join" });
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
161
|
-
const onMessage = (event: MessageEvent | { data: string }) => {
|
|
162
|
-
const data =
|
|
163
|
-
typeof event === "string"
|
|
164
|
-
? event
|
|
165
|
-
: typeof (event as MessageEvent).data === "string"
|
|
166
|
-
? (event as MessageEvent).data
|
|
167
|
-
: null;
|
|
168
|
-
if (!data) return;
|
|
169
|
-
|
|
170
|
-
let msg: SignalingOutgoing;
|
|
171
|
-
try {
|
|
172
|
-
msg = JSON.parse(data);
|
|
173
|
-
} catch {
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
this.handleMessage(msg, resolve);
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
const onClose = (event: CloseEvent | { code?: number }) => {
|
|
181
|
-
const wasConnected = this.isConnected;
|
|
182
|
-
this.isConnected = false;
|
|
183
|
-
this.localPeerId = null;
|
|
184
|
-
|
|
185
|
-
if (this.connectionAttempt) {
|
|
186
|
-
this.connectionAttempt = null;
|
|
187
|
-
reject(
|
|
188
|
-
new Error(`Signaling WebSocket closed: ${(event as CloseEvent)?.code}`),
|
|
189
|
-
);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
this.emit("disconnected");
|
|
193
|
-
|
|
194
|
-
if (!wasConnected) return;
|
|
195
|
-
|
|
196
|
-
// Auto-reconnect if desired
|
|
197
|
-
if (this.shouldConnect && !this.cancelRetry) {
|
|
198
|
-
setTimeout(() => this.connect(), this.config.delay);
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
const onError = (err: Event) => {
|
|
203
|
-
if (this.connectionAttempt) {
|
|
204
|
-
this.connectionAttempt = null;
|
|
205
|
-
reject(err);
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
this.wsHandlers = {
|
|
210
|
-
open: onOpen,
|
|
211
|
-
message: onMessage,
|
|
212
|
-
close: onClose,
|
|
213
|
-
error: onError,
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
for (const [name, handler] of Object.entries(this.wsHandlers)) {
|
|
217
|
-
ws.addEventListener(name, handler);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
this.ws = ws;
|
|
221
|
-
this.connectionAttempt = { resolve, reject };
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
private handleMessage(msg: SignalingOutgoing, resolveConnection?: (v?: any) => void): void {
|
|
226
|
-
switch (msg.type) {
|
|
227
|
-
case "welcome":
|
|
228
|
-
this.localPeerId = msg.peer_id;
|
|
229
|
-
if (this.connectionAttempt) {
|
|
230
|
-
this.connectionAttempt = null;
|
|
231
|
-
resolveConnection?.();
|
|
232
|
-
}
|
|
233
|
-
this.emit("welcome", {
|
|
234
|
-
peerId: msg.peer_id,
|
|
235
|
-
peers: msg.peers,
|
|
236
|
-
});
|
|
237
|
-
break;
|
|
238
|
-
|
|
239
|
-
case "joined":
|
|
240
|
-
this.emit("joined", {
|
|
241
|
-
peer_id: msg.peer_id,
|
|
242
|
-
user_id: msg.user_id,
|
|
243
|
-
muted: msg.muted,
|
|
244
|
-
video: msg.video,
|
|
245
|
-
screen: msg.screen,
|
|
246
|
-
name: msg.name,
|
|
247
|
-
color: msg.color,
|
|
248
|
-
} satisfies PeerInfo);
|
|
249
|
-
break;
|
|
250
|
-
|
|
251
|
-
case "left":
|
|
252
|
-
this.emit("left", { peerId: msg.peer_id });
|
|
253
|
-
break;
|
|
254
|
-
|
|
255
|
-
case "offer":
|
|
256
|
-
this.emit("offer", { from: msg.from, sdp: msg.sdp });
|
|
257
|
-
break;
|
|
258
|
-
|
|
259
|
-
case "answer":
|
|
260
|
-
this.emit("answer", { from: msg.from, sdp: msg.sdp });
|
|
261
|
-
break;
|
|
262
|
-
|
|
263
|
-
case "ice":
|
|
264
|
-
this.emit("ice", { from: msg.from, candidate: msg.candidate });
|
|
265
|
-
break;
|
|
266
|
-
|
|
267
|
-
case "mute":
|
|
268
|
-
this.emit("mute", {
|
|
269
|
-
peerId: msg.peer_id,
|
|
270
|
-
muted: msg.muted,
|
|
271
|
-
});
|
|
272
|
-
break;
|
|
273
|
-
|
|
274
|
-
case "media-state":
|
|
275
|
-
this.emit("media-state", {
|
|
276
|
-
peerId: msg.peer_id,
|
|
277
|
-
video: msg.video,
|
|
278
|
-
screen: msg.screen,
|
|
279
|
-
});
|
|
280
|
-
break;
|
|
281
|
-
|
|
282
|
-
case "profile":
|
|
283
|
-
this.emit("profile", {
|
|
284
|
-
peerId: msg.peer_id,
|
|
285
|
-
name: msg.name,
|
|
286
|
-
color: msg.color,
|
|
287
|
-
});
|
|
288
|
-
break;
|
|
289
|
-
|
|
290
|
-
case "ping":
|
|
291
|
-
this.sendRaw({ type: "pong" });
|
|
292
|
-
break;
|
|
293
|
-
|
|
294
|
-
case "error":
|
|
295
|
-
this.emit("error", {
|
|
296
|
-
code: msg.code,
|
|
297
|
-
message: msg.message,
|
|
298
|
-
});
|
|
299
|
-
break;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
private sendRaw(msg: SignalingIncoming): void {
|
|
304
|
-
if (this.ws?.readyState === 1 /* OPEN */) {
|
|
305
|
-
this.ws.send(JSON.stringify(msg));
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// ── Public send methods ─────────────────────────────────────────────────
|
|
310
|
-
|
|
311
|
-
sendOffer(to: string, sdp: string): void {
|
|
312
|
-
this.sendRaw({ type: "offer", to, sdp });
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
sendAnswer(to: string, sdp: string): void {
|
|
316
|
-
this.sendRaw({ type: "answer", to, sdp });
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
sendIce(to: string, candidate: string): void {
|
|
320
|
-
this.sendRaw({ type: "ice", to, candidate });
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
sendMute(muted: boolean): void {
|
|
324
|
-
this.sendRaw({ type: "mute", muted });
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
sendMediaState(video: boolean, screen: boolean): void {
|
|
328
|
-
this.sendRaw({ type: "media-state", video, screen });
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
sendProfile(name: string, color: string): void {
|
|
332
|
-
this.sendRaw({ type: "profile", name, color });
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
sendLeave(): void {
|
|
336
|
-
this.sendRaw({ type: "leave" });
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
// ── Lifecycle ───────────────────────────────────────────────────────────
|
|
340
|
-
|
|
341
|
-
disconnect(): void {
|
|
342
|
-
this.shouldConnect = false;
|
|
343
|
-
this.sendLeave();
|
|
344
|
-
|
|
345
|
-
if (this.cancelRetry) {
|
|
346
|
-
this.cancelRetry();
|
|
347
|
-
this.cancelRetry = undefined;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
this.cleanup();
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
destroy(): void {
|
|
354
|
-
this.disconnect();
|
|
355
|
-
this.removeAllListeners();
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
private cleanup(): void {
|
|
359
|
-
if (!this.ws) return;
|
|
360
|
-
|
|
361
|
-
for (const [name, handler] of Object.entries(this.wsHandlers)) {
|
|
362
|
-
this.ws.removeEventListener(name, handler);
|
|
363
|
-
}
|
|
364
|
-
this.wsHandlers = {};
|
|
365
|
-
|
|
366
|
-
try {
|
|
367
|
-
if (this.ws.readyState !== 3 /* CLOSED */) {
|
|
368
|
-
this.ws.close();
|
|
369
|
-
}
|
|
370
|
-
} catch {
|
|
371
|
-
// Ignore close errors
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
this.ws = null;
|
|
375
|
-
this.isConnected = false;
|
|
376
|
-
this.localPeerId = null;
|
|
377
|
-
}
|
|
378
|
-
}
|