@fro.bot/space-bus 0.12.0 → 0.13.1
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 +7 -1
- package/dist/cli.js +457 -59
- package/dist/config.d.ts +53 -0
- package/dist/config.js +415 -13
- package/dist/format.d.ts +13 -0
- package/dist/format.js +4 -0
- package/dist/index.js +1201 -195
- package/dist/mcp.d.ts +3 -1
- package/dist/mcp.js +1235 -117
- package/dist/registry-entry.js +159 -135
- package/dist/registry.d.ts +1 -0
- package/dist/server.js +418 -20
- package/dist/tools/bus_registry.d.ts +123 -0
- package/dist/tools/shared.d.ts +31 -1
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -3,9 +3,9 @@ import { createRequire } from "node:module";
|
|
|
3
3
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
4
4
|
|
|
5
5
|
// src/config.ts
|
|
6
|
-
import { existsSync, readFileSync as
|
|
7
|
-
import { homedir as
|
|
8
|
-
import { join as
|
|
6
|
+
import { existsSync, readFileSync as readFileSync3, realpathSync as realpathSync2 } from "node:fs";
|
|
7
|
+
import { homedir as homedir3 } from "node:os";
|
|
8
|
+
import { join as join3 } from "node:path";
|
|
9
9
|
import { z as z3 } from "zod";
|
|
10
10
|
|
|
11
11
|
// src/contract.ts
|
|
@@ -434,6 +434,380 @@ function removeProvisional(rosterPath) {
|
|
|
434
434
|
} catch {}
|
|
435
435
|
}
|
|
436
436
|
|
|
437
|
+
// src/registry.ts
|
|
438
|
+
import { randomBytes as randomBytes2 } from "node:crypto";
|
|
439
|
+
import {
|
|
440
|
+
closeSync as closeSync2,
|
|
441
|
+
constants as fsConstants2,
|
|
442
|
+
fstatSync,
|
|
443
|
+
lstatSync,
|
|
444
|
+
mkdirSync as mkdirSync2,
|
|
445
|
+
openSync as openSync2,
|
|
446
|
+
readFileSync as readFileSync2,
|
|
447
|
+
realpathSync,
|
|
448
|
+
renameSync as renameSync2,
|
|
449
|
+
statSync as statSync2,
|
|
450
|
+
unlinkSync as unlinkSync2,
|
|
451
|
+
writeSync
|
|
452
|
+
} from "node:fs";
|
|
453
|
+
import { homedir as homedir2 } from "node:os";
|
|
454
|
+
import { isAbsolute, join as join2 } from "node:path";
|
|
455
|
+
var EMPTY_REGISTRY = { version: 1, rosters: [] };
|
|
456
|
+
function registryPath() {
|
|
457
|
+
const base = process.env["XDG_CONFIG_HOME"] ?? join2(homedir2(), ".config");
|
|
458
|
+
return join2(base, "space-bus", "rosters.json");
|
|
459
|
+
}
|
|
460
|
+
function isEnoent2(err) {
|
|
461
|
+
return isErrnoCode2(err, "ENOENT");
|
|
462
|
+
}
|
|
463
|
+
function isErrnoCode2(err, code) {
|
|
464
|
+
return typeof err === "object" && err !== null && "code" in err && err.code === code;
|
|
465
|
+
}
|
|
466
|
+
function closeQuietly2(fd) {
|
|
467
|
+
try {
|
|
468
|
+
closeSync2(fd);
|
|
469
|
+
} catch {}
|
|
470
|
+
}
|
|
471
|
+
function readRegistry() {
|
|
472
|
+
const target = registryPath();
|
|
473
|
+
let raw;
|
|
474
|
+
try {
|
|
475
|
+
raw = readFileSync2(target, "utf8");
|
|
476
|
+
} catch (err) {
|
|
477
|
+
if (isEnoent2(err)) {
|
|
478
|
+
return { ok: true, registry: EMPTY_REGISTRY };
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
ok: false,
|
|
482
|
+
error: `space-bus: cannot read registry at ${target}: ${String(err)}`
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
let json;
|
|
486
|
+
try {
|
|
487
|
+
json = JSON.parse(raw);
|
|
488
|
+
} catch (err) {
|
|
489
|
+
return {
|
|
490
|
+
ok: false,
|
|
491
|
+
error: `space-bus: registry at ${target} is not valid JSON: ${String(err)}`
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
const parsed = registryFileSchema.safeParse(json);
|
|
495
|
+
if (!parsed.success) {
|
|
496
|
+
return {
|
|
497
|
+
ok: false,
|
|
498
|
+
error: `space-bus: registry at ${target} failed schema validation: ${parsed.error.message}`
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
const nonAbsolute = parsed.data.rosters.find((entry) => !isAbsolute(entry.path));
|
|
502
|
+
if (nonAbsolute) {
|
|
503
|
+
return {
|
|
504
|
+
ok: false,
|
|
505
|
+
error: `space-bus: registry at ${target} contains a non-absolute path for roster "${nonAbsolute.name}": ${nonAbsolute.path}`
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
return { ok: true, registry: parsed.data };
|
|
509
|
+
}
|
|
510
|
+
function writeRegistryAtomic(data) {
|
|
511
|
+
const target = registryPath();
|
|
512
|
+
const dir = join2(target, "..");
|
|
513
|
+
try {
|
|
514
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
515
|
+
} catch (err) {
|
|
516
|
+
return {
|
|
517
|
+
ok: false,
|
|
518
|
+
error: `space-bus: failed to create registry dir ${dir}: ${String(err)}`
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
const tempPath = join2(dir, `.rosters.${randomBytes2(8).toString("hex")}.tmp`);
|
|
522
|
+
let fd;
|
|
523
|
+
try {
|
|
524
|
+
fd = openSync2(tempPath, fsConstants2.O_CREAT | fsConstants2.O_EXCL | fsConstants2.O_WRONLY, 384);
|
|
525
|
+
writeSync(fd, JSON.stringify(data, null, 2));
|
|
526
|
+
closeSync2(fd);
|
|
527
|
+
fd = undefined;
|
|
528
|
+
renameSync2(tempPath, target);
|
|
529
|
+
} catch (err) {
|
|
530
|
+
if (fd !== undefined)
|
|
531
|
+
closeQuietly2(fd);
|
|
532
|
+
try {
|
|
533
|
+
unlinkSync2(tempPath);
|
|
534
|
+
} catch {}
|
|
535
|
+
return {
|
|
536
|
+
ok: false,
|
|
537
|
+
error: `space-bus: failed to write registry at ${target}: ${String(err)}`
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
return { ok: true };
|
|
541
|
+
}
|
|
542
|
+
function registryLockPath() {
|
|
543
|
+
return join2(registryPath(), "..", "rosters.lock");
|
|
544
|
+
}
|
|
545
|
+
var LOCK_RETRY_COUNT = 10;
|
|
546
|
+
var LOCK_RETRY_DELAY_MS = 50;
|
|
547
|
+
var LOCK_STALE_MS = 1e4;
|
|
548
|
+
function sleepSync(ms) {
|
|
549
|
+
const buf = new Int32Array(new SharedArrayBuffer(4));
|
|
550
|
+
Atomics.wait(buf, 0, 0, ms);
|
|
551
|
+
}
|
|
552
|
+
function tryCreateRegistryLock(target) {
|
|
553
|
+
let fd;
|
|
554
|
+
try {
|
|
555
|
+
fd = openSync2(target, fsConstants2.O_CREAT | fsConstants2.O_EXCL | fsConstants2.O_WRONLY, 384);
|
|
556
|
+
} catch (err) {
|
|
557
|
+
if (isErrnoCode2(err, "EEXIST"))
|
|
558
|
+
return false;
|
|
559
|
+
throw err;
|
|
560
|
+
}
|
|
561
|
+
try {
|
|
562
|
+
writeSync(fd, String(process.pid));
|
|
563
|
+
} finally {
|
|
564
|
+
closeQuietly2(fd);
|
|
565
|
+
}
|
|
566
|
+
return true;
|
|
567
|
+
}
|
|
568
|
+
function isRegistryLockStale(target) {
|
|
569
|
+
try {
|
|
570
|
+
return Date.now() - statSync2(target).mtimeMs > LOCK_STALE_MS;
|
|
571
|
+
} catch {
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
function breakStaleAndRetry(target) {
|
|
576
|
+
if (!isRegistryLockStale(target))
|
|
577
|
+
return false;
|
|
578
|
+
try {
|
|
579
|
+
unlinkSync2(target);
|
|
580
|
+
} catch (err) {
|
|
581
|
+
if (!isEnoent2(err))
|
|
582
|
+
throw err;
|
|
583
|
+
}
|
|
584
|
+
return tryCreateRegistryLock(target);
|
|
585
|
+
}
|
|
586
|
+
function acquireRegistryLock() {
|
|
587
|
+
const dir = join2(registryPath(), "..");
|
|
588
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
589
|
+
const target = registryLockPath();
|
|
590
|
+
for (let attempt = 0;attempt <= LOCK_RETRY_COUNT; attempt++) {
|
|
591
|
+
if (tryCreateRegistryLock(target))
|
|
592
|
+
return true;
|
|
593
|
+
if (breakStaleAndRetry(target))
|
|
594
|
+
return true;
|
|
595
|
+
if (attempt < LOCK_RETRY_COUNT)
|
|
596
|
+
sleepSync(LOCK_RETRY_DELAY_MS);
|
|
597
|
+
}
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
600
|
+
function releaseRegistryLock() {
|
|
601
|
+
try {
|
|
602
|
+
unlinkSync2(registryLockPath());
|
|
603
|
+
} catch {}
|
|
604
|
+
}
|
|
605
|
+
function withRegistryLock(mutate) {
|
|
606
|
+
if (!acquireRegistryLock()) {
|
|
607
|
+
return {
|
|
608
|
+
ok: false,
|
|
609
|
+
error: "space-bus: timed out waiting for the registry lock (another process is mutating rosters.json) — try again"
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
try {
|
|
613
|
+
return mutate();
|
|
614
|
+
} finally {
|
|
615
|
+
releaseRegistryLock();
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function openAndValidateRosterFile(rosterPath) {
|
|
619
|
+
let fd;
|
|
620
|
+
try {
|
|
621
|
+
fd = openSync2(rosterPath, fsConstants2.O_RDONLY | fsConstants2.O_NOFOLLOW);
|
|
622
|
+
} catch (err) {
|
|
623
|
+
if (isErrnoCode2(err, "ELOOP")) {
|
|
624
|
+
return {
|
|
625
|
+
ok: false,
|
|
626
|
+
error: `space-bus: roster path ${rosterPath} is a symlink — refusing to register a symlinked roster file`
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
if (isEnoent2(err)) {
|
|
630
|
+
return {
|
|
631
|
+
ok: false,
|
|
632
|
+
error: `space-bus: roster path ${rosterPath} does not exist: ${String(err)}`
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
return {
|
|
636
|
+
ok: false,
|
|
637
|
+
error: `space-bus: failed to open roster path ${rosterPath}: ${String(err)}`
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
try {
|
|
641
|
+
let stat;
|
|
642
|
+
try {
|
|
643
|
+
stat = fstatSync(fd);
|
|
644
|
+
} catch (err) {
|
|
645
|
+
return {
|
|
646
|
+
ok: false,
|
|
647
|
+
error: `space-bus: failed to stat roster path ${rosterPath}: ${String(err)}`
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
if (!stat.isFile()) {
|
|
651
|
+
return {
|
|
652
|
+
ok: false,
|
|
653
|
+
error: `space-bus: roster path ${rosterPath} is not a regular file — refusing to register`
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
let raw;
|
|
657
|
+
try {
|
|
658
|
+
raw = readFileSync2(fd, "utf8");
|
|
659
|
+
} catch (err) {
|
|
660
|
+
return {
|
|
661
|
+
ok: false,
|
|
662
|
+
error: `space-bus: failed to read roster path ${rosterPath}: ${String(err)}`
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
let json;
|
|
666
|
+
try {
|
|
667
|
+
json = JSON.parse(raw);
|
|
668
|
+
} catch (err) {
|
|
669
|
+
return {
|
|
670
|
+
ok: false,
|
|
671
|
+
error: `space-bus: roster path ${rosterPath} is not valid JSON: ${String(err)}`
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
const parsed = manifestSchema.safeParse(json);
|
|
675
|
+
if (!parsed.success) {
|
|
676
|
+
return {
|
|
677
|
+
ok: false,
|
|
678
|
+
error: `space-bus: roster path ${rosterPath} is not a valid spacebus.json — refusing to register: ${parsed.error.message}`
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
} finally {
|
|
682
|
+
closeQuietly2(fd);
|
|
683
|
+
}
|
|
684
|
+
return { ok: true };
|
|
685
|
+
}
|
|
686
|
+
function registerRoster(name, rosterPath) {
|
|
687
|
+
const nameCheck = registryEntrySchema.shape.name.safeParse(name);
|
|
688
|
+
if (!nameCheck.success) {
|
|
689
|
+
return {
|
|
690
|
+
ok: false,
|
|
691
|
+
error: `space-bus: invalid roster name "${name}": ${nameCheck.error.issues[0]?.message ?? "must be 1-64 lowercase letters, digits, or hyphens"}`
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
const validated = openAndValidateRosterFile(rosterPath);
|
|
695
|
+
if (!validated.ok)
|
|
696
|
+
return validated;
|
|
697
|
+
let canonicalPath;
|
|
698
|
+
try {
|
|
699
|
+
canonicalPath = realpathSync(rosterPath);
|
|
700
|
+
} catch (err) {
|
|
701
|
+
return {
|
|
702
|
+
ok: false,
|
|
703
|
+
error: `space-bus: failed to canonicalize roster path ${rosterPath}: ${String(err)}`
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
return withRegistryLock(() => {
|
|
707
|
+
const readResult = readRegistry();
|
|
708
|
+
if (!readResult.ok)
|
|
709
|
+
return readResult;
|
|
710
|
+
const registry = readResult.registry;
|
|
711
|
+
const collision = registry.rosters.find((entry2) => entry2.name.toLowerCase() === name.toLowerCase());
|
|
712
|
+
if (collision) {
|
|
713
|
+
return {
|
|
714
|
+
ok: false,
|
|
715
|
+
error: `space-bus: a roster named "${collision.name}" is already registered`
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
const entry = { name, path: canonicalPath };
|
|
719
|
+
const next = {
|
|
720
|
+
...registry,
|
|
721
|
+
rosters: [...registry.rosters, entry]
|
|
722
|
+
};
|
|
723
|
+
return writeRegistryAtomic(next);
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
function unregisterRoster(name) {
|
|
727
|
+
return withRegistryLock(() => {
|
|
728
|
+
const readResult = readRegistry();
|
|
729
|
+
if (!readResult.ok)
|
|
730
|
+
return readResult;
|
|
731
|
+
const registry = readResult.registry;
|
|
732
|
+
const exists = registry.rosters.some((entry) => entry.name.toLowerCase() === name.toLowerCase());
|
|
733
|
+
if (!exists) {
|
|
734
|
+
return {
|
|
735
|
+
ok: false,
|
|
736
|
+
error: `space-bus: no roster named "${name}" is registered`
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
const rosters = registry.rosters.filter((entry) => entry.name.toLowerCase() !== name.toLowerCase());
|
|
740
|
+
const next = {
|
|
741
|
+
...registry,
|
|
742
|
+
rosters,
|
|
743
|
+
default: registry.default !== undefined && registry.default.toLowerCase() === name.toLowerCase() ? undefined : registry.default
|
|
744
|
+
};
|
|
745
|
+
return writeRegistryAtomic(next);
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
function setDefaultRoster(name) {
|
|
749
|
+
return withRegistryLock(() => {
|
|
750
|
+
const readResult = readRegistry();
|
|
751
|
+
if (!readResult.ok)
|
|
752
|
+
return readResult;
|
|
753
|
+
const registry = readResult.registry;
|
|
754
|
+
const entry = registry.rosters.find((r) => r.name.toLowerCase() === name.toLowerCase());
|
|
755
|
+
if (!entry) {
|
|
756
|
+
return {
|
|
757
|
+
ok: false,
|
|
758
|
+
error: `space-bus: no roster named "${name}" is registered`
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
const next = { ...registry, default: entry.name };
|
|
762
|
+
return writeRegistryAtomic(next);
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
function resolveRosterByName(name) {
|
|
766
|
+
const readResult = readRegistry();
|
|
767
|
+
if (!readResult.ok)
|
|
768
|
+
return readResult;
|
|
769
|
+
const registry = readResult.registry;
|
|
770
|
+
const entry = registry.rosters.find((r) => r.name.toLowerCase() === name.toLowerCase());
|
|
771
|
+
if (!entry) {
|
|
772
|
+
const known = registry.rosters.map((r) => r.name);
|
|
773
|
+
return {
|
|
774
|
+
ok: false,
|
|
775
|
+
error: known.length > 0 ? `space-bus: no roster named "${name}" is registered (known: ${known.join(", ")})` : `space-bus: no roster named "${name}" is registered (no rosters are registered)`
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
let stat;
|
|
779
|
+
try {
|
|
780
|
+
stat = lstatSync(entry.path);
|
|
781
|
+
} catch {
|
|
782
|
+
return {
|
|
783
|
+
ok: false,
|
|
784
|
+
error: `space-bus: registered roster file no longer exists at ${entry.path} (name "${entry.name}") — was it moved or deleted?`
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
if (stat.isSymbolicLink()) {
|
|
788
|
+
return {
|
|
789
|
+
ok: false,
|
|
790
|
+
error: `space-bus: registered roster path ${entry.path} (name "${entry.name}") is now a symlink — refusing to resolve a symlinked roster file`
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
if (!stat.isFile()) {
|
|
794
|
+
return {
|
|
795
|
+
ok: false,
|
|
796
|
+
error: `space-bus: registered roster path ${entry.path} (name "${entry.name}") is no longer a regular file`
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
let canonicalPath;
|
|
800
|
+
try {
|
|
801
|
+
canonicalPath = realpathSync(entry.path);
|
|
802
|
+
} catch (err) {
|
|
803
|
+
return {
|
|
804
|
+
ok: false,
|
|
805
|
+
error: `space-bus: failed to canonicalize registered roster path ${entry.path} (name "${entry.name}"): ${String(err)}`
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
return { ok: true, path: canonicalPath, name: entry.name };
|
|
809
|
+
}
|
|
810
|
+
|
|
437
811
|
// src/config.ts
|
|
438
812
|
var managedServerConfigSchema = z3.object({
|
|
439
813
|
command: z3.array(z3.string()).optional(),
|
|
@@ -467,7 +841,7 @@ var manifestSchema = z3.object({
|
|
|
467
841
|
}))
|
|
468
842
|
});
|
|
469
843
|
function expandHome(path) {
|
|
470
|
-
return path.startsWith("~") ?
|
|
844
|
+
return path.startsWith("~") ? join3(homedir3(), path.slice(1)) : path;
|
|
471
845
|
}
|
|
472
846
|
function resolveRosterPath(directory) {
|
|
473
847
|
const override = process.env["SPACE_BUS_CONFIG"];
|
|
@@ -479,19 +853,18 @@ function resolveRosterPath(directory) {
|
|
|
479
853
|
if (!expanded.startsWith("/")) {
|
|
480
854
|
throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got: ${override})`);
|
|
481
855
|
}
|
|
482
|
-
return existsSync(expanded) ?
|
|
856
|
+
return existsSync(expanded) ? realpathSync2(expanded) : expanded;
|
|
483
857
|
}
|
|
484
858
|
if (directory) {
|
|
485
|
-
const candidate =
|
|
486
|
-
return existsSync(candidate) ?
|
|
859
|
+
const candidate = join3(directory, "spacebus.json");
|
|
860
|
+
return existsSync(candidate) ? realpathSync2(candidate) : candidate;
|
|
487
861
|
}
|
|
488
862
|
throw new Error("space-bus: cannot locate spacebus.json — pass a directory containing spacebus.json, or set SPACE_BUS_CONFIG to an absolute path (or ~-prefixed path) to the roster file");
|
|
489
863
|
}
|
|
490
|
-
function
|
|
491
|
-
const manifestPath = resolveRosterPath(directory);
|
|
864
|
+
function getRosterAtPath(manifestPath) {
|
|
492
865
|
let raw;
|
|
493
866
|
try {
|
|
494
|
-
raw =
|
|
867
|
+
raw = readFileSync3(manifestPath, "utf8");
|
|
495
868
|
} catch (err) {
|
|
496
869
|
throw new Error(`space-bus: cannot read roster at ${manifestPath}: ${err.message} (set SPACE_BUS_CONFIG or ensure spacebus.json exists in the workspace directory)`);
|
|
497
870
|
}
|
|
@@ -514,6 +887,10 @@ function getRoster(directory) {
|
|
|
514
887
|
}
|
|
515
888
|
return parsed.data;
|
|
516
889
|
}
|
|
890
|
+
function getRoster(directory) {
|
|
891
|
+
const manifestPath = resolveRosterPath(directory);
|
|
892
|
+
return getRosterAtPath(manifestPath);
|
|
893
|
+
}
|
|
517
894
|
function getProjects(manifest) {
|
|
518
895
|
return manifest.projects.map((p) => {
|
|
519
896
|
const expandedPath = expandHome(p.path);
|
|
@@ -527,9 +904,8 @@ function getCredentials() {
|
|
|
527
904
|
const username = process.env["OPENCODE_SERVER_USERNAME"] ?? "opencode";
|
|
528
905
|
return { username, password };
|
|
529
906
|
}
|
|
530
|
-
function
|
|
531
|
-
const
|
|
532
|
-
const manifest = getRoster(directory);
|
|
907
|
+
function loadContextAtPath(rosterPath) {
|
|
908
|
+
const manifest = getRosterAtPath(rosterPath);
|
|
533
909
|
const projects = getProjects(manifest);
|
|
534
910
|
if (manifest.server.baseUrl === undefined) {
|
|
535
911
|
const live = attachLive(rosterPath);
|
|
@@ -546,6 +922,28 @@ function loadContext(directory) {
|
|
|
546
922
|
credentials: getCredentials()
|
|
547
923
|
};
|
|
548
924
|
}
|
|
925
|
+
function loadContext(directory) {
|
|
926
|
+
const rosterPath = resolveRosterPath(directory);
|
|
927
|
+
return loadContextAtPath(rosterPath);
|
|
928
|
+
}
|
|
929
|
+
function loadContextForRoster(rosterName) {
|
|
930
|
+
const resolved = resolveRosterByName(rosterName);
|
|
931
|
+
if (!resolved.ok) {
|
|
932
|
+
throw new Error(resolved.error);
|
|
933
|
+
}
|
|
934
|
+
return {
|
|
935
|
+
context: loadContextAtPath(resolved.path),
|
|
936
|
+
rosterPath: resolved.path,
|
|
937
|
+
rosterName: resolved.name
|
|
938
|
+
};
|
|
939
|
+
}
|
|
940
|
+
function loadContextForRosterPath(rosterPath) {
|
|
941
|
+
return loadContextAtPath(rosterPath);
|
|
942
|
+
}
|
|
943
|
+
function isManagedRosterAtPath(rosterPath) {
|
|
944
|
+
const manifest = getRosterAtPath(rosterPath);
|
|
945
|
+
return manifest.server.managed !== undefined;
|
|
946
|
+
}
|
|
549
947
|
function isManagedRoster(directory) {
|
|
550
948
|
const manifest = getRoster(directory);
|
|
551
949
|
return manifest.server.managed !== undefined;
|
|
@@ -553,8 +951,8 @@ function isManagedRoster(directory) {
|
|
|
553
951
|
|
|
554
952
|
// src/server.ts
|
|
555
953
|
import { spawn } from "node:child_process";
|
|
556
|
-
import { randomBytes as
|
|
557
|
-
import { existsSync as existsSync2, openSync as
|
|
954
|
+
import { randomBytes as randomBytes3 } from "node:crypto";
|
|
955
|
+
import { existsSync as existsSync2, openSync as openSync3, readFileSync as readFileSync4, realpathSync as realpathSync3 } from "node:fs";
|
|
558
956
|
import { dirname } from "node:path";
|
|
559
957
|
var DEFAULT_READINESS_BUDGET_MS = 15000;
|
|
560
958
|
var DEFAULT_LOCK_WAIT_BUDGET_MS = 15000;
|
|
@@ -569,13 +967,13 @@ function sleep(ms) {
|
|
|
569
967
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
570
968
|
}
|
|
571
969
|
function readManifest(rosterPath) {
|
|
572
|
-
const raw =
|
|
970
|
+
const raw = readFileSync4(rosterPath, "utf8");
|
|
573
971
|
const json = JSON.parse(raw);
|
|
574
972
|
return manifestSchema.parse(json);
|
|
575
973
|
}
|
|
576
974
|
function readLogTail(logPath, maxChars = 4000) {
|
|
577
975
|
try {
|
|
578
|
-
const content =
|
|
976
|
+
const content = readFileSync4(logPath, "utf8");
|
|
579
977
|
return content.length > maxChars ? content.slice(-maxChars) : content;
|
|
580
978
|
} catch {
|
|
581
979
|
return "";
|
|
@@ -748,12 +1146,12 @@ function reapOrphanedProvisional(rosterPath) {
|
|
|
748
1146
|
}
|
|
749
1147
|
async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
750
1148
|
reapOrphanedProvisional(rosterPath);
|
|
751
|
-
const password =
|
|
1149
|
+
const password = randomBytes3(32).toString("base64url");
|
|
752
1150
|
const command = managedConfig.command && managedConfig.command.length > 0 ? managedConfig.command : ["harness", "serve"];
|
|
753
1151
|
const cwd = managedConfig.cwd ?? dirname(rosterPath);
|
|
754
1152
|
const requestedPort = managedConfig.port ?? 0;
|
|
755
1153
|
const logPath = logFilePath(rosterPath);
|
|
756
|
-
const logFd =
|
|
1154
|
+
const logFd = openSync3(logPath, "w", 384);
|
|
757
1155
|
let child;
|
|
758
1156
|
let pid;
|
|
759
1157
|
let spawnError;
|
|
@@ -820,7 +1218,7 @@ async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
|
820
1218
|
}
|
|
821
1219
|
async function ensureServer(rosterPath, options = {}) {
|
|
822
1220
|
if (existsSync2(rosterPath)) {
|
|
823
|
-
rosterPath =
|
|
1221
|
+
rosterPath = realpathSync3(rosterPath);
|
|
824
1222
|
}
|
|
825
1223
|
const readinessBudgetMs = options.readinessBudgetMs ?? DEFAULT_READINESS_BUDGET_MS;
|
|
826
1224
|
const lockWaitBudgetMs = options.lockWaitBudgetMs ?? DEFAULT_LOCK_WAIT_BUDGET_MS;
|
|
@@ -1044,37 +1442,37 @@ import { fileURLToPath } from "node:url";
|
|
|
1044
1442
|
// src/service.ts
|
|
1045
1443
|
import {
|
|
1046
1444
|
chmodSync as chmodSync3,
|
|
1047
|
-
closeSync as
|
|
1445
|
+
closeSync as closeSync4,
|
|
1048
1446
|
existsSync as existsSync3,
|
|
1049
1447
|
fchmodSync,
|
|
1050
|
-
constants as
|
|
1051
|
-
mkdirSync as
|
|
1052
|
-
openSync as
|
|
1053
|
-
unlinkSync as
|
|
1448
|
+
constants as fsConstants4,
|
|
1449
|
+
mkdirSync as mkdirSync3,
|
|
1450
|
+
openSync as openSync5,
|
|
1451
|
+
unlinkSync as unlinkSync3
|
|
1054
1452
|
} from "node:fs";
|
|
1055
|
-
import { homedir as
|
|
1056
|
-
import { isAbsolute, join as
|
|
1453
|
+
import { homedir as homedir5 } from "node:os";
|
|
1454
|
+
import { isAbsolute as isAbsolute2, join as join5 } from "node:path";
|
|
1057
1455
|
|
|
1058
1456
|
// src/launchd.ts
|
|
1059
1457
|
import { spawn as spawn2 } from "node:child_process";
|
|
1060
|
-
import { randomBytes as
|
|
1458
|
+
import { randomBytes as randomBytes4 } from "node:crypto";
|
|
1061
1459
|
import {
|
|
1062
1460
|
chmodSync as chmodSync2,
|
|
1063
|
-
closeSync as
|
|
1064
|
-
constants as
|
|
1065
|
-
lstatSync,
|
|
1066
|
-
openSync as
|
|
1067
|
-
renameSync as
|
|
1068
|
-
statSync as
|
|
1461
|
+
closeSync as closeSync3,
|
|
1462
|
+
constants as fsConstants3,
|
|
1463
|
+
lstatSync as lstatSync2,
|
|
1464
|
+
openSync as openSync4,
|
|
1465
|
+
renameSync as renameSync3,
|
|
1466
|
+
statSync as statSync3,
|
|
1069
1467
|
writeFileSync as writeFileSync2
|
|
1070
1468
|
} from "node:fs";
|
|
1071
|
-
import { homedir as
|
|
1072
|
-
import { join as
|
|
1469
|
+
import { homedir as homedir4 } from "node:os";
|
|
1470
|
+
import { join as join4 } from "node:path";
|
|
1073
1471
|
function serviceLabel(rosterPath) {
|
|
1074
1472
|
return `bot.fro.space-bus.${rosterKey(rosterPath)}`;
|
|
1075
1473
|
}
|
|
1076
1474
|
function plistPath(label, baseDir) {
|
|
1077
|
-
return
|
|
1475
|
+
return join4(baseDir ?? join4(homedir4(), "Library", "LaunchAgents"), `${label}.plist`);
|
|
1078
1476
|
}
|
|
1079
1477
|
function xmlEscape(value) {
|
|
1080
1478
|
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
@@ -1084,8 +1482,8 @@ function renderPlist(opts) {
|
|
|
1084
1482
|
const cliEntry = xmlEscape(opts.cliEntry);
|
|
1085
1483
|
const rosterPath = xmlEscape(opts.rosterPath);
|
|
1086
1484
|
const label = xmlEscape(opts.label);
|
|
1087
|
-
const outLog = xmlEscape(
|
|
1088
|
-
const errLog = xmlEscape(
|
|
1485
|
+
const outLog = xmlEscape(join4(opts.stateDir, "service.log"));
|
|
1486
|
+
const errLog = xmlEscape(join4(opts.stateDir, "service.err.log"));
|
|
1089
1487
|
const envEntries = [
|
|
1090
1488
|
` <key>SPACE_BUS_CONFIG</key>
|
|
1091
1489
|
<string>${rosterPath}</string>`
|
|
@@ -1134,24 +1532,24 @@ ${envEntries.join(`
|
|
|
1134
1532
|
`;
|
|
1135
1533
|
}
|
|
1136
1534
|
function writePlistAtomic(path, content) {
|
|
1137
|
-
const dir =
|
|
1138
|
-
const tempPath =
|
|
1535
|
+
const dir = join4(path, "..");
|
|
1536
|
+
const tempPath = join4(dir, `.${randomBytes4(8).toString("hex")}.tmp`);
|
|
1139
1537
|
let fd;
|
|
1140
1538
|
try {
|
|
1141
|
-
fd =
|
|
1539
|
+
fd = openSync4(tempPath, fsConstants3.O_EXCL | fsConstants3.O_CREAT | fsConstants3.O_WRONLY, 420);
|
|
1142
1540
|
} catch (err) {
|
|
1143
1541
|
return { ok: false, error: `failed to create temp plist: ${String(err)}` };
|
|
1144
1542
|
}
|
|
1145
1543
|
try {
|
|
1146
1544
|
writeFileSync2(fd, content);
|
|
1147
1545
|
} catch (err) {
|
|
1148
|
-
|
|
1546
|
+
closeQuietly3(fd);
|
|
1149
1547
|
return { ok: false, error: `failed to write temp plist: ${String(err)}` };
|
|
1150
1548
|
}
|
|
1151
|
-
|
|
1549
|
+
closeQuietly3(fd);
|
|
1152
1550
|
try {
|
|
1153
1551
|
chmodSync2(tempPath, 420);
|
|
1154
|
-
|
|
1552
|
+
renameSync3(tempPath, path);
|
|
1155
1553
|
} catch (err) {
|
|
1156
1554
|
return { ok: false, error: `failed to install plist: ${String(err)}` };
|
|
1157
1555
|
}
|
|
@@ -1161,7 +1559,7 @@ function verifyPlistSafe(path) {
|
|
|
1161
1559
|
let mode;
|
|
1162
1560
|
let uid;
|
|
1163
1561
|
try {
|
|
1164
|
-
const linkStat =
|
|
1562
|
+
const linkStat = lstatSync2(path);
|
|
1165
1563
|
if (linkStat.isSymbolicLink()) {
|
|
1166
1564
|
return { safe: false, reason: "plist path is a symlink" };
|
|
1167
1565
|
}
|
|
@@ -1169,7 +1567,7 @@ function verifyPlistSafe(path) {
|
|
|
1169
1567
|
return { safe: false, reason: `failed to stat plist: ${String(err)}` };
|
|
1170
1568
|
}
|
|
1171
1569
|
try {
|
|
1172
|
-
const stat =
|
|
1570
|
+
const stat = statSync3(path);
|
|
1173
1571
|
mode = stat.mode & 511;
|
|
1174
1572
|
uid = stat.uid;
|
|
1175
1573
|
} catch (err) {
|
|
@@ -1189,9 +1587,9 @@ function verifyPlistSafe(path) {
|
|
|
1189
1587
|
}
|
|
1190
1588
|
return { safe: true };
|
|
1191
1589
|
}
|
|
1192
|
-
function
|
|
1590
|
+
function closeQuietly3(fd) {
|
|
1193
1591
|
try {
|
|
1194
|
-
|
|
1592
|
+
closeSync3(fd);
|
|
1195
1593
|
} catch {}
|
|
1196
1594
|
}
|
|
1197
1595
|
var LAUNCHCTL_TIMEOUT_MS = 15000;
|
|
@@ -1304,7 +1702,7 @@ function resolveDeps(deps) {
|
|
|
1304
1702
|
now: deps.now ?? Date.now,
|
|
1305
1703
|
verifyBudgetMs: deps.verifyBudgetMs ?? DEFAULT_VERIFY_BUDGET_MS,
|
|
1306
1704
|
verifyPollMs: deps.verifyPollMs ?? DEFAULT_VERIFY_POLL_MS,
|
|
1307
|
-
launchAgentsDir: deps.launchAgentsDir ??
|
|
1705
|
+
launchAgentsDir: deps.launchAgentsDir ?? join5(homedir5(), "Library", "LaunchAgents")
|
|
1308
1706
|
};
|
|
1309
1707
|
}
|
|
1310
1708
|
function isEphemeralCachePath(path) {
|
|
@@ -1331,7 +1729,7 @@ function identityFor(rosterPath, launchAgentsDir) {
|
|
|
1331
1729
|
function preCreateLog(path) {
|
|
1332
1730
|
let fd;
|
|
1333
1731
|
try {
|
|
1334
|
-
fd =
|
|
1732
|
+
fd = openSync5(path, fsConstants4.O_WRONLY | fsConstants4.O_APPEND | fsConstants4.O_CREAT | fsConstants4.O_NOFOLLOW, 384);
|
|
1335
1733
|
} catch (err) {
|
|
1336
1734
|
if (isSymlinkError(err)) {
|
|
1337
1735
|
return { ok: false, error: `refusing symlinked log path: ${path}` };
|
|
@@ -1343,7 +1741,7 @@ function preCreateLog(path) {
|
|
|
1343
1741
|
} catch (err) {
|
|
1344
1742
|
return { ok: false, error: `failed to chmod log ${path}: ${String(err)}` };
|
|
1345
1743
|
} finally {
|
|
1346
|
-
|
|
1744
|
+
closeSync4(fd);
|
|
1347
1745
|
}
|
|
1348
1746
|
return { ok: true };
|
|
1349
1747
|
}
|
|
@@ -1352,7 +1750,7 @@ function isSymlinkError(err) {
|
|
|
1352
1750
|
}
|
|
1353
1751
|
function ensureStateDir(stateDir) {
|
|
1354
1752
|
try {
|
|
1355
|
-
|
|
1753
|
+
mkdirSync3(stateDir, { recursive: true, mode: 448 });
|
|
1356
1754
|
} catch (err) {
|
|
1357
1755
|
return {
|
|
1358
1756
|
ok: false,
|
|
@@ -1380,7 +1778,7 @@ function resolveXdgStateHome() {
|
|
|
1380
1778
|
const xdgStateHomeEnv = process.env["XDG_STATE_HOME"];
|
|
1381
1779
|
if (xdgStateHomeEnv === undefined)
|
|
1382
1780
|
return { ok: true, value: undefined };
|
|
1383
|
-
if (!
|
|
1781
|
+
if (!isAbsolute2(xdgStateHomeEnv)) {
|
|
1384
1782
|
return {
|
|
1385
1783
|
ok: false,
|
|
1386
1784
|
error: `XDG_STATE_HOME must be an absolute path to pin into the launchd environment, got: ${xdgStateHomeEnv}`
|
|
@@ -1419,7 +1817,7 @@ async function bootstrapWithExit5Retry(resolved, label, plistFilePath) {
|
|
|
1419
1817
|
}
|
|
1420
1818
|
function prepareInstallFilesystem(resolved, stateDir) {
|
|
1421
1819
|
try {
|
|
1422
|
-
|
|
1820
|
+
mkdirSync3(resolved.launchAgentsDir, { recursive: true, mode: 493 });
|
|
1423
1821
|
} catch (err) {
|
|
1424
1822
|
return {
|
|
1425
1823
|
ok: false,
|
|
@@ -1548,7 +1946,7 @@ async function verifyRunning(rosterPath, label, resolved, plistFilePath) {
|
|
|
1548
1946
|
await resolved.sleep(resolved.verifyPollMs);
|
|
1549
1947
|
}
|
|
1550
1948
|
const stateDir = stateDirFor(rosterPath);
|
|
1551
|
-
const logPath =
|
|
1949
|
+
const logPath = join5(stateDir, "service.log");
|
|
1552
1950
|
return {
|
|
1553
1951
|
ok: false,
|
|
1554
1952
|
error: `install did not verify as running within ${resolved.verifyBudgetMs}ms ` + `(loaded=${lastLoaded}, running=${lastRunning}` + `${lastProbeError ? `, last probe error: ${lastProbeError}` : ""})` + ` — label=${label}, plist=${plistFilePath ?? plistPath(label, resolved.launchAgentsDir)}, log=${logPath}`
|
|
@@ -1573,10 +1971,10 @@ async function uninstallService(rosterPath, deps = {}) {
|
|
|
1573
1971
|
}
|
|
1574
1972
|
let plistRemoved = false;
|
|
1575
1973
|
try {
|
|
1576
|
-
|
|
1974
|
+
unlinkSync3(plistFilePath);
|
|
1577
1975
|
plistRemoved = true;
|
|
1578
1976
|
} catch (err) {
|
|
1579
|
-
if (!
|
|
1977
|
+
if (!isEnoent3(err)) {
|
|
1580
1978
|
return {
|
|
1581
1979
|
ok: false,
|
|
1582
1980
|
error: `failed to remove plist ${plistFilePath}: ${String(err)}`
|
|
@@ -1589,7 +1987,7 @@ async function uninstallService(rosterPath, deps = {}) {
|
|
|
1589
1987
|
label
|
|
1590
1988
|
};
|
|
1591
1989
|
}
|
|
1592
|
-
function
|
|
1990
|
+
function isEnoent3(err) {
|
|
1593
1991
|
return typeof err === "object" && err !== null && "code" in err && err.code === "ENOENT";
|
|
1594
1992
|
}
|
|
1595
1993
|
async function serviceStatus(rosterPath, deps = {}) {
|