@dxos/network-manager 0.1.13 → 0.1.16
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/dist/lib/browser/index.mjs +2127 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/browser/testing/index.mjs +2197 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node/index.cjs +2173 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node/testing/index.cjs +2225 -0
- package/dist/lib/node/testing/index.cjs.map +7 -0
- package/dist/{src → types/src}/connection-log.d.ts +0 -0
- package/dist/types/src/connection-log.d.ts.map +1 -0
- package/dist/{src → types/src}/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/{src → types/src}/network-manager.d.ts +9 -11
- package/dist/types/src/network-manager.d.ts.map +1 -0
- package/dist/{src → types/src}/protocol-factory.d.ts +11 -5
- package/dist/types/src/protocol-factory.d.ts.map +1 -0
- package/dist/{src → types/src}/signal/index.d.ts +0 -0
- package/dist/types/src/signal/index.d.ts.map +1 -0
- package/dist/{src → types/src}/signal/integration.test.d.ts +0 -0
- package/dist/types/src/signal/integration.test.d.ts.map +1 -0
- package/dist/{src → types/src}/signal/message-router.d.ts +0 -0
- package/dist/types/src/signal/message-router.d.ts.map +1 -0
- package/dist/{src → types/src}/signal/message-router.test.d.ts +0 -0
- package/dist/types/src/signal/message-router.test.d.ts.map +1 -0
- package/dist/{src → types/src}/signal/signal-connection.d.ts +0 -0
- package/dist/types/src/signal/signal-connection.d.ts.map +1 -0
- package/dist/{src → types/src}/signal/signal-messenger.d.ts +0 -0
- package/dist/types/src/signal/signal-messenger.d.ts.map +1 -0
- package/dist/{src → types/src}/swarm/connection.d.ts +5 -17
- package/dist/types/src/swarm/connection.d.ts.map +1 -0
- package/dist/{src → types/src}/swarm/index.d.ts +0 -0
- package/dist/types/src/swarm/index.d.ts.map +1 -0
- package/dist/types/src/swarm/peer.d.ts +70 -0
- package/dist/types/src/swarm/peer.d.ts.map +1 -0
- package/dist/{src → types/src}/swarm/swarm-mapper.d.ts +1 -1
- package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -0
- package/dist/{src → types/src}/swarm/swarm.d.ts +4 -8
- package/dist/types/src/swarm/swarm.d.ts.map +1 -0
- package/dist/{src → types/src}/swarm/swarm.test.d.ts +0 -0
- package/dist/types/src/swarm/swarm.test.d.ts.map +1 -0
- package/dist/{src → types/src}/testing/index.d.ts +0 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/{src → types/src}/testing/test-builder.d.ts +1 -1
- package/dist/types/src/testing/test-builder.d.ts.map +1 -0
- package/dist/{src → types/src}/testing/test-protocol.d.ts +2 -2
- package/dist/types/src/testing/test-protocol.d.ts.map +1 -0
- package/dist/{src → types/src}/tests/basic-test-suite.d.ts +0 -0
- package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -0
- package/dist/{src → types/src}/tests/memory-transport.test.d.ts +0 -0
- package/dist/types/src/tests/memory-transport.test.d.ts.map +1 -0
- package/dist/{src → types/src}/tests/presence.test.d.ts +0 -0
- package/dist/types/src/tests/presence.test.d.ts.map +1 -0
- package/dist/{src → types/src}/tests/property-test-suite.d.ts +0 -0
- package/dist/types/src/tests/property-test-suite.d.ts.map +1 -0
- package/dist/{src → types/src}/tests/utils.d.ts +0 -0
- package/dist/types/src/tests/utils.d.ts.map +1 -0
- package/dist/{src → types/src}/tests/webrtc-transport.test.d.ts +0 -0
- package/dist/types/src/tests/webrtc-transport.test.d.ts.map +1 -0
- package/dist/{src → types/src}/topology/fully-connected-topology.d.ts +0 -0
- package/dist/types/src/topology/fully-connected-topology.d.ts.map +1 -0
- package/dist/{src → types/src}/topology/index.d.ts +0 -0
- package/dist/types/src/topology/index.d.ts.map +1 -0
- package/dist/{src → types/src}/topology/mmst-topology.d.ts +0 -0
- package/dist/types/src/topology/mmst-topology.d.ts.map +1 -0
- package/dist/{src → types/src}/topology/star-topology.d.ts +0 -0
- package/dist/types/src/topology/star-topology.d.ts.map +1 -0
- package/dist/{src → types/src}/topology/topology.d.ts +0 -0
- package/dist/types/src/topology/topology.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/index.d.ts +0 -0
- package/dist/types/src/transport/index.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/memory-transport.d.ts +0 -0
- package/dist/types/src/transport/memory-transport.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/memory-transport.test.d.ts +0 -0
- package/dist/types/src/transport/memory-transport.test.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/transport.d.ts +1 -1
- package/dist/types/src/transport/transport.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/webrtc-simple-peer.test.d.ts +0 -0
- package/dist/types/src/transport/webrtc-simple-peer.test.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/webrtc-transport-proxy.d.ts +1 -1
- package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/webrtc-transport-proxy.test.d.ts +0 -0
- package/dist/types/src/transport/webrtc-transport-proxy.test.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/webrtc-transport-service.d.ts +0 -0
- package/dist/types/src/transport/webrtc-transport-service.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/webrtc-transport.d.ts +1 -1
- package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/webrtc-transport.test.d.ts +0 -0
- package/dist/types/src/transport/webrtc-transport.test.d.ts.map +1 -0
- package/dist/{src → types/src}/transport/webrtc.d.ts +0 -0
- package/dist/types/src/transport/webrtc.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +6 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/wire-protocol.d.ts +37 -0
- package/dist/types/src/wire-protocol.d.ts.map +1 -0
- package/package.json +24 -22
- package/src/connection-log.ts +8 -8
- package/src/globals.d.ts +1 -1
- package/src/index.ts +1 -0
- package/src/network-manager.ts +14 -5
- package/src/protocol-factory.ts +7 -4
- package/src/signal/integration.test.ts +8 -7
- package/src/signal/message-router.test.ts +14 -15
- package/src/signal/message-router.ts +1 -1
- package/src/swarm/connection.ts +11 -55
- package/src/swarm/peer.ts +119 -30
- package/src/swarm/swarm.test.ts +9 -8
- package/src/swarm/swarm.ts +97 -116
- package/src/testing/test-builder.ts +4 -1
- package/src/testing/test-protocol.ts +1 -1
- package/src/tests/basic-test-suite.ts +7 -6
- package/src/tests/memory-transport.test.ts +1 -1
- package/src/tests/presence.test.ts +5 -4
- package/src/tests/property-test-suite.ts +4 -3
- package/src/tests/utils.ts +1 -1
- package/src/tests/webrtc-transport.test.ts +2 -2
- package/src/transport/memory-transport.test.ts +4 -4
- package/src/transport/memory-transport.ts +5 -5
- package/src/transport/webrtc-simple-peer.test.ts +13 -10
- package/src/transport/webrtc-transport-proxy.test.ts +9 -13
- package/src/transport/webrtc-transport-proxy.ts +1 -1
- package/src/transport/webrtc-transport-service.ts +2 -2
- package/src/transport/webrtc-transport.test.ts +4 -4
- package/src/transport/webrtc-transport.ts +3 -3
- package/src/wire-protocol.ts +75 -0
- package/testing.d.ts +1 -1
- package/testing.js +1 -1
- package/dist/src/connection-log.d.ts.map +0 -1
- package/dist/src/connection-log.js +0 -98
- package/dist/src/connection-log.js.map +0 -1
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -28
- package/dist/src/index.js.map +0 -1
- package/dist/src/network-manager.d.ts.map +0 -1
- package/dist/src/network-manager.js +0 -118
- package/dist/src/network-manager.js.map +0 -1
- package/dist/src/protocol-factory.d.ts.map +0 -1
- package/dist/src/protocol-factory.js +0 -66
- package/dist/src/protocol-factory.js.map +0 -1
- package/dist/src/signal/index.d.ts.map +0 -1
- package/dist/src/signal/index.js +0 -23
- package/dist/src/signal/index.js.map +0 -1
- package/dist/src/signal/integration.test.d.ts.map +0 -1
- package/dist/src/signal/integration.test.js +0 -105
- package/dist/src/signal/integration.test.js.map +0 -1
- package/dist/src/signal/message-router.d.ts.map +0 -1
- package/dist/src/signal/message-router.js +0 -135
- package/dist/src/signal/message-router.js.map +0 -1
- package/dist/src/signal/message-router.test.d.ts.map +0 -1
- package/dist/src/signal/message-router.test.js +0 -211
- package/dist/src/signal/message-router.test.js.map +0 -1
- package/dist/src/signal/signal-connection.d.ts.map +0 -1
- package/dist/src/signal/signal-connection.js +0 -6
- package/dist/src/signal/signal-connection.js.map +0 -1
- package/dist/src/signal/signal-messenger.d.ts.map +0 -1
- package/dist/src/signal/signal-messenger.js +0 -6
- package/dist/src/signal/signal-messenger.js.map +0 -1
- package/dist/src/swarm/connection.d.ts.map +0 -1
- package/dist/src/swarm/connection.js +0 -204
- package/dist/src/swarm/connection.js.map +0 -1
- package/dist/src/swarm/index.d.ts.map +0 -1
- package/dist/src/swarm/index.js +0 -23
- package/dist/src/swarm/index.js.map +0 -1
- package/dist/src/swarm/peer.d.ts +0 -45
- package/dist/src/swarm/peer.d.ts.map +0 -1
- package/dist/src/swarm/peer.js +0 -113
- package/dist/src/swarm/peer.js.map +0 -1
- package/dist/src/swarm/swarm-mapper.d.ts.map +0 -1
- package/dist/src/swarm/swarm-mapper.js +0 -92
- package/dist/src/swarm/swarm-mapper.js.map +0 -1
- package/dist/src/swarm/swarm.d.ts.map +0 -1
- package/dist/src/swarm/swarm.js +0 -305
- package/dist/src/swarm/swarm.js.map +0 -1
- package/dist/src/swarm/swarm.test.d.ts.map +0 -1
- package/dist/src/swarm/swarm.test.js +0 -135
- package/dist/src/swarm/swarm.test.js.map +0 -1
- package/dist/src/testing/index.d.ts.map +0 -1
- package/dist/src/testing/index.js +0 -22
- package/dist/src/testing/index.js.map +0 -1
- package/dist/src/testing/test-builder.d.ts.map +0 -1
- package/dist/src/testing/test-builder.js +0 -145
- package/dist/src/testing/test-builder.js.map +0 -1
- package/dist/src/testing/test-protocol.d.ts.map +0 -1
- package/dist/src/testing/test-protocol.js +0 -143
- package/dist/src/testing/test-protocol.js.map +0 -1
- package/dist/src/tests/basic-test-suite.d.ts.map +0 -1
- package/dist/src/tests/basic-test-suite.js +0 -142
- package/dist/src/tests/basic-test-suite.js.map +0 -1
- package/dist/src/tests/memory-transport.test.d.ts.map +0 -1
- package/dist/src/tests/memory-transport.test.js +0 -12
- package/dist/src/tests/memory-transport.test.js.map +0 -1
- package/dist/src/tests/presence.test.d.ts.map +0 -1
- package/dist/src/tests/presence.test.js +0 -61
- package/dist/src/tests/presence.test.js.map +0 -1
- package/dist/src/tests/property-test-suite.d.ts.map +0 -1
- package/dist/src/tests/property-test-suite.js +0 -203
- package/dist/src/tests/property-test-suite.js.map +0 -1
- package/dist/src/tests/utils.d.ts.map +0 -1
- package/dist/src/tests/utils.js +0 -76
- package/dist/src/tests/utils.js.map +0 -1
- package/dist/src/tests/webrtc-transport.test.d.ts.map +0 -1
- package/dist/src/tests/webrtc-transport.test.js +0 -16
- package/dist/src/tests/webrtc-transport.test.js.map +0 -1
- package/dist/src/topology/fully-connected-topology.d.ts.map +0 -1
- package/dist/src/topology/fully-connected-topology.js +0 -35
- package/dist/src/topology/fully-connected-topology.js.map +0 -1
- package/dist/src/topology/index.d.ts.map +0 -1
- package/dist/src/topology/index.js +0 -24
- package/dist/src/topology/index.js.map +0 -1
- package/dist/src/topology/mmst-topology.d.ts.map +0 -1
- package/dist/src/topology/mmst-topology.js +0 -73
- package/dist/src/topology/mmst-topology.js.map +0 -1
- package/dist/src/topology/star-topology.d.ts.map +0 -1
- package/dist/src/topology/star-topology.js +0 -60
- package/dist/src/topology/star-topology.js.map +0 -1
- package/dist/src/topology/topology.d.ts.map +0 -1
- package/dist/src/topology/topology.js +0 -6
- package/dist/src/topology/topology.js.map +0 -1
- package/dist/src/transport/index.d.ts.map +0 -1
- package/dist/src/transport/index.js +0 -25
- package/dist/src/transport/index.js.map +0 -1
- package/dist/src/transport/memory-transport.d.ts.map +0 -1
- package/dist/src/transport/memory-transport.js +0 -138
- package/dist/src/transport/memory-transport.js.map +0 -1
- package/dist/src/transport/memory-transport.test.d.ts.map +0 -1
- package/dist/src/transport/memory-transport.test.js +0 -96
- package/dist/src/transport/memory-transport.test.js.map +0 -1
- package/dist/src/transport/transport.d.ts.map +0 -1
- package/dist/src/transport/transport.js +0 -6
- package/dist/src/transport/transport.js.map +0 -1
- package/dist/src/transport/webrtc-simple-peer.test.d.ts.map +0 -1
- package/dist/src/transport/webrtc-simple-peer.test.js +0 -24
- package/dist/src/transport/webrtc-simple-peer.test.js.map +0 -1
- package/dist/src/transport/webrtc-transport-proxy.d.ts.map +0 -1
- package/dist/src/transport/webrtc-transport-proxy.js +0 -139
- package/dist/src/transport/webrtc-transport-proxy.js.map +0 -1
- package/dist/src/transport/webrtc-transport-proxy.test.d.ts.map +0 -1
- package/dist/src/transport/webrtc-transport-proxy.test.js +0 -219
- package/dist/src/transport/webrtc-transport-proxy.test.js.map +0 -1
- package/dist/src/transport/webrtc-transport-service.d.ts.map +0 -1
- package/dist/src/transport/webrtc-transport-service.js +0 -93
- package/dist/src/transport/webrtc-transport-service.js.map +0 -1
- package/dist/src/transport/webrtc-transport.d.ts.map +0 -1
- package/dist/src/transport/webrtc-transport.js +0 -77
- package/dist/src/transport/webrtc-transport.js.map +0 -1
- package/dist/src/transport/webrtc-transport.test.d.ts.map +0 -1
- package/dist/src/transport/webrtc-transport.test.js +0 -91
- package/dist/src/transport/webrtc-transport.test.js.map +0 -1
- package/dist/src/transport/webrtc.d.ts.map +0 -1
- package/dist/src/transport/webrtc.js +0 -17
- package/dist/src/transport/webrtc.js.map +0 -1
- package/dist/src/types.d.ts +0 -6
- package/dist/src/types.d.ts.map +0 -1
- package/dist/src/types.js +0 -6
- package/dist/src/types.js.map +0 -1
|
@@ -0,0 +1,2225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
|
|
26
|
+
// packages/core/mesh/network-manager/src/testing/index.ts
|
|
27
|
+
var testing_exports = {};
|
|
28
|
+
__export(testing_exports, {
|
|
29
|
+
TEST_SIGNAL_URL: () => TEST_SIGNAL_URL,
|
|
30
|
+
TestBuilder: () => TestBuilder,
|
|
31
|
+
TestPeer: () => TestPeer,
|
|
32
|
+
TestProtocolPlugin: () => TestProtocolPlugin,
|
|
33
|
+
TestSwarmConnection: () => TestSwarmConnection,
|
|
34
|
+
getPeerId: () => getPeerId,
|
|
35
|
+
testProtocolProvider: () => testProtocolProvider
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(testing_exports);
|
|
38
|
+
|
|
39
|
+
// packages/core/mesh/network-manager/src/testing/test-builder.ts
|
|
40
|
+
var import_keys12 = require("@dxos/keys");
|
|
41
|
+
var import_messaging2 = require("@dxos/messaging");
|
|
42
|
+
var import_protocols2 = require("@dxos/protocols");
|
|
43
|
+
var import_rpc = require("@dxos/rpc");
|
|
44
|
+
var import_util8 = require("@dxos/util");
|
|
45
|
+
|
|
46
|
+
// packages/core/mesh/network-manager/src/network-manager.ts
|
|
47
|
+
var import_node_assert5 = __toESM(require("node:assert"));
|
|
48
|
+
var import_async6 = require("@dxos/async");
|
|
49
|
+
var import_keys6 = require("@dxos/keys");
|
|
50
|
+
var import_log6 = require("@dxos/log");
|
|
51
|
+
var import_messaging = require("@dxos/messaging");
|
|
52
|
+
var import_util5 = require("@dxos/util");
|
|
53
|
+
|
|
54
|
+
// packages/core/mesh/network-manager/src/connection-log.ts
|
|
55
|
+
var import_async5 = require("@dxos/async");
|
|
56
|
+
var import_debug3 = require("@dxos/debug");
|
|
57
|
+
var import_keys5 = require("@dxos/keys");
|
|
58
|
+
var import_util4 = require("@dxos/util");
|
|
59
|
+
|
|
60
|
+
// packages/core/mesh/network-manager/src/swarm/connection.ts
|
|
61
|
+
var import_node_assert = __toESM(require("node:assert"));
|
|
62
|
+
var import_async = require("@dxos/async");
|
|
63
|
+
var import_debug = require("@dxos/debug");
|
|
64
|
+
var import_log = require("@dxos/log");
|
|
65
|
+
var __decorate = function(decorators, target, key, desc) {
|
|
66
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
67
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
68
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
69
|
+
else
|
|
70
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
71
|
+
if (d = decorators[i])
|
|
72
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
73
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
74
|
+
};
|
|
75
|
+
var ConnectionState;
|
|
76
|
+
(function(ConnectionState4) {
|
|
77
|
+
ConnectionState4["INITIAL"] = "INITIAL";
|
|
78
|
+
ConnectionState4["CONNECTING"] = "CONNECTING";
|
|
79
|
+
ConnectionState4["CONNECTED"] = "CONNECTED";
|
|
80
|
+
ConnectionState4["CLOSED"] = "CLOSED";
|
|
81
|
+
})(ConnectionState || (ConnectionState = {}));
|
|
82
|
+
var Connection = class {
|
|
83
|
+
constructor(topic, ownId, remoteId, sessionId, initiator, _signalMessaging, _protocol, _transportFactory) {
|
|
84
|
+
this.topic = topic;
|
|
85
|
+
this.ownId = ownId;
|
|
86
|
+
this.remoteId = remoteId;
|
|
87
|
+
this.sessionId = sessionId;
|
|
88
|
+
this.initiator = initiator;
|
|
89
|
+
this._signalMessaging = _signalMessaging;
|
|
90
|
+
this._protocol = _protocol;
|
|
91
|
+
this._transportFactory = _transportFactory;
|
|
92
|
+
this._state = ConnectionState.INITIAL;
|
|
93
|
+
this._bufferedSignals = [];
|
|
94
|
+
this.stateChanged = new import_async.Event();
|
|
95
|
+
this.errors = new import_debug.ErrorStream();
|
|
96
|
+
}
|
|
97
|
+
get state() {
|
|
98
|
+
return this._state;
|
|
99
|
+
}
|
|
100
|
+
get transport() {
|
|
101
|
+
return this._transport;
|
|
102
|
+
}
|
|
103
|
+
get protocol() {
|
|
104
|
+
return this._protocol;
|
|
105
|
+
}
|
|
106
|
+
openConnection() {
|
|
107
|
+
(0, import_node_assert.default)(this._state === ConnectionState.INITIAL, "Invalid state.");
|
|
108
|
+
this._changeState(ConnectionState.CONNECTING);
|
|
109
|
+
this._protocol.initialize().catch((err) => {
|
|
110
|
+
this.errors.raise(err);
|
|
111
|
+
});
|
|
112
|
+
(0, import_node_assert.default)(!this._transport);
|
|
113
|
+
this._transport = this._transportFactory.createTransport({
|
|
114
|
+
initiator: this.initiator,
|
|
115
|
+
stream: this._protocol.stream,
|
|
116
|
+
sendSignal: async (signal) => {
|
|
117
|
+
await this._signalMessaging.signal({
|
|
118
|
+
author: this.ownId,
|
|
119
|
+
recipient: this.remoteId,
|
|
120
|
+
sessionId: this.sessionId,
|
|
121
|
+
topic: this.topic,
|
|
122
|
+
data: {
|
|
123
|
+
signal
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
this._transport.connected.once(() => {
|
|
129
|
+
this._changeState(ConnectionState.CONNECTED);
|
|
130
|
+
});
|
|
131
|
+
this._transport.closed.once(() => {
|
|
132
|
+
this._transport = void 0;
|
|
133
|
+
this.close().catch((err) => this.errors.raise(err));
|
|
134
|
+
});
|
|
135
|
+
this._transport.errors.pipeTo(this.errors);
|
|
136
|
+
for (const signal of this._bufferedSignals) {
|
|
137
|
+
void this._transport.signal(signal);
|
|
138
|
+
}
|
|
139
|
+
this._bufferedSignals = [];
|
|
140
|
+
}
|
|
141
|
+
async close() {
|
|
142
|
+
var _a;
|
|
143
|
+
if (this._state === ConnectionState.CLOSED) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
(0, import_log.log)("closing...", {
|
|
147
|
+
peerId: this.ownId
|
|
148
|
+
}, {
|
|
149
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts",
|
|
150
|
+
line: 131,
|
|
151
|
+
scope: this,
|
|
152
|
+
callSite: (f, a) => f(...a)
|
|
153
|
+
});
|
|
154
|
+
try {
|
|
155
|
+
await this._protocol.destroy();
|
|
156
|
+
} catch (err) {
|
|
157
|
+
import_log.log.catch(err, {}, {
|
|
158
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts",
|
|
159
|
+
line: 137,
|
|
160
|
+
scope: this,
|
|
161
|
+
callSite: (f, a) => f(...a)
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
await ((_a = this._transport) == null ? void 0 : _a.destroy());
|
|
166
|
+
} catch (err1) {
|
|
167
|
+
import_log.log.catch(err1, {}, {
|
|
168
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts",
|
|
169
|
+
line: 144,
|
|
170
|
+
scope: this,
|
|
171
|
+
callSite: (f, a) => f(...a)
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
(0, import_log.log)("closed", {
|
|
175
|
+
peerId: this.ownId
|
|
176
|
+
}, {
|
|
177
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts",
|
|
178
|
+
line: 147,
|
|
179
|
+
scope: this,
|
|
180
|
+
callSite: (f, a) => f(...a)
|
|
181
|
+
});
|
|
182
|
+
this._changeState(ConnectionState.CLOSED);
|
|
183
|
+
}
|
|
184
|
+
async signal(msg) {
|
|
185
|
+
var _a, _b;
|
|
186
|
+
(0, import_node_assert.default)(msg.sessionId);
|
|
187
|
+
if (!msg.sessionId.equals(this.sessionId)) {
|
|
188
|
+
(0, import_log.log)("dropping signal for incorrect session id", {}, {
|
|
189
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts",
|
|
190
|
+
line: 154,
|
|
191
|
+
scope: this,
|
|
192
|
+
callSite: (f, a) => f(...a)
|
|
193
|
+
});
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
(0, import_node_assert.default)(msg.data.signal);
|
|
197
|
+
(0, import_node_assert.default)((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId));
|
|
198
|
+
(0, import_node_assert.default)((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId));
|
|
199
|
+
if (this._state === ConnectionState.INITIAL) {
|
|
200
|
+
(0, import_log.log)("buffered signal", {
|
|
201
|
+
peerId: this.ownId,
|
|
202
|
+
remoteId: this.remoteId,
|
|
203
|
+
msg: msg.data
|
|
204
|
+
}, {
|
|
205
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts",
|
|
206
|
+
line: 162,
|
|
207
|
+
scope: this,
|
|
208
|
+
callSite: (f, a) => f(...a)
|
|
209
|
+
});
|
|
210
|
+
this._bufferedSignals.push(msg.data.signal);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
(0, import_node_assert.default)(this._transport, "Connection not ready to accept signals.");
|
|
214
|
+
(0, import_log.log)("received signal", {
|
|
215
|
+
peerId: this.ownId,
|
|
216
|
+
remoteId: this.remoteId,
|
|
217
|
+
msg: msg.data
|
|
218
|
+
}, {
|
|
219
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts",
|
|
220
|
+
line: 168,
|
|
221
|
+
scope: this,
|
|
222
|
+
callSite: (f, a) => f(...a)
|
|
223
|
+
});
|
|
224
|
+
await this._transport.signal(msg.data.signal);
|
|
225
|
+
}
|
|
226
|
+
_changeState(state) {
|
|
227
|
+
(0, import_node_assert.default)(state !== this._state, "Already in this state.");
|
|
228
|
+
this._state = state;
|
|
229
|
+
this.stateChanged.emit(state);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
__decorate([
|
|
233
|
+
import_async.synchronized
|
|
234
|
+
], Connection.prototype, "close", null);
|
|
235
|
+
|
|
236
|
+
// packages/core/mesh/network-manager/src/swarm/swarm.ts
|
|
237
|
+
var import_node_assert4 = __toESM(require("node:assert"));
|
|
238
|
+
var import_async3 = require("@dxos/async");
|
|
239
|
+
var import_context = require("@dxos/context");
|
|
240
|
+
var import_debug2 = require("@dxos/debug");
|
|
241
|
+
var import_keys3 = require("@dxos/keys");
|
|
242
|
+
var import_log4 = require("@dxos/log");
|
|
243
|
+
var import_util2 = require("@dxos/util");
|
|
244
|
+
|
|
245
|
+
// packages/core/mesh/network-manager/src/signal/message-router.ts
|
|
246
|
+
var import_node_assert2 = __toESM(require("node:assert"));
|
|
247
|
+
var import_keys = require("@dxos/keys");
|
|
248
|
+
var import_log2 = require("@dxos/log");
|
|
249
|
+
var import_protocols = require("@dxos/protocols");
|
|
250
|
+
var import_util = require("@dxos/util");
|
|
251
|
+
var MessageRouter = class {
|
|
252
|
+
constructor({ sendMessage, onSignal, onOffer, topic }) {
|
|
253
|
+
this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
|
|
254
|
+
this._sendMessage = sendMessage;
|
|
255
|
+
this._onSignal = onSignal;
|
|
256
|
+
this._onOffer = onOffer;
|
|
257
|
+
this._topic = topic;
|
|
258
|
+
}
|
|
259
|
+
async receiveMessage({ author, recipient, payload }) {
|
|
260
|
+
var _a, _b, _c;
|
|
261
|
+
if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const message = import_protocols.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage").decode(payload.value);
|
|
265
|
+
if (!this._topic.equals(message.topic)) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
(0, import_log2.log)("received", {
|
|
269
|
+
from: author,
|
|
270
|
+
to: recipient,
|
|
271
|
+
msg: message
|
|
272
|
+
}, {
|
|
273
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/message-router.ts",
|
|
274
|
+
line: 67,
|
|
275
|
+
scope: this,
|
|
276
|
+
callSite: (f, a) => f(...a)
|
|
277
|
+
});
|
|
278
|
+
if ((_a = message.data) == null ? void 0 : _a.offer) {
|
|
279
|
+
await this._handleOffer({
|
|
280
|
+
author,
|
|
281
|
+
recipient,
|
|
282
|
+
message
|
|
283
|
+
});
|
|
284
|
+
} else if ((_b = message.data) == null ? void 0 : _b.answer) {
|
|
285
|
+
await this._resolveAnswers(message);
|
|
286
|
+
} else if ((_c = message.data) == null ? void 0 : _c.signal) {
|
|
287
|
+
await this._handleSignal({
|
|
288
|
+
author,
|
|
289
|
+
recipient,
|
|
290
|
+
message
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
async signal(message) {
|
|
295
|
+
var _a;
|
|
296
|
+
(0, import_node_assert2.default)((_a = message.data) == null ? void 0 : _a.signal);
|
|
297
|
+
await this._sendReliableMessage({
|
|
298
|
+
author: message.author,
|
|
299
|
+
recipient: message.recipient,
|
|
300
|
+
message
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
async offer(message) {
|
|
304
|
+
const networkMessage = {
|
|
305
|
+
...message,
|
|
306
|
+
messageId: import_keys.PublicKey.random()
|
|
307
|
+
};
|
|
308
|
+
return new Promise((resolve, reject) => {
|
|
309
|
+
this._offerRecords.set(networkMessage.messageId, {
|
|
310
|
+
resolve,
|
|
311
|
+
reject
|
|
312
|
+
});
|
|
313
|
+
return this._sendReliableMessage({
|
|
314
|
+
author: message.author,
|
|
315
|
+
recipient: message.recipient,
|
|
316
|
+
message: networkMessage
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
async _sendReliableMessage({ author, recipient, message }) {
|
|
321
|
+
var _a;
|
|
322
|
+
const networkMessage = {
|
|
323
|
+
...message,
|
|
324
|
+
messageId: (_a = message.messageId) != null ? _a : import_keys.PublicKey.random()
|
|
325
|
+
};
|
|
326
|
+
(0, import_log2.log)("sending", {
|
|
327
|
+
from: author,
|
|
328
|
+
to: recipient,
|
|
329
|
+
msg: networkMessage
|
|
330
|
+
}, {
|
|
331
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/message-router.ts",
|
|
332
|
+
line: 117,
|
|
333
|
+
scope: this,
|
|
334
|
+
callSite: (f, a) => f(...a)
|
|
335
|
+
});
|
|
336
|
+
await this._encodeAndSend({
|
|
337
|
+
author,
|
|
338
|
+
recipient,
|
|
339
|
+
message: networkMessage
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
async _encodeAndSend({ author, recipient, message }) {
|
|
343
|
+
await this._sendMessage({
|
|
344
|
+
author,
|
|
345
|
+
recipient,
|
|
346
|
+
payload: {
|
|
347
|
+
type_url: "dxos.mesh.swarm.SwarmMessage",
|
|
348
|
+
value: import_protocols.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage").encode(message)
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
async _resolveAnswers(message) {
|
|
353
|
+
var _a, _b, _c;
|
|
354
|
+
(0, import_node_assert2.default)((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId");
|
|
355
|
+
const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
|
|
356
|
+
if (offerRecord) {
|
|
357
|
+
this._offerRecords.delete(message.data.answer.offerMessageId);
|
|
358
|
+
(0, import_node_assert2.default)((_c = message.data) == null ? void 0 : _c.answer, "No answer");
|
|
359
|
+
(0, import_log2.log)("resolving", {
|
|
360
|
+
answer: message.data.answer
|
|
361
|
+
}, {
|
|
362
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/message-router.ts",
|
|
363
|
+
line: 146,
|
|
364
|
+
scope: this,
|
|
365
|
+
callSite: (f, a) => f(...a)
|
|
366
|
+
});
|
|
367
|
+
offerRecord.resolve(message.data.answer);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
async _handleOffer({ author, recipient, message }) {
|
|
371
|
+
(0, import_node_assert2.default)(message.data.offer, "No offer");
|
|
372
|
+
const offerMessage = {
|
|
373
|
+
author,
|
|
374
|
+
recipient,
|
|
375
|
+
...message,
|
|
376
|
+
data: {
|
|
377
|
+
offer: message.data.offer
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
const answer = await this._onOffer(offerMessage);
|
|
381
|
+
answer.offerMessageId = message.messageId;
|
|
382
|
+
await this._sendReliableMessage({
|
|
383
|
+
author: recipient,
|
|
384
|
+
recipient: author,
|
|
385
|
+
message: {
|
|
386
|
+
topic: message.topic,
|
|
387
|
+
sessionId: message.sessionId,
|
|
388
|
+
data: {
|
|
389
|
+
answer
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
async _handleSignal({ author, recipient, message }) {
|
|
395
|
+
(0, import_node_assert2.default)(message.messageId);
|
|
396
|
+
(0, import_node_assert2.default)(message.data.signal, "No Signal");
|
|
397
|
+
const signalMessage = {
|
|
398
|
+
author,
|
|
399
|
+
recipient,
|
|
400
|
+
...message,
|
|
401
|
+
data: {
|
|
402
|
+
signal: message.data.signal
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
await this._onSignal(signalMessage);
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
// packages/core/mesh/network-manager/src/swarm/peer.ts
|
|
410
|
+
var import_node_assert3 = __toESM(require("node:assert"));
|
|
411
|
+
var import_async2 = require("@dxos/async");
|
|
412
|
+
var import_keys2 = require("@dxos/keys");
|
|
413
|
+
var import_log3 = require("@dxos/log");
|
|
414
|
+
var __decorate2 = function(decorators, target, key, desc) {
|
|
415
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
416
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
417
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
418
|
+
else
|
|
419
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
420
|
+
if (d = decorators[i])
|
|
421
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
422
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
423
|
+
};
|
|
424
|
+
var Peer = class {
|
|
425
|
+
constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _callbacks) {
|
|
426
|
+
this.id = id;
|
|
427
|
+
this.topic = topic;
|
|
428
|
+
this.localPeerId = localPeerId;
|
|
429
|
+
this._signalMessaging = _signalMessaging;
|
|
430
|
+
this._protocolProvider = _protocolProvider;
|
|
431
|
+
this._transportFactory = _transportFactory;
|
|
432
|
+
this._callbacks = _callbacks;
|
|
433
|
+
this.advertizing = false;
|
|
434
|
+
this.initiating = false;
|
|
435
|
+
}
|
|
436
|
+
async onOffer(message) {
|
|
437
|
+
const remoteId = message.author;
|
|
438
|
+
if (this.connection || this.initiating) {
|
|
439
|
+
if (remoteId.toHex() < this.localPeerId.toHex()) {
|
|
440
|
+
import_log3.log.info("closing local connection and accepting remote peer's offer", {
|
|
441
|
+
id: this.id,
|
|
442
|
+
topic: this.topic,
|
|
443
|
+
peerId: this.localPeerId
|
|
444
|
+
}, {
|
|
445
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
446
|
+
line: 83,
|
|
447
|
+
scope: this,
|
|
448
|
+
callSite: (f, a) => f(...a)
|
|
449
|
+
});
|
|
450
|
+
if (this.connection) {
|
|
451
|
+
await this.closeConnection();
|
|
452
|
+
}
|
|
453
|
+
} else {
|
|
454
|
+
return {
|
|
455
|
+
accept: false
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (await this._callbacks.onOffer(remoteId)) {
|
|
460
|
+
if (!this.connection) {
|
|
461
|
+
(0, import_node_assert3.default)(message.sessionId);
|
|
462
|
+
const connection = this._createConnection(false, message.sessionId);
|
|
463
|
+
try {
|
|
464
|
+
connection.openConnection();
|
|
465
|
+
} catch (err) {
|
|
466
|
+
import_log3.log.warn("connection error", {
|
|
467
|
+
topic: this.topic,
|
|
468
|
+
peerId: this.localPeerId,
|
|
469
|
+
remoteId: this.id,
|
|
470
|
+
err
|
|
471
|
+
}, {
|
|
472
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
473
|
+
line: 107,
|
|
474
|
+
scope: this,
|
|
475
|
+
callSite: (f, a) => f(...a)
|
|
476
|
+
});
|
|
477
|
+
await this.closeConnection();
|
|
478
|
+
}
|
|
479
|
+
return {
|
|
480
|
+
accept: true
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return {
|
|
485
|
+
accept: false
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
async initiateConnection() {
|
|
489
|
+
(0, import_node_assert3.default)(!this.initiating, "Initiation in progress.");
|
|
490
|
+
(0, import_node_assert3.default)(!this.connection, "Already connected.");
|
|
491
|
+
const sessionId = import_keys2.PublicKey.random();
|
|
492
|
+
(0, import_log3.log)("initiating...", {
|
|
493
|
+
id: this.id,
|
|
494
|
+
topic: this.topic,
|
|
495
|
+
peerId: this.id,
|
|
496
|
+
sessionId
|
|
497
|
+
}, {
|
|
498
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
499
|
+
line: 125,
|
|
500
|
+
scope: this,
|
|
501
|
+
callSite: (f, a) => f(...a)
|
|
502
|
+
});
|
|
503
|
+
const connection = this._createConnection(true, sessionId);
|
|
504
|
+
this.initiating = true;
|
|
505
|
+
try {
|
|
506
|
+
const answer = await this._signalMessaging.offer({
|
|
507
|
+
author: this.localPeerId,
|
|
508
|
+
recipient: this.id,
|
|
509
|
+
sessionId,
|
|
510
|
+
topic: this.topic,
|
|
511
|
+
data: {
|
|
512
|
+
offer: {}
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
(0, import_log3.log)("received", {
|
|
516
|
+
answer,
|
|
517
|
+
topic: this.topic,
|
|
518
|
+
ownId: this.localPeerId,
|
|
519
|
+
remoteId: this.id
|
|
520
|
+
}, {
|
|
521
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
522
|
+
line: 137,
|
|
523
|
+
scope: this,
|
|
524
|
+
callSite: (f, a) => f(...a)
|
|
525
|
+
});
|
|
526
|
+
if (connection.state !== ConnectionState.INITIAL) {
|
|
527
|
+
(0, import_log3.log)("ignoring response", {}, {
|
|
528
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
529
|
+
line: 139,
|
|
530
|
+
scope: this,
|
|
531
|
+
callSite: (f, a) => f(...a)
|
|
532
|
+
});
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
if (!answer.accept) {
|
|
536
|
+
this._callbacks.onRejected();
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
connection.openConnection();
|
|
540
|
+
this._callbacks.onAccepted();
|
|
541
|
+
} catch (err) {
|
|
542
|
+
import_log3.log.warn("initiation error", {
|
|
543
|
+
topic: this.topic,
|
|
544
|
+
peerId: this.localPeerId,
|
|
545
|
+
remoteId: this.id,
|
|
546
|
+
err
|
|
547
|
+
}, {
|
|
548
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
549
|
+
line: 150,
|
|
550
|
+
scope: this,
|
|
551
|
+
callSite: (f, a) => f(...a)
|
|
552
|
+
});
|
|
553
|
+
await this.closeConnection();
|
|
554
|
+
throw err;
|
|
555
|
+
} finally {
|
|
556
|
+
this.initiating = false;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
_createConnection(initiator, sessionId) {
|
|
560
|
+
(0, import_log3.log)("creating connection", {
|
|
561
|
+
topic: this.topic,
|
|
562
|
+
peerId: this.localPeerId,
|
|
563
|
+
remoteId: this.id,
|
|
564
|
+
initiator,
|
|
565
|
+
sessionId
|
|
566
|
+
}, {
|
|
567
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
568
|
+
line: 164,
|
|
569
|
+
scope: this,
|
|
570
|
+
callSite: (f, a) => f(...a)
|
|
571
|
+
});
|
|
572
|
+
(0, import_node_assert3.default)(!this.connection, "Already connected.");
|
|
573
|
+
const connection = new Connection(
|
|
574
|
+
this.topic,
|
|
575
|
+
this.localPeerId,
|
|
576
|
+
this.id,
|
|
577
|
+
sessionId,
|
|
578
|
+
initiator,
|
|
579
|
+
this._signalMessaging,
|
|
580
|
+
this._protocolProvider({
|
|
581
|
+
initiator,
|
|
582
|
+
localPeerId: this.localPeerId,
|
|
583
|
+
remotePeerId: this.id,
|
|
584
|
+
topic: this.topic
|
|
585
|
+
}),
|
|
586
|
+
this._transportFactory
|
|
587
|
+
);
|
|
588
|
+
this._callbacks.onInitiated(connection);
|
|
589
|
+
connection.stateChanged.on((state) => {
|
|
590
|
+
switch (state) {
|
|
591
|
+
case ConnectionState.CONNECTED: {
|
|
592
|
+
this._callbacks.onConnected();
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
case ConnectionState.CLOSED: {
|
|
596
|
+
(0, import_log3.log)("connection closed", {
|
|
597
|
+
topic: this.topic,
|
|
598
|
+
peerId: this.localPeerId,
|
|
599
|
+
remoteId: this.id,
|
|
600
|
+
initiator
|
|
601
|
+
}, {
|
|
602
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
603
|
+
line: 193,
|
|
604
|
+
scope: this,
|
|
605
|
+
callSite: (f, a) => f(...a)
|
|
606
|
+
});
|
|
607
|
+
(0, import_node_assert3.default)(this.connection === connection, "Connection mismatch (race condition).");
|
|
608
|
+
this.connection = void 0;
|
|
609
|
+
this._callbacks.onDisconnected();
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
connection.errors.handle((err) => {
|
|
615
|
+
import_log3.log.warn("connection error", {
|
|
616
|
+
topic: this.topic,
|
|
617
|
+
peerId: this.localPeerId,
|
|
618
|
+
remoteId: this.id,
|
|
619
|
+
initiator,
|
|
620
|
+
err
|
|
621
|
+
}, {
|
|
622
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
623
|
+
line: 203,
|
|
624
|
+
scope: this,
|
|
625
|
+
callSite: (f, a) => f(...a)
|
|
626
|
+
});
|
|
627
|
+
void this.closeConnection().catch(() => {
|
|
628
|
+
import_log3.log.catch(err, {}, {
|
|
629
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
630
|
+
line: 207,
|
|
631
|
+
scope: this,
|
|
632
|
+
callSite: (f, a) => f(...a)
|
|
633
|
+
});
|
|
634
|
+
});
|
|
635
|
+
});
|
|
636
|
+
this.connection = connection;
|
|
637
|
+
return connection;
|
|
638
|
+
}
|
|
639
|
+
async closeConnection() {
|
|
640
|
+
if (!this.connection) {
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
const connection = this.connection;
|
|
644
|
+
(0, import_log3.log)("closing...", {
|
|
645
|
+
peerId: this.id,
|
|
646
|
+
sessionId: connection.sessionId
|
|
647
|
+
}, {
|
|
648
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
649
|
+
line: 221,
|
|
650
|
+
scope: this,
|
|
651
|
+
callSite: (f, a) => f(...a)
|
|
652
|
+
});
|
|
653
|
+
await connection.close();
|
|
654
|
+
(0, import_log3.log)("closed", {
|
|
655
|
+
peerId: this.id,
|
|
656
|
+
sessionId: connection.sessionId
|
|
657
|
+
}, {
|
|
658
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
659
|
+
line: 227,
|
|
660
|
+
scope: this,
|
|
661
|
+
callSite: (f, a) => f(...a)
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
async onSignal(message) {
|
|
665
|
+
if (!this.connection) {
|
|
666
|
+
(0, import_log3.log)("dropping signal message for non-existent connection", {
|
|
667
|
+
message
|
|
668
|
+
}, {
|
|
669
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
670
|
+
line: 232,
|
|
671
|
+
scope: this,
|
|
672
|
+
callSite: (f, a) => f(...a)
|
|
673
|
+
});
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
await this.connection.signal(message);
|
|
677
|
+
}
|
|
678
|
+
async destroy() {
|
|
679
|
+
var _a;
|
|
680
|
+
(0, import_log3.log)("Destroying peer", {
|
|
681
|
+
peerId: this.id,
|
|
682
|
+
topic: this.topic
|
|
683
|
+
}, {
|
|
684
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts",
|
|
685
|
+
line: 240,
|
|
686
|
+
scope: this,
|
|
687
|
+
callSite: (f, a) => f(...a)
|
|
688
|
+
});
|
|
689
|
+
await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
__decorate2([
|
|
693
|
+
import_async2.synchronized
|
|
694
|
+
], Peer.prototype, "destroy", null);
|
|
695
|
+
|
|
696
|
+
// packages/core/mesh/network-manager/src/swarm/swarm.ts
|
|
697
|
+
var __decorate3 = function(decorators, target, key, desc) {
|
|
698
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
699
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
700
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
701
|
+
else
|
|
702
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
703
|
+
if (d = decorators[i])
|
|
704
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
705
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
706
|
+
};
|
|
707
|
+
var INITIATION_DELAY = 100;
|
|
708
|
+
var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
|
|
709
|
+
var Swarm = class {
|
|
710
|
+
constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label) {
|
|
711
|
+
this._topic = _topic;
|
|
712
|
+
this._ownPeerId = _ownPeerId;
|
|
713
|
+
this._topology = _topology;
|
|
714
|
+
this._protocolProvider = _protocolProvider;
|
|
715
|
+
this._messenger = _messenger;
|
|
716
|
+
this._transportFactory = _transportFactory;
|
|
717
|
+
this._label = _label;
|
|
718
|
+
this._peers = new import_util2.ComplexMap(import_keys3.PublicKey.hash);
|
|
719
|
+
this._ctx = new import_context.Context();
|
|
720
|
+
this.connectionAdded = new import_async3.Event();
|
|
721
|
+
this.disconnected = new import_async3.Event();
|
|
722
|
+
this.connected = new import_async3.Event();
|
|
723
|
+
this.errors = new import_debug2.ErrorStream();
|
|
724
|
+
this.instanceId = import_keys3.PublicKey.random();
|
|
725
|
+
(0, import_log4.log)("creating swarm", {
|
|
726
|
+
peerId: _ownPeerId
|
|
727
|
+
}, {
|
|
728
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
729
|
+
line: 80,
|
|
730
|
+
scope: this,
|
|
731
|
+
callSite: (f, a) => f(...a)
|
|
732
|
+
});
|
|
733
|
+
_topology.init(this._getSwarmController());
|
|
734
|
+
this._swarmMessenger = new MessageRouter({
|
|
735
|
+
sendMessage: async (msg) => await this._messenger.sendMessage(msg),
|
|
736
|
+
onSignal: async (msg) => await this.onSignal(msg),
|
|
737
|
+
onOffer: async (msg) => await this.onOffer(msg),
|
|
738
|
+
topic: this._topic
|
|
739
|
+
});
|
|
740
|
+
this._messenger.listen({
|
|
741
|
+
peerId: this._ownPeerId,
|
|
742
|
+
payloadType: "dxos.mesh.swarm.SwarmMessage",
|
|
743
|
+
onMessage: async (message) => await this._swarmMessenger.receiveMessage(message)
|
|
744
|
+
}).catch((error) => import_log4.log.catch(error, {}, {
|
|
745
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
746
|
+
line: 96,
|
|
747
|
+
scope: this,
|
|
748
|
+
callSite: (f, a) => f(...a)
|
|
749
|
+
}));
|
|
750
|
+
}
|
|
751
|
+
get connections() {
|
|
752
|
+
return Array.from(this._peers.values()).map((peer) => peer.connection).filter(import_util2.isNotNullOrUndefined);
|
|
753
|
+
}
|
|
754
|
+
get ownPeerId() {
|
|
755
|
+
return this._ownPeerId;
|
|
756
|
+
}
|
|
757
|
+
get label() {
|
|
758
|
+
return this._label;
|
|
759
|
+
}
|
|
760
|
+
get topic() {
|
|
761
|
+
return this._topic;
|
|
762
|
+
}
|
|
763
|
+
async destroy() {
|
|
764
|
+
(0, import_log4.log)("destroying...", {}, {
|
|
765
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
766
|
+
line: 124,
|
|
767
|
+
scope: this,
|
|
768
|
+
callSite: (f, a) => f(...a)
|
|
769
|
+
});
|
|
770
|
+
await this._ctx.dispose();
|
|
771
|
+
await this._topology.destroy();
|
|
772
|
+
await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
|
|
773
|
+
(0, import_log4.log)("destroyed", {}, {
|
|
774
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
775
|
+
line: 128,
|
|
776
|
+
scope: this,
|
|
777
|
+
callSite: (f, a) => f(...a)
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
async setTopology(topology) {
|
|
781
|
+
if (topology === this._topology) {
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
(0, import_log4.log)("setting topology", {
|
|
785
|
+
previous: getClassName(this._topology),
|
|
786
|
+
topology: getClassName(topology)
|
|
787
|
+
}, {
|
|
788
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
789
|
+
line: 135,
|
|
790
|
+
scope: this,
|
|
791
|
+
callSite: (f, a) => f(...a)
|
|
792
|
+
});
|
|
793
|
+
await this._topology.destroy();
|
|
794
|
+
this._topology = topology;
|
|
795
|
+
this._topology.init(this._getSwarmController());
|
|
796
|
+
this._topology.update();
|
|
797
|
+
}
|
|
798
|
+
_getOrCreatePeer(peerId) {
|
|
799
|
+
let peer = this._peers.get(peerId);
|
|
800
|
+
if (!peer) {
|
|
801
|
+
peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, {
|
|
802
|
+
onInitiated: (connection) => {
|
|
803
|
+
this.connectionAdded.emit(connection);
|
|
804
|
+
},
|
|
805
|
+
onConnected: () => {
|
|
806
|
+
this.connected.emit(peerId);
|
|
807
|
+
},
|
|
808
|
+
onDisconnected: async () => {
|
|
809
|
+
if (!peer.advertizing) {
|
|
810
|
+
await this._destroyPeer(peer.id);
|
|
811
|
+
}
|
|
812
|
+
this.disconnected.emit(peerId);
|
|
813
|
+
this._topology.update();
|
|
814
|
+
},
|
|
815
|
+
onRejected: () => {
|
|
816
|
+
if (this._peers.has(peerId)) {
|
|
817
|
+
void this._destroyPeer(peerId);
|
|
818
|
+
}
|
|
819
|
+
},
|
|
820
|
+
onAccepted: () => {
|
|
821
|
+
this._topology.update();
|
|
822
|
+
},
|
|
823
|
+
onOffer: (remoteId) => {
|
|
824
|
+
return this._topology.onOffer(remoteId);
|
|
825
|
+
}
|
|
826
|
+
});
|
|
827
|
+
this._peers.set(peerId, peer);
|
|
828
|
+
}
|
|
829
|
+
return peer;
|
|
830
|
+
}
|
|
831
|
+
async _destroyPeer(peerId) {
|
|
832
|
+
(0, import_node_assert4.default)(this._peers.has(peerId));
|
|
833
|
+
await this._peers.get(peerId).destroy();
|
|
834
|
+
this._peers.delete(peerId);
|
|
835
|
+
}
|
|
836
|
+
onSwarmEvent(swarmEvent) {
|
|
837
|
+
(0, import_log4.log)("swarm event", {
|
|
838
|
+
swarmEvent
|
|
839
|
+
}, {
|
|
840
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
841
|
+
line: 199,
|
|
842
|
+
scope: this,
|
|
843
|
+
callSite: (f, a) => f(...a)
|
|
844
|
+
});
|
|
845
|
+
if (this._ctx.disposed) {
|
|
846
|
+
import_log4.log.warn("ignored for destroyed swarm", {}, {
|
|
847
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
848
|
+
line: 201,
|
|
849
|
+
scope: this,
|
|
850
|
+
callSite: (f, a) => f(...a)
|
|
851
|
+
});
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
if (swarmEvent.peerAvailable) {
|
|
855
|
+
const peerId = import_keys3.PublicKey.from(swarmEvent.peerAvailable.peer);
|
|
856
|
+
(0, import_log4.log)("new peer", {
|
|
857
|
+
peerId
|
|
858
|
+
}, {
|
|
859
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
860
|
+
line: 207,
|
|
861
|
+
scope: this,
|
|
862
|
+
callSite: (f, a) => f(...a)
|
|
863
|
+
});
|
|
864
|
+
if (!peerId.equals(this._ownPeerId)) {
|
|
865
|
+
const peer = this._getOrCreatePeer(peerId);
|
|
866
|
+
peer.advertizing = true;
|
|
867
|
+
}
|
|
868
|
+
} else if (swarmEvent.peerLeft) {
|
|
869
|
+
const peer1 = this._peers.get(import_keys3.PublicKey.from(swarmEvent.peerLeft.peer));
|
|
870
|
+
if (peer1) {
|
|
871
|
+
peer1.advertizing = false;
|
|
872
|
+
if (!peer1.connection) {
|
|
873
|
+
void this._destroyPeer(peer1.id);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
this._topology.update();
|
|
878
|
+
}
|
|
879
|
+
async onOffer(message) {
|
|
880
|
+
var _a, _b;
|
|
881
|
+
(0, import_log4.log)("offer", {
|
|
882
|
+
message
|
|
883
|
+
}, {
|
|
884
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
885
|
+
line: 226,
|
|
886
|
+
scope: this,
|
|
887
|
+
callSite: (f, a) => f(...a)
|
|
888
|
+
});
|
|
889
|
+
if (this._ctx.disposed) {
|
|
890
|
+
import_log4.log.info("ignored for destroyed swarm", {}, {
|
|
891
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
892
|
+
line: 228,
|
|
893
|
+
scope: this,
|
|
894
|
+
callSite: (f, a) => f(...a)
|
|
895
|
+
});
|
|
896
|
+
return {
|
|
897
|
+
accept: false
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
(0, import_node_assert4.default)(message.author);
|
|
901
|
+
if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
|
|
902
|
+
(0, import_log4.log)("rejecting offer with incorrect peerId", {
|
|
903
|
+
message
|
|
904
|
+
}, {
|
|
905
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
906
|
+
line: 235,
|
|
907
|
+
scope: this,
|
|
908
|
+
callSite: (f, a) => f(...a)
|
|
909
|
+
});
|
|
910
|
+
return {
|
|
911
|
+
accept: false
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
|
|
915
|
+
(0, import_log4.log)("rejecting offer with incorrect topic", {
|
|
916
|
+
message
|
|
917
|
+
}, {
|
|
918
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
919
|
+
line: 239,
|
|
920
|
+
scope: this,
|
|
921
|
+
callSite: (f, a) => f(...a)
|
|
922
|
+
});
|
|
923
|
+
return {
|
|
924
|
+
accept: false
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
const peer = this._getOrCreatePeer(message.author);
|
|
928
|
+
const answer = await peer.onOffer(message);
|
|
929
|
+
this._topology.update();
|
|
930
|
+
return answer;
|
|
931
|
+
}
|
|
932
|
+
async onSignal(message) {
|
|
933
|
+
var _a, _b;
|
|
934
|
+
(0, import_log4.log)("signal", {
|
|
935
|
+
message
|
|
936
|
+
}, {
|
|
937
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
938
|
+
line: 250,
|
|
939
|
+
scope: this,
|
|
940
|
+
callSite: (f, a) => f(...a)
|
|
941
|
+
});
|
|
942
|
+
if (this._ctx.disposed) {
|
|
943
|
+
import_log4.log.info("ignored for destroyed swarm", {}, {
|
|
944
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
945
|
+
line: 252,
|
|
946
|
+
scope: this,
|
|
947
|
+
callSite: (f, a) => f(...a)
|
|
948
|
+
});
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
(0, import_node_assert4.default)((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
|
|
952
|
+
(0, import_node_assert4.default)((_b = message.topic) == null ? void 0 : _b.equals(this._topic));
|
|
953
|
+
(0, import_node_assert4.default)(message.author);
|
|
954
|
+
const peer = this._getOrCreatePeer(message.author);
|
|
955
|
+
await peer.onSignal(message);
|
|
956
|
+
}
|
|
957
|
+
_getSwarmController() {
|
|
958
|
+
return {
|
|
959
|
+
getState: () => ({
|
|
960
|
+
ownPeerId: this._ownPeerId,
|
|
961
|
+
connected: Array.from(this._peers.values()).filter((peer) => peer.connection).map((peer) => peer.id),
|
|
962
|
+
candidates: Array.from(this._peers.values()).filter((peer) => !peer.connection && peer.advertizing).map((peer) => peer.id)
|
|
963
|
+
}),
|
|
964
|
+
connect: (peer) => {
|
|
965
|
+
if (this._ctx.disposed) {
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
(0, import_async3.scheduleTask)(this._ctx, async () => {
|
|
969
|
+
try {
|
|
970
|
+
await this._initiateConnection(peer);
|
|
971
|
+
} catch (err) {
|
|
972
|
+
import_log4.log.warn("initiation error", err, {
|
|
973
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
974
|
+
line: 287,
|
|
975
|
+
scope: this,
|
|
976
|
+
callSite: (f, a) => f(...a)
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
});
|
|
980
|
+
},
|
|
981
|
+
disconnect: async (peer) => {
|
|
982
|
+
if (this._ctx.disposed) {
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
(0, import_async3.scheduleTask)(this._ctx, async () => {
|
|
986
|
+
await this._closeConnection(peer);
|
|
987
|
+
this._topology.update();
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
async _initiateConnection(remoteId) {
|
|
993
|
+
if (remoteId.toHex() < this._ownPeerId.toHex()) {
|
|
994
|
+
(0, import_log4.log)("initiation delay", {
|
|
995
|
+
remoteId
|
|
996
|
+
}, {
|
|
997
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
998
|
+
line: 312,
|
|
999
|
+
scope: this,
|
|
1000
|
+
callSite: (f, a) => f(...a)
|
|
1001
|
+
});
|
|
1002
|
+
await (0, import_async3.sleep)(INITIATION_DELAY);
|
|
1003
|
+
}
|
|
1004
|
+
if (this._ctx.disposed) {
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
const peer = this._getOrCreatePeer(remoteId);
|
|
1008
|
+
if (peer.connection) {
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
(0, import_log4.log)("initiating connection...", {
|
|
1012
|
+
remoteId
|
|
1013
|
+
}, {
|
|
1014
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
1015
|
+
line: 326,
|
|
1016
|
+
scope: this,
|
|
1017
|
+
callSite: (f, a) => f(...a)
|
|
1018
|
+
});
|
|
1019
|
+
await peer.initiateConnection();
|
|
1020
|
+
this._topology.update();
|
|
1021
|
+
(0, import_log4.log)("initiated", {
|
|
1022
|
+
remoteId
|
|
1023
|
+
}, {
|
|
1024
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts",
|
|
1025
|
+
line: 329,
|
|
1026
|
+
scope: this,
|
|
1027
|
+
callSite: (f, a) => f(...a)
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
async _closeConnection(peerId) {
|
|
1031
|
+
const peer = this._peers.get(peerId);
|
|
1032
|
+
if (!peer) {
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
await peer.closeConnection();
|
|
1036
|
+
}
|
|
1037
|
+
};
|
|
1038
|
+
__decorate3([
|
|
1039
|
+
import_log4.logInfo
|
|
1040
|
+
], Swarm.prototype, "instanceId", void 0);
|
|
1041
|
+
__decorate3([
|
|
1042
|
+
import_log4.logInfo
|
|
1043
|
+
], Swarm.prototype, "ownPeerId", null);
|
|
1044
|
+
__decorate3([
|
|
1045
|
+
import_log4.logInfo
|
|
1046
|
+
], Swarm.prototype, "topic", null);
|
|
1047
|
+
|
|
1048
|
+
// packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts
|
|
1049
|
+
var import_async4 = require("@dxos/async");
|
|
1050
|
+
var import_keys4 = require("@dxos/keys");
|
|
1051
|
+
var import_log5 = require("@dxos/log");
|
|
1052
|
+
var import_util3 = require("@dxos/util");
|
|
1053
|
+
var SwarmMapper = class {
|
|
1054
|
+
get peers() {
|
|
1055
|
+
return Array.from(this._peers.values());
|
|
1056
|
+
}
|
|
1057
|
+
constructor(_swarm, _presence) {
|
|
1058
|
+
this._swarm = _swarm;
|
|
1059
|
+
this._presence = _presence;
|
|
1060
|
+
this._subscriptions = new import_async4.EventSubscriptions();
|
|
1061
|
+
this._connectionSubscriptions = new import_util3.ComplexMap(import_keys4.PublicKey.hash);
|
|
1062
|
+
this._peers = new import_util3.ComplexMap(import_keys4.PublicKey.hash);
|
|
1063
|
+
this.mapUpdated = new import_async4.Event();
|
|
1064
|
+
this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
|
|
1065
|
+
this._update();
|
|
1066
|
+
this._connectionSubscriptions.set(connection.remoteId, connection.stateChanged.on(() => {
|
|
1067
|
+
this._update();
|
|
1068
|
+
}));
|
|
1069
|
+
}));
|
|
1070
|
+
this._subscriptions.add(_swarm.disconnected.on((peerId) => {
|
|
1071
|
+
var _a;
|
|
1072
|
+
(_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
|
|
1073
|
+
this._connectionSubscriptions.delete(peerId);
|
|
1074
|
+
this._update();
|
|
1075
|
+
}));
|
|
1076
|
+
if (_presence) {
|
|
1077
|
+
this._subscriptions.add(_presence.graphUpdated.on(() => {
|
|
1078
|
+
this._update();
|
|
1079
|
+
}));
|
|
1080
|
+
}
|
|
1081
|
+
this._update();
|
|
1082
|
+
}
|
|
1083
|
+
_update() {
|
|
1084
|
+
(0, import_log5.log)("updating swarm", {}, {
|
|
1085
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts",
|
|
1086
|
+
line: 77,
|
|
1087
|
+
scope: this,
|
|
1088
|
+
callSite: (f, a) => f(...a)
|
|
1089
|
+
});
|
|
1090
|
+
this._peers.clear();
|
|
1091
|
+
this._peers.set(this._swarm.ownPeerId, {
|
|
1092
|
+
id: this._swarm.ownPeerId,
|
|
1093
|
+
state: "ME",
|
|
1094
|
+
connections: []
|
|
1095
|
+
});
|
|
1096
|
+
for (const connection of this._swarm.connections) {
|
|
1097
|
+
this._peers.set(connection.remoteId, {
|
|
1098
|
+
id: connection.remoteId,
|
|
1099
|
+
state: connection.state,
|
|
1100
|
+
connections: [
|
|
1101
|
+
this._swarm.ownPeerId
|
|
1102
|
+
]
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
if (this._presence) {
|
|
1106
|
+
this._presence.graph.forEachNode((node) => {
|
|
1107
|
+
const id = import_keys4.PublicKey.fromHex(node.id);
|
|
1108
|
+
if (this._peers.has(id)) {
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
this._peers.set(id, {
|
|
1112
|
+
id,
|
|
1113
|
+
state: "INDIRECTLY_CONNECTED",
|
|
1114
|
+
connections: []
|
|
1115
|
+
});
|
|
1116
|
+
});
|
|
1117
|
+
this._presence.graph.forEachLink((link) => {
|
|
1118
|
+
const from = import_keys4.PublicKey.from(link.fromId);
|
|
1119
|
+
const to = import_keys4.PublicKey.from(link.toId);
|
|
1120
|
+
if (!from.equals(this._swarm.ownPeerId) && !to.equals(this._swarm.ownPeerId)) {
|
|
1121
|
+
this._peers.get(from).connections.push(to);
|
|
1122
|
+
}
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
(0, import_log5.log)("graph changed", {
|
|
1126
|
+
directConnections: this._swarm.connections.length,
|
|
1127
|
+
totalPeersInSwarm: this._peers.size
|
|
1128
|
+
}, {
|
|
1129
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts",
|
|
1130
|
+
line: 118,
|
|
1131
|
+
scope: this,
|
|
1132
|
+
callSite: (f, a) => f(...a)
|
|
1133
|
+
});
|
|
1134
|
+
this.mapUpdated.emit(Array.from(this._peers.values()));
|
|
1135
|
+
}
|
|
1136
|
+
destroy() {
|
|
1137
|
+
Array.from(this._connectionSubscriptions.values()).forEach((cb) => cb());
|
|
1138
|
+
this._subscriptions.clear();
|
|
1139
|
+
}
|
|
1140
|
+
};
|
|
1141
|
+
|
|
1142
|
+
// packages/core/mesh/network-manager/src/connection-log.ts
|
|
1143
|
+
var EventType;
|
|
1144
|
+
(function(EventType2) {
|
|
1145
|
+
EventType2["CONNECTION_STATE_CHANGED"] = "CONNECTION_STATE_CHANGED";
|
|
1146
|
+
EventType2["PROTOCOL_ERROR"] = "PROTOCOL_ERROR";
|
|
1147
|
+
EventType2["PROTOCOL_EXTENSIONS_INITIALIZED"] = "PROTOCOL_EXTENSIONS_INITIALIZED";
|
|
1148
|
+
EventType2["PROTOCOL_EXTENSIONS_HANDSHAKE"] = "PROTOCOL_EXTENSIONS_HANDSHAKE";
|
|
1149
|
+
EventType2["PROTOCOL_HANDSHAKE"] = "PROTOCOL_HANDSHAKE";
|
|
1150
|
+
})(EventType || (EventType = {}));
|
|
1151
|
+
var ConnectionLog = class {
|
|
1152
|
+
constructor() {
|
|
1153
|
+
this._swarms = new import_util4.ComplexMap(import_keys5.PublicKey.hash);
|
|
1154
|
+
this.update = new import_async5.Event();
|
|
1155
|
+
}
|
|
1156
|
+
getSwarmInfo(swarmId) {
|
|
1157
|
+
var _a;
|
|
1158
|
+
return (_a = this._swarms.get(swarmId)) != null ? _a : (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
|
|
1159
|
+
}
|
|
1160
|
+
get swarms() {
|
|
1161
|
+
return Array.from(this._swarms.values());
|
|
1162
|
+
}
|
|
1163
|
+
swarmJoined(swarm) {
|
|
1164
|
+
const info = {
|
|
1165
|
+
id: swarm.instanceId,
|
|
1166
|
+
topic: swarm.topic,
|
|
1167
|
+
isActive: true,
|
|
1168
|
+
label: swarm.label,
|
|
1169
|
+
connections: []
|
|
1170
|
+
};
|
|
1171
|
+
this._swarms.set(swarm.instanceId, info);
|
|
1172
|
+
this.update.emit();
|
|
1173
|
+
swarm.connectionAdded.on((connection) => {
|
|
1174
|
+
var _a, _b, _c, _d, _e;
|
|
1175
|
+
const connectionInfo = {
|
|
1176
|
+
state: ConnectionState.INITIAL,
|
|
1177
|
+
remotePeerId: connection.remoteId,
|
|
1178
|
+
sessionId: connection.sessionId,
|
|
1179
|
+
transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
|
|
1180
|
+
protocolExtensions: (_a = connection.protocol.protocol) == null ? void 0 : _a.extensionNames,
|
|
1181
|
+
events: []
|
|
1182
|
+
};
|
|
1183
|
+
info.connections.push(connectionInfo);
|
|
1184
|
+
this.update.emit();
|
|
1185
|
+
connection.stateChanged.on((state) => {
|
|
1186
|
+
connectionInfo.state = state;
|
|
1187
|
+
connectionInfo.events.push({
|
|
1188
|
+
type: EventType.CONNECTION_STATE_CHANGED,
|
|
1189
|
+
newState: state
|
|
1190
|
+
});
|
|
1191
|
+
this.update.emit();
|
|
1192
|
+
});
|
|
1193
|
+
(_b = connection.protocol.protocol) == null ? void 0 : _b.error.on((error) => {
|
|
1194
|
+
var _a2;
|
|
1195
|
+
connectionInfo.events.push({
|
|
1196
|
+
type: EventType.PROTOCOL_ERROR,
|
|
1197
|
+
error: (_a2 = error.stack) != null ? _a2 : error.message
|
|
1198
|
+
});
|
|
1199
|
+
this.update.emit();
|
|
1200
|
+
});
|
|
1201
|
+
(_c = connection.protocol.protocol) == null ? void 0 : _c.extensionsInitialized.on(() => {
|
|
1202
|
+
connectionInfo.events.push({
|
|
1203
|
+
type: EventType.PROTOCOL_EXTENSIONS_INITIALIZED
|
|
1204
|
+
});
|
|
1205
|
+
this.update.emit();
|
|
1206
|
+
});
|
|
1207
|
+
(_d = connection.protocol.protocol) == null ? void 0 : _d.extensionsHandshake.on(() => {
|
|
1208
|
+
connectionInfo.events.push({
|
|
1209
|
+
type: EventType.PROTOCOL_EXTENSIONS_HANDSHAKE
|
|
1210
|
+
});
|
|
1211
|
+
this.update.emit();
|
|
1212
|
+
});
|
|
1213
|
+
(_e = connection.protocol.protocol) == null ? void 0 : _e.handshake.on(() => {
|
|
1214
|
+
connectionInfo.events.push({
|
|
1215
|
+
type: EventType.PROTOCOL_HANDSHAKE
|
|
1216
|
+
});
|
|
1217
|
+
this.update.emit();
|
|
1218
|
+
});
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
swarmLeft(swarm) {
|
|
1222
|
+
this.getSwarmInfo(swarm.instanceId).isActive = false;
|
|
1223
|
+
this.update.emit();
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
|
|
1227
|
+
// packages/core/mesh/network-manager/src/network-manager.ts
|
|
1228
|
+
var NetworkManager = class {
|
|
1229
|
+
constructor({ transportFactory, signalManager, log: log15 }) {
|
|
1230
|
+
this._swarms = new import_util5.ComplexMap(import_keys6.PublicKey.hash);
|
|
1231
|
+
this._mappers = new import_util5.ComplexMap(import_keys6.PublicKey.hash);
|
|
1232
|
+
this.topicsUpdated = new import_async6.Event();
|
|
1233
|
+
this._transportFactory = transportFactory;
|
|
1234
|
+
this._signalManager = signalManager;
|
|
1235
|
+
this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
|
|
1236
|
+
var _a;
|
|
1237
|
+
return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
|
|
1238
|
+
});
|
|
1239
|
+
this._messenger = new import_messaging.Messenger({
|
|
1240
|
+
signalManager: this._signalManager
|
|
1241
|
+
});
|
|
1242
|
+
this._signalConnection = {
|
|
1243
|
+
join: (opts) => this._signalManager.join(opts),
|
|
1244
|
+
leave: (opts) => this._signalManager.leave(opts)
|
|
1245
|
+
};
|
|
1246
|
+
if (log15) {
|
|
1247
|
+
this._connectionLog = new ConnectionLog();
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
get connectionLog() {
|
|
1251
|
+
return this._connectionLog;
|
|
1252
|
+
}
|
|
1253
|
+
get signalManager() {
|
|
1254
|
+
return this._signalManager;
|
|
1255
|
+
}
|
|
1256
|
+
get topics() {
|
|
1257
|
+
return Array.from(this._swarms.keys());
|
|
1258
|
+
}
|
|
1259
|
+
getSwarmMap(topic) {
|
|
1260
|
+
return this._mappers.get(topic);
|
|
1261
|
+
}
|
|
1262
|
+
getSwarm(topic) {
|
|
1263
|
+
return this._swarms.get(topic);
|
|
1264
|
+
}
|
|
1265
|
+
async close() {
|
|
1266
|
+
for (const topic of this._swarms.keys()) {
|
|
1267
|
+
await this.leaveSwarm(topic).catch((err) => {
|
|
1268
|
+
(0, import_log6.log)(err, {}, {
|
|
1269
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts",
|
|
1270
|
+
line: 128,
|
|
1271
|
+
scope: this,
|
|
1272
|
+
callSite: (f, a) => f(...a)
|
|
1273
|
+
});
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
await this._signalManager.destroy();
|
|
1277
|
+
}
|
|
1278
|
+
async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, presence, label }) {
|
|
1279
|
+
var _a;
|
|
1280
|
+
(0, import_node_assert5.default)(import_keys6.PublicKey.isPublicKey(topic));
|
|
1281
|
+
(0, import_node_assert5.default)(import_keys6.PublicKey.isPublicKey(peerId));
|
|
1282
|
+
(0, import_node_assert5.default)(topology);
|
|
1283
|
+
(0, import_node_assert5.default)(typeof protocol === "function");
|
|
1284
|
+
if (this._swarms.has(topic)) {
|
|
1285
|
+
throw new Error(`Already connected to swarm: ${import_keys6.PublicKey.from(topic)}`);
|
|
1286
|
+
}
|
|
1287
|
+
(0, import_log6.log)("joining", {
|
|
1288
|
+
topic: import_keys6.PublicKey.from(topic),
|
|
1289
|
+
peerId,
|
|
1290
|
+
topology: topology.toString()
|
|
1291
|
+
}, {
|
|
1292
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts",
|
|
1293
|
+
line: 155,
|
|
1294
|
+
scope: this,
|
|
1295
|
+
callSite: (f, a) => f(...a)
|
|
1296
|
+
});
|
|
1297
|
+
const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label);
|
|
1298
|
+
swarm.errors.handle((error) => {
|
|
1299
|
+
(0, import_log6.log)("swarm error", {
|
|
1300
|
+
error
|
|
1301
|
+
}, {
|
|
1302
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts",
|
|
1303
|
+
line: 158,
|
|
1304
|
+
scope: this,
|
|
1305
|
+
callSite: (f, a) => f(...a)
|
|
1306
|
+
});
|
|
1307
|
+
});
|
|
1308
|
+
this._swarms.set(topic, swarm);
|
|
1309
|
+
this._signalConnection.join({
|
|
1310
|
+
topic,
|
|
1311
|
+
peerId
|
|
1312
|
+
}).catch((error) => import_log6.log.catch(error, {}, {
|
|
1313
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts",
|
|
1314
|
+
line: 162,
|
|
1315
|
+
scope: this,
|
|
1316
|
+
callSite: (f, a) => f(...a)
|
|
1317
|
+
}));
|
|
1318
|
+
this._mappers.set(topic, new SwarmMapper(swarm, presence));
|
|
1319
|
+
this.topicsUpdated.emit();
|
|
1320
|
+
(_a = this._connectionLog) == null ? void 0 : _a.swarmJoined(swarm);
|
|
1321
|
+
(0, import_log6.log)("joined", {
|
|
1322
|
+
topic: import_keys6.PublicKey.from(topic),
|
|
1323
|
+
count: this._swarms.size
|
|
1324
|
+
}, {
|
|
1325
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts",
|
|
1326
|
+
line: 167,
|
|
1327
|
+
scope: this,
|
|
1328
|
+
callSite: (f, a) => f(...a)
|
|
1329
|
+
});
|
|
1330
|
+
return {
|
|
1331
|
+
close: () => this.leaveSwarm(topic)
|
|
1332
|
+
};
|
|
1333
|
+
}
|
|
1334
|
+
async leaveSwarm(topic) {
|
|
1335
|
+
var _a;
|
|
1336
|
+
if (!this._swarms.has(topic)) {
|
|
1337
|
+
import_log6.log.warn("swarm not open", {
|
|
1338
|
+
topic: import_keys6.PublicKey.from(topic)
|
|
1339
|
+
}, {
|
|
1340
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts",
|
|
1341
|
+
line: 179,
|
|
1342
|
+
scope: this,
|
|
1343
|
+
callSite: (f, a) => f(...a)
|
|
1344
|
+
});
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
(0, import_log6.log)("leaving", {
|
|
1348
|
+
topic: import_keys6.PublicKey.from(topic)
|
|
1349
|
+
}, {
|
|
1350
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts",
|
|
1351
|
+
line: 183,
|
|
1352
|
+
scope: this,
|
|
1353
|
+
callSite: (f, a) => f(...a)
|
|
1354
|
+
});
|
|
1355
|
+
const swarm = this._swarms.get(topic);
|
|
1356
|
+
await this._signalConnection.leave({
|
|
1357
|
+
topic,
|
|
1358
|
+
peerId: swarm.ownPeerId
|
|
1359
|
+
});
|
|
1360
|
+
const map = this._mappers.get(topic);
|
|
1361
|
+
map.destroy();
|
|
1362
|
+
this._mappers.delete(topic);
|
|
1363
|
+
(_a = this._connectionLog) == null ? void 0 : _a.swarmLeft(swarm);
|
|
1364
|
+
await swarm.destroy();
|
|
1365
|
+
this._swarms.delete(topic);
|
|
1366
|
+
await this.topicsUpdated.emit();
|
|
1367
|
+
(0, import_log6.log)("left", {
|
|
1368
|
+
topic: import_keys6.PublicKey.from(topic),
|
|
1369
|
+
count: this._swarms.size
|
|
1370
|
+
}, {
|
|
1371
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts",
|
|
1372
|
+
line: 197,
|
|
1373
|
+
scope: this,
|
|
1374
|
+
callSite: (f, a) => f(...a)
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
};
|
|
1378
|
+
|
|
1379
|
+
// packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
|
|
1380
|
+
var import_node_assert6 = __toESM(require("node:assert"));
|
|
1381
|
+
var FullyConnectedTopology = class {
|
|
1382
|
+
toString() {
|
|
1383
|
+
return "FullyConnectedTopology";
|
|
1384
|
+
}
|
|
1385
|
+
init(controller) {
|
|
1386
|
+
(0, import_node_assert6.default)(!this._controller, "Already initialized");
|
|
1387
|
+
this._controller = controller;
|
|
1388
|
+
}
|
|
1389
|
+
update() {
|
|
1390
|
+
(0, import_node_assert6.default)(this._controller, "Not initialized");
|
|
1391
|
+
const { candidates: discovered } = this._controller.getState();
|
|
1392
|
+
for (const peer of discovered) {
|
|
1393
|
+
this._controller.connect(peer);
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
async onOffer(peer) {
|
|
1397
|
+
return true;
|
|
1398
|
+
}
|
|
1399
|
+
async destroy() {
|
|
1400
|
+
}
|
|
1401
|
+
};
|
|
1402
|
+
|
|
1403
|
+
// packages/core/mesh/network-manager/src/topology/mmst-topology.ts
|
|
1404
|
+
var import_xor_distance = __toESM(require("xor-distance"));
|
|
1405
|
+
var import_log7 = require("@dxos/log");
|
|
1406
|
+
|
|
1407
|
+
// packages/core/mesh/network-manager/src/topology/star-topology.ts
|
|
1408
|
+
var import_log8 = require("@dxos/log");
|
|
1409
|
+
|
|
1410
|
+
// packages/core/mesh/network-manager/src/transport/memory-transport.ts
|
|
1411
|
+
var import_node_assert7 = __toESM(require("node:assert"));
|
|
1412
|
+
var import_node_stream = require("node:stream");
|
|
1413
|
+
var import_async7 = require("@dxos/async");
|
|
1414
|
+
var import_debug4 = require("@dxos/debug");
|
|
1415
|
+
var import_keys7 = require("@dxos/keys");
|
|
1416
|
+
var import_log9 = require("@dxos/log");
|
|
1417
|
+
var import_util6 = require("@dxos/util");
|
|
1418
|
+
var MEMORY_TRANSPORT_DELAY = 1;
|
|
1419
|
+
var createStreamDelay = (delay) => {
|
|
1420
|
+
return new import_node_stream.Transform({
|
|
1421
|
+
objectMode: true,
|
|
1422
|
+
transform: (chunk, _, cb) => {
|
|
1423
|
+
setTimeout(() => cb(null, chunk), delay);
|
|
1424
|
+
}
|
|
1425
|
+
});
|
|
1426
|
+
};
|
|
1427
|
+
var MemoryTransportFactory = {
|
|
1428
|
+
createTransport: (params) => new MemoryTransport(params)
|
|
1429
|
+
};
|
|
1430
|
+
var _MemoryTransport = class {
|
|
1431
|
+
constructor(options) {
|
|
1432
|
+
var _a;
|
|
1433
|
+
this.options = options;
|
|
1434
|
+
this.closed = new import_async7.Event();
|
|
1435
|
+
this.connected = new import_async7.Event();
|
|
1436
|
+
this.errors = new import_debug4.ErrorStream();
|
|
1437
|
+
this._id = import_keys7.PublicKey.random();
|
|
1438
|
+
this._remote = new import_async7.Trigger();
|
|
1439
|
+
this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
1440
|
+
this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
1441
|
+
this._destroyed = false;
|
|
1442
|
+
(0, import_log9.log)("creating", {
|
|
1443
|
+
id: this._id
|
|
1444
|
+
}, {
|
|
1445
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts",
|
|
1446
|
+
line: 59,
|
|
1447
|
+
scope: this,
|
|
1448
|
+
callSite: (f, a) => f(...a)
|
|
1449
|
+
});
|
|
1450
|
+
(0, import_node_assert7.default)(!_MemoryTransport._connections.has(this._id), "Duplicate memory connection");
|
|
1451
|
+
_MemoryTransport._connections.set(this._id, this);
|
|
1452
|
+
if (this.options.initiator) {
|
|
1453
|
+
setTimeout(async () => {
|
|
1454
|
+
(0, import_log9.log)("sending signal", {
|
|
1455
|
+
transportId: this._id
|
|
1456
|
+
}, {
|
|
1457
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts",
|
|
1458
|
+
line: 68,
|
|
1459
|
+
scope: this,
|
|
1460
|
+
callSite: (f, a) => f(...a)
|
|
1461
|
+
});
|
|
1462
|
+
void this.options.sendSignal({
|
|
1463
|
+
payload: {
|
|
1464
|
+
transportId: this._id.toHex()
|
|
1465
|
+
}
|
|
1466
|
+
});
|
|
1467
|
+
});
|
|
1468
|
+
} else {
|
|
1469
|
+
this._remote.wait({
|
|
1470
|
+
timeout: (_a = this.options.timeout) != null ? _a : 1e3
|
|
1471
|
+
}).then((remoteId) => {
|
|
1472
|
+
if (this._destroyed) {
|
|
1473
|
+
return;
|
|
1474
|
+
}
|
|
1475
|
+
this._remoteId = remoteId;
|
|
1476
|
+
this._remoteConnection = _MemoryTransport._connections.get(this._remoteId);
|
|
1477
|
+
if (!this._remoteConnection) {
|
|
1478
|
+
this._destroyed = true;
|
|
1479
|
+
this.closed.emit();
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
(0, import_node_assert7.default)(!this._remoteConnection._remoteConnection, new Error(`Remote already connected: ${this._remoteId}`));
|
|
1483
|
+
this._remoteConnection._remoteConnection = this;
|
|
1484
|
+
this._remoteConnection._remoteId = this._id;
|
|
1485
|
+
(0, import_log9.log)("connected", {
|
|
1486
|
+
id: this._id,
|
|
1487
|
+
remote: this._remoteId
|
|
1488
|
+
}, {
|
|
1489
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts",
|
|
1490
|
+
line: 94,
|
|
1491
|
+
scope: this,
|
|
1492
|
+
callSite: (f, a) => f(...a)
|
|
1493
|
+
});
|
|
1494
|
+
this.options.stream.pipe(this._outgoingDelay).pipe(this._remoteConnection.options.stream).pipe(this._incomingDelay).pipe(this.options.stream);
|
|
1495
|
+
this.connected.emit();
|
|
1496
|
+
this._remoteConnection.connected.emit();
|
|
1497
|
+
}).catch((err) => {
|
|
1498
|
+
if (this._destroyed) {
|
|
1499
|
+
return;
|
|
1500
|
+
}
|
|
1501
|
+
this.errors.raise(err);
|
|
1502
|
+
});
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
async destroy() {
|
|
1506
|
+
(0, import_log9.log)("closing", {
|
|
1507
|
+
id: this._id
|
|
1508
|
+
}, {
|
|
1509
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts",
|
|
1510
|
+
line: 115,
|
|
1511
|
+
scope: this,
|
|
1512
|
+
callSite: (f, a) => f(...a)
|
|
1513
|
+
});
|
|
1514
|
+
this._destroyed = true;
|
|
1515
|
+
_MemoryTransport._connections.delete(this._id);
|
|
1516
|
+
if (this._remoteConnection) {
|
|
1517
|
+
(0, import_log9.log)("closing", {
|
|
1518
|
+
id: this._remoteId
|
|
1519
|
+
}, {
|
|
1520
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts",
|
|
1521
|
+
line: 120,
|
|
1522
|
+
scope: this,
|
|
1523
|
+
callSite: (f, a) => f(...a)
|
|
1524
|
+
});
|
|
1525
|
+
this._remoteConnection._destroyed = true;
|
|
1526
|
+
_MemoryTransport._connections.delete(this._remoteId);
|
|
1527
|
+
this._outgoingDelay.unpipe();
|
|
1528
|
+
this._incomingDelay.unpipe();
|
|
1529
|
+
this._remoteConnection.closed.emit();
|
|
1530
|
+
this._remoteConnection._remoteConnection = void 0;
|
|
1531
|
+
this._remoteConnection = void 0;
|
|
1532
|
+
(0, import_log9.log)("closed", {
|
|
1533
|
+
id: this._remoteId
|
|
1534
|
+
}, {
|
|
1535
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts",
|
|
1536
|
+
line: 138,
|
|
1537
|
+
scope: this,
|
|
1538
|
+
callSite: (f, a) => f(...a)
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
this.closed.emit();
|
|
1542
|
+
(0, import_log9.log)("closed", {
|
|
1543
|
+
id: this._id
|
|
1544
|
+
}, {
|
|
1545
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts",
|
|
1546
|
+
line: 142,
|
|
1547
|
+
scope: this,
|
|
1548
|
+
callSite: (f, a) => f(...a)
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
signal(signal) {
|
|
1552
|
+
const { payload: json } = signal;
|
|
1553
|
+
if (json && json.transportId) {
|
|
1554
|
+
const transportId = json.transportId;
|
|
1555
|
+
if (transportId) {
|
|
1556
|
+
const remoteId = import_keys7.PublicKey.fromHex(transportId);
|
|
1557
|
+
(0, import_log9.log)("received signal", {
|
|
1558
|
+
id: this._id,
|
|
1559
|
+
remoteId
|
|
1560
|
+
}, {
|
|
1561
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts",
|
|
1562
|
+
line: 152,
|
|
1563
|
+
scope: this,
|
|
1564
|
+
callSite: (f, a) => f(...a)
|
|
1565
|
+
});
|
|
1566
|
+
this._remote.wake(remoteId);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
};
|
|
1571
|
+
var MemoryTransport = _MemoryTransport;
|
|
1572
|
+
MemoryTransport._connections = new import_util6.ComplexMap(import_keys7.PublicKey.hash);
|
|
1573
|
+
|
|
1574
|
+
// packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
|
|
1575
|
+
var import_node_assert8 = __toESM(require("node:assert"));
|
|
1576
|
+
var import_simple_peer = __toESM(require("simple-peer"));
|
|
1577
|
+
var import_async8 = require("@dxos/async");
|
|
1578
|
+
var import_debug5 = require("@dxos/debug");
|
|
1579
|
+
var import_log10 = require("@dxos/log");
|
|
1580
|
+
|
|
1581
|
+
// packages/core/mesh/network-manager/src/transport/webrtc.ts
|
|
1582
|
+
var wrtc = null;
|
|
1583
|
+
try {
|
|
1584
|
+
wrtc = require("@koush/wrtc");
|
|
1585
|
+
} catch (e) {
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
// packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
|
|
1589
|
+
var WebRTCTransport = class {
|
|
1590
|
+
constructor(params) {
|
|
1591
|
+
var _a;
|
|
1592
|
+
this.params = params;
|
|
1593
|
+
this.closed = new import_async8.Event();
|
|
1594
|
+
this.connected = new import_async8.Event();
|
|
1595
|
+
this.errors = new import_debug5.ErrorStream();
|
|
1596
|
+
(0, import_log10.log)("created connection", params, {
|
|
1597
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts",
|
|
1598
|
+
line: 34,
|
|
1599
|
+
scope: this,
|
|
1600
|
+
callSite: (f, a) => f(...a)
|
|
1601
|
+
});
|
|
1602
|
+
this._peer = new import_simple_peer.default({
|
|
1603
|
+
initiator: this.params.initiator,
|
|
1604
|
+
wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : (0, import_debug5.raise)(new Error("wrtc not available")),
|
|
1605
|
+
config: this.params.webrtcConfig
|
|
1606
|
+
});
|
|
1607
|
+
this._peer.on("signal", async (data) => {
|
|
1608
|
+
(0, import_log10.log)("signal", data, {
|
|
1609
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts",
|
|
1610
|
+
line: 42,
|
|
1611
|
+
scope: this,
|
|
1612
|
+
callSite: (f, a) => f(...a)
|
|
1613
|
+
});
|
|
1614
|
+
await this.params.sendSignal({
|
|
1615
|
+
payload: {
|
|
1616
|
+
data
|
|
1617
|
+
}
|
|
1618
|
+
});
|
|
1619
|
+
});
|
|
1620
|
+
this._peer.on("connect", () => {
|
|
1621
|
+
(0, import_log10.log)("connected", {}, {
|
|
1622
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts",
|
|
1623
|
+
line: 47,
|
|
1624
|
+
scope: this,
|
|
1625
|
+
callSite: (f, a) => f(...a)
|
|
1626
|
+
});
|
|
1627
|
+
this.params.stream.pipe(this._peer).pipe(this.params.stream);
|
|
1628
|
+
this.connected.emit();
|
|
1629
|
+
});
|
|
1630
|
+
this._peer.on("close", async () => {
|
|
1631
|
+
(0, import_log10.log)("closed", {}, {
|
|
1632
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts",
|
|
1633
|
+
line: 53,
|
|
1634
|
+
scope: this,
|
|
1635
|
+
callSite: (f, a) => f(...a)
|
|
1636
|
+
});
|
|
1637
|
+
await this._disconnectStreams();
|
|
1638
|
+
this.closed.emit();
|
|
1639
|
+
});
|
|
1640
|
+
this._peer.on("error", async (err) => {
|
|
1641
|
+
this.errors.raise(err);
|
|
1642
|
+
await this.destroy();
|
|
1643
|
+
});
|
|
1644
|
+
}
|
|
1645
|
+
async destroy() {
|
|
1646
|
+
(0, import_log10.log)("closing...", {}, {
|
|
1647
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts",
|
|
1648
|
+
line: 65,
|
|
1649
|
+
scope: this,
|
|
1650
|
+
callSite: (f, a) => f(...a)
|
|
1651
|
+
});
|
|
1652
|
+
await this._disconnectStreams();
|
|
1653
|
+
this._peer.destroy();
|
|
1654
|
+
this.closed.emit();
|
|
1655
|
+
(0, import_log10.log)("closed", {}, {
|
|
1656
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts",
|
|
1657
|
+
line: 69,
|
|
1658
|
+
scope: this,
|
|
1659
|
+
callSite: (f, a) => f(...a)
|
|
1660
|
+
});
|
|
1661
|
+
}
|
|
1662
|
+
signal(signal) {
|
|
1663
|
+
(0, import_node_assert8.default)(this._peer, "Connection not ready to accept signals.");
|
|
1664
|
+
(0, import_node_assert8.default)(signal.payload.data, "Signal message must contain signal data.");
|
|
1665
|
+
this._peer.signal(signal.payload.data);
|
|
1666
|
+
}
|
|
1667
|
+
async _disconnectStreams() {
|
|
1668
|
+
var _a, _b, _c, _d;
|
|
1669
|
+
(_d = (_c = (_b = (_a = this.params.stream).unpipe) == null ? void 0 : _b.call(_a, this._peer)) == null ? void 0 : _c.unpipe) == null ? void 0 : _d.call(_c, this.params.stream);
|
|
1670
|
+
}
|
|
1671
|
+
};
|
|
1672
|
+
|
|
1673
|
+
// packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
|
|
1674
|
+
var import_node_assert9 = __toESM(require("node:assert"));
|
|
1675
|
+
var import_node_stream2 = require("node:stream");
|
|
1676
|
+
var import_codec_protobuf = require("@dxos/codec-protobuf");
|
|
1677
|
+
var import_keys8 = require("@dxos/keys");
|
|
1678
|
+
var import_log11 = require("@dxos/log");
|
|
1679
|
+
var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
|
|
1680
|
+
var import_util7 = require("@dxos/util");
|
|
1681
|
+
var WebRTCTransportService = class {
|
|
1682
|
+
constructor(_webrtcConfig) {
|
|
1683
|
+
this._webrtcConfig = _webrtcConfig;
|
|
1684
|
+
this.transports = new import_util7.ComplexMap(import_keys8.PublicKey.hash);
|
|
1685
|
+
}
|
|
1686
|
+
open(request) {
|
|
1687
|
+
const rpcStream = new import_codec_protobuf.Stream(({ ready, next, close }) => {
|
|
1688
|
+
const duplex = new import_node_stream2.Duplex({
|
|
1689
|
+
read: () => {
|
|
1690
|
+
},
|
|
1691
|
+
write: function(chunk, _, callback) {
|
|
1692
|
+
next({
|
|
1693
|
+
data: {
|
|
1694
|
+
payload: chunk
|
|
1695
|
+
}
|
|
1696
|
+
});
|
|
1697
|
+
callback();
|
|
1698
|
+
}
|
|
1699
|
+
});
|
|
1700
|
+
const transport = new WebRTCTransport({
|
|
1701
|
+
initiator: request.initiator,
|
|
1702
|
+
stream: duplex,
|
|
1703
|
+
sendSignal: async (signal) => {
|
|
1704
|
+
next({
|
|
1705
|
+
signal: {
|
|
1706
|
+
payload: signal
|
|
1707
|
+
}
|
|
1708
|
+
});
|
|
1709
|
+
}
|
|
1710
|
+
});
|
|
1711
|
+
next({
|
|
1712
|
+
connection: {
|
|
1713
|
+
state: import_bridge.ConnectionState.CONNECTING
|
|
1714
|
+
}
|
|
1715
|
+
});
|
|
1716
|
+
transport.connected.on(() => {
|
|
1717
|
+
next({
|
|
1718
|
+
connection: {
|
|
1719
|
+
state: import_bridge.ConnectionState.CONNECTED
|
|
1720
|
+
}
|
|
1721
|
+
});
|
|
1722
|
+
});
|
|
1723
|
+
transport.errors.handle((err) => {
|
|
1724
|
+
next({
|
|
1725
|
+
connection: {
|
|
1726
|
+
state: import_bridge.ConnectionState.CLOSED,
|
|
1727
|
+
error: err.toString()
|
|
1728
|
+
}
|
|
1729
|
+
});
|
|
1730
|
+
close(err);
|
|
1731
|
+
});
|
|
1732
|
+
transport.closed.on(() => {
|
|
1733
|
+
next({
|
|
1734
|
+
connection: {
|
|
1735
|
+
state: import_bridge.ConnectionState.CLOSED
|
|
1736
|
+
}
|
|
1737
|
+
});
|
|
1738
|
+
close();
|
|
1739
|
+
});
|
|
1740
|
+
ready();
|
|
1741
|
+
this.transports.set(request.proxyId, {
|
|
1742
|
+
transport,
|
|
1743
|
+
stream: duplex
|
|
1744
|
+
});
|
|
1745
|
+
});
|
|
1746
|
+
return rpcStream;
|
|
1747
|
+
}
|
|
1748
|
+
async sendSignal({ proxyId, signal }) {
|
|
1749
|
+
(0, import_node_assert9.default)(this.transports.has(proxyId));
|
|
1750
|
+
await this.transports.get(proxyId).transport.signal(signal);
|
|
1751
|
+
}
|
|
1752
|
+
async sendData({ proxyId, payload }) {
|
|
1753
|
+
(0, import_node_assert9.default)(this.transports.has(proxyId));
|
|
1754
|
+
await this.transports.get(proxyId).stream.push(payload);
|
|
1755
|
+
}
|
|
1756
|
+
async close({ proxyId }) {
|
|
1757
|
+
var _a, _b;
|
|
1758
|
+
await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
|
|
1759
|
+
await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
|
|
1760
|
+
this.transports.delete(proxyId);
|
|
1761
|
+
(0, import_log11.log)("Closed.", {}, {
|
|
1762
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts",
|
|
1763
|
+
line: 108,
|
|
1764
|
+
scope: this,
|
|
1765
|
+
callSite: (f, a) => f(...a)
|
|
1766
|
+
});
|
|
1767
|
+
}
|
|
1768
|
+
};
|
|
1769
|
+
|
|
1770
|
+
// packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
|
|
1771
|
+
var import_node_assert10 = __toESM(require("node:assert"));
|
|
1772
|
+
var import_async9 = require("@dxos/async");
|
|
1773
|
+
var import_debug6 = require("@dxos/debug");
|
|
1774
|
+
var import_keys9 = require("@dxos/keys");
|
|
1775
|
+
var import_log12 = require("@dxos/log");
|
|
1776
|
+
var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
|
|
1777
|
+
var WebRTCTransportProxy = class {
|
|
1778
|
+
constructor(_params) {
|
|
1779
|
+
this._params = _params;
|
|
1780
|
+
this.closed = new import_async9.Event();
|
|
1781
|
+
this._closed = false;
|
|
1782
|
+
this.connected = new import_async9.Event();
|
|
1783
|
+
this.errors = new import_debug6.ErrorStream();
|
|
1784
|
+
this._proxyId = import_keys9.PublicKey.random();
|
|
1785
|
+
this._serviceStream = this._params.bridgeService.open({
|
|
1786
|
+
proxyId: this._proxyId,
|
|
1787
|
+
initiator: this._params.initiator
|
|
1788
|
+
});
|
|
1789
|
+
this._serviceStream.waitUntilReady().then(() => {
|
|
1790
|
+
this._serviceStream.subscribe(async (event) => {
|
|
1791
|
+
(0, import_log12.log)("WebRTCTransportProxy: event", event, {
|
|
1792
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts",
|
|
1793
|
+
line: 46,
|
|
1794
|
+
scope: this,
|
|
1795
|
+
callSite: (f, a) => f(...a)
|
|
1796
|
+
});
|
|
1797
|
+
if (event.connection) {
|
|
1798
|
+
await this._handleConnection(event.connection);
|
|
1799
|
+
} else if (event.data) {
|
|
1800
|
+
this._handleData(event.data);
|
|
1801
|
+
} else if (event.signal) {
|
|
1802
|
+
await this._handleSignal(event.signal);
|
|
1803
|
+
}
|
|
1804
|
+
});
|
|
1805
|
+
this._params.stream.on("data", async (data) => {
|
|
1806
|
+
try {
|
|
1807
|
+
await this._params.bridgeService.sendData({
|
|
1808
|
+
proxyId: this._proxyId,
|
|
1809
|
+
payload: data
|
|
1810
|
+
});
|
|
1811
|
+
} catch (err) {
|
|
1812
|
+
import_log12.log.catch(err, {}, {
|
|
1813
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts",
|
|
1814
|
+
line: 63,
|
|
1815
|
+
scope: this,
|
|
1816
|
+
callSite: (f, a) => f(...a)
|
|
1817
|
+
});
|
|
1818
|
+
}
|
|
1819
|
+
});
|
|
1820
|
+
}, (error) => import_log12.log.catch(error, {}, {
|
|
1821
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts",
|
|
1822
|
+
line: 67,
|
|
1823
|
+
scope: this,
|
|
1824
|
+
callSite: (f, a) => f(...a)
|
|
1825
|
+
}));
|
|
1826
|
+
}
|
|
1827
|
+
async _handleConnection(connectionEvent) {
|
|
1828
|
+
if (connectionEvent.error) {
|
|
1829
|
+
this.errors.raise(new Error(connectionEvent.error));
|
|
1830
|
+
}
|
|
1831
|
+
switch (connectionEvent.state) {
|
|
1832
|
+
case import_bridge2.ConnectionState.CONNECTED: {
|
|
1833
|
+
this.connected.emit();
|
|
1834
|
+
break;
|
|
1835
|
+
}
|
|
1836
|
+
case import_bridge2.ConnectionState.CLOSED: {
|
|
1837
|
+
await this.destroy();
|
|
1838
|
+
break;
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
_handleData(dataEvent) {
|
|
1843
|
+
this._params.stream.write(Buffer.from(dataEvent.payload));
|
|
1844
|
+
}
|
|
1845
|
+
async _handleSignal(signalEvent) {
|
|
1846
|
+
await this._params.sendSignal(signalEvent.payload);
|
|
1847
|
+
}
|
|
1848
|
+
signal(signal) {
|
|
1849
|
+
this._params.bridgeService.sendSignal({
|
|
1850
|
+
proxyId: this._proxyId,
|
|
1851
|
+
signal
|
|
1852
|
+
}).catch((err) => this.errors.raise(err));
|
|
1853
|
+
}
|
|
1854
|
+
async destroy() {
|
|
1855
|
+
if (this._closed) {
|
|
1856
|
+
return;
|
|
1857
|
+
}
|
|
1858
|
+
this._serviceStream.close();
|
|
1859
|
+
try {
|
|
1860
|
+
await this._params.bridgeService.close({
|
|
1861
|
+
proxyId: this._proxyId
|
|
1862
|
+
});
|
|
1863
|
+
} catch (err) {
|
|
1864
|
+
import_log12.log.catch(err, {}, {
|
|
1865
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts",
|
|
1866
|
+
line: 117,
|
|
1867
|
+
scope: this,
|
|
1868
|
+
callSite: (f, a) => f(...a)
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
this.closed.emit();
|
|
1872
|
+
this._closed = true;
|
|
1873
|
+
}
|
|
1874
|
+
forceClose() {
|
|
1875
|
+
this._serviceStream.close();
|
|
1876
|
+
this.closed.emit();
|
|
1877
|
+
this._closed = true;
|
|
1878
|
+
}
|
|
1879
|
+
};
|
|
1880
|
+
var WebRTCTransportProxyFactory = class {
|
|
1881
|
+
constructor() {
|
|
1882
|
+
this._connections = /* @__PURE__ */ new Set();
|
|
1883
|
+
}
|
|
1884
|
+
setBridgeService(bridgeService) {
|
|
1885
|
+
this._bridgeService = bridgeService;
|
|
1886
|
+
for (const connection of this._connections) {
|
|
1887
|
+
connection.forceClose();
|
|
1888
|
+
}
|
|
1889
|
+
return this;
|
|
1890
|
+
}
|
|
1891
|
+
createTransport(options) {
|
|
1892
|
+
(0, import_node_assert10.default)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections");
|
|
1893
|
+
const transport = new WebRTCTransportProxy({
|
|
1894
|
+
...options,
|
|
1895
|
+
bridgeService: this._bridgeService
|
|
1896
|
+
});
|
|
1897
|
+
this._connections.add(transport);
|
|
1898
|
+
transport.closed.on(() => this._connections.delete(transport));
|
|
1899
|
+
return transport;
|
|
1900
|
+
}
|
|
1901
|
+
};
|
|
1902
|
+
|
|
1903
|
+
// packages/core/mesh/network-manager/src/wire-protocol.ts
|
|
1904
|
+
var import_crypto = require("@dxos/crypto");
|
|
1905
|
+
var import_teleport = require("@dxos/teleport");
|
|
1906
|
+
var adaptProtocolProvider = (factory) => ({ initiator, localPeerId, remotePeerId, topic }) => {
|
|
1907
|
+
const protocol = factory({
|
|
1908
|
+
channel: (0, import_crypto.discoveryKey)(topic),
|
|
1909
|
+
initiator
|
|
1910
|
+
});
|
|
1911
|
+
return {
|
|
1912
|
+
initialize: () => protocol.open(),
|
|
1913
|
+
destroy: () => protocol.close(),
|
|
1914
|
+
stream: protocol.stream,
|
|
1915
|
+
protocol
|
|
1916
|
+
};
|
|
1917
|
+
};
|
|
1918
|
+
|
|
1919
|
+
// packages/core/mesh/network-manager/src/testing/test-protocol.ts
|
|
1920
|
+
var import_node_assert12 = __toESM(require("node:assert"));
|
|
1921
|
+
var import_node_events = require("node:events");
|
|
1922
|
+
var import_keys11 = require("@dxos/keys");
|
|
1923
|
+
var import_log14 = require("@dxos/log");
|
|
1924
|
+
var import_mesh_protocol2 = require("@dxos/mesh-protocol");
|
|
1925
|
+
|
|
1926
|
+
// packages/core/mesh/network-manager/src/protocol-factory.ts
|
|
1927
|
+
var import_node_assert11 = __toESM(require("node:assert"));
|
|
1928
|
+
var import_crypto2 = require("@dxos/crypto");
|
|
1929
|
+
var import_keys10 = require("@dxos/keys");
|
|
1930
|
+
var import_log13 = require("@dxos/log");
|
|
1931
|
+
var import_mesh_protocol = require("@dxos/mesh-protocol");
|
|
1932
|
+
var protocolFactory = ({ session = {}, plugins = [], getTopics }) => {
|
|
1933
|
+
(0, import_node_assert11.default)(getTopics);
|
|
1934
|
+
return ({ channel, initiator }) => {
|
|
1935
|
+
(0, import_log13.log)("creating protocol", {}, {
|
|
1936
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/protocol-factory.ts",
|
|
1937
|
+
line: 41,
|
|
1938
|
+
scope: void 0,
|
|
1939
|
+
callSite: (f, a) => f(...a)
|
|
1940
|
+
});
|
|
1941
|
+
const protocol = new import_mesh_protocol.Protocol({
|
|
1942
|
+
streamOptions: {
|
|
1943
|
+
live: true
|
|
1944
|
+
},
|
|
1945
|
+
discoveryToPublicKey: (dk) => {
|
|
1946
|
+
const publicKey = getTopics().find((topic) => (0, import_crypto2.discoveryKey)(topic).equals(dk));
|
|
1947
|
+
if (publicKey) {
|
|
1948
|
+
protocol.setContext({
|
|
1949
|
+
topic: import_keys10.PublicKey.stringify(publicKey)
|
|
1950
|
+
});
|
|
1951
|
+
}
|
|
1952
|
+
(0, import_node_assert11.default)(publicKey, "PublicKey not found in discovery.");
|
|
1953
|
+
return publicKey;
|
|
1954
|
+
},
|
|
1955
|
+
initiator,
|
|
1956
|
+
userSession: session,
|
|
1957
|
+
discoveryKey: channel
|
|
1958
|
+
});
|
|
1959
|
+
protocol.setExtensions(plugins.map((plugin) => plugin.createExtension())).init();
|
|
1960
|
+
return protocol;
|
|
1961
|
+
};
|
|
1962
|
+
};
|
|
1963
|
+
|
|
1964
|
+
// packages/core/mesh/network-manager/src/testing/test-protocol.ts
|
|
1965
|
+
var EXTENSION_NAME = "test";
|
|
1966
|
+
var getPeerId = (protocol) => {
|
|
1967
|
+
var _a;
|
|
1968
|
+
const { peerId } = (_a = protocol.getSession()) != null ? _a : {};
|
|
1969
|
+
if (!peerId) {
|
|
1970
|
+
return void 0;
|
|
1971
|
+
}
|
|
1972
|
+
return import_keys11.PublicKey.bufferize(peerId);
|
|
1973
|
+
};
|
|
1974
|
+
var testProtocolProvider = (topic, peerId, protocolPlugin) => {
|
|
1975
|
+
(0, import_log14.log)("creating protocol-factory", {
|
|
1976
|
+
topic: import_keys11.PublicKey.from(topic),
|
|
1977
|
+
peerId
|
|
1978
|
+
}, {
|
|
1979
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-protocol.ts",
|
|
1980
|
+
line: 34,
|
|
1981
|
+
scope: void 0,
|
|
1982
|
+
callSite: (f, a) => f(...a)
|
|
1983
|
+
});
|
|
1984
|
+
return protocolFactory({
|
|
1985
|
+
getTopics: () => [
|
|
1986
|
+
topic
|
|
1987
|
+
],
|
|
1988
|
+
session: {
|
|
1989
|
+
peerId: peerId.toHex()
|
|
1990
|
+
},
|
|
1991
|
+
plugins: [
|
|
1992
|
+
protocolPlugin
|
|
1993
|
+
]
|
|
1994
|
+
});
|
|
1995
|
+
};
|
|
1996
|
+
var TestProtocolPlugin = class extends import_node_events.EventEmitter {
|
|
1997
|
+
constructor(peerId, uppercase = false) {
|
|
1998
|
+
super();
|
|
1999
|
+
this._peers = /* @__PURE__ */ new Map();
|
|
2000
|
+
this.initCalled = false;
|
|
2001
|
+
(0, import_node_assert12.default)(Buffer.isBuffer(peerId));
|
|
2002
|
+
this._peerId = peerId;
|
|
2003
|
+
this._uppercase = uppercase;
|
|
2004
|
+
}
|
|
2005
|
+
get peerId() {
|
|
2006
|
+
return this._peerId;
|
|
2007
|
+
}
|
|
2008
|
+
get peers() {
|
|
2009
|
+
return Array.from(this._peers.values());
|
|
2010
|
+
}
|
|
2011
|
+
createExtension() {
|
|
2012
|
+
(0, import_log14.log)("creating extension", {
|
|
2013
|
+
peerId: import_keys11.PublicKey.from(this.peerId)
|
|
2014
|
+
}, {
|
|
2015
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-protocol.ts",
|
|
2016
|
+
line: 92,
|
|
2017
|
+
scope: this,
|
|
2018
|
+
callSite: (f, a) => f(...a)
|
|
2019
|
+
});
|
|
2020
|
+
return new import_mesh_protocol2.Extension(EXTENSION_NAME, {
|
|
2021
|
+
binary: true
|
|
2022
|
+
}).setInitHandler(this._init.bind(this)).setMessageHandler(this._receive.bind(this)).setHandshakeHandler(this._onPeerConnect.bind(this)).setCloseHandler(this._onPeerDisconnect.bind(this));
|
|
2023
|
+
}
|
|
2024
|
+
async _init(protocol) {
|
|
2025
|
+
this.initCalled = true;
|
|
2026
|
+
}
|
|
2027
|
+
async send(peerId, payload) {
|
|
2028
|
+
(0, import_node_assert12.default)(Buffer.isBuffer(peerId));
|
|
2029
|
+
const peerIdStr = import_keys11.PublicKey.stringify(peerId);
|
|
2030
|
+
const peer = this._peers.get(peerIdStr);
|
|
2031
|
+
if (!peer) {
|
|
2032
|
+
throw new Error(`peer not found: ${import_keys11.PublicKey.from(peerId).truncate()}`);
|
|
2033
|
+
}
|
|
2034
|
+
const extension = peer.getExtension(EXTENSION_NAME);
|
|
2035
|
+
const encoded = Buffer.from(payload);
|
|
2036
|
+
(0, import_log14.log)("sending", {
|
|
2037
|
+
peerId: import_keys11.PublicKey.from(peerId),
|
|
2038
|
+
payload
|
|
2039
|
+
}, {
|
|
2040
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-protocol.ts",
|
|
2041
|
+
line: 124,
|
|
2042
|
+
scope: this,
|
|
2043
|
+
callSite: (f, a) => f(...a)
|
|
2044
|
+
});
|
|
2045
|
+
return await extension.send(encoded, {
|
|
2046
|
+
oneway: true
|
|
2047
|
+
});
|
|
2048
|
+
}
|
|
2049
|
+
async _receive(protocol, data) {
|
|
2050
|
+
const peerId = getPeerId(protocol);
|
|
2051
|
+
(0, import_node_assert12.default)(peerId !== void 0);
|
|
2052
|
+
let payload = data.data.toString();
|
|
2053
|
+
if (this._uppercase) {
|
|
2054
|
+
payload = payload.toUpperCase();
|
|
2055
|
+
}
|
|
2056
|
+
(0, import_log14.log)("received", {
|
|
2057
|
+
peerId: import_keys11.PublicKey.from(peerId),
|
|
2058
|
+
payload
|
|
2059
|
+
}, {
|
|
2060
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-protocol.ts",
|
|
2061
|
+
line: 136,
|
|
2062
|
+
scope: this,
|
|
2063
|
+
callSite: (f, a) => f(...a)
|
|
2064
|
+
});
|
|
2065
|
+
this.emit("receive", protocol, payload);
|
|
2066
|
+
}
|
|
2067
|
+
async _onPeerConnect(protocol) {
|
|
2068
|
+
(0, import_log14.log)("connecting", {
|
|
2069
|
+
id: import_keys11.PublicKey.from(protocol.id)
|
|
2070
|
+
}, {
|
|
2071
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-protocol.ts",
|
|
2072
|
+
line: 141,
|
|
2073
|
+
scope: this,
|
|
2074
|
+
callSite: (f, a) => f(...a)
|
|
2075
|
+
});
|
|
2076
|
+
const peerId = getPeerId(protocol);
|
|
2077
|
+
if (peerId === void 0) {
|
|
2078
|
+
return;
|
|
2079
|
+
}
|
|
2080
|
+
const peerIdStr = import_keys11.PublicKey.stringify(peerId);
|
|
2081
|
+
if (this._peers.has(peerIdStr)) {
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
this._peers.set(peerIdStr, protocol);
|
|
2085
|
+
this.emit("connect", protocol);
|
|
2086
|
+
}
|
|
2087
|
+
async _onPeerDisconnect(protocol) {
|
|
2088
|
+
(0, import_log14.log)("disconnecting", {
|
|
2089
|
+
id: import_keys11.PublicKey.from(protocol.id)
|
|
2090
|
+
}, {
|
|
2091
|
+
file: "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-protocol.ts",
|
|
2092
|
+
line: 157,
|
|
2093
|
+
scope: this,
|
|
2094
|
+
callSite: (f, a) => f(...a)
|
|
2095
|
+
});
|
|
2096
|
+
const peerId = getPeerId(protocol);
|
|
2097
|
+
if (peerId === void 0) {
|
|
2098
|
+
return;
|
|
2099
|
+
}
|
|
2100
|
+
this._peers.delete(import_keys11.PublicKey.stringify(peerId));
|
|
2101
|
+
this.emit("disconnect", peerId);
|
|
2102
|
+
}
|
|
2103
|
+
};
|
|
2104
|
+
|
|
2105
|
+
// packages/core/mesh/network-manager/src/testing/test-builder.ts
|
|
2106
|
+
var TEST_SIGNAL_URL = "ws://localhost:4000/.well-known/dx/signal";
|
|
2107
|
+
var TestBuilder = class {
|
|
2108
|
+
constructor(options = {}) {
|
|
2109
|
+
this.options = options;
|
|
2110
|
+
this._signalContext = new import_messaging2.MemorySignalManagerContext();
|
|
2111
|
+
}
|
|
2112
|
+
createSignalManager() {
|
|
2113
|
+
if (this.options.signalHosts) {
|
|
2114
|
+
return new import_messaging2.WebsocketSignalManager(this.options.signalHosts);
|
|
2115
|
+
}
|
|
2116
|
+
return new import_messaging2.MemorySignalManager(this._signalContext);
|
|
2117
|
+
}
|
|
2118
|
+
createPeer() {
|
|
2119
|
+
return new TestPeer(this);
|
|
2120
|
+
}
|
|
2121
|
+
};
|
|
2122
|
+
var TestPeer = class {
|
|
2123
|
+
constructor(testBuilder) {
|
|
2124
|
+
this.testBuilder = testBuilder;
|
|
2125
|
+
this.peerId = import_keys12.PublicKey.random();
|
|
2126
|
+
this._swarms = new import_util8.ComplexMap(import_keys12.PublicKey.hash);
|
|
2127
|
+
this._networkManager = this.createNetworkManager();
|
|
2128
|
+
}
|
|
2129
|
+
createNetworkManager() {
|
|
2130
|
+
let transportFactory = MemoryTransportFactory;
|
|
2131
|
+
if (this.testBuilder.options.signalHosts) {
|
|
2132
|
+
if (this.testBuilder.options.bridge) {
|
|
2133
|
+
const [proxyPort, servicePort] = (0, import_rpc.createLinkedPorts)();
|
|
2134
|
+
this._proxy = (0, import_rpc.createProtoRpcPeer)({
|
|
2135
|
+
port: proxyPort,
|
|
2136
|
+
requested: {
|
|
2137
|
+
BridgeService: import_protocols2.schema.getService("dxos.mesh.bridge.BridgeService")
|
|
2138
|
+
},
|
|
2139
|
+
noHandshake: true,
|
|
2140
|
+
encodingOptions: {
|
|
2141
|
+
preserveAny: true
|
|
2142
|
+
}
|
|
2143
|
+
});
|
|
2144
|
+
this._service = (0, import_rpc.createProtoRpcPeer)({
|
|
2145
|
+
port: servicePort,
|
|
2146
|
+
exposed: {
|
|
2147
|
+
BridgeService: import_protocols2.schema.getService("dxos.mesh.bridge.BridgeService")
|
|
2148
|
+
},
|
|
2149
|
+
handlers: {
|
|
2150
|
+
BridgeService: new WebRTCTransportService()
|
|
2151
|
+
},
|
|
2152
|
+
noHandshake: true,
|
|
2153
|
+
encodingOptions: {
|
|
2154
|
+
preserveAny: true
|
|
2155
|
+
}
|
|
2156
|
+
});
|
|
2157
|
+
transportFactory = new WebRTCTransportProxyFactory().setBridgeService(this._proxy.rpc.BridgeService);
|
|
2158
|
+
} else {
|
|
2159
|
+
transportFactory = {
|
|
2160
|
+
createTransport: (params) => new WebRTCTransport(params)
|
|
2161
|
+
};
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
return new NetworkManager({
|
|
2165
|
+
signalManager: this.testBuilder.createSignalManager(),
|
|
2166
|
+
transportFactory
|
|
2167
|
+
});
|
|
2168
|
+
}
|
|
2169
|
+
async open() {
|
|
2170
|
+
var _a, _b;
|
|
2171
|
+
await ((_a = this._proxy) == null ? void 0 : _a.open());
|
|
2172
|
+
await ((_b = this._service) == null ? void 0 : _b.open());
|
|
2173
|
+
}
|
|
2174
|
+
async close() {
|
|
2175
|
+
var _a, _b;
|
|
2176
|
+
await Promise.all(Array.from(this._swarms.values()).map((swarm) => swarm.leave()));
|
|
2177
|
+
this._swarms.clear();
|
|
2178
|
+
await ((_a = this._proxy) == null ? void 0 : _a.close());
|
|
2179
|
+
await ((_b = this._service) == null ? void 0 : _b.close());
|
|
2180
|
+
await this._networkManager.close();
|
|
2181
|
+
}
|
|
2182
|
+
getSwarm(topic) {
|
|
2183
|
+
const swarm = this._swarms.get(topic);
|
|
2184
|
+
if (!swarm) {
|
|
2185
|
+
throw new Error(`Swarm not found for topic: ${topic}`);
|
|
2186
|
+
}
|
|
2187
|
+
return swarm;
|
|
2188
|
+
}
|
|
2189
|
+
createSwarm(topic) {
|
|
2190
|
+
const swarm = new TestSwarmConnection(this, topic);
|
|
2191
|
+
this._swarms.set(topic, swarm);
|
|
2192
|
+
return swarm;
|
|
2193
|
+
}
|
|
2194
|
+
};
|
|
2195
|
+
var TestSwarmConnection = class {
|
|
2196
|
+
constructor(peer, topic) {
|
|
2197
|
+
this.peer = peer;
|
|
2198
|
+
this.topic = topic;
|
|
2199
|
+
this.plugin = new TestProtocolPlugin(this.peer.peerId.asBuffer());
|
|
2200
|
+
}
|
|
2201
|
+
async join(topology = new FullyConnectedTopology()) {
|
|
2202
|
+
await this.peer._networkManager.joinSwarm({
|
|
2203
|
+
topic: this.topic,
|
|
2204
|
+
peerId: this.peer.peerId,
|
|
2205
|
+
protocolProvider: adaptProtocolProvider(testProtocolProvider(this.topic.asBuffer(), this.peer.peerId, this.plugin)),
|
|
2206
|
+
topology
|
|
2207
|
+
});
|
|
2208
|
+
return this;
|
|
2209
|
+
}
|
|
2210
|
+
async leave() {
|
|
2211
|
+
await this.peer._networkManager.leaveSwarm(this.topic);
|
|
2212
|
+
return this;
|
|
2213
|
+
}
|
|
2214
|
+
};
|
|
2215
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2216
|
+
0 && (module.exports = {
|
|
2217
|
+
TEST_SIGNAL_URL,
|
|
2218
|
+
TestBuilder,
|
|
2219
|
+
TestPeer,
|
|
2220
|
+
TestProtocolPlugin,
|
|
2221
|
+
TestSwarmConnection,
|
|
2222
|
+
getPeerId,
|
|
2223
|
+
testProtocolProvider
|
|
2224
|
+
});
|
|
2225
|
+
//# sourceMappingURL=index.cjs.map
|