@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.
@@ -2,9 +2,9 @@ import { createRequire } from "node:module";
2
2
  var __require = /* @__PURE__ */ createRequire(import.meta.url);
3
3
 
4
4
  // src/config.ts
5
- import { existsSync, readFileSync as readFileSync2, realpathSync } from "node:fs";
6
- import { homedir as homedir2 } from "node:os";
7
- import { join as join2 } from "node:path";
5
+ import { existsSync, readFileSync as readFileSync3, realpathSync as realpathSync2 } from "node:fs";
6
+ import { homedir as homedir3 } from "node:os";
7
+ import { join as join3 } from "node:path";
8
8
  import { z as z3 } from "zod";
9
9
 
10
10
  // src/contract.ts
@@ -433,123 +433,6 @@ function removeProvisional(rosterPath) {
433
433
  } catch {}
434
434
  }
435
435
 
436
- // src/config.ts
437
- var managedServerConfigSchema = z3.object({
438
- command: z3.array(z3.string()).optional(),
439
- cwd: z3.string().optional(),
440
- port: z3.number().int().nonnegative().optional()
441
- });
442
- var serverConfigSchema = z3.object({
443
- baseUrl: z3.url().optional(),
444
- managed: managedServerConfigSchema.optional()
445
- }).superRefine((val, ctx) => {
446
- const hasBaseUrl = val.baseUrl !== undefined;
447
- const hasManaged = val.managed !== undefined;
448
- if (hasBaseUrl && hasManaged) {
449
- ctx.addIssue({
450
- code: "custom",
451
- message: "space-bus: roster's server block must specify exactly one of `baseUrl` or `managed`, not both"
452
- });
453
- } else if (!hasBaseUrl && !hasManaged) {
454
- ctx.addIssue({
455
- code: "custom",
456
- message: "space-bus: roster's server block must specify one of `baseUrl` (externally-managed) or `managed` (plugin-spawned) — neither was present"
457
- });
458
- }
459
- });
460
- var manifestSchema = z3.object({
461
- server: serverConfigSchema,
462
- projects: z3.array(z3.object({
463
- name: z3.string(),
464
- path: z3.string(),
465
- description: z3.string()
466
- }))
467
- });
468
- function expandHome(path) {
469
- return path.startsWith("~") ? join2(homedir2(), path.slice(1)) : path;
470
- }
471
- function resolveRosterPath(directory) {
472
- const override = process.env["SPACE_BUS_CONFIG"];
473
- if (override) {
474
- if (override.includes("://")) {
475
- throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got a URL: ${override})`);
476
- }
477
- const expanded = expandHome(override);
478
- if (!expanded.startsWith("/")) {
479
- throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got: ${override})`);
480
- }
481
- return existsSync(expanded) ? realpathSync(expanded) : expanded;
482
- }
483
- if (directory) {
484
- const candidate = join2(directory, "spacebus.json");
485
- return existsSync(candidate) ? realpathSync(candidate) : candidate;
486
- }
487
- 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");
488
- }
489
- function getRoster(directory) {
490
- const manifestPath = resolveRosterPath(directory);
491
- let raw;
492
- try {
493
- raw = readFileSync2(manifestPath, "utf8");
494
- } catch (err) {
495
- 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)`);
496
- }
497
- let json;
498
- try {
499
- json = JSON.parse(raw);
500
- } catch (err) {
501
- throw new Error(`space-bus: roster at ${manifestPath} is not valid JSON: ${err.message}`);
502
- }
503
- const parsed = manifestSchema.safeParse(json);
504
- if (!parsed.success) {
505
- throw new Error(`space-bus: roster at ${manifestPath} failed schema validation: ${parsed.error.message}`);
506
- }
507
- if (parsed.data.server.baseUrl !== undefined) {
508
- const url = new URL(parsed.data.server.baseUrl);
509
- const hostname = url.hostname;
510
- if (!LOOPBACK_HOSTS.has(hostname)) {
511
- throw new Error(`space-bus: spacebus.json baseUrl must point to localhost (got ${hostname}) — refusing to send credentials off-machine`);
512
- }
513
- }
514
- return parsed.data;
515
- }
516
- function getProjects(manifest) {
517
- return manifest.projects.map((p) => {
518
- const expandedPath = expandHome(p.path);
519
- return { ...p, expandedPath, exists: existsSync(expandedPath) };
520
- });
521
- }
522
- function getCredentials() {
523
- const password = process.env["OPENCODE_SERVER_PASSWORD"];
524
- if (!password)
525
- return {};
526
- const username = process.env["OPENCODE_SERVER_USERNAME"] ?? "opencode";
527
- return { username, password };
528
- }
529
- function loadContext(directory) {
530
- const rosterPath = resolveRosterPath(directory);
531
- const manifest = getRoster(directory);
532
- const projects = getProjects(manifest);
533
- if (manifest.server.baseUrl === undefined) {
534
- const live = attachLive(rosterPath);
535
- if (!live) {
536
- throw new Error(`space-bus: managed server not running for ${rosterPath} — call ensureServer() or run \`space-bus serve\``);
537
- }
538
- return {
539
- roster: { server: { baseUrl: live.baseUrl }, projects },
540
- credentials: live.credentials
541
- };
542
- }
543
- return {
544
- roster: { server: { baseUrl: manifest.server.baseUrl }, projects },
545
- credentials: getCredentials()
546
- };
547
- }
548
- function isManagedRoster(directory) {
549
- const manifest = getRoster(directory);
550
- return manifest.server.managed !== undefined;
551
- }
552
-
553
436
  // src/registry.ts
