@highway1/cli 0.1.41 → 0.1.42
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/index.js +63 -21
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/commands/join.ts +69 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@highway1/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.42",
|
|
4
4
|
"description": "CLI tool for Clawiverse network",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"clean": "rm -rf dist"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@highway1/core": "^0.1.
|
|
16
|
+
"@highway1/core": "^0.1.41",
|
|
17
17
|
"chalk": "^5.3.0",
|
|
18
18
|
"cli-table3": "^0.6.5",
|
|
19
19
|
"commander": "^12.1.0",
|
package/src/commands/join.ts
CHANGED
|
@@ -60,16 +60,31 @@ export function registerJoinCommand(program: Command): void {
|
|
|
60
60
|
const connectSpin = spinner('Connecting to bootstrap peers...');
|
|
61
61
|
|
|
62
62
|
// Phase 1: wait for peer:connect (up to 10s)
|
|
63
|
+
let connected = false;
|
|
63
64
|
await new Promise<void>((resolve) => {
|
|
64
65
|
const timeout = setTimeout(resolve, 10000);
|
|
65
66
|
node.libp2p.addEventListener('peer:connect', () => {
|
|
67
|
+
connected = true;
|
|
66
68
|
clearTimeout(timeout);
|
|
67
69
|
resolve();
|
|
68
70
|
}, { once: true });
|
|
69
71
|
});
|
|
70
72
|
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
+
// If discovery has not connected us yet, proactively dial bootstrap peers once.
|
|
74
|
+
if (!connected && bootstrapPeers.length > 0) {
|
|
75
|
+
info('No peer discovered yet, dialing bootstrap peers...');
|
|
76
|
+
for (const bootstrapAddr of bootstrapPeers) {
|
|
77
|
+
try {
|
|
78
|
+
await node.libp2p.dial(bootstrapAddr);
|
|
79
|
+
connected = true;
|
|
80
|
+
break;
|
|
81
|
+
} catch {
|
|
82
|
+
// try next bootstrap peer
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Phase 2: wait for relay reservation using event + polling (more reliable across libp2p versions)
|
|
73
88
|
const countRelayAddrs = () => node.getMultiaddrs().filter(a => a.includes('/p2p-circuit')).length;
|
|
74
89
|
const initialRelayCount = countRelayAddrs();
|
|
75
90
|
|
|
@@ -77,32 +92,63 @@ export function registerJoinCommand(program: Command): void {
|
|
|
77
92
|
info('Waiting for relay reservation...');
|
|
78
93
|
|
|
79
94
|
let reservationSucceeded = false;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
95
|
+
if (initialRelayCount === 0) {
|
|
96
|
+
await new Promise<void>((resolve) => {
|
|
97
|
+
const RELAY_WAIT_MS = 30000;
|
|
98
|
+
const POLL_MS = 500;
|
|
99
|
+
let settled = false;
|
|
100
|
+
|
|
101
|
+
const finish = () => {
|
|
102
|
+
if (settled) return;
|
|
103
|
+
settled = true;
|
|
104
|
+
clearTimeout(timeout);
|
|
105
|
+
clearInterval(pollTimer);
|
|
106
|
+
(node.libp2p as any).removeEventListener('relay:reservation', onReservation);
|
|
107
|
+
(node.libp2p as any).removeEventListener('self:peer:update', onPeerUpdate);
|
|
108
|
+
resolve();
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const onReservation = () => {
|
|
112
|
+
reservationSucceeded = true;
|
|
113
|
+
info('✓ Relay reservation successful!');
|
|
114
|
+
setTimeout(finish, 300);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const onPeerUpdate = () => {
|
|
118
|
+
if (countRelayAddrs() > 0) {
|
|
119
|
+
reservationSucceeded = true;
|
|
120
|
+
info('✓ Relay address detected from peer update');
|
|
121
|
+
finish();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
88
124
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
125
|
+
const pollTimer = setInterval(() => {
|
|
126
|
+
if (countRelayAddrs() > 0) {
|
|
127
|
+
reservationSucceeded = true;
|
|
128
|
+
info('✓ Relay address detected');
|
|
129
|
+
finish();
|
|
130
|
+
}
|
|
131
|
+
}, POLL_MS);
|
|
96
132
|
|
|
97
|
-
|
|
133
|
+
const timeout = setTimeout(() => {
|
|
134
|
+
if (!reservationSucceeded) {
|
|
135
|
+
info(`Relay reservation timeout after ${RELAY_WAIT_MS / 1000}s.`);
|
|
136
|
+
info(`Connected peers: ${node.libp2p.getPeers().map(p => p.toString()).join(', ')}`);
|
|
137
|
+
}
|
|
138
|
+
finish();
|
|
139
|
+
}, RELAY_WAIT_MS);
|
|
98
140
|
|
|
99
|
-
|
|
100
|
-
(node.libp2p as any).
|
|
101
|
-
|
|
102
|
-
|
|
141
|
+
// Event names vary by libp2p internals; keep as any to avoid typing mismatch.
|
|
142
|
+
(node.libp2p as any).addEventListener('relay:reservation', onReservation);
|
|
143
|
+
(node.libp2p as any).addEventListener('self:peer:update', onPeerUpdate);
|
|
144
|
+
});
|
|
145
|
+
} else {
|
|
146
|
+
reservationSucceeded = true;
|
|
147
|
+
info('✓ Relay address already present');
|
|
148
|
+
}
|
|
103
149
|
|
|
104
150
|
if (!reservationSucceeded) {
|
|
105
|
-
info('⚠ Relay reservation did not complete
|
|
151
|
+
info('⚠ Relay reservation did not complete, continuing with fallback relay addresses');
|
|
106
152
|
}
|
|
107
153
|
|
|
108
154
|
connectSpin.succeed('Connected to network!');
|