@farm.js/cli 0.1.0-beta.18 → 0.1.0-beta.20

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/index.js CHANGED
@@ -451,10 +451,12 @@ const HOP_BY_HOP_HEADERS = /* @__PURE__ */ new Set([
451
451
  function createPreviewGatewayPlan(target, options = {}) {
452
452
  const requestedName = sanitizePreviewName$1(options.name || process.env.FARM_PREVIEW_NAME) || randomPreviewName$1();
453
453
  const gatewayUrl = normalizeGatewayUrl(options.gatewayUrl || process.env.FARM_PREVIEW_GATEWAY_URL || DEFAULT_GATEWAY_URL);
454
+ const relayUrl = normalizeRelayUrl(process.env.FARM_PREVIEW_RELAY_URL || gatewayUrl);
454
455
  const requestedHostname = `${requestedName}.${normalizePreviewDomain$1(process.env.FARM_PREVIEW_DOMAIN || DEFAULT_PREVIEW_DOMAIN)}`;
455
456
  return {
456
457
  provider: "farm-gateway",
457
458
  gatewayUrl,
459
+ relayUrl,
458
460
  target,
459
461
  requestedName,
460
462
  requestedHostname,
@@ -619,6 +621,7 @@ async function closeGatewaySession(plan, session) {
619
621
  function formatGatewayPlan(plan) {
620
622
  return [
621
623
  `Gateway: ${plan.gatewayUrl}`,
624
+ `Relay: ${plan.relayUrl}`,
622
625
  `Local: ${plan.target.localUrl}`,
623
626
  `Public: ${plan.requestedPublicUrl}`
624
627
  ].join("\n");
@@ -630,6 +633,17 @@ function formatRequestPath(path) {
630
633
  function normalizeGatewayUrl(value) {
631
634
  return value.replace(/\/+$/, "");
632
635
  }
636
+ function normalizeRelayUrl(value) {
637
+ const url = new URL(value);
638
+ if (url.protocol === "http:") url.protocol = "ws:";
639
+ if (url.protocol === "https:") url.protocol = "wss:";
640
+ if (url.protocol !== "ws:" && url.protocol !== "wss:") throw new Error(`Preview relay must use ws or wss, received ${url.protocol}`);
641
+ const pathname = url.pathname.replace(/\/+$/, "");
642
+ url.pathname = pathname.endsWith("/agent") ? pathname : `${pathname}/agent`;
643
+ url.search = "";
644
+ url.hash = "";
645
+ return url.toString();
646
+ }
633
647
  function normalizePreviewDomain$1(value) {
634
648
  return value.replace(/^https?:\/\//, "").replace(/^\.*/, "").replace(/\/*$/, "");
635
649
  }
@@ -641,6 +655,39 @@ function randomPreviewName$1() {
641
655
  return `farm-${Math.random().toString(36).slice(2, 8)}`;
642
656
  }
643
657
  //#endregion
658
+ //#region src/preview-native.ts
659
+ async function runNativePreviewTunnel(plan, options = {}) {
660
+ const runtime = options.runtime || await loadNativeTunnel();
661
+ const session = await runtime.startPreviewAgent(plan.relayUrl, plan.requestedName, plan.target.localUrl);
662
+ let stopping = false;
663
+ const stop = () => {
664
+ if (stopping) return;
665
+ stopping = true;
666
+ runtime.stopPreviewAgent(session.sessionId);
667
+ };
668
+ process.once("SIGINT", stop);
669
+ process.once("SIGTERM", stop);
670
+ _farm_js_core.logger.success("Preview URL ready.");
671
+ _farm_js_core.logger.info(`Public: ${session.publicUrl}`);
672
+ _farm_js_core.logger.info("Forwarding requests through the native tunnel until Ctrl+C.");
673
+ try {
674
+ if (!await runtime.waitPreviewAgent(session.sessionId)) throw new Error("The native preview tunnel stopped before its lifecycle could be observed.");
675
+ return session;
676
+ } finally {
677
+ process.removeListener("SIGINT", stop);
678
+ process.removeListener("SIGTERM", stop);
679
+ await runtime.stopPreviewAgent(session.sessionId).catch(() => false);
680
+ }
681
+ }
682
+ async function loadNativeTunnel() {
683
+ try {
684
+ return await import("@farm.js/tunnel");
685
+ } catch (error) {
686
+ const reason = error instanceof Error ? ` ${error.message}` : "";
687
+ throw new Error(`Could not load @farm.js/tunnel for this platform. Reinstall @farm.js/cli so its native platform package is restored.${reason}`);
688
+ }
689
+ }
690
+ //#endregion
644
691
  //#region src/preview.ts
645
692
  const DEFAULT_PREVIEW_PORTS = [
646
693
  3e3,
@@ -664,15 +711,26 @@ async function previewFarm(options = {}) {
664
711
  plan
665
712
  };
666
713
  }
667
- _farm_js_core.logger.info(`Gateway: ${plan.gatewayUrl}`);
668
- _farm_js_core.logger.info("Opening Farm preview gateway session...");
669
- const session = await runPreviewGateway(plan, { timeoutMs: options.timeoutMs });
670
- return {
671
- target,
672
- plan,
673
- publicUrl: session.publicUrl,
674
- session
675
- };
714
+ _farm_js_core.logger.info(`Relay: ${plan.relayUrl}`);
715
+ _farm_js_core.logger.info("Opening native Farm preview tunnel...");
716
+ try {
717
+ const session = await runNativePreviewTunnel(plan);
718
+ return {
719
+ target,
720
+ plan,
721
+ publicUrl: session.publicUrl,
722
+ session
723
+ };
724
+ } catch (error) {
725
+ _farm_js_core.logger.warn(`Native preview relay unavailable; using compatibility gateway polling.${formatPreviewError(error)}`);
726
+ const session = await runPreviewGateway(plan, { timeoutMs: options.timeoutMs });
727
+ return {
728
+ target,
729
+ plan,
730
+ publicUrl: session.publicUrl,
731
+ session
732
+ };
733
+ }
676
734
  }
677
735
  const plan = createPreviewTunnelPlan(target, options);
678
736
  if (options.dryRun) {
@@ -690,6 +748,9 @@ async function previewFarm(options = {}) {
690
748
  publicUrl: await runPreviewTunnel(plan, options.timeoutMs ?? 3e4)
691
749
  };
692
750
  }
751
+ function formatPreviewError(error) {
752
+ return error instanceof Error && error.message ? ` ${error.message}` : "";
753
+ }
693
754
  function shouldUseManagedGateway(options) {
694
755
  if (options.provider === "farm" || process.env.FARM_PREVIEW_PROVIDER === "farm") return true;
695
756
  if (options.provider === "local" || process.env.FARM_PREVIEW_PROVIDER === "local") return false;
@@ -3244,6 +3305,7 @@ exports.resolveCloudflareAgentDeployPlan = resolveCloudflareAgentDeployPlan;
3244
3305
  exports.resolvePreviewTarget = resolvePreviewTarget;
3245
3306
  exports.runFarmCronJob = runFarmCronJob;
3246
3307
  exports.runFarmDoctor = runFarmDoctor;
3308
+ exports.runNativePreviewTunnel = runNativePreviewTunnel;
3247
3309
  exports.runPreviewGateway = runPreviewGateway;
3248
3310
  Object.defineProperty(exports, "startDevServer", {
3249
3311
  enumerable: true,