pinomad 0.3.0 → 0.4.0

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.
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ assertSecureServerOrigin,
4
+ probeServerAccounts,
5
+ runServerOperation
6
+ } from "./chunk-5785kwej.js";
7
+ import {
8
+ readServerConfig,
9
+ removeServerConfig
10
+ } from "./chunk-302y6by9.js";
11
+ import"./chunk-knaw1a9g.js";
12
+ import {
13
+ argValue,
14
+ restartService,
15
+ serviceManager,
16
+ withoutRelayArgs
17
+ } from "./chunk-akgxwg8f.js";
18
+ import"./chunk-fgx2sh7d.js";
19
+ import"./chunk-374zqx5h.js";
20
+ import"./chunk-myjp910s.js";
21
+ import {
22
+ loadHostKey,
23
+ loadRelayKey
24
+ } from "./chunk-837gh8nr.js";
25
+ import {
26
+ relayHostId
27
+ } from "./chunk-7gmkgyaz.js";
28
+ import {
29
+ DEFAULT_DATA_DIR
30
+ } from "./chunk-cjfmfyjb.js";
31
+ import"./chunk-df32avw6.js";
32
+
33
+ // apps/host/src/cli/leave.ts
34
+ import { stat } from "node:fs/promises";
35
+ import { join, resolve } from "node:path";
36
+ import { parseArgs } from "node:util";
37
+ var message = (error) => error instanceof Error ? error.message : String(error);
38
+ async function leaveServer(args, deps) {
39
+ const dataDir = resolve(args.dataDir);
40
+ const config = await readServerConfig(dataDir);
41
+ if (config === undefined)
42
+ throw new Error(`not joined — no server.json in ${dataDir}`);
43
+ const origin = config.origin;
44
+ const warnings = [];
45
+ const notes = [];
46
+ let hostId;
47
+ assertSecureServerOrigin(origin);
48
+ if (await stat(join(dataDir, "relay-key")).then(() => true, () => false)) {
49
+ const signingKey = await loadRelayKey(dataDir);
50
+ const hostKey = await loadHostKey(dataDir);
51
+ hostId = relayHostId(signingKey.publicKey);
52
+ const probe = await probeServerAccounts(origin, deps?.fetch);
53
+ if (probe === "unreachable") {
54
+ warnings.push(`cannot reach ${origin} — the server-side binding was not removed`);
55
+ } else if (probe === "enabled") {
56
+ try {
57
+ const reply = await runServerOperation({
58
+ origin,
59
+ signingKey,
60
+ noisePublicKey: hostKey.publicKey,
61
+ op: "leave"
62
+ });
63
+ if (reply.t === "refused" && reply.reason !== "not_joined") {
64
+ warnings.push(`the server refused the leave: ${reply.reason}`);
65
+ } else if (reply.t === "refused") {
66
+ notes.push("the server had no binding for this host");
67
+ }
68
+ } catch (error) {
69
+ warnings.push(`the server could not be asked to unbind (${message(error)}) — its record of this host may remain`);
70
+ }
71
+ } else {
72
+ notes.push("the server runs no accounts — nothing was bound");
73
+ }
74
+ } else {
75
+ warnings.push("no relay key on disk — the server-side binding, if any, was not removed");
76
+ }
77
+ await removeServerConfig(dataDir);
78
+ const manager = deps?.manager ?? serviceManager();
79
+ try {
80
+ const installed = await manager.state().then((state) => state.installed, () => false);
81
+ const previous = installed ? await manager.read() : undefined;
82
+ if (!installed) {
83
+ notes.push("no service installed — restart any running host so it stops dialing the server");
84
+ } else if (previous !== undefined && resolve(previous.dataDir) !== dataDir) {
85
+ notes.push(`the installed service uses ${previous.dataDir}, not ${dataDir} — left untouched`);
86
+ } else {
87
+ const stripped = previous === undefined ? undefined : { ...previous, hostArgs: withoutRelayArgs(previous.hostArgs) };
88
+ const rewriting = stripped !== undefined && stripped.hostArgs.length !== previous.hostArgs.length;
89
+ const port = argValue(previous?.hostArgs ?? [], "--port");
90
+ const code = await restartService({
91
+ force: false,
92
+ argv: ["--data-dir", dataDir, ...port === undefined ? [] : ["--url", `ws://127.0.0.1:${port}`]],
93
+ manager,
94
+ ...rewriting ? {
95
+ restart: async () => {
96
+ await manager.install(stripped);
97
+ }
98
+ } : {}
99
+ });
100
+ if (code !== 0)
101
+ warnings.push("the service restart did not finish — check `service status`");
102
+ }
103
+ } catch (error) {
104
+ warnings.push(`could not update the service definition: ${message(error)} — remove --relay from it and restart manually`);
105
+ }
106
+ return {
107
+ origin,
108
+ ...hostId === undefined ? {} : { hostId },
109
+ warnings,
110
+ notes
111
+ };
112
+ }
113
+ async function main(argv) {
114
+ const { values } = parseArgs({
115
+ args: [...argv],
116
+ options: { "data-dir": { type: "string" } }
117
+ });
118
+ try {
119
+ const outcome = await leaveServer({ dataDir: values["data-dir"] ?? DEFAULT_DATA_DIR });
120
+ console.log(`left ${outcome.origin}`);
121
+ if (outcome.hostId !== undefined)
122
+ console.log(`host id: ${outcome.hostId}`);
123
+ for (const note of outcome.notes)
124
+ console.log(`note: ${note}`);
125
+ for (const warning of outcome.warnings)
126
+ console.error(`warning: ${warning}`);
127
+ if (outcome.warnings.length > 0)
128
+ process.exitCode = 1;
129
+ } catch (error) {
130
+ console.error(message(error));
131
+ process.exitCode = 1;
132
+ }
133
+ }
134
+ if (false)
135
+ ;
136
+ export {
137
+ main,
138
+ leaveServer
139
+ };
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  loadRelayKey
3
- } from "./chunk-fxaewke2.js";
3
+ } from "./chunk-837gh8nr.js";
4
4
  import {
5
5
  relayHostId
6
- } from "./chunk-z4x88s0q.js";
6
+ } from "./chunk-7gmkgyaz.js";
7
7
  import {
8
8
  DEFAULT_DATA_DIR
9
9
  } from "./chunk-cjfmfyjb.js";