@procedures/peer 0.2.3 → 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.
Files changed (69) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/README.md +3 -3
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/connection/index.d.ts +3 -2
  5. package/dist/connection/index.d.ts.map +1 -1
  6. package/dist/connection/index.js +6 -2
  7. package/dist/connection/index.js.map +1 -1
  8. package/dist/discovery/index.d.ts +4 -2
  9. package/dist/discovery/index.d.ts.map +1 -1
  10. package/dist/discovery/index.js +22 -34
  11. package/dist/discovery/index.js.map +1 -1
  12. package/dist/input-schemas.d.ts +5 -7
  13. package/dist/input-schemas.d.ts.map +1 -1
  14. package/dist/input-schemas.js +4 -15
  15. package/dist/input-schemas.js.map +1 -1
  16. package/dist/module.d.ts +2 -1
  17. package/dist/module.d.ts.map +1 -1
  18. package/dist/module.js +2 -7
  19. package/dist/module.js.map +1 -1
  20. package/dist/output-schemas.d.ts +1 -4
  21. package/dist/output-schemas.d.ts.map +1 -1
  22. package/dist/output-schemas.js +0 -23
  23. package/dist/output-schemas.js.map +1 -1
  24. package/dist/peer/index.d.ts +16 -3
  25. package/dist/peer/index.d.ts.map +1 -1
  26. package/dist/peer/index.js +45 -6
  27. package/dist/peer/index.js.map +1 -1
  28. package/dist/procedures/index.d.ts +0 -6
  29. package/dist/procedures/index.d.ts.map +1 -1
  30. package/dist/procedures/index.js +1 -3
  31. package/dist/procedures/index.js.map +1 -1
  32. package/dist/procedures/manifest.generate.d.ts.map +1 -1
  33. package/dist/procedures/manifest.generate.js +10 -33
  34. package/dist/procedures/manifest.generate.js.map +1 -1
  35. package/dist/procedures/server.call.d.ts.map +1 -1
  36. package/dist/procedures/server.call.js +8 -3
  37. package/dist/procedures/server.call.js.map +1 -1
  38. package/dist/procedures/server.connect.d.ts.map +1 -1
  39. package/dist/procedures/server.connect.js +3 -1
  40. package/dist/procedures/server.connect.js.map +1 -1
  41. package/dist/types.d.ts +2 -58
  42. package/dist/types.d.ts.map +1 -1
  43. package/package.json +3 -3
  44. package/src/connection/index.ts +8 -5
  45. package/src/discovery/index.ts +26 -35
  46. package/src/input-schemas.ts +4 -27
  47. package/src/module.ts +2 -7
  48. package/src/output-schemas.ts +0 -29
  49. package/src/peer/index.ts +70 -7
  50. package/src/procedures/index.ts +1 -8
  51. package/src/procedures/manifest.generate.ts +10 -33
  52. package/src/procedures/server.call.ts +8 -3
  53. package/src/procedures/server.connect.ts +3 -1
  54. package/src/types.ts +2 -76
  55. package/dist/procedures/server.start.d.ts +0 -14
  56. package/dist/procedures/server.start.d.ts.map +0 -1
  57. package/dist/procedures/server.start.js +0 -182
  58. package/dist/procedures/server.start.js.map +0 -1
  59. package/dist/procedures/server.status.d.ts +0 -13
  60. package/dist/procedures/server.status.d.ts.map +0 -1
  61. package/dist/procedures/server.status.js +0 -118
  62. package/dist/procedures/server.status.js.map +0 -1
  63. package/dist/procedures/server.stop.d.ts +0 -12
  64. package/dist/procedures/server.stop.d.ts.map +0 -1
  65. package/dist/procedures/server.stop.js +0 -98
  66. package/dist/procedures/server.stop.js.map +0 -1
  67. package/src/procedures/server.start.ts +0 -212
  68. package/src/procedures/server.status.ts +0 -141
  69. package/src/procedures/server.stop.ts +0 -123
