Skip to content

Commit 699cda5

Browse files
committed
fix(ci): align supabase ports in worktrees
1 parent 41d4f00 commit 699cda5

6 files changed

Lines changed: 88 additions & 13 deletions

File tree

scripts/with-worktree-supabase-env.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export SUPABASE_INSPECTOR_PORT="$((BASE_PORT + 13))"
4040

4141
export SUPABASE_URL="http://127.0.0.1:${SUPABASE_API_PORT}"
4242
export SUPA_URL="${SUPABASE_URL}"
43+
export SUPABASE_EXTERNAL_URL="${SUPABASE_URL}"
44+
export API_URL="${SUPABASE_URL}"
45+
export S3_ENDPOINT="127.0.0.1:${SUPABASE_API_PORT}/storage/v1/s3"
46+
export STORAGE_API_URL="${SUPABASE_URL}/storage/v1"
4347

4448
if [ "${1:-}" = "--print-env" ]; then
4549
printf 'SUPABASE_PROJECT_ID=%s\n' "${SUPABASE_PROJECT_ID}"
@@ -53,6 +57,10 @@ if [ "${1:-}" = "--print-env" ]; then
5357
printf 'SUPABASE_INSPECTOR_PORT=%s\n' "${SUPABASE_INSPECTOR_PORT}"
5458
printf 'SUPABASE_URL=%s\n' "${SUPABASE_URL}"
5559
printf 'SUPA_URL=%s\n' "${SUPA_URL}"
60+
printf 'SUPABASE_EXTERNAL_URL=%s\n' "${SUPABASE_EXTERNAL_URL}"
61+
printf 'API_URL=%s\n' "${API_URL}"
62+
printf 'S3_ENDPOINT=%s\n' "${S3_ENDPOINT}"
63+
printf 'STORAGE_API_URL=%s\n' "${STORAGE_API_URL}"
5664
exit 0
5765
fi
5866

@@ -61,4 +69,45 @@ if [ "$#" -eq 0 ]; then
6169
exit 1
6270
fi
6371

72+
# For local functions runtime, override only non-reserved local endpoints so
73+
# uploads and storage calls follow the worktree port assignments.
74+
cmd=("$@")
75+
if [ "${#cmd[@]}" -ge 4 ]; then
76+
subcommand_start=-1
77+
if [ "${cmd[0]}" = "supabase" ]; then
78+
subcommand_start=1
79+
elif [ "${cmd[0]}" = "bunx" ] && [ "${cmd[1]:-}" = "supabase" ]; then
80+
subcommand_start=2
81+
fi
82+
83+
if [ "${subcommand_start}" -ge 0 ] \
84+
&& [ "${cmd[subcommand_start]:-}" = "functions" ] \
85+
&& [ "${cmd[$((subcommand_start + 1))]:-}" = "serve" ]; then
86+
has_env_file=false
87+
for arg in "${cmd[@]}"; do
88+
if [ "${arg}" = "--env-file" ]; then
89+
has_env_file=true
90+
break
91+
fi
92+
done
93+
94+
if [ "${has_env_file}" = false ]; then
95+
functions_env_file="${ROOT_DIR}/.context/worktree-supabase-functions.env"
96+
mkdir -p "${ROOT_DIR}/.context"
97+
if [ -f "${ROOT_DIR}/supabase/functions/.env" ]; then
98+
grep -vE '^(API_URL|S3_ENDPOINT|STORAGE_API_URL)=' "${ROOT_DIR}/supabase/functions/.env" > "${functions_env_file}" || true
99+
else
100+
: > "${functions_env_file}"
101+
fi
102+
{
103+
printf '\n'
104+
printf 'API_URL=%s\n' "${API_URL}"
105+
printf 'S3_ENDPOINT=%s\n' "${S3_ENDPOINT}"
106+
printf 'STORAGE_API_URL=%s\n' "${STORAGE_API_URL}"
107+
} >> "${functions_env_file}"
108+
exec "${cmd[@]}" --env-file "${functions_env_file}"
109+
fi
110+
fi
111+
fi
112+
64113
exec "$@"

supabase/functions/_backend/files/files.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { getAppByAppIdPg, getUserIdFromApikey } from '../utils/pg_files.ts'
1616
import { checkPermissionPg } from '../utils/rbac.ts'
1717
import { createStatsBandwidth } from '../utils/stats.ts'
1818
import { supabaseAdmin } from '../utils/supabase.ts'
19-
import { backgroundTask } from '../utils/utils.ts'
19+
import { backgroundTask, getEnv } from '../utils/utils.ts'
2020
import { app as files_config } from './files_config.ts'
2121
import { parseUploadMetadata } from './parse.ts'
2222
import { DEFAULT_RETRY_PARAMS, RetryBucket } from './retry.ts'
@@ -55,7 +55,8 @@ async function getHandler(c: Context): Promise<Response> {
5555
const { data } = supabaseAdmin(c).storage.from('capgo').getPublicUrl(fileId)
5656

5757
// cloudlog('publicUrl', data.publicUrl)
58-
const url = data.publicUrl.replace('http://kong:8000', 'http://localhost:54321')
58+
const localExternalUrl = getEnv(c, 'SUPABASE_EXTERNAL_URL') || getEnv(c, 'API_URL') || 'http://localhost:54321'
59+
const url = data.publicUrl.replace('http://kong:8000', localExternalUrl)
5960
// cloudlog('url', url)
6061
return c.redirect(url)
6162
}

supabase/functions/_backend/files/supabaseTusProxy.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,35 @@ function rewriteLocationHeader(c: Context, supabaseLocation: string): string {
7979
}
8080

