fbi-proxy 1.4.0 → 1.6.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/ts/cli.ts CHANGED
@@ -1,49 +1,43 @@
1
1
  #!/usr/bin/env bun
2
2
  import getPort from "get-port";
3
3
  import hotMemo from "hot-memo";
4
- import minimist from "minimist";
5
4
  import path from "path";
6
- import promiseAllProperties from "promise-all-properties";
7
- import { buildFbiProxy } from "./buildFbiProxy";
8
- import { $ } from "./dRun";
5
+ import yargs from "yargs";
6
+ import { hideBin } from "yargs/helpers";
7
+ import { getFbiProxyBinary } from "./buildFbiProxy";
8
+ import { $ } from "./dSpawn";
9
9
  import { downloadCaddy } from "./downloadCaddy";
10
+ import { execa } from "execa";
10
11
 
11
12
  process.chdir(path.resolve(__dirname, "..")); // Change to project root directory
12
13
 
13
- console.log("Preparing Binaries");
14
-
15
- const { proxy, caddy } = await promiseAllProperties({
16
- proxy: buildFbiProxy(),
17
- caddy: downloadCaddy(),
18
- });
14
+ // Parse command line arguments with yargs
15
+ const argv = await yargs(hideBin(process.argv))
16
+ .option("fbihost", {
17
+ type: "string",
18
+ default: "fbi.com",
19
+ description: "Set the FBI host",
20
+ })
21
+ .option("caddy", {
22
+ type: "boolean",
23
+ default: false,
24
+ description: "Start Caddy server",
25
+ })
26
+ .option("dev", {
27
+ alias: "d",
28
+ type: "boolean",
29
+ default: false,
30
+ description: "Run in development mode",
31
+ })
32
+ .help().argv;
19
33
 
20
- console.log("Running fbi-proxy", JSON.stringify({ caddy, proxy }));
34
+ console.log("Preparing Binaries");
21
35
 
22
- // assume caddy is installed, launch proxy server now
23
- const argv = minimist(process.argv.slice(2), {
24
- default: {
25
- dev: false,
26
- d: false,
27
- fbihost: "fbi.com", // Default FBI host
28
- },
29
- alias: {
30
- dev: "d",
31
- },
32
- boolean: ["dev", "d", "help"],
33
- });
34
- // console.log(argv);
35
- if (argv.help) {
36
- console.log(`Usage: fbi-proxy [options]
37
- Options:
38
- --help Show this help message
39
- --fbihost Set the FBI host (default: fbi.com)
40
- `);
41
- process.exit(0);
42
- }
36
+ const FBIHOST = argv.fbihost;
37
+ const FBIPROXY_PORT = String(await getPort({ port: 2432 }));
43
38
 
