@dxos/network-manager 0.1.57-main.e87098f → 0.1.57

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 (95) hide show
  1. package/dist/lib/browser/{chunk-UAB4ONRL.mjs → chunk-UKMTAO4L.mjs} +553 -121
  2. package/dist/lib/browser/chunk-UKMTAO4L.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-YERMWDRB.mjs +24 -0
  4. package/dist/lib/browser/chunk-YERMWDRB.mjs.map +7 -0
  5. package/dist/lib/browser/index.browser-O53CPVK3.mjs +14 -0
  6. package/dist/lib/browser/index.browser-O53CPVK3.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +29 -13
  8. package/dist/lib/browser/meta.json +1 -1
  9. package/dist/lib/browser/testing/index.mjs +83 -38
  10. package/dist/lib/browser/testing/index.mjs.map +3 -3
  11. package/dist/lib/node/index.cjs +1048 -124
  12. package/dist/lib/node/index.cjs.map +4 -4
  13. package/dist/lib/node/meta.json +1 -1
  14. package/dist/lib/node/testing/index.cjs +1109 -151
  15. package/dist/lib/node/testing/index.cjs.map +4 -4
  16. package/dist/types/src/swarm/connection.d.ts +6 -0
  17. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  18. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  19. package/dist/types/src/testing/test-builder.d.ts +5 -2
  20. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  21. package/dist/types/src/transport/datachannel/index.browser.d.ts +15 -0
  22. package/dist/types/src/transport/datachannel/index.browser.d.ts.map +1 -0
  23. package/dist/types/src/transport/datachannel/index.d.ts +8 -0
  24. package/dist/types/src/transport/datachannel/index.d.ts.map +1 -0
  25. package/dist/types/src/transport/datachannel/rtc-data-channel.d.ts +29 -0
  26. package/dist/types/src/transport/datachannel/rtc-data-channel.d.ts.map +1 -0
  27. package/dist/types/src/transport/datachannel/rtc-events.d.ts +9 -0
  28. package/dist/types/src/transport/datachannel/rtc-events.d.ts.map +1 -0
  29. package/dist/types/src/transport/datachannel/rtc-ice-candidate.d.ts +22 -0
  30. package/dist/types/src/transport/datachannel/rtc-ice-candidate.d.ts.map +1 -0
  31. package/dist/types/src/transport/datachannel/rtc-peer-connection.d.ts +47 -0
  32. package/dist/types/src/transport/datachannel/rtc-peer-connection.d.ts.map +1 -0
  33. package/dist/types/src/transport/datachannel/rtc-peer-connection.test.d.ts +2 -0
  34. package/dist/types/src/transport/datachannel/rtc-peer-connection.test.d.ts.map +1 -0
  35. package/dist/types/src/transport/datachannel/rtc-session-description.d.ts +10 -0
  36. package/dist/types/src/transport/datachannel/rtc-session-description.d.ts.map +1 -0
  37. package/dist/types/src/transport/index.d.ts +5 -3
  38. package/dist/types/src/transport/index.d.ts.map +1 -1
  39. package/dist/types/src/transport/libdatachannel-transport.d.ts +35 -0
  40. package/dist/types/src/transport/libdatachannel-transport.d.ts.map +1 -0
  41. package/dist/types/src/transport/libdatachannel-transport.test.d.ts +2 -0
  42. package/dist/types/src/transport/libdatachannel-transport.test.d.ts.map +1 -0
  43. package/dist/types/src/transport/simplepeer-simple-peer.d.ts +2 -0
  44. package/dist/types/src/transport/simplepeer-simple-peer.d.ts.map +1 -0
  45. package/dist/types/src/transport/simplepeer-transport-proxy-test.d.ts +2 -0
  46. package/dist/types/src/transport/simplepeer-transport-proxy-test.d.ts.map +1 -0
  47. package/dist/types/src/transport/{webrtc-transport-proxy.d.ts → simplepeer-transport-proxy.d.ts} +5 -5
  48. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -0
  49. package/dist/types/src/transport/{webrtc-transport-service.d.ts → simplepeer-transport-service.d.ts} +2 -2
  50. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -0
  51. package/dist/types/src/transport/{webrtc-transport.d.ts → simplepeer-transport.d.ts} +8 -8
  52. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -0
  53. package/dist/types/src/transport/simplepeer-transport.test.d.ts +2 -0
  54. package/dist/types/src/transport/simplepeer-transport.test.d.ts.map +1 -0
  55. package/dist/types/src/transport/transport.d.ts +6 -0
  56. package/dist/types/src/transport/transport.d.ts.map +1 -1
  57. package/dist/types/src/wire-protocol.d.ts +3 -2
  58. package/dist/types/src/wire-protocol.d.ts.map +1 -1
  59. package/package.json +25 -20
  60. package/src/connection-log.ts +1 -1
  61. package/src/swarm/connection.test.ts +8 -3
  62. package/src/swarm/connection.ts +58 -6
  63. package/src/swarm/peer.ts +4 -0
  64. package/src/swarm/swarm.test.ts +3 -2
  65. package/src/testing/test-builder.ts +72 -41
  66. package/src/transport/datachannel/README.md +2 -0
  67. package/src/transport/datachannel/index.browser.ts +8 -0
  68. package/src/transport/datachannel/index.ts +17 -0
  69. package/src/transport/datachannel/rtc-data-channel.ts +148 -0
  70. package/src/transport/datachannel/rtc-events.ts +23 -0
  71. package/src/transport/datachannel/rtc-ice-candidate.ts +54 -0
  72. package/src/transport/datachannel/rtc-peer-connection.test.ts +112 -0
  73. package/src/transport/datachannel/rtc-peer-connection.ts +347 -0
  74. package/src/transport/datachannel/rtc-session-description.ts +23 -0
  75. package/src/transport/index.ts +5 -3
  76. package/src/transport/libdatachannel-transport.test.ts +99 -0
  77. package/src/transport/libdatachannel-transport.ts +252 -0
  78. package/src/transport/{webrtc-transport-proxy.test.ts → simplepeer-transport-proxy-test.ts} +15 -15
  79. package/src/transport/{webrtc-transport-proxy.ts → simplepeer-transport-proxy.ts} +8 -8
  80. package/src/transport/{webrtc-transport-service.ts → simplepeer-transport-service.ts} +4 -4
  81. package/src/transport/{webrtc-transport.test.ts → simplepeer-transport.test.ts} +5 -5
  82. package/src/transport/{webrtc-transport.ts → simplepeer-transport.ts} +13 -27
  83. package/src/transport/transport.ts +7 -0
  84. package/src/wire-protocol.ts +8 -4
  85. package/dist/lib/browser/chunk-UAB4ONRL.mjs.map +0 -7
  86. package/dist/types/src/transport/webrtc-simple-peer.test.d.ts +0 -2
  87. package/dist/types/src/transport/webrtc-simple-peer.test.d.ts.map +0 -1
  88. package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +0 -1
  89. package/dist/types/src/transport/webrtc-transport-proxy.test.d.ts +0 -2
  90. package/dist/types/src/transport/webrtc-transport-proxy.test.d.ts.map +0 -1
  91. package/dist/types/src/transport/webrtc-transport-service.d.ts.map +0 -1
  92. package/dist/types/src/transport/webrtc-transport.d.ts.map +0 -1
  93. package/dist/types/src/transport/webrtc-transport.test.d.ts +0 -2
  94. package/dist/types/src/transport/webrtc-transport.test.d.ts.map +0 -1
  95. /package/src/transport/{webrtc-simple-peer.test.ts → simplepeer-simple-peer.ts} +0 -0
@@ -5,6 +5,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __esm = (fn, res) => function __init() {
9
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
10
+ };
8
11
  var __export = (target, all) => {
9
12
  for (var name in all)
10
13
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -27,6 +30,477 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
30
  ));
28
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
32
 
