Files
t3code/packages/shared/src/preview.ts
T

167 lines
5.7 KiB
TypeScript

/**
* Pure URL helpers shared between the preview server, desktop main process,
* and web renderer. Centralising these guarantees the four call sites agree
* on what counts as "loopback" and how to normalise a free-form URL string.
*/
import * as Schema from "effect/Schema";
const TAB_ID_PREFIX = "tab_";
let nextPreviewTabSequence = 0;
/**
* Generate a fresh preview tab id. Lives in shared (not contracts) because
* the contracts package is schema-only — runtime helpers belong here.
*/
export function newPreviewTabId(): string {
nextPreviewTabSequence += 1;
return `${TAB_ID_PREFIX}${nextPreviewTabSequence.toString(36)}`;
}
const LOOPBACK_HOSTS: ReadonlySet<string> = new Set(["localhost", "127.0.0.1", "0.0.0.0", "::1"]);
/** Internal — used by `lsof` parsing where the host string is wire-formatted. */
export const LSOF_LOCAL_HOST_TOKENS: ReadonlySet<string> = new Set([
...LOOPBACK_HOSTS,
"*",
"[::]",
"[::1]",
]);
const LOOPBACK_PREFIX_PATTERN = /^(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1?\])(?::|\/|$)/i;
export function isLoopbackHost(host: string): boolean {
if (LOOPBACK_HOSTS.has(host)) return true;
if (host === "[::1]") return true;
return false;
}
export class PreviewUrlNormalizationError extends Schema.TaggedError<PreviewUrlNormalizationError>()(
"PreviewUrlNormalizationError",
{
inputLength: Schema.Number,
reason: Schema.Literals(["empty", "parse", "unsupported-protocol"]),
protocol: Schema.optional(Schema.String),
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
const protocol = this.protocol === undefined ? "" : `: ${this.protocol}`;
return `Invalid preview URL (${this.reason}${protocol}; input length ${this.inputLength}).`;
}
}
export const isPreviewUrlNormalizationError = Schema.is(PreviewUrlNormalizationError);
function previewUrlProtocol(rawUrl: string): string | undefined {
return /^([A-Za-z][A-Za-z\d+.-]*):/.exec(rawUrl)?.[1]?.toLowerCase().concat(":");
}
const SEARCH_URL = "https://duckduckgo.com/?q=";
/** A bare host that is always an address: localhost, an IPv4 literal, or a bracketed IPv6 one. */
const ADDRESS_HOST_PATTERN = /^(?:localhost|\d{1,3}(?:\.\d{1,3}){3}|\[[\da-f:.]+\])$/i;
const BARE_HOST_PORT_PATTERN = /^[^/?#:@]+:\d+(?:[/?#]|$)/;
/**
* Registered schemes that are never a bare host, so `ftp:21` or `tel:5551234`
* is rejected rather than read as host and port.
*/
const KNOWN_NON_WEB_SCHEMES: ReadonlySet<string> = new Set([
"about:",
"blob:",
"chrome:",
"data:",
"file:",
"ftp:",
"javascript:",
"mailto:",
"sms:",
"ssh:",
"tel:",
"view-source:",
"ws:",
"wss:",
]);
/**
* Turns what a user typed in an address bar into the URL to open: an address
* as `normalizePreviewUrl` reads it, or else a web search for the text. Text
* is an address when it has a scheme, or no spaces and a host that has a dot,
* a port, or is localhost or an IP. Throws only for empty input or an
* explicit unsupported scheme (`ftp://`, `mailto:`, `data:`).
*/
export function resolveAddressBarInput(rawInput: string): string {
const trimmed = rawInput.trim();
if (/^[a-z][a-z\d+.-]*:\/\//i.test(trimmed) || trimmed.length === 0)
return normalizePreviewUrl(trimmed);
if (!/\s/.test(trimmed)) {
// `mailto:x` or `data:...` is an explicit scheme; `example.com:8080` or
// `devbox:8080` is a bare host and port. `normalizePreviewUrl` would read
// `mailto:[email protected]` as credentials on a bare host, so reject here.
const protocol = previewUrlProtocol(trimmed);
if (
protocol !== undefined &&
(KNOWN_NON_WEB_SCHEMES.has(protocol) || !BARE_HOST_PORT_PATTERN.test(trimmed))
) {
if (protocol !== "http:" && protocol !== "https:") {
throw new PreviewUrlNormalizationError({
inputLength: rawInput.length,
reason: "unsupported-protocol",
protocol,
});
}
return normalizePreviewUrl(trimmed);
}
const authority = trimmed.split(/[/?#]/, 1)[0] ?? "";
const host = authority.replace(/^[^@]*@/, "").replace(/:\d+$/, "");
const hasPort = host.length < authority.replace(/^[^@]*@/, "").length;
if (hasPort || ADDRESS_HOST_PATTERN.test(host) || /^[^.]+(?:\.[^.]+)+\.?$/.test(host)) {
try {
return normalizePreviewUrl(trimmed);
} catch {
// Not a usable address after all; search for it.
}
}
}
return `${SEARCH_URL}${encodeURIComponent(trimmed)}`;
}
/**
* Normalise a free-form URL string into a fully-qualified `http(s)://` URL.
*
* - Bare loopback hosts (`localhost`, `localhost:5173`) become `http://...`.
* - Bare public hosts (`example.com`) become `https://...`.
* - Already-qualified URLs are validated and returned as `URL.href`.
*
* Throws `PreviewUrlNormalizationError` for empty, unparseable, or
* unsupported-protocol inputs.
*/
export function normalizePreviewUrl(rawUrl: string): string {
const trimmed = rawUrl.trim();
if (trimmed.length === 0) {
throw new PreviewUrlNormalizationError({ inputLength: rawUrl.length, reason: "empty" });
}
const useHttp = LOOPBACK_PREFIX_PATTERN.test(trimmed);
const candidate = trimmed.includes("://")
? trimmed
: `${useHttp ? "http" : "https"}://${trimmed}`;
let parsed: URL;
try {
parsed = new URL(candidate);
} catch (cause) {
throw new PreviewUrlNormalizationError({
inputLength: rawUrl.length,
reason: "parse",
protocol: previewUrlProtocol(candidate),
cause,
});
}
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
throw new PreviewUrlNormalizationError({
inputLength: rawUrl.length,
reason: "unsupported-protocol",
protocol: parsed.protocol,
});
}
return parsed.href;
}