44
- const FBIHOST = argv.fbihost || "fbi.com"; // Default FBI host
45
- const FBIPROXY_PORT = String(await getPort({ port: 24306 }));
46
39
  const proxyProcess = await hotMemo(async () => {
40
+ const proxy = await getFbiProxyBinary();
47
41
  console.log("Starting Rust proxy server");
48
42
  const p = $.opt({
49
43
  env: {
@@ -59,25 +53,35 @@ const proxyProcess = await hotMemo(async () => {
59
53
  return p;
60
54
  });
61
55
 
62
- const caddyProcess = await hotMemo(async () => {
63
- const p = $.opt({
64
- env: {
65
- ...process.env,
66
- FBIPROXY_PORT, // Rust proxy server port
67
- FBIHOST,
68
- },
69
- })`${caddy} run`.process;
70
- p.on("exit", (code) => {
71
- console.log(`Caddy exited with code ${code}`);
72
- process.exit(code || 0);
56
+ let caddyProcess: any = null;
57
+
58
+ // Only start Caddy if --caddy flag is passed
59
+ if (argv.caddy) {
60
+ const caddy = await downloadCaddy();
61
+ caddyProcess = await hotMemo(async () => {
62
+ const p = $.opt({
63
+ env: {
64
+ ...process.env,
65
+ FBIPROXY_PORT, // Rust proxy server port
66
+ FBIHOST,
67
+ },
68
+ })`${caddy} run`.process;
69
+ p.on("exit", (code) => {
70
+ console.log(`Caddy exited with code ${code}`);
71
+ process.exit(code || 0);
72
+ });
73
+ return p;
73
74
  });
74
- return p;
75
- });
75
+ }
76
76
 
77
- console.log("all done");
77
+ console.log("All services started successfully!");
78
78
  // show process pids
79
79
  console.log(`Proxy server PID: ${proxyProcess.pid}`);
80
- console.log(`Caddy server PID: ${caddyProcess.pid}`);
80
+ if (caddyProcess) {
81
+ console.log(`Caddy server PID: ${caddyProcess.pid}`);
82
+ } else {
83
+ console.log("Caddy server not started (use --caddy to start it)");
84
+ }
81
85
 
82
86
  const exit = () => {
83
87
  console.log("Shutting down...");
@@ -15,17 +15,18 @@ type DRunProc = Promise<{
15
15
  process: ChildProcess;
16
16
  };
17
17
 
18
- const dRun = ({
18
+ const dSpawn = ({
19
19
  cwd = process.cwd(),
20
20
  env = process.env as Record<string, string>,
21
21
  } = {}) =>
22
22
  tsaComposer(
23
- (s: string | { raw: string }) =>
24
- typeof s === "string"
23
+ // slot is un dividable
24
+ (slot: string | { raw: string }) =>
25
+ typeof slot === "string"
25
26
  ? {
26
- raw: String(s), // todo: escape quotes
27
+ raw: String(slot), // todo: escape quotes
27
28
  }
28
- : s,
29
+ : slot,
29
30
  (...slots): DRunProc => {
30
31
  try {
31
32
  // TODO: parse slots for quotes
@@ -106,7 +107,7 @@ const dRun = ({
106
107
  },
107
108
  );
108
109
 
109
- export const $ = Object.assign(dRun(), {
110
- opt: dRun,
111
- cwd: (path: string) => dRun({ cwd: path }),
110
+ export const $ = Object.assign(dSpawn(), {
111
+ opt: dSpawn,
112
+ cwd: (path: string) => dSpawn({ cwd: path }),
112
113
  });
@@ -1,31 +1,24 @@
1
1
  import { existsSync } from "fs";
2
- import fsp from "fs/promises";
3
- import { $ } from "./dRun";
2
+ import { $ } from "./dSpawn";
4
3
 
5
- export const downloadCaddy = async () => {
6
- // use pwdCaddy if already downloaded
7
- const pwdCaddy = "./caddy";
4
+ if (import.meta.main) {
5
+ // if this file is run directly, download caddy
6
+ const caddy = await downloadCaddy();
7
+ console.log(`Caddy downloaded to: ${caddy}`);
8
+ process.exit(0);
9
+ }
8
10
 
9
- // if ./caddy exists in pwd, return it
11
+ export async function downloadCaddy() {
12
+ // use pwdCaddy if already downloaded
13
+ const pwdCaddy = "./node_modules/.bin/caddy";
10
14
  if (existsSync(pwdCaddy)) return pwdCaddy;
11
15
 
12
- // or use system caddy if installed, run `caddy --version` to check
16
+ // // or use system caddy if installed, run `caddy --version` to check
13
17
  if (await $`caddy --version`.catch(() => false)) {
14
18
  return "caddy";
15
19
  }
16
20
 
17
- // or if system caddy is not installed, download caddy using caddy-baron
18
- if (!existsSync(pwdCaddy)) {
19
- // download latest caddy to ./caddy
20
- console.log("Downloading Caddy...");
21
- // @ts-ignore
22
- await import("../node_modules/caddy-baron/index.mjs");
23
-
24
- if (!existsSync(pwdCaddy))
25
- throw new Error(
26
- "Failed to download Caddy. Please install Caddy manually or check your network connection.",
27
- );
28
- }
29
-
30
- return pwdCaddy;
31
- };
21
+ throw new Error(
22
+ "Failed to download Caddy. Please install Caddy manually or check your network connection.",
23
+ );
24
+ }
@@ -1,4 +1,4 @@
1
- export function getProxyFilename() {
1
+ export function getFbiProxyFilename() {
2
2
  return {
3
3
  "darwin-arm64": "fbi-proxy-darwin",
4
4
  "darwin-x64": "fbi-proxy-darwin",