@secondlayer/cli 13.0.0 → 13.0.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/dist/cli.js CHANGED
@@ -34997,6 +34997,7 @@ import { spawn as spawn4, spawnSync as spawnSync5 } from "node:child_process";
34997
34997
  import { randomBytes as randomBytes4 } from "node:crypto";
34998
34998
  import { existsSync as existsSync11 } from "node:fs";
34999
34999
  import { writeFile as writeFile7 } from "node:fs/promises";
35000
+ import { createServer } from "node:net";
35000
35001
  import { join as join19, resolve as resolvePath2 } from "node:path";
35001
35002
  import { FLOORS, diskFloorGb } from "@secondlayer/shared/runtime";
35002
35003
  function parseSetupNodeMode(value) {
@@ -35024,6 +35025,7 @@ function resolveNonInteractiveConfig(flags) {
35024
35025
  nodeMode,
35025
35026
  apiPort: flags.apiPort ?? "127.0.0.1:3800",
35026
35027
  indexerPort: "127.0.0.1:3700",
35028
+ postgresPort: "127.0.0.1:5432",
35027
35029
  dir,
35028
35030
  against: flags.against,
35029
35031
  skipBootstrap: !!flags.skipBootstrap,
@@ -35047,6 +35049,44 @@ async function checkDocker() {
35047
35049
  const probe = spawnSync5("docker", ["info"], { stdio: "ignore" });
35048
35050
  return !probe.error && probe.status === 0;
35049
35051
  }
35052
+ function isPortFree(host, port) {
35053
+ return new Promise((resolvePromise) => {
35054
+ const srv = createServer();
35055
+ srv.once("error", () => resolvePromise(false));
35056
+ srv.once("listening", () => srv.close(() => resolvePromise(true)));
35057
+ srv.listen(port, host);
35058
+ });
35059
+ }
35060
+ function parsePublishSpec(spec) {
35061
+ const idx = spec.lastIndexOf(":");
35062
+ if (idx === -1)
35063
+ return { host: "127.0.0.1", port: Number(spec) };
35064
+ return { host: spec.slice(0, idx), port: Number(spec.slice(idx + 1)) };
35065
+ }
35066
+ function dockerPublishedHostPorts() {
35067
+ const probe = spawnSync5("docker", ["ps", "--format", "{{.Ports}}"], {
35068
+ encoding: "utf8"
35069
+ });
35070
+ const ports = new Set;
35071
+ if (probe.error || probe.status !== 0 || !probe.stdout)
35072
+ return ports;
35073
+ for (const match of probe.stdout.matchAll(/:(\d+)->/g)) {
35074
+ ports.add(Number(match[1]));
35075
+ }
35076
+ return ports;
35077
+ }
35078
+ async function resolveAvailablePublishSpec(spec, claimedByDocker = new Set, maxAttempts = 20) {
35079
+ const { host, port } = parsePublishSpec(spec);
35080
+ for (let attempt = 0;attempt < maxAttempts; attempt++) {
35081
+ const candidate = port + attempt;
35082
+ if (claimedByDocker.has(candidate))
35083
+ continue;
35084
+ if (await isPortFree(host, candidate)) {
35085
+ return { spec: `${host}:${candidate}`, remapped: attempt > 0 };
35086
+ }
35087
+ }
35088
+ return { spec, remapped: false };
35089
+ }
35050
35090
  function randomHex(bytes) {
35051
35091
  return randomBytes4(bytes).toString("hex");
35052
35092
  }
@@ -35095,7 +35135,7 @@ function renderSetupEnv(config, secrets) {
35095
35135
  "POSTGRES_USER=secondlayer",
35096
35136
  `POSTGRES_PASSWORD=${secrets.postgresPassword}`,
35097
35137
  "POSTGRES_DB=secondlayer",
35098
- "POSTGRES_PORT=127.0.0.1:5432",
35138
+ `POSTGRES_PORT=${config.postgresPort}`,
35099
35139
  "# The instance credential. The API validates bearer tokens against it,",
35100
35140
  "# and the CLI/SDK/MCP read it first when authenticating.",
35101
35141
  `INSTANCE_TOKEN=${i3.INSTANCE_TOKEN}`,
@@ -35255,7 +35295,8 @@ function buildSuccessSummary(config, secrets) {
35255
35295
  return lines2.join(`
35256
35296
  `);
35257
35297
  }
35258
- async function runSetup(config, emit2) {
35298
+ async function runSetup(initialConfig, emit2) {
35299
+ let config = initialConfig;
35259
35300
  emit2({ type: "step-start", step: "preflight" });
35260
35301
  if (!await checkDocker()) {
35261
35302
  emit2({
@@ -35265,6 +35306,31 @@ async function runSetup(config, emit2) {
35265
35306
  });
35266
35307
  return { ok: false, config };
35267
35308
  }
35309
+ const claimedByDocker = dockerPublishedHostPorts();
35310
+ const ports = {
35311
+ api: await resolveAvailablePublishSpec(config.apiPort, claimedByDocker),
35312
+ indexer: await resolveAvailablePublishSpec(config.indexerPort, claimedByDocker),
35313
+ postgres: await resolveAvailablePublishSpec(config.postgresPort, claimedByDocker)
35314
+ };
35315
+ config = {
35316
+ ...config,
35317
+ apiPort: ports.api.spec,
35318
+ indexerPort: ports.indexer.spec,
35319
+ postgresPort: ports.postgres.spec
35320
+ };
35321
+ for (const [label, result] of [
35322
+ ["api", ports.api],
35323
+ ["indexer", ports.indexer],
35324
+ ["postgres", ports.postgres]
35325
+ ]) {
35326
+ if (result.remapped) {
35327
+ emit2({
35328
+ type: "step-log",
35329
+ step: "preflight",
35330
+ line: `${label} port was already in use locally, using ${result.spec} instead`
35331
+ });
35332
+ }
35333
+ }
35268
35334
  const floors = guardrailPreview(config.nodeMode, config.network);
35269
35335
  emit2({
35270
35336
  type: "step-done",
@@ -35512,6 +35578,7 @@ function App({ flags }) {
35512
35578
  nodeMode,
35513
35579
  apiPort: flags.apiPort ?? "127.0.0.1:3800",
35514
35580
  indexerPort: "127.0.0.1:3700",
35581
+ postgresPort: "127.0.0.1:5432",
35515
35582
  dir: flags.dir ?? process.cwd(),
35516
35583
  against: skipBootstrap ? undefined : against,
35517
35584
  skipBootstrap,
@@ -36032,7 +36099,7 @@ var {
36032
36099
  // package.json
36033
36100
  var package_default = {
36034
36101
  name: "@secondlayer/cli",
36035
- version: "13.0.0",
36102
+ version: "13.0.1",
36036
36103
  description: "Run a Stacks index on your own hardware — set it up, restore verified history, and keep it current.",
36037
36104
  repository: {
36038
36105
  type: "git",
@@ -41430,6 +41497,7 @@ async function promptSetupConfig(flags) {
41430
41497
  nodeMode,
41431
41498
  apiPort: flags.apiPort ?? "127.0.0.1:3800",
41432
41499
  indexerPort: "127.0.0.1:3700",
41500
+ postgresPort: "127.0.0.1:5432",
41433
41501
  dir: flags.dir ?? process.cwd(),
41434
41502
  against: skipBootstrap ? undefined : against,
41435
41503
  skipBootstrap,
@@ -41650,5 +41718,5 @@ program.commandsGroup("Archive:");
41650
41718
  registerCreditsCommand(program);
41651
41719
  program.parse();
41652
41720
 
41653
- //# debugId=9A62CC1B91BCEC2C64756E2164756E21
41721
+ //# debugId=E4B3B7874FAC14F764756E2164756E21
41654
41722
  //# sourceMappingURL=cli.js.map