mirror your GitHub repos to tangled.org automatically
1/**
2 * Map a knot hostname (as stored on `sh.tangled.repo`) to the SSH host we
3 * actually push to. For the appview-hosted knot, the HTTPS XRPC endpoint is
4 * `knot1.tangled.sh` (Cloudflare-fronted) but SSH lives on `tangled.org`.
5 * Self-hosted knots serve both on the same host (their `knot` value may
6 * include a `:port` suffix for non-default SSH; git URL parsing handles it).
7 *
8 * The official UI does this same mapping in
9 * `appview/pages/templates/repo/empty.html`. If tangled adds more
10 * appview-hosted knots in future this'll need updating \u2014 see PLAN.md
11 * "Deferred / follow-ups".
12 */
13export function sshHostForKnot(knot: string): string {
14 if (knot === 'knot1.tangled.sh') return 'tangled.org'
15 return knot
16}
17
18/**
19 * Split a knot value into the ssh host and optional port. Self-hosted knots
20 * may carry a `:port` suffix for a non-default ssh port; the appview-hosted
21 * knot maps through `sshHostForKnot` and has no port.
22 */
23export function sshEndpointForKnot(knot: string): { host: string, port?: number } {
24 const mapped = sshHostForKnot(knot)
25 const colon = mapped.lastIndexOf(':')
26 if (colon === -1) return { host: mapped }
27 const port = Number.parseInt(mapped.slice(colon + 1), 10)
28 if (Number.isNaN(port)) return { host: mapped }
29 return { host: mapped.slice(0, colon), port }
30}