@dxos/network-manager 0.3.9-main.3fcc0fa → 0.3.9-main.4e39f3a

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 (54) hide show
  1. package/dist/lib/browser/{chunk-KGZ5C6JO.mjs → chunk-6B4JGIV2.mjs} +91 -52
  2. package/dist/lib/browser/chunk-6B4JGIV2.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -11
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +3 -3
  6. package/dist/lib/node/{chunk-WUBZ4UOJ.cjs → chunk-Y2B5BDRH.cjs} +87 -54
  7. package/dist/lib/node/{chunk-WUBZ4UOJ.cjs.map → chunk-Y2B5BDRH.cjs.map} +3 -3
  8. package/dist/lib/node/index.cjs +27 -36
  9. package/dist/lib/node/index.cjs.map +2 -2
  10. package/dist/lib/node/meta.json +1 -1
  11. package/dist/lib/node/testing/index.cjs +18 -19
  12. package/dist/lib/node/testing/index.cjs.map +1 -1
  13. package/dist/types/src/transport/index.d.ts +0 -1
  14. package/dist/types/src/transport/index.d.ts.map +1 -1
  15. package/dist/types/src/transport/libdatachannel-transport.d.ts +1 -0
  16. package/dist/types/src/transport/libdatachannel-transport.d.ts.map +1 -1
  17. package/package.json +18 -18
  18. package/src/transport/index.ts +0 -1
  19. package/src/transport/libdatachannel-transport.ts +76 -34
  20. package/src/transport/simplepeer-transport.ts +2 -2
  21. package/dist/lib/browser/chunk-KGZ5C6JO.mjs.map +0 -7
  22. package/dist/lib/browser/chunk-YERMWDRB.mjs +0 -24
  23. package/dist/lib/browser/chunk-YERMWDRB.mjs.map +0 -7
  24. package/dist/lib/browser/index.browser-O53CPVK3.mjs +0 -14
  25. package/dist/lib/browser/index.browser-O53CPVK3.mjs.map +0 -7
  26. package/dist/lib/node/chunk-JDKM4UPJ.cjs +0 -470
  27. package/dist/lib/node/chunk-JDKM4UPJ.cjs.map +0 -7
  28. package/dist/lib/node/datachannel-3VTTIMR5.cjs +0 -35
  29. package/dist/lib/node/datachannel-3VTTIMR5.cjs.map +0 -7
  30. package/dist/types/src/transport/datachannel/index.browser.d.ts +0 -15
  31. package/dist/types/src/transport/datachannel/index.browser.d.ts.map +0 -1
  32. package/dist/types/src/transport/datachannel/index.d.ts +0 -8
  33. package/dist/types/src/transport/datachannel/index.d.ts.map +0 -1
  34. package/dist/types/src/transport/datachannel/rtc-data-channel.d.ts +0 -29
  35. package/dist/types/src/transport/datachannel/rtc-data-channel.d.ts.map +0 -1
  36. package/dist/types/src/transport/datachannel/rtc-events.d.ts +0 -9
  37. package/dist/types/src/transport/datachannel/rtc-events.d.ts.map +0 -1
  38. package/dist/types/src/transport/datachannel/rtc-ice-candidate.d.ts +0 -22
  39. package/dist/types/src/transport/datachannel/rtc-ice-candidate.d.ts.map +0 -1
  40. package/dist/types/src/transport/datachannel/rtc-peer-connection.d.ts +0 -54
  41. package/dist/types/src/transport/datachannel/rtc-peer-connection.d.ts.map +0 -1
  42. package/dist/types/src/transport/datachannel/rtc-peer-connection.test.d.ts +0 -2
  43. package/dist/types/src/transport/datachannel/rtc-peer-connection.test.d.ts.map +0 -1
  44. package/dist/types/src/transport/datachannel/rtc-session-description.d.ts +0 -10
  45. package/dist/types/src/transport/datachannel/rtc-session-description.d.ts.map +0 -1
  46. package/src/transport/datachannel/README.md +0 -2
  47. package/src/transport/datachannel/index.browser.ts +0 -8
  48. package/src/transport/datachannel/index.ts +0 -17
  49. package/src/transport/datachannel/rtc-data-channel.ts +0 -148
  50. package/src/transport/datachannel/rtc-events.ts +0 -23
  51. package/src/transport/datachannel/rtc-ice-candidate.ts +0 -54
  52. package/src/transport/datachannel/rtc-peer-connection.test.ts +0 -112
  53. package/src/transport/datachannel/rtc-peer-connection.ts +0 -355
  54. package/src/transport/datachannel/rtc-session-description.ts +0 -23
