lianrouter 2.3.2 → 2.3.4

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 (2) hide show
  1. package/dist/cli.js +156 -29
  2. package/package.json +4 -1
package/dist/cli.js CHANGED
@@ -17,7 +17,7 @@ var package_default;
17
17
  var init_package = __esm(() => {
18
18
  package_default = {
19
19
  name: "lianrouter",
20
- version: "2.3.2",
20
+ version: "2.3.4",
21
21
  description: "LianRouter launcher: install, run and control the gateway from one command",
22
22
  type: "module",
23
23
  bin: {
@@ -36,6 +36,9 @@ var init_package = __esm(() => {
36
36
  postinstall: "echo lianrouter: the gateway binary installs on first run, not during this install",
37
37
  prepublishOnly: "bun run build"
38
38
  },
39
+ dependencies: {
40
+ "embedded-postgres": "18.6.0-beta.19"
41
+ },
39
42
  repository: {
40
43
  type: "git",
41
44
  url: "git+https://github.com/ReXooGen/Lian-Router.git",
@@ -1593,8 +1596,8 @@ var init_app = __esm(async () => {
1593
1596
 
1594
1597
  // cli/src/launcher.ts
1595
1598
  init_version();
1596
- import { existsSync as existsSync5 } from "fs";
1597
- import { join as join8 } from "path";
1599
+ import { existsSync as existsSync6 } from "fs";
1600
+ import { join as join9 } from "path";
1598
1601
 
1599
1602
  // cli/src/bootstrap.ts
1600
1603
  init_download();
@@ -2186,6 +2189,114 @@ Select [1-${items.length}]: `);
2186
2189
  // cli/src/launcher.ts
2187
2190
  init_paths();
2188
2191
 
2192
+ // cli/src/embedded-database.ts
2193
+ import { spawn } from "child_process";
2194
+ import { existsSync as existsSync3 } from "fs";
2195
+ import { join as join7 } from "path";
2196
+ var EMBEDDED_DATABASE_PORT = 55433;
2197
+ var EMBEDDED_DATABASE_USER = "postgres";
2198
+ var EMBEDDED_DATABASE_PASSWORD = "postgres";
2199
+ var EMBEDDED_DATABASE_NAME = "lianrouter";
2200
+ function embeddedDatabaseUrl(port = EMBEDDED_DATABASE_PORT) {
2201
+ return `postgres://${EMBEDDED_DATABASE_USER}:${EMBEDDED_DATABASE_PASSWORD}@127.0.0.1:${port}/${EMBEDDED_DATABASE_NAME}`;
2202
+ }
2203
+ function resolveEmbeddedDatabaseDir(installRoot) {
2204
+ return join7(installRoot, "pgdata");
2205
+ }
2206
+
2207
+ class EmbeddedDatabaseUnavailableError extends Error {
2208
+ constructor(message) {
2209
+ super(message);
2210
+ this.name = "EmbeddedDatabaseUnavailableError";
2211
+ }
2212
+ }
2213
+ async function resolvePlatformBinary() {
2214
+ const platform = process.platform;
2215
+ const arch = process.arch;
2216
+ const packageName = platform === "win32" && arch === "x64" ? "@embedded-postgres/windows-x64" : platform === "darwin" && arch === "arm64" ? "@embedded-postgres/darwin-arm64" : platform === "darwin" && arch === "x64" ? "@embedded-postgres/darwin-x64" : platform === "linux" && arch === "x64" ? "@embedded-postgres/linux-x64" : platform === "linux" && arch === "arm64" ? "@embedded-postgres/linux-arm64" : platform === "linux" && arch === "arm" ? "@embedded-postgres/linux-arm" : undefined;
2217
+ if (packageName === undefined) {
2218
+ return;
2219
+ }
2220
+ try {
2221
+ const resolved = import.meta.resolve(packageName);
2222
+ const entryPath = resolved.startsWith("file:") ? new URL(resolved).pathname : resolved;
2223
+ const index = entryPath.lastIndexOf(`${packageName.replace("/", "/")}`);
2224
+ if (index === -1) {
2225
+ return;
2226
+ }
2227
+ const packageRoot = entryPath.slice(0, index + packageName.length);
2228
+ return { dir: packageRoot };
2229
+ } catch {
2230
+ return;
2231
+ }
2232
+ }
2233
+ async function stopOrphanCluster(databaseDir) {
2234
+ const dataDirectory = databaseDir;
2235
+ if (!existsSync3(join7(dataDirectory, "PG_VERSION"))) {
2236
+ return false;
2237
+ }
2238
+ const binary = await resolvePlatformBinary();
2239
+ if (binary === undefined) {
2240
+ return false;
2241
+ }
2242
+ const pgCtl = join7(binary.dir, "native", "bin", process.platform === "win32" ? "pg_ctl.exe" : "pg_ctl");
2243
+ if (!existsSync3(pgCtl)) {
2244
+ return false;
2245
+ }
2246
+ const { promise, resolve: settle } = Promise.withResolvers();
2247
+ const child = spawn(pgCtl, ["stop", "-D", dataDirectory, "-m", "fast", "-w"], {
2248
+ stdio: "ignore"
2249
+ });
2250
+ child.once("error", () => settle(false));
2251
+ child.once("exit", (code) => settle(code === 0));
2252
+ return await promise;
2253
+ }
2254
+ async function startEmbeddedDatabase(options) {
2255
+ const port = options.port ?? EMBEDDED_DATABASE_PORT;
2256
+ const databaseDir = resolveEmbeddedDatabaseDir(options.installRoot);
2257
+ let EmbeddedPostgres;
2258
+ try {
2259
+ ({ default: EmbeddedPostgres } = await import("embedded-postgres"));
2260
+ } catch (error) {
2261
+ const reason = error instanceof Error ? error.message : String(error);
2262
+ throw new EmbeddedDatabaseUnavailableError(`DATABASE_MODE=embedded needs the \`embedded-postgres\` package, which is not installed here (${reason}). Reinstall the launcher, or set DATABASE_URL to a PostgreSQL server you already run.`);
2263
+ }
2264
+ const binary = await resolvePlatformBinary();
2265
+ if (binary === undefined) {
2266
+ throw new EmbeddedDatabaseUnavailableError(`DATABASE_MODE=embedded has no PostgreSQL binaries for ${process.platform}-${process.arch}. Set DATABASE_URL to a PostgreSQL server instead.`);
2267
+ }
2268
+ const log = options.onLog;
2269
+ const server = new EmbeddedPostgres({
2270
+ databaseDir,
2271
+ user: EMBEDDED_DATABASE_USER,
2272
+ password: EMBEDDED_DATABASE_PASSWORD,
2273
+ port,
2274
+ persistent: true,
2275
+ ...log === undefined ? {} : { onLog: log, onError: (message) => log(String(message)) }
2276
+ });
2277
+ if (!existsSync3(join7(databaseDir, "PG_VERSION"))) {
2278
+ await server.initialise();
2279
+ } else if (await stopOrphanCluster(databaseDir)) {
2280
+ log?.("[launcher] reaped an embedded PostgreSQL cluster left by a previous run");
2281
+ }
2282
+ await server.start();
2283
+ try {
2284
+ await server.createDatabase(EMBEDDED_DATABASE_NAME);
2285
+ } catch {}
2286
+ const url = embeddedDatabaseUrl(port);
2287
+ let stopping;
2288
+ return {
2289
+ url,
2290
+ port,
2291
+ stop() {
2292
+ stopping ??= server.stop().catch(() => {
2293
+ return;
2294
+ });
2295
+ return stopping;
2296
+ }
2297
+ };
2298
+ }
2299
+
2189
2300
  // cli/src/shutdown.ts
2190
2301
  init_paths();
2191
2302
  import { rmSync } from "fs";
@@ -2683,8 +2794,8 @@ class GatewaySupervisor {
2683
2794
  // cli/src/install-config.ts
2684
2795
  init_paths();
2685
2796
  import { randomBytes } from "crypto";
2686
- import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync } from "fs";
2687
- var ALWAYS_REQUIRED = ["DATABASE_URL", "LIANROUTER_ENCRYPTION_KEY"];
2797
+ import { existsSync as existsSync4, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync } from "fs";
2798
+ var ALWAYS_REQUIRED = ["LIANROUTER_ENCRYPTION_KEY"];
2688
2799
  function parseEnvFile(text) {
2689
2800
  const values = {};
2690
2801
  for (const rawLine of text.split(/\r?\n/)) {
@@ -2713,7 +2824,7 @@ function parseEnvFile(text) {
2713
2824
  }
2714
2825
  function resolveGatewayConfig(installRoot, shellEnv = process.env) {
2715
2826
  const path = resolveConfigFile(installRoot);
2716
- const present = existsSync3(path);
2827
+ const present = existsSync4(path);
2717
2828
  const fileValues = present ? parseEnvFile(readFileSync2(path, "utf8")) : {};
2718
2829
  const env = {};
2719
2830
  for (const [key, value] of Object.entries(fileValues)) {
@@ -2733,12 +2844,16 @@ function resolveGatewayConfig(installRoot, shellEnv = process.env) {
2733
2844
  const fromFile = fileValues[name];
2734
2845
  return fromFile !== undefined && fromFile.trim() !== "" ? fromFile.trim() : undefined;
2735
2846
  };
2847
+ const databaseMode = (effective("DATABASE_MODE") ?? "url") === "embedded" ? "embedded" : "url";
2736
2848
  const required = [...ALWAYS_REQUIRED];
2849
+ if (databaseMode !== "embedded") {
2850
+ required.push("DATABASE_URL");
2851
+ }
2737
2852
  if ((effective("REDIS_MODE") ?? "normal") === "normal") {
2738
2853
  required.push("REDIS_URL");
2739
2854
  }
2740
2855
  const missing = required.filter((name) => effective(name) === undefined);
2741
- return { path, present, env, missing };
2856
+ return { path, present, env, missing, databaseMode };
2742
2857
  }
2743
2858
  function describeMissingConfig(config) {
2744
2859
  const location = config.present ? ` config file: ${config.path}` : ` config file: ${config.path} (not found)`;
@@ -2758,8 +2873,11 @@ function describeMissingConfig(config) {
2758
2873
  "or set them in your shell before starting; a shell value wins over the file.",
2759
2874
  `run \`lianrouter init\` to write a starter file at ${config.path}`
2760
2875
  ];
2876
+ if (config.missing.includes("DATABASE_URL")) {
2877
+ lines.push("to have the launcher start a private PostgreSQL for you, set DATABASE_MODE=embedded instead of DATABASE_URL");
2878
+ }
2761
2879
  if (config.missing.includes("REDIS_URL")) {
2762
- lines.push("to run a single gateway process without Redis, set REDIS_MODE=single_instance_local instead of REDIS_URL");
2880
+ lines.push("to run without a Redis server, set REDIS_MODE=embedded (in-process) or REDIS_MODE=single_instance_local (no Redis at all, and no console)");
2763
2881
  }
2764
2882
  return lines.join(`
2765
2883
  `);
@@ -2775,10 +2893,12 @@ function starterConfigText(encryptionKey) {
2775
2893
  # A variable already set in your shell wins over the value written here.
2776
2894
  # Generated by \`lianrouter init\`.
2777
2895
 
2778
- # PostgreSQL connection string. Required.
2779
- # Replace the placeholder below and remove the leading "# ". The gateway never
2780
- # infers a Docker or Laragon connection.
2896
+ # Database. DATABASE_MODE=embedded has the launcher start a private PostgreSQL
2897
+ # for this install and hand the gateway a DATABASE_URL pointing at it \u2014 nothing
2898
+ # to install, nothing to configure. To use your own server instead, delete the
2899
+ # DATABASE_MODE line and set DATABASE_URL:
2781
2900
  # DATABASE_URL=postgres://user:password@localhost:5432/lianrouter
2901
+ DATABASE_MODE=embedded
2782
2902
 
2783
2903
  # 256-bit key for encrypting provider credentials and hashing API keys.
2784
2904
  # Required. Base64 (44 characters) or hex (64 characters); it must decode to
@@ -2786,10 +2906,10 @@ function starterConfigText(encryptionKey) {
2786
2906
  # credentials encrypted with the old one.
2787
2907
  LIANROUTER_ENCRYPTION_KEY=${encryptionKey}
2788
2908
 
2789
- # Redis coordination. REDIS_URL is required in the default mode; set
2790
- # REDIS_MODE=single_instance_local to run a single gateway process without
2791
- # Redis at all.
2792
- REDIS_MODE=normal
2909
+ # Redis coordination. \`embedded\` runs a Redis client inside the gateway process,
2910
+ # which is what a single-machine install wants; set REDIS_URL (with
2911
+ # REDIS_MODE=normal) to share coordination state with another process.
2912
+ REDIS_MODE=embedded
2793
2913
  # REDIS_URL=redis://localhost:6379
2794
2914
  `;
2795
2915
  }
@@ -2947,19 +3067,19 @@ async function checkLauncherVersion(options) {
2947
3067
 
2948
3068
  // cli/src/tray-launcher.ts
2949
3069
  init_paths();
2950
- import { existsSync as existsSync4 } from "fs";
3070
+ import { existsSync as existsSync5 } from "fs";
2951
3071
  import { readFile as readFile2, rm as rm8, writeFile as writeFile3 } from "fs/promises";
2952
- import { join as join7 } from "path";
3072
+ import { join as join8 } from "path";
2953
3073
  function resolveTrayEntry(options) {
2954
- return trayEntryCandidates(options).find((candidate) => existsSync4(candidate));
3074
+ return trayEntryCandidates(options).find((candidate) => existsSync5(candidate));
2955
3075
  }
2956
3076
  function trayEntryCandidates(options) {
2957
3077
  const bundleDir = options.bundleDir ?? moduleDir;
2958
3078
  const candidates = [];
2959
3079
  if (options.devRepoRoot !== undefined) {
2960
- candidates.push(join7(options.devRepoRoot, "cli", "dist", "tray.js"));
3080
+ candidates.push(join8(options.devRepoRoot, "cli", "dist", "tray.js"));
2961
3081
  }
2962
- candidates.push(join7(bundleDir, "tray.js"));
3082
+ candidates.push(join8(bundleDir, "tray.js"));
2963
3083
  return candidates;
2964
3084
  }
2965
3085
  async function startTray(options) {
@@ -3205,7 +3325,7 @@ function readPortEnv() {
3205
3325
  }
3206
3326
  function resolveRuntime() {
3207
3327
  const repoRoot = resolveDevRepoRoot();
3208
- if (repoRoot !== undefined && existsSync5(join8(repoRoot, "src", "app.ts"))) {
3328
+ if (repoRoot !== undefined && existsSync6(join9(repoRoot, "src", "app.ts"))) {
3209
3329
  return {
3210
3330
  installRoot: resolveInstallRoot(),
3211
3331
  cwd: repoRoot,
@@ -3217,7 +3337,7 @@ function resolveRuntime() {
3217
3337
  return { installRoot, cwd: installRoot, command: undefined, devRepoRoot: undefined };
3218
3338
  }
3219
3339
  async function ensureInstalledGateway(installRoot) {
3220
- const present = existsSync5(resolveBinaryPath(installRoot));
3340
+ const present = existsSync6(resolveBinaryPath(installRoot));
3221
3341
  if (present) {
3222
3342
  await ensureGatewayInstalled({ installRoot, currentVersion: CLI_VERSION });
3223
3343
  return;
@@ -3254,7 +3374,7 @@ async function run(argv) {
3254
3374
  if (options.command === "init") {
3255
3375
  const path = writeStarterConfig(runtime.installRoot);
3256
3376
  console.log(`Wrote ${path}`);
3257
- console.log("Set DATABASE_URL in that file (uncomment the line), then run lianrouter.");
3377
+ console.log("It starts a private PostgreSQL for this install (DATABASE_MODE=embedded) and an " + "in-process Redis, so `lianrouter` works as-is. Point DATABASE_URL at your own " + "server instead if you prefer.");
3258
3378
  return 0;
3259
3379
  }
3260
3380
  const startsGateway = options.command === undefined || options.command === "start" || options.command === "tray" || options.command === "tui";
@@ -3262,6 +3382,11 @@ async function run(argv) {
3262
3382
  if (gatewayConfig !== undefined) {
3263
3383
  assertGatewayConfigured(gatewayConfig);
3264
3384
  }
3385
+ const embeddedDatabase = gatewayConfig?.databaseMode === "embedded" && startsGateway ? await startEmbeddedDatabase({ installRoot: runtime.installRoot }) : undefined;
3386
+ if (embeddedDatabase !== undefined && options.background) {
3387
+ await embeddedDatabase.stop();
3388
+ throw new Error("DATABASE_MODE=embedded keeps PostgreSQL inside this process, so --background " + "cannot return and leave it running. Use `lianrouter tray` for a background " + "install with an icon, or set DATABASE_URL to a server that outlives the launcher.");
3389
+ }
3265
3390
  if (runtime.command === undefined && options.command !== "status" && options.command !== "stop") {
3266
3391
  await ensureInstalledGateway(runtime.installRoot);
3267
3392
  }
@@ -3272,7 +3397,8 @@ async function run(argv) {
3272
3397
  cwd: runtime.cwd,
3273
3398
  env: {
3274
3399
  LIANROUTER_LAUNCHER_HOME: runtime.installRoot,
3275
- ...gatewayConfig?.env ?? {}
3400
+ ...gatewayConfig?.env ?? {},
3401
+ ...embeddedDatabase === undefined ? {} : { DATABASE_URL: embeddedDatabase.url }
3276
3402
  },
3277
3403
  ...runtime.command === undefined ? {} : { command: runtime.command }
3278
3404
  });
@@ -3282,21 +3408,21 @@ async function run(argv) {
3282
3408
  case "stop":
3283
3409
  return await stopServer(supervisor);
3284
3410
  case "start":
3285
- return await withShutdownHandlers(supervisor, runtime, () => runInteractive(supervisor, options, runtime));
3411
+ return await withShutdownHandlers(supervisor, runtime, () => runInteractive(supervisor, options, runtime), embeddedDatabase);
3286
3412
  case "tray":
3287
- return await withShutdownHandlers(supervisor, runtime, () => enterTrayMode(supervisor, options, runtime));
3413
+ return await withShutdownHandlers(supervisor, runtime, () => enterTrayMode(supervisor, options, runtime), embeddedDatabase);
3288
3414
  case "tui":
3289
- return await withShutdownHandlers(supervisor, runtime, () => runTui2(supervisor, options, runtime));
3415
+ return await withShutdownHandlers(supervisor, runtime, () => runTui2(supervisor, options, runtime), embeddedDatabase);
3290
3416
  case "update":
3291
3417
  return await runUpdateFlow(supervisor, runtime);
3292
3418
  case undefined:
3293
- return await withShutdownHandlers(supervisor, runtime, () => runInteractive(supervisor, options, runtime));
3419
+ return await withShutdownHandlers(supervisor, runtime, () => runInteractive(supervisor, options, runtime), embeddedDatabase);
3294
3420
  default:
3295
3421
  throw new Error(`unknown command ${options.command}`);
3296
3422
  }
3297
3423
  }
3298
3424
  var activeControl;
3299
- async function withShutdownHandlers(supervisor, runtime, command) {
3425
+ async function withShutdownHandlers(supervisor, runtime, command, embeddedDatabase) {
3300
3426
  const registration = installShutdownHandlers(() => shutdownSessionFiles({
3301
3427
  installRoot: runtime.installRoot,
3302
3428
  stopControl: async () => {
@@ -3307,6 +3433,7 @@ async function withShutdownHandlers(supervisor, runtime, command) {
3307
3433
  stopGateway: async () => {
3308
3434
  await supervisor.stop();
3309
3435
  await supervisor.dispose();
3436
+ await embeddedDatabase?.stop();
3310
3437
  }
3311
3438
  }), (code) => process.exit(code));
3312
3439
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lianrouter",
3
- "version": "2.3.2",
3
+ "version": "2.3.4",
4
4
  "description": "LianRouter launcher: install, run and control the gateway from one command",
5
5
  "type": "module",
6
6
  "bin": {
@@ -19,6 +19,9 @@
19
19
  "postinstall": "echo lianrouter: the gateway binary installs on first run, not during this install",
20
20
  "prepublishOnly": "bun run build"
21
21
  },
22
+ "dependencies": {
23
+ "embedded-postgres": "18.6.0-beta.19"
24
+ },
22
25
  "repository": {
23
26
  "type": "git",
24
27
  "url": "git+https://github.com/ReXooGen/Lian-Router.git",