@@ -1,118 +0,0 @@
1
- /**
2
- * Procedure: server.status
3
- * Gets the current CLI server status
4
- */
5
- import * as path from "node:path";
6
- import * as os from "node:os";
7
- import { createProcedure } from "@procedures/client";
8
- import { wrapSchema } from "../schema.js";
9
- import { ServerStatusOutputSchema } from "../output-schemas.js";
10
- import { serverStatusInputSchema } from "../input-schemas.js";
11
- import { checkServer, readAllLockfiles, readLockfileForPort, removeLockfileForPort } from "../lockfile.js";
12
- const MARK_DIR = path.join(os.homedir(), ".mark");
13
- const LOG_PATH = path.join(MARK_DIR, "server.log");
14
- const serverStatusOutputSchema = wrapSchema(ServerStatusOutputSchema);
15
- /**
16
- * Format uptime as human-readable string
17
- */
18
- function formatUptime(startedAt) {
19
- const start = new Date(startedAt).getTime();
20
- const now = Date.now();
21
- const diff = Math.floor((now - start) / 1000);
22
- if (diff < 60) {
23
- return `${diff}s`;
24
- }
25
- if (diff < 3600) {
26
- const mins = Math.floor(diff / 60);
27
- const secs = diff % 60;
28
- return `${mins}m ${secs}s`;
29
- }
30
- if (diff < 86400) {
31
- const hours = Math.floor(diff / 3600);
32
- const mins = Math.floor((diff % 3600) / 60);
33
- return `${hours}h ${mins}m`;
34
- }
35
- const days = Math.floor(diff / 86400);
36
- const hours = Math.floor((diff % 86400) / 3600);
37
- return `${days}d ${hours}h`;
38
- }
39
- export const serverStatusProcedure = createProcedure()
40
- .path(["server", "status"])
41
- .input(serverStatusInputSchema)
42
- .output(serverStatusOutputSchema)
43
- .meta({
44
- description: "Get CLI server status",
45
- // It deletes the lockfile of a server that does not answer
46
- effects: { reads: ["fs", "server"], writes: ["fs"], idempotent: true },
47
- args: [],
48
- shorts: { port: "p" },
49
- output: "text",
50
- })
51
- .handler(async (input) => {
52
- // Get target servers
53
- let targets;
54
- if (input.port !== undefined) {
55
- const lockfile = readLockfileForPort(input.port);
56
- targets = lockfile ? [lockfile] : [];
57
- }
58
- else {
59
- targets = readAllLockfiles();
60
- }
61
- if (targets.length === 0) {
62
- return {
63
- running: false,
64
- message: input.port
65
- ? `No server found on port ${input.port}`
66
- : "No servers running",
67
- };
68
- }
69
- // For single server, return detailed info
70
- if (targets.length === 1) {
71
- const lockfile = targets[0];
72
- const { pid, port, endpoint, startedAt, transport } = lockfile;
73
- if ((await checkServer(lockfile)) !== "alive") {
74
- removeLockfileForPort(port);
75
- return {
76
- running: false,
77
- message: "Server is not running (cleaned up stale lockfile)",
78
- };
79
- }
80
- const uptime = formatUptime(startedAt);
81
- return {
82
- running: true,
83
- pid,
84
- port,
85
- endpoint,
86
- transport,
87
- startedAt,
88
- uptime,
89
- logFile: LOG_PATH,
90
- message: `Server running (PID ${pid}, port ${port}, uptime ${uptime})`,
91
- };
92
- }
93
- // Multiple servers - summarize
94
- const lines = [];
95
- let anyRunning = false;
96
- for (const lockfile of targets) {
97
- const { pid, port, endpoint, startedAt } = lockfile;
98
- if ((await checkServer(lockfile)) !== "alive") {
99
- removeLockfileForPort(port);
100
- continue;
101
- }
102
- anyRunning = true;
103
- const uptime = formatUptime(startedAt);
104
- lines.push(` Port ${port}: PID ${pid}, uptime ${uptime}, ${endpoint}`);
105
- }
106
- if (!anyRunning) {
107
- return {
108
- running: false,
109
- message: "No servers running (cleaned up stale lockfiles)",
110
- };
111
- }
112
- return {
113
- running: true,
114
- message: `${lines.length} server(s) running:\n${lines.join("\n")}`,
115
- };
116
- })
117
- .build();
118
- //# sourceMappingURL=server.status.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.status.js","sourceRoot":"","sources":["../../src/procedures/server.status.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAgC,MAAM,oBAAoB,CAAC;AACnF,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AAChE,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,qBAAqB,EAAqB,MAAM,gBAAgB,CAAC;AAG9H,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,OAAO,CAAC,CAAC;AAClD,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;AAEnD,MAAM,wBAAwB,GAAG,UAAU,CAAqB,wBAAwB,CAAC,CAAC;AAM1F;;GAEG;AACH,SAAS,YAAY,CAAC,SAAiB;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AC;KACD,KAAK,EAAE,CAAC"}
@@ -1,12 +0,0 @@
1
- /**
2
- * Procedure: server.stop
3
- * Stops the running CLI server daemon
4
- */
5
- import { type Effects, type Procedure } from "@procedures/client";
6
- import type { ServerStopInput, ServerStopOutput } from "../types.js";
7
- export declare const serverStopProcedure: Procedure<ServerStopInput, ServerStopOutput, {
8
- description: string;
9
- effects: Effects;
10
- }>;
11
- export type { ServerStopInput, ServerStopOutput };
12
- //# sourceMappingURL=server.stop.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.stop.d.ts","sourceRoot":"","sources":["../../src/procedures/server.stop.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAmB,KAAK,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAKnF,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAqBrE,eAAO,MAAM,mBAAmB,EAAE,SAAS,CACzC,eAAe,EACf,gBAAgB,EAChB;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAsFjC,CAAC;AAEX,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,CAAC"}
@@ -1,98 +0,0 @@
1
- /**
2
- * Procedure: server.stop
3
- * Stops the running CLI server daemon
4
- */
5
- import { createProcedure } from "@procedures/client";
6
- import { wrapSchema } from "../schema.js";
7
- import { ServerStopOutputSchema } from "../output-schemas.js";
8
- import { serverStopInputSchema } from "../input-schemas.js";
9
- import { checkServer, readAllLockfiles, readLockfileForPort, removeLockfileForPort } from "../lockfile.js";
10
- const serverStopOutputSchema = wrapSchema(ServerStopOutputSchema);
11
- /**
12
- * Kill process by PID
13
- */
14
- function killProcess(pid, signal = "SIGTERM") {
15
- try {
16
- process.kill(pid, signal);
17
- return true;
18
- }
19
- catch {
20
- return false;
21
- }
22
- }
23
- export const serverStopProcedure = createProcedure()
24
- .path(["server", "stop"])
25
- .input(serverStopInputSchema)
26
- .output(serverStopOutputSchema)
27
- .meta({
28
- description: "Stop running CLI server",
29
- // It stops the servers of the lockfiles and deletes their lockfiles
30
- effects: { reads: ["fs", "server"], writes: ["fs", "server"], idempotent: true },
31
- args: [],
32
- shorts: { port: "p" },
33
- })
34
- .handler(async (input) => {
35
- const force = input.force ?? false;
36
- const signal = force ? "SIGKILL" : "SIGTERM";
37
- // Get target servers
38
- let targets;
39
- if (input.port !== undefined) {
40
- const lockfile = readLockfileForPort(input.port);
41
- targets = lockfile ? [lockfile] : [];
42
- }
43
- else {
44
- targets = readAllLockfiles();
45
- }
46
- if (targets.length === 0) {
47
- return {
48
- success: false,
49
- message: input.port
50
- ? `No server found on port ${input.port}`
51
- : "No servers running",
52
- };
53
- }
54
- const results = [];
55
- let allSuccess = true;
56
- for (const lockfile of targets) {
57
- const { pid, port } = lockfile;
58
- // Signal the PID only when the server answers as the peer of this lockfile: Windows reuses
59
- // PIDs, and before, a stale lockfile made this kill an unrelated process (deep dive CLI-4)
60
- const state = await checkServer(lockfile);
61
- if (state !== "alive") {
62
- removeLockfileForPort(port);
63
- results.push(state === "dead"
64
- ? `Port ${port}: cleaned up stale lockfile`
65
- : `Port ${port}: another process answers on this port; nothing stopped (stale lockfile removed)`);
66
- continue;
67
- }
68
- const sent = killProcess(pid, signal);
69
- if (!sent) {
70
- allSuccess = false;
71
- results.push(`Port ${port}: failed to send ${signal} to PID ${pid}`);
72
- continue;
73
- }
74
- // Wait for process to die
75
- const maxWait = force ? 500 : 5000;
76
- const startTime = Date.now();
77
- let stopped = false;
78
- while (Date.now() - startTime < maxWait) {
79
- if ((await checkServer(lockfile, 300)) === "dead") {
80
- removeLockfileForPort(port);
81
- results.push(`Port ${port}: stopped (PID ${pid})`);
82
- stopped = true;
83
- break;
84
- }
85
- await new Promise((resolve) => setTimeout(resolve, 100));
86
- }
87
- if (!stopped) {
88
- allSuccess = false;
89
- results.push(`Port ${port}: PID ${pid} did not stop${force ? "" : " (try --force)"}`);
90
- }
91
- }
92
- return {
93
- success: allSuccess,
94
- message: results.join("\n"),
95
- };
96
- })
97
- .build();
98
- //# sourceMappingURL=server.stop.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.stop.js","sourceRoot":"","sources":["../../src/procedures/server.stop.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,eAAe,EAAgC,MAAM,oBAAoB,CAAC;AACnF,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,qBAAqB,EAAqB,MAAM,gBAAgB,CAAC;AAI9H,MAAM,sBAAsB,GAAG,UAAU,CAAmB,sBAAsB,CAAC,CAAC;AAMpF;;GAEG;AACH,SAAS,WAAW,CAAC,GAAW,EAAE,MAAM,GAAmB,SAAS;IAClE,IAAI,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,mBAAmB,GAI5B,eAAe,EAAE;KAClB,IAAI,CAAC,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;KACxB,KAAK,CAAC,qBAAqB,CAAC;KAC5B,MAAM,CAAC,sBAAsB,CAAC;KAC9B,IAAI,CAAC;IACJ,WAAW,EAAE,yBAAyB;IACtC,oEAAoE;IACpE,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE;IAChF,IAAI,EAAE,EAAE;IACR,MAAM,E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AE,UAAU;QACnB,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;KAC5B,CAAC;AACJ,CAAC,CAAC;KACD,KAAK,EAAE,CAAC"}
@@ -1,212 +0,0 @@
1
- /**
2
- * Procedure: server.start
3
- * Starts the CLI server as a background daemon
4
- */
5
-
6
- import { spawn } from "node:child_process";
7
- import * as fs from "node:fs";
8
- import * as path from "node:path";
9
- import * as os from "node:os";
10
- import { createProcedure, type Effects, type Procedure } from "@procedures/client";
11
- import { wrapSchema } from "../schema.js";
12
- import { ServerStartOutputSchema } from "../output-schemas.js";
13
- import { serverStartInputSchema } from "../input-schemas.js";
14
- import { checkServer, readLockfileForPort } from "../lockfile.js";
15
- import type { ServerStartInput, ServerStartOutput } from "../types.js";
16
-
17
- const MARK_DIR = path.join(os.homedir(), ".mark");
18
- const SERVERS_DIR = path.join(MARK_DIR, "servers");
19
- const LOG_PATH = path.join(MARK_DIR, "server.log");
20
- const LOG_PREV_PATH = path.join(MARK_DIR, "server.log.1");
21
-
22
- const serverStartOutputSchema = wrapSchema<ServerStartOutput>(ServerStartOutputSchema);
23
-
24
-
25
- /**
26
- * Check if process is alive
27
- */
28
- function isProcessAlive(pid: number): boolean {
29
- try {
30
- process.kill(pid, 0);
31
- return true;
32
- } catch {
33
- return false;
34
- }
35
- }
36
-
37
- /**
38
- * Rotate log file
39
- */
40
- function rotateLog(): void {
41
- try {
42
- fs.mkdirSync(MARK_DIR, { recursive: true });
43
- if (fs.existsSync(LOG_PATH)) {
44
- fs.renameSync(LOG_PATH, LOG_PREV_PATH);
45
- }
46
- } catch {
47
- // Ignore rotation errors
48
- }
49
- }
50
-
51
- /**
52
- * Find the CLI entry point
53
- */
54
- function findCliPath(): string {
55
- // First, try to use the currently running script if it's the CLI
56
- const currentScript = process.argv[1];
57
- if (currentScript) {
58
- // Normalize path for cross-platform check
59
- const normalized = currentScript.replace(/\\/g, "/");
60
- // mark runs as dist/bin.js (its bin) or dist/cli.js
61
- if (normalized.endsWith("/dist/bin.js") || normalized.endsWith("cli.js") || normalized.includes("cli/dist")) {
62
- // Return absolute path
63
- return path.resolve(currentScript);
64
- }
65
- }
66
-
67
- // Try to find the mark CLI in node_modules or as a direct path
68
- const candidates = [
69
- path.join(process.cwd(), "node_modules", "@procedures", "mark", "dist", "cli.js"),
70
- path.join(process.cwd(), "node_modules", ".bin", "mark"),
71
- // Check parent directories for monorepo setups
72
- path.join(process.cwd(), "..", "cli", "dist", "cli.js"),
73
- // Direct check in current directory
74
- path.join(process.cwd(), "dist", "cli.js"),
75
- ];
76
-
77
- // Check which candidate exists
78
- for (const candidate of candidates) {
79
- if (fs.existsSync(candidate)) {
80
- return candidate;
81
- }
82
- }
83
-
84
- // Fallback: try mark in PATH (will fail if not installed globally)
85
- return "mark";
86
- }
87
-
88
- export const serverStartProcedure: Procedure<
89
- ServerStartInput,
90
- ServerStartOutput,
91
- { description: string; effects: Effects; args: string[]; shorts: Record<string, string> }
92
- > = createProcedure()
93
- .path(["server", "start"])
94
- .input(serverStartInputSchema)
95
- .output(serverStartOutputSchema)
96
- .meta({
97
- description: "Start CLI server as background daemon",
98
- // The daemon loads the procedure packages of the workspace. The procedure moves server.log
99
- // over server.log.1.
100
- effects: { reads: ["fs", "server"], writes: ["fs", "network", "process", "server"] },
101
- args: [],
102
- shorts: { port: "p", host: "h" },
103
- })
104
- .handler(async (input: ServerStartInput): Promise<ServerStartOutput> => {
105
- const port = input.port ?? 3000;
106
- // Loopback by default; pass host: "0.0.0.0" to expose on the LAN deliberately.
107
- // See documentation/BUGS-2026-07.md (H19).
108
- const host = input.host ?? "127.0.0.1";
109
- const transport = input.transport ?? "http";
110
-
111
- // Check if server is already running on this port
112
- const existing = readLockfileForPort(port);
113
- if (existing && (await checkServer(existing)) === "alive") {
114
- return {
115
- success: false,
116
- message: `Server already running on port ${port} (PID ${existing.pid})`,
117
- };
118
- }
119
-
120
- // Rotate log file
121
- rotateLog();
122
-
123
- // Ensure directories exist
124
- fs.mkdirSync(SERVERS_DIR, { recursive: true });
125
-
126
- // Open log file for writing
127
- const logFd = fs.openSync(LOG_PATH, "a");
128
-
129
- // Find CLI path
130
- const cliPath = findCliPath();
131
-
132
- // Build arguments
133
- const args = ["--server", "--port", String(port), "--host", host];
134
- if (transport !== "http") {
135
- args.push("--transport", transport);
136
- }
137
-
138
- // Determine how to spawn
139
- let spawnCmd: string;
140
- let spawnArgs: string[];
141
-
142
- if (cliPath === "mark" || cliPath.endsWith(".bin/mark") || cliPath.endsWith(".bin\\mark")) {
143
- // Use mark command directly
144
- spawnCmd = cliPath;
145
- spawnArgs = args;
146
- } else {
147
- // Use node to run the JS file
148
- spawnCmd = process.execPath;
149
- spawnArgs = [cliPath, ...args];
150
- }
151
-
152
- // Spawn daemon process
153
- const child = spawn(spawnCmd, spawnArgs, {
154
- detached: true,
155
- stdio: ["ignore", logFd, logFd],
156
- env: { ...process.env },
157
- cwd: process.cwd(),
158
- // No console window on Windows (deep dive CLI-16)
159
- windowsHide: true,
160
- });
161
-
162
- // Unref so parent can exit
163
- child.unref();
164
- fs.closeSync(logFd);
165
-
166
- // Poll for lockfile (server takes ~4-8s to load ecosystem)
167
- const maxWait = 15000;
168
- const pollInterval = 500;
169
- const startTime = Date.now();
170
-
171
- while (Date.now() - startTime < maxWait) {
172
- await new Promise((resolve) => setTimeout(resolve, pollInterval));
173
-
174
- // Check if child died
175
- if (child.pid && !isProcessAlive(child.pid)) {
176
- break;
177
- }
178
-
179
- // Check for lockfile
180
- const lockfile = readLockfileForPort(port);
181
- if (lockfile && (await checkServer(lockfile)) === "alive") {
182
- return {
183
- success: true,
184
- pid: lockfile.pid,
185
- port: lockfile.port,
186
- endpoint: lockfile.endpoint,
187
- logFile: LOG_PATH,
188
- message: `Server started (PID ${lockfile.pid})`,
189
- };
190
- }
191
- }
192
-
193
- // Timed out waiting for lockfile, but child may still be alive
194
- if (child.pid && isProcessAlive(child.pid)) {
195
- return {
196
- success: true,
197
- pid: child.pid,
198
- port,
199
- endpoint: `http://${host === "0.0.0.0" ? "localhost" : host}:${port}/api`,
200
- logFile: LOG_PATH,
201
- message: `Server starting (PID ${child.pid}, still loading...)`,
202
- };
203
- }
204
-
205
- return {
206
- success: false,
207
- message: `Failed to start server. Check ${LOG_PATH} for details.`,
208
- };
209
- })
210
- .build();
211
-
212
- export type { ServerStartInput, ServerStartOutput };
@@ -1,141 +0,0 @@
1
- /**
2
- * Procedure: server.status
3
- * Gets the current CLI server status
4
- */
5
-
6
- import * as path from "node:path";
7
- import * as os from "node:os";
8
- import { createProcedure, type Effects, type Procedure } from "@procedures/client";
9
- import { wrapSchema } from "../schema.js";
10
- import { ServerStatusOutputSchema } from "../output-schemas.js";
11
- import { serverStatusInputSchema } from "../input-schemas.js";
12
- import { checkServer, readAllLockfiles, readLockfileForPort, removeLockfileForPort, type LockfileData } from "../lockfile.js";
13
- import type { ServerStatusInput, ServerStatusOutput } from "../types.js";
14
-
15
- const MARK_DIR = path.join(os.homedir(), ".mark");
16
- const LOG_PATH = path.join(MARK_DIR, "server.log");
17
-
18
- const serverStatusOutputSchema = wrapSchema<ServerStatusOutput>(ServerStatusOutputSchema);
19
-
20
-
21
-
22
-
23
-
24
- /**
25
- * Format uptime as human-readable string
26
- */
27
- function formatUptime(startedAt: string): string {
28
- const start = new Date(startedAt).getTime();
29
- const now = Date.now();
30
- const diff = Math.floor((now - start) / 1000);
31
-
32
- if (diff < 60) {
33
- return `${diff}s`;
34
- }
35
- if (diff < 3600) {
36
- const mins = Math.floor(diff / 60);
37
- const secs = diff % 60;
38
- return `${mins}m ${secs}s`;
39
- }
40
- if (diff < 86400) {
41
- const hours = Math.floor(diff / 3600);
42
- const mins = Math.floor((diff % 3600) / 60);
43
- return `${hours}h ${mins}m`;
44
- }
45
- const days = Math.floor(diff / 86400);
46
- const hours = Math.floor((diff % 86400) / 3600);
47
- return `${days}d ${hours}h`;
48
- }
49
-
50
- export const serverStatusProcedure: Procedure<
51
- ServerStatusInput,
52
- ServerStatusOutput,
53
- { description: string; effects: Effects; output: string }
54
- > = createProcedure()
55
- .path(["server", "status"])
56
- .input(serverStatusInputSchema)
57
- .output(serverStatusOutputSchema)
58
- .meta({
59
- description: "Get CLI server status",
60
- // It deletes the lockfile of a server that does not answer
61
- effects: { reads: ["fs", "server"], writes: ["fs"], idempotent: true },
62
- args: [],
63
- shorts: { port: "p" },
64
- output: "text",
65
- })
66
- .handler(async (input: ServerStatusInput): Promise<ServerStatusOutput> => {
67
- // Get target servers
68
- let targets: LockfileData[];
69
- if (input.port !== undefined) {
70
- const lockfile = readLockfileForPort(input.port);
71
- targets = lockfile ? [lockfile] : [];
72
- } else {
73
- targets = readAllLockfiles();
74
- }
75
-
76
- if (targets.length === 0) {
77
- return {
78
- running: false,
79
- message: input.port
80
- ? `No server found on port ${input.port}`
81
- : "No servers running",
82
- };
83
- }
84
-
85
- // For single server, return detailed info
86
- if (targets.length === 1) {
87
- const lockfile = targets[0]!;
88
- const { pid, port, endpoint, startedAt, transport } = lockfile;
89
-
90
- if ((await checkServer(lockfile)) !== "alive") {
91
- removeLockfileForPort(port);
92
- return {
93
- running: false,
94
- message: "Server is not running (cleaned up stale lockfile)",
95
- };
96
- }
97
-
98
- const uptime = formatUptime(startedAt);
99
- return {
100
- running: true,
101
- pid,
102
- port,
103
- endpoint,
104
- transport,
105
- startedAt,
106
- uptime,
107
- logFile: LOG_PATH,
108
- message: `Server running (PID ${pid}, port ${port}, uptime ${uptime})`,
109
- };
110
- }
111
-
112
- // Multiple servers - summarize
113
- const lines: string[] = [];
114
- let anyRunning = false;
115
-
116
- for (const lockfile of targets) {
117
- const { pid, port, endpoint, startedAt } = lockfile;
118
- if ((await checkServer(lockfile)) !== "alive") {
119
- removeLockfileForPort(port);
120
- continue;
121
- }
122
- anyRunning = true;
123
- const uptime = formatUptime(startedAt);
124
- lines.push(` Port ${port}: PID ${pid}, uptime ${uptime}, ${endpoint}`);
125
- }
126
-
127
- if (!anyRunning) {
128
- return {
129
- running: false,
130
- message: "No servers running (cleaned up stale lockfiles)",
131
- };
132
- }
133
-
134
- return {
135
- running: true,
136
- message: `${lines.length} server(s) running:\n${lines.join("\n")}`,
137
- };
138
- })
139
- .build();
140
-
141
- export type { ServerStatusInput, ServerStatusOutput };