Repository navigation
refactor(studio): lazy self-hosted admin client + enforce in API routes (from #46424) - #47104
Conversation
The self-hosted storage/auth API routes each constructed a module-scope admin client via createClient(SUPABASE_URL!, SUPABASE_SERVICE_KEY!). Those env vars only exist on self-hosted deployments, so eager construction at module scope is both wasteful on platform and fragile on any runtime that evaluates an API module before its route is hit (constructing with undefined credentials throws). Wrap it in a Proxy that defers construction until first property access — which only happens inside a handler, i.e. on self-hosted where the vars are set. Behaviour is unchanged on the current build. Extracted from the TanStack Start migration (#46424) to shrink that PR; the lazy client is what makes those routes safe under TanStack's single-handler module evaluation.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
This pull request has been ignored for the connected project Preview Branches by Supabase. |
📝 WalkthroughWalkthroughA new ChangesShared self-hosted admin client
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🎭 Playwright Test ResultsDetails
Flaky testsFeatures › realtime-inspector.spec.ts › Realtime Inspector › Basic Inspector UI › channel selection popover opens and works Skipped testsFeatures › auth-users.spec.ts › should show web3 users as enabled when the matching web3 provider is enabled |
The migration made all 17 self-hosted storage/auth handlers lazy; the initial extraction only captured 13. Add the 4 vector-buckets handlers, and add the eslint guardrail (no module-scope createClient in API routes) now that every flagged handler is lazy. Also carries the analytics-SQL no-restricted-syntax rule from the same eslint config (0 violations on master). Verified: studio lint (0 errors) + typecheck green.
Braintrust eval report
|
…es (from #46424) (#47104) Extracted from the TanStack Start migration (#46424) to shrink that PR. The self-hosted storage/auth API routes each constructed a module-scope admin client (`createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SERVICE_KEY!)`). Those env vars only exist on self-hosted, so eager module-scope construction is wasteful on platform and fragile on any runtime that evaluates an API module before its route is hit (constructing with `undefined` credentials throws on import). **Changed:** - Add `lib/api/self-hosted-admin.ts` — `selfHostedSupabaseAdmin`, a `Proxy` that defers `createClient(...)` until first property access (inside a handler, i.e. on self-hosted where the vars are set). - Swap **all 17** storage/auth/vector-bucket handlers from module-scope `createClient(...)` to `import { selfHostedSupabaseAdmin as supabase }`. - **Enforce it:** add an eslint `no-restricted-syntax` rule banning module-scope `createClient` in `pages/api/**` + `routes/**` (now that every flagged handler is lazy). The same eslint config block also carries an analytics-SQL boundary rule — 0 violations on master. Behaviour is unchanged (the client is still built lazily inside the handler). This is also the change that makes those routes safe under TanStack's single-handler module evaluation. ## To test - Self-hosted Studio: storage buckets/objects, vector buckets, and auth users operations work as before. ## Verification studio lint (0 errors, both rules active) ✓ · studio typecheck ✓. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Standardized self-hosted Supabase admin client usage across platform authentication and storage endpoints, removing per-route client setup. * Improved reliability by lazily creating the admin client only when first used. * **Chores / Tooling** * Updated ESLint rules to prevent module-scope Supabase client creation in API routes and to enforce safe analytics SQL access patterns. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Alaister Young <[email protected]> Co-authored-by: Ali Waseem <[email protected]>
Extracted from the TanStack Start migration (#46424) to shrink that PR.
The self-hosted storage/auth API routes each constructed a module-scope admin client (
createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SERVICE_KEY!)). Those env vars only exist on self-hosted, so eager module-scope construction is wasteful on platform and fragile on any runtime that evaluates an API module before its route is hit (constructing withundefinedcredentials throws on import).Changed:
lib/api/self-hosted-admin.ts—selfHostedSupabaseAdmin, aProxythat deferscreateClient(...)until first property access (inside a handler, i.e. on self-hosted where the vars are set).createClient(...)toimport { selfHostedSupabaseAdmin as supabase }.no-restricted-syntaxrule banning module-scopecreateClientinpages/api/**+routes/**(now that every flagged handler is lazy). The same eslint config block also carries an analytics-SQL boundary rule — 0 violations on master.Behaviour is unchanged (the client is still built lazily inside the handler). This is also the change that makes those routes safe under TanStack's single-handler module evaluation.
To test
Verification
studio lint (0 errors, both rules active) ✓ · studio typecheck ✓.
Summary by CodeRabbit