peakroute 0.5.6 → 0.5.8

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.
@@ -633,15 +633,59 @@ var FRAMEWORKS_NEEDING_PORT = {
633
633
  astro: { strictPort: false },
634
634
  ng: { strictPort: false }
635
635
  };
636
- function injectFrameworkFlags(commandArgs, port) {
636
+ var PACKAGE_MANAGERS = /* @__PURE__ */ new Set(["npm", "yarn", "pnpm", "bun"]);
637
+ function detectFrameworkFromPackageJson(scriptName) {
638
+ try {
639
+ const pkgPath = path2.join(process.cwd(), "package.json");
640
+ if (!fs2.existsSync(pkgPath)) return null;
641
+ const pkg = JSON.parse(fs2.readFileSync(pkgPath, "utf-8"));
642
+ const script = pkg.scripts?.[scriptName];
643
+ if (!script) return null;
644
+ for (const [framework] of Object.entries(FRAMEWORKS_NEEDING_PORT)) {
645
+ const regex = new RegExp(
646
+ `(?:^|[\\s;&|/]|cross-env(?:\\s+[^\\s]+)*\\s+)${framework}(?=\\s|$)`,
647
+ "i"
648
+ );
649
+ if (regex.test(script)) {
650
+ return framework;
651
+ }
652
+ }
653
+ } catch {
654
+ }
655
+ return null;
656
+ }
657
+ function extractScriptName(commandArgs, basename2) {
658
+ const runIndex = commandArgs.indexOf("run");
659
+ if (runIndex !== -1 && runIndex + 1 < commandArgs.length) {
660
+ return commandArgs[runIndex + 1];
661
+ }
662
+ if (basename2 === "yarn" || basename2 === "bun" || basename2 === "pnpm") {
663
+ const scriptName = commandArgs[1];
664
+ if (scriptName && !scriptName.startsWith("-")) {
665
+ return scriptName;
666
+ }
667
+ }
668
+ return null;
669
+ }
670
+ function resolveFramework(commandArgs) {
637
671
  const cmd = commandArgs[0];
638
- if (!cmd) return;
672
+ if (!cmd) return null;
639
673
  const basename2 = path2.basename(cmd);
640
- const framework = FRAMEWORKS_NEEDING_PORT[basename2];
641
- if (!framework) return;
674
+ const directFramework = FRAMEWORKS_NEEDING_PORT[basename2];
675
+ if (directFramework) {
676
+ return { name: basename2, ...directFramework };
677
+ }
678
+ if (!PACKAGE_MANAGERS.has(basename2)) return null;
679
+ const scriptName = extractScriptName(commandArgs, basename2);
680
+ if (!scriptName) return null;
681
+ const detectedFramework = detectFrameworkFromPackageJson(scriptName);
682
+ if (!detectedFramework) return null;
683
+ return { name: detectedFramework, ...FRAMEWORKS_NEEDING_PORT[detectedFramework] };
684
+ }
685
+ function injectPortAndHostFlags(commandArgs, port, strictPort) {
642
686
  if (!commandArgs.includes("--port")) {
643
687
  commandArgs.push("--port", port.toString());
644
- if (framework.strictPort) {
688
+ if (strictPort) {
645
689
  commandArgs.push("--strictPort");
646
690
  }
647
691
  }
@@ -649,6 +693,20 @@ function injectFrameworkFlags(commandArgs, port) {
649
693
  commandArgs.push("--host", "127.0.0.1");
650
694
  }
651
695
  }
696
+ function injectFrameworkFlags(commandArgs, port, manualFramework) {
697
+ if (manualFramework === "force") {
698
+ injectPortAndHostFlags(commandArgs, port, false);
699
+ return;
700
+ }
701
+ if (manualFramework && FRAMEWORKS_NEEDING_PORT[manualFramework]) {
702
+ injectPortAndHostFlags(commandArgs, port, FRAMEWORKS_NEEDING_PORT[manualFramework].strictPort);
703
+ return;
704
+ }
705
+ const framework = resolveFramework(commandArgs);
706
+ if (framework) {
707
+ injectPortAndHostFlags(commandArgs, port, framework.strictPort);
708
+ }
709
+ }
652
710
  function prompt(question) {
653
711
  const rl = readline.createInterface({
654
712
  input: process.stdin,
@@ -662,6 +720,83 @@ function prompt(question) {
662
720
  });
663
721
  });
664
722
  }
723
+ var UPDATE_CHECK_CACHE_MS = 24 * 60 * 60 * 1e3;
724
+ function getUpdateCachePath() {
725
+ return path2.join(USER_STATE_DIR, ".update-check.json");
726
+ }
727
+ function compareVersions(v1, v2) {
728
+ const parts1 = v1.split(".").map(Number);
729
+ const parts2 = v2.split(".").map(Number);
730
+ const maxLen = Math.max(parts1.length, parts2.length);
731
+ for (let i = 0; i < maxLen; i++) {
732
+ const a = parts1[i] || 0;
733
+ const b = parts2[i] || 0;
734
+ if (a < b) return -1;
735
+ if (a > b) return 1;
736
+ }
737
+ return 0;
738
+ }
739
+ async function checkForUpdate(currentVersion) {
740
+ if (process.env.PEAKROUTE_NO_UPDATE_CHECK) {
741
+ return null;
742
+ }
743
+ const cachePath = getUpdateCachePath();
744
+ try {
745
+ const cache = JSON.parse(fs2.readFileSync(cachePath, "utf-8"));
746
+ if (Date.now() - cache.lastCheck < UPDATE_CHECK_CACHE_MS) {
747
+ if (compareVersions(cache.version, currentVersion) > 0) {
748
+ return cache.version;
749
+ }
750
+ return null;
751
+ }
752
+ } catch {
753
+ }
754
+ return new Promise((resolve) => {
755
+ const req = https.get(
756
+ "https://registry.npmjs.org/peakroute/latest",
757
+ {
758
+ timeout: 3e3,
759
+ headers: {
760
+ Accept: "application/json"
761
+ }
762
+ },
763
+ (res) => {
764
+ let data = "";
765
+ res.on("data", (chunk) => {
766
+ data += chunk;
767
+ });
768
+ res.on("end", () => {
769
+ try {
770
+ const response = JSON.parse(data);
771
+ const latestVersion = response.version;
772
+ const cache = {
773
+ version: latestVersion,
774
+ lastCheck: Date.now()
775
+ };
776
+ try {
777
+ fs2.mkdirSync(USER_STATE_DIR, { recursive: true });
778
+ fs2.writeFileSync(cachePath, JSON.stringify(cache), { mode: 420 });
779
+ } catch {
780
+ }
781
+ if (compareVersions(latestVersion, currentVersion) > 0) {
782
+ resolve(latestVersion);
783
+ } else {
784
+ resolve(null);
785
+ }
786
+ } catch {
787
+ resolve(null);
788
+ }
789
+ });
790
+ }
791
+ );
792
+ req.on("error", () => resolve(null));
793
+ req.on("timeout", () => {
794
+ req.destroy();
795
+ resolve(null);
796
+ });
797
+ req.setTimeout(3e3);
798
+ });
799
+ }
665
800
 
666
801
  // src/routes.ts
667
802
  import * as fs3 from "fs";
@@ -869,6 +1004,7 @@ export {
869
1004
  spawnCommand,
870
1005
  injectFrameworkFlags,
871
1006
  prompt,
1007
+ checkForUpdate,
872
1008
  FILE_MODE,
873
1009
  DIR_MODE,
874
1010
  SYSTEM_DIR_MODE,
package/dist/cli.js CHANGED
@@ -5,6 +5,7 @@ import {
5
5
  PRIVILEGED_PORT_THRESHOLD,
6
6
  RouteConflictError,
7
7
  RouteStore,
8
+ checkForUpdate,
8
9
  chmodSafe,
9
10
  createProxyServer,
10
11
  discoverState,
@@ -24,7 +25,7 @@ import {
24
25
  spawnCommand,
25
26
  waitForProxy,
26
27
  writeTlsMarker
27
- } from "./chunk-IVO6GF7V.js";
28
+ } from "./chunk-EWW65DJW.js";
28
29
 
29
30
  // src/cli.ts
30
31
  import chalk from "chalk";
@@ -676,7 +677,7 @@ function listRoutes(store, proxyPort, tls2) {
676
677
  }
677
678
  console.log();
678
679
  }
679
- async function runApp(store, proxyPort, stateDir, name, commandArgs, tls2, force) {
680
+ async function runApp(store, proxyPort, stateDir, name, commandArgs, tls2, force, inject) {
680
681
  const hostname = parseHostname(name);
681
682
  console.log(chalk.blue.bold(`
682
683
  peakroute
@@ -787,7 +788,7 @@ peakroute
787
788
  console.log(chalk.cyan.bold(`
788
789
  -> ${finalUrl}
789
790
  `));
790
- injectFrameworkFlags(commandArgs, port);
791
+ injectFrameworkFlags(commandArgs, port, inject ? "force" : void 0);
791
792
  console.log(chalk.gray(`Running: PORT=${port} HOST=127.0.0.1 ${commandArgs.join(" ")}
792
793
  `));
793
794
  spawnCommand(commandArgs, {
@@ -806,6 +807,7 @@ peakroute
806
807
  });
807
808
  }
808
809
  async function main() {
810
+ const updatePromise = checkForUpdate("0.5.8");
809
811
  if (process.stdin.isTTY) {
810
812
  process.on("exit", () => {
811
813
  try {
@@ -815,6 +817,15 @@ async function main() {
815
817
  });
816
818
  }
817
819
  const args = process.argv.slice(2);
820
+ const newerVersion = await updatePromise;
821
+ if (newerVersion) {
822
+ console.log(
823
+ chalk.yellow(`
824
+ \u2192 Update available: ${chalk.bold(newerVersion)} (current: ${"0.5.8"})`)
825
+ );
826
+ console.log(chalk.gray(` Run: npm install -g peakroute
827
+ `));
828
+ }
818
829
  const isNpx = process.env.npm_command === "exec" && !process.env.npm_lifecycle_event;
819
830
  const isPnpmDlx = !!process.env.PNPM_SCRIPT_SRC_DIR && !process.env.npm_lifecycle_event;
820
831
  if (isNpx || isPnpmDlx) {
@@ -884,6 +895,7 @@ ${chalk.bold("Options:")}
884
895
  --no-tls Disable HTTPS (overrides PEAKROUTE_HTTPS)
885
896
  --foreground Run proxy in foreground (for debugging)
886
897
  --force Override an existing route registered by another process
898
+ --inject Force injection of --port and --host flags (when auto-detection fails)
887
899
 
888
900
  ${chalk.bold("Environment variables:")}
889
901
  PEAKROUTE_PORT=<number> Override the default proxy port (e.g. in .bashrc)
@@ -898,7 +910,7 @@ ${chalk.bold("Skip peakroute:")}
898
910
  process.exit(0);
899
911
  }
900
912
  if (args[0] === "--version" || args[0] === "-v") {
901
- console.log("0.5.6");
913
+ console.log("0.5.8");
902
914
  process.exit(0);
903
915
  }
904
916
  if (args[0] === "trust") {
@@ -1135,7 +1147,9 @@ ${chalk.bold("Usage: peakroute proxy <command>")}
1135
1147
  }
1136
1148
  const forceIdx = args.indexOf("--force");
1137
1149
  const force = forceIdx >= 0 && forceIdx <= 1;
1138
- const appArgs = force ? [...args.slice(0, forceIdx), ...args.slice(forceIdx + 1)] : args;
1150
+ const injectIdx = args.indexOf("--inject");
1151
+ const injectFlag = injectIdx >= 0 && injectIdx <= 1;
1152
+ const appArgs = args.filter((_, i) => i !== forceIdx && i !== injectIdx);
1139
1153
  const name = appArgs[0];
1140
1154
  const commandArgs = appArgs.slice(1);
1141
1155
  if (commandArgs.length === 0) {
@@ -1150,7 +1164,7 @@ ${chalk.bold("Usage: peakroute proxy <command>")}
1150
1164
  const store = new RouteStore(dir, {
1151
1165
  onWarning: (msg) => console.warn(chalk.yellow(msg))
1152
1166
  });
1153
- await runApp(store, port, dir, name, commandArgs, tls2, force);
1167
+ await runApp(store, port, dir, name, commandArgs, tls2, force, injectFlag);
1154
1168
  }
1155
1169
  main().catch((err) => {
1156
1170
  const message = err instanceof Error ? err.message : String(err);
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  formatUrl,
15
15
  isErrnoException,
16
16
  parseHostname
17
- } from "./chunk-IVO6GF7V.js";
17
+ } from "./chunk-EWW65DJW.js";
18
18
  export {
19
19
  DIR_MODE,
20
20
  FILE_MODE,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "peakroute",
3
- "version": "0.5.6",
3
+ "version": "0.5.8",
4
4
  "description": "Replace port numbers with stable, named .localhost URLs. For humans and agents. (Formerly portless)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",