Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: added jsdocs to exported functions #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,74 @@ import { existsSync } from "node:fs";
import consola from "consola";

export interface TunnelOptions {
/**
* The URL to which the tunnel should connect. The default is to construct a URL based on protocol, hostname and port.
* @optional
*/
url?: string;

/**
* The port number to use for the tunnel connection. Can be a string or a number. Defaults to 3000.
* @optional
*/
port?: number | string;

/**
* The host name for the tunnel connection. Defaults to localhost.
* @optional
*/
hostname?: string;

/**
* The protocol to use for the tunnel connection, either "http" or "https". Default is http.
* @optional
*/
protocol?: "http" | "https";

/**
* Specifies whether to enforce TLS verification. Default is true.
* @optional
*/
verifyTLS?: boolean;

/**
* Indicates whether the user accepts the Cloudflare Terms of Service for using cloudflared. Default is false.
* @optional
*/
acceptCloudflareNotice?: boolean;
}

export interface Tunnel {
/**
* Get the current URL of the active tunnel.
* @returns {Promise<string>} A promise that resolves to the URL of the tunnel.
*/
getURL: () => Promise<string>;

/**
* Close the active tunnel.
* @returns {Promise<void>} A promise that will be resolved when the tunnel is successfully closed.
*/
close: () => Promise<void>;
}

/**
* Initialises and starts a network tunnel using cloudflared.
* @param {TunnelOptions} opts - Configuration options for the tunnel.
* @returns {Promise<undefined | Tunnel>} A promise that resolves to the tunnel instance, or undefined if the setup fails.
* @throws {Error} If there are problems installing cloudflared or starting the tunnel.
* @example
* const tunnelOptions = {
* protocol: "https",
* port: "443",
* hostname: "example.com",
* verifyTLS: true
* acceptCloudflareNotice: true
* };
* const tunnel = await startTunnel(tunnelOptions);
* console.log(await tunnel.getURL());
* await tunnel.close();
*/
export async function startTunnel(
opts: TunnelOptions,
): Promise<undefined | Tunnel> {
Expand Down