Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

tanStackSourceHosts.ts

42 lines1.7 KB

1import { TANSTACK_SOURCE_HOST, withoutOwnerMark } from "../shared/definition.ts";
2​
3export const TANSTACK_SOURCE_HOSTS = [TANSTACK_SOURCE_HOST] as const;
4export type TanStackSourceHost = (typeof TANSTACK_SOURCE_HOSTS)[number];
5​
6export function tanStackSourceHost(host: string): TanStackSourceHost | null {
7 const normalized = host.trim().toLowerCase();
8 return TANSTACK_SOURCE_HOSTS.find((candidate) => candidate === normalized) ?? null;
9}
10​
11const SEGMENT = /^[A-Za-z0-9_.-]+$/u;
12​
13/** `owner/name` from the API spelling or either web spelling of the owner, `.git` dropped. */
14export function tanStackSourceRepositoryPath(path: string): string | null {
15 const [owner, name, ...rest] = withoutOwnerMark(
16 path
17 .trim()
18 .replace(/^\/+|\/+$/gu, "")
19 .replace(/\.git$/u, ""),
20 ).split("/");
21 return rest.length === 0 &&
22 owner !== undefined &&
23 name !== undefined &&
24 [owner, name].every((segment) => SEGMENT.test(segment) && segment !== "." && segment !== "..")
25 ? `${owner}/${name}`
26 : null;
28​
29/** Source serves git over HTTPS only, at `https://<host>/@<owner>/<repo>.git`. */
30export function parseTanStackSourceRemote(
31 url: string,
32): { readonly host: TanStackSourceHost; readonly repository: string } | null {
33 const parsed = URL.parse(url.trim());
34 if (parsed === null || (parsed.protocol !== "https:" && parsed.protocol !== "http:")) return null;
35 const host = tanStackSourceHost(parsed.hostname);
36 if (host === null || !parsed.pathname.startsWith("/@")) return null;
37 const repository = tanStackSourceRepositoryPath(parsed.pathname);
38 return repository === null ? null : { host, repository };
40​
41export const webRepositoryUrl = (host: string, repository: string) =>
42 `https://${host}/@${repository}`;