554
437
  import { randomBytes as randomBytes2 } from "node:crypto";
555
438
  import {
@@ -559,19 +442,19 @@ import {
559
442
  lstatSync,
560
443
  mkdirSync as mkdirSync2,
561
444
  openSync as openSync2,
562
- readFileSync as readFileSync3,
563
- realpathSync as realpathSync2,
445
+ readFileSync as readFileSync2,
446
+ realpathSync,
564
447
  renameSync as renameSync2,
565
448
  statSync as statSync2,
566
449
  unlinkSync as unlinkSync2,
567
450
  writeSync
568
451
  } from "node:fs";
569
- import { homedir as homedir3 } from "node:os";
570
- import { isAbsolute, join as join3 } from "node:path";
452
+ import { homedir as homedir2 } from "node:os";
453
+ import { isAbsolute, join as join2 } from "node:path";
571
454
  var EMPTY_REGISTRY = { version: 1, rosters: [] };
572
455
  function registryPath() {
573
- const base = process.env["XDG_CONFIG_HOME"] ?? join3(homedir3(), ".config");
574
- return join3(base, "space-bus", "rosters.json");
456
+ const base = process.env["XDG_CONFIG_HOME"] ?? join2(homedir2(), ".config");
457
+ return join2(base, "space-bus", "rosters.json");
575
458
  }
576
459
  function isEnoent2(err) {
577
460
  return isErrnoCode2(err, "ENOENT");
@@ -588,7 +471,7 @@ function readRegistry() {
588
471
  const target = registryPath();
589
472
  let raw;
590
473
  try {
591
- raw = readFileSync3(target, "utf8");
474
+ raw = readFileSync2(target, "utf8");
592
475
  } catch (err) {
593
476
  if (isEnoent2(err)) {
594
477
  return { ok: true, registry: EMPTY_REGISTRY };
@@ -625,7 +508,7 @@ function readRegistry() {
625
508
  }
626
509
  function writeRegistryAtomic(data) {
627
510
  const target = registryPath();
628
- const dir = join3(target, "..");
511
+ const dir = join2(target, "..");
629
512
  try {
630
513
  mkdirSync2(dir, { recursive: true, mode: 448 });
631
514
  } catch (err) {
@@ -634,7 +517,7 @@ function writeRegistryAtomic(data) {
634
517
  error: `space-bus: failed to create registry dir ${dir}: ${String(err)}`
635
518
  };
636
519
  }
637
- const tempPath = join3(dir, `.rosters.${randomBytes2(8).toString("hex")}.tmp`);
520
+ const tempPath = join2(dir, `.rosters.${randomBytes2(8).toString("hex")}.tmp`);
638
521
  let fd;
639
522
  try {
640
523
  fd = openSync2(tempPath, fsConstants2.O_CREAT | fsConstants2.O_EXCL | fsConstants2.O_WRONLY, 384);
@@ -656,7 +539,7 @@ function writeRegistryAtomic(data) {
656
539
  return { ok: true };
657
540
  }
658
541
  function registryLockPath() {
659
- return join3(registryPath(), "..", "rosters.lock");
542
+ return join2(registryPath(), "..", "rosters.lock");
660
543
  }
661
544
  var LOCK_RETRY_COUNT = 10;
662
545
  var LOCK_RETRY_DELAY_MS = 50;
@@ -700,7 +583,7 @@ function breakStaleAndRetry(target) {
700
583
  return tryCreateRegistryLock(target);
701
584
  }
702
585
  function acquireRegistryLock() {
703
- const dir = join3(registryPath(), "..");
586
+ const dir = join2(registryPath(), "..");
704
587
  mkdirSync2(dir, { recursive: true, mode: 448 });
705
588
  const target = registryLockPath();
706
589
  for (let attempt = 0;attempt <= LOCK_RETRY_COUNT; attempt++) {
@@ -771,7 +654,7 @@ function openAndValidateRosterFile(rosterPath) {
771
654
  }
772
655
  let raw;
773
656
  try {
774
- raw = readFileSync3(fd, "utf8");
657
+ raw = readFileSync2(fd, "utf8");
775
658
  } catch (err) {
776
659
  return {
777
660
  ok: false,
@@ -812,7 +695,7 @@ function registerRoster(name, rosterPath) {
812
695
  return validated;
813
696
  let canonicalPath;
814
697
  try {
815
- canonicalPath = realpathSync2(rosterPath);
698
+ canonicalPath = realpathSync(rosterPath);
816
699
  } catch (err) {
817
700
  return {
818
701
  ok: false,
@@ -914,14 +797,155 @@ function resolveRosterByName(name) {
914
797
  }
915
798
  let canonicalPath;
916
799
  try {
917
- canonicalPath = realpathSync2(entry.path);
800
+ canonicalPath = realpathSync(entry.path);
918
801
  } catch (err) {
919
802
  return {
920
803
  ok: false,
921
804
  error: `space-bus: failed to canonicalize registered roster path ${entry.path} (name "${entry.name}"): ${String(err)}`
922
805
  };
923
806
  }
924
- return { ok: true, path: canonicalPath };
807
+ return { ok: true, path: canonicalPath, name: entry.name };
808
+ }
809
+
810
+ // src/config.ts
811
+ var managedServerConfigSchema = z3.object({
812
+ command: z3.array(z3.string()).optional(),
813
+ cwd: z3.string().optional(),
814
+ port: z3.number().int().nonnegative().optional()
815
+ });
816
+ var serverConfigSchema = z3.object({
817
+ baseUrl: z3.url().optional(),
818
+ managed: managedServerConfigSchema.optional()
819
+ }).superRefine((val, ctx) => {
820
+ const hasBaseUrl = val.baseUrl !== undefined;
821
+ const hasManaged = val.managed !== undefined;
822
+ if (hasBaseUrl && hasManaged) {
823
+ ctx.addIssue({
824
+ code: "custom",
825
+ message: "space-bus: roster's server block must specify exactly one of `baseUrl` or `managed`, not both"
826
+ });
827
+ } else if (!hasBaseUrl && !hasManaged) {
828
+ ctx.addIssue({
829
+ code: "custom",
830
+ message: "space-bus: roster's server block must specify one of `baseUrl` (externally-managed) or `managed` (plugin-spawned) — neither was present"
831
+ });
832
+ }
833
+ });
834
+ var manifestSchema = z3.object({
835
+ server: serverConfigSchema,
836
+ projects: z3.array(z3.object({
837
+ name: z3.string(),
838
+ path: z3.string(),
839
+ description: z3.string()
840
+ }))
841
+ });
842
+ function expandHome(path) {
843
+ return path.startsWith("~") ? join3(homedir3(), path.slice(1)) : path;
844
+ }
845
+ function resolveRosterPath(directory) {
846
+ const override = process.env["SPACE_BUS_CONFIG"];
847
+ if (override) {
848
+ if (override.includes("://")) {
849
+ throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got a URL: ${override})`);
850
+ }
851
+ const expanded = expandHome(override);
852
+ if (!expanded.startsWith("/")) {
853
+ throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got: ${override})`);
854
+ }
855
+ return existsSync(expanded) ? realpathSync2(expanded) : expanded;
856
+ }
857
+ if (directory) {
858
+ const candidate = join3(directory, "spacebus.json");
859
+ return existsSync(candidate) ? realpathSync2(candidate) : candidate;
860
+ }
861
+ 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");
862
+ }
863
+ function getRosterAtPath(manifestPath) {
864
+ let raw;
865
+ try {
866
+ raw = readFileSync3(manifestPath, "utf8");
867
+ } catch (err) {
868
+ 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)`);
869
+ }
870
+ let json;
871
+ try {
872
+ json = JSON.parse(raw);
873
+ } catch (err) {
874
+ throw new Error(`space-bus: roster at ${manifestPath} is not valid JSON: ${err.message}`);
875
+ }
876
+ const parsed = manifestSchema.safeParse(json);
877
+ if (!parsed.success) {
878
+ throw new Error(`space-bus: roster at ${manifestPath} failed schema validation: ${parsed.error.message}`);
879
+ }
880
+ if (parsed.data.server.baseUrl !== undefined) {
881
+ const url = new URL(parsed.data.server.baseUrl);
882
+ const hostname = url.hostname;
883
+ if (!LOOPBACK_HOSTS.has(hostname)) {
884
+ throw new Error(`space-bus: spacebus.json baseUrl must point to localhost (got ${hostname}) — refusing to send credentials off-machine`);
885
+ }
886
+ }
887
+ return parsed.data;
888
+ }
889
+ function getRoster(directory) {
890
+ const manifestPath = resolveRosterPath(directory);
891
+ return getRosterAtPath(manifestPath);
892
+ }
893
+ function getProjects(manifest) {
894
+ return manifest.projects.map((p) => {
895
+ const expandedPath = expandHome(p.path);
896
+ return { ...p, expandedPath, exists: existsSync(expandedPath) };
897
+ });
898
+ }
899
+ function getCredentials() {
900
+ const password = process.env["OPENCODE_SERVER_PASSWORD"];
901
+ if (!password)
902
+ return {};
903
+ const username = process.env["OPENCODE_SERVER_USERNAME"] ?? "opencode";
904
+ return { username, password };
905
+ }
906
+ function loadContextAtPath(rosterPath) {
907
+ const manifest = getRosterAtPath(rosterPath);
908
+ const projects = getProjects(manifest);
909
+ if (manifest.server.baseUrl === undefined) {
910
+ const live = attachLive(rosterPath);
911
+ if (!live) {
912
+ throw new Error(`space-bus: managed server not running for ${rosterPath} — call ensureServer() or run \`space-bus serve\``);
913
+ }
914
+ return {
915
+ roster: { server: { baseUrl: live.baseUrl }, projects },
916
+ credentials: live.credentials
917
+ };
918
+ }
919
+ return {
920
+ roster: { server: { baseUrl: manifest.server.baseUrl }, projects },
921
+ credentials: getCredentials()
922
+ };
923
+ }
924
+ function loadContext(directory) {
925
+ const rosterPath = resolveRosterPath(directory);
926
+ return loadContextAtPath(rosterPath);
927
+ }
928
+ function loadContextForRoster(rosterName) {
929
+ const resolved = resolveRosterByName(rosterName);
930
+ if (!resolved.ok) {
931
+ throw new Error(resolved.error);
932
+ }
933
+ return {
934
+ context: loadContextAtPath(resolved.path),
935
+ rosterPath: resolved.path,
936
+ rosterName: resolved.name
937
+ };
938
+ }
939
+ function loadContextForRosterPath(rosterPath) {
940
+ return loadContextAtPath(rosterPath);
941
+ }
942
+ function isManagedRosterAtPath(rosterPath) {
943
+ const manifest = getRosterAtPath(rosterPath);
944
+ return manifest.server.managed !== undefined;
945
+ }
946
+ function isManagedRoster(directory) {
947
+ const manifest = getRoster(directory);
948
+ return manifest.server.managed !== undefined;
925
949
  }
926
950
  // src/roster-edit.ts
927
951
  import { randomBytes as randomBytes3 } from "node:crypto";
@@ -51,6 +51,7 @@ export declare function setDefaultRoster(name: string): Result;
51
51
  export declare function resolveRosterByName(name: string): {
52
52
  ok: true;
53
53
  path: string;
54
+ name: string;
54
55
  } | {
55
56
  ok: false;
56
57
  error: string;