@@ -1,355 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import node, { type SelectedCandidateInfo } from 'node-datachannel';
6
- import defer, { type DeferredPromise } from 'p-defer';
7
-
8
- import { DataChannel } from './rtc-data-channel';
9
- import { DataChannelEvent, PeerConnectionIceEvent } from './rtc-events';
10
- import { IceCandidate } from './rtc-ice-candidate';
11
- import { SessionDescription } from './rtc-session-description';
12
-
13
- export class PeerConnection extends EventTarget implements RTCPeerConnection {
14
- static async generateCertificate(keygenAlgorithm: AlgorithmIdentifier): Promise<RTCCertificate> {
15
- throw new Error('Not implemented');
16
- }
17
-
18
- canTrickleIceCandidates: boolean | null;
19
- sctp: RTCSctpTransport | null;
20
-
21
- onconnectionstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null;
22
- ondatachannel: ((this: RTCPeerConnection, ev: RTCDataChannelEvent) => any) | null;
23
- onicecandidate: ((this: RTCPeerConnection, ev: RTCPeerConnectionIceEvent) => any) | null;
24
- onicecandidateerror: ((this: RTCPeerConnection, ev: Event) => any) | null;
25
- oniceconnectionstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null;
26
- onicegatheringstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null;
27
- onnegotiationneeded: ((this: RTCPeerConnection, ev: Event) => any) | null;
28
- onsignalingstatechange: ((this: RTCPeerConnection, ev: Event) => any) | null;
29
- ontrack: ((this: RTCPeerConnection, ev: RTCTrackEvent) => any) | null;
30
-
31
- #peerConnection: node.PeerConnection;
32
- #config: RTCConfiguration;
33
- #localOffer: DeferredPromise<RTCSessionDescriptionInit>;
34
- #localAnswer: DeferredPromise<RTCSessionDescriptionInit>;
35
- #dataChannels: Set<DataChannel>;
36
-
37
- constructor(init: RTCConfiguration = {}) {
38
- super();
39
-
40
- this.#config = init;
41
- this.#localOffer = defer();
42
- this.#localAnswer = defer();
43
- this.#dataChannels = new Set();
44
-
45
- const iceServers = init.iceServers ?? [];
46
-
47
- this.#peerConnection = new node.PeerConnection(`peer-${Math.random()}`, {
48
- // convert https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection#iceservers to the format expected by https://github.com/murat-dogan/node-datachannel/blob/master/src/peer-connection-wrapper.cpp#L101
49
- iceServers: iceServers
50
- .map((server) => {
51
- const urls = Array.isArray(server.urls) ? server.urls : [server.urls];
52
-
53
- return urls.map((url) => {
54
- if (server.username && server.credential) {
55
- const [protocol, rest] = url.split(/:(.*)/);
56
- return `${protocol}:${server.username}:${server.credential}@${rest}`;
57
- }
58
- return url;
59
- });
60
- })
61
- .flat(),
62
- iceTransportPolicy: init?.iceTransportPolicy,
63
- });
64
-
65
- this.#peerConnection.onStateChange(() => {
66
- this.dispatchEvent(new Event('connectionstatechange'));
67
- });
68
- // https://github.com/murat-dogan/node-datachannel/pull/171
69
- // this.#peerConnection.onSignalingStateChange(() => {
70
- // this.dispatchEvent(new Event('signalingstatechange'))
71
- // })
72
- this.#peerConnection.onGatheringStateChange(() => {
73
- this.dispatchEvent(new Event('icegatheringstatechange'));
74
- });
75
- this.#peerConnection.onDataChannel((channel) => {
76
- this.dispatchEvent(new DataChannelEvent(new DataChannel(channel)));
77
- });
78
-
79
- // forward events to properties
80
- this.addEventListener('connectionstatechange', (event) => {
81
- this.onconnectionstatechange?.(event);
82
- });
83
- this.addEventListener('signalingstatechange', (event) => {
84
- this.onsignalingstatechange?.(event);
85
- });
86
- this.addEventListener('icegatheringstatechange', (event) => {
87
- this.onicegatheringstatechange?.(event);
88
- });
89
- this.addEventListener('datachannel', (event) => {
90
- this.ondatachannel?.(event as RTCDataChannelEvent);
91
- });
92
-
93
- this.#peerConnection.onLocalDescription((sdp, type) => {
94
- if (type === 'offer') {
95
- this.#localOffer.resolve({
96
- sdp,
97
- type,
98
- });
99
- }
100
-
101
- if (type === 'answer') {
102
- this.#localAnswer.resolve({
103
- sdp,
104
- type,
105
- });
106
- }
107
- });
108
-
109
- this.#peerConnection.onLocalCandidate((candidate, mid) => {
110
- if (mid === 'unspec') {
111
- this.#localAnswer.reject(new Error(`Invalid description type ${mid}`));
112
- return;
113
- }
114
-
115
- const event = new PeerConnectionIceEvent(new IceCandidate({ candidate }));
116
-
117
- this.onicecandidate?.(event);
118
- });
119
-
120
- this.canTrickleIceCandidates = null;
121
- this.sctp = null;
122
- this.onconnectionstatechange = null;
123
- this.ondatachannel = null;
124
- this.onicecandidate = null;
125
- this.onicecandidateerror = null;
126
- this.oniceconnectionstatechange = null;
127
- this.onicegatheringstatechange = null;
128
- this.onnegotiationneeded = null;
129
- this.onsignalingstatechange = null;
130
- this.ontrack = null;
131
- }
132
-
133
- get connectionState(): RTCPeerConnectionState {
134
- return assertState(this.#peerConnection.state(), RTCPeerConnectionStates);
135
- }
136
-
137
- get iceConnectionState(): RTCIceConnectionState {
138
- return assertState(this.#peerConnection.state(), RTCIceConnectionStates);
139
- }
140
-
141
- get iceGatheringState(): RTCIceGatheringState {
142
- return assertState(this.#peerConnection.gatheringState(), RTCIceGatheringStates);
143
- }
144
-
145
- get signalingState(): RTCSignalingState {
146
- return assertState(this.#peerConnection.signalingState(), RTCSignalingStates);
147
- }
148
-
149
- get currentLocalDescription(): RTCSessionDescription | null {
150
- return toSessionDescription(this.#peerConnection.localDescription());
151
- }
152
-
153
- get localDescription(): RTCSessionDescription | null {
154
- return toSessionDescription(this.#peerConnection.localDescription());
155
- }
156
-
157
- get pendingLocalDescription(): RTCSessionDescription | null {
158
- return toSessionDescription(this.#peerConnection.localDescription());
159
- }
160
-
161
- get currentRemoteDescription(): RTCSessionDescription | null {
162
- // not exposed by node-datachannel
163
- console.log("node-datachannel doesn't expose currentRemoteDescription");
164
- return toSessionDescription(null);
165
- }
166
-
167
- get pendingRemoteDescription(): RTCSessionDescription | null {
168
- // not exposed by node-datachannel
169
- console.log("node-datachannel doesn't expose pendingRemoteDescription");
170
- return toSessionDescription(null);
171
- }
172
-
173
- get remoteDescription(): RTCSessionDescription | null {
174
- // not exposed by node-datachannel
175
- console.log("node-datachannel doesn't expose remoteDescription");
176
- return toSessionDescription(null);
177
- }
178
-
179
- async addIceCandidate(candidate?: RTCIceCandidateInit): Promise<void> {
180
- if (candidate == null || candidate.candidate == null) {
181
- throw new Error('Candidate invalid');
182
- }
183
-
184
- this.#peerConnection.addRemoteCandidate(candidate.candidate, candidate.sdpMid ?? '0');
185
- }
186
-
187
- getSelectedCandidatePair(): { local: SelectedCandidateInfo; remote: SelectedCandidateInfo } | null {
188
- return this.#peerConnection.getSelectedCandidatePair();
189
- }
190
-
191
- addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender {
192
- throw new Error('addTrack Not implemented');
193
- }
194
-
195
- addTransceiver(trackOrKind: MediaStreamTrack | string, init?: RTCRtpTransceiverInit): RTCRtpTransceiver {
196
- throw new Error('addTransciever Not implemented');
197
- }
198
-
199
- close(): void {
200
- // close all channels before shutting down
201
- this.#dataChannels.forEach((channel) => {
202
- channel.close();
203
- });
204
-
205
- this.#peerConnection.close();
206
- this.#peerConnection.destroy();
207
- }
208
-
209
- bytesSent(): number {
210
- return this.#peerConnection.bytesSent();
211
- }
212
-
213
- bytesReceived(): number {
214
- return this.#peerConnection.bytesReceived();
215
- }
216
-
217
- createDataChannel(label: string, dataChannelDict: RTCDataChannelInit = {}): RTCDataChannel {
218
- const channel = this.#peerConnection.createDataChannel(label, dataChannelDict);
219
- const dataChannel = new DataChannel(channel, dataChannelDict);
220
-
221
- // ensure we can close all channels when shutting down
222
- this.#dataChannels.add(dataChannel);
223
- dataChannel.addEventListener('close', () => {
224
- this.#dataChannels.delete(dataChannel);
225
- });
226
-
227
- return dataChannel;
228
- }
229
-
230
- async createOffer(options?: RTCOfferOptions): Promise<RTCSessionDescriptionInit>;
231
- async createOffer(
232
- successCallback: RTCSessionDescriptionCallback,
233
- failureCallback: RTCPeerConnectionErrorCallback,
234
- options?: RTCOfferOptions,
235
- ): Promise<void>;
236
-
237
- async createOffer(...args: any[]): Promise<any> {
238
- return this.#localOffer.promise;
239
- }
240
-
241
- async createAnswer(options?: RTCAnswerOptions): Promise<RTCSessionDescriptionInit>;
242
- async createAnswer(
243
- successCallback: RTCSessionDescriptionCallback,
244
- failureCallback: RTCPeerConnectionErrorCallback,
245
- ): Promise<void>;
246
-
247
- async createAnswer(...args: any[]): Promise<any> {
248
- return this.#localAnswer.promise;
249
- }
250
-
251
- getConfiguration(): RTCConfiguration {
252
- return this.#config;
253
- }
254
-
255
- getReceivers(): RTCRtpReceiver[] {
256
- throw new Error('getReceivers Not implemented');
257
- }
258
-
259
- getSenders(): RTCRtpSender[] {
260
- throw new Error('getSenders Not implemented');
261
- }
262
-
263
- async getStats(selector?: MediaStreamTrack | null): Promise<RTCStatsReport> {
264
- throw new Error('getStats Not implemented');
265
- }
266
-
267
- getTransceivers(): RTCRtpTransceiver[] {
268
- throw new Error('getTranscievers Not implemented');
269
- }
270
-
271
- removeTrack(sender: RTCRtpSender): void {
272
- throw new Error('removeTrack Not implemented');
273
- }
274
-
275
- restartIce(): void {
276
- throw new Error('restartIce Not implemented');
277
- }
278
-
279
- setConfiguration(configuration: RTCConfiguration = {}): void {
280
- this.#config = configuration;
281
- }
282
-
283
- async setLocalDescription(description?: RTCLocalSessionDescriptionInit): Promise<void> {
284
- if (description == null || description.type == null) {
285
- throw new Error('Local description type must be set');
286
- }
287
-
288
- if (description.type !== 'offer') {
289
- console.log(`node-datachannel: setLocalDescription: only offer is supported, not ${description.type}`);
290
- // any other type causes libdatachannel to throw
291
- return;
292
- }
293
-
294
- // @ts-expect-error types are wrong
295
- this.#peerConnection.setLocalDescription(description.type);
296
- }
297
-
298
- async setRemoteDescription(description: RTCSessionDescriptionInit): Promise<void> {
299
- if (description.sdp == null) {
300
- throw new Error('Remote SDP must be set');
301
- }
302
-
303
- // @ts-expect-error types are wrong
304
- this.#peerConnection.setRemoteDescription(description.sdp, description.type);
305
- }
306
- }
307
-
308
- export { PeerConnection as RTCPeerConnection };
309
-
310
- const assertState = <T>(state: any, states: T[]): T => {
311
- if (state != null && !states.includes(state)) {
312
- throw new Error(`Invalid value encountered - "${state}" must be one of ${states}`);
313
- }
314
-
315
- return state as T;
316
- };
317
-
318
- const toSessionDescription = (description: { sdp?: string; type: string } | null): RTCSessionDescription | null => {
319
- if (description == null) {
320
- return null;
321
- }
322
-
323
- return new SessionDescription({
324
- sdp: description.sdp,
325
- type: assertState(description.type, RTCSdpTypes),
326
- });
327
- };
328
-
329
- const RTCPeerConnectionStates: RTCPeerConnectionState[] = [
330
- 'closed',
331
- 'connected',
332
- 'connecting',
333
- 'disconnected',
334
- 'failed',
335
- 'new',
336
- ];
337
- const RTCSdpTypes: RTCSdpType[] = ['answer', 'offer', 'pranswer', 'rollback'];
338
- const RTCIceConnectionStates: RTCIceConnectionState[] = [
339
- 'checking',
340
- 'closed',
341
- 'completed',
342
- 'connected',
343
- 'disconnected',
344
- 'failed',
345
- 'new',
346
- ];
347
- const RTCIceGatheringStates: RTCIceGatheringState[] = ['complete', 'gathering', 'new'];
348
- const RTCSignalingStates: RTCSignalingState[] = [
349
- 'closed',
350
- 'have-local-offer',
351
- 'have-local-pranswer',
352
- 'have-remote-offer',
353
- 'have-remote-pranswer',
354
- 'stable',
355
- ];
@@ -1,23 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- /**
6
- * @see https://developer.mozilla.org/docs/Web/API/RTCSessionDescription
7
- */
8
- export class SessionDescription implements RTCSessionDescription {
9
- readonly sdp: string;
10
- readonly type: RTCSdpType;
11
-
12
- constructor(init: RTCSessionDescriptionInit) {
13
- this.sdp = init.sdp ?? '';
14
- this.type = init.type;
15
- }
16
-
17
- toJSON(): RTCSessionDescriptionInit {
18
- return {
19
- sdp: this.sdp,
20
- type: this.type,
21
- };
22
- }
23
- }