peerband 0.1.5 → 0.1.6
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/peerband.mjs +216 -216
- package/herdr/control.mjs +2 -1
- package/herdr/lifecycle.mjs +30 -18
- package/herdr/pane.mjs +22 -7
- package/herdr-plugin.toml +1 -1
- package/package.json +1 -1
package/herdr/control.mjs
CHANGED
|
@@ -13,7 +13,8 @@ const matches = (a, b) => typeof a === "string" && timingSafeEqual(
|
|
|
13
13
|
createHash("sha256").update(b).digest(),
|
|
14
14
|
);
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
// Generous: on a loaded Windows machine the helper can take seconds to answer even a local pipe.
|
|
17
|
+
export async function connectControl({ timeout = 10_000, initialCommand } = {}) {
|
|
17
18
|
const record = JSON.parse(await readFile(recordPath, "utf8"));
|
|
18
19
|
const socket = createConnection(channel);
|
|
19
20
|
await new Promise((resolve, reject) => {
|
package/herdr/lifecycle.mjs
CHANGED
|
@@ -16,15 +16,33 @@ const pluginRoot = resolve(dirname(script), "..");
|
|
|
16
16
|
const connectorEntry = process.env.PEERBAND_CONNECTOR_ENTRY || join(pluginRoot, "dist", "peerband.mjs");
|
|
17
17
|
const paneStatePath = relayStatePath(config);
|
|
18
18
|
|
|
19
|
+
// Starting Node can take several seconds on a loaded machine (5-8 s has been seen on Windows), so waits are
|
|
20
|
+
// deadlines measured in time, not a fixed number of quick retries.
|
|
21
|
+
const CONTROL_TIMEOUT_MS = 10_000;
|
|
22
|
+
const HELPER_START_MS = 30_000;
|
|
23
|
+
// A helper started for the Relay pane stops if no pane attaches within this time.
|
|
24
|
+
const AWAIT_UI_MS = 60_000;
|
|
25
|
+
|
|
26
|
+
async function waitForHelper() {
|
|
27
|
+
const deadline = Date.now() + HELPER_START_MS;
|
|
28
|
+
for (;;) {
|
|
29
|
+
try { return await request("status"); }
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (Date.now() >= deadline) throw error;
|
|
32
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
19
37
|
async function request(command) {
|
|
20
|
-
const socket = await connectControl({ initialCommand: command, timeout:
|
|
38
|
+
const socket = await connectControl({ initialCommand: command, timeout: CONTROL_TIMEOUT_MS });
|
|
21
39
|
return new Promise((resolve, reject) => {
|
|
22
40
|
let text = "";
|
|
23
41
|
let settled = false;
|
|
24
42
|
const timer = setTimeout(() => {
|
|
25
43
|
socket.destroy();
|
|
26
44
|
finish(new Error("Helper control timeout"));
|
|
27
|
-
},
|
|
45
|
+
}, CONTROL_TIMEOUT_MS);
|
|
28
46
|
const finish = (error, value) => {
|
|
29
47
|
if (settled) return;
|
|
30
48
|
settled = true;
|
|
@@ -274,7 +292,7 @@ async function supervise() {
|
|
|
274
292
|
});
|
|
275
293
|
if (process.argv[3] !== "--await-ui") startChild();
|
|
276
294
|
else if (!owner) {
|
|
277
|
-
const idleTimer = setTimeout(() => { if (!owner) stopSupervisor(); },
|
|
295
|
+
const idleTimer = setTimeout(() => { if (!owner) stopSupervisor(); }, AWAIT_UI_MS);
|
|
278
296
|
idleTimer.unref();
|
|
279
297
|
}
|
|
280
298
|
}
|
|
@@ -361,11 +379,8 @@ async function main() {
|
|
|
361
379
|
cwd: config, detached: true, stdio: "ignore", windowsHide: true, shell: false,
|
|
362
380
|
});
|
|
363
381
|
supervisor.unref();
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
catch { await new Promise((resolve) => setTimeout(resolve, 100)); }
|
|
367
|
-
}
|
|
368
|
-
throw new Error("The PeerBand helper did not start. Run `peerband status`, then `peerband login` if you are not logged in.");
|
|
382
|
+
try { console.log(JSON.stringify(await waitForHelper())); return; }
|
|
383
|
+
catch { throw new Error("The PeerBand helper did not start. Run `peerband status`, then `peerband login` if you are not logged in."); }
|
|
369
384
|
}
|
|
370
385
|
if (command === "start" || command === "enable") {
|
|
371
386
|
await assertConnectorEntry();
|
|
@@ -379,17 +394,14 @@ async function main() {
|
|
|
379
394
|
shell: false,
|
|
380
395
|
});
|
|
381
396
|
supervisor.unref();
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
397
|
+
try {
|
|
398
|
+
console.log(JSON.stringify(await waitForHelper()));
|
|
399
|
+
return;
|
|
400
|
+
} catch {
|
|
401
|
+
throw new Error(
|
|
402
|
+
"The PeerBand helper did not start. Run `peerband status`, then `peerband login` if you are not logged in.",
|
|
403
|
+
);
|
|
389
404
|
}
|
|
390
|
-
throw new Error(
|
|
391
|
-
"The PeerBand helper did not start. Run `peerband status`, then `peerband login` if you are not logged in.",
|
|
392
|
-
);
|
|
393
405
|
}
|
|
394
406
|
if (command === "status" || command === "stop" || command === "disable") {
|
|
395
407
|
try {
|
package/herdr/pane.mjs
CHANGED
|
@@ -50,14 +50,25 @@ function draw() {
|
|
|
50
50
|
stdout.write("\x1b[2J\x1b[H" + renderPane({ snapshot, identity, email, messages, error: errorText, columns: stdout.columns, rows: stdout.rows }));
|
|
51
51
|
}
|
|
52
52
|
async function runLifecycle(command) {
|
|
53
|
-
|
|
53
|
+
// start-ui starts Node twice (lifecycle, then the helper); on a loaded machine that alone can take 10+ seconds.
|
|
54
|
+
const { stdout: result } = await exec(process.execPath, [lifecycle, command], { timeout: 45_000, windowsHide: true, maxBuffer: 65_536 });
|
|
54
55
|
return JSON.parse(result.trim());
|
|
55
56
|
}
|
|
56
57
|
const controlAlive = () => Boolean(control && !control.destroyed);
|
|
58
|
+
// Renaming the tab runs the herdr CLI (several processes). Do it when the On/Off state changes, not on every refresh.
|
|
59
|
+
let tabState;
|
|
60
|
+
function syncTab(enabled) {
|
|
61
|
+
if (tabState === enabled) return;
|
|
62
|
+
tabState = enabled;
|
|
63
|
+
// Retry on the next refresh when the rename did not happen (it returns false without running herdr when this pane
|
|
64
|
+
// is not the recorded Relay tab).
|
|
65
|
+
const retry = () => { if (tabState === enabled) tabState = undefined; };
|
|
66
|
+
void syncRelayTab(enabled).then((renamed) => { if (!renamed) retry(); }, retry);
|
|
67
|
+
}
|
|
57
68
|
function showUnavailable(message = UNAVAILABLE) {
|
|
58
69
|
snapshot = { state: "stopping", connectionState: "helper-unavailable", activity: snapshot?.activity ?? [] };
|
|
59
70
|
errorText = message;
|
|
60
|
-
|
|
71
|
+
syncTab(false);
|
|
61
72
|
}
|
|
62
73
|
async function refresh() {
|
|
63
74
|
if (closing || pending || refreshInFlight) return;
|
|
@@ -66,7 +77,7 @@ async function refresh() {
|
|
|
66
77
|
const generation = refreshGeneration;
|
|
67
78
|
try {
|
|
68
79
|
const next = await sendUi("ui-status");
|
|
69
|
-
if (generation === refreshGeneration) { snapshot = next; if (!busyError) errorText = "";
|
|
80
|
+
if (generation === refreshGeneration) { snapshot = next; if (!busyError) errorText = ""; syncTab(next.state === "running"); }
|
|
70
81
|
} catch {
|
|
71
82
|
if (generation !== refreshGeneration) return;
|
|
72
83
|
showUnavailable();
|
|
@@ -83,14 +94,16 @@ async function toggle() {
|
|
|
83
94
|
draw();
|
|
84
95
|
try {
|
|
85
96
|
if (!controlAlive()) {
|
|
86
|
-
// No helper: Space starts one, like opening the tab does.
|
|
97
|
+
// No helper: Space starts one, like opening the tab does. That can take a while on a busy machine.
|
|
98
|
+
errorText = "Starting the PeerBand relay…";
|
|
99
|
+
draw();
|
|
87
100
|
await attach({ start: true });
|
|
88
101
|
return;
|
|
89
102
|
}
|
|
90
103
|
const next = await sendUi(snapshot?.state === "running" ? "ui-disable" : "ui-enable");
|
|
91
104
|
if (next.error === "busy") throw new Error("The relay is still stopping. Try On again in a moment.");
|
|
92
105
|
snapshot = next;
|
|
93
|
-
|
|
106
|
+
syncTab(next.state === "running");
|
|
94
107
|
} catch (error) {
|
|
95
108
|
errorText = error.message || "Could not change the connection. Check enrollment and the helper owner.";
|
|
96
109
|
busyError = error.message === "The relay is still stopping. Try On again in a moment.";
|
|
@@ -184,7 +197,7 @@ async function attach({ start }) {
|
|
|
184
197
|
ownsUi = true;
|
|
185
198
|
snapshot = start ? await sendUi("ui-enable") : attached;
|
|
186
199
|
errorText = "";
|
|
187
|
-
|
|
200
|
+
syncTab(snapshot.state === "running");
|
|
188
201
|
draw();
|
|
189
202
|
if (!timer) {
|
|
190
203
|
timer = setInterval(refresh, 1000);
|
|
@@ -219,6 +232,8 @@ if (!stdinIsInteractive()) {
|
|
|
219
232
|
console.log(renderPane({ snapshot, identity, email, messages, error: "Interactive terminal controls are required in this pane." }));
|
|
220
233
|
} else {
|
|
221
234
|
try {
|
|
235
|
+
errorText = "Starting the PeerBand relay…";
|
|
236
|
+
draw();
|
|
222
237
|
await attach({ start: true });
|
|
223
238
|
await refresh();
|
|
224
239
|
} catch (error) {
|
|
@@ -236,7 +251,7 @@ function sendUi(command) {
|
|
|
236
251
|
return new Promise((resolve, reject) => {
|
|
237
252
|
if (!control || control.destroyed) { reject(new Error("Relay owner channel is closed")); return; }
|
|
238
253
|
const request = { resolve, reject, timer: undefined };
|
|
239
|
-
request.timer = setTimeout(() => failControl(new Error("Relay owner request timed out")),
|
|
254
|
+
request.timer = setTimeout(() => failControl(new Error("Relay owner request timed out")), 10_000);
|
|
240
255
|
replies.push(request);
|
|
241
256
|
control.write(JSON.stringify({ command }) + "\n");
|
|
242
257
|
});
|
package/herdr-plugin.toml
CHANGED
package/package.json
CHANGED