33
+ // packages/core/mesh/network-manager/src/transport/datachannel/rtc-ice-candidate.ts
34
+ var IceCandidate;
35
+ var init_rtc_ice_candidate = __esm({
36
+ "packages/core/mesh/network-manager/src/transport/datachannel/rtc-ice-candidate.ts"() {
37
+ "use strict";
38
+ IceCandidate = class {
39
+ constructor(init) {
40
+ if (init.candidate == null) {
41
+ throw new DOMException("candidate must be specified");
42
+ }
43
+ this.candidate = init.candidate;
44
+ this.sdpMLineIndex = init.sdpMLineIndex ?? null;
45
+ this.sdpMid = init.sdpMid ?? null;
46
+ this.usernameFragment = init.usernameFragment ?? null;
47
+ this.address = null;
48
+ this.component = null;
49
+ this.foundation = null;
50
+ this.port = null;
51
+ this.priority = null;
52
+ this.protocol = null;
53
+ this.relatedAddress = null;
54
+ this.relatedPort = null;
55
+ this.tcpType = null;
56
+ this.type = null;
57
+ }
58
+ toJSON() {
59
+ return {
60
+ candidate: this.candidate,
61
+ sdpMLineIndex: this.sdpMLineIndex,
62
+ sdpMid: this.sdpMid,
63
+ usernameFragment: this.usernameFragment
64
+ };
65
+ }
66
+ };
67
+ }
68
+ });
69
+
70
+ // packages/core/mesh/network-manager/src/transport/datachannel/rtc-data-channel.ts
71
+ var DataChannel;
72
+ var init_rtc_data_channel = __esm({
73
+ "packages/core/mesh/network-manager/src/transport/datachannel/rtc-data-channel.ts"() {
74
+ "use strict";
75
+ DataChannel = class extends EventTarget {
76
+ constructor(dataChannel, dataChannelDict = {}) {
77
+ super();
78
+ this.#dataChannel = dataChannel;
79
+ this.#readyState = "connecting";
80
+ this.#bufferedAmountLowThreshold = 0;
81
+ this.binaryType = "arraybuffer";
82
+ this.#dataChannel.onOpen(() => {
83
+ this.#readyState = "open";
84
+ this.dispatchEvent(new Event("open"));
85
+ });
86
+ this.#dataChannel.onClosed(() => {
87
+ this.#readyState = "closed";
88
+ this.dispatchEvent(new Event("close"));
89
+ });
90
+ this.#dataChannel.onError((msg) => {
91
+ this.#readyState = "closed";
92
+ this.dispatchEvent(new RTCErrorEvent("error", {
93
+ error: new RTCError({
94
+ errorDetail: "data-channel-failure"
95
+ }, msg)
96
+ }));
97
+ });
98
+ this.#dataChannel.onBufferedAmountLow(() => {
99
+ this.dispatchEvent(new Event("bufferedamountlow"));
100
+ });
101
+ this.#dataChannel.onMessage((data) => {
102
+ if (typeof data === "string") {
103
+ data = Buffer.from(data);
104
+ }
105
+ this.dispatchEvent(new MessageEvent("message", {
106
+ data
107
+ }));
108
+ });
109
+ this.addEventListener("message", (event) => {
110
+ this.onmessage?.(event);
111
+ });
112
+ this.addEventListener("bufferedamountlow", (event) => {
113
+ this.onbufferedamountlow?.(event);
114
+ });
115
+ this.addEventListener("error", (event) => {
116
+ this.onerror?.(event);
117
+ });
118
+ this.addEventListener("close", (event) => {
119
+ this.onclose?.(event);
120
+ });
121
+ this.addEventListener("closing", (event) => {
122
+ this.onclosing?.(event);
123
+ });
124
+ this.addEventListener("open", (event) => {
125
+ this.onopen?.(event);
126
+ });
127
+ this.onbufferedamountlow = null;
128
+ this.onclose = null;
129
+ this.onclosing = null;
130
+ this.onerror = null;
131
+ this.onmessage = null;
132
+ this.onopen = null;
133
+ this.maxPacketLifeTime = dataChannelDict.maxPacketLifeTime ?? null;
134
+ this.maxRetransmits = dataChannelDict.maxRetransmits ?? null;
135
+ this.negotiated = dataChannelDict.negotiated ?? false;
136
+ this.ordered = dataChannelDict.ordered ?? true;
137
+ }
138
+ #dataChannel;
139
+ #bufferedAmountLowThreshold;
140
+ #readyState;
141
+ get id() {
142
+ return this.#dataChannel.getId();
143
+ }
144
+ get label() {
145
+ return this.#dataChannel.getLabel();
146
+ }
147
+ get protocol() {
148
+ return this.#dataChannel.getProtocol();
149
+ }
150
+ get bufferedAmount() {
151
+ return this.#dataChannel.bufferedAmount();
152
+ }
153
+ set bufferedAmountLowThreshold(threshold) {
154
+ this.#bufferedAmountLowThreshold = threshold;
155
+ this.#dataChannel.setBufferedAmountLowThreshold(threshold);
156
+ }
157
+ get bufferedAmountLowThreshold() {
158
+ return this.#bufferedAmountLowThreshold;
159
+ }
160
+ get readyState() {
161
+ return this.#readyState;
162
+ }
163
+ close() {
164
+ this.#readyState = "closing";
165
+ this.dispatchEvent(new Event("closing"));
166
+ this.#dataChannel.close();
167
+ }
168
+ send(data) {
169
+ if (typeof data === "string") {
170
+ this.#dataChannel.sendMessage(data);
171
+ } else {
172
+ this.#dataChannel.sendMessageBinary(data);
173
+ }
174
+ }
175
+ };
176
+ }
177
+ });
178
+
179
+ // packages/core/mesh/network-manager/src/transport/datachannel/rtc-events.ts
180
+ var PeerConnectionIceEvent, DataChannelEvent;
181
+ var init_rtc_events = __esm({
182
+ "packages/core/mesh/network-manager/src/transport/datachannel/rtc-events.ts"() {
183
+ "use strict";
184
+ PeerConnectionIceEvent = class extends Event {
185
+ constructor(candidate) {
186
+ super("icecandidate");
187
+ this.candidate = candidate;
188
+ }
189
+ };
190
+ DataChannelEvent = class extends Event {
191
+ constructor(channel) {
192
+ super("datachannel");
193
+ this.channel = channel;
194
+ }
195
+ };
196
+ }
197
+ });
198
+
199
+ // packages/core/mesh/network-manager/src/transport/datachannel/rtc-session-description.ts
200
+ var SessionDescription;
201
+ var init_rtc_session_description = __esm({
202
+ "packages/core/mesh/network-manager/src/transport/datachannel/rtc-session-description.ts"() {
203
+ "use strict";
204
+ SessionDescription = class {
205
+ constructor(init) {
206
+ this.sdp = init.sdp ?? "";
207
+ this.type = init.type;
208
+ }
209
+ toJSON() {
210
+ return {
211
+ sdp: this.sdp,
212
+ type: this.type
213
+ };
214
+ }
215
+ };
216
+ }
217
+ });
218
+
219
+ // packages/core/mesh/network-manager/src/transport/datachannel/rtc-peer-connection.ts
220
+ var import_node_datachannel, import_p_defer, PeerConnection, assertState, toSessionDescription, RTCPeerConnectionStates, RTCSdpTypes, RTCIceConnectionStates, RTCIceGatheringStates, RTCSignalingStates;
221
+ var init_rtc_peer_connection = __esm({
222
+ "packages/core/mesh/network-manager/src/transport/datachannel/rtc-peer-connection.ts"() {
223
+ "use strict";
224
+ import_node_datachannel = __toESM(require("node-datachannel"));
225
+ import_p_defer = __toESM(require("p-defer"));
226
+ init_rtc_data_channel();
227
+ init_rtc_events();
228
+ init_rtc_ice_candidate();
229
+ init_rtc_session_description();
230
+ PeerConnection = class extends EventTarget {
231
+ constructor(init = {}) {
232
+ super();
233
+ this.#config = init;
234
+ this.#localOffer = (0, import_p_defer.default)();
235
+ this.#localAnswer = (0, import_p_defer.default)();
236
+ this.#dataChannels = /* @__PURE__ */ new Set();
237
+ const iceServers = init.iceServers ?? [];
238
+ this.#peerConnection = new import_node_datachannel.default.PeerConnection(`peer-${Math.random()}`, {
239
+ iceServers: iceServers.map((server) => {
240
+ const urls = (Array.isArray(server.urls) ? server.urls : [
241
+ server.urls
242
+ ]).map((str) => new URL(str));
243
+ return urls.map((url) => {
244
+ const iceServer = {
245
+ hostname: url.hostname,
246
+ port: parseInt(url.port, 10),
247
+ username: server.username,
248
+ password: server.credential
249
+ };
250
+ return iceServer;
251
+ });
252
+ }).flat(),
253
+ iceTransportPolicy: init?.iceTransportPolicy
254
+ });
255
+ this.#peerConnection.onStateChange(() => {
256
+ this.dispatchEvent(new Event("connectionstatechange"));
257
+ });
258
+ this.#peerConnection.onGatheringStateChange(() => {
259
+ this.dispatchEvent(new Event("icegatheringstatechange"));
260
+ });
261
+ this.#peerConnection.onDataChannel((channel) => {
262
+ this.dispatchEvent(new DataChannelEvent(new DataChannel(channel)));
263
+ });
264
+ this.addEventListener("connectionstatechange", (event) => {
265
+ this.onconnectionstatechange?.(event);
266
+ });
267
+ this.addEventListener("signalingstatechange", (event) => {
268
+ this.onsignalingstatechange?.(event);
269
+ });
270
+ this.addEventListener("icegatheringstatechange", (event) => {
271
+ this.onicegatheringstatechange?.(event);
272
+ });
273
+ this.addEventListener("datachannel", (event) => {
274
+ this.ondatachannel?.(event);
275
+ });
276
+ this.#peerConnection.onLocalDescription((sdp, type) => {
277
+ if (type === "offer") {
278
+ this.#localOffer.resolve({
279
+ sdp,
280
+ type
281
+ });
282
+ }
283
+ if (type === "answer") {
284
+ this.#localAnswer.resolve({
285
+ sdp,
286
+ type
287
+ });
288
+ }
289
+ });
290
+ this.#peerConnection.onLocalCandidate((candidate, mid) => {
291
+ if (mid === "unspec") {
292
+ this.#localAnswer.reject(new Error(`Invalid description type ${mid}`));
293
+ return;
294
+ }
295
+ const event = new PeerConnectionIceEvent(new IceCandidate({
296
+ candidate
297
+ }));
298
+ this.onicecandidate?.(event);
299
+ });
300
+ this.canTrickleIceCandidates = null;
301
+ this.sctp = null;
302
+ this.onconnectionstatechange = null;
303
+ this.ondatachannel = null;
304
+ this.onicecandidate = null;
305
+ this.onicecandidateerror = null;
306
+ this.oniceconnectionstatechange = null;
307
+ this.onicegatheringstatechange = null;
308
+ this.onnegotiationneeded = null;
309
+ this.onsignalingstatechange = null;
310
+ this.ontrack = null;
311
+ }
312
+ static async generateCertificate(keygenAlgorithm) {
313
+ throw new Error("Not implemented");
314
+ }
315
+ #peerConnection;
316
+ #config;
317
+ #localOffer;
318
+ #localAnswer;
319
+ #dataChannels;
320
+ get connectionState() {
321
+ return assertState(this.#peerConnection.state(), RTCPeerConnectionStates);
322
+ }
323
+ get iceConnectionState() {
324
+ return assertState(this.#peerConnection.state(), RTCIceConnectionStates);
325
+ }
326
+ get iceGatheringState() {
327
+ return assertState(this.#peerConnection.gatheringState(), RTCIceGatheringStates);
328
+ }
329
+ get signalingState() {
330
+ return assertState(this.#peerConnection.signalingState(), RTCSignalingStates);
331
+ }
332
+ get currentLocalDescription() {
333
+ return toSessionDescription(this.#peerConnection.localDescription());
334
+ }
335
+ get localDescription() {
336
+ return toSessionDescription(this.#peerConnection.localDescription());
337
+ }
338
+ get pendingLocalDescription() {
339
+ return toSessionDescription(this.#peerConnection.localDescription());
340
+ }
341
+ get currentRemoteDescription() {
342
+ console.log("node-datachannel doesn't expose currentRemoteDescription");
343
+ return toSessionDescription(null);
344
+ }
345
+ get pendingRemoteDescription() {
346
+ console.log("node-datachannel doesn't expose pendingRemoteDescription");
347
+ return toSessionDescription(null);
348
+ }
349
+ get remoteDescription() {
350
+ console.log("node-datachannel doesn't expose remoteDescription");
351
+ return toSessionDescription(null);
352
+ }
353
+ async addIceCandidate(candidate) {
354
+ if (candidate == null || candidate.candidate == null) {
355
+ throw new Error("Candidate invalid");
356
+ }
357
+ this.#peerConnection.addRemoteCandidate(candidate.candidate, candidate.sdpMid ?? "0");
358
+ }
359
+ addTrack(track, ...streams) {
360
+ throw new Error("addTrack Not implemented");
361
+ }
362
+ addTransceiver(trackOrKind, init) {
363
+ throw new Error("addTransciever Not implemented");
364
+ }
365
+ close() {
366
+ this.#dataChannels.forEach((channel) => {
367
+ channel.close();
368
+ });
369
+ this.#peerConnection.close();
370
+ this.#peerConnection.destroy();
371
+ }
372
+ createDataChannel(label, dataChannelDict = {}) {
373
+ const channel = this.#peerConnection.createDataChannel(label, dataChannelDict);
374
+ const dataChannel = new DataChannel(channel, dataChannelDict);
375
+ this.#dataChannels.add(dataChannel);
376
+ dataChannel.addEventListener("close", () => {
377
+ this.#dataChannels.delete(dataChannel);
378
+ });
379
+ return dataChannel;
380
+ }
381
+ async createOffer(...args) {
382
+ return this.#localOffer.promise;
383
+ }
384
+ async createAnswer(...args) {
385
+ return this.#localAnswer.promise;
386
+ }
387
+ getConfiguration() {
388
+ return this.#config;
389
+ }
390
+ getReceivers() {
391
+ throw new Error("getReceivers Not implemented");
392
+ }
393
+ getSenders() {
394
+ throw new Error("getSenders Not implemented");
395
+ }
396
+ async getStats(selector) {
397
+ throw new Error("getStats Not implemented");
398
+ }
399
+ getTransceivers() {
400
+ throw new Error("getTranscievers Not implemented");
401
+ }
402
+ removeTrack(sender) {
403
+ throw new Error("removeTrack Not implemented");
404
+ }
405
+ restartIce() {
406
+ throw new Error("restartIce Not implemented");
407
+ }
408
+ setConfiguration(configuration = {}) {
409
+ this.#config = configuration;
410
+ }
411
+ async setLocalDescription(description) {
412
+ if (description == null || description.type == null) {
413
+ throw new Error("Local description type must be set");
414
+ }
415
+ if (description.type !== "offer") {
416
+ console.log(`node-datachannel: setLocalDescription: only offer is supported, not ${description.type}`);
417
+ return;
418
+ }
419
+ this.#peerConnection.setLocalDescription(description.type);
420
+ }
421
+ async setRemoteDescription(description) {
422
+ if (description.sdp == null) {
423
+ throw new Error("Remote SDP must be set");
424
+ }
425
+ this.#peerConnection.setRemoteDescription(description.sdp, description.type);
426
+ }
427
+ };
428
+ assertState = (state, states) => {
429
+ if (state != null && !states.includes(state)) {
430
+ throw new Error(`Invalid value encountered - "${state}" must be one of ${states}`);
431
+ }
432
+ return state;
433
+ };
434
+ toSessionDescription = (description) => {
435
+ if (description == null) {
436
+ return null;
437
+ }
438
+ return new SessionDescription({
439
+ sdp: description.sdp,
440
+ type: assertState(description.type, RTCSdpTypes)
441
+ });
442
+ };
443
+ RTCPeerConnectionStates = [
444
+ "closed",
445
+ "connected",
446
+ "connecting",
447
+ "disconnected",
448
+ "failed",
449
+ "new"
450
+ ];
451
+ RTCSdpTypes = [
452
+ "answer",
453
+ "offer",
454
+ "pranswer",
455
+ "rollback"
456
+ ];
457
+ RTCIceConnectionStates = [
458
+ "checking",
459
+ "closed",
460
+ "completed",
461
+ "connected",
462
+ "disconnected",
463
+ "failed",
464
+ "new"
465
+ ];
466
+ RTCIceGatheringStates = [
467
+ "complete",
468
+ "gathering",
469
+ "new"
470
+ ];
471
+ RTCSignalingStates = [
472
+ "closed",
473
+ "have-local-offer",
474
+ "have-local-pranswer",
475
+ "have-remote-offer",
476
+ "have-remote-pranswer",
477
+ "stable"
478
+ ];
479
+ }
480
+ });
481
+
482
+ // packages/core/mesh/network-manager/src/transport/datachannel/index.ts
483
+ var datachannel_exports = {};
484
+ __export(datachannel_exports, {
485
+ RTCIceCandidate: () => IceCandidate,
486
+ RTCPeerConnection: () => PeerConnection,
487
+ RTCSessionDescription: () => SessionDescription,
488
+ cleanup: () => cleanup
489
+ });
490
+ var import_node_datachannel2, cleanup;
491
+ var init_datachannel = __esm({
492
+ "packages/core/mesh/network-manager/src/transport/datachannel/index.ts"() {
493
+ "use strict";
494
+ import_node_datachannel2 = __toESM(require("node-datachannel"));
495
+ init_rtc_ice_candidate();
496
+ init_rtc_peer_connection();
497
+ init_rtc_session_description();
498
+ cleanup = () => {
499
+ import_node_datachannel2.default.cleanup();
500
+ };
501
+ }
502
+ });
503
+
30
504
  // packages/core/mesh/network-manager/src/index.ts
31
505
  var src_exports = {};
32
506
  __export(src_exports, {
@@ -36,21 +510,28 @@ __export(src_exports, {
36
510
  ConnectionState: () => ConnectionState,
37
511
  EventType: () => EventType,
38
512
  FullyConnectedTopology: () => FullyConnectedTopology,
513
+ LibDataChannelTransport: () => LibDataChannelTransport,
39
514
  MAX_CONCURRENT_INITIATING_CONNECTIONS: () => MAX_CONCURRENT_INITIATING_CONNECTIONS,
40
515
  MMSTTopology: () => MMSTTopology,
41
516
  MemoryTransport: () => MemoryTransport,
42
517
  MemoryTransportFactory: () => MemoryTransportFactory,
43
518
  NetworkManager: () => NetworkManager,
519
+ RTCIceCandidate: () => IceCandidate,
520
+ RTCPeerConnection: () => PeerConnection,
521
+ RTCSessionDescription: () => SessionDescription,
522
+ SimplePeerTransport: () => SimplePeerTransport,
523
+ SimplePeerTransportProxy: () => SimplePeerTransportProxy,
524
+ SimplePeerTransportProxyFactory: () => SimplePeerTransportProxyFactory,
525
+ SimplePeerTransportService: () => SimplePeerTransportService,
44
526
  StarTopology: () => StarTopology,
45
527
  Swarm: () => Swarm,
46
528
  SwarmMapper: () => SwarmMapper,
47
529
  SwarmMessenger: () => SwarmMessenger,
48
- WebRTCTransport: () => WebRTCTransport,
49
- WebRTCTransportProxy: () => WebRTCTransportProxy,
50
- WebRTCTransportProxyFactory: () => WebRTCTransportProxyFactory,
51
- WebRTCTransportService: () => WebRTCTransportService,
52
- createTeleportProtocolFactory: () => createTeleportProtocolFactory,
53
- createWebRTCTransportFactory: () => createWebRTCTransportFactory
530
+ TransportKind: () => TransportKind,
531
+ cleanup: () => cleanup,
532
+ createLibDataChannelTransportFactory: () => createLibDataChannelTransportFactory,
533
+ createSimplePeerTransportFactory: () => createSimplePeerTransportFactory,
534
+ createTeleportProtocolFactory: () => createTeleportProtocolFactory
54
535
  });
55
536
  module.exports = __toCommonJS(src_exports);
56
537
 
@@ -79,11 +560,17 @@ function _ts_decorate(decorators, target, key, desc) {
79
560
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
80
561
  return c > 3 && r && Object.defineProperty(target, key, r), r;
81
562
  }
82
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
563
+ var __dxlog_file = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/connection.ts";
83
564
  var STARTING_SIGNALLING_DELAY = 10;
84
565
  var MAX_SIGNALLING_DELAY = 300;
85
566
  var ConnectionState;
86
567
  (function(ConnectionState5) {
568
+ ConnectionState5[
569
+ /**
570
+ * Connection is created, but not yet passed through the connection limiter.
571
+ */
572
+ "CREATED"
573
+ ] = "CREATED";
87
574
  ConnectionState5[
88
575
  /**
89
576
  * Initial state. Connection is registered but no attempt to connect to the remote peer has been performed.
@@ -128,7 +615,7 @@ var Connection = class {
128
615
  this._transportFactory = _transportFactory;
129
616
  this._callbacks = _callbacks;
130
617
  this._ctx = new import_context.Context();
131
- this._state = ConnectionState.INITIAL;
618
+ this._state = ConnectionState.CREATED;
132
619
  this._incomingSignalBuffer = [];
133
620
  this._outgoingSignalBuffer = [];
134
621
  this.stateChanged = new import_async.Event();
@@ -146,7 +633,7 @@ var Connection = class {
146
633
  initiator: this.initiator
147
634
  }, {
148
635
  F: __dxlog_file,
149
- L: 109,
636
+ L: 114,
150
637
  S: this,
151
638
  C: (f, a) => f(...a)
152
639
  });
@@ -166,7 +653,7 @@ var Connection = class {
166
653
  async openConnection() {
167
654
  (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
168
655
  F: __dxlog_file,
169
- L: 134,
656
+ L: 139,
170
657
  S: this,
171
658
  A: [
172
659
  "this._state === ConnectionState.INITIAL",
@@ -177,7 +664,7 @@ var Connection = class {
177
664
  id: this._instanceId
178
665
  }), {
179
666
  F: __dxlog_file,
180
- L: 135,
667
+ L: 140,
181
668
  S: this,
182
669
  C: (f, a) => f(...a)
183
670
  });
@@ -189,18 +676,18 @@ var Connection = class {
189
676
  initiator: this.initiator
190
677
  }, {
191
678
  F: __dxlog_file,
192
- L: 136,
679
+ L: 141,
193
680
  S: this,
194
681
  C: (f, a) => f(...a)
195
682
  });
196
683
  this._changeState(ConnectionState.CONNECTING);
197
- this._protocol.initialize().catch((err) => {
684
+ this._protocol.open().catch((err) => {
198
685
  this.errors.raise(err);
199
686
  });
200
687
  this._protocol.stream.on("close", () => {
201
688
  (0, import_log.log)("protocol stream closed", void 0, {
202
689
  F: __dxlog_file,
203
- L: 153,
690
+ L: 158,
204
691
  S: this,
205
692
  C: (f, a) => f(...a)
206
693
  });
@@ -208,7 +695,7 @@ var Connection = class {
208
695
  });
209
696
  (0, import_invariant.invariant)(!this._transport, void 0, {
210
697
  F: __dxlog_file,
211
- L: 157,
698
+ L: 162,
212
699
  S: this,
213
700
  A: [
214
701
  "!this._transport",
@@ -226,12 +713,35 @@ var Connection = class {
226
713
  });
227
714
  this._transport.closed.once(() => {
228
715
  this._transport = void 0;
229
- this.close().catch((err) => this.errors.raise(err));
716
+ (0, import_log.log)("abort triggered by transport close", void 0, {
717
+ F: __dxlog_file,
718
+ L: 176,
719
+ S: this,
720
+ C: (f, a) => f(...a)
721
+ });
722
+ this.abort().catch((err) => this.errors.raise(err));
230
723
  });
231
724
  this._transport.errors.handle((err) => {
725
+ (0, import_log.log)("transport error:", {
726
+ err
727
+ }, {
728
+ F: __dxlog_file,
729
+ L: 181,
730
+ S: this,
731
+ C: (f, a) => f(...a)
732
+ });
232
733
  if (!this.closeReason) {
233
734
  this.closeReason = err?.message;
234
735
  }
736
+ if (err?.message.includes("OperationError")) {
737
+ (0, import_log.log)("aborting due to transport OperationError", void 0, {
738
+ F: __dxlog_file,
739
+ L: 187,
740
+ S: this,
741
+ C: (f, a) => f(...a)
742
+ });
743
+ this.abort().catch((err2) => this.errors.raise(err2));
744
+ }
235
745
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
236
746
  this.errors.raise(err);
237
747
  }
@@ -244,11 +754,65 @@ var Connection = class {
244
754
  id: this._instanceId
245
755
  }), {
246
756
  F: __dxlog_file,
247
- L: 190,
757
+ L: 203,
248
758
  S: this,
249
759
  C: (f, a) => f(...a)
250
760
  });
251
761
  }
762
+ async abort(err) {
763
+ (0, import_log.log)("aborting...", {
764
+ err
765
+ }, {
766
+ F: __dxlog_file,
767
+ L: 209,
768
+ S: this,
769
+ C: (f, a) => f(...a)
770
+ });
771
+ if (!this.closeReason) {
772
+ this.closeReason = err?.message;
773
+ }
774
+ if (this._state === ConnectionState.CLOSED) {
775
+ (0, import_log.log)("abort ignored: already closed", this.closeReason, {
776
+ F: __dxlog_file,
777
+ L: 214,
778
+ S: this,
779
+ C: (f, a) => f(...a)
780
+ });
781
+ return;
782
+ }
783
+ this._changeState(ConnectionState.CLOSING);
784
+ await this._ctx.dispose();
785
+ try {
786
+ (0, import_log.log)("aborting protocol... ", {
787
+ proto: this._protocol
788
+ }, {
789
+ F: __dxlog_file,
790
+ L: 224,
791
+ S: this,
792
+ C: (f, a) => f(...a)
793
+ });
794
+ await this._protocol.abort();
795
+ } catch (err2) {
796
+ import_log.log.catch(err2, void 0, {
797
+ F: __dxlog_file,
798
+ L: 227,
799
+ S: this,
800
+ C: (f, a) => f(...a)
801
+ });
802
+ }
803
+ try {
804
+ await this._transport?.destroy();
805
+ } catch (err2) {
806
+ import_log.log.catch(err2, void 0, {
807
+ F: __dxlog_file,
808
+ L: 234,
809
+ S: this,
810
+ C: (f, a) => f(...a)
811
+ });
812
+ }
813
+ this._changeState(ConnectionState.CLOSED);
814
+ this._callbacks?.onClosed?.(err);
815
+ }
252
816
  async close(err) {
253
817
  if (!this.closeReason) {
254
818
  this.closeReason = err?.message;
@@ -262,16 +826,16 @@ var Connection = class {
262
826
  peerId: this.ownId
263
827
  }, {
264
828
  F: __dxlog_file,
265
- L: 205,
829
+ L: 253,
266
830
  S: this,
267
831
  C: (f, a) => f(...a)
268
832
  });
269
833
  try {
270
- await this._protocol.destroy();
834
+ await this._protocol.close();
271
835
  } catch (err2) {
272
836
  import_log.log.catch(err2, void 0, {
273
837
  F: __dxlog_file,
274
- L: 211,
838
+ L: 259,
275
839
  S: this,
276
840
  C: (f, a) => f(...a)
277
841
  });
@@ -281,7 +845,7 @@ var Connection = class {
281
845
  } catch (err2) {
282
846
  import_log.log.catch(err2, void 0, {
283
847
  F: __dxlog_file,
284
- L: 218,
848
+ L: 266,
285
849
  S: this,
286
850
  C: (f, a) => f(...a)
287
851
  });
@@ -290,7 +854,7 @@ var Connection = class {
290
854
  peerId: this.ownId
291
855
  }, {
292
856
  F: __dxlog_file,
293
- L: 221,
857
+ L: 269,
294
858
  S: this,
295
859
  C: (f, a) => f(...a)
296
860
  });
@@ -329,11 +893,11 @@ var Connection = class {
329
893
  if (err instanceof import_errors.CancelledError) {
330
894
  return;
331
895
  }
332
- import_log.log.warn("Signal failed", {
896
+ import_log.log.warn("signal failed", {
333
897
  err
334
898
  }, {
335
899
  F: __dxlog_file,
336
- L: 258,
900
+ L: 306,
337
901
  S: this,
338
902
  C: (f, a) => f(...a)
339
903
  });
@@ -346,7 +910,7 @@ var Connection = class {
346
910
  async signal(msg) {
347
911
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
348
912
  F: __dxlog_file,
349
- L: 267,
913
+ L: 315,
350
914
  S: this,
351
915
  A: [
352
916
  "msg.sessionId",
@@ -356,7 +920,7 @@ var Connection = class {
356
920
  if (!msg.sessionId.equals(this.sessionId)) {
357
921
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
358
922
  F: __dxlog_file,
359
- L: 269,
923
+ L: 317,
360
924
  S: this,
361
925
  C: (f, a) => f(...a)
362
926
  });
@@ -364,7 +928,7 @@ var Connection = class {
364
928
  }
365
929
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
366
930
  F: __dxlog_file,
367
- L: 272,
931
+ L: 320,
368
932
  S: this,
369
933
  A: [
370
934
  "msg.data.signal || msg.data.signalBatch",
@@ -373,7 +937,7 @@ var Connection = class {
373
937
  });
374
938
  (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
375
939
  F: __dxlog_file,
376
- L: 273,
940
+ L: 321,
377
941
  S: this,
378
942
  A: [
379
943
  "msg.author?.equals(this.remoteId)",
@@ -382,7 +946,7 @@ var Connection = class {
382
946
  });
383
947
  (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
384
948
  F: __dxlog_file,
385
- L: 274,
949
+ L: 322,
386
950
  S: this,
387
951
  A: [
388
952
  "msg.recipient?.equals(this.ownId)",
@@ -396,14 +960,17 @@ var Connection = class {
396
960
  if (!signal) {
397
961
  continue;
398
962
  }
399
- if (this._state === ConnectionState.INITIAL) {
963
+ if ([
964
+ ConnectionState.CREATED,
965
+ ConnectionState.INITIAL
966
+ ].includes(this.state)) {
400
967
  (0, import_log.log)("buffered signal", {
401
968
  peerId: this.ownId,
402
969
  remoteId: this.remoteId,
403
970
  msg: msg.data
404
971
  }, {
405
972
  F: __dxlog_file,
406
- L: 283,
973
+ L: 331,
407
974
  S: this,
408
975
  C: (f, a) => f(...a)
409
976
  });
@@ -411,7 +978,7 @@ var Connection = class {
411
978
  } else {
412
979
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
413
980
  F: __dxlog_file,
414
- L: 286,
981
+ L: 334,
415
982
  S: this,
416
983
  A: [
417
984
  "this._transport",
@@ -424,7 +991,7 @@ var Connection = class {
424
991
  msg: msg.data
425
992
  }, {
426
993
  F: __dxlog_file,
427
- L: 287,
994
+ L: 335,
428
995
  S: this,
429
996
  C: (f, a) => f(...a)
430
997
  });
@@ -432,6 +999,9 @@ var Connection = class {
432
999
  }
433
1000
  }
434
1001
  }
1002
+ initiate() {
1003
+ this._changeState(ConnectionState.INITIAL);
1004
+ }
435
1005
  _changeState(state) {
436
1006
  (0, import_log.log)("stateChanged", {
437
1007
  from: this._state,
@@ -439,13 +1009,13 @@ var Connection = class {
439
1009
  peerId: this.ownId
440
1010
  }, {
441
1011
  F: __dxlog_file,
442
- L: 294,
1012
+ L: 346,
443
1013
  S: this,
444
1014
  C: (f, a) => f(...a)
445
1015
  });
446
1016
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
447
1017
  F: __dxlog_file,
448
- L: 295,
1018
+ L: 347,
449
1019
  S: this,
450
1020
  A: [
451
1021
  "state !== this._state",
@@ -456,6 +1026,9 @@ var Connection = class {
456
1026
  this.stateChanged.emit(state);
457
1027
  }
458
1028
  };
1029
+ _ts_decorate([
1030
+ import_async.synchronized
1031
+ ], Connection.prototype, "abort", null);
459
1032
  _ts_decorate([
460
1033
  import_async.synchronized
461
1034
  ], Connection.prototype, "close", null);
@@ -477,7 +1050,7 @@ var import_keys2 = require("@dxos/keys");
477
1050
  var import_log2 = require("@dxos/log");
478
1051
  var import_protocols2 = require("@dxos/protocols");
479
1052
  var import_util = require("@dxos/util");
480
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1053
+ var __dxlog_file2 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
481
1054
  var SwarmMessage = import_protocols2.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
482
1055
  var SwarmMessenger = class {
483
1056
  constructor({ sendMessage, onSignal, onOffer, topic }) {
@@ -719,7 +1292,7 @@ function _ts_decorate2(decorators, target, key, desc) {
719
1292
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
720
1293
  return c > 3 && r && Object.defineProperty(target, key, r), r;
721
1294
  }
722
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1295
+ var __dxlog_file3 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/peer.ts";
723
1296
  var ConnectionDisplacedError = class extends import_errors2.SystemError {
724
1297
  constructor() {
725
1298
  super("Connection displaced by remote initiator.");
@@ -758,7 +1331,7 @@ var Peer = class {
758
1331
  sessionId: this.connection?.sessionId
759
1332
  }, {
760
1333
  F: __dxlog_file3,
761
- L: 116,
1334
+ L: 117,
762
1335
  S: this,
763
1336
  C: (f, a) => f(...a)
764
1337
  });
@@ -775,7 +1348,7 @@ var Peer = class {
775
1348
  if (!this.connection) {
776
1349
  (0, import_invariant3.invariant)(message.sessionId, void 0, {
777
1350
  F: __dxlog_file3,
778
- L: 136,
1351
+ L: 137,
779
1352
  S: this,
780
1353
  A: [
781
1354
  "message.sessionId",
@@ -785,6 +1358,7 @@ var Peer = class {
785
1358
  const connection = this._createConnection(false, message.sessionId);
786
1359
  try {
787
1360
  await this._connectionLimiter.connecting(message.sessionId);
1361
+ connection.initiate();
788
1362
  await connection.openConnection();
789
1363
  } catch (err) {
790
1364
  if (!(err instanceof import_errors2.CancelledError)) {
@@ -795,7 +1369,7 @@ var Peer = class {
795
1369
  err
796
1370
  }, {
797
1371
  F: __dxlog_file3,
798
- L: 144,
1372
+ L: 147,
799
1373
  S: this,
800
1374
  C: (f, a) => f(...a)
801
1375
  });
@@ -817,7 +1391,7 @@ var Peer = class {
817
1391
  async initiateConnection() {
818
1392
  (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
819
1393
  F: __dxlog_file3,
820
- L: 161,
1394
+ L: 164,
821
1395
  S: this,
822
1396
  A: [
823
1397
  "!this.initiating",
@@ -826,7 +1400,7 @@ var Peer = class {
826
1400
  });
827
1401
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
828
1402
  F: __dxlog_file3,
829
- L: 162,
1403
+ L: 165,
830
1404
  S: this,
831
1405
  A: [
832
1406
  "!this.connection",
@@ -841,7 +1415,7 @@ var Peer = class {
841
1415
  sessionId
842
1416
  }, {
843
1417
  F: __dxlog_file3,
844
- L: 164,
1418
+ L: 167,
845
1419
  S: this,
846
1420
  C: (f, a) => f(...a)
847
1421
  });
@@ -849,6 +1423,7 @@ var Peer = class {
849
1423
  this.initiating = true;
850
1424
  try {
851
1425
  await this._connectionLimiter.connecting(sessionId);
1426
+ connection.initiate();
852
1427
  const answer = await this._signalMessaging.offer({
853
1428
  author: this.localPeerId,
854
1429
  recipient: this.id,
@@ -865,14 +1440,14 @@ var Peer = class {
865
1440
  remoteId: this.id
866
1441
  }, {
867
1442
  F: __dxlog_file3,
868
- L: 179,
1443
+ L: 183,
869
1444
  S: this,
870
1445
  C: (f, a) => f(...a)
871
1446
  });
872
1447
  if (connection.state !== ConnectionState.INITIAL) {
873
1448
  (0, import_log3.log)("ignoring response", void 0, {
874
1449
  F: __dxlog_file3,
875
- L: 181,
1450
+ L: 185,
876
1451
  S: this,
877
1452
  C: (f, a) => f(...a)
878
1453
  });
@@ -892,7 +1467,7 @@ var Peer = class {
892
1467
  remoteId: this.id
893
1468
  }, {
894
1469
  F: __dxlog_file3,
895
- L: 192,
1470
+ L: 196,
896
1471
  S: this,
897
1472
  C: (f, a) => f(...a)
898
1473
  });
@@ -915,13 +1490,13 @@ var Peer = class {
915
1490
  sessionId
916
1491
  }, {
917
1492
  F: __dxlog_file3,
918
- L: 206,
1493
+ L: 210,
919
1494
  S: this,
920
1495
  C: (f, a) => f(...a)
921
1496
  });
922
1497
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
923
1498
  F: __dxlog_file3,
924
- L: 213,
1499
+ L: 217,
925
1500
  S: this,
926
1501
  A: [
927
1502
  "!this.connection",
@@ -957,7 +1532,7 @@ var Peer = class {
957
1532
  initiator
958
1533
  }, {
959
1534
  F: __dxlog_file3,
960
- L: 232,
1535
+ L: 236,
961
1536
  S: this,
962
1537
  C: (f, a) => f(...a)
963
1538
  });
@@ -970,14 +1545,14 @@ var Peer = class {
970
1545
  initiator
971
1546
  }, {
972
1547
  F: __dxlog_file3,
973
- L: 241,
1548
+ L: 245,
974
1549
  S: this,
975
1550
  C: (f, a) => f(...a)
976
1551
  });
977
1552
  this._connectionLimiter.doneConnecting(sessionId);
978
1553
  (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
979
1554
  F: __dxlog_file3,
980
- L: 246,
1555
+ L: 250,
981
1556
  S: this,
982
1557
  A: [
983
1558
  "this.connection === connection",
@@ -992,7 +1567,7 @@ var Peer = class {
992
1567
  initiator
993
1568
  }, {
994
1569
  F: __dxlog_file3,
995
- L: 248,
1570
+ L: 252,
996
1571
  S: this,
997
1572
  C: (f, a) => f(...a)
998
1573
  });
@@ -1027,7 +1602,7 @@ var Peer = class {
1027
1602
  err
1028
1603
  }, {
1029
1604
  F: __dxlog_file3,
1030
- L: 288,
1605
+ L: 292,
1031
1606
  S: this,
1032
1607
  C: (f, a) => f(...a)
1033
1608
  });
@@ -1040,7 +1615,7 @@ var Peer = class {
1040
1615
  err
1041
1616
  }, {
1042
1617
  F: __dxlog_file3,
1043
- L: 289,
1618
+ L: 293,
1044
1619
  S: this,
1045
1620
  C: (f, a) => f(...a)
1046
1621
  });
@@ -1059,7 +1634,7 @@ var Peer = class {
1059
1634
  sessionId: connection.sessionId
1060
1635
  }, {
1061
1636
  F: __dxlog_file3,
1062
- L: 314,
1637
+ L: 318,
1063
1638
  S: this,
1064
1639
  C: (f, a) => f(...a)
1065
1640
  });
@@ -1069,7 +1644,7 @@ var Peer = class {
1069
1644
  sessionId: connection.sessionId
1070
1645
  }, {
1071
1646
  F: __dxlog_file3,
1072
- L: 320,
1647
+ L: 324,
1073
1648
  S: this,
1074
1649
  C: (f, a) => f(...a)
1075
1650
  });
@@ -1080,7 +1655,7 @@ var Peer = class {
1080
1655
  message
1081
1656
  }, {
1082
1657
  F: __dxlog_file3,
1083
- L: 325,
1658
+ L: 329,
1084
1659
  S: this,
1085
1660
  C: (f, a) => f(...a)
1086
1661
  });
@@ -1095,7 +1670,7 @@ var Peer = class {
1095
1670
  topic: this.topic
1096
1671
  }, {
1097
1672
  F: __dxlog_file3,
1098
- L: 335,
1673
+ L: 339,
1099
1674
  S: this,
1100
1675
  C: (f, a) => f(...a)
1101
1676
  });
@@ -1129,7 +1704,7 @@ function _ts_decorate3(decorators, target, key, desc) {
1129
1704
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1130
1705
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1131
1706
  }
1132
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1707
+ var __dxlog_file4 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1133
1708
  var INITIATION_DELAY = 100;
1134
1709
  var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
1135
1710
  var Swarm = class {
@@ -1603,7 +2178,7 @@ var import_async4 = require("@dxos/async");
1603
2178
  var import_keys5 = require("@dxos/keys");
1604
2179
  var import_log5 = require("@dxos/log");
1605
2180
  var import_util3 = require("@dxos/util");
1606
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
2181
+ var __dxlog_file5 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
1607
2182
  var SwarmMapper = class {
1608
2183
  get peers() {
1609
2184
  return Array.from(this._peers.values());
@@ -1676,7 +2251,7 @@ var import_invariant5 = require("@dxos/invariant");
1676
2251
  var import_keys6 = require("@dxos/keys");
1677
2252
  var import_log6 = require("@dxos/log");
1678
2253
  var import_util4 = require("@dxos/util");
1679
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2254
+ var __dxlog_file6 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1680
2255
  var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
1681
2256
  var ConnectionLimiter = class {
1682
2257
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
@@ -1785,7 +2360,7 @@ var ConnectionLog = class {
1785
2360
  this.update.emit();
1786
2361
  swarm.connectionAdded.on((connection) => {
1787
2362
  const connectionInfo = {
1788
- state: ConnectionState.INITIAL,
2363
+ state: ConnectionState.CREATED,
1789
2364
  closeReason: connection.closeReason,
1790
2365
  remotePeerId: connection.remoteId,
1791
2366
  sessionId: connection.sessionId,
@@ -1825,7 +2400,7 @@ var import_messaging = require("@dxos/messaging");
1825
2400
  var import_protocols4 = require("@dxos/protocols");
1826
2401
  var import_services = require("@dxos/protocols/proto/dxos/client/services");
1827
2402
  var import_util6 = require("@dxos/util");
1828
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
2403
+ var __dxlog_file7 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/network-manager.ts";
1829
2404
  var NetworkManager = class {
1830
2405
  constructor({ transportFactory, signalManager, log: log1 }) {
1831
2406
  /**
@@ -2061,7 +2636,7 @@ var NetworkManager = class {
2061
2636
 
2062
2637
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
2063
2638
  var import_invariant7 = require("@dxos/invariant");
2064
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2639
+ var __dxlog_file8 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2065
2640
  var FullyConnectedTopology = class {
2066
2641
  toString() {
2067
2642
  return "FullyConnectedTopology";
@@ -2104,7 +2679,7 @@ var FullyConnectedTopology = class {
2104
2679
  var import_xor_distance = __toESM(require("xor-distance"));
2105
2680
  var import_invariant8 = require("@dxos/invariant");
2106
2681
  var import_log8 = require("@dxos/log");
2107
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2682
+ var __dxlog_file9 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2108
2683
  var MMSTTopology = class {
2109
2684
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
2110
2685
  this._sampleCollected = false;
@@ -2213,7 +2788,7 @@ var sortByXorDistance = (keys, reference) => keys.sort((a, b) => import_xor_dist
2213
2788
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
2214
2789
  var import_invariant9 = require("@dxos/invariant");
2215
2790
  var import_log9 = require("@dxos/log");
2216
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2791
+ var __dxlog_file10 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2217
2792
  var StarTopology = class {
2218
2793
  // prettier-ignore
2219
2794
  constructor(_centralPeer) {
@@ -2325,7 +2900,7 @@ function _ts_decorate4(decorators, target, key, desc) {
2325
2900
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2326
2901
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2327
2902
  }
2328
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2903
+ var __dxlog_file11 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2329
2904
  var MEMORY_TRANSPORT_DELAY = 1;
2330
2905
  var createStreamDelay = (delay) => {
2331
2906
  return new import_node_stream.Transform({
@@ -2493,11 +3068,20 @@ _ts_decorate4([
2493
3068
  import_log10.logInfo
2494
3069
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
2495
3070
 
2496
- // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
3071
+ // packages/core/mesh/network-manager/src/transport/transport.ts
3072
+ var TransportKind;
3073
+ (function(TransportKind2) {
3074
+ TransportKind2["SIMPLE_PEER"] = "SIMPLE_PEER";
3075
+ TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
3076
+ TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
3077
+ TransportKind2["MEMORY"] = "MEMORY";
3078
+ })(TransportKind || (TransportKind = {}));
3079
+
3080
+ // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
2497
3081
  var import_simple_peer = __toESM(require("simple-peer"));
3082
+ var import_tiny_invariant = __toESM(require("tiny-invariant"));
2498
3083
  var import_async9 = require("@dxos/async");
2499
3084
  var import_debug5 = require("@dxos/debug");
2500
- var import_invariant11 = require("@dxos/invariant");
2501
3085
  var import_keys10 = require("@dxos/keys");
2502
3086
  var import_log11 = require("@dxos/log");
2503
3087
  var import_protocols5 = require("@dxos/protocols");
@@ -2509,44 +3093,40 @@ try {
2509
3093
  } catch {
2510
3094
  }
2511
3095
 
2512
- // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2513
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2514
- var WebRTCTransport = class {
3096
+ // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
3097
+ var __dxlog_file12 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
3098
+ var SimplePeerTransport = class {
2515
3099
  constructor(params) {
2516
3100
  this.params = params;
2517
- this._instanceId = import_keys10.PublicKey.random().toHex();
2518
3101
  this._closed = false;
2519
3102
  this.closed = new import_async9.Event();
2520
3103
  this.connected = new import_async9.Event();
2521
3104
  this.errors = new import_debug5.ErrorStream();
3105
+ this._instanceId = import_keys10.PublicKey.random().toHex();
2522
3106
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2523
3107
  id: this._instanceId
2524
3108
  }), {
2525
3109
  F: __dxlog_file12,
2526
- L: 44,
3110
+ L: 39,
2527
3111
  S: this,
2528
3112
  C: (f, a) => f(...a)
2529
3113
  });
2530
3114
  (0, import_log11.log)("created connection", params, {
2531
3115
  F: __dxlog_file12,
2532
- L: 45,
3116
+ L: 40,
2533
3117
  S: this,
2534
3118
  C: (f, a) => f(...a)
2535
3119
  });
2536
3120
  this._peer = new import_simple_peer.default({
3121
+ channelName: "dxos.mesh.transport",
2537
3122
  initiator: this.params.initiator,
2538
- config: this.params.webrtcConfig,
2539
- // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel
2540
- channelConfig: {
2541
- protocol: "dxos.mesh"
2542
- },
2543
- // Custom WebRTC implementation (for Node); otherwise get-browser-rtc.
2544
- wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available"))
3123
+ wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available")),
3124
+ config: this.params.webrtcConfig
2545
3125
  });
2546
3126
  this._peer.on("signal", async (data) => {
2547
3127
  (0, import_log11.log)("signal", data, {
2548
3128
  F: __dxlog_file12,
2549
- L: 62,
3129
+ L: 49,
2550
3130
  S: this,
2551
3131
  C: (f, a) => f(...a)
2552
3132
  });
@@ -2559,7 +3139,7 @@ var WebRTCTransport = class {
2559
3139
  this._peer.on("connect", () => {
2560
3140
  (0, import_log11.log)("connected", void 0, {
2561
3141
  F: __dxlog_file12,
2562
- L: 67,
3142
+ L: 54,
2563
3143
  S: this,
2564
3144
  C: (f, a) => f(...a)
2565
3145
  });
@@ -2569,7 +3149,7 @@ var WebRTCTransport = class {
2569
3149
  this._peer.on("close", async () => {
2570
3150
  (0, import_log11.log)("closed", void 0, {
2571
3151
  F: __dxlog_file12,
2572
- L: 73,
3152
+ L: 60,
2573
3153
  S: this,
2574
3154
  C: (f, a) => f(...a)
2575
3155
  });
@@ -2586,7 +3166,7 @@ var WebRTCTransport = class {
2586
3166
  stats
2587
3167
  }, {
2588
3168
  F: __dxlog_file12,
2589
- L: 85,
3169
+ L: 72,
2590
3170
  S: this,
2591
3171
  C: (f, a) => f(...a)
2592
3172
  });
@@ -2600,7 +3180,7 @@ var WebRTCTransport = class {
2600
3180
  id: this._instanceId
2601
3181
  }), {
2602
3182
  F: __dxlog_file12,
2603
- L: 95,
3183
+ L: 82,
2604
3184
  S: this,
2605
3185
  C: (f, a) => f(...a)
2606
3186
  });
@@ -2608,7 +3188,7 @@ var WebRTCTransport = class {
2608
3188
  async destroy() {
2609
3189
  (0, import_log11.log)("closing...", void 0, {
2610
3190
  F: __dxlog_file12,
2611
- L: 99,
3191
+ L: 86,
2612
3192
  S: this,
2613
3193
  C: (f, a) => f(...a)
2614
3194
  });
@@ -2618,7 +3198,7 @@ var WebRTCTransport = class {
2618
3198
  this.closed.emit();
2619
3199
  (0, import_log11.log)("closed", void 0, {
2620
3200
  F: __dxlog_file12,
2621
- L: 104,
3201
+ L: 91,
2622
3202
  S: this,
2623
3203
  C: (f, a) => f(...a)
2624
3204
  });
@@ -2627,38 +3207,30 @@ var WebRTCTransport = class {
2627
3207
  if (this._closed) {
2628
3208
  return;
2629
3209
  }
2630
- (0, import_invariant11.invariant)(signal.payload.data, "Signal message must contain signal data.", {
2631
- F: __dxlog_file12,
2632
- L: 112,
2633
- S: this,
2634
- A: [
2635
- "signal.payload.data",
2636
- "'Signal message must contain signal data.'"
2637
- ]
2638
- });
3210
+ (0, import_tiny_invariant.default)(signal.payload.data, "Signal message must contain signal data.");
2639
3211
  this._peer.signal(signal.payload.data);
2640
3212
  }
2641
3213
  async _disconnectStreams() {
2642
3214
  this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2643
3215
  }
2644
3216
  };
2645
- var createWebRTCTransportFactory = (webrtcConfig) => ({
2646
- createTransport: (params) => new WebRTCTransport({
3217
+ var createSimplePeerTransportFactory = (webrtcConfig) => ({
3218
+ createTransport: (params) => new SimplePeerTransport({
2647
3219
  ...params,
2648
3220
  webrtcConfig
2649
3221
  })
2650
3222
  });
2651
3223
 
2652
- // packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
3224
+ // packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts
2653
3225
  var import_node_stream2 = require("node:stream");
2654
3226
  var import_codec_protobuf = require("@dxos/codec-protobuf");
2655
- var import_invariant12 = require("@dxos/invariant");
3227
+ var import_invariant11 = require("@dxos/invariant");
2656
3228
  var import_keys11 = require("@dxos/keys");
2657
3229
  var import_log12 = require("@dxos/log");
2658
3230
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
2659
3231
  var import_util8 = require("@dxos/util");
2660
- var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2661
- var WebRTCTransportService = class {
3232
+ var __dxlog_file13 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3233
+ var SimplePeerTransportService = class {
2662
3234
  // prettier-ignore
2663
3235
  constructor(_webrtcConfig) {
2664
3236
  this._webrtcConfig = _webrtcConfig;
@@ -2685,7 +3257,7 @@ var WebRTCTransportService = class {
2685
3257
  callback();
2686
3258
  }
2687
3259
  });
2688
- const transport = new WebRTCTransport({
3260
+ const transport = new SimplePeerTransport({
2689
3261
  initiator: request.initiator,
2690
3262
  stream: duplex,
2691
3263
  webrtcConfig: this._webrtcConfig,
@@ -2737,7 +3309,7 @@ var WebRTCTransportService = class {
2737
3309
  return rpcStream;
2738
3310
  }
2739
3311
  async sendSignal({ proxyId, signal }) {
2740
- (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
3312
+ (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
2741
3313
  F: __dxlog_file13,
2742
3314
  L: 113,
2743
3315
  S: this,
@@ -2749,7 +3321,7 @@ var WebRTCTransportService = class {
2749
3321
  await this.transports.get(proxyId).transport.signal(signal);
2750
3322
  }
2751
3323
  async sendData({ proxyId, payload }) {
2752
- (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
3324
+ (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
2753
3325
  F: __dxlog_file13,
2754
3326
  L: 118,
2755
3327
  S: this,
@@ -2779,18 +3351,18 @@ var WebRTCTransportService = class {
2779
3351
  }
2780
3352
  };
2781
3353
 
2782
- // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
3354
+ // packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts
2783
3355
  var import_node_stream3 = require("node:stream");
2784
3356
  var import_async10 = require("@dxos/async");
2785
3357
  var import_context6 = require("@dxos/context");
2786
3358
  var import_debug6 = require("@dxos/debug");
2787
- var import_invariant13 = require("@dxos/invariant");
3359
+ var import_invariant12 = require("@dxos/invariant");
2788
3360
  var import_keys12 = require("@dxos/keys");
2789
3361
  var import_log13 = require("@dxos/log");
2790
3362
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
2791
3363
  var import_util9 = require("@dxos/util");
2792
- var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2793
- var WebRTCTransportProxy = class {
3364
+ var __dxlog_file14 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3365
+ var SimplePeerTransportProxy = class {
2794
3366
  // prettier-ignore
2795
3367
  constructor(_params) {
2796
3368
  this._params = _params;
@@ -2806,7 +3378,7 @@ var WebRTCTransportProxy = class {
2806
3378
  });
2807
3379
  this._serviceStream.waitUntilReady().then(() => {
2808
3380
  this._serviceStream.subscribe(async (event) => {
2809
- (0, import_log13.log)("WebRTCTransportProxy: event", event, {
3381
+ (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
2810
3382
  F: __dxlog_file14,
2811
3383
  L: 51,
2812
3384
  S: this,
@@ -2901,7 +3473,7 @@ var WebRTCTransportProxy = class {
2901
3473
  this._closed = true;
2902
3474
  }
2903
3475
  };
2904
- var WebRTCTransportProxyFactory = class {
3476
+ var SimplePeerTransportProxyFactory = class {
2905
3477
  constructor() {
2906
3478
  this._connections = /* @__PURE__ */ new Set();
2907
3479
  }
@@ -2917,16 +3489,16 @@ var WebRTCTransportProxyFactory = class {
2917
3489
  return this;
2918
3490
  }
2919
3491
  createTransport(options) {
2920
- (0, import_invariant13.invariant)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections", {
3492
+ (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
2921
3493
  F: __dxlog_file14,
2922
3494
  L: 163,
2923
3495
  S: this,
2924
3496
  A: [
2925
3497
  "this._bridgeService",
2926
- "'WebRTCTransportProxyFactory is not ready to open connections'"
3498
+ "'SimplePeerTransportProxyFactory is not ready to open connections'"
2927
3499
  ]
2928
3500
  });
2929
- const transport = new WebRTCTransportProxy({
3501
+ const transport = new SimplePeerTransportProxy({
2930
3502
  ...options,
2931
3503
  bridgeService: this._bridgeService
2932
3504
  });
@@ -2936,6 +3508,348 @@ var WebRTCTransportProxyFactory = class {
2936
3508
  }
2937
3509
  };
2938
3510
 
3511
+ // packages/core/mesh/network-manager/src/transport/index.ts
3512
+ init_datachannel();
3513
+
3514
+ // packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts
3515
+ var import_stream = require("stream");
3516
+ var import_async11 = require("@dxos/async");
3517
+ var import_debug7 = require("@dxos/debug");
3518
+ var import_log14 = require("@dxos/log");
3519
+ function _ts_decorate5(decorators, target, key, desc) {
3520
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3521
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
3522
+ r = Reflect.decorate(decorators, target, key, desc);
3523
+ else
3524
+ for (var i = decorators.length - 1; i >= 0; i--)
3525
+ if (d = decorators[i])
3526
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3527
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
3528
+ }
3529
+ var __dxlog_file15 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts";
3530
+ var DATACHANNEL_LABEL = "dxos.mesh.transport";
3531
+ var MAX_BUFFERED_AMOUNT = 64 * 1024;
3532
+ var MAX_MESSAGE_SIZE = 64 * 1024;
3533
+ var LibDataChannelTransport = class {
3534
+ constructor(params) {
3535
+ this.params = params;
3536
+ this._closed = false;
3537
+ this.closed = new import_async11.Event();
3538
+ this._connected = false;
3539
+ this.connected = new import_async11.Event();
3540
+ this.errors = new import_debug7.ErrorStream();
3541
+ this._readyForCandidates = new import_async11.Trigger();
3542
+ this._writeCallback = null;
3543
+ this._peer = (async () => {
3544
+ const { RTCPeerConnection: PeerConnection2 } = await Promise.resolve().then(() => (init_datachannel(), datachannel_exports));
3545
+ if (this._closed) {
3546
+ this.errors.raise(new Error("connection already closed"));
3547
+ }
3548
+ const peer = new PeerConnection2(params.webrtcConfig);
3549
+ peer.onicecandidateerror = (event) => {
3550
+ import_log14.log.error("peer.onicecandidateerror", {
3551
+ event
3552
+ }, {
3553
+ F: __dxlog_file15,
3554
+ L: 53,
3555
+ S: this,
3556
+ C: (f, a) => f(...a)
3557
+ });
3558
+ };
3559
+ peer.onconnectionstatechange = (event) => {
3560
+ import_log14.log.debug("peer.onconnectionstatechange", {
3561
+ event,
3562
+ peerConnectionState: peer.connectionState,
3563
+ transportConnectionState: this._connected
3564
+ }, {
3565
+ F: __dxlog_file15,
3566
+ L: 57,
3567
+ S: this,
3568
+ C: (f, a) => f(...a)
3569
+ });
3570
+ };
3571
+ peer.onicecandidate = async (event) => {
3572
+ import_log14.log.debug("peer.onicecandidate", {
3573
+ event
3574
+ }, {
3575
+ F: __dxlog_file15,
3576
+ L: 66,
3577
+ S: this,
3578
+ C: (f, a) => f(...a)
3579
+ });
3580
+ if (event.candidate) {
3581
+ try {
3582
+ await params.sendSignal({
3583
+ payload: {
3584
+ data: {
3585
+ type: "candidate",
3586
+ candidate: {
3587
+ candidate: event.candidate.candidate,
3588
+ // these fields never seem to be not null, but connecting to Chrome doesn't work if they are
3589
+ sdpMLineIndex: event.candidate.sdpMLineIndex ?? 0,
3590
+ sdpMid: event.candidate.sdpMid ?? 0
3591
+ }
3592
+ }
3593
+ }
3594
+ });
3595
+ } catch (err) {
3596
+ import_log14.log.warn("signaling errror", {
3597
+ err
3598
+ }, {
3599
+ F: __dxlog_file15,
3600
+ L: 83,
3601
+ S: this,
3602
+ C: (f, a) => f(...a)
3603
+ });
3604
+ }
3605
+ }
3606
+ };
3607
+ if (params.initiator) {
3608
+ peer.createOffer().then(async (offer) => {
3609
+ if (peer.connectionState !== "connecting") {
3610
+ import_log14.log.error("i am initiator but peer not in state connecting", {
3611
+ peer
3612
+ }, {
3613
+ F: __dxlog_file15,
3614
+ L: 92,
3615
+ S: this,
3616
+ C: (f, a) => f(...a)
3617
+ });
3618
+ this.errors.raise(new Error("invalid state: peer is initiator, but other peer not in state connecting"));
3619
+ }
3620
+ import_log14.log.debug(`im the initiator, creating offer, peer is in state ${peer.connectionState}`, {
3621
+ offer
3622
+ }, {
3623
+ F: __dxlog_file15,
3624
+ L: 95,
3625
+ S: this,
3626
+ C: (f, a) => f(...a)
3627
+ });
3628
+ await peer.setLocalDescription(offer);
3629
+ await params.sendSignal({
3630
+ payload: {
3631
+ data: {
3632
+ type: offer.type,
3633
+ sdp: offer.sdp
3634
+ }
3635
+ }
3636
+ });
3637
+ return offer;
3638
+ }).catch((err) => {
3639
+ this.errors.raise(err);
3640
+ });
3641
+ this.handleChannel(peer.createDataChannel(DATACHANNEL_LABEL));
3642
+ import_log14.log.debug("created data channel", void 0, {
3643
+ F: __dxlog_file15,
3644
+ L: 104,
3645
+ S: this,
3646
+ C: (f, a) => f(...a)
3647
+ });
3648
+ peer.ondatachannel = (event) => {
3649
+ this.errors.raise(new Error("got ondatachannel when i am the initiator?"));
3650
+ };
3651
+ } else {
3652
+ peer.ondatachannel = (event) => {
3653
+ import_log14.log.debug("peer.ondatachannel (non-initiator)", {
3654
+ event
3655
+ }, {
3656
+ F: __dxlog_file15,
3657
+ L: 110,
3658
+ S: this,
3659
+ C: (f, a) => f(...a)
3660
+ });
3661
+ if (event.channel.label !== DATACHANNEL_LABEL) {
3662
+ this.errors.raise(new Error(`unexpected channel label ${event.channel.label}`));
3663
+ }
3664
+ this.handleChannel(event.channel);
3665
+ };
3666
+ }
3667
+ return peer;
3668
+ })();
3669
+ }
3670
+ handleChannel(dataChannel) {
3671
+ this._channel = dataChannel;
3672
+ this._channel.onopen = () => {
3673
+ import_log14.log.debug("dataChannel.onopen", void 0, {
3674
+ F: __dxlog_file15,
3675
+ L: 126,
3676
+ S: this,
3677
+ C: (f, a) => f(...a)
3678
+ });
3679
+ const duplex = new import_stream.Duplex({
3680
+ read: () => {
3681
+ },
3682
+ write: (chunk, encoding, callback) => {
3683
+ if (chunk.length > MAX_MESSAGE_SIZE) {
3684
+ this.errors.raise(new Error(`message too large: ${chunk.length} > ${MAX_MESSAGE_SIZE}`));
3685
+ }
3686
+ dataChannel.send(chunk);
3687
+ if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
3688
+ if (this._writeCallback !== null) {
3689
+ import_log14.log.error("consumer trying to write before we're ready for more data", void 0, {
3690
+ F: __dxlog_file15,
3691
+ L: 139,
3692
+ S: this,
3693
+ C: (f, a) => f(...a)
3694
+ });
3695
+ }
3696
+ this._writeCallback = callback;
3697
+ } else {
3698
+ callback();
3699
+ }
3700
+ }
3701
+ });
3702
+ duplex.pipe(this.params.stream).pipe(duplex);
3703
+ this._stream = duplex;
3704
+ this._connected = true;
3705
+ this.connected.emit();
3706
+ };
3707
+ this._channel.onerror = async (err) => {
3708
+ this.errors.raise(new Error("channel error: " + err.toString()));
3709
+ await this._close();
3710
+ };
3711
+ this._channel.onclose = async (err) => {
3712
+ import_log14.log.info("channel onclose", {
3713
+ err
3714
+ }, {
3715
+ F: __dxlog_file15,
3716
+ L: 159,
3717
+ S: this,
3718
+ C: (f, a) => f(...a)
3719
+ });
3720
+ await this._close();
3721
+ };
3722
+ this._channel.onmessage = (event) => {
3723
+ let data = event.data;
3724
+ if (data instanceof ArrayBuffer) {
3725
+ data = Buffer.from(data);
3726
+ }
3727
+ this._stream.push(data);
3728
+ };
3729
+ this._channel.onbufferedamountlow = () => {
3730
+ const cb = this._writeCallback;
3731
+ this._writeCallback = null;
3732
+ cb?.();
3733
+ };
3734
+ }
3735
+ async _close() {
3736
+ if (this._closed) {
3737
+ return;
3738
+ }
3739
+ await this._disconnectStreams();
3740
+ this._closed = true;
3741
+ this.closed.emit();
3742
+ }
3743
+ signal(signal) {
3744
+ this._peer.then(async (peer) => {
3745
+ const data = signal.payload.data;
3746
+ switch (data.type) {
3747
+ case "offer": {
3748
+ if ((await this._peer).connectionState !== "new") {
3749
+ import_log14.log.error("received offer but peer not in state new", {
3750
+ peer
3751
+ }, {
3752
+ F: __dxlog_file15,
3753
+ L: 194,
3754
+ S: this,
3755
+ C: (f, a) => f(...a)
3756
+ });
3757
+ this.errors.raise(new Error("invalid signalling state: received offer when peer is not in state new"));
3758
+ break;
3759
+ }
3760
+ try {
3761
+ await peer.setRemoteDescription({
3762
+ type: data.type,
3763
+ sdp: data.sdp
3764
+ });
3765
+ const answer = await peer.createAnswer();
3766
+ await peer.setLocalDescription(answer);
3767
+ await this.params.sendSignal({
3768
+ payload: {
3769
+ data: {
3770
+ type: answer.type,
3771
+ sdp: answer.sdp
3772
+ }
3773
+ }
3774
+ });
3775
+ this._readyForCandidates.wake();
3776
+ } catch (err) {
3777
+ import_log14.log.error("can't handle offer from signalling server", {
3778
+ err
3779
+ }, {
3780
+ F: __dxlog_file15,
3781
+ L: 205,
3782
+ S: this,
3783
+ C: (f, a) => f(...a)
3784
+ });
3785
+ this.errors.raise(new Error("error handling offer"));
3786
+ }
3787
+ break;
3788
+ }
3789
+ case "answer":
3790
+ try {
3791
+ await peer.setRemoteDescription({
3792
+ type: data.type,
3793
+ sdp: data.sdp
3794
+ });
3795
+ this._readyForCandidates.wake();
3796
+ } catch (err) {
3797
+ import_log14.log.error("can't handle answer from signalling server", {
3798
+ err
3799
+ }, {
3800
+ F: __dxlog_file15,
3801
+ L: 216,
3802
+ S: this,
3803
+ C: (f, a) => f(...a)
3804
+ });
3805
+ this.errors.raise(new Error("error handling answer"));
3806
+ }
3807
+ break;
3808
+ case "candidate":
3809
+ await this._readyForCandidates.wait();
3810
+ await peer.addIceCandidate({
3811
+ candidate: data.candidate.candidate
3812
+ });
3813
+ break;
3814
+ default:
3815
+ import_log14.log.error("unhandled signal type", {
3816
+ type: data.type,
3817
+ signal
3818
+ }, {
3819
+ F: __dxlog_file15,
3820
+ L: 227,
3821
+ S: this,
3822
+ C: (f, a) => f(...a)
3823
+ });
3824
+ this.errors.raise(new Error(`unhandled signal type ${data.type}`));
3825
+ }
3826
+ }).catch((err) => {
3827
+ import_log14.log.catch(err, void 0, {
3828
+ F: __dxlog_file15,
3829
+ L: 232,
3830
+ S: this,
3831
+ C: (f, a) => f(...a)
3832
+ });
3833
+ });
3834
+ }
3835
+ // TODO(nf): add classmethod to call node-datachannel.cleanup() when all instances have been destroyed?
3836
+ async destroy() {
3837
+ await this._close();
3838
+ }
3839
+ async _disconnectStreams() {
3840
+ this.params.stream.unpipe?.(this._stream)?.unpipe?.(this.params.stream);
3841
+ }
3842
+ };
3843
+ _ts_decorate5([
3844
+ import_async11.synchronized
3845
+ ], LibDataChannelTransport.prototype, "_close", null);
3846
+ var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3847
+ createTransport: (params) => new LibDataChannelTransport({
3848
+ ...params,
3849
+ webrtcConfig
3850
+ })
3851
+ });
3852
+
2939
3853
  // packages/core/mesh/network-manager/src/wire-protocol.ts
2940
3854
  var import_teleport = require("@dxos/teleport");
2941
3855
  var createTeleportProtocolFactory = (onConnection) => {
@@ -2943,12 +3857,15 @@ var createTeleportProtocolFactory = (onConnection) => {
2943
3857
  const teleport = new import_teleport.Teleport(params);
2944
3858
  return {
2945
3859
  stream: teleport.stream,
2946
- initialize: async () => {
3860
+ open: async () => {
2947
3861
  await teleport.open();
2948
3862
  await onConnection(teleport);
2949
3863
  },
2950
- destroy: async () => {
3864
+ close: async () => {
2951
3865
  await teleport.close();
3866
+ },
3867
+ abort: async () => {
3868
+ await teleport.abort();
2952
3869
  }
2953
3870
  };
2954
3871
  };
@@ -2961,20 +3878,27 @@ var createTeleportProtocolFactory = (onConnection) => {
2961
3878
  ConnectionState,
2962
3879
  EventType,
2963
3880
  FullyConnectedTopology,
3881
+ LibDataChannelTransport,
2964
3882
  MAX_CONCURRENT_INITIATING_CONNECTIONS,
2965
3883
  MMSTTopology,
2966
3884
  MemoryTransport,
2967
3885
  MemoryTransportFactory,
2968
3886
  NetworkManager,
3887
+ RTCIceCandidate,
3888
+ RTCPeerConnection,
3889
+ RTCSessionDescription,
3890
+ SimplePeerTransport,
3891
+ SimplePeerTransportProxy,
3892
+ SimplePeerTransportProxyFactory,
3893
+ SimplePeerTransportService,
2969
3894
  StarTopology,
2970
3895
  Swarm,
2971
3896
  SwarmMapper,
2972
3897
  SwarmMessenger,
2973
- WebRTCTransport,
2974
- WebRTCTransportProxy,
2975
- WebRTCTransportProxyFactory,
2976
- WebRTCTransportService,
2977
- createTeleportProtocolFactory,
2978
- createWebRTCTransportFactory
3898
+ TransportKind,
3899
+ cleanup,
3900
+ createLibDataChannelTransportFactory,
3901
+ createSimplePeerTransportFactory,
3902
+ createTeleportProtocolFactory
2979
3903
  });
2980
3904
  //# sourceMappingURL=index.cjs.map