8181
const supabaseUrl = getEnv(c, 'SUPABASE_URL')
82+
const localExternalBaseUrl = getEnv(c, 'SUPABASE_EXTERNAL_URL') || getEnv(c, 'API_URL') || 'http://localhost:54321'
8283
const isLocalDev = supabaseUrl.includes('kong:8000')
8384

8485
let forwardedHost = c.req.header('X-Forwarded-Host')
8586
const forwardedProto = c.req.header('X-Forwarded-Proto') || 'https'
8687

87-
if (isLocalDev && forwardedHost && !forwardedHost.includes(':')) {
88-
if (forwardedHost === 'localhost' || forwardedHost === '127.0.0.1') {
89-
forwardedHost = `${forwardedHost}:54321`
88+
if (isLocalDev && forwardedHost) {
89+
try {
90+
const localExternalUrl = new URL(localExternalBaseUrl)
91+
const localPort = localExternalUrl.port
92+
if (localPort) {
93+
const [forwardedHostName] = forwardedHost.split(':')
94+
if (forwardedHostName === 'localhost' || forwardedHostName === '127.0.0.1')
95+
forwardedHost = `${forwardedHostName}:${localPort}`
96+
}
97+
}
98+
catch {
99+
// Keep forwarded host untouched; fallback logic below handles defaults.
90100
}
91101
}
92102

93-
cloudlog({ requestId, message: 'rewriteLocationHeader debug', supabaseUrl, forwardedHost, forwardedProto, isLocalDev })
103+
cloudlog({ requestId, message: 'rewriteLocationHeader debug', supabaseUrl, localExternalBaseUrl, forwardedHost, forwardedProto, isLocalDev })
94104

95105
let baseUrl: string
96106
if (forwardedHost) {
97107
baseUrl = `${forwardedProto}://${forwardedHost}`
98108
}
99109
else if (isLocalDev) {
100-
baseUrl = 'http://localhost:54321'
110+
baseUrl = localExternalBaseUrl
101111
}
102112
else {
103113
cloudlog({

supabase/functions/_backend/utils/downloadUrl.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Database } from './supabase.types.ts'
33
import { getRuntimeKey } from 'hono/adapter'
44
import { cloudlog, cloudlogErr } from './logging.ts'
55
import { s3 } from './s3.ts'
6+
import { getEnv } from './utils.ts'
67

78
const EXPIRATION_SECONDS = 604800
89
const BASE_PATH = 'files/read/attachments'
@@ -13,6 +14,19 @@ export interface ManifestEntry {
1314
download_url: string | null
1415
}
1516

17+
function getLocalHost(c: Context, fallbackHost: string): string {
18+
const externalUrl = getEnv(c, 'SUPABASE_EXTERNAL_URL') || getEnv(c, 'API_URL')
19+
if (externalUrl) {
20+
try {
21+
return new URL(externalUrl).host
22+
}
23+
catch {
24+
return fallbackHost
25+
}
26+
}
27+
return fallbackHost
28+
}
29+
1630
export async function getBundleUrl(
1731
c: Context,
1832
r2_path: string | null,
@@ -39,8 +53,8 @@ export async function getBundleUrl(
3953
const url = new URL(c.req.url)
4054
let finalPath = BASE_PATH
4155
// .replace('http://supabase_edge_runtime_capgo:8081', 'http://localhost:54321')
42-
if (url.host === 'supabase_edge_runtime_capgo-app:8081') {
43-
url.host = 'localhost:54321'
56+
if (url.host.includes('supabase_edge_runtime') && url.port === '8081') {
57+
url.host = getLocalHost(c, 'localhost:54321')
4458
finalPath = `functions/v1/${BASE_PATH}`
4559
}
4660
const downloadUrl = `${url.protocol}//${url.host}/${finalPath}/${r2_path}?key=${checksum}&device_id=${deviceId}`
@@ -56,8 +70,8 @@ export function getManifestUrl(c: Context, versionId: number, manifest: Partial<
5670
const url = new URL(c.req.url)
5771
let finalPath = BASE_PATH
5872
// .replace('http://supabase_edge_runtime_capgo:8081', 'http://localhost:54321')
59-
if (url.host === 'supabase_edge_runtime_capgo:8081') {
60-
url.host = 'localhost:54321'
73+
if (url.host.includes('supabase_edge_runtime') && url.port === '8081') {
74+
url.host = getLocalHost(c, 'localhost:54321')
6175
finalPath = `functions/v1/${BASE_PATH}`
6276
}
6377
const signKey = versionId

tests/cli-s3.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface UploadResponse {
77
}
88

99
describe('upload_link', async () => {
10-
const API_URL = process.env.API_URL ?? 'http://127.0.0.1:54321'
10+
const API_URL = process.env.API_URL ?? process.env.SUPABASE_URL ?? 'http://127.0.0.1:54321'
1111
const id = randomUUID()
1212
const fileId = '1.0.42'
1313
const APPNAME = `com.cli_s3_${id}`

tests/test-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { env } from 'node:process'
44
import { createClient } from '@supabase/supabase-js'
55
import { Pool } from 'pg'
66

7-
export const POSTGRES_URL = 'postgresql://postgres:postgres@127.0.0.1:54322/postgres'
7+
const SUPABASE_DB_PORT = env.SUPABASE_DB_PORT || '54322'
8+
export const POSTGRES_URL = `postgresql://postgres:postgres@127.0.0.1:${SUPABASE_DB_PORT}/postgres`
89

910
function normalizeLocalhostUrl(raw: string | undefined): string | undefined {
1011
if (!raw)

0 commit comments

Comments
 (0)