@feigi/fleet-ctl 3.24.5 → 3.24.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/package.json +1 -1
- package/scripts/board.mjs +55 -20
package/package.json
CHANGED
package/scripts/board.mjs
CHANGED
|
@@ -52,6 +52,7 @@ import { fileURLToPath } from "node:url";
|
|
|
52
52
|
import { isCLI } from "./is-cli.mjs";
|
|
53
53
|
import { dirname, join, basename } from "node:path";
|
|
54
54
|
import { createServer, request as httpRequest } from "node:http";
|
|
55
|
+
import { connect } from "node:net";
|
|
55
56
|
import { inspect } from "node:util";
|
|
56
57
|
|
|
57
58
|
const NAME = "board";
|
|
@@ -1582,7 +1583,34 @@ function bindFailure(server, port) {
|
|
|
1582
1583
|
const onListening = () => { server.removeListener("error", onError); resolve(null); };
|
|
1583
1584
|
server.once("error", onError);
|
|
1584
1585
|
server.once("listening", onListening);
|
|
1585
|
-
server.listen(port);
|
|
1586
|
+
server.listen(port, COCKPIT_HOST);
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
// Whether something already answers on COCKPIT_HOST:port, asked BEFORE the
|
|
1591
|
+
// bind rather than read off it: a 127.0.0.1 bind does not reliably fail when
|
|
1592
|
+
// the port is taken. On macOS/BSD, SO_REUSEADDR lets it succeed beside a
|
|
1593
|
+
// listener that holds every interface — a dev server, or a cockpit started
|
|
1594
|
+
// before the bind was narrowed to loopback — and the launch would then sit
|
|
1595
|
+
// on that holder's 127.0.0.1 traffic instead of stepping over it or reusing
|
|
1596
|
+
// it. Linux refuses that bind outright; this check gives both the same answer.
|
|
1597
|
+
// Resolves in bindFailure()'s vocabulary so the loop reads one shape: null
|
|
1598
|
+
// when the connection is refused (nothing holds the port), an EADDRINUSE
|
|
1599
|
+
// error when it connects or times out, and any other error as itself — a
|
|
1600
|
+
// fault of its own, like EACCES on a bind. A timeout counts as held because
|
|
1601
|
+
// a live holder whose accept backlog is full drops the SYN rather than
|
|
1602
|
+
// refusing it. The window between this check and the bind is a race the
|
|
1603
|
+
// loop accepts, like its others.
|
|
1604
|
+
function heldFailure(port) {
|
|
1605
|
+
return new Promise((resolve) => {
|
|
1606
|
+
const socket = connect({ host: COCKPIT_HOST, port });
|
|
1607
|
+
const held = () => {
|
|
1608
|
+
socket.destroy();
|
|
1609
|
+
resolve(Object.assign(new Error(`port ${port} answers on ${COCKPIT_HOST}`), { code: "EADDRINUSE" }));
|
|
1610
|
+
};
|
|
1611
|
+
socket.setTimeout(PROBE_TIMEOUT_MS, held);
|
|
1612
|
+
socket.once("connect", held);
|
|
1613
|
+
socket.once("error", (e) => resolve(e.code === "ECONNREFUSED" ? null : e));
|
|
1586
1614
|
});
|
|
1587
1615
|
}
|
|
1588
1616
|
|
|
@@ -1605,12 +1633,16 @@ function bindFailure(server, port) {
|
|
|
1605
1633
|
// ruled out as the fix for it — it would have to clear a gather(), which
|
|
1606
1634
|
// charges every launch past a stranger-held port that much per candidate.
|
|
1607
1635
|
const PROBE_TIMEOUT_MS = 1000;
|
|
1608
|
-
//
|
|
1609
|
-
//
|
|
1610
|
-
//
|
|
1611
|
-
//
|
|
1612
|
-
//
|
|
1613
|
-
|
|
1636
|
+
// The one address a cockpit binds, and so the one address the probe and the
|
|
1637
|
+
// pre-bind check dial: whatever holds a candidate where this launch would
|
|
1638
|
+
// bind answers here. 127.0.0.1 rather than `localhost` keeps a resolver out
|
|
1639
|
+
// of the launch path, and loopback alone keeps the board — a read-only
|
|
1640
|
+
// mirror of the ledger — off every other machine on the network; remote
|
|
1641
|
+
// viewing goes through an `ssh -L` tunnel. IPv4 only: `::1` is left unbound,
|
|
1642
|
+
// which is why every URL printed below names 127.0.0.1 too. `localhost`
|
|
1643
|
+
// resolves `::1` first, and on `::1` a browser can reach some other process
|
|
1644
|
+
// that holds the same port.
|
|
1645
|
+
const COCKPIT_HOST = "127.0.0.1";
|
|
1614
1646
|
// A holder is not necessarily a cockpit, and a second of localhost writes is
|
|
1615
1647
|
// a lot of memory to accept from one. The board payload for a real run is
|
|
1616
1648
|
// kilobytes; anything past this is not one, so it is refused as foreign
|
|
@@ -1663,7 +1695,7 @@ export function probeCockpitWorkspace(port, timeoutMs = PROBE_TIMEOUT_MS) {
|
|
|
1663
1695
|
req.destroy();
|
|
1664
1696
|
resolve(workspace);
|
|
1665
1697
|
}
|
|
1666
|
-
const req = httpRequest({ host:
|
|
1698
|
+
const req = httpRequest({ host: COCKPIT_HOST, port, path: "/board.json", method: "GET" }, (res) => {
|
|
1667
1699
|
if (res.statusCode !== 200) { finish(null); return; }
|
|
1668
1700
|
let body = "";
|
|
1669
1701
|
res.setEncoding("utf8");
|
|
@@ -1810,9 +1842,10 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
|
|
|
1810
1842
|
// port with nothing a handshake could ever match, reopening the
|
|
1811
1843
|
// dual-cockpit hazard.
|
|
1812
1844
|
//
|
|
1813
|
-
// Bind stays
|
|
1814
|
-
//
|
|
1815
|
-
//
|
|
1845
|
+
// Bind stays ahead of any handshake per candidate — only heldFailure()'s
|
|
1846
|
+
// bare connect precedes it: that is what lets two launches racing at start
|
|
1847
|
+
// settle on one winner quickly, the winner's identity published (below)
|
|
1848
|
+
// before it can block inside its own gather().
|
|
1816
1849
|
// But a successful bind is not the end of the story — once this process
|
|
1817
1850
|
// holds a candidate, the REST of the window still gets checked for a
|
|
1818
1851
|
// live cockpit that landed further along, exactly the shape a departed
|
|
@@ -1829,14 +1862,15 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
|
|
|
1829
1862
|
for (let i = 0; i < candidates.length; i++) {
|
|
1830
1863
|
const candidate = candidates[i];
|
|
1831
1864
|
const attempt = createBoardServer(stateDir);
|
|
1832
|
-
|
|
1865
|
+
// --port 0 asks the kernel for a port nobody holds; there is nothing to dial.
|
|
1866
|
+
const failure = (candidate === 0 ? null : await heldFailure(candidate)) ?? await bindFailure(attempt, candidate);
|
|
1833
1867
|
if (!failure) {
|
|
1834
1868
|
const rest = scannable ? candidates.slice(i + 1) : [];
|
|
1835
1869
|
const holders = await Promise.all(rest.map((p) => probeCockpitWorkspace(p)));
|
|
1836
1870
|
const matchAt = holders.indexOf(instance.workspace);
|
|
1837
1871
|
if (matchAt === -1) { server = attempt; break; }
|
|
1838
1872
|
attempt.close();
|
|
1839
|
-
const url = `http
|
|
1873
|
+
const url = `http://${COCKPIT_HOST}:${rest[matchAt]}/`;
|
|
1840
1874
|
console.error(`${NAME}: cockpit already running for this workspace on ${url}`);
|
|
1841
1875
|
// Nothing is opened here, --open or not. This cockpit was
|
|
1842
1876
|
// already running when this launch arrived, so its tab was the business
|
|
@@ -1852,14 +1886,15 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
|
|
|
1852
1886
|
process.exit(0);
|
|
1853
1887
|
}
|
|
1854
1888
|
// Only a port that is TAKEN is a candidate for any of the below. EACCES
|
|
1855
|
-
// on a privileged port, EADDRNOTAVAIL on an unusable address
|
|
1856
|
-
//
|
|
1857
|
-
//
|
|
1889
|
+
// on a privileged port, EADDRNOTAVAIL on an unusable address, a pre-bind
|
|
1890
|
+
// connect that fails with anything but ECONNREFUSED: those are faults of
|
|
1891
|
+
// their own, and scanning past them would bury each one under an
|
|
1892
|
+
// exhausted-range message at the end that names the wrong problem.
|
|
1858
1893
|
if (failure.code !== "EADDRINUSE") die(failure.message);
|
|
1859
1894
|
if (!scannable) die(`port ${candidate} in use — pass --port <n>`);
|
|
1860
1895
|
const holder = await probeCockpitWorkspace(candidate);
|
|
1861
1896
|
if (holder === instance.workspace) {
|
|
1862
|
-
const url = `http
|
|
1897
|
+
const url = `http://${COCKPIT_HOST}:${candidate}/`;
|
|
1863
1898
|
console.error(`${NAME}: cockpit already running for this workspace on ${url}`);
|
|
1864
1899
|
// Already running, so nothing opens — the same rule as the arm above.
|
|
1865
1900
|
process.exit(0);
|
|
@@ -1894,11 +1929,11 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
|
|
|
1894
1929
|
//
|
|
1895
1930
|
// Announce the port we GOT, not the one we asked for. They differ for the
|
|
1896
1931
|
// one value `--port 0` newly permits: listen(0) binds an ephemeral port, so
|
|
1897
|
-
// echoing the request prints — and --opens — http://
|
|
1932
|
+
// echoing the request prints — and --opens — http://127.0.0.1:0, which
|
|
1898
1933
|
// reaches nothing while the board sits on a port nobody was told.
|
|
1899
1934
|
// address() is only populated once listening, hence only here.
|
|
1900
1935
|
const bound = server.address().port;
|
|
1901
|
-
console.error(`${NAME}: cockpit on http
|
|
1936
|
+
console.error(`${NAME}: cockpit on http://${COCKPIT_HOST}:${bound} (interval ${interval}s)`);
|
|
1902
1937
|
|
|
1903
1938
|
// Publish identity the instant this port is ours — before the
|
|
1904
1939
|
// first tick, which is the one that can be slow (gather() shells out to
|
|
@@ -1948,7 +1983,7 @@ export async function serve({ ledgerFile, port, interval, open, spendDir } = {})
|
|
|
1948
1983
|
// return only when that browser exits, so a launcher awaited any earlier
|
|
1949
1984
|
// could hold the first tick back for the whole session. openBrowser()
|
|
1950
1985
|
// never throws; a launcher that is missing or fails is one warning line.
|
|
1951
|
-
if (open) await openBrowser(`http
|
|
1986
|
+
if (open) await openBrowser(`http://${COCKPIT_HOST}:${bound}/`);
|
|
1952
1987
|
await new Promise(() => {}); // run until signalled
|
|
1953
1988
|
}
|
|
1954
1989
|
|