Commit 825873bf authored by Kim, Hyeonseo's avatar Kim, Hyeonseo
Browse files

refactor: extract tunnel creation logic into a separate function for test

parent daedc1c4
Loading
Loading
Loading
Loading
+39 −21
Original line number Diff line number Diff line
@@ -4,32 +4,50 @@ import ora from "ora";

const service = new EnumType(["localhost.run", "serveo.net"]);

export const command = new Command()
  .type("service", service)
  .arguments("<port:integer>")
  .description(
    "Expose a local HTTP server to the public internet using a secure tunnel.\n\n" +
      "Note that the HTTP requests through the tunnel have X-Forwarded-* headers.",
  )
  .option("-s, --service <service:service>", "The localtunnel service to use.")
  .action(async (options, port: number) => {
    const spinner = ora({
export async function tunnelAction(
  options: { service?: "localhost.run" | "serveo.net" },
  port: number,
  deps: {
    openTunnel: typeof openTunnel;
    ora: typeof ora;
    console: typeof console;
    addSignalListener: typeof Deno.addSignalListener;
    exit: typeof Deno.exit;
  } = {
    openTunnel,
    ora,
    console,
    addSignalListener: Deno.addSignalListener,
    exit: Deno.exit,
  },
) {
  const spinner = deps.ora({
    text: "Creating a secure tunnel...",
    discardStdin: false,
  }).start();
  let tunnel: Tunnel;
  try {
      tunnel = await openTunnel({ port, service: options.service });
    tunnel = await deps.openTunnel({ port, service: options.service });
  } catch {
    spinner.fail("Failed to create a secure tunnel.");
      Deno.exit(1);
    deps.exit(1);
  }
  spinner.succeed(
    `Your local server at ${port} is now publicly accessible:\n`,
  );
    console.log(tunnel.url.href);
    console.error("\nPress ^C to close the tunnel.");
    Deno.addSignalListener("SIGINT", async () => {
  deps.console.log(tunnel.url.href);
  deps.console.error("\nPress ^C to close the tunnel.");
  deps.addSignalListener("SIGINT", async () => {
    await tunnel.close();
  });
  });
}

export const command = new Command()
  .type("service", service)
  .arguments("<port:integer>")
  .description(
    "Expose a local HTTP server to the public internet using a secure tunnel.\n\n" +
      "Note that the HTTP requests through the tunnel have X-Forwarded-* headers.",
  )
  .option("-s, --service <service:service>", "The localtunnel service to use.")
  .action(tunnelAction);