@harness-sdk/harnessd 0.0.2 → 0.1.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.
- package/README.md +20 -8
- package/dist/channel.d.ts +15 -10
- package/dist/channel.d.ts.map +1 -1
- package/dist/channel.js +112 -157
- package/dist/channel.js.map +1 -1
- package/dist/claude.d.ts +9 -4
- package/dist/claude.d.ts.map +1 -1
- package/dist/claude.js +53 -29
- package/dist/claude.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +58 -27
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +33 -10
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +94 -43
- package/dist/config.js.map +1 -1
- package/dist/daemon.d.ts +12 -0
- package/dist/daemon.d.ts.map +1 -1
- package/dist/daemon.js +66 -4
- package/dist/daemon.js.map +1 -1
- package/package.json +4 -10
- package/src/channel.ts +151 -172
- package/src/claude.ts +55 -28
- package/src/cli.ts +98 -48
- package/src/config.ts +155 -57
- package/src/daemon.ts +86 -3
package/src/cli.ts
CHANGED
|
@@ -12,9 +12,15 @@ import { createConnection } from "node:net";
|
|
|
12
12
|
import { userInfo } from "node:os";
|
|
13
13
|
import { delimiter, dirname } from "node:path";
|
|
14
14
|
import { createInterface } from "node:readline";
|
|
15
|
-
import {
|
|
15
|
+
import { processCliIo, runCli } from "toolport/cli";
|
|
16
16
|
import {
|
|
17
|
-
|
|
17
|
+
readStatus,
|
|
18
|
+
serveChannel,
|
|
19
|
+
serverCli,
|
|
20
|
+
subscriptionLabel,
|
|
21
|
+
} from "./channel";
|
|
22
|
+
import {
|
|
23
|
+
defaultConfig,
|
|
18
24
|
ownFolder,
|
|
19
25
|
pathsOf,
|
|
20
26
|
readConfig,
|
|
@@ -43,10 +49,11 @@ usage: harnessd <command>
|
|
|
43
49
|
|
|
44
50
|
service install Start harnessd now and at every login; with sudo, at every boot
|
|
45
51
|
service uninstall Stop harnessd and remove its service
|
|
46
|
-
status [--json] What runs, which conversation,
|
|
52
|
+
status [--json] What runs, which conversation, the channel's events, and the log's last lines
|
|
47
53
|
start Start Claude Code after a stop
|
|
48
54
|
stop Stop Claude Code; harnessd stays up, and Claude Code stays stopped until start
|
|
49
55
|
attach Open Claude Code's terminal; Ctrl-\\ detaches
|
|
56
|
+
tools <server> ... Run a server's tools, as the agent's <server> command does
|
|
50
57
|
--version Print harnessd's version
|
|
51
58
|
|
|
52
59
|
service install takes:
|
|
@@ -304,7 +311,7 @@ const install = async (flags: ServiceFlags, bin: string, io: Io) => {
|
|
|
304
311
|
if (!existsSync(paths.config))
|
|
305
312
|
writeFileSync(
|
|
306
313
|
paths.config,
|
|
307
|
-
`${JSON.stringify(
|
|
314
|
+
`${JSON.stringify(defaultConfig(), null, 2)}\n`,
|
|
308
315
|
);
|
|
309
316
|
chmodSync(paths.config, 0o600);
|
|
310
317
|
// the user's own file before a boot service's manager, as root, makes it: a later login item has to append to it
|
|
@@ -360,40 +367,51 @@ const ago = (iso: string) => {
|
|
|
360
367
|
: `${Math.round(s / 86_400)}d`;
|
|
361
368
|
};
|
|
362
369
|
|
|
363
|
-
/**
|
|
370
|
+
/** The channel as of the Claude Code that runs now (what an earlier run heard says nothing about this one); none without subscriptions. */
|
|
364
371
|
const channelReport = (
|
|
365
372
|
paths: Harnessd.Paths,
|
|
366
373
|
config: Harnessd.Config | undefined,
|
|
367
374
|
running: Daemon.Status | undefined,
|
|
368
|
-
) =>
|
|
375
|
+
) => {
|
|
376
|
+
const subscribe = config?.channel.subscribe ?? [];
|
|
377
|
+
if (subscribe.length === 0) return undefined;
|
|
378
|
+
const status = readStatus(paths.channel);
|
|
379
|
+
const since =
|
|
380
|
+
running?.state === "running" || running?.state === "starting"
|
|
381
|
+
? running.startedAt
|
|
382
|
+
: undefined;
|
|
383
|
+
const now = (at: string | undefined) =>
|
|
384
|
+
since !== undefined && at !== undefined && at >= since;
|
|
385
|
+
const events =
|
|
386
|
+
since === undefined
|
|
387
|
+
? "off (Claude Code isn't running)"
|
|
388
|
+
: running?.channelsRefused
|
|
389
|
+
? "refused"
|
|
390
|
+
: now(status.heardAt)
|
|
391
|
+
? "arriving"
|
|
392
|
+
: now(status.pushedAt)
|
|
393
|
+
? "sent, not in the conversation yet"
|
|
394
|
+
: "none yet";
|
|
395
|
+
return {
|
|
396
|
+
events,
|
|
397
|
+
subscribe: subscribe.map(subscriptionLabel),
|
|
398
|
+
...(status.pushedAt && { pushedAt: status.pushedAt }),
|
|
399
|
+
...(status.heardAt && { heardAt: status.heardAt }),
|
|
400
|
+
};
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
/** Each server: its URL, and the events the channel takes from it. */
|
|
404
|
+
const serverReport = (config: Harnessd.Config | undefined) =>
|
|
369
405
|
Object.fromEntries(
|
|
370
|
-
Object.entries(config?.
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
since === undefined
|
|
380
|
-
? "off (Claude Code isn't running)"
|
|
381
|
-
: running?.channelsRefused
|
|
382
|
-
? "refused"
|
|
383
|
-
: now(status.heardAt)
|
|
384
|
-
? "arriving"
|
|
385
|
-
: now(status.pushedAt)
|
|
386
|
-
? "sent, not in the conversation yet"
|
|
387
|
-
: "none yet";
|
|
388
|
-
return [
|
|
389
|
-
name,
|
|
390
|
-
{
|
|
391
|
-
url: channel.url,
|
|
392
|
-
events,
|
|
393
|
-
...status,
|
|
394
|
-
},
|
|
395
|
-
];
|
|
396
|
-
}),
|
|
406
|
+
Object.entries(config?.servers ?? {}).map(([name, server]) => [
|
|
407
|
+
name,
|
|
408
|
+
{
|
|
409
|
+
url: server.url,
|
|
410
|
+
events: config!.channel.subscribe
|
|
411
|
+
.filter((subscription) => subscription.server === name)
|
|
412
|
+
.map((subscription) => subscription.event),
|
|
413
|
+
},
|
|
414
|
+
]),
|
|
397
415
|
);
|
|
398
416
|
|
|
399
417
|
const status = async (paths: Harnessd.Paths, json: boolean, io: Io) => {
|
|
@@ -405,7 +423,8 @@ const status = async (paths: Harnessd.Paths, json: boolean, io: Io) => {
|
|
|
405
423
|
} catch (error) {
|
|
406
424
|
configError = error instanceof Error ? error.message : String(error);
|
|
407
425
|
}
|
|
408
|
-
const
|
|
426
|
+
const channel = channelReport(paths, config, s);
|
|
427
|
+
const servers = serverReport(config);
|
|
409
428
|
// without a harnessd to ask, the service's own output says why
|
|
410
429
|
const log = s || !existsSync(paths.serviceLog) ? paths.log : paths.serviceLog;
|
|
411
430
|
const logTail = tail(log, 10);
|
|
@@ -418,7 +437,8 @@ const status = async (paths: Harnessd.Paths, json: boolean, io: Io) => {
|
|
|
418
437
|
config: paths.config,
|
|
419
438
|
...(s && { status: s }),
|
|
420
439
|
...(configError && { configError }),
|
|
421
|
-
|
|
440
|
+
servers,
|
|
441
|
+
...(channel && { channel }),
|
|
422
442
|
log,
|
|
423
443
|
logTail,
|
|
424
444
|
}),
|
|
@@ -430,13 +450,19 @@ const status = async (paths: Harnessd.Paths, json: boolean, io: Io) => {
|
|
|
430
450
|
const since = s.startedAt ? ` for ${ago(s.startedAt)}` : "";
|
|
431
451
|
const what =
|
|
432
452
|
s.state === "running"
|
|
433
|
-
? `Claude Code running${since} (pid ${s.pid})`
|
|
453
|
+
? `Claude Code${s.claudeVersion ? ` ${s.claudeVersion}` : ""} running${since} (pid ${s.pid})`
|
|
434
454
|
: s.state === "waiting"
|
|
435
455
|
? `Claude Code starting again${s.nextStartAt ? ` in ${Math.max(0, Math.round((Date.parse(s.nextStartAt) - Date.now()) / 1000))}s` : ""}`
|
|
436
456
|
: s.state === "stopped"
|
|
437
457
|
? "Claude Code stopped (harnessd start starts it)"
|
|
438
458
|
: "Claude Code starting";
|
|
439
459
|
io.out(`harnessd ${s.daemonPid}: ${what}`);
|
|
460
|
+
if (s.update)
|
|
461
|
+
io.out(
|
|
462
|
+
s.update.at
|
|
463
|
+
? `updated from Claude Code ${s.update.from} to ${s.update.to} ${ago(s.update.at)} ago`
|
|
464
|
+
: `Claude Code ${s.update.to} is installed; harnessd restarts into it once the agent is idle`,
|
|
465
|
+
);
|
|
440
466
|
if (s.account) io.out(`account ${s.account}`);
|
|
441
467
|
for (const [name, stop] of Object.entries(s.accountsStopped ?? {}))
|
|
442
468
|
io.out(
|
|
@@ -454,10 +480,15 @@ const status = async (paths: Harnessd.Paths, json: boolean, io: Io) => {
|
|
|
454
480
|
);
|
|
455
481
|
}
|
|
456
482
|
if (configError) io.out(configError);
|
|
457
|
-
for (const [name,
|
|
458
|
-
io.out(
|
|
459
|
-
|
|
460
|
-
|
|
483
|
+
for (const [name, server] of Object.entries(servers))
|
|
484
|
+
io.out(
|
|
485
|
+
`server ${name}: the ${name} command, ${server.events.length > 0 ? `events ${server.events.join(", ")}` : "no events subscribed"} (${server.url})`,
|
|
486
|
+
);
|
|
487
|
+
if (channel) {
|
|
488
|
+
io.out(`channel: events ${channel.events}`);
|
|
489
|
+
for (const label of channel.subscribe) io.out(` ${label}`);
|
|
490
|
+
}
|
|
491
|
+
if (s?.channelsRefused && channel) io.out(CHANNELS_REFUSED);
|
|
461
492
|
io.out(`log ${log}`);
|
|
462
493
|
for (const line of logTail) io.out(` ${line}`);
|
|
463
494
|
return code;
|
|
@@ -533,7 +564,10 @@ export const runHarnessd = async (
|
|
|
533
564
|
if (
|
|
534
565
|
first === undefined ||
|
|
535
566
|
first === "help" ||
|
|
536
|
-
|
|
567
|
+
// past a server's name, --help is that server's
|
|
568
|
+
argv
|
|
569
|
+
.slice(0, first === "tools" ? 2 : undefined)
|
|
570
|
+
.some((arg) => arg === "--help" || arg === "-h")
|
|
537
571
|
) {
|
|
538
572
|
io.out(USAGE);
|
|
539
573
|
return 0;
|
|
@@ -615,17 +649,33 @@ const run = async (
|
|
|
615
649
|
io.err(answer ? String(answer.message) : notRunning(paths));
|
|
616
650
|
return 1;
|
|
617
651
|
}
|
|
618
|
-
case "
|
|
619
|
-
|
|
620
|
-
const
|
|
621
|
-
if (
|
|
652
|
+
case "idle": {
|
|
653
|
+
takes(first, rest);
|
|
654
|
+
const answer = await ask(paths.socket, { command: "idle" });
|
|
655
|
+
if (answer?.ok) return 0;
|
|
656
|
+
io.err(answer ? String(answer.message) : notRunning(paths));
|
|
657
|
+
return 1;
|
|
658
|
+
}
|
|
659
|
+
case "channel":
|
|
660
|
+
takes(first, rest);
|
|
622
661
|
await serveChannel({
|
|
623
|
-
|
|
624
|
-
channel,
|
|
662
|
+
config: readConfig(paths),
|
|
625
663
|
paths,
|
|
626
|
-
log: logTo(paths.log,
|
|
664
|
+
log: logTo(paths.log, "channel: "),
|
|
627
665
|
});
|
|
628
666
|
return 0;
|
|
667
|
+
case "tools": {
|
|
668
|
+
const [name, ...args] = rest;
|
|
669
|
+
if (name === undefined)
|
|
670
|
+
throw usageError("harnessd tools takes a server's name.");
|
|
671
|
+
const { servers } = readConfig(paths);
|
|
672
|
+
const server = Object.hasOwn(servers, name) ? servers[name] : undefined;
|
|
673
|
+
if (!server) throw new Error(`No server "${name}" in ${paths.config}`);
|
|
674
|
+
return runCli(serverCli(paths, name, server, process.env), {
|
|
675
|
+
...(await processCliIo(args)),
|
|
676
|
+
stdout: io.out,
|
|
677
|
+
stderr: io.err,
|
|
678
|
+
});
|
|
629
679
|
}
|
|
630
680
|
default:
|
|
631
681
|
throw usageError(`Unknown command ${first}.`);
|
package/src/config.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { isAbsolute, join, resolve } from "node:path";
|
|
|
6
6
|
export namespace Harnessd {
|
|
7
7
|
/** `config.json`, read at every start of Claude Code. */
|
|
8
8
|
export type Config = {
|
|
9
|
+
/** The day the file was written, `YYYY-MM-DD`: harnessd behaves as it did on that day. */
|
|
10
|
+
readonly compatibilityDate: string;
|
|
9
11
|
/** The agent's folder: Claude Code runs here and reads its CLAUDE.md. */
|
|
10
12
|
readonly folder: string;
|
|
11
13
|
/** More arguments for `claude`. */
|
|
@@ -14,8 +16,10 @@ export namespace Harnessd {
|
|
|
14
16
|
readonly env: Readonly<Record<string, string>>;
|
|
15
17
|
/** A shell command run before each start; its stdout is JSON `{ env?, args? }` for that start. */
|
|
16
18
|
readonly prestart?: string;
|
|
17
|
-
/** MCP servers
|
|
18
|
-
readonly
|
|
19
|
+
/** MCP servers by name: each one's tools are a command of that name for the agent. */
|
|
20
|
+
readonly servers: Readonly<Record<string, Server>>;
|
|
21
|
+
/** The one channel: the server events that reach the agent. */
|
|
22
|
+
readonly channel: Channel;
|
|
19
23
|
/** Claude accounts by name, in the order harnessd tries them: when one hits its usage limit or its sign-in fails, Claude Code goes on with the next. */
|
|
20
24
|
readonly accounts: Readonly<Record<string, Account>>;
|
|
21
25
|
};
|
|
@@ -26,16 +30,26 @@ export namespace Harnessd {
|
|
|
26
30
|
};
|
|
27
31
|
|
|
28
32
|
/** A Streamable HTTP MCP server; `${VAR}` in a header reads the environment. */
|
|
29
|
-
export type
|
|
33
|
+
export type Server = {
|
|
30
34
|
readonly url: string;
|
|
31
35
|
readonly headers: Readonly<Record<string, string>>;
|
|
32
|
-
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type Channel = {
|
|
39
|
+
/** The events that come in, each with its own arguments; nothing else does. */
|
|
40
|
+
readonly subscribe: readonly Subscription[];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** One event type of one server, such as the board to follow for `task.handed`. */
|
|
44
|
+
export type Subscription = {
|
|
45
|
+
readonly server: string;
|
|
46
|
+
readonly event: string;
|
|
33
47
|
readonly arguments: Readonly<Record<string, string | number | boolean>>;
|
|
34
48
|
};
|
|
35
49
|
|
|
36
50
|
/** Where one harnessd keeps its files. */
|
|
37
51
|
export type Paths = {
|
|
38
|
-
/** The folder harnessd writes to: its state, log and
|
|
52
|
+
/** The folder harnessd writes to: its state, log and where the channel's events resume. */
|
|
39
53
|
readonly home: string;
|
|
40
54
|
readonly config: string;
|
|
41
55
|
readonly state: string;
|
|
@@ -44,7 +58,9 @@ export namespace Harnessd {
|
|
|
44
58
|
readonly serviceLog: string;
|
|
45
59
|
readonly socket: string;
|
|
46
60
|
readonly plugin: string;
|
|
47
|
-
|
|
61
|
+
/** The servers' commands, first on Claude Code's PATH. */
|
|
62
|
+
readonly bin: string;
|
|
63
|
+
readonly channel: string;
|
|
48
64
|
readonly label: string;
|
|
49
65
|
/** The environment that gives another process these same paths. */
|
|
50
66
|
readonly env: Readonly<Record<string, string>>;
|
|
@@ -53,8 +69,7 @@ export namespace Harnessd {
|
|
|
53
69
|
|
|
54
70
|
const LOOPBACK = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
55
71
|
const NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
56
|
-
|
|
57
|
-
const RESERVED = new Set(["stream", "login", "help"]);
|
|
72
|
+
const DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
58
73
|
// macOS caps a socket path at 104 bytes
|
|
59
74
|
const SOCKET_MAX = 103;
|
|
60
75
|
|
|
@@ -64,7 +79,7 @@ const tooLong = (socket: string) => Buffer.byteLength(socket) > SOCKET_MAX;
|
|
|
64
79
|
|
|
65
80
|
// The XDG base directories: config.json in $XDG_CONFIG_HOME/harnessd (~/.config), everything harnessd writes in
|
|
66
81
|
// $XDG_STATE_HOME/harnessd (~/.local/state). HARNESSD_HOME puts both in one folder. There is no --config flag:
|
|
67
|
-
// the service
|
|
82
|
+
// the service, the channel's process and the servers' commands must find the same file, and these variables already reach them.
|
|
68
83
|
export const pathsOf = (
|
|
69
84
|
env: NodeJS.ProcessEnv = process.env,
|
|
70
85
|
): Harnessd.Paths => {
|
|
@@ -96,7 +111,8 @@ export const pathsOf = (
|
|
|
96
111
|
serviceLog: join(home, "service.log"),
|
|
97
112
|
socket,
|
|
98
113
|
plugin: join(home, "plugin"),
|
|
99
|
-
|
|
114
|
+
bin: join(home, "plugin", "bin"),
|
|
115
|
+
channel: join(home, "channel.json"),
|
|
100
116
|
label:
|
|
101
117
|
!one && stateHome === join(homedir(), ".local", "state")
|
|
102
118
|
? "dev.harnessd"
|
|
@@ -121,7 +137,19 @@ export const ownFolder = (dir: string) => {
|
|
|
121
137
|
chmodSync(dir, 0o700);
|
|
122
138
|
};
|
|
123
139
|
|
|
124
|
-
|
|
140
|
+
/** The local date, as `compatibilityDate` takes it. */
|
|
141
|
+
export const today = (now = new Date()) =>
|
|
142
|
+
[now.getFullYear(), now.getMonth() + 1, now.getDate()]
|
|
143
|
+
.map((part) => String(part).padStart(2, "0"))
|
|
144
|
+
.join("-");
|
|
145
|
+
|
|
146
|
+
/** The file `harnessd service install` writes when there is none. */
|
|
147
|
+
export const defaultConfig = () => ({
|
|
148
|
+
compatibilityDate: today(),
|
|
149
|
+
folder: "~/agent",
|
|
150
|
+
servers: {},
|
|
151
|
+
channel: { subscribe: [] },
|
|
152
|
+
});
|
|
125
153
|
|
|
126
154
|
const fail = (message: string): never => {
|
|
127
155
|
throw new Error(`config.json: ${message}`);
|
|
@@ -157,39 +185,32 @@ const expandHome = (path: string) =>
|
|
|
157
185
|
? join(homedir(), path.slice(2))
|
|
158
186
|
: path;
|
|
159
187
|
|
|
160
|
-
|
|
161
|
-
if (
|
|
162
|
-
const value = raw as Record<string, unknown>;
|
|
163
|
-
only(
|
|
164
|
-
value,
|
|
165
|
-
["folder", "args", "env", "prestart", "channels", "accounts"],
|
|
166
|
-
"it",
|
|
167
|
-
);
|
|
168
|
-
if (typeof value.folder !== "string" || value.folder === "")
|
|
169
|
-
fail("folder must be a path");
|
|
170
|
-
const args = value.args ?? [];
|
|
171
|
-
if (!Array.isArray(args) || args.some((arg) => typeof arg !== "string"))
|
|
172
|
-
fail("args must be a list of strings");
|
|
188
|
+
const scalars = (value: unknown, what: string) => {
|
|
189
|
+
if (value === undefined) return {};
|
|
173
190
|
if (
|
|
174
|
-
value
|
|
175
|
-
(
|
|
191
|
+
!isRecord(value) ||
|
|
192
|
+
Object.values(value).some(
|
|
193
|
+
(v) => !["string", "number", "boolean"].includes(typeof v),
|
|
194
|
+
)
|
|
176
195
|
)
|
|
177
|
-
fail(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
196
|
+
fail(`${what} must map names to strings, numbers or booleans`);
|
|
197
|
+
return value as Record<string, string | number | boolean>;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const parseServers = (value: unknown) => {
|
|
201
|
+
const servers: Record<string, Harnessd.Server> = {};
|
|
202
|
+
if (value === undefined) return servers;
|
|
203
|
+
if (!isRecord(value)) fail("servers must map names to { url, headers }");
|
|
204
|
+
for (const [name, server] of Object.entries(
|
|
205
|
+
value as Record<string, unknown>,
|
|
184
206
|
)) {
|
|
185
|
-
if (!NAME.test(name)
|
|
207
|
+
if (!NAME.test(name))
|
|
186
208
|
fail(
|
|
187
|
-
`
|
|
209
|
+
`server "${name}" needs a name of lowercase letters, digits and dashes`,
|
|
188
210
|
);
|
|
189
|
-
if (!isRecord(
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
only(entry, ["url", "headers", "arguments"], `channels.${name}`);
|
|
211
|
+
if (!isRecord(server)) fail(`servers.${name} must be { url, headers }`);
|
|
212
|
+
const entry = server as Record<string, unknown>;
|
|
213
|
+
only(entry, ["url", "headers"], `servers.${name}`);
|
|
193
214
|
let url: URL | undefined;
|
|
194
215
|
try {
|
|
195
216
|
url = new URL(String(entry.url));
|
|
@@ -200,24 +221,99 @@ export const parseConfig = (raw: unknown): Harnessd.Config => {
|
|
|
200
221
|
!(url.protocol === "http:" && LOOPBACK.has(url.hostname)))
|
|
201
222
|
)
|
|
202
223
|
fail(
|
|
203
|
-
`
|
|
224
|
+
`servers.${name}.url must be an https URL (http only on this computer: localhost, 127.0.0.1, [::1])`,
|
|
204
225
|
);
|
|
205
|
-
|
|
206
|
-
if (
|
|
207
|
-
!isRecord(args) ||
|
|
208
|
-
Object.values(args).some(
|
|
209
|
-
(v) => !["string", "number", "boolean"].includes(typeof v),
|
|
210
|
-
)
|
|
211
|
-
)
|
|
212
|
-
fail(
|
|
213
|
-
`channels.${name}.arguments must map names to strings, numbers or booleans`,
|
|
214
|
-
);
|
|
215
|
-
channels[name] = {
|
|
226
|
+
servers[name] = {
|
|
216
227
|
url: url!.href,
|
|
217
|
-
headers: strings(entry.headers, `
|
|
218
|
-
arguments: args as Record<string, string | number | boolean>,
|
|
228
|
+
headers: strings(entry.headers, `servers.${name}.headers`),
|
|
219
229
|
};
|
|
220
230
|
}
|
|
231
|
+
return servers;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const parseChannel = (
|
|
235
|
+
value: unknown,
|
|
236
|
+
servers: Readonly<Record<string, Harnessd.Server>>,
|
|
237
|
+
): Harnessd.Channel => {
|
|
238
|
+
if (value === undefined) return { subscribe: [] };
|
|
239
|
+
if (!isRecord(value)) fail("channel must be { subscribe }");
|
|
240
|
+
const entry = value as Record<string, unknown>;
|
|
241
|
+
only(entry, ["subscribe"], "channel");
|
|
242
|
+
const listed = entry.subscribe ?? [];
|
|
243
|
+
if (!Array.isArray(listed))
|
|
244
|
+
fail("channel.subscribe must be a list of { server, event, arguments }");
|
|
245
|
+
const seen = new Set<string>();
|
|
246
|
+
const subscribe = (listed as unknown[]).map((item, i) => {
|
|
247
|
+
const what = `channel.subscribe[${i}]`;
|
|
248
|
+
if (!isRecord(item)) fail(`${what} must be { server, event, arguments }`);
|
|
249
|
+
const one = item as Record<string, unknown>;
|
|
250
|
+
only(one, ["server", "event", "arguments"], what);
|
|
251
|
+
if (typeof one.server !== "string" || !Object.hasOwn(servers, one.server))
|
|
252
|
+
fail(`${what}.server must name one of servers`);
|
|
253
|
+
if (typeof one.event !== "string" || one.event === "")
|
|
254
|
+
fail(`${what}.event must name one of the server's events`);
|
|
255
|
+
const subscription: Harnessd.Subscription = {
|
|
256
|
+
server: one.server as string,
|
|
257
|
+
event: one.event as string,
|
|
258
|
+
arguments: scalars(one.arguments, `${what}.arguments`),
|
|
259
|
+
};
|
|
260
|
+
const key = subscriptionKey(subscription);
|
|
261
|
+
if (seen.has(key)) fail(`${what} repeats an earlier subscription`);
|
|
262
|
+
seen.add(key);
|
|
263
|
+
return subscription;
|
|
264
|
+
});
|
|
265
|
+
return { subscribe };
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/** What tells one subscription from another: its server, event and arguments. */
|
|
269
|
+
export const subscriptionKey = (subscription: Harnessd.Subscription) =>
|
|
270
|
+
JSON.stringify([
|
|
271
|
+
subscription.server,
|
|
272
|
+
subscription.event,
|
|
273
|
+
Object.entries(subscription.arguments).sort(([a], [b]) =>
|
|
274
|
+
a < b ? -1 : a > b ? 1 : 0,
|
|
275
|
+
),
|
|
276
|
+
]);
|
|
277
|
+
|
|
278
|
+
export const parseConfig = (raw: unknown): Harnessd.Config => {
|
|
279
|
+
if (!isRecord(raw)) fail("must be a JSON object");
|
|
280
|
+
const value = raw as Record<string, unknown>;
|
|
281
|
+
only(
|
|
282
|
+
value,
|
|
283
|
+
[
|
|
284
|
+
"compatibilityDate",
|
|
285
|
+
"folder",
|
|
286
|
+
"args",
|
|
287
|
+
"env",
|
|
288
|
+
"prestart",
|
|
289
|
+
"servers",
|
|
290
|
+
"channel",
|
|
291
|
+
"accounts",
|
|
292
|
+
],
|
|
293
|
+
"it",
|
|
294
|
+
);
|
|
295
|
+
const date = value.compatibilityDate;
|
|
296
|
+
if (
|
|
297
|
+
typeof date !== "string" ||
|
|
298
|
+
!DATE.test(date) ||
|
|
299
|
+
// Date rolls a day past the month's end over into the next month
|
|
300
|
+
new Date(Date.parse(date) || 0).toISOString().slice(0, 10) !== date
|
|
301
|
+
)
|
|
302
|
+
fail(
|
|
303
|
+
"compatibilityDate must be a date, YYYY-MM-DD: the day the file was written",
|
|
304
|
+
);
|
|
305
|
+
if (typeof value.folder !== "string" || value.folder === "")
|
|
306
|
+
fail("folder must be a path");
|
|
307
|
+
const args = value.args ?? [];
|
|
308
|
+
if (!Array.isArray(args) || args.some((arg) => typeof arg !== "string"))
|
|
309
|
+
fail("args must be a list of strings");
|
|
310
|
+
if (
|
|
311
|
+
value.prestart !== undefined &&
|
|
312
|
+
(typeof value.prestart !== "string" || value.prestart === "")
|
|
313
|
+
)
|
|
314
|
+
fail("prestart must be a shell command");
|
|
315
|
+
const servers = parseServers(value.servers);
|
|
316
|
+
const channel = parseChannel(value.channel, servers);
|
|
221
317
|
const accounts: Record<string, Harnessd.Account> = {};
|
|
222
318
|
const named = value.accounts === undefined ? {} : value.accounts;
|
|
223
319
|
if (!isRecord(named)) fail("accounts must map names to { env }");
|
|
@@ -240,11 +336,13 @@ export const parseConfig = (raw: unknown): Harnessd.Config => {
|
|
|
240
336
|
accounts[name] = { env };
|
|
241
337
|
}
|
|
242
338
|
return {
|
|
339
|
+
compatibilityDate: date as string,
|
|
243
340
|
folder: resolve(expandHome(value.folder as string)),
|
|
244
341
|
args: args as string[],
|
|
245
342
|
env: strings(value.env, "env"),
|
|
246
343
|
...(value.prestart !== undefined && { prestart: value.prestart as string }),
|
|
247
|
-
|
|
344
|
+
servers,
|
|
345
|
+
channel,
|
|
248
346
|
accounts,
|
|
249
347
|
};
|
|
250
348
|
};
|
|
@@ -263,13 +361,13 @@ export const readConfig = (paths: Harnessd.Paths): Harnessd.Config => {
|
|
|
263
361
|
return parseConfig(raw);
|
|
264
362
|
};
|
|
265
363
|
|
|
266
|
-
/** A
|
|
364
|
+
/** A server's headers with each `${VAR}` read from `env`; a variable that isn't set throws. */
|
|
267
365
|
export const headersOf = (
|
|
268
|
-
|
|
366
|
+
server: Harnessd.Server,
|
|
269
367
|
env: NodeJS.ProcessEnv,
|
|
270
368
|
): Record<string, string> =>
|
|
271
369
|
Object.fromEntries(
|
|
272
|
-
Object.entries(
|
|
370
|
+
Object.entries(server.headers).map(([key, value]) => [
|
|
273
371
|
key,
|
|
274
372
|
value.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (_, name: string) => {
|
|
275
373
|
const found = env[name];
|