|
| 1 | +import { z } from "zod/v4"; |
| 2 | +import { env } from "../env.ts"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Represents a valid connection to a database. |
| 6 | + * Connectable instances are always pre-validated and don't need to be validated again. |
| 7 | + */ |
| 8 | +export class Connectable { |
| 9 | + private constructor(public readonly url: URL) {} |
| 10 | + |
| 11 | + /** |
| 12 | + * Custom logic for parsing a string into a Connectable through zod. |
| 13 | + */ |
| 14 | + static transform( |
| 15 | + urlString: string, |
| 16 | + ctx: z.RefinementCtx<string> |
| 17 | + ): Connectable { |
| 18 | + if ( |
| 19 | + !urlString.startsWith("postgres://") && |
| 20 | + !urlString.startsWith("postgresql://") |
| 21 | + ) { |
| 22 | + ctx.addIssue({ |
| 23 | + code: "custom", |
| 24 | + message: "URL must start with 'postgres://' or 'postgresql://'", |
| 25 | + expected: "string", |
| 26 | + input: urlString, |
| 27 | + }); |
| 28 | + return z.NEVER; |
| 29 | + } |
| 30 | + |
| 31 | + let url: URL; |
| 32 | + try { |
| 33 | + url = new URL(urlString); |
| 34 | + } catch (_: unknown) { |
| 35 | + ctx.addIssue({ |
| 36 | + code: "invalid_type", |
| 37 | + message: "Invalid URL", |
| 38 | + expected: "string", |
| 39 | + input: urlString, |
| 40 | + }); |
| 41 | + return z.NEVER; |
| 42 | + } |
| 43 | + // we don't want to allow localhost access for hosted sync instances |
| 44 | + // to prevent users from connecting to our hosted db |
| 45 | + // (even though all our dbs should should be password protected) |
| 46 | + const isLocalhost = |
| 47 | + url.hostname === "localhost" || |
| 48 | + // ipv4 localhost |
| 49 | + /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(url.hostname) || |
| 50 | + // ipv6 localhost |
| 51 | + url.hostname === "[::1]"; |
| 52 | + if (isLocalhost && env.HOSTED) { |
| 53 | + ctx.addIssue({ |
| 54 | + code: "custom", |
| 55 | + message: |
| 56 | + "Syncing to localhost is not allowed. Run the sync server locally to access your local database", |
| 57 | + }); |
| 58 | + } |
| 59 | + // Our current hosted sync instances do not support ipv6 |
| 60 | + // we need users to use pooler.supabase.com instead |
| 61 | + if (env.HOSTED && Connectable.isDirectSupabaseConnection(url)) { |
| 62 | + const account = Connectable.extractSupabaseAccount(url); |
| 63 | + // send the user directly to the right place if there's an account available |
| 64 | + const link = account |
| 65 | + ? `https://supabase.com/dashboard/project/${account}?showConnect=true` |
| 66 | + : "https://supabase.com/docs/guides/troubleshooting/supabase--your-network-ipv4-and-ipv6-compatibility-cHe3BP"; |
| 67 | + ctx.addIssue({ |
| 68 | + code: "custom", |
| 69 | + message: `You are using a direct connection to a supabase instance. Supabase does not accept IPv4 connections, try using a transaction pooler connection instead ${link}`, |
| 70 | + }); |
| 71 | + } |
| 72 | + if (ctx.issues.length > 0) { |
| 73 | + return z.NEVER; |
| 74 | + } |
| 75 | + return new Connectable(url); |
| 76 | + } |
| 77 | + |
| 78 | + private static extractSupabaseAccount(url: URL): string | undefined { |
| 79 | + const match = url.toString().match(/db\.(\w+)\.supabase\.co/); |
| 80 | + if (!match) { |
| 81 | + return; |
| 82 | + } |
| 83 | + return match[1]; |
| 84 | + } |
| 85 | + |
| 86 | + private static isDirectSupabaseConnection(url: URL) { |
| 87 | + return ( |
| 88 | + url.hostname.includes("supabase.co") && |
| 89 | + !url.hostname.includes("pooler.supabase.co") |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + toString() { |
| 94 | + return this.url.toString(); |
| 95 | + } |
| 96 | +} |
0 commit comments