@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/testing/index.ts
31
505
  var testing_exports = {};
32
506
  __export(testing_exports, {
@@ -39,6 +513,7 @@ module.exports = __toCommonJS(testing_exports);
39
513
 
40
514
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
41
515
  var import_keys14 = require("@dxos/keys");
516
+ var import_log16 = require("@dxos/log");
42
517
  var import_messaging2 = require("@dxos/messaging");
43
518
  var import_protocols6 = require("@dxos/protocols");
44
519
  var import_services2 = require("@dxos/protocols/proto/dxos/client/services");
@@ -80,11 +555,17 @@ function _ts_decorate(decorators, target, key, desc) {
80
555
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
81
556
  return c > 3 && r && Object.defineProperty(target, key, r), r;
82
557
  }
83
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
558
+ var __dxlog_file = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/connection.ts";
84
559
  var STARTING_SIGNALLING_DELAY = 10;
85
560
  var MAX_SIGNALLING_DELAY = 300;
86
561
  var ConnectionState;
87
562
  (function(ConnectionState6) {
563
+ ConnectionState6[
564
+ /**
565
+ * Connection is created, but not yet passed through the connection limiter.
566
+ */
567
+ "CREATED"
568
+ ] = "CREATED";
88
569
  ConnectionState6[
89
570
  /**
90
571
  * Initial state. Connection is registered but no attempt to connect to the remote peer has been performed.
@@ -129,7 +610,7 @@ var Connection = class {
129
610
  this._transportFactory = _transportFactory;
130
611
  this._callbacks = _callbacks;
131
612
  this._ctx = new import_context.Context();
132
- this._state = ConnectionState.INITIAL;
613
+ this._state = ConnectionState.CREATED;
133
614
  this._incomingSignalBuffer = [];
134
615
  this._outgoingSignalBuffer = [];
135
616
  this.stateChanged = new import_async.Event();
@@ -147,7 +628,7 @@ var Connection = class {
147
628
  initiator: this.initiator
148
629
  }, {
149
630
  F: __dxlog_file,
150
- L: 109,
631
+ L: 114,
151
632
  S: this,
152
633
  C: (f, a) => f(...a)
153
634
  });
@@ -167,7 +648,7 @@ var Connection = class {
167
648
  async openConnection() {
168
649
  (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
169
650
  F: __dxlog_file,
170
- L: 134,
651
+ L: 139,
171
652
  S: this,
172
653
  A: [
173
654
  "this._state === ConnectionState.INITIAL",
@@ -178,7 +659,7 @@ var Connection = class {
178
659
  id: this._instanceId
179
660
  }), {
180
661
  F: __dxlog_file,
181
- L: 135,
662
+ L: 140,
182
663
  S: this,
183
664
  C: (f, a) => f(...a)
184
665
  });
@@ -190,18 +671,18 @@ var Connection = class {
190
671
  initiator: this.initiator
191
672
  }, {
192
673
  F: __dxlog_file,
193
- L: 136,
674
+ L: 141,
194
675
  S: this,
195
676
  C: (f, a) => f(...a)
196
677
  });
197
678
  this._changeState(ConnectionState.CONNECTING);
198
- this._protocol.initialize().catch((err) => {
679
+ this._protocol.open().catch((err) => {
199
680
  this.errors.raise(err);
200
681
  });
201
682
  this._protocol.stream.on("close", () => {
202
683
  (0, import_log.log)("protocol stream closed", void 0, {
203
684
  F: __dxlog_file,
204
- L: 153,
685
+ L: 158,
205
686
  S: this,
206
687
  C: (f, a) => f(...a)
207
688
  });
@@ -209,7 +690,7 @@ var Connection = class {
209
690
  });
210
691
  (0, import_invariant.invariant)(!this._transport, void 0, {
211
692
  F: __dxlog_file,
212
- L: 157,
693
+ L: 162,
213
694
  S: this,
214
695
  A: [
215
696
  "!this._transport",
@@ -227,12 +708,35 @@ var Connection = class {
227
708
  });
228
709
  this._transport.closed.once(() => {
229
710
  this._transport = void 0;
230
- this.close().catch((err) => this.errors.raise(err));
711
+ (0, import_log.log)("abort triggered by transport close", void 0, {
712
+ F: __dxlog_file,
713
+ L: 176,
714
+ S: this,
715
+ C: (f, a) => f(...a)
716
+ });
717
+ this.abort().catch((err) => this.errors.raise(err));
231
718
  });
232
719
  this._transport.errors.handle((err) => {
720
+ (0, import_log.log)("transport error:", {
721
+ err
722
+ }, {
723
+ F: __dxlog_file,
724
+ L: 181,
725
+ S: this,
726
+ C: (f, a) => f(...a)
727
+ });
233
728
  if (!this.closeReason) {
234
729
  this.closeReason = err?.message;
235
730
  }
731
+ if (err?.message.includes("OperationError")) {
732
+ (0, import_log.log)("aborting due to transport OperationError", void 0, {
733
+ F: __dxlog_file,
734
+ L: 187,
735
+ S: this,
736
+ C: (f, a) => f(...a)
737
+ });
738
+ this.abort().catch((err2) => this.errors.raise(err2));
739
+ }
236
740
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
237
741
  this.errors.raise(err);
238
742
  }
@@ -245,10 +749,64 @@ var Connection = class {
245
749
  id: this._instanceId
246
750
  }), {
247
751
  F: __dxlog_file,
248
- L: 190,
752
+ L: 203,
753
+ S: this,
754
+ C: (f, a) => f(...a)
755
+ });
756
+ }
757
+ async abort(err) {
758
+ (0, import_log.log)("aborting...", {
759
+ err
760
+ }, {
761
+ F: __dxlog_file,
762
+ L: 209,
249
763
  S: this,
250
764
  C: (f, a) => f(...a)
251
765
  });
766
+ if (!this.closeReason) {
767
+ this.closeReason = err?.message;
768
+ }
769
+ if (this._state === ConnectionState.CLOSED) {
770
+ (0, import_log.log)("abort ignored: already closed", this.closeReason, {
771
+ F: __dxlog_file,
772
+ L: 214,
773
+ S: this,
774
+ C: (f, a) => f(...a)
775
+ });
776
+ return;
777
+ }
778
+ this._changeState(ConnectionState.CLOSING);
779
+ await this._ctx.dispose();
780
+ try {
781
+ (0, import_log.log)("aborting protocol... ", {
782
+ proto: this._protocol
783
+ }, {
784
+ F: __dxlog_file,
785
+ L: 224,
786
+ S: this,
787
+ C: (f, a) => f(...a)
788
+ });
789
+ await this._protocol.abort();
790
+ } catch (err2) {
791
+ import_log.log.catch(err2, void 0, {
792
+ F: __dxlog_file,
793
+ L: 227,
794
+ S: this,
795
+ C: (f, a) => f(...a)
796
+ });
797
+ }
798
+ try {
799
+ await this._transport?.destroy();
800
+ } catch (err2) {
801
+ import_log.log.catch(err2, void 0, {
802
+ F: __dxlog_file,
803
+ L: 234,
804
+ S: this,
805
+ C: (f, a) => f(...a)
806
+ });
807
+ }
808
+ this._changeState(ConnectionState.CLOSED);
809
+ this._callbacks?.onClosed?.(err);
252
810
  }
253
811
  async close(err) {
254
812
  if (!this.closeReason) {
@@ -263,16 +821,16 @@ var Connection = class {
263
821
  peerId: this.ownId
264
822
  }, {
265
823
  F: __dxlog_file,
266
- L: 205,
824
+ L: 253,
267
825
  S: this,
268
826
  C: (f, a) => f(...a)
269
827
  });
270
828
  try {
271
- await this._protocol.destroy();
829
+ await this._protocol.close();
272
830
  } catch (err2) {
273
831
  import_log.log.catch(err2, void 0, {
274
832
  F: __dxlog_file,
275
- L: 211,
833
+ L: 259,
276
834
  S: this,
277
835
  C: (f, a) => f(...a)
278
836
  });
@@ -282,7 +840,7 @@ var Connection = class {
282
840
  } catch (err2) {
283
841
  import_log.log.catch(err2, void 0, {
284
842
  F: __dxlog_file,
285
- L: 218,
843
+ L: 266,
286
844
  S: this,
287
845
  C: (f, a) => f(...a)
288
846
  });
@@ -291,7 +849,7 @@ var Connection = class {
291
849
  peerId: this.ownId
292
850
  }, {
293
851
  F: __dxlog_file,
294
- L: 221,
852
+ L: 269,
295
853
  S: this,
296
854
  C: (f, a) => f(...a)
297
855
  });
@@ -330,11 +888,11 @@ var Connection = class {
330
888
  if (err instanceof import_errors.CancelledError) {
331
889
  return;
332
890
  }
333
- import_log.log.warn("Signal failed", {
891
+ import_log.log.warn("signal failed", {
334
892
  err
335
893
  }, {
336
894
  F: __dxlog_file,
337
- L: 258,
895
+ L: 306,
338
896
  S: this,
339
897
  C: (f, a) => f(...a)
340
898
  });
@@ -347,7 +905,7 @@ var Connection = class {
347
905
  async signal(msg) {
348
906
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
349
907
  F: __dxlog_file,
350
- L: 267,
908
+ L: 315,
351
909
  S: this,
352
910
  A: [
353
911
  "msg.sessionId",
@@ -357,7 +915,7 @@ var Connection = class {
357
915
  if (!msg.sessionId.equals(this.sessionId)) {
358
916
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
359
917
  F: __dxlog_file,
360
- L: 269,
918
+ L: 317,
361
919
  S: this,
362
920
  C: (f, a) => f(...a)
363
921
  });
@@ -365,7 +923,7 @@ var Connection = class {
365
923
  }
366
924
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
367
925
  F: __dxlog_file,
368
- L: 272,
926
+ L: 320,
369
927
  S: this,
370
928
  A: [
371
929
  "msg.data.signal || msg.data.signalBatch",
@@ -374,7 +932,7 @@ var Connection = class {
374
932
  });
375
933
  (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
376
934
  F: __dxlog_file,
377
- L: 273,
935
+ L: 321,
378
936
  S: this,
379
937
  A: [
380
938
  "msg.author?.equals(this.remoteId)",
@@ -383,7 +941,7 @@ var Connection = class {
383
941
  });
384
942
  (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
385
943
  F: __dxlog_file,
386
- L: 274,
944
+ L: 322,
387
945
  S: this,
388
946
  A: [
389
947
  "msg.recipient?.equals(this.ownId)",
@@ -397,14 +955,17 @@ var Connection = class {
397
955
  if (!signal) {
398
956
  continue;
399
957
  }
400
- if (this._state === ConnectionState.INITIAL) {
958
+ if ([
959
+ ConnectionState.CREATED,
960
+ ConnectionState.INITIAL
961
+ ].includes(this.state)) {
401
962
  (0, import_log.log)("buffered signal", {
402
963
  peerId: this.ownId,
403
964
  remoteId: this.remoteId,
404
965
  msg: msg.data
405
966
  }, {
406
967
  F: __dxlog_file,
407
- L: 283,
968
+ L: 331,
408
969
  S: this,
409
970
  C: (f, a) => f(...a)
410
971
  });
@@ -412,7 +973,7 @@ var Connection = class {
412
973
  } else {
413
974
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
414
975
  F: __dxlog_file,
415
- L: 286,
976
+ L: 334,
416
977
  S: this,
417
978
  A: [
418
979
  "this._transport",
@@ -425,7 +986,7 @@ var Connection = class {
425
986
  msg: msg.data
426
987
  }, {
427
988
  F: __dxlog_file,
428
- L: 287,
989
+ L: 335,
429
990
  S: this,
430
991
  C: (f, a) => f(...a)
431
992
  });
@@ -433,6 +994,9 @@ var Connection = class {
433
994
  }
434
995
  }
435
996
  }
997
+ initiate() {
998
+ this._changeState(ConnectionState.INITIAL);
999
+ }
436
1000
  _changeState(state) {
437
1001
  (0, import_log.log)("stateChanged", {
438
1002
  from: this._state,
@@ -440,13 +1004,13 @@ var Connection = class {
440
1004
  peerId: this.ownId
441
1005
  }, {
442
1006
  F: __dxlog_file,
443
- L: 294,
1007
+ L: 346,
444
1008
  S: this,
445
1009
  C: (f, a) => f(...a)
446
1010
  });
447
1011
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
448
1012
  F: __dxlog_file,
449
- L: 295,
1013
+ L: 347,
450
1014
  S: this,
451
1015
  A: [
452
1016
  "state !== this._state",
@@ -457,6 +1021,9 @@ var Connection = class {
457
1021
  this.stateChanged.emit(state);
458
1022
  }
459
1023
  };
1024
+ _ts_decorate([
1025
+ import_async.synchronized
1026
+ ], Connection.prototype, "abort", null);
460
1027
  _ts_decorate([
461
1028
  import_async.synchronized
462
1029
  ], Connection.prototype, "close", null);
@@ -478,7 +1045,7 @@ var import_keys2 = require("@dxos/keys");
478
1045
  var import_log2 = require("@dxos/log");
479
1046
  var import_protocols2 = require("@dxos/protocols");
480
1047
  var import_util = require("@dxos/util");
481
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1048
+ var __dxlog_file2 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
482
1049
  var SwarmMessage = import_protocols2.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
483
1050
  var SwarmMessenger = class {
484
1051
  constructor({ sendMessage, onSignal, onOffer, topic }) {
@@ -720,7 +1287,7 @@ function _ts_decorate2(decorators, target, key, desc) {
720
1287
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
721
1288
  return c > 3 && r && Object.defineProperty(target, key, r), r;
722
1289
  }
723
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1290
+ var __dxlog_file3 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/peer.ts";
724
1291
  var ConnectionDisplacedError = class extends import_errors2.SystemError {
725
1292
  constructor() {
726
1293
  super("Connection displaced by remote initiator.");
@@ -759,7 +1326,7 @@ var Peer = class {
759
1326
  sessionId: this.connection?.sessionId
760
1327
  }, {
761
1328
  F: __dxlog_file3,
762
- L: 116,
1329
+ L: 117,
763
1330
  S: this,
764
1331
  C: (f, a) => f(...a)
765
1332
  });
@@ -776,7 +1343,7 @@ var Peer = class {
776
1343
  if (!this.connection) {
777
1344
  (0, import_invariant3.invariant)(message.sessionId, void 0, {
778
1345
  F: __dxlog_file3,
779
- L: 136,
1346
+ L: 137,
780
1347
  S: this,
781
1348
  A: [
782
1349
  "message.sessionId",
@@ -786,6 +1353,7 @@ var Peer = class {
786
1353
  const connection = this._createConnection(false, message.sessionId);
787
1354
  try {
788
1355
  await this._connectionLimiter.connecting(message.sessionId);
1356
+ connection.initiate();
789
1357
  await connection.openConnection();
790
1358
  } catch (err) {
791
1359
  if (!(err instanceof import_errors2.CancelledError)) {
@@ -796,7 +1364,7 @@ var Peer = class {
796
1364
  err
797
1365
  }, {
798
1366
  F: __dxlog_file3,
799
- L: 144,
1367
+ L: 147,
800
1368
  S: this,
801
1369
  C: (f, a) => f(...a)
802
1370
  });
@@ -818,7 +1386,7 @@ var Peer = class {
818
1386
  async initiateConnection() {
819
1387
  (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
820
1388
  F: __dxlog_file3,
821
- L: 161,
1389
+ L: 164,
822
1390
  S: this,
823
1391
  A: [
824
1392
  "!this.initiating",
@@ -827,7 +1395,7 @@ var Peer = class {
827
1395
  });
828
1396
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
829
1397
  F: __dxlog_file3,
830
- L: 162,
1398
+ L: 165,
831
1399
  S: this,
832
1400
  A: [
833
1401
  "!this.connection",
@@ -842,7 +1410,7 @@ var Peer = class {
842
1410
  sessionId
843
1411
  }, {
844
1412
  F: __dxlog_file3,
845
- L: 164,
1413
+ L: 167,
846
1414
  S: this,
847
1415
  C: (f, a) => f(...a)
848
1416
  });
@@ -850,6 +1418,7 @@ var Peer = class {
850
1418
  this.initiating = true;
851
1419
  try {
852
1420
  await this._connectionLimiter.connecting(sessionId);
1421
+ connection.initiate();
853
1422
  const answer = await this._signalMessaging.offer({
854
1423
  author: this.localPeerId,
855
1424
  recipient: this.id,
@@ -866,14 +1435,14 @@ var Peer = class {
866
1435
  remoteId: this.id
867
1436
  }, {
868
1437
  F: __dxlog_file3,
869
- L: 179,
1438
+ L: 183,
870
1439
  S: this,
871
1440
  C: (f, a) => f(...a)
872
1441
  });
873
1442
  if (connection.state !== ConnectionState.INITIAL) {
874
1443
  (0, import_log3.log)("ignoring response", void 0, {
875
1444
  F: __dxlog_file3,
876
- L: 181,
1445
+ L: 185,
877
1446
  S: this,
878
1447
  C: (f, a) => f(...a)
879
1448
  });
@@ -893,7 +1462,7 @@ var Peer = class {
893
1462
  remoteId: this.id
894
1463
  }, {
895
1464
  F: __dxlog_file3,
896
- L: 192,
1465
+ L: 196,
897
1466
  S: this,
898
1467
  C: (f, a) => f(...a)
899
1468
  });
@@ -916,13 +1485,13 @@ var Peer = class {
916
1485
  sessionId
917
1486
  }, {
918
1487
  F: __dxlog_file3,
919
- L: 206,
1488
+ L: 210,
920
1489
  S: this,
921
1490
  C: (f, a) => f(...a)
922
1491
  });
923
1492
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
924
1493
  F: __dxlog_file3,
925
- L: 213,
1494
+ L: 217,
926
1495
  S: this,
927
1496
  A: [
928
1497
  "!this.connection",
@@ -958,7 +1527,7 @@ var Peer = class {
958
1527
  initiator
959
1528
  }, {
960
1529
  F: __dxlog_file3,
961
- L: 232,
1530
+ L: 236,
962
1531
  S: this,
963
1532
  C: (f, a) => f(...a)
964
1533
  });
@@ -971,14 +1540,14 @@ var Peer = class {
971
1540
  initiator
972
1541
  }, {
973
1542
  F: __dxlog_file3,
974
- L: 241,
1543
+ L: 245,
975
1544
  S: this,
976
1545
  C: (f, a) => f(...a)
977
1546
  });
978
1547
  this._connectionLimiter.doneConnecting(sessionId);
979
1548
  (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
980
1549
  F: __dxlog_file3,
981
- L: 246,
1550
+ L: 250,
982
1551
  S: this,
983
1552
  A: [
984
1553
  "this.connection === connection",
@@ -993,7 +1562,7 @@ var Peer = class {
993
1562
  initiator
994
1563
  }, {
995
1564
  F: __dxlog_file3,
996
- L: 248,
1565
+ L: 252,
997
1566
  S: this,
998
1567
  C: (f, a) => f(...a)
999
1568
  });
@@ -1028,7 +1597,7 @@ var Peer = class {
1028
1597
  err
1029
1598
  }, {
1030
1599
  F: __dxlog_file3,
1031
- L: 288,
1600
+ L: 292,
1032
1601
  S: this,
1033
1602
  C: (f, a) => f(...a)
1034
1603
  });
@@ -1041,7 +1610,7 @@ var Peer = class {
1041
1610
  err
1042
1611
  }, {
1043
1612
  F: __dxlog_file3,
1044
- L: 289,
1613
+ L: 293,
1045
1614
  S: this,
1046
1615
  C: (f, a) => f(...a)
1047
1616
  });
@@ -1060,7 +1629,7 @@ var Peer = class {
1060
1629
  sessionId: connection.sessionId
1061
1630
  }, {
1062
1631
  F: __dxlog_file3,
1063
- L: 314,
1632
+ L: 318,
1064
1633
  S: this,
1065
1634
  C: (f, a) => f(...a)
1066
1635
  });
@@ -1070,7 +1639,7 @@ var Peer = class {
1070
1639
  sessionId: connection.sessionId
1071
1640
  }, {
1072
1641
  F: __dxlog_file3,
1073
- L: 320,
1642
+ L: 324,
1074
1643
  S: this,
1075
1644
  C: (f, a) => f(...a)
1076
1645
  });
@@ -1081,7 +1650,7 @@ var Peer = class {
1081
1650
  message
1082
1651
  }, {
1083
1652
  F: __dxlog_file3,
1084
- L: 325,
1653
+ L: 329,
1085
1654
  S: this,
1086
1655
  C: (f, a) => f(...a)
1087
1656
  });
@@ -1096,7 +1665,7 @@ var Peer = class {
1096
1665
  topic: this.topic
1097
1666
  }, {
1098
1667
  F: __dxlog_file3,
1099
- L: 335,
1668
+ L: 339,
1100
1669
  S: this,
1101
1670
  C: (f, a) => f(...a)
1102
1671
  });
@@ -1130,7 +1699,7 @@ function _ts_decorate3(decorators, target, key, desc) {
1130
1699
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1131
1700
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1132
1701
  }
1133
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1702
+ var __dxlog_file4 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1134
1703
  var INITIATION_DELAY = 100;
1135
1704
  var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
1136
1705
  var Swarm = class {
@@ -1604,7 +2173,7 @@ var import_async4 = require("@dxos/async");
1604
2173
  var import_keys5 = require("@dxos/keys");
1605
2174
  var import_log5 = require("@dxos/log");
1606
2175
  var import_util3 = require("@dxos/util");
1607
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
2176
+ var __dxlog_file5 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
1608
2177
  var SwarmMapper = class {
1609
2178
  get peers() {
1610
2179
  return Array.from(this._peers.values());
@@ -1677,7 +2246,7 @@ var import_invariant5 = require("@dxos/invariant");
1677
2246
  var import_keys6 = require("@dxos/keys");
1678
2247
  var import_log6 = require("@dxos/log");
1679
2248
  var import_util4 = require("@dxos/util");
1680
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2249
+ var __dxlog_file6 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1681
2250
  var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
1682
2251
  var ConnectionLimiter = class {
1683
2252
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
@@ -1786,7 +2355,7 @@ var ConnectionLog = class {
1786
2355
  this.update.emit();
1787
2356
  swarm.connectionAdded.on((connection) => {
1788
2357
  const connectionInfo = {
1789
- state: ConnectionState.INITIAL,
2358
+ state: ConnectionState.CREATED,
1790
2359
  closeReason: connection.closeReason,
1791
2360
  remotePeerId: connection.remoteId,
1792
2361
  sessionId: connection.sessionId,
@@ -1818,7 +2387,7 @@ var ConnectionLog = class {
1818
2387
  };
1819
2388
 
1820
2389
  // packages/core/mesh/network-manager/src/network-manager.ts
1821
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
2390
+ var __dxlog_file7 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/network-manager.ts";
1822
2391
  var NetworkManager = class {
1823
2392
  constructor({ transportFactory, signalManager, log: log1 }) {
1824
2393
  /**
@@ -2054,7 +2623,7 @@ var NetworkManager = class {
2054
2623
 
2055
2624
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
2056
2625
  var import_invariant7 = require("@dxos/invariant");
2057
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2626
+ var __dxlog_file8 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2058
2627
  var FullyConnectedTopology = class {
2059
2628
  toString() {
2060
2629
  return "FullyConnectedTopology";
@@ -2120,7 +2689,7 @@ function _ts_decorate4(decorators, target, key, desc) {
2120
2689
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2121
2690
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2122
2691
  }
2123
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2692
+ var __dxlog_file9 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2124
2693
  var MEMORY_TRANSPORT_DELAY = 1;
2125
2694
  var createStreamDelay = (delay) => {
2126
2695
  return new import_node_stream.Transform({
@@ -2288,11 +2857,20 @@ _ts_decorate4([
2288
2857
  import_log10.logInfo
2289
2858
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
2290
2859
 
2291
- // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2860
+ // packages/core/mesh/network-manager/src/transport/transport.ts
2861
+ var TransportKind;
2862
+ (function(TransportKind2) {
2863
+ TransportKind2["SIMPLE_PEER"] = "SIMPLE_PEER";
2864
+ TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
2865
+ TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
2866
+ TransportKind2["MEMORY"] = "MEMORY";
2867
+ })(TransportKind || (TransportKind = {}));
2868
+
2869
+ // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
2292
2870
  var import_simple_peer = __toESM(require("simple-peer"));
2871
+ var import_tiny_invariant = __toESM(require("tiny-invariant"));
2293
2872
  var import_async9 = require("@dxos/async");
2294
2873
  var import_debug5 = require("@dxos/debug");
2295
- var import_invariant11 = require("@dxos/invariant");
2296
2874
  var import_keys10 = require("@dxos/keys");
2297
2875
  var import_log11 = require("@dxos/log");
2298
2876
  var import_protocols5 = require("@dxos/protocols");
@@ -2304,44 +2882,40 @@ try {
2304
2882
  } catch {
2305
2883
  }
2306
2884
 
2307
- // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2308
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2309
- var WebRTCTransport = class {
2885
+ // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
2886
+ var __dxlog_file10 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
2887
+ var SimplePeerTransport = class {
2310
2888
  constructor(params) {
2311
2889
  this.params = params;
2312
- this._instanceId = import_keys10.PublicKey.random().toHex();
2313
2890
  this._closed = false;
2314
2891
  this.closed = new import_async9.Event();
2315
2892
  this.connected = new import_async9.Event();
2316
2893
  this.errors = new import_debug5.ErrorStream();
2894
+ this._instanceId = import_keys10.PublicKey.random().toHex();
2317
2895
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2318
2896
  id: this._instanceId
2319
2897
  }), {
2320
2898
  F: __dxlog_file10,
2321
- L: 44,
2899
+ L: 39,
2322
2900
  S: this,
2323
2901
  C: (f, a) => f(...a)
2324
2902
  });
2325
2903
  (0, import_log11.log)("created connection", params, {
2326
2904
  F: __dxlog_file10,
2327
- L: 45,
2905
+ L: 40,
2328
2906
  S: this,
2329
2907
  C: (f, a) => f(...a)
2330
2908
  });
2331
2909
  this._peer = new import_simple_peer.default({
2910
+ channelName: "dxos.mesh.transport",
2332
2911
  initiator: this.params.initiator,
2333
- config: this.params.webrtcConfig,
2334
- // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel
2335
- channelConfig: {
2336
- protocol: "dxos.mesh"
2337
- },
2338
- // Custom WebRTC implementation (for Node); otherwise get-browser-rtc.
2339
- wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available"))
2912
+ wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available")),
2913
+ config: this.params.webrtcConfig
2340
2914
  });
2341
2915
  this._peer.on("signal", async (data) => {
2342
2916
  (0, import_log11.log)("signal", data, {
2343
2917
  F: __dxlog_file10,
2344
- L: 62,
2918
+ L: 49,
2345
2919
  S: this,
2346
2920
  C: (f, a) => f(...a)
2347
2921
  });
@@ -2354,7 +2928,7 @@ var WebRTCTransport = class {
2354
2928
  this._peer.on("connect", () => {
2355
2929
  (0, import_log11.log)("connected", void 0, {
2356
2930
  F: __dxlog_file10,
2357
- L: 67,
2931
+ L: 54,
2358
2932
  S: this,
2359
2933
  C: (f, a) => f(...a)
2360
2934
  });
@@ -2364,7 +2938,7 @@ var WebRTCTransport = class {
2364
2938
  this._peer.on("close", async () => {
2365
2939
  (0, import_log11.log)("closed", void 0, {
2366
2940
  F: __dxlog_file10,
2367
- L: 73,
2941
+ L: 60,
2368
2942
  S: this,
2369
2943
  C: (f, a) => f(...a)
2370
2944
  });
@@ -2381,7 +2955,7 @@ var WebRTCTransport = class {
2381
2955
  stats
2382
2956
  }, {
2383
2957
  F: __dxlog_file10,
2384
- L: 85,
2958
+ L: 72,
2385
2959
  S: this,
2386
2960
  C: (f, a) => f(...a)
2387
2961
  });
@@ -2395,7 +2969,7 @@ var WebRTCTransport = class {
2395
2969
  id: this._instanceId
2396
2970
  }), {
2397
2971
  F: __dxlog_file10,
2398
- L: 95,
2972
+ L: 82,
2399
2973
  S: this,
2400
2974
  C: (f, a) => f(...a)
2401
2975
  });
@@ -2403,7 +2977,7 @@ var WebRTCTransport = class {
2403
2977
  async destroy() {
2404
2978
  (0, import_log11.log)("closing...", void 0, {
2405
2979
  F: __dxlog_file10,
2406
- L: 99,
2980
+ L: 86,
2407
2981
  S: this,
2408
2982
  C: (f, a) => f(...a)
2409
2983
  });
@@ -2413,7 +2987,7 @@ var WebRTCTransport = class {
2413
2987
  this.closed.emit();
2414
2988
  (0, import_log11.log)("closed", void 0, {
2415
2989
  F: __dxlog_file10,
2416
- L: 104,
2990
+ L: 91,
2417
2991
  S: this,
2418
2992
  C: (f, a) => f(...a)
2419
2993
  });
@@ -2422,32 +2996,30 @@ var WebRTCTransport = class {
2422
2996
  if (this._closed) {
2423
2997
  return;
2424
2998
  }
2425
- (0, import_invariant11.invariant)(signal.payload.data, "Signal message must contain signal data.", {
2426
- F: __dxlog_file10,
2427
- L: 112,
2428
- S: this,
2429
- A: [
2430
- "signal.payload.data",
2431
- "'Signal message must contain signal data.'"
2432
- ]
2433
- });
2999
+ (0, import_tiny_invariant.default)(signal.payload.data, "Signal message must contain signal data.");
2434
3000
  this._peer.signal(signal.payload.data);
2435
3001
  }
2436
3002
  async _disconnectStreams() {
2437
3003
  this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2438
3004
  }
2439
3005
  };
3006
+ var createSimplePeerTransportFactory = (webrtcConfig) => ({
3007
+ createTransport: (params) => new SimplePeerTransport({
3008
+ ...params,
3009
+ webrtcConfig
3010
+ })
3011
+ });
2440
3012
 
2441
- // packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
3013
+ // packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts
2442
3014
  var import_node_stream2 = require("node:stream");
2443
3015
  var import_codec_protobuf = require("@dxos/codec-protobuf");
2444
- var import_invariant12 = require("@dxos/invariant");
3016
+ var import_invariant11 = require("@dxos/invariant");
2445
3017
  var import_keys11 = require("@dxos/keys");
2446
3018
  var import_log12 = require("@dxos/log");
2447
3019
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
2448
3020
  var import_util8 = require("@dxos/util");
2449
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2450
- var WebRTCTransportService = class {
3021
+ var __dxlog_file11 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3022
+ var SimplePeerTransportService = class {
2451
3023
  // prettier-ignore
2452
3024
  constructor(_webrtcConfig) {
2453
3025
  this._webrtcConfig = _webrtcConfig;
@@ -2474,7 +3046,7 @@ var WebRTCTransportService = class {
2474
3046
  callback();
2475
3047
  }
2476
3048
  });
2477
- const transport = new WebRTCTransport({
3049
+ const transport = new SimplePeerTransport({
2478
3050
  initiator: request.initiator,
2479
3051
  stream: duplex,
2480
3052
  webrtcConfig: this._webrtcConfig,
@@ -2526,7 +3098,7 @@ var WebRTCTransportService = class {
2526
3098
  return rpcStream;
2527
3099
  }
2528
3100
  async sendSignal({ proxyId, signal }) {
2529
- (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
3101
+ (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
2530
3102
  F: __dxlog_file11,
2531
3103
  L: 113,
2532
3104
  S: this,
@@ -2538,7 +3110,7 @@ var WebRTCTransportService = class {
2538
3110
  await this.transports.get(proxyId).transport.signal(signal);
2539
3111
  }
2540
3112
  async sendData({ proxyId, payload }) {
2541
- (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
3113
+ (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
2542
3114
  F: __dxlog_file11,
2543
3115
  L: 118,
2544
3116
  S: this,
@@ -2568,18 +3140,18 @@ var WebRTCTransportService = class {
2568
3140
  }
2569
3141
  };
2570
3142
 
2571
- // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
3143
+ // packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts
2572
3144
  var import_node_stream3 = require("node:stream");
2573
3145
  var import_async10 = require("@dxos/async");
2574
3146
  var import_context6 = require("@dxos/context");
2575
3147
  var import_debug6 = require("@dxos/debug");
2576
- var import_invariant13 = require("@dxos/invariant");
3148
+ var import_invariant12 = require("@dxos/invariant");
2577
3149
  var import_keys12 = require("@dxos/keys");
2578
3150
  var import_log13 = require("@dxos/log");
2579
3151
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
2580
3152
  var import_util9 = require("@dxos/util");
2581
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2582
- var WebRTCTransportProxy = class {
3153
+ var __dxlog_file12 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3154
+ var SimplePeerTransportProxy = class {
2583
3155
  // prettier-ignore
2584
3156
  constructor(_params) {
2585
3157
  this._params = _params;
@@ -2595,7 +3167,7 @@ var WebRTCTransportProxy = class {
2595
3167
  });
2596
3168
  this._serviceStream.waitUntilReady().then(() => {
2597
3169
  this._serviceStream.subscribe(async (event) => {
2598
- (0, import_log13.log)("WebRTCTransportProxy: event", event, {
3170
+ (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
2599
3171
  F: __dxlog_file12,
2600
3172
  L: 51,
2601
3173
  S: this,
@@ -2690,7 +3262,7 @@ var WebRTCTransportProxy = class {
2690
3262
  this._closed = true;
2691
3263
  }
2692
3264
  };
2693
- var WebRTCTransportProxyFactory = class {
3265
+ var SimplePeerTransportProxyFactory = class {
2694
3266
  constructor() {
2695
3267
  this._connections = /* @__PURE__ */ new Set();
2696
3268
  }
@@ -2706,16 +3278,16 @@ var WebRTCTransportProxyFactory = class {
2706
3278
  return this;
2707
3279
  }
2708
3280
  createTransport(options) {
2709
- (0, import_invariant13.invariant)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections", {
3281
+ (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
2710
3282
  F: __dxlog_file12,
2711
3283
  L: 163,
2712
3284
  S: this,
2713
3285
  A: [
2714
3286
  "this._bridgeService",
2715
- "'WebRTCTransportProxyFactory is not ready to open connections'"
3287
+ "'SimplePeerTransportProxyFactory is not ready to open connections'"
2716
3288
  ]
2717
3289
  });
2718
- const transport = new WebRTCTransportProxy({
3290
+ const transport = new SimplePeerTransportProxy({
2719
3291
  ...options,
2720
3292
  bridgeService: this._bridgeService
2721
3293
  });
@@ -2725,10 +3297,352 @@ var WebRTCTransportProxyFactory = class {
2725
3297
  }
2726
3298
  };
2727
3299
 
2728
- // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
3300
+ // packages/core/mesh/network-manager/src/transport/index.ts
3301
+ init_datachannel();
3302
+
3303
+ // packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts
3304
+ var import_stream = require("stream");
2729
3305
  var import_async11 = require("@dxos/async");
2730
- var import_keys13 = require("@dxos/keys");
3306
+ var import_debug7 = require("@dxos/debug");
2731
3307
  var import_log14 = require("@dxos/log");
3308
+ function _ts_decorate5(decorators, target, key, desc) {
3309
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3310
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
3311
+ r = Reflect.decorate(decorators, target, key, desc);
3312
+ else
3313
+ for (var i = decorators.length - 1; i >= 0; i--)
3314
+ if (d = decorators[i])
3315
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3316
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
3317
+ }
3318
+ var __dxlog_file13 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts";
3319
+ var DATACHANNEL_LABEL = "dxos.mesh.transport";
3320
+ var MAX_BUFFERED_AMOUNT = 64 * 1024;
3321
+ var MAX_MESSAGE_SIZE = 64 * 1024;
3322
+ var LibDataChannelTransport = class {
3323
+ constructor(params) {
3324
+ this.params = params;
3325
+ this._closed = false;
3326
+ this.closed = new import_async11.Event();
3327
+ this._connected = false;
3328
+ this.connected = new import_async11.Event();
3329
+ this.errors = new import_debug7.ErrorStream();
3330
+ this._readyForCandidates = new import_async11.Trigger();
3331
+ this._writeCallback = null;
3332
+ this._peer = (async () => {
3333
+ const { RTCPeerConnection: PeerConnection2 } = await Promise.resolve().then(() => (init_datachannel(), datachannel_exports));
3334
+ if (this._closed) {
3335
+ this.errors.raise(new Error("connection already closed"));
3336
+ }
3337
+ const peer = new PeerConnection2(params.webrtcConfig);
3338
+ peer.onicecandidateerror = (event) => {
3339
+ import_log14.log.error("peer.onicecandidateerror", {
3340
+ event
3341
+ }, {
3342
+ F: __dxlog_file13,
3343
+ L: 53,
3344
+ S: this,
3345
+ C: (f, a) => f(...a)
3346
+ });
3347
+ };
3348
+ peer.onconnectionstatechange = (event) => {
3349
+ import_log14.log.debug("peer.onconnectionstatechange", {
3350
+ event,
3351
+ peerConnectionState: peer.connectionState,
3352
+ transportConnectionState: this._connected
3353
+ }, {
3354
+ F: __dxlog_file13,
3355
+ L: 57,
3356
+ S: this,
3357
+ C: (f, a) => f(...a)
3358
+ });
3359
+ };
3360
+ peer.onicecandidate = async (event) => {
3361
+ import_log14.log.debug("peer.onicecandidate", {
3362
+ event
3363
+ }, {
3364
+ F: __dxlog_file13,
3365
+ L: 66,
3366
+ S: this,
3367
+ C: (f, a) => f(...a)
3368
+ });
3369
+ if (event.candidate) {
3370
+ try {
3371
+ await params.sendSignal({
3372
+ payload: {
3373
+ data: {
3374
+ type: "candidate",
3375
+ candidate: {
3376
+ candidate: event.candidate.candidate,
3377
+ // these fields never seem to be not null, but connecting to Chrome doesn't work if they are
3378
+ sdpMLineIndex: event.candidate.sdpMLineIndex ?? 0,
3379
+ sdpMid: event.candidate.sdpMid ?? 0
3380
+ }
3381
+ }
3382
+ }
3383
+ });
3384
+ } catch (err) {
3385
+ import_log14.log.warn("signaling errror", {
3386
+ err
3387
+ }, {
3388
+ F: __dxlog_file13,
3389
+ L: 83,
3390
+ S: this,
3391
+ C: (f, a) => f(...a)
3392
+ });
3393
+ }
3394
+ }
3395
+ };
3396
+ if (params.initiator) {
3397
+ peer.createOffer().then(async (offer) => {
3398
+ if (peer.connectionState !== "connecting") {
3399
+ import_log14.log.error("i am initiator but peer not in state connecting", {
3400
+ peer
3401
+ }, {
3402
+ F: __dxlog_file13,
3403
+ L: 92,
3404
+ S: this,
3405
+ C: (f, a) => f(...a)
3406
+ });
3407
+ this.errors.raise(new Error("invalid state: peer is initiator, but other peer not in state connecting"));
3408
+ }
3409
+ import_log14.log.debug(`im the initiator, creating offer, peer is in state ${peer.connectionState}`, {
3410
+ offer
3411
+ }, {
3412
+ F: __dxlog_file13,
3413
+ L: 95,
3414
+ S: this,
3415
+ C: (f, a) => f(...a)
3416
+ });
3417
+ await peer.setLocalDescription(offer);
3418
+ await params.sendSignal({
3419
+ payload: {
3420
+ data: {
3421
+ type: offer.type,
3422
+ sdp: offer.sdp
3423
+ }
3424
+ }
3425
+ });
3426
+ return offer;
3427
+ }).catch((err) => {
3428
+ this.errors.raise(err);
3429
+ });
3430
+ this.handleChannel(peer.createDataChannel(DATACHANNEL_LABEL));
3431
+ import_log14.log.debug("created data channel", void 0, {
3432
+ F: __dxlog_file13,
3433
+ L: 104,
3434
+ S: this,
3435
+ C: (f, a) => f(...a)
3436
+ });
3437
+ peer.ondatachannel = (event) => {
3438
+ this.errors.raise(new Error("got ondatachannel when i am the initiator?"));
3439
+ };
3440
+ } else {
3441
+ peer.ondatachannel = (event) => {
3442
+ import_log14.log.debug("peer.ondatachannel (non-initiator)", {
3443
+ event
3444
+ }, {
3445
+ F: __dxlog_file13,
3446
+ L: 110,
3447
+ S: this,
3448
+ C: (f, a) => f(...a)
3449
+ });
3450
+ if (event.channel.label !== DATACHANNEL_LABEL) {
3451
+ this.errors.raise(new Error(`unexpected channel label ${event.channel.label}`));
3452
+ }
3453
+ this.handleChannel(event.channel);
3454
+ };
3455
+ }
3456
+ return peer;
3457
+ })();
3458
+ }
3459
+ handleChannel(dataChannel) {
3460
+ this._channel = dataChannel;
3461
+ this._channel.onopen = () => {
3462
+ import_log14.log.debug("dataChannel.onopen", void 0, {
3463
+ F: __dxlog_file13,
3464
+ L: 126,
3465
+ S: this,
3466
+ C: (f, a) => f(...a)
3467
+ });
3468
+ const duplex = new import_stream.Duplex({
3469
+ read: () => {
3470
+ },
3471
+ write: (chunk, encoding, callback) => {
3472
+ if (chunk.length > MAX_MESSAGE_SIZE) {
3473
+ this.errors.raise(new Error(`message too large: ${chunk.length} > ${MAX_MESSAGE_SIZE}`));
3474
+ }
3475
+ dataChannel.send(chunk);
3476
+ if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
3477
+ if (this._writeCallback !== null) {
3478
+ import_log14.log.error("consumer trying to write before we're ready for more data", void 0, {
3479
+ F: __dxlog_file13,
3480
+ L: 139,
3481
+ S: this,
3482
+ C: (f, a) => f(...a)
3483
+ });
3484
+ }
3485
+ this._writeCallback = callback;
3486
+ } else {
3487
+ callback();
3488
+ }
3489
+ }
3490
+ });
3491
+ duplex.pipe(this.params.stream).pipe(duplex);
3492
+ this._stream = duplex;
3493
+ this._connected = true;
3494
+ this.connected.emit();
3495
+ };
3496
+ this._channel.onerror = async (err) => {
3497
+ this.errors.raise(new Error("channel error: " + err.toString()));
3498
+ await this._close();
3499
+ };
3500
+ this._channel.onclose = async (err) => {
3501
+ import_log14.log.info("channel onclose", {
3502
+ err
3503
+ }, {
3504
+ F: __dxlog_file13,
3505
+ L: 159,
3506
+ S: this,
3507
+ C: (f, a) => f(...a)
3508
+ });
3509
+ await this._close();
3510
+ };
3511
+ this._channel.onmessage = (event) => {
3512
+ let data = event.data;
3513
+ if (data instanceof ArrayBuffer) {
3514
+ data = Buffer.from(data);
3515
+ }
3516
+ this._stream.push(data);
3517
+ };
3518
+ this._channel.onbufferedamountlow = () => {
3519
+ const cb = this._writeCallback;
3520
+ this._writeCallback = null;
3521
+ cb?.();
3522
+ };
3523
+ }
3524
+ async _close() {
3525
+ if (this._closed) {
3526
+ return;
3527
+ }
3528
+ await this._disconnectStreams();
3529
+ this._closed = true;
3530
+ this.closed.emit();
3531
+ }
3532
+ signal(signal) {
3533
+ this._peer.then(async (peer) => {
3534
+ const data = signal.payload.data;
3535
+ switch (data.type) {
3536
+ case "offer": {
3537
+ if ((await this._peer).connectionState !== "new") {
3538
+ import_log14.log.error("received offer but peer not in state new", {
3539
+ peer
3540
+ }, {
3541
+ F: __dxlog_file13,
3542
+ L: 194,
3543
+ S: this,
3544
+ C: (f, a) => f(...a)
3545
+ });
3546
+ this.errors.raise(new Error("invalid signalling state: received offer when peer is not in state new"));
3547
+ break;
3548
+ }
3549
+ try {
3550
+ await peer.setRemoteDescription({
3551
+ type: data.type,
3552
+ sdp: data.sdp
3553
+ });
3554
+ const answer = await peer.createAnswer();
3555
+ await peer.setLocalDescription(answer);
3556
+ await this.params.sendSignal({
3557
+ payload: {
3558
+ data: {
3559
+ type: answer.type,
3560
+ sdp: answer.sdp
3561
+ }
3562
+ }
3563
+ });
3564
+ this._readyForCandidates.wake();
3565
+ } catch (err) {
3566
+ import_log14.log.error("can't handle offer from signalling server", {
3567
+ err
3568
+ }, {
3569
+ F: __dxlog_file13,
3570
+ L: 205,
3571
+ S: this,
3572
+ C: (f, a) => f(...a)
3573
+ });
3574
+ this.errors.raise(new Error("error handling offer"));
3575
+ }
3576
+ break;
3577
+ }
3578
+ case "answer":
3579
+ try {
3580
+ await peer.setRemoteDescription({
3581
+ type: data.type,
3582
+ sdp: data.sdp
3583
+ });
3584
+ this._readyForCandidates.wake();
3585
+ } catch (err) {
3586
+ import_log14.log.error("can't handle answer from signalling server", {
3587
+ err
3588
+ }, {
3589
+ F: __dxlog_file13,
3590
+ L: 216,
3591
+ S: this,
3592
+ C: (f, a) => f(...a)
3593
+ });
3594
+ this.errors.raise(new Error("error handling answer"));
3595
+ }
3596
+ break;
3597
+ case "candidate":
3598
+ await this._readyForCandidates.wait();
3599
+ await peer.addIceCandidate({
3600
+ candidate: data.candidate.candidate
3601
+ });
3602
+ break;
3603
+ default:
3604
+ import_log14.log.error("unhandled signal type", {
3605
+ type: data.type,
3606
+ signal
3607
+ }, {
3608
+ F: __dxlog_file13,
3609
+ L: 227,
3610
+ S: this,
3611
+ C: (f, a) => f(...a)
3612
+ });
3613
+ this.errors.raise(new Error(`unhandled signal type ${data.type}`));
3614
+ }
3615
+ }).catch((err) => {
3616
+ import_log14.log.catch(err, void 0, {
3617
+ F: __dxlog_file13,
3618
+ L: 232,
3619
+ S: this,
3620
+ C: (f, a) => f(...a)
3621
+ });
3622
+ });
3623
+ }
3624
+ // TODO(nf): add classmethod to call node-datachannel.cleanup() when all instances have been destroyed?
3625
+ async destroy() {
3626
+ await this._close();
3627
+ }
3628
+ async _disconnectStreams() {
3629
+ this.params.stream.unpipe?.(this._stream)?.unpipe?.(this.params.stream);
3630
+ }
3631
+ };
3632
+ _ts_decorate5([
3633
+ import_async11.synchronized
3634
+ ], LibDataChannelTransport.prototype, "_close", null);
3635
+ var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3636
+ createTransport: (params) => new LibDataChannelTransport({
3637
+ ...params,
3638
+ webrtcConfig
3639
+ })
3640
+ });
3641
+
3642
+ // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
3643
+ var import_async12 = require("@dxos/async");
3644
+ var import_keys13 = require("@dxos/keys");
3645
+ var import_log15 = require("@dxos/log");
2732
3646
  var import_teleport2 = require("@dxos/teleport");
2733
3647
  var import_util10 = require("@dxos/util");
2734
3648
 
@@ -2739,33 +3653,36 @@ var createTeleportProtocolFactory = (onConnection) => {
2739
3653
  const teleport = new import_teleport.Teleport(params);
2740
3654
  return {
2741
3655
  stream: teleport.stream,
2742
- initialize: async () => {
3656
+ open: async () => {
2743
3657
  await teleport.open();
2744
3658
  await onConnection(teleport);
2745
3659
  },
2746
- destroy: async () => {
3660
+ close: async () => {
2747
3661
  await teleport.close();
3662
+ },
3663
+ abort: async () => {
3664
+ await teleport.abort();
2748
3665
  }
2749
3666
  };
2750
3667
  };
2751
3668
  };
2752
3669
 
2753
3670
  // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
2754
- var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
3671
+ var __dxlog_file14 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
2755
3672
  var TestWireProtocol = class {
2756
3673
  constructor(peerId, _extensionFactory = () => []) {
2757
3674
  this.peerId = peerId;
2758
3675
  this._extensionFactory = _extensionFactory;
2759
3676
  this.connections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
2760
3677
  this.streamConnections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
2761
- this.connected = new import_async11.Event();
2762
- this.disconnected = new import_async11.Event();
3678
+ this.connected = new import_async12.Event();
3679
+ this.disconnected = new import_async12.Event();
2763
3680
  this.otherConnections = new import_util10.ComplexMap(({ remotePeerId, extension }) => remotePeerId.toHex() + extension);
2764
3681
  this.factory = createTeleportProtocolFactory(async (teleport) => {
2765
- (0, import_log14.log)("create", {
3682
+ (0, import_log15.log)("create", {
2766
3683
  remotePeerId: teleport.remotePeerId
2767
3684
  }, {
2768
- F: __dxlog_file13,
3685
+ F: __dxlog_file14,
2769
3686
  L: 38,
2770
3687
  S: this,
2771
3688
  C: (f, a) => f(...a)
@@ -2799,15 +3716,15 @@ var TestWireProtocol = class {
2799
3716
  if (this.connections.has(peerId)) {
2800
3717
  return this.connections.get(peerId);
2801
3718
  }
2802
- (0, import_log14.log)("waitForConnection", {
3719
+ (0, import_log15.log)("waitForConnection", {
2803
3720
  peerId
2804
3721
  }, {
2805
- F: __dxlog_file13,
3722
+ F: __dxlog_file14,
2806
3723
  L: 67,
2807
3724
  S: this,
2808
3725
  C: (f, a) => f(...a)
2809
3726
  });
2810
- await (0, import_async11.asyncTimeout)(
3727
+ await (0, import_async12.asyncTimeout)(
2811
3728
  this.connected.waitFor((connectedId) => connectedId.equals(peerId)),
2812
3729
  // TODO(nf): Make this configurable.
2813
3730
  1e4
@@ -2835,6 +3752,7 @@ var TestWireProtocol = class {
2835
3752
  };
2836
3753
 
2837
3754
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
3755
+ var __dxlog_file15 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/testing/test-builder.ts";
2838
3756
  var port = process.env.SIGNAL_PORT ?? 4e3;
2839
3757
  var TEST_SIGNAL_HOSTS = [
2840
3758
  {
@@ -2853,52 +3771,92 @@ var TestBuilder = class {
2853
3771
  return new import_messaging2.MemorySignalManager(this._signalContext);
2854
3772
  }
2855
3773
  createPeer(peerId = import_keys14.PublicKey.random()) {
2856
- return new TestPeer(this, peerId);
3774
+ return new TestPeer(this, peerId, this.options.transport);
2857
3775
  }
2858
3776
  };
2859
3777
  var TestPeer = class {
2860
- constructor(testBuilder, peerId) {
3778
+ constructor(testBuilder, peerId, transport) {
2861
3779
  this.testBuilder = testBuilder;
2862
3780
  this.peerId = peerId;
3781
+ this.transport = transport;
2863
3782
  this._swarms = new import_util11.ComplexMap(import_keys14.PublicKey.hash);
2864
3783
  this._signalManager = this.testBuilder.createSignalManager();
2865
- this._networkManager = this.createNetworkManager();
3784
+ if (!transport) {
3785
+ if (this.testBuilder.options.signalHosts) {
3786
+ transport = TransportKind.SIMPLE_PEER;
3787
+ } else {
3788
+ transport = TransportKind.MEMORY;
3789
+ }
3790
+ }
3791
+ this._networkManager = this.createNetworkManager(transport);
2866
3792
  }
2867
3793
  // TODO(burdon): Move to TestBuilder.
2868
- createNetworkManager() {
2869
- let transportFactory = MemoryTransportFactory;
3794
+ createNetworkManager(transport) {
3795
+ let transportFactory;
2870
3796
  if (this.testBuilder.options.signalHosts) {
2871
- if (this.testBuilder.options.bridge) {
2872
- const [proxyPort, servicePort] = (0, import_rpc.createLinkedPorts)();
2873
- this._proxy = (0, import_rpc.createProtoRpcPeer)({
2874
- port: proxyPort,
2875
- requested: {
2876
- BridgeService: import_protocols6.schema.getService("dxos.mesh.bridge.BridgeService")
2877
- },
2878
- noHandshake: true,
2879
- encodingOptions: {
2880
- preserveAny: true
2881
- }
2882
- });
2883
- this._service = (0, import_rpc.createProtoRpcPeer)({
2884
- port: servicePort,
2885
- exposed: {
2886
- BridgeService: import_protocols6.schema.getService("dxos.mesh.bridge.BridgeService")
2887
- },
2888
- handlers: {
2889
- BridgeService: new WebRTCTransportService()
2890
- },
2891
- noHandshake: true,
2892
- encodingOptions: {
2893
- preserveAny: true
3797
+ import_log16.log.info(`using ${transport} transport with signal server.`, void 0, {
3798
+ F: __dxlog_file15,
3799
+ L: 104,
3800
+ S: this,
3801
+ C: (f, a) => f(...a)
3802
+ });
3803
+ switch (transport) {
3804
+ case TransportKind.MEMORY:
3805
+ throw new Error("Memory transport not supported with signal server.");
3806
+ case TransportKind.SIMPLE_PEER:
3807
+ transportFactory = createSimplePeerTransportFactory();
3808
+ break;
3809
+ case TransportKind.LIBDATACHANNEL:
3810
+ transportFactory = createLibDataChannelTransportFactory();
3811
+ break;
3812
+ case TransportKind.SIMPLE_PEER_PROXY:
3813
+ {
3814
+ const [proxyPort, servicePort] = (0, import_rpc.createLinkedPorts)();
3815
+ this._proxy = (0, import_rpc.createProtoRpcPeer)({
3816
+ port: proxyPort,
3817
+ requested: {
3818
+ BridgeService: import_protocols6.schema.getService("dxos.mesh.bridge.BridgeService")
3819
+ },
3820
+ noHandshake: true,
3821
+ encodingOptions: {
3822
+ preserveAny: true
3823
+ }
3824
+ });
3825
+ this._service = (0, import_rpc.createProtoRpcPeer)({
3826
+ port: servicePort,
3827
+ exposed: {
3828
+ BridgeService: import_protocols6.schema.getService("dxos.mesh.bridge.BridgeService")
3829
+ },
3830
+ handlers: {
3831
+ BridgeService: new SimplePeerTransportService()
3832
+ },
3833
+ noHandshake: true,
3834
+ encodingOptions: {
3835
+ preserveAny: true
3836
+ }
3837
+ });
3838
+ transportFactory = new SimplePeerTransportProxyFactory().setBridgeService(this._proxy.rpc.BridgeService);
2894
3839
  }
3840
+ break;
3841
+ default:
3842
+ throw new Error(`Unsupported transport: ${transport}`);
3843
+ }
3844
+ } else {
3845
+ if (transport !== TransportKind.MEMORY) {
3846
+ import_log16.log.warn(`specified transport ${transport} but no signalling configured, using memory transport instead`, void 0, {
3847
+ F: __dxlog_file15,
3848
+ L: 150,
3849
+ S: this,
3850
+ C: (f, a) => f(...a)
2895
3851
  });
2896
- transportFactory = new WebRTCTransportProxyFactory().setBridgeService(this._proxy.rpc.BridgeService);
2897
- } else {
2898
- transportFactory = {
2899
- createTransport: (params) => new WebRTCTransport(params)
2900
- };
2901
3852
  }
3853
+ import_log16.log.info(`using ${transport} transport without signal server.`, void 0, {
3854
+ F: __dxlog_file15,
3855
+ L: 152,
3856
+ S: this,
3857
+ C: (f, a) => f(...a)
3858
+ });
3859
+ transportFactory = MemoryTransportFactory;
2902
3860
  }
2903
3861
  return new NetworkManager({
2904
3862
  signalManager: this._signalManager,