@dxos/network-manager 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/browser/{chunk-JUVDHWW4.mjs → chunk-ERB3AQAF.mjs} +478 -1996
- package/dist/lib/browser/chunk-ERB3AQAF.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +198 -4
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +45 -77
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/browser/transport/tcp/index.mjs +2 -33
- package/dist/lib/browser/transport/tcp/index.mjs.map +4 -4
- package/dist/lib/browser/transport/tcp/tcp-transport.browser.mjs +36 -0
- package/dist/lib/browser/transport/tcp/tcp-transport.browser.mjs.map +7 -0
- package/dist/lib/browser/transport/tcp/tcp-transport.mjs +125 -0
- package/dist/lib/browser/transport/tcp/tcp-transport.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-HUSU3OKQ.mjs → chunk-KYLPLEZQ.mjs} +478 -1996
- package/dist/lib/node-esm/chunk-KYLPLEZQ.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +198 -4
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +45 -77
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/transport/tcp/index.mjs +2 -164
- package/dist/lib/node-esm/transport/tcp/index.mjs.map +4 -4
- package/dist/lib/node-esm/transport/tcp/tcp-transport.browser.mjs +36 -0
- package/dist/lib/node-esm/transport/tcp/tcp-transport.browser.mjs.map +7 -0
- package/dist/lib/node-esm/transport/tcp/tcp-transport.mjs +125 -0
- package/dist/lib/node-esm/transport/tcp/tcp-transport.mjs.map +7 -0
- package/dist/types/src/connection-log.d.ts.map +1 -1
- package/dist/types/src/network-manager.d.ts +6 -6
- package/dist/types/src/network-manager.d.ts.map +1 -1
- package/dist/types/src/signal/ice.d.ts.map +1 -1
- package/dist/types/src/signal/signal-connection.d.ts +3 -2
- package/dist/types/src/signal/signal-connection.d.ts.map +1 -1
- package/dist/types/src/signal/signal-messenger.d.ts +3 -2
- package/dist/types/src/signal/signal-messenger.d.ts.map +1 -1
- package/dist/types/src/signal/swarm-messenger.d.ts +7 -7
- package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
- package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
- package/dist/types/src/swarm/connection.d.ts +2 -2
- package/dist/types/src/swarm/connection.d.ts.map +1 -1
- package/dist/types/src/swarm/peer.d.ts +4 -3
- package/dist/types/src/swarm/peer.d.ts.map +1 -1
- package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
- package/dist/types/src/swarm/swarm.d.ts +3 -5
- package/dist/types/src/swarm/swarm.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -1
- package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
- package/dist/types/src/tests/property-test-suite.d.ts.map +1 -1
- package/dist/types/src/tests/utils.d.ts.map +1 -1
- package/dist/types/src/topology/fully-connected-topology.d.ts.map +1 -1
- package/dist/types/src/topology/mmst-topology.d.ts.map +1 -1
- package/dist/types/src/topology/star-topology.d.ts.map +1 -1
- package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
- package/dist/types/src/transport/tcp/index.d.ts +1 -1
- package/dist/types/src/transport/tcp/index.d.ts.map +1 -1
- package/dist/types/src/transport/tcp/tcp-transport.browser.d.ts.map +1 -1
- package/dist/types/src/transport/tcp/tcp-transport.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc/rtc-peer-connection.d.ts +4 -4
- package/dist/types/src/transport/webrtc/rtc-peer-connection.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc/rtc-transport-channel.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc/rtc-transport-factory.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc/rtc-transport-proxy.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc/rtc-transport-service.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc/rtc-transport-stats.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc/test-utils.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc/utils.d.ts.map +1 -1
- package/dist/types/src/wire-protocol.d.ts +5 -5
- package/dist/types/src/wire-protocol.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +34 -44
- package/src/network-manager.ts +24 -22
- package/src/signal/integration.node.test.ts +11 -10
- package/src/signal/signal-connection.ts +3 -2
- package/src/signal/signal-messenger.ts +3 -2
- package/src/signal/swarm-messenger.node.test.ts +25 -24
- package/src/signal/swarm-messenger.ts +65 -55
- package/src/swarm/connection.test.ts +8 -8
- package/src/swarm/connection.ts +15 -22
- package/src/swarm/peer.ts +7 -7
- package/src/swarm/swarm.test.ts +4 -4
- package/src/swarm/swarm.ts +10 -17
- package/src/testing/test-builder.ts +4 -4
- package/src/tests/basic-test-suite.ts +0 -1
- package/src/tests/memory-transport.test.ts +0 -2
- package/src/tests/property-test-suite.ts +4 -3
- package/src/tests/tcp-transport.node.test.ts +0 -2
- package/src/tests/webrtc-transport.test.ts +4 -3
- package/src/transport/tcp/index.ts +1 -1
- package/src/transport/webrtc/rtc-peer-connection.ts +1 -2
- package/src/transport/webrtc/rtc-transport-channel.test.ts +0 -2
- package/src/transport/webrtc/rtc-transport-channel.ts +1 -2
- package/src/transport/webrtc/rtc-transport-factory.ts +0 -1
- package/src/transport/webrtc/rtc-transport-proxy.test.ts +2 -3
- package/src/transport/webrtc/rtc-transport-proxy.ts +6 -4
- package/src/transport/webrtc/rtc-transport-service.ts +0 -1
- package/src/transport/webrtc/rtc-transport.test.ts +2 -2
- package/src/wire-protocol.ts +6 -6
- package/dist/lib/browser/chunk-JUVDHWW4.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HUSU3OKQ.mjs.map +0 -7
|
@@ -5,21 +5,20 @@ import { DeferredTask, Event, Trigger, scheduleTask, scheduleTaskInterval, sleep
|
|
|
5
5
|
import { Context, ContextDisposedError, cancelWithContext } from "@dxos/context";
|
|
6
6
|
import { ErrorStream } from "@dxos/debug";
|
|
7
7
|
import { invariant } from "@dxos/invariant";
|
|
8
|
-
import { PublicKey } from "@dxos/keys";
|
|
9
8
|
import { log, logInfo } from "@dxos/log";
|
|
10
|
-
import { CancelledError, ConnectionResetError, ConnectivityError, ProtocolError, TimeoutError
|
|
9
|
+
import { CancelledError, ConnectionResetError, ConnectivityError, ProtocolError, TimeoutError } from "@dxos/protocols";
|
|
10
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
|
|
11
11
|
function _ts_decorate(decorators, target, key, desc) {
|
|
12
12
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
13
13
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
14
14
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
15
15
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
16
16
|
}
|
|
17
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
|
|
18
17
|
var STARTING_SIGNALLING_DELAY = 10;
|
|
19
18
|
var TRANSPORT_CONNECTION_TIMEOUT = 1e4;
|
|
20
19
|
var TRANSPORT_STATS_INTERVAL = 5e3;
|
|
21
20
|
var MAX_SIGNALLING_DELAY = 300;
|
|
22
|
-
var ConnectionState = /* @__PURE__ */ function(ConnectionState5) {
|
|
21
|
+
var ConnectionState = /* @__PURE__ */ (function(ConnectionState5) {
|
|
23
22
|
ConnectionState5["CREATED"] = "CREATED";
|
|
24
23
|
ConnectionState5["INITIAL"] = "INITIAL";
|
|
25
24
|
ConnectionState5["CONNECTING"] = "CONNECTING";
|
|
@@ -29,7 +28,7 @@ var ConnectionState = /* @__PURE__ */ function(ConnectionState5) {
|
|
|
29
28
|
ConnectionState5["ABORTING"] = "ABORTING";
|
|
30
29
|
ConnectionState5["ABORTED"] = "ABORTED";
|
|
31
30
|
return ConnectionState5;
|
|
32
|
-
}({});
|
|
31
|
+
})({});
|
|
33
32
|
var Connection = class {
|
|
34
33
|
topic;
|
|
35
34
|
localInfo;
|
|
@@ -40,21 +39,22 @@ var Connection = class {
|
|
|
40
39
|
_protocol;
|
|
41
40
|
_transportFactory;
|
|
42
41
|
_callbacks;
|
|
43
|
-
_ctx;
|
|
44
|
-
connectedTimeoutContext;
|
|
45
|
-
_protocolClosed;
|
|
46
|
-
_transportClosed;
|
|
47
|
-
_state;
|
|
42
|
+
_ctx = new Context(void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 66 });
|
|
43
|
+
connectedTimeoutContext = new Context(void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 67 });
|
|
44
|
+
_protocolClosed = new Trigger();
|
|
45
|
+
_transportClosed = new Trigger();
|
|
46
|
+
_state = "CREATED";
|
|
48
47
|
_transport;
|
|
49
48
|
closeReason;
|
|
50
|
-
_incomingSignalBuffer;
|
|
51
|
-
_outgoingSignalBuffer;
|
|
52
|
-
stateChanged;
|
|
53
|
-
errors;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
49
|
+
_incomingSignalBuffer = [];
|
|
50
|
+
_outgoingSignalBuffer = [];
|
|
51
|
+
stateChanged = new Event();
|
|
52
|
+
errors = new ErrorStream();
|
|
53
|
+
transportStats = new Event();
|
|
54
|
+
_signalSendTask = new DeferredTask(this._ctx, async () => {
|
|
55
|
+
await this._flushSignalBuffer(this._ctx);
|
|
56
|
+
});
|
|
57
|
+
_signallingDelay = STARTING_SIGNALLING_DELAY;
|
|
58
58
|
constructor(topic, localInfo, remoteInfo, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
|
|
59
59
|
this.topic = topic;
|
|
60
60
|
this.localInfo = localInfo;
|
|
@@ -65,39 +65,13 @@ var Connection = class {
|
|
|
65
65
|
this._protocol = _protocol;
|
|
66
66
|
this._transportFactory = _transportFactory;
|
|
67
67
|
this._callbacks = _callbacks;
|
|
68
|
-
this._ctx = new Context(void 0, {
|
|
69
|
-
F: __dxlog_file,
|
|
70
|
-
L: 100
|
|
71
|
-
});
|
|
72
|
-
this.connectedTimeoutContext = new Context(void 0, {
|
|
73
|
-
F: __dxlog_file,
|
|
74
|
-
L: 101
|
|
75
|
-
});
|
|
76
|
-
this._protocolClosed = new Trigger();
|
|
77
|
-
this._transportClosed = new Trigger();
|
|
78
|
-
this._state = "CREATED";
|
|
79
|
-
this._incomingSignalBuffer = [];
|
|
80
|
-
this._outgoingSignalBuffer = [];
|
|
81
|
-
this.stateChanged = new Event();
|
|
82
|
-
this.errors = new ErrorStream();
|
|
83
|
-
this._instanceId = PublicKey.random().toHex();
|
|
84
|
-
this.transportStats = new Event();
|
|
85
|
-
this._signalSendTask = new DeferredTask(this._ctx, async () => {
|
|
86
|
-
await this._flushSignalBuffer();
|
|
87
|
-
});
|
|
88
|
-
this._signallingDelay = STARTING_SIGNALLING_DELAY;
|
|
89
68
|
log.trace("dxos.mesh.connection.construct", {
|
|
90
69
|
sessionId: this.sessionId,
|
|
91
70
|
topic: this.topic,
|
|
92
71
|
localPeer: this.localInfo,
|
|
93
72
|
remotePeer: this.remoteInfo,
|
|
94
73
|
initiator: this.initiator
|
|
95
|
-
}, {
|
|
96
|
-
F: __dxlog_file,
|
|
97
|
-
L: 137,
|
|
98
|
-
S: this,
|
|
99
|
-
C: (f, a) => f(...a)
|
|
100
|
-
});
|
|
74
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 92, S: this });
|
|
101
75
|
}
|
|
102
76
|
get sessionIdString() {
|
|
103
77
|
return this.sessionId.truncate();
|
|
@@ -115,69 +89,35 @@ var Connection = class {
|
|
|
115
89
|
* Create an underlying transport and prepares it for the connection.
|
|
116
90
|
*/
|
|
117
91
|
async openConnection() {
|
|
118
|
-
invariant(this._state === "INITIAL", "Invalid state.", {
|
|
119
|
-
|
|
120
|
-
L: 167,
|
|
121
|
-
S: this,
|
|
122
|
-
A: [
|
|
123
|
-
"this._state === ConnectionState.INITIAL",
|
|
124
|
-
"'Invalid state.'"
|
|
125
|
-
]
|
|
126
|
-
});
|
|
127
|
-
log.trace("dxos.mesh.connection.open-connection", trace.begin({
|
|
128
|
-
id: this._instanceId
|
|
129
|
-
}), {
|
|
130
|
-
F: __dxlog_file,
|
|
131
|
-
L: 168,
|
|
132
|
-
S: this,
|
|
133
|
-
C: (f, a) => f(...a)
|
|
134
|
-
});
|
|
92
|
+
invariant(this._state === "INITIAL", "Invalid state.", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 115, S: this, A: ['this._state === "INITIAL"', "'Invalid state.'"] });
|
|
93
|
+
log("opening connection", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 116, S: this });
|
|
135
94
|
log.trace("dxos.mesh.connection.open", {
|
|
136
95
|
sessionId: this.sessionId,
|
|
137
96
|
topic: this.topic,
|
|
138
97
|
localPeerId: this.localInfo,
|
|
139
98
|
remotePeerId: this.remoteInfo,
|
|
140
99
|
initiator: this.initiator
|
|
141
|
-
}, {
|
|
142
|
-
F: __dxlog_file,
|
|
143
|
-
L: 169,
|
|
144
|
-
S: this,
|
|
145
|
-
C: (f, a) => f(...a)
|
|
146
|
-
});
|
|
100
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 117, S: this });
|
|
147
101
|
this._changeState("CONNECTING");
|
|
148
102
|
this._protocol.open(this.sessionId).catch((err) => {
|
|
149
103
|
this.errors.raise(err);
|
|
150
104
|
});
|
|
151
105
|
this._protocol.stream.on("close", () => {
|
|
152
|
-
log("protocol stream closed", void 0, {
|
|
153
|
-
F: __dxlog_file,
|
|
154
|
-
L: 186,
|
|
155
|
-
S: this,
|
|
156
|
-
C: (f, a) => f(...a)
|
|
157
|
-
});
|
|
106
|
+
log("protocol stream closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 131, S: this });
|
|
158
107
|
this._protocolClosed.wake();
|
|
159
108
|
this.close({
|
|
160
|
-
error: new ProtocolError(
|
|
109
|
+
error: new ProtocolError({
|
|
110
|
+
message: "protocol stream closed"
|
|
111
|
+
})
|
|
161
112
|
}).catch((err) => this.errors.raise(err));
|
|
162
113
|
});
|
|
163
114
|
scheduleTask(this.connectedTimeoutContext, async () => {
|
|
164
|
-
log.info(`timeout waiting ${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect, aborting`, void 0, {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
C: (f, a) => f(...a)
|
|
169
|
-
});
|
|
170
|
-
await this.abort(new TimeoutError(`${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect`)).catch((err) => this.errors.raise(err));
|
|
115
|
+
log.info(`timeout waiting ${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect, aborting`, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 140, S: this });
|
|
116
|
+
await this.abort(new TimeoutError({
|
|
117
|
+
message: `${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect`
|
|
118
|
+
})).catch((err) => this.errors.raise(err));
|
|
171
119
|
}, TRANSPORT_CONNECTION_TIMEOUT);
|
|
172
|
-
invariant(!this._transport, void 0, {
|
|
173
|
-
F: __dxlog_file,
|
|
174
|
-
L: 202,
|
|
175
|
-
S: this,
|
|
176
|
-
A: [
|
|
177
|
-
"!this._transport",
|
|
178
|
-
""
|
|
179
|
-
]
|
|
180
|
-
});
|
|
120
|
+
invariant(!this._transport, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 145, S: this, A: ["!this._transport", ""] });
|
|
181
121
|
this._transport = this._transportFactory.createTransport({
|
|
182
122
|
ownPeerKey: this.localInfo.peerKey,
|
|
183
123
|
remotePeerKey: this.remoteInfo.peerKey,
|
|
@@ -196,41 +136,21 @@ var Connection = class {
|
|
|
196
136
|
this._transport.closed.once(() => {
|
|
197
137
|
this._transport = void 0;
|
|
198
138
|
this._transportClosed.wake();
|
|
199
|
-
log("abort triggered by transport close", void 0, {
|
|
200
|
-
F: __dxlog_file,
|
|
201
|
-
L: 224,
|
|
202
|
-
S: this,
|
|
203
|
-
C: (f, a) => f(...a)
|
|
204
|
-
});
|
|
139
|
+
log("abort triggered by transport close", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 164, S: this });
|
|
205
140
|
this.abort().catch((err) => this.errors.raise(err));
|
|
206
141
|
});
|
|
207
142
|
this._transport.errors.handle(async (err) => {
|
|
208
143
|
log("transport error:", {
|
|
209
144
|
err
|
|
210
|
-
}, {
|
|
211
|
-
F: __dxlog_file,
|
|
212
|
-
L: 229,
|
|
213
|
-
S: this,
|
|
214
|
-
C: (f, a) => f(...a)
|
|
215
|
-
});
|
|
145
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 168, S: this });
|
|
216
146
|
if (!this.closeReason) {
|
|
217
147
|
this.closeReason = err?.message;
|
|
218
148
|
}
|
|
219
149
|
if (err instanceof ConnectionResetError) {
|
|
220
|
-
log.info("aborting due to transport ConnectionResetError", void 0, {
|
|
221
|
-
F: __dxlog_file,
|
|
222
|
-
L: 236,
|
|
223
|
-
S: this,
|
|
224
|
-
C: (f, a) => f(...a)
|
|
225
|
-
});
|
|
150
|
+
log.info("aborting due to transport ConnectionResetError", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 176, S: this });
|
|
226
151
|
this.abort(err).catch((err2) => this.errors.raise(err2));
|
|
227
152
|
} else if (err instanceof ConnectivityError) {
|
|
228
|
-
log.info("aborting due to transport ConnectivityError", void 0, {
|
|
229
|
-
F: __dxlog_file,
|
|
230
|
-
L: 239,
|
|
231
|
-
S: this,
|
|
232
|
-
C: (f, a) => f(...a)
|
|
233
|
-
});
|
|
153
|
+
log.info("aborting due to transport ConnectivityError", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 179, S: this });
|
|
234
154
|
this.abort(err).catch((err2) => this.errors.raise(err2));
|
|
235
155
|
}
|
|
236
156
|
if (this._state !== "CLOSED" && this._state !== "CLOSING") {
|
|
@@ -243,31 +163,14 @@ var Connection = class {
|
|
|
243
163
|
void this._transport.onSignal(signal);
|
|
244
164
|
}
|
|
245
165
|
this._incomingSignalBuffer = [];
|
|
246
|
-
log
|
|
247
|
-
id: this._instanceId
|
|
248
|
-
}), {
|
|
249
|
-
F: __dxlog_file,
|
|
250
|
-
L: 258,
|
|
251
|
-
S: this,
|
|
252
|
-
C: (f, a) => f(...a)
|
|
253
|
-
});
|
|
166
|
+
log("opened connection", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 193, S: this });
|
|
254
167
|
}
|
|
255
168
|
async abort(err) {
|
|
256
169
|
log("abort", {
|
|
257
170
|
err
|
|
258
|
-
}, {
|
|
259
|
-
F: __dxlog_file,
|
|
260
|
-
L: 265,
|
|
261
|
-
S: this,
|
|
262
|
-
C: (f, a) => f(...a)
|
|
263
|
-
});
|
|
171
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 196, S: this });
|
|
264
172
|
if (this._state === "CLOSED" || this._state === "ABORTED") {
|
|
265
|
-
log(`abort ignored: already ${this._state}`, this.closeReason, {
|
|
266
|
-
F: __dxlog_file,
|
|
267
|
-
L: 267,
|
|
268
|
-
S: this,
|
|
269
|
-
C: (f, a) => f(...a)
|
|
270
|
-
});
|
|
173
|
+
log(`abort ignored: already ${this._state}`, this.closeReason, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 200, S: this });
|
|
271
174
|
return;
|
|
272
175
|
}
|
|
273
176
|
await this.connectedTimeoutContext.dispose();
|
|
@@ -279,55 +182,30 @@ var Connection = class {
|
|
|
279
182
|
log("aborting...", {
|
|
280
183
|
peerId: this.localInfo,
|
|
281
184
|
err
|
|
282
|
-
}, {
|
|
283
|
-
F: __dxlog_file,
|
|
284
|
-
L: 279,
|
|
285
|
-
S: this,
|
|
286
|
-
C: (f, a) => f(...a)
|
|
287
|
-
});
|
|
185
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 209, S: this });
|
|
288
186
|
try {
|
|
289
187
|
await this._closeProtocol({
|
|
290
188
|
abort: true
|
|
291
189
|
});
|
|
292
190
|
} catch (err2) {
|
|
293
|
-
log.catch(err2, void 0, {
|
|
294
|
-
F: __dxlog_file,
|
|
295
|
-
L: 285,
|
|
296
|
-
S: this,
|
|
297
|
-
C: (f, a) => f(...a)
|
|
298
|
-
});
|
|
191
|
+
log.catch(err2, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 219, S: this });
|
|
299
192
|
}
|
|
300
193
|
try {
|
|
301
194
|
await this._closeTransport();
|
|
302
195
|
} catch (err2) {
|
|
303
|
-
log.catch(err2, void 0, {
|
|
304
|
-
F: __dxlog_file,
|
|
305
|
-
L: 292,
|
|
306
|
-
S: this,
|
|
307
|
-
C: (f, a) => f(...a)
|
|
308
|
-
});
|
|
196
|
+
log.catch(err2, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 225, S: this });
|
|
309
197
|
}
|
|
310
198
|
try {
|
|
311
199
|
this._callbacks?.onClosed?.(err);
|
|
312
200
|
} catch (err2) {
|
|
313
|
-
log.catch(err2, void 0, {
|
|
314
|
-
F: __dxlog_file,
|
|
315
|
-
L: 298,
|
|
316
|
-
S: this,
|
|
317
|
-
C: (f, a) => f(...a)
|
|
318
|
-
});
|
|
201
|
+
log.catch(err2, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 230, S: this });
|
|
319
202
|
}
|
|
320
203
|
this._changeState("ABORTED");
|
|
321
204
|
}
|
|
322
205
|
async close({ error, reason } = {}) {
|
|
323
206
|
log("close", {
|
|
324
207
|
error
|
|
325
|
-
}, {
|
|
326
|
-
F: __dxlog_file,
|
|
327
|
-
L: 305,
|
|
328
|
-
S: this,
|
|
329
|
-
C: (f, a) => f(...a)
|
|
330
|
-
});
|
|
208
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 235, S: this });
|
|
331
209
|
if (!this.closeReason) {
|
|
332
210
|
this.closeReason = reason ?? error?.message;
|
|
333
211
|
} else {
|
|
@@ -337,12 +215,7 @@ var Connection = class {
|
|
|
337
215
|
log("close ignored: already in progress", {
|
|
338
216
|
state: this._state,
|
|
339
217
|
error
|
|
340
|
-
}, {
|
|
341
|
-
F: __dxlog_file,
|
|
342
|
-
L: 316,
|
|
343
|
-
S: this,
|
|
344
|
-
C: (f, a) => f(...a)
|
|
345
|
-
});
|
|
218
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 244, S: this });
|
|
346
219
|
return;
|
|
347
220
|
}
|
|
348
221
|
const lastState = this._state;
|
|
@@ -351,111 +224,66 @@ var Connection = class {
|
|
|
351
224
|
await this._ctx.dispose();
|
|
352
225
|
let abortProtocol = false;
|
|
353
226
|
if (lastState !== "CONNECTED" || error != null) {
|
|
354
|
-
log(`graceful close requested when we were in ${lastState} state? aborting`, void 0, {
|
|
355
|
-
F: __dxlog_file,
|
|
356
|
-
L: 327,
|
|
357
|
-
S: this,
|
|
358
|
-
C: (f, a) => f(...a)
|
|
359
|
-
});
|
|
227
|
+
log(`graceful close requested when we were in ${lastState} state? aborting`, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 256, S: this });
|
|
360
228
|
abortProtocol = true;
|
|
361
229
|
}
|
|
362
230
|
log("closing...", {
|
|
363
231
|
peerId: this.localInfo,
|
|
364
232
|
abortProtocol,
|
|
365
233
|
error
|
|
366
|
-
}, {
|
|
367
|
-
F: __dxlog_file,
|
|
368
|
-
L: 331,
|
|
369
|
-
S: this,
|
|
370
|
-
C: (f, a) => f(...a)
|
|
371
|
-
});
|
|
234
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 259, S: this });
|
|
372
235
|
try {
|
|
373
236
|
await this._closeProtocol({
|
|
374
237
|
abort: abortProtocol
|
|
375
238
|
});
|
|
376
239
|
} catch (err) {
|
|
377
|
-
log.catch(err, void 0, {
|
|
378
|
-
F: __dxlog_file,
|
|
379
|
-
L: 336,
|
|
380
|
-
S: this,
|
|
381
|
-
C: (f, a) => f(...a)
|
|
382
|
-
});
|
|
240
|
+
log.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 269, S: this });
|
|
383
241
|
}
|
|
384
242
|
try {
|
|
385
243
|
await this._closeTransport();
|
|
386
244
|
} catch (err) {
|
|
387
|
-
log.catch(err, void 0, {
|
|
388
|
-
F: __dxlog_file,
|
|
389
|
-
L: 342,
|
|
390
|
-
S: this,
|
|
391
|
-
C: (f, a) => f(...a)
|
|
392
|
-
});
|
|
245
|
+
log.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 275, S: this });
|
|
393
246
|
}
|
|
394
247
|
log("closed", {
|
|
395
248
|
peerId: this.localInfo
|
|
396
|
-
}, {
|
|
397
|
-
F: __dxlog_file,
|
|
398
|
-
L: 345,
|
|
399
|
-
S: this,
|
|
400
|
-
C: (f, a) => f(...a)
|
|
401
|
-
});
|
|
249
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 277, S: this });
|
|
402
250
|
this._changeState("CLOSED");
|
|
403
251
|
this._callbacks?.onClosed?.(error);
|
|
404
252
|
}
|
|
405
253
|
async _closeProtocol(options) {
|
|
406
|
-
log("closing protocol", options, {
|
|
407
|
-
F: __dxlog_file,
|
|
408
|
-
L: 351,
|
|
409
|
-
S: this,
|
|
410
|
-
C: (f, a) => f(...a)
|
|
411
|
-
});
|
|
254
|
+
log("closing protocol", options, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 284, S: this });
|
|
412
255
|
await Promise.race([
|
|
413
256
|
options?.abort ? this._protocol.abort() : this._protocol.close(),
|
|
414
257
|
this._protocolClosed.wait()
|
|
415
258
|
]);
|
|
416
|
-
log("protocol closed", options, {
|
|
417
|
-
F: __dxlog_file,
|
|
418
|
-
L: 353,
|
|
419
|
-
S: this,
|
|
420
|
-
C: (f, a) => f(...a)
|
|
421
|
-
});
|
|
259
|
+
log("protocol closed", options, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 289, S: this });
|
|
422
260
|
}
|
|
423
261
|
async _closeTransport() {
|
|
424
|
-
log("closing transport", void 0, {
|
|
425
|
-
F: __dxlog_file,
|
|
426
|
-
L: 357,
|
|
427
|
-
S: this,
|
|
428
|
-
C: (f, a) => f(...a)
|
|
429
|
-
});
|
|
262
|
+
log("closing transport", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 292, S: this });
|
|
430
263
|
await Promise.race([
|
|
431
264
|
this._transport?.close(),
|
|
432
265
|
this._transportClosed.wait()
|
|
433
266
|
]);
|
|
434
|
-
log("transport closed", void 0, {
|
|
435
|
-
F: __dxlog_file,
|
|
436
|
-
L: 359,
|
|
437
|
-
S: this,
|
|
438
|
-
C: (f, a) => f(...a)
|
|
439
|
-
});
|
|
267
|
+
log("transport closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 297, S: this });
|
|
440
268
|
}
|
|
441
269
|
_sendSignal(signal) {
|
|
442
270
|
this._outgoingSignalBuffer.push(signal);
|
|
443
271
|
this._signalSendTask.schedule();
|
|
444
272
|
}
|
|
445
|
-
async _flushSignalBuffer() {
|
|
273
|
+
async _flushSignalBuffer(ctx) {
|
|
446
274
|
if (this._outgoingSignalBuffer.length === 0) {
|
|
447
275
|
return;
|
|
448
276
|
}
|
|
449
277
|
try {
|
|
450
278
|
if (true) {
|
|
451
|
-
await cancelWithContext(
|
|
279
|
+
await cancelWithContext(ctx, sleep(this._signallingDelay));
|
|
452
280
|
this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
|
|
453
281
|
}
|
|
454
282
|
const signals = [
|
|
455
283
|
...this._outgoingSignalBuffer
|
|
456
284
|
];
|
|
457
285
|
this._outgoingSignalBuffer.length = 0;
|
|
458
|
-
await this._signalMessaging.signal({
|
|
286
|
+
await this._signalMessaging.signal(ctx, {
|
|
459
287
|
author: this.localInfo,
|
|
460
288
|
recipient: this.remoteInfo,
|
|
461
289
|
sessionId: this.sessionId,
|
|
@@ -472,66 +300,27 @@ var Connection = class {
|
|
|
472
300
|
}
|
|
473
301
|
log.info("signal message failed to deliver", {
|
|
474
302
|
err
|
|
475
|
-
}, {
|
|
476
|
-
F: __dxlog_file,
|
|
477
|
-
L: 399,
|
|
478
|
-
S: this,
|
|
479
|
-
C: (f, a) => f(...a)
|
|
480
|
-
});
|
|
303
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 333, S: this });
|
|
481
304
|
await this.close({
|
|
482
|
-
error: new ConnectivityError(
|
|
305
|
+
error: new ConnectivityError({
|
|
306
|
+
message: "signal message failed to deliver",
|
|
307
|
+
cause: err
|
|
308
|
+
})
|
|
483
309
|
});
|
|
484
310
|
}
|
|
485
311
|
}
|
|
486
312
|
/**
|
|
487
313
|
* Receive a signal from the remote peer.
|
|
488
314
|
*/
|
|
489
|
-
async signal(msg) {
|
|
490
|
-
invariant(msg.sessionId, void 0, {
|
|
491
|
-
F: __dxlog_file,
|
|
492
|
-
L: 408,
|
|
493
|
-
S: this,
|
|
494
|
-
A: [
|
|
495
|
-
"msg.sessionId",
|
|
496
|
-
""
|
|
497
|
-
]
|
|
498
|
-
});
|
|
315
|
+
async signal(_ctx, msg) {
|
|
316
|
+
invariant(msg.sessionId, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 347, S: this, A: ["msg.sessionId", ""] });
|
|
499
317
|
if (!msg.sessionId.equals(this.sessionId)) {
|
|
500
|
-
log("dropping signal for incorrect session id", void 0, {
|
|
501
|
-
F: __dxlog_file,
|
|
502
|
-
L: 410,
|
|
503
|
-
S: this,
|
|
504
|
-
C: (f, a) => f(...a)
|
|
505
|
-
});
|
|
318
|
+
log("dropping signal for incorrect session id", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 349, S: this });
|
|
506
319
|
return;
|
|
507
320
|
}
|
|
508
|
-
invariant(msg.data.signal || msg.data.signalBatch, void 0, {
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
S: this,
|
|
512
|
-
A: [
|
|
513
|
-
"msg.data.signal || msg.data.signalBatch",
|
|
514
|
-
""
|
|
515
|
-
]
|
|
516
|
-
});
|
|
517
|
-
invariant(msg.author.peerKey === this.remoteInfo.peerKey, void 0, {
|
|
518
|
-
F: __dxlog_file,
|
|
519
|
-
L: 414,
|
|
520
|
-
S: this,
|
|
521
|
-
A: [
|
|
522
|
-
"msg.author.peerKey === this.remoteInfo.peerKey",
|
|
523
|
-
""
|
|
524
|
-
]
|
|
525
|
-
});
|
|
526
|
-
invariant(msg.recipient.peerKey === this.localInfo.peerKey, void 0, {
|
|
527
|
-
F: __dxlog_file,
|
|
528
|
-
L: 415,
|
|
529
|
-
S: this,
|
|
530
|
-
A: [
|
|
531
|
-
"msg.recipient.peerKey === this.localInfo.peerKey",
|
|
532
|
-
""
|
|
533
|
-
]
|
|
534
|
-
});
|
|
321
|
+
invariant(msg.data.signal || msg.data.signalBatch, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 352, S: this, A: ["msg.data.signal || msg.data.signalBatch", ""] });
|
|
322
|
+
invariant(msg.author.peerKey === this.remoteInfo.peerKey, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 353, S: this, A: ["msg.author.peerKey === this.remoteInfo.peerKey", ""] });
|
|
323
|
+
invariant(msg.recipient.peerKey === this.localInfo.peerKey, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 354, S: this, A: ["msg.recipient.peerKey === this.localInfo.peerKey", ""] });
|
|
535
324
|
const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
|
|
536
325
|
msg.data.signal
|
|
537
326
|
];
|
|
@@ -547,33 +336,15 @@ var Connection = class {
|
|
|
547
336
|
peerId: this.localInfo,
|
|
548
337
|
remoteId: this.remoteInfo,
|
|
549
338
|
msg: msg.data
|
|
550
|
-
}, {
|
|
551
|
-
F: __dxlog_file,
|
|
552
|
-
L: 424,
|
|
553
|
-
S: this,
|
|
554
|
-
C: (f, a) => f(...a)
|
|
555
|
-
});
|
|
339
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 366, S: this });
|
|
556
340
|
this._incomingSignalBuffer.push(signal);
|
|
557
341
|
} else {
|
|
558
|
-
invariant(this._transport, "Connection not ready to accept signals.", {
|
|
559
|
-
F: __dxlog_file,
|
|
560
|
-
L: 427,
|
|
561
|
-
S: this,
|
|
562
|
-
A: [
|
|
563
|
-
"this._transport",
|
|
564
|
-
"'Connection not ready to accept signals.'"
|
|
565
|
-
]
|
|
566
|
-
});
|
|
342
|
+
invariant(this._transport, "Connection not ready to accept signals.", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 373, S: this, A: ["this._transport", "'Connection not ready to accept signals.'"] });
|
|
567
343
|
log("received signal", {
|
|
568
344
|
peerId: this.localInfo,
|
|
569
345
|
remoteId: this.remoteInfo,
|
|
570
346
|
msg: msg.data
|
|
571
|
-
}, {
|
|
572
|
-
F: __dxlog_file,
|
|
573
|
-
L: 428,
|
|
574
|
-
S: this,
|
|
575
|
-
C: (f, a) => f(...a)
|
|
576
|
-
});
|
|
347
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 374, S: this });
|
|
577
348
|
await this._transport.onSignal(signal);
|
|
578
349
|
}
|
|
579
350
|
}
|
|
@@ -586,21 +357,8 @@ var Connection = class {
|
|
|
586
357
|
from: this._state,
|
|
587
358
|
to: state,
|
|
588
359
|
peerId: this.localInfo
|
|
589
|
-
}, {
|
|
590
|
-
|
|
591
|
-
L: 439,
|
|
592
|
-
S: this,
|
|
593
|
-
C: (f, a) => f(...a)
|
|
594
|
-
});
|
|
595
|
-
invariant(state !== this._state, "Already in this state.", {
|
|
596
|
-
F: __dxlog_file,
|
|
597
|
-
L: 440,
|
|
598
|
-
S: this,
|
|
599
|
-
A: [
|
|
600
|
-
"state !== this._state",
|
|
601
|
-
"'Already in this state.'"
|
|
602
|
-
]
|
|
603
|
-
});
|
|
360
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 387, S: this });
|
|
361
|
+
invariant(state !== this._state, "Already in this state.", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 392, S: this, A: ["state !== this._state", "'Already in this state.'"] });
|
|
604
362
|
this._state = state;
|
|
605
363
|
this.stateChanged.emit(state);
|
|
606
364
|
}
|
|
@@ -621,54 +379,16 @@ _ts_decorate([
|
|
|
621
379
|
synchronized
|
|
622
380
|
], Connection.prototype, "close", null);
|
|
623
381
|
|
|
624
|
-
// src/signal/ice.ts
|
|
625
|
-
import { asyncTimeout } from "@dxos/async";
|
|
626
|
-
import { log as log2 } from "@dxos/log";
|
|
627
|
-
import { isNonNullable } from "@dxos/util";
|
|
628
|
-
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/signal/ice.ts";
|
|
629
|
-
var createIceProvider = (iceProviders) => {
|
|
630
|
-
let cachedIceServers;
|
|
631
|
-
return {
|
|
632
|
-
getIceServers: async () => {
|
|
633
|
-
if (cachedIceServers) {
|
|
634
|
-
return cachedIceServers;
|
|
635
|
-
}
|
|
636
|
-
cachedIceServers = (await Promise.all(iceProviders.map(({ urls }) => asyncTimeout(fetch(urls, {
|
|
637
|
-
method: "GET"
|
|
638
|
-
}), 1e4).then((response) => response.json()).catch((err) => {
|
|
639
|
-
const isDev = typeof window !== "undefined" && window.location.href.includes("localhost");
|
|
640
|
-
if (!isDev) {
|
|
641
|
-
log2.error("Failed to fetch ICE servers from provider", {
|
|
642
|
-
urls,
|
|
643
|
-
err
|
|
644
|
-
}, {
|
|
645
|
-
F: __dxlog_file2,
|
|
646
|
-
L: 30,
|
|
647
|
-
S: void 0,
|
|
648
|
-
C: (f, a) => f(...a)
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
})))).filter(isNonNullable).map(({ iceServers }) => iceServers).flat();
|
|
652
|
-
return cachedIceServers;
|
|
653
|
-
}
|
|
654
|
-
};
|
|
655
|
-
};
|
|
656
|
-
|
|
657
382
|
// src/signal/swarm-messenger.ts
|
|
658
|
-
import { Context as Context2 } from "@dxos/context";
|
|
659
383
|
import { invariant as invariant2 } from "@dxos/invariant";
|
|
660
|
-
import { PublicKey
|
|
661
|
-
import { log as
|
|
384
|
+
import { PublicKey } from "@dxos/keys";
|
|
385
|
+
import { log as log2 } from "@dxos/log";
|
|
662
386
|
import { TimeoutError as TimeoutError2 } from "@dxos/protocols";
|
|
663
387
|
import { schema } from "@dxos/protocols/proto";
|
|
664
388
|
import { ComplexMap } from "@dxos/util";
|
|
665
|
-
var
|
|
389
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
|
|
666
390
|
var SwarmMessage = schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
|
|
667
391
|
var SwarmMessenger = class {
|
|
668
|
-
_ctx = new Context2(void 0, {
|
|
669
|
-
F: __dxlog_file3,
|
|
670
|
-
L: 35
|
|
671
|
-
});
|
|
672
392
|
_sendMessage;
|
|
673
393
|
_onSignal;
|
|
674
394
|
_onOffer;
|
|
@@ -680,7 +400,7 @@ var SwarmMessenger = class {
|
|
|
680
400
|
this._onOffer = onOffer;
|
|
681
401
|
this._topic = topic;
|
|
682
402
|
}
|
|
683
|
-
async receiveMessage({ author, recipient, payload }) {
|
|
403
|
+
async receiveMessage(ctx, { author, recipient, payload }) {
|
|
684
404
|
if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
|
|
685
405
|
return;
|
|
686
406
|
}
|
|
@@ -688,18 +408,13 @@ var SwarmMessenger = class {
|
|
|
688
408
|
if (!this._topic.equals(message.topic)) {
|
|
689
409
|
return;
|
|
690
410
|
}
|
|
691
|
-
|
|
411
|
+
log2("received", {
|
|
692
412
|
from: author,
|
|
693
413
|
to: recipient,
|
|
694
414
|
msg: message
|
|
695
|
-
}, {
|
|
696
|
-
F: __dxlog_file3,
|
|
697
|
-
L: 71,
|
|
698
|
-
S: this,
|
|
699
|
-
C: (f, a) => f(...a)
|
|
700
|
-
});
|
|
415
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 35, S: this });
|
|
701
416
|
if (message.data?.offer) {
|
|
702
|
-
await this._handleOffer({
|
|
417
|
+
await this._handleOffer(ctx, {
|
|
703
418
|
author,
|
|
704
419
|
recipient,
|
|
705
420
|
message
|
|
@@ -707,77 +422,59 @@ var SwarmMessenger = class {
|
|
|
707
422
|
} else if (message.data?.answer) {
|
|
708
423
|
await this._resolveAnswers(message);
|
|
709
424
|
} else if (message.data?.signal) {
|
|
710
|
-
await this._handleSignal({
|
|
425
|
+
await this._handleSignal(ctx, {
|
|
711
426
|
author,
|
|
712
427
|
recipient,
|
|
713
428
|
message
|
|
714
429
|
});
|
|
715
430
|
} else if (message.data?.signalBatch) {
|
|
716
|
-
await this._handleSignal({
|
|
431
|
+
await this._handleSignal(ctx, {
|
|
717
432
|
author,
|
|
718
433
|
recipient,
|
|
719
434
|
message
|
|
720
435
|
});
|
|
721
436
|
} else {
|
|
722
|
-
|
|
437
|
+
log2.warn("unknown message", {
|
|
723
438
|
message
|
|
724
|
-
}, {
|
|
725
|
-
F: __dxlog_file3,
|
|
726
|
-
L: 82,
|
|
727
|
-
S: this,
|
|
728
|
-
C: (f, a) => f(...a)
|
|
729
|
-
});
|
|
439
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 61, S: this });
|
|
730
440
|
}
|
|
731
441
|
}
|
|
732
|
-
async signal(message) {
|
|
733
|
-
invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", {
|
|
734
|
-
|
|
735
|
-
L: 87,
|
|
736
|
-
S: this,
|
|
737
|
-
A: [
|
|
738
|
-
"message.data?.signal || message.data?.signalBatch",
|
|
739
|
-
"'Invalid message'"
|
|
740
|
-
]
|
|
741
|
-
});
|
|
742
|
-
await this._sendReliableMessage({
|
|
442
|
+
async signal(ctx, message) {
|
|
443
|
+
invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 67, S: this, A: ["message.data?.signal || message.data?.signalBatch", "'Invalid message'"] });
|
|
444
|
+
await this._sendReliableMessage(ctx, {
|
|
743
445
|
author: message.author,
|
|
744
446
|
recipient: message.recipient,
|
|
745
447
|
message
|
|
746
448
|
});
|
|
747
449
|
}
|
|
748
|
-
async offer(message) {
|
|
450
|
+
async offer(ctx, message) {
|
|
749
451
|
const networkMessage = {
|
|
750
452
|
...message,
|
|
751
|
-
messageId:
|
|
453
|
+
messageId: PublicKey.random()
|
|
752
454
|
};
|
|
753
455
|
return new Promise((resolve, reject) => {
|
|
754
456
|
this._offerRecords.set(networkMessage.messageId, {
|
|
755
457
|
resolve
|
|
756
458
|
});
|
|
757
|
-
this._sendReliableMessage({
|
|
459
|
+
this._sendReliableMessage(ctx, {
|
|
758
460
|
author: message.author,
|
|
759
461
|
recipient: message.recipient,
|
|
760
462
|
message: networkMessage
|
|
761
463
|
}).catch((err) => reject(err));
|
|
762
464
|
});
|
|
763
465
|
}
|
|
764
|
-
async _sendReliableMessage({ author, recipient, message }) {
|
|
466
|
+
async _sendReliableMessage(ctx, { author, recipient, message }) {
|
|
765
467
|
const networkMessage = {
|
|
766
468
|
...message,
|
|
767
469
|
// Setting unique message_id if it not specified yet.
|
|
768
|
-
messageId: message.messageId ??
|
|
470
|
+
messageId: message.messageId ?? PublicKey.random()
|
|
769
471
|
};
|
|
770
|
-
|
|
472
|
+
log2("sending", {
|
|
771
473
|
from: author,
|
|
772
474
|
to: recipient,
|
|
773
475
|
msg: networkMessage
|
|
774
|
-
}, {
|
|
775
|
-
|
|
776
|
-
L: 125,
|
|
777
|
-
S: this,
|
|
778
|
-
C: (f, a) => f(...a)
|
|
779
|
-
});
|
|
780
|
-
await this._sendMessage({
|
|
476
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 96, S: this });
|
|
477
|
+
await this._sendMessage(ctx, {
|
|
781
478
|
author,
|
|
782
479
|
recipient,
|
|
783
480
|
payload: {
|
|
@@ -787,48 +484,19 @@ var SwarmMessenger = class {
|
|
|
787
484
|
});
|
|
788
485
|
}
|
|
789
486
|
async _resolveAnswers(message) {
|
|
790
|
-
invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", {
|
|
791
|
-
F: __dxlog_file3,
|
|
792
|
-
L: 137,
|
|
793
|
-
S: this,
|
|
794
|
-
A: [
|
|
795
|
-
"message.data?.answer?.offerMessageId",
|
|
796
|
-
"'No offerMessageId'"
|
|
797
|
-
]
|
|
798
|
-
});
|
|
487
|
+
invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 111, S: this, A: ["message.data?.answer?.offerMessageId", "'No offerMessageId'"] });
|
|
799
488
|
const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
|
|
800
489
|
if (offerRecord) {
|
|
801
490
|
this._offerRecords.delete(message.data.answer.offerMessageId);
|
|
802
|
-
invariant2(message.data?.answer, "No answer", {
|
|
803
|
-
|
|
804
|
-
L: 141,
|
|
805
|
-
S: this,
|
|
806
|
-
A: [
|
|
807
|
-
"message.data?.answer",
|
|
808
|
-
"'No answer'"
|
|
809
|
-
]
|
|
810
|
-
});
|
|
811
|
-
log3("resolving", {
|
|
491
|
+
invariant2(message.data?.answer, "No answer", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 115, S: this, A: ["message.data?.answer", "'No answer'"] });
|
|
492
|
+
log2("resolving", {
|
|
812
493
|
answer: message.data.answer
|
|
813
|
-
}, {
|
|
814
|
-
F: __dxlog_file3,
|
|
815
|
-
L: 142,
|
|
816
|
-
S: this,
|
|
817
|
-
C: (f, a) => f(...a)
|
|
818
|
-
});
|
|
494
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 116, S: this });
|
|
819
495
|
offerRecord.resolve(message.data.answer);
|
|
820
496
|
}
|
|
821
497
|
}
|
|
822
|
-
async _handleOffer({ author, recipient, message }) {
|
|
823
|
-
invariant2(message.data.offer, "No offer", {
|
|
824
|
-
F: __dxlog_file3,
|
|
825
|
-
L: 156,
|
|
826
|
-
S: this,
|
|
827
|
-
A: [
|
|
828
|
-
"message.data.offer",
|
|
829
|
-
"'No offer'"
|
|
830
|
-
]
|
|
831
|
-
});
|
|
498
|
+
async _handleOffer(ctx, { author, recipient, message }) {
|
|
499
|
+
invariant2(message.data.offer, "No offer", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 123, S: this, A: ["message.data.offer", "'No offer'"] });
|
|
832
500
|
const offerMessage = {
|
|
833
501
|
author,
|
|
834
502
|
recipient,
|
|
@@ -837,10 +505,10 @@ var SwarmMessenger = class {
|
|
|
837
505
|
offer: message.data.offer
|
|
838
506
|
}
|
|
839
507
|
};
|
|
840
|
-
const answer = await this._onOffer(offerMessage);
|
|
508
|
+
const answer = await this._onOffer(ctx, offerMessage);
|
|
841
509
|
answer.offerMessageId = message.messageId;
|
|
842
510
|
try {
|
|
843
|
-
await this._sendReliableMessage({
|
|
511
|
+
await this._sendReliableMessage(ctx, {
|
|
844
512
|
author: recipient,
|
|
845
513
|
recipient: author,
|
|
846
514
|
message: {
|
|
@@ -853,45 +521,19 @@ var SwarmMessenger = class {
|
|
|
853
521
|
});
|
|
854
522
|
} catch (err) {
|
|
855
523
|
if (err instanceof TimeoutError2) {
|
|
856
|
-
|
|
524
|
+
log2.info("timeout sending answer to offer", {
|
|
857
525
|
err
|
|
858
|
-
}, {
|
|
859
|
-
F: __dxlog_file3,
|
|
860
|
-
L: 177,
|
|
861
|
-
S: this,
|
|
862
|
-
C: (f, a) => f(...a)
|
|
863
|
-
});
|
|
526
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 148, S: this });
|
|
864
527
|
} else {
|
|
865
|
-
|
|
528
|
+
log2.info("error sending answer to offer", {
|
|
866
529
|
err
|
|
867
|
-
}, {
|
|
868
|
-
F: __dxlog_file3,
|
|
869
|
-
L: 179,
|
|
870
|
-
S: this,
|
|
871
|
-
C: (f, a) => f(...a)
|
|
872
|
-
});
|
|
530
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 152, S: this });
|
|
873
531
|
}
|
|
874
532
|
}
|
|
875
533
|
}
|
|
876
|
-
async _handleSignal({ author, recipient, message }) {
|
|
877
|
-
invariant2(message.messageId, void 0, {
|
|
878
|
-
|
|
879
|
-
L: 193,
|
|
880
|
-
S: this,
|
|
881
|
-
A: [
|
|
882
|
-
"message.messageId",
|
|
883
|
-
""
|
|
884
|
-
]
|
|
885
|
-
});
|
|
886
|
-
invariant2(message.data.signal || message.data.signalBatch, "Invalid message", {
|
|
887
|
-
F: __dxlog_file3,
|
|
888
|
-
L: 194,
|
|
889
|
-
S: this,
|
|
890
|
-
A: [
|
|
891
|
-
"message.data.signal || message.data.signalBatch",
|
|
892
|
-
"'Invalid message'"
|
|
893
|
-
]
|
|
894
|
-
});
|
|
534
|
+
async _handleSignal(ctx, { author, recipient, message }) {
|
|
535
|
+
invariant2(message.messageId, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 159, S: this, A: ["message.messageId", ""] });
|
|
536
|
+
invariant2(message.data.signal || message.data.signalBatch, "Invalid message", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 160, S: this, A: ["message.data.signal || message.data.signalBatch", "'Invalid message'"] });
|
|
895
537
|
const signalMessage = {
|
|
896
538
|
author,
|
|
897
539
|
recipient,
|
|
@@ -901,38 +543,39 @@ var SwarmMessenger = class {
|
|
|
901
543
|
signalBatch: message.data.signalBatch
|
|
902
544
|
}
|
|
903
545
|
};
|
|
904
|
-
await this._onSignal(signalMessage);
|
|
546
|
+
await this._onSignal(ctx, signalMessage);
|
|
905
547
|
}
|
|
906
548
|
};
|
|
907
549
|
|
|
908
550
|
// src/swarm/swarm.ts
|
|
909
551
|
import { Event as Event3, scheduleTask as scheduleTask3, sleep as sleep2, synchronized as synchronized3 } from "@dxos/async";
|
|
910
|
-
import { Context as
|
|
552
|
+
import { Context as Context3 } from "@dxos/context";
|
|
911
553
|
import { ErrorStream as ErrorStream2 } from "@dxos/debug";
|
|
912
554
|
import { invariant as invariant4 } from "@dxos/invariant";
|
|
913
|
-
import { PublicKey as
|
|
914
|
-
import { log as
|
|
555
|
+
import { PublicKey as PublicKey3 } from "@dxos/keys";
|
|
556
|
+
import { log as log4, logInfo as logInfo2 } from "@dxos/log";
|
|
915
557
|
import { PeerInfoHash } from "@dxos/messaging";
|
|
916
|
-
import {
|
|
917
|
-
import { ComplexMap as ComplexMap2, isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
558
|
+
import { ComplexMap as ComplexMap2, isNonNullable } from "@dxos/util";
|
|
918
559
|
|
|
919
560
|
// src/swarm/peer.ts
|
|
920
561
|
import { Event as Event2, scheduleTask as scheduleTask2, synchronized as synchronized2 } from "@dxos/async";
|
|
921
|
-
import { Context as
|
|
562
|
+
import { Context as Context2 } from "@dxos/context";
|
|
922
563
|
import { invariant as invariant3 } from "@dxos/invariant";
|
|
923
|
-
import { PublicKey as
|
|
924
|
-
import { log as
|
|
564
|
+
import { PublicKey as PublicKey2 } from "@dxos/keys";
|
|
565
|
+
import { log as log3 } from "@dxos/log";
|
|
925
566
|
import { CancelledError as CancelledError2, SystemError } from "@dxos/protocols";
|
|
567
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
|
|
926
568
|
function _ts_decorate2(decorators, target, key, desc) {
|
|
927
569
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
928
570
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
929
571
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
930
572
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
931
573
|
}
|
|
932
|
-
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
|
|
933
574
|
var ConnectionDisplacedError = class extends SystemError {
|
|
934
575
|
constructor() {
|
|
935
|
-
super(
|
|
576
|
+
super({
|
|
577
|
+
message: "Connection displaced by remote initiator."
|
|
578
|
+
});
|
|
936
579
|
}
|
|
937
580
|
};
|
|
938
581
|
var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
|
|
@@ -948,18 +591,18 @@ var Peer = class {
|
|
|
948
591
|
/**
|
|
949
592
|
* Will be available to connect after this time.
|
|
950
593
|
*/
|
|
951
|
-
_availableAfter;
|
|
952
|
-
availableToConnect;
|
|
594
|
+
_availableAfter = 0;
|
|
595
|
+
availableToConnect = true;
|
|
953
596
|
_lastConnectionTime;
|
|
954
|
-
_ctx;
|
|
597
|
+
_ctx = new Context2(void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 44 });
|
|
955
598
|
_connectionCtx;
|
|
956
599
|
connection;
|
|
957
600
|
/**
|
|
958
601
|
* Whether the peer is currently advertizing itself on the signal-network.
|
|
959
602
|
*/
|
|
960
|
-
advertizing;
|
|
961
|
-
initiating;
|
|
962
|
-
connectionDisplaced;
|
|
603
|
+
advertizing = false;
|
|
604
|
+
initiating = false;
|
|
605
|
+
connectionDisplaced = new Event2();
|
|
963
606
|
constructor(remoteInfo, topic, localInfo, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
|
|
964
607
|
this.remoteInfo = remoteInfo;
|
|
965
608
|
this.topic = topic;
|
|
@@ -969,49 +612,30 @@ var Peer = class {
|
|
|
969
612
|
this._transportFactory = _transportFactory;
|
|
970
613
|
this._connectionLimiter = _connectionLimiter;
|
|
971
614
|
this._callbacks = _callbacks;
|
|
972
|
-
this._availableAfter = 0;
|
|
973
|
-
this.availableToConnect = true;
|
|
974
|
-
this._ctx = new Context3(void 0, {
|
|
975
|
-
F: __dxlog_file4,
|
|
976
|
-
L: 81
|
|
977
|
-
});
|
|
978
|
-
this.advertizing = false;
|
|
979
|
-
this.initiating = false;
|
|
980
|
-
this.connectionDisplaced = new Event2();
|
|
981
615
|
}
|
|
982
616
|
/**
|
|
983
617
|
* Respond to remote offer.
|
|
984
618
|
*/
|
|
985
|
-
async onOffer(message) {
|
|
619
|
+
async onOffer(_ctx, message) {
|
|
986
620
|
const remote = message.author;
|
|
987
621
|
if (this.connection && ![
|
|
988
622
|
ConnectionState.CREATED,
|
|
989
623
|
ConnectionState.INITIAL,
|
|
990
624
|
ConnectionState.CONNECTING
|
|
991
625
|
].includes(this.connection.state)) {
|
|
992
|
-
|
|
993
|
-
F: __dxlog_file4,
|
|
994
|
-
L: 116,
|
|
995
|
-
S: this,
|
|
996
|
-
C: (f, a) => f(...a)
|
|
997
|
-
});
|
|
626
|
+
log3.info(`received offer when connection already in ${this.connection.state} state`, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 71, S: this });
|
|
998
627
|
return {
|
|
999
628
|
accept: false
|
|
1000
629
|
};
|
|
1001
630
|
}
|
|
1002
631
|
if (this.connection || this.initiating) {
|
|
1003
632
|
if (remote.peerKey < this.localInfo.peerKey) {
|
|
1004
|
-
|
|
633
|
+
log3("close local connection", {
|
|
1005
634
|
localPeer: this.localInfo,
|
|
1006
635
|
topic: this.topic,
|
|
1007
636
|
remotePeer: this.remoteInfo,
|
|
1008
637
|
sessionId: this.connection?.sessionId
|
|
1009
|
-
}, {
|
|
1010
|
-
F: __dxlog_file4,
|
|
1011
|
-
L: 125,
|
|
1012
|
-
S: this,
|
|
1013
|
-
C: (f, a) => f(...a)
|
|
1014
|
-
});
|
|
638
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 82, S: this });
|
|
1015
639
|
if (this.connection) {
|
|
1016
640
|
await this.closeConnection(new ConnectionDisplacedError());
|
|
1017
641
|
}
|
|
@@ -1023,15 +647,7 @@ var Peer = class {
|
|
|
1023
647
|
}
|
|
1024
648
|
if (await this._callbacks.onOffer(remote)) {
|
|
1025
649
|
if (!this.connection) {
|
|
1026
|
-
invariant3(message.sessionId, void 0, {
|
|
1027
|
-
F: __dxlog_file4,
|
|
1028
|
-
L: 145,
|
|
1029
|
-
S: this,
|
|
1030
|
-
A: [
|
|
1031
|
-
"message.sessionId",
|
|
1032
|
-
""
|
|
1033
|
-
]
|
|
1034
|
-
});
|
|
650
|
+
invariant3(message.sessionId, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 102, S: this, A: ["message.sessionId", ""] });
|
|
1035
651
|
const connection = this._createConnection(false, message.sessionId);
|
|
1036
652
|
try {
|
|
1037
653
|
await this._connectionLimiter.connecting(message.sessionId);
|
|
@@ -1039,17 +655,12 @@ var Peer = class {
|
|
|
1039
655
|
await connection.openConnection();
|
|
1040
656
|
} catch (err) {
|
|
1041
657
|
if (!(err instanceof CancelledError2)) {
|
|
1042
|
-
|
|
658
|
+
log3.info("connection error", {
|
|
1043
659
|
topic: this.topic,
|
|
1044
660
|
peerId: this.localInfo,
|
|
1045
661
|
remoteId: this.remoteInfo,
|
|
1046
662
|
err
|
|
1047
|
-
}, {
|
|
1048
|
-
F: __dxlog_file4,
|
|
1049
|
-
L: 155,
|
|
1050
|
-
S: this,
|
|
1051
|
-
C: (f, a) => f(...a)
|
|
1052
|
-
});
|
|
663
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 110, S: this });
|
|
1053
664
|
}
|
|
1054
665
|
await this.closeConnection(err);
|
|
1055
666
|
}
|
|
@@ -1065,44 +676,23 @@ var Peer = class {
|
|
|
1065
676
|
/**
|
|
1066
677
|
* Initiate a connection to the remote peer.
|
|
1067
678
|
*/
|
|
1068
|
-
async initiateConnection() {
|
|
1069
|
-
invariant3(!this.initiating, "Initiation in progress.", {
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
A: [
|
|
1074
|
-
"!this.initiating",
|
|
1075
|
-
"'Initiation in progress.'"
|
|
1076
|
-
]
|
|
1077
|
-
});
|
|
1078
|
-
invariant3(!this.connection, "Already connected.", {
|
|
1079
|
-
F: __dxlog_file4,
|
|
1080
|
-
L: 173,
|
|
1081
|
-
S: this,
|
|
1082
|
-
A: [
|
|
1083
|
-
"!this.connection",
|
|
1084
|
-
"'Already connected.'"
|
|
1085
|
-
]
|
|
1086
|
-
});
|
|
1087
|
-
const sessionId = PublicKey3.random();
|
|
1088
|
-
log4("initiating...", {
|
|
679
|
+
async initiateConnection(ctx) {
|
|
680
|
+
invariant3(!this.initiating, "Initiation in progress.", { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 132, S: this, A: ["!this.initiating", "'Initiation in progress.'"] });
|
|
681
|
+
invariant3(!this.connection, "Already connected.", { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 133, S: this, A: ["!this.connection", "'Already connected.'"] });
|
|
682
|
+
const sessionId = PublicKey2.random();
|
|
683
|
+
log3("initiating...", {
|
|
1089
684
|
local: this.localInfo,
|
|
1090
685
|
topic: this.topic,
|
|
1091
686
|
remote: this.remoteInfo,
|
|
1092
687
|
sessionId
|
|
1093
|
-
}, {
|
|
1094
|
-
F: __dxlog_file4,
|
|
1095
|
-
L: 175,
|
|
1096
|
-
S: this,
|
|
1097
|
-
C: (f, a) => f(...a)
|
|
1098
|
-
});
|
|
688
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 135, S: this });
|
|
1099
689
|
const connection = this._createConnection(true, sessionId);
|
|
1100
690
|
this.initiating = true;
|
|
1101
691
|
let answer;
|
|
1102
692
|
try {
|
|
1103
693
|
await this._connectionLimiter.connecting(sessionId);
|
|
1104
694
|
connection.initiate();
|
|
1105
|
-
answer = await this._signalMessaging.offer({
|
|
695
|
+
answer = await this._signalMessaging.offer(ctx, {
|
|
1106
696
|
author: this.localInfo,
|
|
1107
697
|
recipient: this.remoteInfo,
|
|
1108
698
|
sessionId,
|
|
@@ -1111,38 +701,23 @@ var Peer = class {
|
|
|
1111
701
|
offer: {}
|
|
1112
702
|
}
|
|
1113
703
|
});
|
|
1114
|
-
|
|
704
|
+
log3("received", {
|
|
1115
705
|
answer,
|
|
1116
706
|
topic: this.topic,
|
|
1117
707
|
local: this.localInfo,
|
|
1118
708
|
remote: this.remoteInfo
|
|
1119
|
-
}, {
|
|
1120
|
-
F: __dxlog_file4,
|
|
1121
|
-
L: 192,
|
|
1122
|
-
S: this,
|
|
1123
|
-
C: (f, a) => f(...a)
|
|
1124
|
-
});
|
|
709
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 156, S: this });
|
|
1125
710
|
if (connection.state !== ConnectionState.INITIAL) {
|
|
1126
|
-
|
|
1127
|
-
F: __dxlog_file4,
|
|
1128
|
-
L: 194,
|
|
1129
|
-
S: this,
|
|
1130
|
-
C: (f, a) => f(...a)
|
|
1131
|
-
});
|
|
711
|
+
log3("ignoring response", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 163, S: this });
|
|
1132
712
|
return;
|
|
1133
713
|
}
|
|
1134
714
|
} catch (err) {
|
|
1135
|
-
|
|
715
|
+
log3("initiation error: send offer", {
|
|
1136
716
|
err,
|
|
1137
717
|
topic: this.topic,
|
|
1138
718
|
local: this.localInfo,
|
|
1139
719
|
remote: this.remoteInfo
|
|
1140
|
-
}, {
|
|
1141
|
-
F: __dxlog_file4,
|
|
1142
|
-
L: 198,
|
|
1143
|
-
S: this,
|
|
1144
|
-
C: (f, a) => f(...a)
|
|
1145
|
-
});
|
|
720
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 167, S: this });
|
|
1146
721
|
await connection.abort(err);
|
|
1147
722
|
throw err;
|
|
1148
723
|
} finally {
|
|
@@ -1154,51 +729,31 @@ var Peer = class {
|
|
|
1154
729
|
return;
|
|
1155
730
|
}
|
|
1156
731
|
} catch (err) {
|
|
1157
|
-
|
|
732
|
+
log3("initiation error: accept answer", {
|
|
1158
733
|
err,
|
|
1159
734
|
topic: this.topic,
|
|
1160
735
|
local: this.localInfo,
|
|
1161
736
|
remote: this.remoteInfo
|
|
1162
|
-
}, {
|
|
1163
|
-
F: __dxlog_file4,
|
|
1164
|
-
L: 211,
|
|
1165
|
-
S: this,
|
|
1166
|
-
C: (f, a) => f(...a)
|
|
1167
|
-
});
|
|
737
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 184, S: this });
|
|
1168
738
|
await connection.abort(err);
|
|
1169
739
|
throw err;
|
|
1170
740
|
} finally {
|
|
1171
741
|
this.initiating = false;
|
|
1172
742
|
}
|
|
1173
743
|
try {
|
|
1174
|
-
|
|
1175
|
-
F: __dxlog_file4,
|
|
1176
|
-
L: 224,
|
|
1177
|
-
S: this,
|
|
1178
|
-
C: (f, a) => f(...a)
|
|
1179
|
-
});
|
|
744
|
+
log3("opening connection as initiator", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 196, S: this });
|
|
1180
745
|
await connection.openConnection();
|
|
1181
746
|
this._callbacks.onAccepted();
|
|
1182
747
|
} catch (err) {
|
|
1183
|
-
|
|
748
|
+
log3("initiation error: open connection", {
|
|
1184
749
|
err,
|
|
1185
750
|
topic: this.topic,
|
|
1186
751
|
local: this.localInfo,
|
|
1187
752
|
remote: this.remoteInfo
|
|
1188
|
-
}, {
|
|
1189
|
-
|
|
1190
|
-
L: 228,
|
|
1191
|
-
S: this,
|
|
1192
|
-
C: (f, a) => f(...a)
|
|
1193
|
-
});
|
|
1194
|
-
log4.warn("closing connection due to unhandled error on openConnection", {
|
|
753
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 200, S: this });
|
|
754
|
+
log3.warn("closing connection due to unhandled error on openConnection", {
|
|
1195
755
|
err
|
|
1196
|
-
}, {
|
|
1197
|
-
F: __dxlog_file4,
|
|
1198
|
-
L: 235,
|
|
1199
|
-
S: this,
|
|
1200
|
-
C: (f, a) => f(...a)
|
|
1201
|
-
});
|
|
756
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 207, S: this });
|
|
1202
757
|
await this.closeConnection(err);
|
|
1203
758
|
throw err;
|
|
1204
759
|
} finally {
|
|
@@ -1210,27 +765,14 @@ var Peer = class {
|
|
|
1210
765
|
* Either we're initiating a connection or creating one in response to an offer from the other peer.
|
|
1211
766
|
*/
|
|
1212
767
|
_createConnection(initiator, sessionId) {
|
|
1213
|
-
|
|
768
|
+
log3("creating connection", {
|
|
1214
769
|
topic: this.topic,
|
|
1215
770
|
peerId: this.localInfo,
|
|
1216
771
|
remoteId: this.remoteInfo,
|
|
1217
772
|
initiator,
|
|
1218
773
|
sessionId
|
|
1219
|
-
}, {
|
|
1220
|
-
|
|
1221
|
-
L: 249,
|
|
1222
|
-
S: this,
|
|
1223
|
-
C: (f, a) => f(...a)
|
|
1224
|
-
});
|
|
1225
|
-
invariant3(!this.connection, "Already connected.", {
|
|
1226
|
-
F: __dxlog_file4,
|
|
1227
|
-
L: 256,
|
|
1228
|
-
S: this,
|
|
1229
|
-
A: [
|
|
1230
|
-
"!this.connection",
|
|
1231
|
-
"'Already connected.'"
|
|
1232
|
-
]
|
|
1233
|
-
});
|
|
774
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 221, S: this });
|
|
775
|
+
invariant3(!this.connection, "Already connected.", { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 228, S: this, A: ["!this.connection", "'Already connected.'"] });
|
|
1234
776
|
const connection = new Connection(
|
|
1235
777
|
this.topic,
|
|
1236
778
|
this.localInfo,
|
|
@@ -1241,8 +783,8 @@ var Peer = class {
|
|
|
1241
783
|
// TODO(dmaretskyi): Init only when connection is established.
|
|
1242
784
|
this._protocolProvider({
|
|
1243
785
|
initiator,
|
|
1244
|
-
localPeerId:
|
|
1245
|
-
remotePeerId:
|
|
786
|
+
localPeerId: PublicKey2.from(this.localInfo.peerKey),
|
|
787
|
+
remotePeerId: PublicKey2.from(this.remoteInfo.peerKey),
|
|
1246
788
|
topic: this.topic
|
|
1247
789
|
}),
|
|
1248
790
|
this._transportFactory,
|
|
@@ -1252,18 +794,13 @@ var Peer = class {
|
|
|
1252
794
|
this._lastConnectionTime = Date.now();
|
|
1253
795
|
this._callbacks.onConnected();
|
|
1254
796
|
this._connectionLimiter.doneConnecting(sessionId);
|
|
1255
|
-
|
|
797
|
+
log3.trace("dxos.mesh.connection.connected", {
|
|
1256
798
|
topic: this.topic,
|
|
1257
799
|
localPeerId: this.localInfo,
|
|
1258
800
|
remotePeerId: this.remoteInfo,
|
|
1259
801
|
sessionId,
|
|
1260
802
|
initiator
|
|
1261
|
-
}, {
|
|
1262
|
-
F: __dxlog_file4,
|
|
1263
|
-
L: 280,
|
|
1264
|
-
S: this,
|
|
1265
|
-
C: (f, a) => f(...a)
|
|
1266
|
-
});
|
|
803
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 241, S: this });
|
|
1267
804
|
},
|
|
1268
805
|
onClosed: (err) => {
|
|
1269
806
|
const logMeta = {
|
|
@@ -1272,34 +809,16 @@ var Peer = class {
|
|
|
1272
809
|
remoteId: this.remoteInfo,
|
|
1273
810
|
initiator
|
|
1274
811
|
};
|
|
1275
|
-
|
|
1276
|
-
F: __dxlog_file4,
|
|
1277
|
-
L: 290,
|
|
1278
|
-
S: this,
|
|
1279
|
-
C: (f, a) => f(...a)
|
|
1280
|
-
});
|
|
812
|
+
log3("connection closed", logMeta, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 256, S: this });
|
|
1281
813
|
this._connectionLimiter.doneConnecting(sessionId);
|
|
1282
|
-
invariant3(this.connection === connection, "Connection mismatch (race condition).", {
|
|
1283
|
-
|
|
1284
|
-
L: 295,
|
|
1285
|
-
S: this,
|
|
1286
|
-
A: [
|
|
1287
|
-
"this.connection === connection",
|
|
1288
|
-
"'Connection mismatch (race condition).'"
|
|
1289
|
-
]
|
|
1290
|
-
});
|
|
1291
|
-
log4.trace("dxos.mesh.connection.closed", {
|
|
814
|
+
invariant3(this.connection === connection, "Connection mismatch (race condition).", { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 259, S: this, A: ["this.connection === connection", "'Connection mismatch (race condition).'"] });
|
|
815
|
+
log3.trace("dxos.mesh.connection.closed", {
|
|
1292
816
|
topic: this.topic,
|
|
1293
817
|
localPeerId: this.localInfo,
|
|
1294
818
|
remotePeerId: this.remoteInfo,
|
|
1295
819
|
sessionId,
|
|
1296
820
|
initiator
|
|
1297
|
-
}, {
|
|
1298
|
-
F: __dxlog_file4,
|
|
1299
|
-
L: 297,
|
|
1300
|
-
S: this,
|
|
1301
|
-
C: (f, a) => f(...a)
|
|
1302
|
-
});
|
|
821
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 260, S: this });
|
|
1303
822
|
if (err instanceof ConnectionDisplacedError) {
|
|
1304
823
|
this.connectionDisplaced.emit(this.connection);
|
|
1305
824
|
} else {
|
|
@@ -1311,12 +830,7 @@ var Peer = class {
|
|
|
1311
830
|
}
|
|
1312
831
|
this._callbacks.onDisconnected();
|
|
1313
832
|
scheduleTask2(this._connectionCtx, () => {
|
|
1314
|
-
|
|
1315
|
-
F: __dxlog_file4,
|
|
1316
|
-
L: 321,
|
|
1317
|
-
S: this,
|
|
1318
|
-
C: (f, a) => f(...a)
|
|
1319
|
-
});
|
|
833
|
+
log3("peer became available", logMeta, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 279, S: this });
|
|
1320
834
|
this.availableToConnect = true;
|
|
1321
835
|
this._callbacks.onPeerAvailable();
|
|
1322
836
|
}, this._availableAfter);
|
|
@@ -1329,31 +843,21 @@ var Peer = class {
|
|
|
1329
843
|
void this._connectionCtx?.dispose();
|
|
1330
844
|
this._connectionCtx = this._ctx.derive();
|
|
1331
845
|
connection.errors.handle((err) => {
|
|
1332
|
-
|
|
846
|
+
log3.info("connection error, closing", {
|
|
1333
847
|
topic: this.topic,
|
|
1334
848
|
peerId: this.localInfo,
|
|
1335
849
|
remoteId: this.remoteInfo,
|
|
1336
850
|
initiator,
|
|
1337
851
|
err
|
|
1338
|
-
}, {
|
|
1339
|
-
|
|
1340
|
-
L: 339,
|
|
1341
|
-
S: this,
|
|
1342
|
-
C: (f, a) => f(...a)
|
|
1343
|
-
});
|
|
1344
|
-
log4.trace("dxos.mesh.connection.error", {
|
|
852
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 291, S: this });
|
|
853
|
+
log3.trace("dxos.mesh.connection.error", {
|
|
1345
854
|
topic: this.topic,
|
|
1346
855
|
localPeerId: this.localInfo,
|
|
1347
856
|
remotePeerId: this.remoteInfo,
|
|
1348
857
|
sessionId,
|
|
1349
858
|
initiator,
|
|
1350
859
|
err
|
|
1351
|
-
}, {
|
|
1352
|
-
F: __dxlog_file4,
|
|
1353
|
-
L: 346,
|
|
1354
|
-
S: this,
|
|
1355
|
-
C: (f, a) => f(...a)
|
|
1356
|
-
});
|
|
860
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 298, S: this });
|
|
1357
861
|
void this.closeConnection(err);
|
|
1358
862
|
});
|
|
1359
863
|
this.connection = connection;
|
|
@@ -1364,53 +868,33 @@ var Peer = class {
|
|
|
1364
868
|
return;
|
|
1365
869
|
}
|
|
1366
870
|
const connection = this.connection;
|
|
1367
|
-
|
|
871
|
+
log3("closing...", {
|
|
1368
872
|
peerId: this.remoteInfo,
|
|
1369
873
|
sessionId: connection.sessionId
|
|
1370
|
-
}, {
|
|
1371
|
-
F: __dxlog_file4,
|
|
1372
|
-
L: 371,
|
|
1373
|
-
S: this,
|
|
1374
|
-
C: (f, a) => f(...a)
|
|
1375
|
-
});
|
|
874
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 317, S: this });
|
|
1376
875
|
await connection.close({
|
|
1377
876
|
error: err
|
|
1378
877
|
});
|
|
1379
|
-
|
|
878
|
+
log3("closed", {
|
|
1380
879
|
peerId: this.remoteInfo,
|
|
1381
880
|
sessionId: connection.sessionId
|
|
1382
|
-
}, {
|
|
1383
|
-
F: __dxlog_file4,
|
|
1384
|
-
L: 377,
|
|
1385
|
-
S: this,
|
|
1386
|
-
C: (f, a) => f(...a)
|
|
1387
|
-
});
|
|
881
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 326, S: this });
|
|
1388
882
|
}
|
|
1389
|
-
async onSignal(message) {
|
|
883
|
+
async onSignal(ctx, message) {
|
|
1390
884
|
if (!this.connection) {
|
|
1391
|
-
|
|
885
|
+
log3("dropping signal message for non-existent connection", {
|
|
1392
886
|
message
|
|
1393
|
-
}, {
|
|
1394
|
-
F: __dxlog_file4,
|
|
1395
|
-
L: 382,
|
|
1396
|
-
S: this,
|
|
1397
|
-
C: (f, a) => f(...a)
|
|
1398
|
-
});
|
|
887
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 333, S: this });
|
|
1399
888
|
return;
|
|
1400
889
|
}
|
|
1401
|
-
await this.connection.signal(message);
|
|
890
|
+
await this.connection.signal(ctx, message);
|
|
1402
891
|
}
|
|
1403
892
|
async safeDestroy(reason) {
|
|
1404
893
|
await this._ctx.dispose();
|
|
1405
|
-
|
|
894
|
+
log3("Destroying peer", {
|
|
1406
895
|
peerId: this.remoteInfo,
|
|
1407
896
|
topic: this.topic
|
|
1408
|
-
}, {
|
|
1409
|
-
F: __dxlog_file4,
|
|
1410
|
-
L: 392,
|
|
1411
|
-
S: this,
|
|
1412
|
-
C: (f, a) => f(...a)
|
|
1413
|
-
});
|
|
897
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 342, S: this });
|
|
1414
898
|
await this?.connection?.close({
|
|
1415
899
|
reason
|
|
1416
900
|
});
|
|
@@ -1433,13 +917,13 @@ var increaseInterval = (interval) => {
|
|
|
1433
917
|
};
|
|
1434
918
|
|
|
1435
919
|
// src/swarm/swarm.ts
|
|
920
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
|
|
1436
921
|
function _ts_decorate3(decorators, target, key, desc) {
|
|
1437
922
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1438
923
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1439
924
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
1440
925
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1441
926
|
}
|
|
1442
|
-
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
|
|
1443
927
|
var INITIATION_DELAY = 100;
|
|
1444
928
|
var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
|
|
1445
929
|
var Swarm = class {
|
|
@@ -1453,33 +937,33 @@ var Swarm = class {
|
|
|
1453
937
|
_connectionLimiter;
|
|
1454
938
|
_initiationDelay;
|
|
1455
939
|
_swarmMessenger;
|
|
1456
|
-
_ctx;
|
|
1457
|
-
_listeningHandle;
|
|
940
|
+
_ctx = new Context3(void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 39 });
|
|
941
|
+
_listeningHandle = void 0;
|
|
1458
942
|
/**
|
|
1459
943
|
* PeerInfo -> Peer.
|
|
1460
944
|
* @internal
|
|
1461
945
|
*/
|
|
1462
|
-
_peers;
|
|
946
|
+
_peers = new ComplexMap2(PeerInfoHash);
|
|
1463
947
|
/**
|
|
1464
948
|
* Unique id of the swarm, local to the current peer, generated when swarm is joined.
|
|
1465
949
|
*/
|
|
1466
|
-
_instanceId;
|
|
950
|
+
_instanceId = PublicKey3.random().toHex();
|
|
1467
951
|
/**
|
|
1468
952
|
* New connection to a peer is started.
|
|
1469
953
|
* @internal
|
|
1470
954
|
*/
|
|
1471
|
-
connectionAdded;
|
|
955
|
+
connectionAdded = new Event3();
|
|
1472
956
|
/**
|
|
1473
957
|
* Connection to a peer is dropped.
|
|
1474
958
|
* @internal
|
|
1475
959
|
*/
|
|
1476
|
-
disconnected;
|
|
960
|
+
disconnected = new Event3();
|
|
1477
961
|
/**
|
|
1478
962
|
* Connection is established to a new peer.
|
|
1479
963
|
* @internal
|
|
1480
964
|
*/
|
|
1481
|
-
connected;
|
|
1482
|
-
errors;
|
|
965
|
+
connected = new Event3();
|
|
966
|
+
errors = new ErrorStream2();
|
|
1483
967
|
// TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
|
|
1484
968
|
// TODO(burdon): Pass in object.
|
|
1485
969
|
constructor(_topic, _ownPeer, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter, _initiationDelay = INITIATION_DELAY) {
|
|
@@ -1492,58 +976,23 @@ var Swarm = class {
|
|
|
1492
976
|
this._label = _label;
|
|
1493
977
|
this._connectionLimiter = _connectionLimiter;
|
|
1494
978
|
this._initiationDelay = _initiationDelay;
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
});
|
|
1499
|
-
this._listeningHandle = void 0;
|
|
1500
|
-
this._peers = new ComplexMap2(PeerInfoHash);
|
|
1501
|
-
this._instanceId = PublicKey4.random().toHex();
|
|
1502
|
-
this.connectionAdded = new Event3();
|
|
1503
|
-
this.disconnected = new Event3();
|
|
1504
|
-
this.connected = new Event3();
|
|
1505
|
-
this.errors = new ErrorStream2();
|
|
1506
|
-
log5.trace("dxos.mesh.swarm.constructor", trace2.begin({
|
|
1507
|
-
id: this._instanceId,
|
|
1508
|
-
data: {
|
|
1509
|
-
topic: this._topic.toHex(),
|
|
1510
|
-
peer: this._ownPeer
|
|
1511
|
-
}
|
|
1512
|
-
}), {
|
|
1513
|
-
F: __dxlog_file5,
|
|
1514
|
-
L: 89,
|
|
1515
|
-
S: this,
|
|
1516
|
-
C: (f, a) => f(...a)
|
|
1517
|
-
});
|
|
1518
|
-
log5("creating swarm", {
|
|
1519
|
-
peerId: _ownPeer
|
|
1520
|
-
}, {
|
|
1521
|
-
F: __dxlog_file5,
|
|
1522
|
-
L: 93,
|
|
1523
|
-
S: this,
|
|
1524
|
-
C: (f, a) => f(...a)
|
|
1525
|
-
});
|
|
979
|
+
log4("creating swarm", {
|
|
980
|
+
topic: this._topic.toHex(),
|
|
981
|
+
peer: this._ownPeer
|
|
982
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 73, S: this });
|
|
1526
983
|
_topology.init(this._getSwarmController());
|
|
1527
984
|
this._swarmMessenger = new SwarmMessenger({
|
|
1528
|
-
sendMessage: async (msg) => await this._messenger.sendMessage(msg),
|
|
1529
|
-
onSignal: async (msg) => await this.onSignal(msg),
|
|
1530
|
-
onOffer: async (msg) => await this.onOffer(msg),
|
|
985
|
+
sendMessage: async (ctx, msg) => await this._messenger.sendMessage(ctx, msg),
|
|
986
|
+
onSignal: async (ctx, msg) => await this.onSignal(ctx, msg),
|
|
987
|
+
onOffer: async (ctx, msg) => await this.onOffer(ctx, msg),
|
|
1531
988
|
topic: this._topic
|
|
1532
989
|
});
|
|
1533
|
-
log5.trace("dxos.mesh.swarm.constructor", trace2.end({
|
|
1534
|
-
id: this._instanceId
|
|
1535
|
-
}), {
|
|
1536
|
-
F: __dxlog_file5,
|
|
1537
|
-
L: 102,
|
|
1538
|
-
S: this,
|
|
1539
|
-
C: (f, a) => f(...a)
|
|
1540
|
-
});
|
|
1541
990
|
}
|
|
1542
991
|
get connections() {
|
|
1543
|
-
return Array.from(this._peers.values()).map((peer) => peer.connection).filter(
|
|
992
|
+
return Array.from(this._peers.values()).map((peer) => peer.connection).filter(isNonNullable);
|
|
1544
993
|
}
|
|
1545
994
|
get ownPeerId() {
|
|
1546
|
-
return
|
|
995
|
+
return PublicKey3.from(this._ownPeer.peerKey);
|
|
1547
996
|
}
|
|
1548
997
|
get ownPeer() {
|
|
1549
998
|
return this._ownPeer;
|
|
@@ -1558,105 +1007,54 @@ var Swarm = class {
|
|
|
1558
1007
|
return this._topic;
|
|
1559
1008
|
}
|
|
1560
1009
|
async open() {
|
|
1561
|
-
invariant4(!this._listeningHandle, void 0, {
|
|
1562
|
-
F: __dxlog_file5,
|
|
1563
|
-
L: 133,
|
|
1564
|
-
S: this,
|
|
1565
|
-
A: [
|
|
1566
|
-
"!this._listeningHandle",
|
|
1567
|
-
""
|
|
1568
|
-
]
|
|
1569
|
-
});
|
|
1010
|
+
invariant4(!this._listeningHandle, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 103, S: this, A: ["!this._listeningHandle", ""] });
|
|
1570
1011
|
this._listeningHandle = await this._messenger.listen({
|
|
1571
1012
|
peer: this._ownPeer,
|
|
1572
1013
|
payloadType: "dxos.mesh.swarm.SwarmMessage",
|
|
1573
1014
|
onMessage: async (message) => {
|
|
1574
|
-
await this._swarmMessenger.receiveMessage(message).catch((err) =>
|
|
1015
|
+
await this._swarmMessenger.receiveMessage(this._ctx, message).catch((err) => log4.info("Error while receiving message", {
|
|
1575
1016
|
err
|
|
1576
|
-
}, {
|
|
1577
|
-
F: __dxlog_file5,
|
|
1578
|
-
L: 141,
|
|
1579
|
-
S: this,
|
|
1580
|
-
C: (f, a) => f(...a)
|
|
1581
|
-
}));
|
|
1017
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 109, S: this }));
|
|
1582
1018
|
}
|
|
1583
1019
|
});
|
|
1584
1020
|
}
|
|
1585
1021
|
async destroy() {
|
|
1586
|
-
|
|
1587
|
-
F: __dxlog_file5,
|
|
1588
|
-
L: 147,
|
|
1589
|
-
S: this,
|
|
1590
|
-
C: (f, a) => f(...a)
|
|
1591
|
-
});
|
|
1022
|
+
log4("destroying...", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 116, S: this });
|
|
1592
1023
|
await this._listeningHandle?.unsubscribe();
|
|
1593
1024
|
this._listeningHandle = void 0;
|
|
1594
1025
|
await this._ctx.dispose();
|
|
1595
1026
|
await this._topology.destroy();
|
|
1596
1027
|
await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key, "swarm destroyed")));
|
|
1597
|
-
|
|
1598
|
-
F: __dxlog_file5,
|
|
1599
|
-
L: 154,
|
|
1600
|
-
S: this,
|
|
1601
|
-
C: (f, a) => f(...a)
|
|
1602
|
-
});
|
|
1028
|
+
log4("destroyed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 122, S: this });
|
|
1603
1029
|
}
|
|
1604
1030
|
async setTopology(topology) {
|
|
1605
|
-
invariant4(!this._ctx.disposed, "Swarm is offline", {
|
|
1606
|
-
F: __dxlog_file5,
|
|
1607
|
-
L: 158,
|
|
1608
|
-
S: this,
|
|
1609
|
-
A: [
|
|
1610
|
-
"!this._ctx.disposed",
|
|
1611
|
-
"'Swarm is offline'"
|
|
1612
|
-
]
|
|
1613
|
-
});
|
|
1031
|
+
invariant4(!this._ctx.disposed, "Swarm is offline", { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 125, S: this, A: ["!this._ctx.disposed", "'Swarm is offline'"] });
|
|
1614
1032
|
if (topology === this._topology) {
|
|
1615
1033
|
return;
|
|
1616
1034
|
}
|
|
1617
|
-
|
|
1035
|
+
log4("setting topology", {
|
|
1618
1036
|
previous: getClassName(this._topology),
|
|
1619
1037
|
topology: getClassName(topology)
|
|
1620
|
-
}, {
|
|
1621
|
-
F: __dxlog_file5,
|
|
1622
|
-
L: 162,
|
|
1623
|
-
S: this,
|
|
1624
|
-
C: (f, a) => f(...a)
|
|
1625
|
-
});
|
|
1038
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 129, S: this });
|
|
1626
1039
|
await this._topology.destroy();
|
|
1627
1040
|
this._topology = topology;
|
|
1628
1041
|
this._topology.init(this._getSwarmController());
|
|
1629
1042
|
this._topology.update();
|
|
1630
1043
|
}
|
|
1631
1044
|
async onSwarmEvent(swarmEvent) {
|
|
1632
|
-
|
|
1045
|
+
log4("swarm event", {
|
|
1633
1046
|
swarmEvent
|
|
1634
|
-
}, {
|
|
1635
|
-
F: __dxlog_file5,
|
|
1636
|
-
L: 175,
|
|
1637
|
-
S: this,
|
|
1638
|
-
C: (f, a) => f(...a)
|
|
1639
|
-
});
|
|
1047
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 139, S: this });
|
|
1640
1048
|
if (this._ctx.disposed) {
|
|
1641
|
-
|
|
1642
|
-
F: __dxlog_file5,
|
|
1643
|
-
L: 178,
|
|
1644
|
-
S: this,
|
|
1645
|
-
C: (f, a) => f(...a)
|
|
1646
|
-
});
|
|
1049
|
+
log4("swarm event ignored for disposed swarm", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 143, S: this });
|
|
1647
1050
|
return;
|
|
1648
1051
|
}
|
|
1649
1052
|
if (swarmEvent.peerAvailable) {
|
|
1650
1053
|
const peerId = swarmEvent.peerAvailable.peer.peerKey;
|
|
1651
1054
|
if (peerId !== this._ownPeer.peerKey) {
|
|
1652
|
-
|
|
1055
|
+
log4("new peer", {
|
|
1653
1056
|
peerId
|
|
1654
|
-
}, {
|
|
1655
|
-
F: __dxlog_file5,
|
|
1656
|
-
L: 185,
|
|
1657
|
-
S: this,
|
|
1658
|
-
C: (f, a) => f(...a)
|
|
1659
|
-
});
|
|
1057
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 149, S: this });
|
|
1660
1058
|
const peer = this._getOrCreatePeer(swarmEvent.peerAvailable.peer);
|
|
1661
1059
|
peer.advertizing = true;
|
|
1662
1060
|
}
|
|
@@ -1665,89 +1063,46 @@ var Swarm = class {
|
|
|
1665
1063
|
if (peer) {
|
|
1666
1064
|
peer.advertizing = false;
|
|
1667
1065
|
if (this._isConnectionEstablishmentInProgress(peer)) {
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
L: 196,
|
|
1671
|
-
S: this,
|
|
1672
|
-
C: (f, a) => f(...a)
|
|
1673
|
-
});
|
|
1674
|
-
void this._destroyPeer(swarmEvent.peerLeft.peer, "peer left").catch((err) => log5.catch(err, void 0, {
|
|
1675
|
-
F: __dxlog_file5,
|
|
1676
|
-
L: 197,
|
|
1677
|
-
S: this,
|
|
1678
|
-
C: (f, a) => f(...a)
|
|
1679
|
-
}));
|
|
1066
|
+
log4(`destroying peer, state: ${peer.connection?.state}`, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 162, S: this });
|
|
1067
|
+
void this._destroyPeer(swarmEvent.peerLeft.peer, "peer left").catch((err) => log4.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 163, S: this }));
|
|
1680
1068
|
}
|
|
1681
1069
|
} else {
|
|
1682
|
-
|
|
1070
|
+
log4("received peerLeft but no peer found", {
|
|
1683
1071
|
peer: swarmEvent.peerLeft.peer.peerKey
|
|
1684
|
-
}, {
|
|
1685
|
-
F: __dxlog_file5,
|
|
1686
|
-
L: 200,
|
|
1687
|
-
S: this,
|
|
1688
|
-
C: (f, a) => f(...a)
|
|
1689
|
-
});
|
|
1072
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 166, S: this });
|
|
1690
1073
|
}
|
|
1691
1074
|
}
|
|
1692
1075
|
this._topology.update();
|
|
1693
1076
|
}
|
|
1694
|
-
async onOffer(message) {
|
|
1695
|
-
|
|
1077
|
+
async onOffer(ctx, message) {
|
|
1078
|
+
log4("offer", {
|
|
1696
1079
|
message
|
|
1697
|
-
}, {
|
|
1698
|
-
F: __dxlog_file5,
|
|
1699
|
-
L: 209,
|
|
1700
|
-
S: this,
|
|
1701
|
-
C: (f, a) => f(...a)
|
|
1702
|
-
});
|
|
1080
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 174, S: this });
|
|
1703
1081
|
if (this._ctx.disposed) {
|
|
1704
|
-
|
|
1705
|
-
F: __dxlog_file5,
|
|
1706
|
-
L: 211,
|
|
1707
|
-
S: this,
|
|
1708
|
-
C: (f, a) => f(...a)
|
|
1709
|
-
});
|
|
1082
|
+
log4("ignored for disposed swarm", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 178, S: this });
|
|
1710
1083
|
return {
|
|
1711
1084
|
accept: false
|
|
1712
1085
|
};
|
|
1713
1086
|
}
|
|
1714
|
-
invariant4(message.author, void 0, {
|
|
1715
|
-
F: __dxlog_file5,
|
|
1716
|
-
L: 216,
|
|
1717
|
-
S: this,
|
|
1718
|
-
A: [
|
|
1719
|
-
"message.author",
|
|
1720
|
-
""
|
|
1721
|
-
]
|
|
1722
|
-
});
|
|
1087
|
+
invariant4(message.author, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 184, S: this, A: ["message.author", ""] });
|
|
1723
1088
|
if (message.recipient.peerKey !== this._ownPeer.peerKey) {
|
|
1724
|
-
|
|
1089
|
+
log4("rejecting offer with incorrect peerId", {
|
|
1725
1090
|
message
|
|
1726
|
-
}, {
|
|
1727
|
-
F: __dxlog_file5,
|
|
1728
|
-
L: 218,
|
|
1729
|
-
S: this,
|
|
1730
|
-
C: (f, a) => f(...a)
|
|
1731
|
-
});
|
|
1091
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 186, S: this });
|
|
1732
1092
|
return {
|
|
1733
1093
|
accept: false
|
|
1734
1094
|
};
|
|
1735
1095
|
}
|
|
1736
1096
|
if (!message.topic?.equals(this._topic)) {
|
|
1737
|
-
|
|
1097
|
+
log4("rejecting offer with incorrect topic", {
|
|
1738
1098
|
message
|
|
1739
|
-
}, {
|
|
1740
|
-
F: __dxlog_file5,
|
|
1741
|
-
L: 222,
|
|
1742
|
-
S: this,
|
|
1743
|
-
C: (f, a) => f(...a)
|
|
1744
|
-
});
|
|
1099
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 194, S: this });
|
|
1745
1100
|
return {
|
|
1746
1101
|
accept: false
|
|
1747
1102
|
};
|
|
1748
1103
|
}
|
|
1749
1104
|
const peer = this._getOfferSenderPeer(message.author);
|
|
1750
|
-
const answer = await peer.onOffer(message);
|
|
1105
|
+
const answer = await peer.onOffer(ctx, message);
|
|
1751
1106
|
this._topology.update();
|
|
1752
1107
|
return answer;
|
|
1753
1108
|
}
|
|
@@ -1761,53 +1116,19 @@ var Swarm = class {
|
|
|
1761
1116
|
}
|
|
1762
1117
|
return peer;
|
|
1763
1118
|
}
|
|
1764
|
-
async onSignal(message) {
|
|
1765
|
-
|
|
1119
|
+
async onSignal(ctx, message) {
|
|
1120
|
+
log4("signal", {
|
|
1766
1121
|
message
|
|
1767
|
-
}, {
|
|
1768
|
-
F: __dxlog_file5,
|
|
1769
|
-
L: 247,
|
|
1770
|
-
S: this,
|
|
1771
|
-
C: (f, a) => f(...a)
|
|
1772
|
-
});
|
|
1122
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 218, S: this });
|
|
1773
1123
|
if (this._ctx.disposed) {
|
|
1774
|
-
|
|
1775
|
-
F: __dxlog_file5,
|
|
1776
|
-
L: 249,
|
|
1777
|
-
S: this,
|
|
1778
|
-
C: (f, a) => f(...a)
|
|
1779
|
-
});
|
|
1124
|
+
log4.info("ignored for offline swarm", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 222, S: this });
|
|
1780
1125
|
return;
|
|
1781
1126
|
}
|
|
1782
|
-
invariant4(message.recipient.peerKey === this._ownPeer.peerKey, `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
S: this,
|
|
1786
|
-
A: [
|
|
1787
|
-
"message.recipient.peerKey === this._ownPeer.peerKey",
|
|
1788
|
-
"`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
|
|
1789
|
-
]
|
|
1790
|
-
});
|
|
1791
|
-
invariant4(message.topic?.equals(this._topic), void 0, {
|
|
1792
|
-
F: __dxlog_file5,
|
|
1793
|
-
L: 256,
|
|
1794
|
-
S: this,
|
|
1795
|
-
A: [
|
|
1796
|
-
"message.topic?.equals(this._topic)",
|
|
1797
|
-
""
|
|
1798
|
-
]
|
|
1799
|
-
});
|
|
1800
|
-
invariant4(message.author, void 0, {
|
|
1801
|
-
F: __dxlog_file5,
|
|
1802
|
-
L: 257,
|
|
1803
|
-
S: this,
|
|
1804
|
-
A: [
|
|
1805
|
-
"message.author",
|
|
1806
|
-
""
|
|
1807
|
-
]
|
|
1808
|
-
});
|
|
1127
|
+
invariant4(message.recipient.peerKey === this._ownPeer.peerKey, `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 225, S: this, A: ["message.recipient.peerKey === this._ownPeer.peerKey", "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"] });
|
|
1128
|
+
invariant4(message.topic?.equals(this._topic), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 226, S: this, A: ["message.topic?.equals(this._topic)", ""] });
|
|
1129
|
+
invariant4(message.author, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 227, S: this, A: ["message.author", ""] });
|
|
1809
1130
|
const peer = this._getOrCreatePeer(message.author);
|
|
1810
|
-
await peer.onSignal(message);
|
|
1131
|
+
await peer.onSignal(ctx, message);
|
|
1811
1132
|
}
|
|
1812
1133
|
// For debug purposes
|
|
1813
1134
|
async goOffline() {
|
|
@@ -1818,21 +1139,10 @@ var Swarm = class {
|
|
|
1818
1139
|
}
|
|
1819
1140
|
// For debug purposes
|
|
1820
1141
|
async goOnline() {
|
|
1821
|
-
this._ctx = new
|
|
1822
|
-
F: __dxlog_file5,
|
|
1823
|
-
L: 273
|
|
1824
|
-
});
|
|
1142
|
+
this._ctx = new Context3(void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 240 });
|
|
1825
1143
|
}
|
|
1826
1144
|
_getOrCreatePeer(peerInfo) {
|
|
1827
|
-
invariant4(peerInfo.peerKey, "PeerInfo.peerKey is required", {
|
|
1828
|
-
F: __dxlog_file5,
|
|
1829
|
-
L: 277,
|
|
1830
|
-
S: this,
|
|
1831
|
-
A: [
|
|
1832
|
-
"peerInfo.peerKey",
|
|
1833
|
-
"'PeerInfo.peerKey is required'"
|
|
1834
|
-
]
|
|
1835
|
-
});
|
|
1145
|
+
invariant4(peerInfo.peerKey, "PeerInfo.peerKey is required", { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 243, S: this, A: ["peerInfo.peerKey", "'PeerInfo.peerKey is required'"] });
|
|
1836
1146
|
let peer = this._peers.get(peerInfo);
|
|
1837
1147
|
if (!peer) {
|
|
1838
1148
|
peer = new Peer(peerInfo, this._topic, this._ownPeer, this._swarmMessenger, this._protocolProvider, this._transportFactory, this._connectionLimiter, {
|
|
@@ -1844,12 +1154,7 @@ var Swarm = class {
|
|
|
1844
1154
|
},
|
|
1845
1155
|
onDisconnected: async () => {
|
|
1846
1156
|
if (this._isUnregistered(peer)) {
|
|
1847
|
-
|
|
1848
|
-
F: __dxlog_file5,
|
|
1849
|
-
L: 297,
|
|
1850
|
-
S: this,
|
|
1851
|
-
C: (f, a) => f(...a)
|
|
1852
|
-
});
|
|
1157
|
+
log4.verbose("ignored onDisconnected for unregistered peer", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 255, S: this });
|
|
1853
1158
|
return;
|
|
1854
1159
|
}
|
|
1855
1160
|
if (!peer.advertizing) {
|
|
@@ -1860,14 +1165,9 @@ var Swarm = class {
|
|
|
1860
1165
|
},
|
|
1861
1166
|
onRejected: () => {
|
|
1862
1167
|
if (!this._isUnregistered(peer)) {
|
|
1863
|
-
|
|
1168
|
+
log4("peer rejected connection", {
|
|
1864
1169
|
peerInfo
|
|
1865
|
-
}, {
|
|
1866
|
-
F: __dxlog_file5,
|
|
1867
|
-
L: 311,
|
|
1868
|
-
S: this,
|
|
1869
|
-
C: (f, a) => f(...a)
|
|
1870
|
-
});
|
|
1170
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 268, S: this });
|
|
1871
1171
|
void this._destroyPeer(peerInfo, "peer rejected connection");
|
|
1872
1172
|
}
|
|
1873
1173
|
},
|
|
@@ -1875,7 +1175,7 @@ var Swarm = class {
|
|
|
1875
1175
|
this._topology.update();
|
|
1876
1176
|
},
|
|
1877
1177
|
onOffer: (remoteId) => {
|
|
1878
|
-
return this._topology.onOffer(
|
|
1178
|
+
return this._topology.onOffer(PublicKey3.from(remoteId.peerKey));
|
|
1879
1179
|
},
|
|
1880
1180
|
onPeerAvailable: () => {
|
|
1881
1181
|
this._topology.update();
|
|
@@ -1886,35 +1186,22 @@ var Swarm = class {
|
|
|
1886
1186
|
return peer;
|
|
1887
1187
|
}
|
|
1888
1188
|
async _destroyPeer(peerInfo, reason) {
|
|
1889
|
-
|
|
1189
|
+
log4("destroy peer", {
|
|
1890
1190
|
peerKey: peerInfo.peerKey,
|
|
1891
1191
|
reason
|
|
1892
|
-
}, {
|
|
1893
|
-
F: __dxlog_file5,
|
|
1894
|
-
L: 333,
|
|
1895
|
-
S: this,
|
|
1896
|
-
C: (f, a) => f(...a)
|
|
1897
|
-
});
|
|
1192
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 289, S: this });
|
|
1898
1193
|
const peer = this._peers.get(peerInfo);
|
|
1899
|
-
invariant4(peer, void 0, {
|
|
1900
|
-
F: __dxlog_file5,
|
|
1901
|
-
L: 335,
|
|
1902
|
-
S: this,
|
|
1903
|
-
A: [
|
|
1904
|
-
"peer",
|
|
1905
|
-
""
|
|
1906
|
-
]
|
|
1907
|
-
});
|
|
1194
|
+
invariant4(peer, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 294, S: this, A: ["peer", ""] });
|
|
1908
1195
|
this._peers.delete(peerInfo);
|
|
1909
1196
|
await peer.safeDestroy(reason);
|
|
1910
1197
|
}
|
|
1911
1198
|
_getSwarmController() {
|
|
1912
1199
|
return {
|
|
1913
1200
|
getState: () => ({
|
|
1914
|
-
ownPeerId:
|
|
1915
|
-
connected: Array.from(this._peers.entries()).filter(([_, peer]) => peer.connection).map(([info]) =>
|
|
1916
|
-
candidates: Array.from(this._peers.entries()).filter(([_, peer]) => !peer.connection && peer.advertizing && peer.availableToConnect).map(([info]) =>
|
|
1917
|
-
allPeers: Array.from(this._peers.keys()).map((info) =>
|
|
1201
|
+
ownPeerId: PublicKey3.from(this._ownPeer.peerKey),
|
|
1202
|
+
connected: Array.from(this._peers.entries()).filter(([_, peer]) => peer.connection).map(([info]) => PublicKey3.from(info.peerKey)),
|
|
1203
|
+
candidates: Array.from(this._peers.entries()).filter(([_, peer]) => !peer.connection && peer.advertizing && peer.availableToConnect).map(([info]) => PublicKey3.from(info.peerKey)),
|
|
1204
|
+
allPeers: Array.from(this._peers.keys()).map((info) => PublicKey3.from(info.peerKey))
|
|
1918
1205
|
}),
|
|
1919
1206
|
connect: (peer) => {
|
|
1920
1207
|
if (this._ctx.disposed) {
|
|
@@ -1926,12 +1213,7 @@ var Swarm = class {
|
|
|
1926
1213
|
peerKey: peer.toHex()
|
|
1927
1214
|
});
|
|
1928
1215
|
} catch (err) {
|
|
1929
|
-
|
|
1930
|
-
F: __dxlog_file5,
|
|
1931
|
-
L: 362,
|
|
1932
|
-
S: this,
|
|
1933
|
-
C: (f, a) => f(...a)
|
|
1934
|
-
});
|
|
1216
|
+
log4("initiation error", err, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 317, S: this });
|
|
1935
1217
|
}
|
|
1936
1218
|
});
|
|
1937
1219
|
},
|
|
@@ -1955,14 +1237,9 @@ var Swarm = class {
|
|
|
1955
1237
|
const ctx = this._ctx;
|
|
1956
1238
|
const peer = this._getOrCreatePeer(remotePeer);
|
|
1957
1239
|
if (remotePeer.peerKey < this._ownPeer.peerKey) {
|
|
1958
|
-
|
|
1240
|
+
log4("initiation delay", {
|
|
1959
1241
|
remotePeer
|
|
1960
|
-
}, {
|
|
1961
|
-
F: __dxlog_file5,
|
|
1962
|
-
L: 390,
|
|
1963
|
-
S: this,
|
|
1964
|
-
C: (f, a) => f(...a)
|
|
1965
|
-
});
|
|
1242
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 343, S: this });
|
|
1966
1243
|
await sleep2(this._initiationDelay);
|
|
1967
1244
|
}
|
|
1968
1245
|
if (ctx.disposed) {
|
|
@@ -1974,24 +1251,14 @@ var Swarm = class {
|
|
|
1974
1251
|
if (peer.connection) {
|
|
1975
1252
|
return;
|
|
1976
1253
|
}
|
|
1977
|
-
|
|
1254
|
+
log4("initiating connection...", {
|
|
1978
1255
|
remotePeer
|
|
1979
|
-
}, {
|
|
1980
|
-
|
|
1981
|
-
L: 406,
|
|
1982
|
-
S: this,
|
|
1983
|
-
C: (f, a) => f(...a)
|
|
1984
|
-
});
|
|
1985
|
-
await peer.initiateConnection();
|
|
1256
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 358, S: this });
|
|
1257
|
+
await peer.initiateConnection(ctx);
|
|
1986
1258
|
this._topology.update();
|
|
1987
|
-
|
|
1259
|
+
log4("initiated", {
|
|
1988
1260
|
remotePeer
|
|
1989
|
-
}, {
|
|
1990
|
-
F: __dxlog_file5,
|
|
1991
|
-
L: 409,
|
|
1992
|
-
S: this,
|
|
1993
|
-
C: (f, a) => f(...a)
|
|
1994
|
-
});
|
|
1261
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 363, S: this });
|
|
1995
1262
|
}
|
|
1996
1263
|
async _closeConnection(peerInfo) {
|
|
1997
1264
|
const peer = this._peers.get(peerInfo);
|
|
@@ -2038,26 +1305,22 @@ _ts_decorate3([
|
|
|
2038
1305
|
|
|
2039
1306
|
// src/swarm/swarm-mapper.ts
|
|
2040
1307
|
import { Event as Event4, SubscriptionList } from "@dxos/async";
|
|
2041
|
-
import { PublicKey as
|
|
2042
|
-
import { log as
|
|
1308
|
+
import { PublicKey as PublicKey4 } from "@dxos/keys";
|
|
1309
|
+
import { log as log5 } from "@dxos/log";
|
|
2043
1310
|
import { PeerInfoHash as PeerInfoHash2 } from "@dxos/messaging";
|
|
2044
1311
|
import { ComplexMap as ComplexMap3 } from "@dxos/util";
|
|
2045
|
-
var
|
|
1312
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
|
|
2046
1313
|
var SwarmMapper = class {
|
|
2047
1314
|
_swarm;
|
|
2048
|
-
_subscriptions;
|
|
2049
|
-
_connectionSubscriptions;
|
|
2050
|
-
_peers;
|
|
2051
|
-
mapUpdated;
|
|
1315
|
+
_subscriptions = new SubscriptionList();
|
|
1316
|
+
_connectionSubscriptions = new ComplexMap3(PeerInfoHash2);
|
|
1317
|
+
_peers = new ComplexMap3(PeerInfoHash2);
|
|
1318
|
+
mapUpdated = new Event4();
|
|
2052
1319
|
get peers() {
|
|
2053
1320
|
return Array.from(this._peers.values());
|
|
2054
1321
|
}
|
|
2055
1322
|
constructor(_swarm) {
|
|
2056
1323
|
this._swarm = _swarm;
|
|
2057
|
-
this._subscriptions = new SubscriptionList();
|
|
2058
|
-
this._connectionSubscriptions = new ComplexMap3(PeerInfoHash2);
|
|
2059
|
-
this._peers = new ComplexMap3(PeerInfoHash2);
|
|
2060
|
-
this.mapUpdated = new Event4();
|
|
2061
1324
|
this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
|
|
2062
1325
|
this._update();
|
|
2063
1326
|
this._connectionSubscriptions.set(connection.remoteInfo, connection.stateChanged.on(() => {
|
|
@@ -2072,12 +1335,7 @@ var SwarmMapper = class {
|
|
|
2072
1335
|
this._update();
|
|
2073
1336
|
}
|
|
2074
1337
|
_update() {
|
|
2075
|
-
|
|
2076
|
-
F: __dxlog_file6,
|
|
2077
|
-
L: 71,
|
|
2078
|
-
S: this,
|
|
2079
|
-
C: (f, a) => f(...a)
|
|
2080
|
-
});
|
|
1338
|
+
log5("updating swarm", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 40, S: this });
|
|
2081
1339
|
this._peers.clear();
|
|
2082
1340
|
this._peers.set(this._swarm.ownPeer, {
|
|
2083
1341
|
id: this._swarm.ownPeerId,
|
|
@@ -2086,22 +1344,17 @@ var SwarmMapper = class {
|
|
|
2086
1344
|
});
|
|
2087
1345
|
for (const connection of this._swarm.connections) {
|
|
2088
1346
|
this._peers.set(connection.remoteInfo, {
|
|
2089
|
-
id:
|
|
1347
|
+
id: PublicKey4.from(connection.remoteInfo.peerKey),
|
|
2090
1348
|
state: connection.state,
|
|
2091
1349
|
connections: [
|
|
2092
1350
|
this._swarm.ownPeerId
|
|
2093
1351
|
]
|
|
2094
1352
|
});
|
|
2095
1353
|
}
|
|
2096
|
-
|
|
1354
|
+
log5("graph changed", {
|
|
2097
1355
|
directConnections: this._swarm.connections.length,
|
|
2098
1356
|
totalPeersInSwarm: this._peers.size
|
|
2099
|
-
}, {
|
|
2100
|
-
F: __dxlog_file6,
|
|
2101
|
-
L: 112,
|
|
2102
|
-
S: this,
|
|
2103
|
-
C: (f, a) => f(...a)
|
|
2104
|
-
});
|
|
1357
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 77, S: this });
|
|
2105
1358
|
this.mapUpdated.emit(Array.from(this._peers.values()));
|
|
2106
1359
|
}
|
|
2107
1360
|
// TODO(burdon): Async open/close.
|
|
@@ -2114,24 +1367,21 @@ var SwarmMapper = class {
|
|
|
2114
1367
|
|
|
2115
1368
|
// src/swarm/connection-limiter.ts
|
|
2116
1369
|
import { DeferredTask as DeferredTask2 } from "@dxos/async";
|
|
2117
|
-
import { Context as
|
|
1370
|
+
import { Context as Context4 } from "@dxos/context";
|
|
2118
1371
|
import { invariant as invariant5 } from "@dxos/invariant";
|
|
2119
|
-
import { PublicKey as
|
|
2120
|
-
import { log as
|
|
1372
|
+
import { PublicKey as PublicKey5 } from "@dxos/keys";
|
|
1373
|
+
import { log as log6 } from "@dxos/log";
|
|
2121
1374
|
import { CancelledError as CancelledError3 } from "@dxos/protocols";
|
|
2122
1375
|
import { ComplexMap as ComplexMap4 } from "@dxos/util";
|
|
2123
|
-
var
|
|
1376
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
|
|
2124
1377
|
var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
|
|
2125
1378
|
var ConnectionLimiter = class {
|
|
2126
|
-
_ctx = new
|
|
2127
|
-
F: __dxlog_file7,
|
|
2128
|
-
L: 23
|
|
2129
|
-
});
|
|
1379
|
+
_ctx = new Context4(void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 15 });
|
|
2130
1380
|
_maxConcurrentInitConnections;
|
|
2131
1381
|
/**
|
|
2132
1382
|
* Queue of promises to resolve when initiating connections amount is below the limit.
|
|
2133
1383
|
*/
|
|
2134
|
-
_waitingPromises = new ComplexMap4(
|
|
1384
|
+
_waitingPromises = new ComplexMap4(PublicKey5.hash);
|
|
2135
1385
|
resolveWaitingPromises = new DeferredTask2(this._ctx, async () => {
|
|
2136
1386
|
Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
|
|
2137
1387
|
resolve();
|
|
@@ -2144,23 +1394,10 @@ var ConnectionLimiter = class {
|
|
|
2144
1394
|
* @returns Promise that resolves in queue when connections amount with 'CONNECTING' state is below the limit.
|
|
2145
1395
|
*/
|
|
2146
1396
|
async connecting(sessionId) {
|
|
2147
|
-
invariant5(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
|
|
2148
|
-
|
|
2149
|
-
L: 48,
|
|
2150
|
-
S: this,
|
|
2151
|
-
A: [
|
|
2152
|
-
"!this._waitingPromises.has(sessionId)",
|
|
2153
|
-
"'Peer is already waiting for connection'"
|
|
2154
|
-
]
|
|
2155
|
-
});
|
|
2156
|
-
log7("waiting", {
|
|
1397
|
+
invariant5(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 31, S: this, A: ["!this._waitingPromises.has(sessionId)", "'Peer is already waiting for connection'"] });
|
|
1398
|
+
log6("waiting", {
|
|
2157
1399
|
sessionId
|
|
2158
|
-
}, {
|
|
2159
|
-
F: __dxlog_file7,
|
|
2160
|
-
L: 49,
|
|
2161
|
-
S: this,
|
|
2162
|
-
C: (f, a) => f(...a)
|
|
2163
|
-
});
|
|
1400
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 32, S: this });
|
|
2164
1401
|
await new Promise((resolve, reject) => {
|
|
2165
1402
|
this._waitingPromises.set(sessionId, {
|
|
2166
1403
|
resolve,
|
|
@@ -2168,27 +1405,17 @@ var ConnectionLimiter = class {
|
|
|
2168
1405
|
});
|
|
2169
1406
|
this.resolveWaitingPromises.schedule();
|
|
2170
1407
|
});
|
|
2171
|
-
|
|
1408
|
+
log6("allow", {
|
|
2172
1409
|
sessionId
|
|
2173
|
-
}, {
|
|
2174
|
-
F: __dxlog_file7,
|
|
2175
|
-
L: 57,
|
|
2176
|
-
S: this,
|
|
2177
|
-
C: (f, a) => f(...a)
|
|
2178
|
-
});
|
|
1410
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 42, S: this });
|
|
2179
1411
|
}
|
|
2180
1412
|
/**
|
|
2181
1413
|
* Rejects promise returned by `connecting` method.
|
|
2182
1414
|
*/
|
|
2183
1415
|
doneConnecting(sessionId) {
|
|
2184
|
-
|
|
1416
|
+
log6("done", {
|
|
2185
1417
|
sessionId
|
|
2186
|
-
}, {
|
|
2187
|
-
F: __dxlog_file7,
|
|
2188
|
-
L: 64,
|
|
2189
|
-
S: this,
|
|
2190
|
-
C: (f, a) => f(...a)
|
|
2191
|
-
});
|
|
1418
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 49, S: this });
|
|
2192
1419
|
if (!this._waitingPromises.has(sessionId)) {
|
|
2193
1420
|
return;
|
|
2194
1421
|
}
|
|
@@ -2201,22 +1428,22 @@ var ConnectionLimiter = class {
|
|
|
2201
1428
|
// src/connection-log.ts
|
|
2202
1429
|
import { Event as Event5 } from "@dxos/async";
|
|
2203
1430
|
import { raise } from "@dxos/debug";
|
|
2204
|
-
import { PublicKey as
|
|
1431
|
+
import { PublicKey as PublicKey6 } from "@dxos/keys";
|
|
2205
1432
|
import { ComplexMap as ComplexMap5 } from "@dxos/util";
|
|
2206
1433
|
var CONNECTION_GC_THRESHOLD = 1e3 * 60 * 15;
|
|
2207
|
-
var EventType = /* @__PURE__ */ function(EventType2) {
|
|
1434
|
+
var EventType = /* @__PURE__ */ (function(EventType2) {
|
|
2208
1435
|
EventType2["CONNECTION_STATE_CHANGED"] = "CONNECTION_STATE_CHANGED";
|
|
2209
1436
|
EventType2["PROTOCOL_ERROR"] = "PROTOCOL_ERROR";
|
|
2210
1437
|
EventType2["PROTOCOL_EXTENSIONS_INITIALIZED"] = "PROTOCOL_EXTENSIONS_INITIALIZED";
|
|
2211
1438
|
EventType2["PROTOCOL_EXTENSIONS_HANDSHAKE"] = "PROTOCOL_EXTENSIONS_HANDSHAKE";
|
|
2212
1439
|
EventType2["PROTOCOL_HANDSHAKE"] = "PROTOCOL_HANDSHAKE";
|
|
2213
1440
|
return EventType2;
|
|
2214
|
-
}({});
|
|
1441
|
+
})({});
|
|
2215
1442
|
var ConnectionLog = class {
|
|
2216
1443
|
/**
|
|
2217
1444
|
* SwarmId => info
|
|
2218
1445
|
*/
|
|
2219
|
-
_swarms = new ComplexMap5(
|
|
1446
|
+
_swarms = new ComplexMap5(PublicKey6.hash);
|
|
2220
1447
|
update = new Event5();
|
|
2221
1448
|
getSwarmInfo(swarmId) {
|
|
2222
1449
|
return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
|
|
@@ -2226,19 +1453,19 @@ var ConnectionLog = class {
|
|
|
2226
1453
|
}
|
|
2227
1454
|
joinedSwarm(swarm) {
|
|
2228
1455
|
const info = {
|
|
2229
|
-
id:
|
|
1456
|
+
id: PublicKey6.from(swarm._instanceId),
|
|
2230
1457
|
topic: swarm.topic,
|
|
2231
1458
|
isActive: true,
|
|
2232
1459
|
label: swarm.label,
|
|
2233
1460
|
connections: []
|
|
2234
1461
|
};
|
|
2235
|
-
this._swarms.set(
|
|
1462
|
+
this._swarms.set(PublicKey6.from(swarm._instanceId), info);
|
|
2236
1463
|
this.update.emit();
|
|
2237
1464
|
swarm.connectionAdded.on((connection) => {
|
|
2238
1465
|
const connectionInfo = {
|
|
2239
1466
|
state: ConnectionState.CREATED,
|
|
2240
1467
|
closeReason: connection.closeReason,
|
|
2241
|
-
remotePeerId:
|
|
1468
|
+
remotePeerId: PublicKey6.from(connection.remoteInfo.peerKey),
|
|
2242
1469
|
sessionId: connection.sessionId,
|
|
2243
1470
|
transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
|
|
2244
1471
|
protocolExtensions: [],
|
|
@@ -2278,7 +1505,7 @@ var ConnectionLog = class {
|
|
|
2278
1505
|
});
|
|
2279
1506
|
}
|
|
2280
1507
|
leftSwarm(swarm) {
|
|
2281
|
-
this.getSwarmInfo(
|
|
1508
|
+
this.getSwarmInfo(PublicKey6.from(swarm._instanceId)).isActive = false;
|
|
2282
1509
|
this.update.emit();
|
|
2283
1510
|
}
|
|
2284
1511
|
};
|
|
@@ -2290,33 +1517,31 @@ var gcSwarm = (swarm) => {
|
|
|
2290
1517
|
|
|
2291
1518
|
// src/network-manager.ts
|
|
2292
1519
|
import { Event as Event6, synchronized as synchronized4 } from "@dxos/async";
|
|
2293
|
-
import { invariant as invariant6 } from "@dxos/invariant";
|
|
2294
|
-
import { PublicKey as
|
|
2295
|
-
import { log as
|
|
1520
|
+
import { assertArgument, invariant as invariant6 } from "@dxos/invariant";
|
|
1521
|
+
import { PublicKey as PublicKey7 } from "@dxos/keys";
|
|
1522
|
+
import { log as log7 } from "@dxos/log";
|
|
2296
1523
|
import { Messenger } from "@dxos/messaging";
|
|
2297
|
-
import { trace as trace3 } from "@dxos/protocols";
|
|
2298
1524
|
import { ConnectionState as ConnectionState2 } from "@dxos/protocols/proto/dxos/client/services";
|
|
2299
1525
|
import { ComplexMap as ComplexMap6 } from "@dxos/util";
|
|
1526
|
+
var __dxlog_file7 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
|
|
2300
1527
|
function _ts_decorate4(decorators, target, key, desc) {
|
|
2301
1528
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2302
1529
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
2303
1530
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2304
1531
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2305
1532
|
}
|
|
2306
|
-
var __dxlog_file8 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
|
|
2307
1533
|
var SwarmNetworkManager = class {
|
|
2308
1534
|
/**
|
|
2309
1535
|
* @internal
|
|
2310
1536
|
*/
|
|
2311
|
-
_swarms = new ComplexMap6(
|
|
2312
|
-
_mappers = new ComplexMap6(
|
|
1537
|
+
_swarms = new ComplexMap6(PublicKey7.hash);
|
|
1538
|
+
_mappers = new ComplexMap6(PublicKey7.hash);
|
|
2313
1539
|
_transportFactory;
|
|
2314
1540
|
_signalManager;
|
|
2315
1541
|
_messenger;
|
|
2316
1542
|
_signalConnection;
|
|
2317
1543
|
_connectionLimiter;
|
|
2318
1544
|
_connectionLog;
|
|
2319
|
-
_instanceId = PublicKey8.random().toHex();
|
|
2320
1545
|
_peerInfo = void 0;
|
|
2321
1546
|
_connectionState = ConnectionState2.ONLINE;
|
|
2322
1547
|
connectionStateChanged = new Event6();
|
|
@@ -2329,8 +1554,8 @@ var SwarmNetworkManager = class {
|
|
|
2329
1554
|
signalManager: this._signalManager
|
|
2330
1555
|
});
|
|
2331
1556
|
this._signalConnection = {
|
|
2332
|
-
join: (opts) => this._signalManager.join(opts),
|
|
2333
|
-
leave: (opts) => this._signalManager.leave(opts)
|
|
1557
|
+
join: (ctx, opts) => this._signalManager.join(ctx, opts),
|
|
1558
|
+
leave: (ctx, opts) => this._signalManager.leave(ctx, opts)
|
|
2334
1559
|
};
|
|
2335
1560
|
this._peerInfo = peerInfo;
|
|
2336
1561
|
this._connectionLimiter = new ConnectionLimiter();
|
|
@@ -2359,34 +1584,15 @@ var SwarmNetworkManager = class {
|
|
|
2359
1584
|
this._peerInfo = peerInfo;
|
|
2360
1585
|
}
|
|
2361
1586
|
async open() {
|
|
2362
|
-
|
|
2363
|
-
id: this._instanceId
|
|
2364
|
-
}), {
|
|
2365
|
-
F: __dxlog_file8,
|
|
2366
|
-
L: 133,
|
|
2367
|
-
S: this,
|
|
2368
|
-
C: (f, a) => f(...a)
|
|
2369
|
-
});
|
|
1587
|
+
log7("opening network manager", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 76, S: this });
|
|
2370
1588
|
await this._messenger.open();
|
|
2371
1589
|
await this._signalManager.open();
|
|
2372
|
-
|
|
2373
|
-
id: this._instanceId
|
|
2374
|
-
}), {
|
|
2375
|
-
F: __dxlog_file8,
|
|
2376
|
-
L: 136,
|
|
2377
|
-
S: this,
|
|
2378
|
-
C: (f, a) => f(...a)
|
|
2379
|
-
});
|
|
1590
|
+
log7("opened network manager", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 79, S: this });
|
|
2380
1591
|
}
|
|
2381
|
-
async close() {
|
|
1592
|
+
async close(ctx) {
|
|
2382
1593
|
for (const topic of this._swarms.keys()) {
|
|
2383
|
-
await this.leaveSwarm(topic).catch((err) => {
|
|
2384
|
-
|
|
2385
|
-
F: __dxlog_file8,
|
|
2386
|
-
L: 142,
|
|
2387
|
-
S: this,
|
|
2388
|
-
C: (f, a) => f(...a)
|
|
2389
|
-
});
|
|
1594
|
+
await this.leaveSwarm(ctx, topic).catch((err) => {
|
|
1595
|
+
log7(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 84, S: this });
|
|
2390
1596
|
});
|
|
2391
1597
|
}
|
|
2392
1598
|
await this._messenger.close();
|
|
@@ -2395,111 +1601,54 @@ var SwarmNetworkManager = class {
|
|
|
2395
1601
|
/**
|
|
2396
1602
|
* Join the swarm.
|
|
2397
1603
|
*/
|
|
2398
|
-
async joinSwarm({ topic, topology, protocolProvider: protocol, label }) {
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
A: [
|
|
2404
|
-
"PublicKey.isPublicKey(topic)",
|
|
2405
|
-
""
|
|
2406
|
-
]
|
|
2407
|
-
});
|
|
2408
|
-
invariant6(topology, void 0, {
|
|
2409
|
-
F: __dxlog_file8,
|
|
2410
|
-
L: 161,
|
|
2411
|
-
S: this,
|
|
2412
|
-
A: [
|
|
2413
|
-
"topology",
|
|
2414
|
-
""
|
|
2415
|
-
]
|
|
2416
|
-
});
|
|
2417
|
-
invariant6(this._peerInfo, void 0, {
|
|
2418
|
-
F: __dxlog_file8,
|
|
2419
|
-
L: 162,
|
|
2420
|
-
S: this,
|
|
2421
|
-
A: [
|
|
2422
|
-
"this._peerInfo",
|
|
2423
|
-
""
|
|
2424
|
-
]
|
|
2425
|
-
});
|
|
2426
|
-
invariant6(typeof protocol === "function", void 0, {
|
|
2427
|
-
F: __dxlog_file8,
|
|
2428
|
-
L: 163,
|
|
2429
|
-
S: this,
|
|
2430
|
-
A: [
|
|
2431
|
-
"typeof protocol === 'function'",
|
|
2432
|
-
""
|
|
2433
|
-
]
|
|
2434
|
-
});
|
|
1604
|
+
async joinSwarm(ctx, { topic, topology, protocolProvider: protocol, label }) {
|
|
1605
|
+
assertArgument(PublicKey7.isPublicKey(topic), "topic");
|
|
1606
|
+
invariant6(topology, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 94, S: this, A: ["topology", ""] });
|
|
1607
|
+
invariant6(this._peerInfo, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 95, S: this, A: ["this._peerInfo", ""] });
|
|
1608
|
+
assertArgument(typeof protocol === "function", "protocolProvider");
|
|
2435
1609
|
if (this._swarms.has(topic)) {
|
|
2436
|
-
throw new Error(`Already connected to swarm: ${
|
|
1610
|
+
throw new Error(`Already connected to swarm: ${PublicKey7.from(topic)}`);
|
|
2437
1611
|
}
|
|
2438
|
-
|
|
2439
|
-
topic:
|
|
1612
|
+
log7("joining", {
|
|
1613
|
+
topic: PublicKey7.from(topic),
|
|
2440
1614
|
peerInfo: this._peerInfo,
|
|
2441
1615
|
topology: topology.toString()
|
|
2442
|
-
}, {
|
|
2443
|
-
F: __dxlog_file8,
|
|
2444
|
-
L: 168,
|
|
2445
|
-
S: this,
|
|
2446
|
-
C: (f, a) => f(...a)
|
|
2447
|
-
});
|
|
1616
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 100, S: this });
|
|
2448
1617
|
const swarm = new Swarm(topic, this._peerInfo, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
|
|
2449
1618
|
swarm.errors.handle((error) => {
|
|
2450
|
-
|
|
1619
|
+
log7("swarm error", {
|
|
2451
1620
|
error
|
|
2452
|
-
}, {
|
|
2453
|
-
F: __dxlog_file8,
|
|
2454
|
-
L: 181,
|
|
2455
|
-
S: this,
|
|
2456
|
-
C: (f, a) => f(...a)
|
|
2457
|
-
});
|
|
1621
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 107, S: this });
|
|
2458
1622
|
});
|
|
2459
1623
|
this._swarms.set(topic, swarm);
|
|
2460
1624
|
this._mappers.set(topic, new SwarmMapper(swarm));
|
|
2461
1625
|
await swarm.open();
|
|
2462
|
-
this._signalConnection.join({
|
|
1626
|
+
this._signalConnection.join(ctx, {
|
|
2463
1627
|
topic,
|
|
2464
1628
|
peer: this._peerInfo
|
|
2465
|
-
}).catch((error) =>
|
|
2466
|
-
F: __dxlog_file8,
|
|
2467
|
-
L: 190,
|
|
2468
|
-
S: this,
|
|
2469
|
-
C: (f, a) => f(...a)
|
|
2470
|
-
}));
|
|
1629
|
+
}).catch((error) => log7.catch(error, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 118, S: this }));
|
|
2471
1630
|
this.topicsUpdated.emit();
|
|
2472
1631
|
this._connectionLog?.joinedSwarm(swarm);
|
|
2473
|
-
|
|
2474
|
-
topic:
|
|
1632
|
+
log7("joined", {
|
|
1633
|
+
topic: PublicKey7.from(topic),
|
|
2475
1634
|
count: this._swarms.size
|
|
2476
|
-
}, {
|
|
2477
|
-
F: __dxlog_file8,
|
|
2478
|
-
L: 194,
|
|
2479
|
-
S: this,
|
|
2480
|
-
C: (f, a) => f(...a)
|
|
2481
|
-
});
|
|
1635
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 121, S: this });
|
|
2482
1636
|
return {
|
|
2483
|
-
close: () => this.leaveSwarm(topic)
|
|
1637
|
+
close: (ctx2) => this.leaveSwarm(ctx2, topic)
|
|
2484
1638
|
};
|
|
2485
1639
|
}
|
|
2486
1640
|
/**
|
|
2487
1641
|
* Close the connection.
|
|
2488
1642
|
*/
|
|
2489
|
-
async leaveSwarm(topic) {
|
|
1643
|
+
async leaveSwarm(ctx, topic) {
|
|
2490
1644
|
if (!this._swarms.has(topic)) {
|
|
2491
1645
|
return;
|
|
2492
1646
|
}
|
|
2493
|
-
|
|
2494
|
-
topic:
|
|
2495
|
-
}, {
|
|
2496
|
-
F: __dxlog_file8,
|
|
2497
|
-
L: 211,
|
|
2498
|
-
S: this,
|
|
2499
|
-
C: (f, a) => f(...a)
|
|
2500
|
-
});
|
|
1647
|
+
log7("leaving", {
|
|
1648
|
+
topic: PublicKey7.from(topic)
|
|
1649
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 136, S: this });
|
|
2501
1650
|
const swarm = this._swarms.get(topic);
|
|
2502
|
-
await this._signalConnection.leave({
|
|
1651
|
+
await this._signalConnection.leave(ctx, {
|
|
2503
1652
|
topic,
|
|
2504
1653
|
peer: swarm.ownPeer
|
|
2505
1654
|
});
|
|
@@ -2510,15 +1659,10 @@ var SwarmNetworkManager = class {
|
|
|
2510
1659
|
await swarm.destroy();
|
|
2511
1660
|
this._swarms.delete(topic);
|
|
2512
1661
|
this.topicsUpdated.emit();
|
|
2513
|
-
|
|
2514
|
-
topic:
|
|
1662
|
+
log7("left", {
|
|
1663
|
+
topic: PublicKey7.from(topic),
|
|
2515
1664
|
count: this._swarms.size
|
|
2516
|
-
}, {
|
|
2517
|
-
F: __dxlog_file8,
|
|
2518
|
-
L: 225,
|
|
2519
|
-
S: this,
|
|
2520
|
-
C: (f, a) => f(...a)
|
|
2521
|
-
});
|
|
1665
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 151, S: this });
|
|
2522
1666
|
}
|
|
2523
1667
|
async setConnectionState(state) {
|
|
2524
1668
|
if (state === this._connectionState) {
|
|
@@ -2556,34 +1700,18 @@ _ts_decorate4([
|
|
|
2556
1700
|
|
|
2557
1701
|
// src/topology/fully-connected-topology.ts
|
|
2558
1702
|
import { invariant as invariant7 } from "@dxos/invariant";
|
|
2559
|
-
var
|
|
1703
|
+
var __dxlog_file8 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
|
|
2560
1704
|
var FullyConnectedTopology = class {
|
|
2561
1705
|
_controller;
|
|
2562
1706
|
toString() {
|
|
2563
1707
|
return "FullyConnectedTopology";
|
|
2564
1708
|
}
|
|
2565
1709
|
init(controller) {
|
|
2566
|
-
invariant7(!this._controller, "Already initialized", {
|
|
2567
|
-
F: __dxlog_file9,
|
|
2568
|
-
L: 18,
|
|
2569
|
-
S: this,
|
|
2570
|
-
A: [
|
|
2571
|
-
"!this._controller",
|
|
2572
|
-
"'Already initialized'"
|
|
2573
|
-
]
|
|
2574
|
-
});
|
|
1710
|
+
invariant7(!this._controller, "Already initialized", { "~LogMeta": "~LogMeta", F: __dxlog_file8, L: 11, S: this, A: ["!this._controller", "'Already initialized'"] });
|
|
2575
1711
|
this._controller = controller;
|
|
2576
1712
|
}
|
|
2577
1713
|
update() {
|
|
2578
|
-
invariant7(this._controller, "Not initialized", {
|
|
2579
|
-
F: __dxlog_file9,
|
|
2580
|
-
L: 23,
|
|
2581
|
-
S: this,
|
|
2582
|
-
A: [
|
|
2583
|
-
"this._controller",
|
|
2584
|
-
"'Not initialized'"
|
|
2585
|
-
]
|
|
2586
|
-
});
|
|
1714
|
+
invariant7(this._controller, "Not initialized", { "~LogMeta": "~LogMeta", F: __dxlog_file8, L: 15, S: this, A: ["this._controller", "'Not initialized'"] });
|
|
2587
1715
|
const { candidates: discovered } = this._controller.getState();
|
|
2588
1716
|
for (const peer of discovered) {
|
|
2589
1717
|
this._controller.connect(peer);
|
|
@@ -2596,329 +1724,21 @@ var FullyConnectedTopology = class {
|
|
|
2596
1724
|
}
|
|
2597
1725
|
};
|
|
2598
1726
|
|
|
2599
|
-
// src/topology/mmst-topology.ts
|
|
2600
|
-
import { invariant as invariant8 } from "@dxos/invariant";
|
|
2601
|
-
import { log as log9 } from "@dxos/log";
|
|
2602
|
-
var __dxlog_file10 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
|
|
2603
|
-
var MIN_UPDATE_INTERVAL = 1e3 * 10;
|
|
2604
|
-
var MAX_CHANGES_PER_UPDATE = 1;
|
|
2605
|
-
var MMSTTopology = class {
|
|
2606
|
-
_originateConnections;
|
|
2607
|
-
_maxPeers;
|
|
2608
|
-
_sampleSize;
|
|
2609
|
-
_controller;
|
|
2610
|
-
_sampleCollected = false;
|
|
2611
|
-
_lastAction = /* @__PURE__ */ new Date(0);
|
|
2612
|
-
constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
|
|
2613
|
-
this._originateConnections = originateConnections;
|
|
2614
|
-
this._maxPeers = maxPeers;
|
|
2615
|
-
this._sampleSize = sampleSize;
|
|
2616
|
-
}
|
|
2617
|
-
init(controller) {
|
|
2618
|
-
invariant8(!this._controller, "Already initialized", {
|
|
2619
|
-
F: __dxlog_file10,
|
|
2620
|
-
L: 49,
|
|
2621
|
-
S: this,
|
|
2622
|
-
A: [
|
|
2623
|
-
"!this._controller",
|
|
2624
|
-
"'Already initialized'"
|
|
2625
|
-
]
|
|
2626
|
-
});
|
|
2627
|
-
this._controller = controller;
|
|
2628
|
-
}
|
|
2629
|
-
update() {
|
|
2630
|
-
invariant8(this._controller, "Not initialized", {
|
|
2631
|
-
F: __dxlog_file10,
|
|
2632
|
-
L: 54,
|
|
2633
|
-
S: this,
|
|
2634
|
-
A: [
|
|
2635
|
-
"this._controller",
|
|
2636
|
-
"'Not initialized'"
|
|
2637
|
-
]
|
|
2638
|
-
});
|
|
2639
|
-
const { connected, candidates } = this._controller.getState();
|
|
2640
|
-
if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
|
|
2641
|
-
log9("Running the algorithm.", void 0, {
|
|
2642
|
-
F: __dxlog_file10,
|
|
2643
|
-
L: 58,
|
|
2644
|
-
S: this,
|
|
2645
|
-
C: (f, a) => f(...a)
|
|
2646
|
-
});
|
|
2647
|
-
this._sampleCollected = true;
|
|
2648
|
-
this._runAlgorithm();
|
|
2649
|
-
}
|
|
2650
|
-
}
|
|
2651
|
-
forceUpdate() {
|
|
2652
|
-
this._lastAction = /* @__PURE__ */ new Date(0);
|
|
2653
|
-
this.update();
|
|
2654
|
-
}
|
|
2655
|
-
async onOffer(peer) {
|
|
2656
|
-
invariant8(this._controller, "Not initialized", {
|
|
2657
|
-
F: __dxlog_file10,
|
|
2658
|
-
L: 70,
|
|
2659
|
-
S: this,
|
|
2660
|
-
A: [
|
|
2661
|
-
"this._controller",
|
|
2662
|
-
"'Not initialized'"
|
|
2663
|
-
]
|
|
2664
|
-
});
|
|
2665
|
-
const { connected } = this._controller.getState();
|
|
2666
|
-
const accept = connected.length < this._maxPeers;
|
|
2667
|
-
log9(`Offer ${peer} accept=${accept}`, void 0, {
|
|
2668
|
-
F: __dxlog_file10,
|
|
2669
|
-
L: 73,
|
|
2670
|
-
S: this,
|
|
2671
|
-
C: (f, a) => f(...a)
|
|
2672
|
-
});
|
|
2673
|
-
return accept;
|
|
2674
|
-
}
|
|
2675
|
-
async destroy() {
|
|
2676
|
-
}
|
|
2677
|
-
_runAlgorithm() {
|
|
2678
|
-
invariant8(this._controller, "Not initialized", {
|
|
2679
|
-
F: __dxlog_file10,
|
|
2680
|
-
L: 82,
|
|
2681
|
-
S: this,
|
|
2682
|
-
A: [
|
|
2683
|
-
"this._controller",
|
|
2684
|
-
"'Not initialized'"
|
|
2685
|
-
]
|
|
2686
|
-
});
|
|
2687
|
-
const { connected, candidates, ownPeerId } = this._controller.getState();
|
|
2688
|
-
if (connected.length > this._maxPeers) {
|
|
2689
|
-
log9(`disconnect ${connected.length - this._maxPeers} peers.`, void 0, {
|
|
2690
|
-
F: __dxlog_file10,
|
|
2691
|
-
L: 88,
|
|
2692
|
-
S: this,
|
|
2693
|
-
C: (f, a) => f(...a)
|
|
2694
|
-
});
|
|
2695
|
-
const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
|
|
2696
|
-
invariant8(sorted.length === 0, void 0, {
|
|
2697
|
-
F: __dxlog_file10,
|
|
2698
|
-
L: 92,
|
|
2699
|
-
S: this,
|
|
2700
|
-
A: [
|
|
2701
|
-
"sorted.length === 0",
|
|
2702
|
-
""
|
|
2703
|
-
]
|
|
2704
|
-
});
|
|
2705
|
-
if (sorted.length > MAX_CHANGES_PER_UPDATE) {
|
|
2706
|
-
log9(`want to disconnect ${sorted.length} peers but limited to ${MAX_CHANGES_PER_UPDATE}`, void 0, {
|
|
2707
|
-
F: __dxlog_file10,
|
|
2708
|
-
L: 95,
|
|
2709
|
-
S: this,
|
|
2710
|
-
C: (f, a) => f(...a)
|
|
2711
|
-
});
|
|
2712
|
-
}
|
|
2713
|
-
if (Date.now() - this._lastAction.getTime() > MIN_UPDATE_INTERVAL) {
|
|
2714
|
-
for (const peer of sorted.slice(0, MAX_CHANGES_PER_UPDATE)) {
|
|
2715
|
-
log9(`Disconnect ${peer}.`, void 0, {
|
|
2716
|
-
F: __dxlog_file10,
|
|
2717
|
-
L: 100,
|
|
2718
|
-
S: this,
|
|
2719
|
-
C: (f, a) => f(...a)
|
|
2720
|
-
});
|
|
2721
|
-
this._controller.disconnect(peer);
|
|
2722
|
-
}
|
|
2723
|
-
this._lastAction = /* @__PURE__ */ new Date();
|
|
2724
|
-
} else {
|
|
2725
|
-
log9("rate limited disconnect", void 0, {
|
|
2726
|
-
F: __dxlog_file10,
|
|
2727
|
-
L: 105,
|
|
2728
|
-
S: this,
|
|
2729
|
-
C: (f, a) => f(...a)
|
|
2730
|
-
});
|
|
2731
|
-
}
|
|
2732
|
-
} else if (connected.length < this._originateConnections) {
|
|
2733
|
-
log9(`connect ${this._originateConnections - connected.length} peers.`, void 0, {
|
|
2734
|
-
F: __dxlog_file10,
|
|
2735
|
-
L: 109,
|
|
2736
|
-
S: this,
|
|
2737
|
-
C: (f, a) => f(...a)
|
|
2738
|
-
});
|
|
2739
|
-
const sample = candidates.sort(() => Math.random() - 0.5).slice(0, this._sampleSize);
|
|
2740
|
-
const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
|
|
2741
|
-
if (sorted.length > MAX_CHANGES_PER_UPDATE) {
|
|
2742
|
-
log9(`want to connect ${sorted.length} peers but limited to ${MAX_CHANGES_PER_UPDATE}`, void 0, {
|
|
2743
|
-
F: __dxlog_file10,
|
|
2744
|
-
L: 114,
|
|
2745
|
-
S: this,
|
|
2746
|
-
C: (f, a) => f(...a)
|
|
2747
|
-
});
|
|
2748
|
-
}
|
|
2749
|
-
if (Date.now() - this._lastAction.getTime() > MIN_UPDATE_INTERVAL) {
|
|
2750
|
-
for (const peer of sorted.slice(0, MAX_CHANGES_PER_UPDATE)) {
|
|
2751
|
-
log9(`Connect ${peer}.`, void 0, {
|
|
2752
|
-
F: __dxlog_file10,
|
|
2753
|
-
L: 118,
|
|
2754
|
-
S: this,
|
|
2755
|
-
C: (f, a) => f(...a)
|
|
2756
|
-
});
|
|
2757
|
-
this._controller.connect(peer);
|
|
2758
|
-
}
|
|
2759
|
-
this._lastAction = /* @__PURE__ */ new Date();
|
|
2760
|
-
} else {
|
|
2761
|
-
log9("rate limited connect", void 0, {
|
|
2762
|
-
F: __dxlog_file10,
|
|
2763
|
-
L: 123,
|
|
2764
|
-
S: this,
|
|
2765
|
-
C: (f, a) => f(...a)
|
|
2766
|
-
});
|
|
2767
|
-
}
|
|
2768
|
-
}
|
|
2769
|
-
}
|
|
2770
|
-
toString() {
|
|
2771
|
-
return "MMSTTopology";
|
|
2772
|
-
}
|
|
2773
|
-
};
|
|
2774
|
-
var sortByXorDistance = (keys, reference) => {
|
|
2775
|
-
const sorted = keys.sort((a, b) => {
|
|
2776
|
-
return compareXor(distXor(a.asBuffer(), reference.asBuffer()), distXor(b.asBuffer(), reference.asBuffer()));
|
|
2777
|
-
});
|
|
2778
|
-
log9("Sorted keys", {
|
|
2779
|
-
keys,
|
|
2780
|
-
reference,
|
|
2781
|
-
sorted
|
|
2782
|
-
}, {
|
|
2783
|
-
F: __dxlog_file10,
|
|
2784
|
-
L: 137,
|
|
2785
|
-
S: void 0,
|
|
2786
|
-
C: (f, a) => f(...a)
|
|
2787
|
-
});
|
|
2788
|
-
return sorted;
|
|
2789
|
-
};
|
|
2790
|
-
var distXor = (a, b) => {
|
|
2791
|
-
const maxLength = Math.max(a.length, b.length);
|
|
2792
|
-
const result = Buffer.allocUnsafe(maxLength);
|
|
2793
|
-
for (let i = 0; i < maxLength; i++) {
|
|
2794
|
-
result[i] = (a[i] || 0) ^ (b[i] || 0);
|
|
2795
|
-
}
|
|
2796
|
-
return result;
|
|
2797
|
-
};
|
|
2798
|
-
var compareXor = (a, b) => {
|
|
2799
|
-
const maxLength = Math.max(a.length, b.length);
|
|
2800
|
-
for (let i = 0; i < maxLength; i++) {
|
|
2801
|
-
if ((a[i] || 0) === (b[i] || 0)) {
|
|
2802
|
-
continue;
|
|
2803
|
-
}
|
|
2804
|
-
return (a[i] || 0) < (b[i] || 0) ? -1 : 1;
|
|
2805
|
-
}
|
|
2806
|
-
return 0;
|
|
2807
|
-
};
|
|
2808
|
-
|
|
2809
|
-
// src/topology/star-topology.ts
|
|
2810
|
-
import { invariant as invariant9 } from "@dxos/invariant";
|
|
2811
|
-
import { log as log10 } from "@dxos/log";
|
|
2812
|
-
var __dxlog_file11 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
|
|
2813
|
-
var StarTopology = class {
|
|
2814
|
-
_centralPeer;
|
|
2815
|
-
_controller;
|
|
2816
|
-
constructor(_centralPeer) {
|
|
2817
|
-
this._centralPeer = _centralPeer;
|
|
2818
|
-
}
|
|
2819
|
-
toString() {
|
|
2820
|
-
return `StarTopology(${this._centralPeer.truncate()})`;
|
|
2821
|
-
}
|
|
2822
|
-
init(controller) {
|
|
2823
|
-
invariant9(!this._controller, "Already initialized.", {
|
|
2824
|
-
F: __dxlog_file11,
|
|
2825
|
-
L: 21,
|
|
2826
|
-
S: this,
|
|
2827
|
-
A: [
|
|
2828
|
-
"!this._controller",
|
|
2829
|
-
"'Already initialized.'"
|
|
2830
|
-
]
|
|
2831
|
-
});
|
|
2832
|
-
this._controller = controller;
|
|
2833
|
-
}
|
|
2834
|
-
update() {
|
|
2835
|
-
invariant9(this._controller, "Not initialized.", {
|
|
2836
|
-
F: __dxlog_file11,
|
|
2837
|
-
L: 26,
|
|
2838
|
-
S: this,
|
|
2839
|
-
A: [
|
|
2840
|
-
"this._controller",
|
|
2841
|
-
"'Not initialized.'"
|
|
2842
|
-
]
|
|
2843
|
-
});
|
|
2844
|
-
const { candidates, connected, ownPeerId } = this._controller.getState();
|
|
2845
|
-
if (!ownPeerId.equals(this._centralPeer)) {
|
|
2846
|
-
log10("leaf peer dropping all connections apart from central peer.", void 0, {
|
|
2847
|
-
F: __dxlog_file11,
|
|
2848
|
-
L: 29,
|
|
2849
|
-
S: this,
|
|
2850
|
-
C: (f, a) => f(...a)
|
|
2851
|
-
});
|
|
2852
|
-
for (const peer of connected) {
|
|
2853
|
-
if (!peer.equals(this._centralPeer)) {
|
|
2854
|
-
log10("dropping connection", {
|
|
2855
|
-
peer
|
|
2856
|
-
}, {
|
|
2857
|
-
F: __dxlog_file11,
|
|
2858
|
-
L: 34,
|
|
2859
|
-
S: this,
|
|
2860
|
-
C: (f, a) => f(...a)
|
|
2861
|
-
});
|
|
2862
|
-
this._controller.disconnect(peer);
|
|
2863
|
-
}
|
|
2864
|
-
}
|
|
2865
|
-
}
|
|
2866
|
-
for (const peer of candidates) {
|
|
2867
|
-
if (peer.equals(this._centralPeer) || ownPeerId.equals(this._centralPeer)) {
|
|
2868
|
-
log10("connecting to peer", {
|
|
2869
|
-
peer
|
|
2870
|
-
}, {
|
|
2871
|
-
F: __dxlog_file11,
|
|
2872
|
-
L: 43,
|
|
2873
|
-
S: this,
|
|
2874
|
-
C: (f, a) => f(...a)
|
|
2875
|
-
});
|
|
2876
|
-
this._controller.connect(peer);
|
|
2877
|
-
}
|
|
2878
|
-
}
|
|
2879
|
-
}
|
|
2880
|
-
async onOffer(peer) {
|
|
2881
|
-
invariant9(this._controller, "Not initialized.", {
|
|
2882
|
-
F: __dxlog_file11,
|
|
2883
|
-
L: 50,
|
|
2884
|
-
S: this,
|
|
2885
|
-
A: [
|
|
2886
|
-
"this._controller",
|
|
2887
|
-
"'Not initialized.'"
|
|
2888
|
-
]
|
|
2889
|
-
});
|
|
2890
|
-
const { ownPeerId } = this._controller.getState();
|
|
2891
|
-
log10("offer", {
|
|
2892
|
-
peer,
|
|
2893
|
-
isCentral: peer.equals(this._centralPeer),
|
|
2894
|
-
isSelfCentral: ownPeerId.equals(this._centralPeer)
|
|
2895
|
-
}, {
|
|
2896
|
-
F: __dxlog_file11,
|
|
2897
|
-
L: 52,
|
|
2898
|
-
S: this,
|
|
2899
|
-
C: (f, a) => f(...a)
|
|
2900
|
-
});
|
|
2901
|
-
return ownPeerId.equals(this._centralPeer) || peer.equals(this._centralPeer);
|
|
2902
|
-
}
|
|
2903
|
-
async destroy() {
|
|
2904
|
-
}
|
|
2905
|
-
};
|
|
2906
|
-
|
|
2907
1727
|
// src/transport/memory-transport.ts
|
|
2908
1728
|
import { Transform } from "@dxos/node-std/stream";
|
|
2909
1729
|
import { Event as Event7, Trigger as Trigger2 } from "@dxos/async";
|
|
2910
1730
|
import { ErrorStream as ErrorStream3 } from "@dxos/debug";
|
|
2911
|
-
import { invariant as
|
|
2912
|
-
import { PublicKey as
|
|
2913
|
-
import { log as
|
|
1731
|
+
import { invariant as invariant8 } from "@dxos/invariant";
|
|
1732
|
+
import { PublicKey as PublicKey8 } from "@dxos/keys";
|
|
1733
|
+
import { log as log8, logInfo as logInfo3 } from "@dxos/log";
|
|
2914
1734
|
import { ComplexMap as ComplexMap7 } from "@dxos/util";
|
|
1735
|
+
var __dxlog_file9 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
|
|
2915
1736
|
function _ts_decorate5(decorators, target, key, desc) {
|
|
2916
1737
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2917
1738
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
2918
1739
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2919
1740
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2920
1741
|
}
|
|
2921
|
-
var __dxlog_file12 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
|
|
2922
1742
|
var MEMORY_TRANSPORT_DELAY = 1;
|
|
2923
1743
|
var createStreamDelay = (delay) => {
|
|
2924
1744
|
return new Transform({
|
|
@@ -2934,55 +1754,29 @@ var MemoryTransportFactory = {
|
|
|
2934
1754
|
var MemoryTransport = class _MemoryTransport {
|
|
2935
1755
|
_options;
|
|
2936
1756
|
// TODO(burdon): Remove static properties (inject context into constructor).
|
|
2937
|
-
static _connections = new ComplexMap7(
|
|
2938
|
-
_instanceId;
|
|
2939
|
-
_remote;
|
|
2940
|
-
_outgoingDelay;
|
|
2941
|
-
_incomingDelay;
|
|
2942
|
-
_closed;
|
|
1757
|
+
static _connections = new ComplexMap7(PublicKey8.hash);
|
|
1758
|
+
_instanceId = PublicKey8.random();
|
|
1759
|
+
_remote = new Trigger2();
|
|
1760
|
+
_outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
1761
|
+
_incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
1762
|
+
_closed = false;
|
|
2943
1763
|
_remoteInstanceId;
|
|
2944
1764
|
_remoteConnection;
|
|
2945
|
-
closed;
|
|
2946
|
-
connected;
|
|
2947
|
-
errors;
|
|
1765
|
+
closed = new Event7();
|
|
1766
|
+
connected = new Event7();
|
|
1767
|
+
errors = new ErrorStream3();
|
|
2948
1768
|
constructor(_options) {
|
|
2949
1769
|
this._options = _options;
|
|
2950
|
-
this._instanceId
|
|
2951
|
-
this._remote = new Trigger2();
|
|
2952
|
-
this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
2953
|
-
this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
2954
|
-
this._closed = false;
|
|
2955
|
-
this.closed = new Event7();
|
|
2956
|
-
this.connected = new Event7();
|
|
2957
|
-
this.errors = new ErrorStream3();
|
|
2958
|
-
invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
|
|
2959
|
-
F: __dxlog_file12,
|
|
2960
|
-
L: 64,
|
|
2961
|
-
S: this,
|
|
2962
|
-
A: [
|
|
2963
|
-
"!MemoryTransport._connections.has(this._instanceId)",
|
|
2964
|
-
"'Duplicate memory connection'"
|
|
2965
|
-
]
|
|
2966
|
-
});
|
|
1770
|
+
invariant8(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 51, S: this, A: ["!MemoryTransport._connections.has(this._instanceId)", "'Duplicate memory connection'"] });
|
|
2967
1771
|
_MemoryTransport._connections.set(this._instanceId, this);
|
|
2968
1772
|
}
|
|
2969
1773
|
get isOpen() {
|
|
2970
1774
|
return !this._closed;
|
|
2971
1775
|
}
|
|
2972
1776
|
async open() {
|
|
2973
|
-
|
|
2974
|
-
F: __dxlog_file12,
|
|
2975
|
-
L: 74,
|
|
2976
|
-
S: this,
|
|
2977
|
-
C: (f, a) => f(...a)
|
|
2978
|
-
});
|
|
1777
|
+
log8("opening...", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 59, S: this });
|
|
2979
1778
|
if (this._options.initiator) {
|
|
2980
|
-
|
|
2981
|
-
F: __dxlog_file12,
|
|
2982
|
-
L: 78,
|
|
2983
|
-
S: this,
|
|
2984
|
-
C: (f, a) => f(...a)
|
|
2985
|
-
});
|
|
1779
|
+
log8("sending signal", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 62, S: this });
|
|
2986
1780
|
try {
|
|
2987
1781
|
await this._options.sendSignal({
|
|
2988
1782
|
payload: {
|
|
@@ -3008,23 +1802,10 @@ var MemoryTransport = class _MemoryTransport {
|
|
|
3008
1802
|
this.closed.emit();
|
|
3009
1803
|
return;
|
|
3010
1804
|
}
|
|
3011
|
-
|
|
3012
|
-
F: __dxlog_file12,
|
|
3013
|
-
L: 104,
|
|
3014
|
-
S: this,
|
|
3015
|
-
A: [
|
|
3016
|
-
"!this._remoteConnection._remoteConnection",
|
|
3017
|
-
"`Remote already connected: ${this._remoteInstanceId}`"
|
|
3018
|
-
]
|
|
3019
|
-
});
|
|
1805
|
+
invariant8(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 90, S: this, A: ["!this._remoteConnection._remoteConnection", "`Remote already connected: ${this._remoteInstanceId}`"] });
|
|
3020
1806
|
this._remoteConnection._remoteConnection = this;
|
|
3021
1807
|
this._remoteConnection._remoteInstanceId = this._instanceId;
|
|
3022
|
-
|
|
3023
|
-
F: __dxlog_file12,
|
|
3024
|
-
L: 108,
|
|
3025
|
-
S: this,
|
|
3026
|
-
C: (f, a) => f(...a)
|
|
3027
|
-
});
|
|
1808
|
+
log8("connected", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 93, S: this });
|
|
3028
1809
|
this._options.stream.pipe(this._outgoingDelay).pipe(this._remoteConnection._options.stream).pipe(this._incomingDelay).pipe(this._options.stream);
|
|
3029
1810
|
this.connected.emit();
|
|
3030
1811
|
this._remoteConnection.connected.emit();
|
|
@@ -3038,12 +1819,7 @@ var MemoryTransport = class _MemoryTransport {
|
|
|
3038
1819
|
return this;
|
|
3039
1820
|
}
|
|
3040
1821
|
async close() {
|
|
3041
|
-
|
|
3042
|
-
F: __dxlog_file12,
|
|
3043
|
-
L: 130,
|
|
3044
|
-
S: this,
|
|
3045
|
-
C: (f, a) => f(...a)
|
|
3046
|
-
});
|
|
1822
|
+
log8("closing...", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 107, S: this });
|
|
3047
1823
|
this._closed = true;
|
|
3048
1824
|
_MemoryTransport._connections.delete(this._instanceId);
|
|
3049
1825
|
if (this._remoteConnection) {
|
|
@@ -3059,29 +1835,19 @@ var MemoryTransport = class _MemoryTransport {
|
|
|
3059
1835
|
this._remoteConnection = void 0;
|
|
3060
1836
|
}
|
|
3061
1837
|
this.closed.emit();
|
|
3062
|
-
|
|
3063
|
-
F: __dxlog_file12,
|
|
3064
|
-
L: 158,
|
|
3065
|
-
S: this,
|
|
3066
|
-
C: (f, a) => f(...a)
|
|
3067
|
-
});
|
|
1838
|
+
log8("closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 130, S: this });
|
|
3068
1839
|
return this;
|
|
3069
1840
|
}
|
|
3070
1841
|
async onSignal({ payload }) {
|
|
3071
|
-
|
|
1842
|
+
log8("received signal", {
|
|
3072
1843
|
payload
|
|
3073
|
-
}, {
|
|
3074
|
-
F: __dxlog_file12,
|
|
3075
|
-
L: 163,
|
|
3076
|
-
S: this,
|
|
3077
|
-
C: (f, a) => f(...a)
|
|
3078
|
-
});
|
|
1844
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 134, S: this });
|
|
3079
1845
|
if (!payload?.transportId) {
|
|
3080
1846
|
return;
|
|
3081
1847
|
}
|
|
3082
1848
|
const transportId = payload.transportId;
|
|
3083
1849
|
if (transportId) {
|
|
3084
|
-
const remoteId =
|
|
1850
|
+
const remoteId = PublicKey8.fromHex(transportId);
|
|
3085
1851
|
this._remote.wake(remoteId);
|
|
3086
1852
|
}
|
|
3087
1853
|
}
|
|
@@ -3106,13 +1872,13 @@ _ts_decorate5([
|
|
|
3106
1872
|
var toError = (err) => err instanceof Error ? err : new Error(String(err));
|
|
3107
1873
|
|
|
3108
1874
|
// src/transport/transport.ts
|
|
3109
|
-
var TransportKind = /* @__PURE__ */ function(TransportKind2) {
|
|
1875
|
+
var TransportKind = /* @__PURE__ */ (function(TransportKind2) {
|
|
3110
1876
|
TransportKind2["WEB_RTC"] = "WEB-RTC";
|
|
3111
1877
|
TransportKind2["WEB_RTC_PROXY"] = "WEB-RTC_PROXY";
|
|
3112
1878
|
TransportKind2["MEMORY"] = "MEMORY";
|
|
3113
1879
|
TransportKind2["TCP"] = "TCP";
|
|
3114
1880
|
return TransportKind2;
|
|
3115
|
-
}({});
|
|
1881
|
+
})({});
|
|
3116
1882
|
|
|
3117
1883
|
// src/transport/webrtc/rtc-connection-factory.ts
|
|
3118
1884
|
import { Mutex } from "@dxos/async";
|
|
@@ -3161,18 +1927,18 @@ var getRtcConnectionFactory = () => {
|
|
|
3161
1927
|
|
|
3162
1928
|
// src/transport/webrtc/rtc-peer-connection.ts
|
|
3163
1929
|
import { Mutex as Mutex2, Trigger as Trigger3, synchronized as synchronized5 } from "@dxos/async";
|
|
3164
|
-
import { invariant as
|
|
3165
|
-
import { log as
|
|
1930
|
+
import { invariant as invariant10 } from "@dxos/invariant";
|
|
1931
|
+
import { log as log10, logInfo as logInfo4 } from "@dxos/log";
|
|
3166
1932
|
import { ConnectivityError as ConnectivityError3 } from "@dxos/protocols";
|
|
3167
|
-
import { trace
|
|
1933
|
+
import { trace } from "@dxos/tracing";
|
|
3168
1934
|
|
|
3169
1935
|
// src/transport/webrtc/rtc-transport-channel.ts
|
|
3170
1936
|
import { Duplex } from "@dxos/node-std/stream";
|
|
3171
1937
|
import { Event as AsyncEvent } from "@dxos/async";
|
|
3172
1938
|
import { Resource } from "@dxos/context";
|
|
3173
1939
|
import { ErrorStream as ErrorStream4 } from "@dxos/debug";
|
|
3174
|
-
import { invariant as
|
|
3175
|
-
import { log as
|
|
1940
|
+
import { invariant as invariant9 } from "@dxos/invariant";
|
|
1941
|
+
import { log as log9 } from "@dxos/log";
|
|
3176
1942
|
import { ConnectivityError as ConnectivityError2 } from "@dxos/protocols";
|
|
3177
1943
|
|
|
3178
1944
|
// src/transport/webrtc/rtc-transport-stats.ts
|
|
@@ -3223,21 +1989,21 @@ var getRtcConnectionStats = async (connection, channelTopic) => {
|
|
|
3223
1989
|
};
|
|
3224
1990
|
|
|
3225
1991
|
// src/transport/webrtc/rtc-transport-channel.ts
|
|
3226
|
-
var
|
|
1992
|
+
var __dxlog_file10 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-channel.ts";
|
|
3227
1993
|
var MAX_MESSAGE_SIZE = 64 * 1024;
|
|
3228
1994
|
var MAX_BUFFERED_AMOUNT = 64 * 1024;
|
|
3229
1995
|
var RtcTransportChannel = class extends Resource {
|
|
3230
1996
|
_connection;
|
|
3231
1997
|
_options;
|
|
3232
|
-
closed;
|
|
3233
|
-
connected;
|
|
3234
|
-
errors;
|
|
1998
|
+
closed = new AsyncEvent();
|
|
1999
|
+
connected = new AsyncEvent();
|
|
2000
|
+
errors = new ErrorStream4();
|
|
3235
2001
|
_channel;
|
|
3236
2002
|
_stream;
|
|
3237
|
-
_streamDataFlushedCallback;
|
|
3238
|
-
_isChannelCreationInProgress;
|
|
2003
|
+
_streamDataFlushedCallback = null;
|
|
2004
|
+
_isChannelCreationInProgress = false;
|
|
3239
2005
|
constructor(_connection, _options) {
|
|
3240
|
-
super(), this._connection = _connection, this._options = _options
|
|
2006
|
+
super(), this._connection = _connection, this._options = _options;
|
|
3241
2007
|
}
|
|
3242
2008
|
get isRtcChannelCreationInProgress() {
|
|
3243
2009
|
return this._isChannelCreationInProgress;
|
|
@@ -3248,15 +2014,7 @@ var RtcTransportChannel = class extends Resource {
|
|
|
3248
2014
|
}
|
|
3249
2015
|
}
|
|
3250
2016
|
async _open() {
|
|
3251
|
-
|
|
3252
|
-
F: __dxlog_file13,
|
|
3253
|
-
L: 57,
|
|
3254
|
-
S: this,
|
|
3255
|
-
A: [
|
|
3256
|
-
"!this._isChannelCreationInProgress",
|
|
3257
|
-
""
|
|
3258
|
-
]
|
|
3259
|
-
});
|
|
2017
|
+
invariant9(!this._isChannelCreationInProgress, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 41, S: this, A: ["!this._isChannelCreationInProgress", ""] });
|
|
3260
2018
|
this._isChannelCreationInProgress = true;
|
|
3261
2019
|
this._connection.createDataChannel(this._options.topic).then((channel) => {
|
|
3262
2020
|
if (this.isOpen) {
|
|
@@ -3267,17 +2025,14 @@ var RtcTransportChannel = class extends Resource {
|
|
|
3267
2025
|
}
|
|
3268
2026
|
}).catch((err) => {
|
|
3269
2027
|
if (this.isOpen) {
|
|
3270
|
-
const error = err instanceof Error ? err : new ConnectivityError2(
|
|
2028
|
+
const error = err instanceof Error ? err : new ConnectivityError2({
|
|
2029
|
+
message: `Failed to create a channel: ${JSON.stringify(err?.message)}`
|
|
2030
|
+
});
|
|
3271
2031
|
this.errors.raise(error);
|
|
3272
2032
|
} else {
|
|
3273
|
-
|
|
2033
|
+
log9.verbose("connection establishment failed after transport was closed", {
|
|
3274
2034
|
err
|
|
3275
|
-
}, {
|
|
3276
|
-
F: __dxlog_file13,
|
|
3277
|
-
L: 77,
|
|
3278
|
-
S: this,
|
|
3279
|
-
C: (f, a) => f(...a)
|
|
3280
|
-
});
|
|
2035
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 57, S: this });
|
|
3281
2036
|
}
|
|
3282
2037
|
}).finally(() => {
|
|
3283
2038
|
this._isChannelCreationInProgress = false;
|
|
@@ -3290,34 +2045,19 @@ var RtcTransportChannel = class extends Resource {
|
|
|
3290
2045
|
this._stream = void 0;
|
|
3291
2046
|
}
|
|
3292
2047
|
this.closed.emit();
|
|
3293
|
-
|
|
3294
|
-
F: __dxlog_file13,
|
|
3295
|
-
L: 93,
|
|
3296
|
-
S: this,
|
|
3297
|
-
C: (f, a) => f(...a)
|
|
3298
|
-
});
|
|
2048
|
+
log9("closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 72, S: this });
|
|
3299
2049
|
}
|
|
3300
2050
|
_initChannel(channel) {
|
|
3301
2051
|
Object.assign(channel, {
|
|
3302
2052
|
onopen: () => {
|
|
3303
2053
|
if (!this.isOpen) {
|
|
3304
|
-
|
|
2054
|
+
log9.warn("channel opened in a closed transport", {
|
|
3305
2055
|
topic: this._options.topic
|
|
3306
|
-
}, {
|
|
3307
|
-
F: __dxlog_file13,
|
|
3308
|
-
L: 100,
|
|
3309
|
-
S: this,
|
|
3310
|
-
C: (f, a) => f(...a)
|
|
3311
|
-
});
|
|
2056
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 78, S: this });
|
|
3312
2057
|
this._safeCloseChannel(channel);
|
|
3313
2058
|
return;
|
|
3314
2059
|
}
|
|
3315
|
-
|
|
3316
|
-
F: __dxlog_file13,
|
|
3317
|
-
L: 105,
|
|
3318
|
-
S: this,
|
|
3319
|
-
C: (f, a) => f(...a)
|
|
3320
|
-
});
|
|
2060
|
+
log9("onopen", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 84, S: this });
|
|
3321
2061
|
const duplex = new Duplex({
|
|
3322
2062
|
read: () => {
|
|
3323
2063
|
},
|
|
@@ -3330,22 +2070,12 @@ var RtcTransportChannel = class extends Resource {
|
|
|
3330
2070
|
this.connected.emit();
|
|
3331
2071
|
},
|
|
3332
2072
|
onclose: async () => {
|
|
3333
|
-
|
|
3334
|
-
F: __dxlog_file13,
|
|
3335
|
-
L: 118,
|
|
3336
|
-
S: this,
|
|
3337
|
-
C: (f, a) => f(...a)
|
|
3338
|
-
});
|
|
2073
|
+
log9("onclose", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 96, S: this });
|
|
3339
2074
|
await this.close();
|
|
3340
2075
|
},
|
|
3341
2076
|
onmessage: async (event) => {
|
|
3342
2077
|
if (!this._stream) {
|
|
3343
|
-
|
|
3344
|
-
F: __dxlog_file13,
|
|
3345
|
-
L: 124,
|
|
3346
|
-
S: this,
|
|
3347
|
-
C: (f, a) => f(...a)
|
|
3348
|
-
});
|
|
2078
|
+
log9.warn("ignoring message on a closed channel", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 101, S: this });
|
|
3349
2079
|
return;
|
|
3350
2080
|
}
|
|
3351
2081
|
let data = event.data;
|
|
@@ -3371,12 +2101,7 @@ var RtcTransportChannel = class extends Resource {
|
|
|
3371
2101
|
}
|
|
3372
2102
|
async _handleChannelWrite(chunk, callback) {
|
|
3373
2103
|
if (!this._channel) {
|
|
3374
|
-
|
|
3375
|
-
F: __dxlog_file13,
|
|
3376
|
-
L: 154,
|
|
3377
|
-
S: this,
|
|
3378
|
-
C: (f, a) => f(...a)
|
|
3379
|
-
});
|
|
2104
|
+
log9.warn("writing to a channel after a connection was closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 127, S: this });
|
|
3380
2105
|
return;
|
|
3381
2106
|
}
|
|
3382
2107
|
if (chunk.length > MAX_MESSAGE_SIZE) {
|
|
@@ -3394,12 +2119,7 @@ var RtcTransportChannel = class extends Resource {
|
|
|
3394
2119
|
}
|
|
3395
2120
|
if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
|
|
3396
2121
|
if (this._streamDataFlushedCallback !== null) {
|
|
3397
|
-
|
|
3398
|
-
F: __dxlog_file13,
|
|
3399
|
-
L: 175,
|
|
3400
|
-
S: this,
|
|
3401
|
-
C: (f, a) => f(...a)
|
|
3402
|
-
});
|
|
2122
|
+
log9.error("consumer trying to write before we are ready for more data", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 145, S: this });
|
|
3403
2123
|
}
|
|
3404
2124
|
this._streamDataFlushedCallback = callback;
|
|
3405
2125
|
} else {
|
|
@@ -3410,12 +2130,7 @@ var RtcTransportChannel = class extends Resource {
|
|
|
3410
2130
|
try {
|
|
3411
2131
|
channel.close();
|
|
3412
2132
|
} catch (error) {
|
|
3413
|
-
|
|
3414
|
-
F: __dxlog_file13,
|
|
3415
|
-
L: 187,
|
|
3416
|
-
S: this,
|
|
3417
|
-
C: (f, a) => f(...a)
|
|
3418
|
-
});
|
|
2133
|
+
log9.catch(error, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 156, S: this });
|
|
3419
2134
|
}
|
|
3420
2135
|
}
|
|
3421
2136
|
onSignal(signal) {
|
|
@@ -3456,24 +2171,24 @@ var deduplicatedSdpLines = (sdp) => {
|
|
|
3456
2171
|
};
|
|
3457
2172
|
|
|
3458
2173
|
// src/transport/webrtc/rtc-peer-connection.ts
|
|
2174
|
+
var __dxlog_file11 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-peer-connection.ts";
|
|
3459
2175
|
function _ts_decorate6(decorators, target, key, desc) {
|
|
3460
2176
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3461
2177
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
3462
2178
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
3463
2179
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
3464
2180
|
}
|
|
3465
|
-
var __dxlog_file14 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-peer-connection.ts";
|
|
3466
2181
|
var RtcPeerConnection = class {
|
|
3467
2182
|
_factory;
|
|
3468
2183
|
_options;
|
|
3469
2184
|
// A peer who is not the initiator waits for another party to open a channel.
|
|
3470
|
-
_channelCreatedCallbacks;
|
|
2185
|
+
_channelCreatedCallbacks = /* @__PURE__ */ new Map();
|
|
3471
2186
|
// Channels indexed by topic.
|
|
3472
|
-
_transportChannels;
|
|
3473
|
-
_dataChannels;
|
|
2187
|
+
_transportChannels = /* @__PURE__ */ new Map();
|
|
2188
|
+
_dataChannels = /* @__PURE__ */ new Map();
|
|
3474
2189
|
// A peer is ready to receive ICE candidates when local and remote description were set.
|
|
3475
|
-
_readyForCandidates;
|
|
3476
|
-
_offerProcessingMutex;
|
|
2190
|
+
_readyForCandidates = new Trigger3();
|
|
2191
|
+
_offerProcessingMutex = new Mutex2();
|
|
3477
2192
|
/**
|
|
3478
2193
|
* Can't use peer.connection.initiator, because if two connections to the same peer are created in
|
|
3479
2194
|
* different swarms, we might be the initiator of the first one, but not of the other one.
|
|
@@ -3485,11 +2200,6 @@ var RtcPeerConnection = class {
|
|
|
3485
2200
|
constructor(_factory, _options) {
|
|
3486
2201
|
this._factory = _factory;
|
|
3487
2202
|
this._options = _options;
|
|
3488
|
-
this._channelCreatedCallbacks = /* @__PURE__ */ new Map();
|
|
3489
|
-
this._transportChannels = /* @__PURE__ */ new Map();
|
|
3490
|
-
this._dataChannels = /* @__PURE__ */ new Map();
|
|
3491
|
-
this._readyForCandidates = new Trigger3();
|
|
3492
|
-
this._offerProcessingMutex = new Mutex2();
|
|
3493
2203
|
this._initiator = chooseInitiatorPeer(_options.ownPeerKey, _options.remotePeerKey) === _options.ownPeerKey;
|
|
3494
2204
|
}
|
|
3495
2205
|
get transportChannelCount() {
|
|
@@ -3515,12 +2225,7 @@ var RtcPeerConnection = class {
|
|
|
3515
2225
|
if (existingChannel) {
|
|
3516
2226
|
return existingChannel;
|
|
3517
2227
|
}
|
|
3518
|
-
|
|
3519
|
-
F: __dxlog_file14,
|
|
3520
|
-
L: 96,
|
|
3521
|
-
S: this,
|
|
3522
|
-
C: (f, a) => f(...a)
|
|
3523
|
-
});
|
|
2228
|
+
log10("waiting for initiator-peer to open a data channel", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 63, S: this });
|
|
3524
2229
|
return new Promise((resolve, reject) => {
|
|
3525
2230
|
this._channelCreatedCallbacks.set(topic, {
|
|
3526
2231
|
resolve,
|
|
@@ -3544,38 +2249,20 @@ var RtcPeerConnection = class {
|
|
|
3544
2249
|
if (this._connection) {
|
|
3545
2250
|
return this._connection;
|
|
3546
2251
|
}
|
|
3547
|
-
|
|
2252
|
+
log10("initializing connection...", () => ({
|
|
3548
2253
|
remotePeer: this._options.remotePeerKey
|
|
3549
|
-
}), {
|
|
3550
|
-
F: __dxlog_file14,
|
|
3551
|
-
L: 121,
|
|
3552
|
-
S: this,
|
|
3553
|
-
C: (f, a) => f(...a)
|
|
3554
|
-
});
|
|
2254
|
+
}), { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 87, S: this });
|
|
3555
2255
|
const config = await this._loadConnectionConfig();
|
|
3556
2256
|
const connection = await this._factory.createConnection(config);
|
|
3557
2257
|
const iceCandidateErrors = [];
|
|
3558
2258
|
Object.assign(connection, {
|
|
3559
2259
|
onnegotiationneeded: async () => {
|
|
3560
|
-
|
|
3561
|
-
F: __dxlog_file14,
|
|
3562
|
-
L: 136,
|
|
3563
|
-
S: this,
|
|
3564
|
-
A: [
|
|
3565
|
-
"this._initiator",
|
|
3566
|
-
""
|
|
3567
|
-
]
|
|
3568
|
-
});
|
|
2260
|
+
invariant10(this._initiator, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 100, S: this, A: ["this._initiator", ""] });
|
|
3569
2261
|
if (connection !== this._connection) {
|
|
3570
2262
|
this._onConnectionCallbackAfterClose("onnegotiationneeded", connection);
|
|
3571
2263
|
return;
|
|
3572
2264
|
}
|
|
3573
|
-
|
|
3574
|
-
F: __dxlog_file14,
|
|
3575
|
-
L: 143,
|
|
3576
|
-
S: this,
|
|
3577
|
-
C: (f, a) => f(...a)
|
|
3578
|
-
});
|
|
2265
|
+
log10("onnegotiationneeded", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 105, S: this });
|
|
3579
2266
|
try {
|
|
3580
2267
|
const offer = await connection.createOffer();
|
|
3581
2268
|
await connection.setLocalDescription(offer);
|
|
@@ -3592,22 +2279,12 @@ var RtcPeerConnection = class {
|
|
|
3592
2279
|
return;
|
|
3593
2280
|
}
|
|
3594
2281
|
if (event.candidate) {
|
|
3595
|
-
|
|
2282
|
+
log10("onicecandidate", {
|
|
3596
2283
|
candidate: event.candidate.candidate
|
|
3597
|
-
}, {
|
|
3598
|
-
F: __dxlog_file14,
|
|
3599
|
-
L: 162,
|
|
3600
|
-
S: this,
|
|
3601
|
-
C: (f, a) => f(...a)
|
|
3602
|
-
});
|
|
2284
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 122, S: this });
|
|
3603
2285
|
await this._sendIceCandidate(event.candidate);
|
|
3604
2286
|
} else {
|
|
3605
|
-
|
|
3606
|
-
F: __dxlog_file14,
|
|
3607
|
-
L: 165,
|
|
3608
|
-
S: this,
|
|
3609
|
-
C: (f, a) => f(...a)
|
|
3610
|
-
});
|
|
2287
|
+
log10("onicecandidate gathering complete", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 127, S: this });
|
|
3611
2288
|
}
|
|
3612
2289
|
},
|
|
3613
2290
|
// When error occurs while performing ICE negotiations through a STUN or TURN server.
|
|
@@ -3628,14 +2305,9 @@ var RtcPeerConnection = class {
|
|
|
3628
2305
|
this._onConnectionCallbackAfterClose("oniceconnectionstatechange", connection);
|
|
3629
2306
|
return;
|
|
3630
2307
|
}
|
|
3631
|
-
|
|
2308
|
+
log10("oniceconnectionstatechange", {
|
|
3632
2309
|
state: connection.iceConnectionState
|
|
3633
|
-
}, {
|
|
3634
|
-
F: __dxlog_file14,
|
|
3635
|
-
L: 185,
|
|
3636
|
-
S: this,
|
|
3637
|
-
C: (f, a) => f(...a)
|
|
3638
|
-
});
|
|
2310
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 148, S: this });
|
|
3639
2311
|
if (connection.iceConnectionState === "failed") {
|
|
3640
2312
|
void this._lockAndAbort(connection, createIceFailureError(iceCandidateErrors));
|
|
3641
2313
|
}
|
|
@@ -3650,52 +2322,29 @@ var RtcPeerConnection = class {
|
|
|
3650
2322
|
}
|
|
3651
2323
|
return;
|
|
3652
2324
|
}
|
|
3653
|
-
|
|
2325
|
+
log10("onconnectionstatechange", {
|
|
3654
2326
|
state: connection.connectionState
|
|
3655
|
-
}, {
|
|
3656
|
-
F: __dxlog_file14,
|
|
3657
|
-
L: 202,
|
|
3658
|
-
S: this,
|
|
3659
|
-
C: (f, a) => f(...a)
|
|
3660
|
-
});
|
|
2327
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 165, S: this });
|
|
3661
2328
|
if (connection.connectionState === "failed") {
|
|
3662
2329
|
void this._lockAndAbort(connection, new Error("Connection failed."));
|
|
3663
2330
|
}
|
|
3664
2331
|
},
|
|
3665
2332
|
onsignalingstatechange: () => {
|
|
3666
|
-
|
|
2333
|
+
log10("onsignalingstatechange", {
|
|
3667
2334
|
state: connection.signalingState
|
|
3668
|
-
}, {
|
|
3669
|
-
F: __dxlog_file14,
|
|
3670
|
-
L: 209,
|
|
3671
|
-
S: this,
|
|
3672
|
-
C: (f, a) => f(...a)
|
|
3673
|
-
});
|
|
2335
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 173, S: this });
|
|
3674
2336
|
},
|
|
3675
2337
|
// When channel is added to connection.
|
|
3676
2338
|
// https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/datachannel_event
|
|
3677
2339
|
ondatachannel: (event) => {
|
|
3678
|
-
|
|
3679
|
-
F: __dxlog_file14,
|
|
3680
|
-
L: 215,
|
|
3681
|
-
S: this,
|
|
3682
|
-
A: [
|
|
3683
|
-
"!this._initiator",
|
|
3684
|
-
"'Initiator is expected to create data channels.'"
|
|
3685
|
-
]
|
|
3686
|
-
});
|
|
2340
|
+
invariant10(!this._initiator, "Initiator is expected to create data channels.", { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 180, S: this, A: ["!this._initiator", "'Initiator is expected to create data channels.'"] });
|
|
3687
2341
|
if (connection !== this._connection) {
|
|
3688
2342
|
this._onConnectionCallbackAfterClose("ondatachannel", connection);
|
|
3689
2343
|
return;
|
|
3690
2344
|
}
|
|
3691
|
-
|
|
2345
|
+
log10("ondatachannel", {
|
|
3692
2346
|
label: event.channel.label
|
|
3693
|
-
}, {
|
|
3694
|
-
F: __dxlog_file14,
|
|
3695
|
-
L: 222,
|
|
3696
|
-
S: this,
|
|
3697
|
-
C: (f, a) => f(...a)
|
|
3698
|
-
});
|
|
2347
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 185, S: this });
|
|
3699
2348
|
this._dataChannels.set(event.channel.label, event.channel);
|
|
3700
2349
|
const pendingCallback = this._channelCreatedCallbacks.get(event.channel.label);
|
|
3701
2350
|
if (pendingCallback) {
|
|
@@ -3716,14 +2365,9 @@ var RtcPeerConnection = class {
|
|
|
3716
2365
|
}
|
|
3717
2366
|
_abortConnection(connection, error) {
|
|
3718
2367
|
if (connection !== this._connection) {
|
|
3719
|
-
|
|
2368
|
+
log10.error("attempted to abort an inactive connection", {
|
|
3720
2369
|
error
|
|
3721
|
-
}, {
|
|
3722
|
-
F: __dxlog_file14,
|
|
3723
|
-
L: 247,
|
|
3724
|
-
S: this,
|
|
3725
|
-
C: (f, a) => f(...a)
|
|
3726
|
-
});
|
|
2370
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 208, S: this });
|
|
3727
2371
|
this._safeCloseConnection(connection);
|
|
3728
2372
|
return;
|
|
3729
2373
|
}
|
|
@@ -3737,46 +2381,23 @@ var RtcPeerConnection = class {
|
|
|
3737
2381
|
}
|
|
3738
2382
|
this._transportChannels.clear();
|
|
3739
2383
|
this._safeCloseConnection();
|
|
3740
|
-
|
|
2384
|
+
log10("connection aborted", {
|
|
3741
2385
|
reason: error.message
|
|
3742
|
-
}, {
|
|
3743
|
-
F: __dxlog_file14,
|
|
3744
|
-
L: 261,
|
|
3745
|
-
S: this,
|
|
3746
|
-
C: (f, a) => f(...a)
|
|
3747
|
-
});
|
|
2386
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 224, S: this });
|
|
3748
2387
|
}
|
|
3749
2388
|
async _lockAndCloseConnection() {
|
|
3750
|
-
|
|
3751
|
-
F: __dxlog_file14,
|
|
3752
|
-
L: 266,
|
|
3753
|
-
S: this,
|
|
3754
|
-
A: [
|
|
3755
|
-
"this._transportChannels.size === 0",
|
|
3756
|
-
""
|
|
3757
|
-
]
|
|
3758
|
-
});
|
|
2389
|
+
invariant10(this._transportChannels.size === 0, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 229, S: this, A: ["this._transportChannels.size === 0", ""] });
|
|
3759
2390
|
if (this._connection) {
|
|
3760
2391
|
this._safeCloseConnection();
|
|
3761
|
-
|
|
3762
|
-
F: __dxlog_file14,
|
|
3763
|
-
L: 269,
|
|
3764
|
-
S: this,
|
|
3765
|
-
C: (f, a) => f(...a)
|
|
3766
|
-
});
|
|
2392
|
+
log10("connection closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 232, S: this });
|
|
3767
2393
|
}
|
|
3768
2394
|
}
|
|
3769
2395
|
async onSignal(signal) {
|
|
3770
2396
|
const connection = this._connection;
|
|
3771
2397
|
if (!connection) {
|
|
3772
|
-
|
|
2398
|
+
log10.warn("a signal ignored because the connection was closed", {
|
|
3773
2399
|
type: signal.payload.data.type
|
|
3774
|
-
}, {
|
|
3775
|
-
F: __dxlog_file14,
|
|
3776
|
-
L: 277,
|
|
3777
|
-
S: this,
|
|
3778
|
-
C: (f, a) => f(...a)
|
|
3779
|
-
});
|
|
2400
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 238, S: this });
|
|
3780
2401
|
return;
|
|
3781
2402
|
}
|
|
3782
2403
|
const data = signal.payload.data;
|
|
@@ -3836,66 +2457,36 @@ var RtcPeerConnection = class {
|
|
|
3836
2457
|
this._abortConnection(connection, new Error(`Unknown signal type ${data.type}.`));
|
|
3837
2458
|
break;
|
|
3838
2459
|
}
|
|
3839
|
-
|
|
2460
|
+
log10("signal processed", {
|
|
3840
2461
|
type: data.type
|
|
3841
|
-
}, {
|
|
3842
|
-
F: __dxlog_file14,
|
|
3843
|
-
L: 336,
|
|
3844
|
-
S: this,
|
|
3845
|
-
C: (f, a) => f(...a)
|
|
3846
|
-
});
|
|
2462
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 301, S: this });
|
|
3847
2463
|
}
|
|
3848
2464
|
async _processIceCandidate(connection, candidate) {
|
|
3849
2465
|
try {
|
|
3850
2466
|
await this._readyForCandidates.wait();
|
|
3851
2467
|
if (connection === this._connection) {
|
|
3852
|
-
|
|
2468
|
+
log10("adding ice candidate", {
|
|
3853
2469
|
candidate
|
|
3854
|
-
}, {
|
|
3855
|
-
F: __dxlog_file14,
|
|
3856
|
-
L: 344,
|
|
3857
|
-
S: this,
|
|
3858
|
-
C: (f, a) => f(...a)
|
|
3859
|
-
});
|
|
2470
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 310, S: this });
|
|
3860
2471
|
await connection.addIceCandidate(candidate);
|
|
3861
2472
|
}
|
|
3862
2473
|
} catch (err) {
|
|
3863
|
-
|
|
3864
|
-
F: __dxlog_file14,
|
|
3865
|
-
L: 348,
|
|
3866
|
-
S: this,
|
|
3867
|
-
C: (f, a) => f(...a)
|
|
3868
|
-
});
|
|
2474
|
+
log10.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 316, S: this });
|
|
3869
2475
|
}
|
|
3870
2476
|
}
|
|
3871
2477
|
_onSessionNegotiated(connection) {
|
|
3872
2478
|
if (connection === this._connection) {
|
|
3873
|
-
|
|
3874
|
-
F: __dxlog_file14,
|
|
3875
|
-
L: 354,
|
|
3876
|
-
S: this,
|
|
3877
|
-
C: (f, a) => f(...a)
|
|
3878
|
-
});
|
|
2479
|
+
log10("ready to process ice candidates", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 321, S: this });
|
|
3879
2480
|
this._readyForCandidates.wake();
|
|
3880
2481
|
} else {
|
|
3881
|
-
|
|
3882
|
-
F: __dxlog_file14,
|
|
3883
|
-
L: 357,
|
|
3884
|
-
S: this,
|
|
3885
|
-
C: (f, a) => f(...a)
|
|
3886
|
-
});
|
|
2482
|
+
log10.warn("session was negotiated after connection became inactive", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 324, S: this });
|
|
3887
2483
|
}
|
|
3888
2484
|
}
|
|
3889
2485
|
_onConnectionCallbackAfterClose(callback, connection) {
|
|
3890
|
-
|
|
2486
|
+
log10.warn("callback invoked after a connection was destroyed, this is probably a bug", {
|
|
3891
2487
|
callback,
|
|
3892
2488
|
state: connection.connectionState
|
|
3893
|
-
}, {
|
|
3894
|
-
F: __dxlog_file14,
|
|
3895
|
-
L: 362,
|
|
3896
|
-
S: this,
|
|
3897
|
-
C: (f, a) => f(...a)
|
|
3898
|
-
});
|
|
2489
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 328, S: this });
|
|
3899
2490
|
this._safeCloseConnection(connection);
|
|
3900
2491
|
}
|
|
3901
2492
|
_safeCloseConnection(connection = this._connection) {
|
|
@@ -3903,23 +2494,13 @@ var RtcPeerConnection = class {
|
|
|
3903
2494
|
try {
|
|
3904
2495
|
connection?.close();
|
|
3905
2496
|
} catch (err) {
|
|
3906
|
-
|
|
3907
|
-
F: __dxlog_file14,
|
|
3908
|
-
L: 374,
|
|
3909
|
-
S: this,
|
|
3910
|
-
C: (f, a) => f(...a)
|
|
3911
|
-
});
|
|
2497
|
+
log10.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 339, S: this });
|
|
3912
2498
|
}
|
|
3913
2499
|
if (resetFields) {
|
|
3914
2500
|
this._connection = void 0;
|
|
3915
2501
|
this._dataChannels.clear();
|
|
3916
2502
|
this._readyForCandidates.wake();
|
|
3917
|
-
void this._factory.onConnectionDestroyed().catch((err) =>
|
|
3918
|
-
F: __dxlog_file14,
|
|
3919
|
-
L: 380,
|
|
3920
|
-
S: this,
|
|
3921
|
-
C: (f, a) => f(...a)
|
|
3922
|
-
}));
|
|
2503
|
+
void this._factory.onConnectionDestroyed().catch((err) => log10.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 345, S: this }));
|
|
3923
2504
|
for (const [_, pendingCallback] of this._channelCreatedCallbacks.entries()) {
|
|
3924
2505
|
pendingCallback.reject("Connection closed.");
|
|
3925
2506
|
}
|
|
@@ -3939,12 +2520,7 @@ var RtcPeerConnection = class {
|
|
|
3939
2520
|
];
|
|
3940
2521
|
}
|
|
3941
2522
|
} catch (error) {
|
|
3942
|
-
|
|
3943
|
-
F: __dxlog_file14,
|
|
3944
|
-
L: 396,
|
|
3945
|
-
S: this,
|
|
3946
|
-
C: (f, a) => f(...a)
|
|
3947
|
-
});
|
|
2523
|
+
log10.catch(error, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 365, S: this });
|
|
3948
2524
|
}
|
|
3949
2525
|
return config;
|
|
3950
2526
|
}
|
|
@@ -3964,14 +2540,9 @@ var RtcPeerConnection = class {
|
|
|
3964
2540
|
}
|
|
3965
2541
|
});
|
|
3966
2542
|
} catch (err) {
|
|
3967
|
-
|
|
2543
|
+
log10.warn("signaling error", {
|
|
3968
2544
|
err
|
|
3969
|
-
}, {
|
|
3970
|
-
F: __dxlog_file14,
|
|
3971
|
-
L: 417,
|
|
3972
|
-
S: this,
|
|
3973
|
-
C: (f, a) => f(...a)
|
|
3974
|
-
});
|
|
2545
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 385, S: this });
|
|
3975
2546
|
}
|
|
3976
2547
|
}
|
|
3977
2548
|
async _sendDescription(connection, description) {
|
|
@@ -4029,13 +2600,13 @@ _ts_decorate6([
|
|
|
4029
2600
|
synchronized5
|
|
4030
2601
|
], RtcPeerConnection.prototype, "onSignal", null);
|
|
4031
2602
|
_ts_decorate6([
|
|
4032
|
-
|
|
2603
|
+
trace.info()
|
|
4033
2604
|
], RtcPeerConnection.prototype, "_connectionInfo", null);
|
|
4034
2605
|
_ts_decorate6([
|
|
4035
2606
|
logInfo4
|
|
4036
2607
|
], RtcPeerConnection.prototype, "_loggerContext", null);
|
|
4037
2608
|
RtcPeerConnection = _ts_decorate6([
|
|
4038
|
-
|
|
2609
|
+
trace.resource()
|
|
4039
2610
|
], RtcPeerConnection);
|
|
4040
2611
|
var isRemoteDescriptionSet = (connection, data) => {
|
|
4041
2612
|
if (!connection.remoteDescription?.type || connection.remoteDescription?.type !== data.type) {
|
|
@@ -4045,8 +2616,10 @@ var isRemoteDescriptionSet = (connection, data) => {
|
|
|
4045
2616
|
};
|
|
4046
2617
|
var createIceFailureError = (details) => {
|
|
4047
2618
|
const candidateErrors = details.map(({ url, errorCode, errorText }) => `${errorCode} ${url}: ${errorText}`);
|
|
4048
|
-
return new ConnectivityError3(
|
|
4049
|
-
|
|
2619
|
+
return new ConnectivityError3({
|
|
2620
|
+
message: `ICE failed:
|
|
2621
|
+
${candidateErrors.join("\n")}`
|
|
2622
|
+
});
|
|
4050
2623
|
};
|
|
4051
2624
|
|
|
4052
2625
|
// src/transport/webrtc/rtc-transport-factory.ts
|
|
@@ -4072,25 +2645,25 @@ import { Writable } from "@dxos/node-std/stream";
|
|
|
4072
2645
|
import { Event as Event8, scheduleTask as scheduleTask4 } from "@dxos/async";
|
|
4073
2646
|
import { Resource as Resource2 } from "@dxos/context";
|
|
4074
2647
|
import { ErrorStream as ErrorStream5 } from "@dxos/debug";
|
|
4075
|
-
import { invariant as
|
|
4076
|
-
import { PublicKey as
|
|
4077
|
-
import { log as
|
|
2648
|
+
import { invariant as invariant11 } from "@dxos/invariant";
|
|
2649
|
+
import { PublicKey as PublicKey9 } from "@dxos/keys";
|
|
2650
|
+
import { log as log11 } from "@dxos/log";
|
|
4078
2651
|
import { ConnectionResetError as ConnectionResetError2, ConnectivityError as ConnectivityError4, TimeoutError as TimeoutError3 } from "@dxos/protocols";
|
|
4079
2652
|
import { ConnectionState as ConnectionState3 } from "@dxos/protocols/proto/dxos/mesh/bridge";
|
|
4080
2653
|
import { arrayToBuffer } from "@dxos/util";
|
|
4081
|
-
var
|
|
2654
|
+
var __dxlog_file12 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-proxy.ts";
|
|
4082
2655
|
var RPC_TIMEOUT = 1e4;
|
|
4083
2656
|
var CLOSE_RPC_TIMEOUT = 3e3;
|
|
4084
2657
|
var RESP_MIN_THRESHOLD = 500;
|
|
4085
2658
|
var RtcTransportProxy = class extends Resource2 {
|
|
4086
2659
|
_options;
|
|
4087
|
-
_proxyId;
|
|
4088
|
-
closed;
|
|
4089
|
-
connected;
|
|
4090
|
-
errors;
|
|
2660
|
+
_proxyId = PublicKey9.random();
|
|
2661
|
+
closed = new Event8();
|
|
2662
|
+
connected = new Event8();
|
|
2663
|
+
errors = new ErrorStream5();
|
|
4091
2664
|
_serviceStream;
|
|
4092
2665
|
constructor(_options) {
|
|
4093
|
-
super(), this._options = _options
|
|
2666
|
+
super(), this._options = _options;
|
|
4094
2667
|
}
|
|
4095
2668
|
async _open() {
|
|
4096
2669
|
let stream;
|
|
@@ -4111,12 +2684,7 @@ var RtcTransportProxy = class extends Resource2 {
|
|
|
4111
2684
|
this._serviceStream = stream;
|
|
4112
2685
|
stream.waitUntilReady().then(() => {
|
|
4113
2686
|
stream.subscribe(async (event) => {
|
|
4114
|
-
|
|
4115
|
-
F: __dxlog_file15,
|
|
4116
|
-
L: 66,
|
|
4117
|
-
S: this,
|
|
4118
|
-
C: (f, a) => f(...a)
|
|
4119
|
-
});
|
|
2687
|
+
log11("rtc transport proxy event", event, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 46, S: this });
|
|
4120
2688
|
if (event.connection) {
|
|
4121
2689
|
await this._handleConnection(event.connection);
|
|
4122
2690
|
} else if (event.data) {
|
|
@@ -4125,14 +2693,9 @@ var RtcTransportProxy = class extends Resource2 {
|
|
|
4125
2693
|
await this._handleSignal(event.signal);
|
|
4126
2694
|
}
|
|
4127
2695
|
}, (err) => {
|
|
4128
|
-
|
|
2696
|
+
log11("rtc bridge stream closed", {
|
|
4129
2697
|
err
|
|
4130
|
-
}, {
|
|
4131
|
-
F: __dxlog_file15,
|
|
4132
|
-
L: 76,
|
|
4133
|
-
S: this,
|
|
4134
|
-
C: (f, a) => f(...a)
|
|
4135
|
-
});
|
|
2698
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 55, S: this });
|
|
4136
2699
|
if (err) {
|
|
4137
2700
|
this._raiseIfOpen(err);
|
|
4138
2701
|
} else {
|
|
@@ -4149,12 +2712,7 @@ var RtcTransportProxy = class extends Resource2 {
|
|
|
4149
2712
|
timeout: RPC_TIMEOUT
|
|
4150
2713
|
}).then(() => {
|
|
4151
2714
|
if (Date.now() - sendStartMs > RESP_MIN_THRESHOLD) {
|
|
4152
|
-
|
|
4153
|
-
F: __dxlog_file15,
|
|
4154
|
-
L: 93,
|
|
4155
|
-
S: this,
|
|
4156
|
-
C: (f, a) => f(...a)
|
|
4157
|
-
});
|
|
2715
|
+
log11("slow response, delaying callback", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 74, S: this });
|
|
4158
2716
|
scheduleTask4(this._ctx, () => callback(), RESP_MIN_THRESHOLD);
|
|
4159
2717
|
} else {
|
|
4160
2718
|
callback();
|
|
@@ -4182,12 +2740,7 @@ var RtcTransportProxy = class extends Resource2 {
|
|
|
4182
2740
|
await this._serviceStream?.close();
|
|
4183
2741
|
this._serviceStream = void 0;
|
|
4184
2742
|
} catch (err) {
|
|
4185
|
-
|
|
4186
|
-
F: __dxlog_file15,
|
|
4187
|
-
L: 128,
|
|
4188
|
-
S: this,
|
|
4189
|
-
C: (f, a) => f(...a)
|
|
4190
|
-
});
|
|
2743
|
+
log11.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 102, S: this });
|
|
4191
2744
|
}
|
|
4192
2745
|
try {
|
|
4193
2746
|
await this._options.bridgeService.close({
|
|
@@ -4196,12 +2749,7 @@ var RtcTransportProxy = class extends Resource2 {
|
|
|
4196
2749
|
timeout: CLOSE_RPC_TIMEOUT
|
|
4197
2750
|
});
|
|
4198
2751
|
} catch (err) {
|
|
4199
|
-
|
|
4200
|
-
F: __dxlog_file15,
|
|
4201
|
-
L: 134,
|
|
4202
|
-
S: this,
|
|
4203
|
-
C: (f, a) => f(...a)
|
|
4204
|
-
});
|
|
2752
|
+
log11.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 111, S: this });
|
|
4205
2753
|
}
|
|
4206
2754
|
this.closed.emit();
|
|
4207
2755
|
}
|
|
@@ -4242,7 +2790,9 @@ var RtcTransportProxy = class extends Resource2 {
|
|
|
4242
2790
|
} catch (error) {
|
|
4243
2791
|
const type = signalEvent.payload.payload.data?.type;
|
|
4244
2792
|
if (type === "offer" || type === "answer") {
|
|
4245
|
-
this._raiseIfOpen(new ConnectivityError4(
|
|
2793
|
+
this._raiseIfOpen(new ConnectivityError4({
|
|
2794
|
+
message: `Session establishment failed: ${type} couldn't be sent.`
|
|
2795
|
+
}));
|
|
4246
2796
|
}
|
|
4247
2797
|
}
|
|
4248
2798
|
}
|
|
@@ -4280,14 +2830,9 @@ var RtcTransportProxy = class extends Resource2 {
|
|
|
4280
2830
|
if (this.isOpen) {
|
|
4281
2831
|
this.errors.raise(error);
|
|
4282
2832
|
} else {
|
|
4283
|
-
|
|
2833
|
+
log11.info("error swallowed because transport was closed", {
|
|
4284
2834
|
message: error.message
|
|
4285
|
-
}, {
|
|
4286
|
-
F: __dxlog_file15,
|
|
4287
|
-
L: 215,
|
|
4288
|
-
S: this,
|
|
4289
|
-
C: (f, a) => f(...a)
|
|
4290
|
-
});
|
|
2835
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 195, S: this });
|
|
4291
2836
|
}
|
|
4292
2837
|
}
|
|
4293
2838
|
/**
|
|
@@ -4313,15 +2858,7 @@ var RtcTransportProxyFactory = class {
|
|
|
4313
2858
|
return this;
|
|
4314
2859
|
}
|
|
4315
2860
|
createTransport(options) {
|
|
4316
|
-
|
|
4317
|
-
F: __dxlog_file15,
|
|
4318
|
-
L: 245,
|
|
4319
|
-
S: this,
|
|
4320
|
-
A: [
|
|
4321
|
-
"this._bridgeService",
|
|
4322
|
-
"'RtcTransportProxyFactory is not ready to open connections'"
|
|
4323
|
-
]
|
|
4324
|
-
});
|
|
2861
|
+
invariant11(this._bridgeService, "RtcTransportProxyFactory is not ready to open connections", { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 221, S: this, A: ["this._bridgeService", "'RtcTransportProxyFactory is not ready to open connections'"] });
|
|
4325
2862
|
const transport = new RtcTransportProxy({
|
|
4326
2863
|
...options,
|
|
4327
2864
|
bridgeService: this._bridgeService
|
|
@@ -4336,11 +2873,17 @@ var RtcTransportProxyFactory = class {
|
|
|
4336
2873
|
var decodeError = (err) => {
|
|
4337
2874
|
const message = typeof err === "string" ? err : err.message;
|
|
4338
2875
|
if (message.includes("CONNECTION_RESET")) {
|
|
4339
|
-
return new ConnectionResetError2(
|
|
2876
|
+
return new ConnectionResetError2({
|
|
2877
|
+
message
|
|
2878
|
+
});
|
|
4340
2879
|
} else if (message.includes("TIMEOUT")) {
|
|
4341
|
-
return new TimeoutError3(
|
|
2880
|
+
return new TimeoutError3({
|
|
2881
|
+
message
|
|
2882
|
+
});
|
|
4342
2883
|
} else if (message.includes("CONNECTIVITY_ERROR")) {
|
|
4343
|
-
return new ConnectivityError4(
|
|
2884
|
+
return new ConnectivityError4({
|
|
2885
|
+
message
|
|
2886
|
+
});
|
|
4344
2887
|
} else {
|
|
4345
2888
|
return typeof err === "string" ? new Error(err) : err;
|
|
4346
2889
|
}
|
|
@@ -4349,18 +2892,17 @@ var decodeError = (err) => {
|
|
|
4349
2892
|
// src/transport/webrtc/rtc-transport-service.ts
|
|
4350
2893
|
import { Duplex as Duplex2 } from "@dxos/node-std/stream";
|
|
4351
2894
|
import { Stream } from "@dxos/codec-protobuf/stream";
|
|
4352
|
-
import { invariant as
|
|
4353
|
-
import { PublicKey as
|
|
4354
|
-
import { log as
|
|
2895
|
+
import { invariant as invariant12 } from "@dxos/invariant";
|
|
2896
|
+
import { PublicKey as PublicKey10 } from "@dxos/keys";
|
|
2897
|
+
import { log as log12 } from "@dxos/log";
|
|
4355
2898
|
import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
|
|
4356
2899
|
import { ComplexMap as ComplexMap8 } from "@dxos/util";
|
|
4357
|
-
var
|
|
2900
|
+
var __dxlog_file13 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-service.ts";
|
|
4358
2901
|
var RtcTransportService = class {
|
|
4359
2902
|
_transportFactory;
|
|
4360
|
-
_openTransports;
|
|
2903
|
+
_openTransports = new ComplexMap8(PublicKey10.hash);
|
|
4361
2904
|
constructor(webrtcConfig, iceProvider, _transportFactory = createRtcTransportFactory(webrtcConfig, iceProvider)) {
|
|
4362
2905
|
this._transportFactory = _transportFactory;
|
|
4363
|
-
this._openTransports = new ComplexMap8(PublicKey11.hash);
|
|
4364
2906
|
}
|
|
4365
2907
|
hasOpenTransports() {
|
|
4366
2908
|
return this._openTransports.size > 0;
|
|
@@ -4368,12 +2910,7 @@ var RtcTransportService = class {
|
|
|
4368
2910
|
open(request) {
|
|
4369
2911
|
const existingTransport = this._openTransports.get(request.proxyId);
|
|
4370
2912
|
if (existingTransport) {
|
|
4371
|
-
|
|
4372
|
-
F: __dxlog_file16,
|
|
4373
|
-
L: 54,
|
|
4374
|
-
S: this,
|
|
4375
|
-
C: (f, a) => f(...a)
|
|
4376
|
-
});
|
|
2913
|
+
log12.error("requesting a new transport bridge for an existing proxy", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 24, S: this });
|
|
4377
2914
|
void this._safeCloseTransport(existingTransport);
|
|
4378
2915
|
this._openTransports.delete(request.proxyId);
|
|
4379
2916
|
}
|
|
@@ -4434,69 +2971,32 @@ var RtcTransportService = class {
|
|
|
4434
2971
|
close(err);
|
|
4435
2972
|
});
|
|
4436
2973
|
ready();
|
|
4437
|
-
|
|
4438
|
-
F: __dxlog_file16,
|
|
4439
|
-
L: 116,
|
|
4440
|
-
S: this,
|
|
4441
|
-
C: (f, a) => f(...a)
|
|
4442
|
-
});
|
|
2974
|
+
log12("stream ready", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 85, S: this });
|
|
4443
2975
|
pushNewState(ConnectionState4.CONNECTING);
|
|
4444
2976
|
});
|
|
4445
2977
|
}
|
|
4446
2978
|
async sendSignal({ proxyId, signal }) {
|
|
4447
2979
|
const transport = this._openTransports.get(proxyId);
|
|
4448
|
-
|
|
4449
|
-
F: __dxlog_file16,
|
|
4450
|
-
L: 124,
|
|
4451
|
-
S: this,
|
|
4452
|
-
A: [
|
|
4453
|
-
"transport",
|
|
4454
|
-
""
|
|
4455
|
-
]
|
|
4456
|
-
});
|
|
2980
|
+
invariant12(transport, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 91, S: this, A: ["transport", ""] });
|
|
4457
2981
|
await transport.transport.onSignal(signal);
|
|
4458
2982
|
}
|
|
4459
2983
|
async getDetails({ proxyId }) {
|
|
4460
2984
|
const transport = this._openTransports.get(proxyId);
|
|
4461
|
-
|
|
4462
|
-
F: __dxlog_file16,
|
|
4463
|
-
L: 131,
|
|
4464
|
-
S: this,
|
|
4465
|
-
A: [
|
|
4466
|
-
"transport",
|
|
4467
|
-
""
|
|
4468
|
-
]
|
|
4469
|
-
});
|
|
2985
|
+
invariant12(transport, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 96, S: this, A: ["transport", ""] });
|
|
4470
2986
|
return {
|
|
4471
2987
|
details: await transport.transport.getDetails()
|
|
4472
2988
|
};
|
|
4473
2989
|
}
|
|
4474
2990
|
async getStats({ proxyId }) {
|
|
4475
2991
|
const transport = this._openTransports.get(proxyId);
|
|
4476
|
-
|
|
4477
|
-
F: __dxlog_file16,
|
|
4478
|
-
L: 138,
|
|
4479
|
-
S: this,
|
|
4480
|
-
A: [
|
|
4481
|
-
"transport",
|
|
4482
|
-
""
|
|
4483
|
-
]
|
|
4484
|
-
});
|
|
2992
|
+
invariant12(transport, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 103, S: this, A: ["transport", ""] });
|
|
4485
2993
|
return {
|
|
4486
2994
|
stats: await transport.transport.getStats()
|
|
4487
2995
|
};
|
|
4488
2996
|
}
|
|
4489
2997
|
async sendData({ proxyId, payload }) {
|
|
4490
2998
|
const transport = this._openTransports.get(proxyId);
|
|
4491
|
-
|
|
4492
|
-
F: __dxlog_file16,
|
|
4493
|
-
L: 145,
|
|
4494
|
-
S: this,
|
|
4495
|
-
A: [
|
|
4496
|
-
"transport",
|
|
4497
|
-
""
|
|
4498
|
-
]
|
|
4499
|
-
});
|
|
2999
|
+
invariant12(transport, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 110, S: this, A: ["transport", ""] });
|
|
4500
3000
|
const bufferHasSpace = transport.connectorStream.push(payload);
|
|
4501
3001
|
if (!bufferHasSpace) {
|
|
4502
3002
|
await new Promise((resolve) => {
|
|
@@ -4520,33 +3020,18 @@ var RtcTransportService = class {
|
|
|
4520
3020
|
try {
|
|
4521
3021
|
await transport.transport.close();
|
|
4522
3022
|
} catch (error) {
|
|
4523
|
-
|
|
3023
|
+
log12.warn("transport close error", {
|
|
4524
3024
|
message: error?.message
|
|
4525
|
-
}, {
|
|
4526
|
-
F: __dxlog_file16,
|
|
4527
|
-
L: 175,
|
|
4528
|
-
S: this,
|
|
4529
|
-
C: (f, a) => f(...a)
|
|
4530
|
-
});
|
|
3025
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 134, S: this });
|
|
4531
3026
|
}
|
|
4532
3027
|
try {
|
|
4533
3028
|
transport.connectorStream.end();
|
|
4534
3029
|
} catch (error) {
|
|
4535
|
-
|
|
3030
|
+
log12.warn("connectorStream close error", {
|
|
4536
3031
|
message: error?.message
|
|
4537
|
-
}, {
|
|
4538
|
-
F: __dxlog_file16,
|
|
4539
|
-
L: 180,
|
|
4540
|
-
S: this,
|
|
4541
|
-
C: (f, a) => f(...a)
|
|
4542
|
-
});
|
|
3032
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 141, S: this });
|
|
4543
3033
|
}
|
|
4544
|
-
|
|
4545
|
-
F: __dxlog_file16,
|
|
4546
|
-
L: 182,
|
|
4547
|
-
S: this,
|
|
4548
|
-
C: (f, a) => f(...a)
|
|
4549
|
-
});
|
|
3034
|
+
log12("closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 145, S: this });
|
|
4550
3035
|
}
|
|
4551
3036
|
};
|
|
4552
3037
|
var createStateUpdater = (next) => {
|
|
@@ -4564,10 +3049,10 @@ var createStateUpdater = (next) => {
|
|
|
4564
3049
|
|
|
4565
3050
|
// src/wire-protocol.ts
|
|
4566
3051
|
import { Teleport } from "@dxos/teleport";
|
|
4567
|
-
var createTeleportProtocolFactory = (onConnection,
|
|
3052
|
+
var createTeleportProtocolFactory = (onConnection, defaultProps) => {
|
|
4568
3053
|
return (params) => {
|
|
4569
3054
|
const teleport = new Teleport({
|
|
4570
|
-
...
|
|
3055
|
+
...defaultProps,
|
|
4571
3056
|
...params
|
|
4572
3057
|
});
|
|
4573
3058
|
return {
|
|
@@ -4589,7 +3074,6 @@ var createTeleportProtocolFactory = (onConnection, defaultParams) => {
|
|
|
4589
3074
|
export {
|
|
4590
3075
|
ConnectionState,
|
|
4591
3076
|
Connection,
|
|
4592
|
-
createIceProvider,
|
|
4593
3077
|
SwarmMessenger,
|
|
4594
3078
|
Swarm,
|
|
4595
3079
|
SwarmMapper,
|
|
@@ -4599,8 +3083,6 @@ export {
|
|
|
4599
3083
|
ConnectionLog,
|
|
4600
3084
|
SwarmNetworkManager,
|
|
4601
3085
|
FullyConnectedTopology,
|
|
4602
|
-
MMSTTopology,
|
|
4603
|
-
StarTopology,
|
|
4604
3086
|
MemoryTransportFactory,
|
|
4605
3087
|
MemoryTransport,
|
|
4606
3088
|
TransportKind,
|
|
@@ -4610,4 +3092,4 @@ export {
|
|
|
4610
3092
|
RtcTransportService,
|
|
4611
3093
|
createTeleportProtocolFactory
|
|
4612
3094
|
};
|
|
4613
|
-
//# sourceMappingURL=chunk-
|
|
3095
|
+
//# sourceMappingURL=chunk-ERB3AQAF.mjs.map
|