better-supabase 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +187 -46
- package/dist/access-sql-B0dF-SoF.js +181 -0
- package/dist/{actor-DaJjqD5R.d.ts → actor-CQTlc1Hh.d.ts} +1 -1
- package/dist/adapter-Bc1o3zyy.js +1106 -0
- package/dist/agents-CVHGd7It.d.ts +94 -0
- package/dist/ai-cache-CBWs1W0k.js +80 -0
- package/dist/ai-cache-DaDu_G5L.d.ts +64 -0
- package/dist/ai-chat-7amvXjmQ.d.ts +590 -0
- package/dist/ai-chat-B24UI962.js +743 -0
- package/dist/ai-files-CSPNswUY.js +319 -0
- package/dist/ai-sdk/agents/index.d.ts +74 -0
- package/dist/ai-sdk/agents/index.js +159 -0
- package/dist/ai-sdk/batches/index.d.ts +74 -0
- package/dist/ai-sdk/batches/index.js +180 -0
- package/dist/ai-sdk/cache/index.d.ts +38 -0
- package/dist/ai-sdk/cache/index.js +127 -0
- package/dist/ai-sdk/chat/index.d.ts +2 -0
- package/dist/ai-sdk/chat/index.js +246 -0
- package/dist/ai-sdk/embeddings/index.d.ts +3 -0
- package/dist/ai-sdk/embeddings/index.js +2 -0
- package/dist/ai-sdk/files/index.d.ts +61 -0
- package/dist/ai-sdk/files/index.js +111 -0
- package/dist/ai-sdk/index.d.ts +145 -0
- package/dist/ai-sdk/index.js +174 -0
- package/dist/ai-sdk/mcp/index.d.ts +102 -0
- package/dist/ai-sdk/mcp/index.js +362 -0
- package/dist/ai-sdk/memory/index.d.ts +67 -0
- package/dist/ai-sdk/memory/index.js +175 -0
- package/dist/ai-sdk/react/index.d.ts +2 -0
- package/dist/ai-sdk/react/index.js +3 -0
- package/dist/ai-sdk/react/server.d.ts +6 -0
- package/dist/ai-sdk/react/server.js +7 -0
- package/dist/ai-sdk/workflow/index.d.ts +249 -0
- package/dist/ai-sdk/workflow/index.js +575 -0
- package/dist/ai-sdk/workflow/react/index.d.ts +20 -0
- package/dist/ai-sdk/workflow/react/index.js +66 -0
- package/dist/ai-sdk/workflow/react/server.d.ts +10 -0
- package/dist/ai-sdk/workflow/react/server.js +12 -0
- package/dist/ai-tasks-Cd1HMFvB.d.ts +131 -0
- package/dist/announcements-CiyOs-xP.js +107 -0
- package/dist/announcements-ZRGnScWI.d.ts +72 -0
- package/dist/api-keys-rCGtKaZA.d.ts +156 -0
- package/dist/assistant-DwdpDH1q.d.ts +147 -0
- package/dist/astro/index.d.ts +86 -0
- package/dist/astro/index.js +93 -0
- package/dist/attributes-5dYul0u0.js +13 -0
- package/dist/auth-link-BjFPqwLz.d.ts +97 -0
- package/dist/auth-link-DrDX9KOT.js +114 -0
- package/dist/authorization-BFCqamMs.d.ts +172 -0
- package/dist/before-route-CnR0C0Jr.js +30 -0
- package/dist/bind-Bu_ZNa3W.d.ts +62 -0
- package/dist/bind-l4kAvYSv.js +97 -0
- package/dist/block-events-CrT8r7Fe.js +76 -0
- package/dist/block-fields-GFkxl4dP.js +49 -0
- package/dist/block-fields-SJ6fEf3o.d.ts +31 -0
- package/dist/block-helpers-CKtbFcRv.d.ts +37 -0
- package/dist/block-helpers-DWHxHsEQ.js +190 -0
- package/dist/block-hooks-BmjMyXTa.js +114 -0
- package/dist/block-hooks-plbZ0AbJ.d.ts +107 -0
- package/dist/{kit-transport-DJlHQUBU.js → block-transport-BLr-ZgLc.js} +11 -7
- package/dist/block-transport-DvaoPuZ9.d.ts +48 -0
- package/dist/blocks/agents/index.d.ts +3 -0
- package/dist/blocks/agents/index.js +123 -0
- package/dist/blocks/ai-cache/index.d.ts +3 -0
- package/dist/blocks/ai-cache/index.js +3 -0
- package/dist/blocks/ai-chat/index.d.ts +31 -0
- package/dist/blocks/ai-chat/index.js +5 -0
- package/dist/blocks/ai-chat/react/index.d.ts +77 -0
- package/dist/blocks/ai-chat/react/index.js +387 -0
- package/dist/blocks/ai-chat/react/server.d.ts +16 -0
- package/dist/blocks/ai-chat/react/server.js +14 -0
- package/dist/blocks/ai-files/index.d.ts +14 -0
- package/dist/blocks/ai-files/index.js +3 -0
- package/dist/blocks/ai-providers/index.d.ts +7 -0
- package/dist/blocks/ai-providers/index.js +211 -0
- package/dist/blocks/ai-tasks/index.d.ts +3 -0
- package/dist/blocks/ai-tasks/index.js +203 -0
- package/dist/blocks/announcements/index.d.ts +3 -0
- package/dist/blocks/announcements/index.js +3 -0
- package/dist/blocks/announcements/react/index.d.ts +31 -0
- package/dist/blocks/announcements/react/index.js +86 -0
- package/dist/blocks/announcements/react/server.d.ts +6 -0
- package/dist/blocks/announcements/react/server.js +7 -0
- package/dist/blocks/api-keys/index.d.ts +35 -0
- package/dist/blocks/api-keys/index.js +334 -0
- package/dist/blocks/attachments/index.d.ts +210 -0
- package/dist/blocks/attachments/index.js +222 -0
- package/dist/blocks/audit/index.d.ts +366 -0
- package/dist/blocks/audit/index.js +714 -0
- package/dist/blocks/billing/index.d.ts +265 -0
- package/dist/blocks/billing/index.js +400 -0
- package/dist/blocks/comments/index.d.ts +200 -0
- package/dist/blocks/comments/index.js +207 -0
- package/dist/blocks/connectors/index.d.ts +3 -0
- package/dist/blocks/connectors/index.js +164 -0
- package/dist/blocks/data-lifecycle/index.d.ts +251 -0
- package/dist/blocks/data-lifecycle/index.js +353 -0
- package/dist/blocks/entitlements/index.d.ts +14 -0
- package/dist/blocks/entitlements/index.js +25 -0
- package/dist/blocks/flags/index.d.ts +226 -0
- package/dist/blocks/flags/index.js +282 -0
- package/dist/blocks/inbox/index.d.ts +6 -0
- package/dist/blocks/inbox/index.js +499 -0
- package/dist/blocks/inbox/react/index.d.ts +2 -0
- package/dist/blocks/inbox/react/index.js +225 -0
- package/dist/blocks/inbox/react/server.d.ts +10 -0
- package/dist/blocks/inbox/react/server.js +12 -0
- package/dist/blocks/index.d.ts +65 -0
- package/dist/blocks/index.js +77 -0
- package/dist/blocks/jobs/index.d.ts +297 -0
- package/dist/blocks/jobs/index.js +1051 -0
- package/dist/blocks/knowledge/index.d.ts +3 -0
- package/dist/blocks/knowledge/index.js +3 -0
- package/dist/blocks/memory/index.d.ts +8 -0
- package/dist/blocks/memory/index.js +289 -0
- package/dist/blocks/notifications/index.d.ts +297 -0
- package/dist/blocks/notifications/index.js +425 -0
- package/dist/blocks/notifications/react/index.d.ts +44 -0
- package/dist/blocks/notifications/react/index.js +67 -0
- package/dist/blocks/notifications/react/server.d.ts +6 -0
- package/dist/blocks/notifications/react/server.js +7 -0
- package/dist/blocks/onboarding/index.d.ts +3 -0
- package/dist/blocks/onboarding/index.js +64 -0
- package/dist/blocks/onboarding/react/index.d.ts +28 -0
- package/dist/blocks/onboarding/react/index.js +78 -0
- package/dist/blocks/onboarding/react/server.d.ts +7 -0
- package/dist/blocks/onboarding/react/server.js +7 -0
- package/dist/blocks/organizations/index.d.ts +241 -0
- package/dist/blocks/organizations/index.js +400 -0
- package/dist/blocks/outbox/index.d.ts +3 -0
- package/dist/blocks/outbox/index.js +266 -0
- package/dist/blocks/profiles/index.d.ts +40 -0
- package/dist/blocks/profiles/index.js +28 -0
- package/dist/blocks/push/index.d.ts +185 -0
- package/dist/blocks/push/index.js +258 -0
- package/dist/blocks/settings/index.d.ts +89 -0
- package/dist/blocks/settings/index.js +104 -0
- package/dist/blocks/sso/index.d.ts +198 -0
- package/dist/blocks/sso/index.js +1297 -0
- package/dist/blocks/usage/index.d.ts +4 -0
- package/dist/blocks/usage/index.js +233 -0
- package/dist/blocks/waitlist/index.d.ts +117 -0
- package/dist/blocks/waitlist/index.js +132 -0
- package/dist/blocks/webhooks/index.d.ts +304 -0
- package/dist/{webhooks → blocks/webhooks}/index.js +468 -148
- package/dist/blocks/workflow-builder/index.d.ts +21 -0
- package/dist/blocks/workflow-builder/index.js +5 -0
- package/dist/blocks/workflow-builder/react/index.d.ts +47 -0
- package/dist/blocks/workflow-builder/react/index.js +119 -0
- package/dist/blocks/workflow-builder/react/server.d.ts +8 -0
- package/dist/blocks/workflow-builder/react/server.js +11 -0
- package/dist/blocks/workflows/index.d.ts +3 -0
- package/dist/blocks/workflows/index.js +3 -0
- package/dist/blocks/workflows/react/index.d.ts +47 -0
- package/dist/blocks/workflows/react/index.js +137 -0
- package/dist/blocks/workflows/react/server.d.ts +8 -0
- package/dist/blocks/workflows/react/server.js +11 -0
- package/dist/{index-SaTsEfGP.d.ts → bucket-Cl46E3jS.d.ts} +134 -91
- package/dist/{simplify-_0oUKqCd.js → build-BXIzE6Xv.js} +222 -124
- package/dist/{channel-FQW-CnGR.d.ts → channel-_2Mx1Oyu.d.ts} +2 -2
- package/dist/channels-CkgQ_X60.js +114 -0
- package/dist/chat-sdk/index.d.ts +303 -0
- package/dist/chat-sdk/index.js +733 -0
- package/dist/chat-sdk/react/index.d.ts +2 -0
- package/dist/chat-sdk/react/index.js +3 -0
- package/dist/chat-sdk/react/server.d.ts +3 -0
- package/dist/chat-sdk/react/server.js +2 -0
- package/dist/claims-DDBQT6BK.js +61 -0
- package/dist/{claims-BAf6c7is.js → claims-PqzCxaTD.js} +39 -2
- package/dist/cli/bin.js +1 -1
- package/dist/cli/{codemod-DqV7612m.js → codemod-Bl_tQpHn.js} +105 -2
- package/dist/cli/config-B9HkL8nF.js +2 -0
- package/dist/cli/config-DU7EJImt.js +49 -0
- package/dist/cli/config-NiPxAmUF.js +1077 -0
- package/dist/cli/{dist-xn4wlboc.js → dist-DRAVaE6w.js} +111 -1
- package/dist/cli/{doctor-C_A-KEAz.js → doctor-B4JOm_ox.js} +991 -317
- package/dist/cli/{env-DmKv_G4A.js → env-CJLbUVPB.js} +1 -1
- package/dist/cli/{env-jIFUADLY.js → env-qXgyMV6r.js} +2 -2
- package/dist/cli/fresh-import-BeYf9I3x.js +40 -0
- package/dist/cli/gen-BGLb_zou.js +3100 -0
- package/dist/cli/gen-DAH8FM2W.js +2 -0
- package/dist/cli/index.d.ts +44 -5
- package/dist/cli/index.js +7 -7
- package/dist/cli/{init-DMa6M7hV.js → init-BIIwclPg.js} +242 -27
- package/dist/cli/{introspect-BKlaIu_M.js → introspect-DNUTuNvb.js} +5 -5
- package/dist/cli/{io-k41_z-jT.js → io-DI1mTdSA.js} +12 -3
- package/dist/cli/{keys-ByKHnvP9.js → keys-BGCnnb-s.js} +8 -6
- package/dist/cli/{ohash-D7bG_EOb.js → ohash-BjQY1cBb.js} +1 -1
- package/dist/cli/{openapi-Cp5Fyta-.js → openapi-BlRJ18s3.js} +2 -2
- package/dist/cli/{project-B_GeXyHh.js → project-DZRaBaY7.js} +11 -6
- package/dist/cli/read-sets-CETOSFx4.js +223 -0
- package/dist/cli/{run-Bo_NLT5n.js → run-DNWxHynX.js} +33 -23
- package/dist/cli/{seed-CUlqWh-8.js → seed-DQzqT7LP.js} +8 -6
- package/dist/cli/{skills-LJJIhLFR.js → skills-QQs4q68J.js} +3 -2
- package/dist/cli/sql-D0GUtUPp.js +536 -0
- package/dist/cli/{config-CPVpXP5I.js → supabase-toml-B-bIukQF.js} +72 -942
- package/dist/cli/typegen-version-DeFbtzYI.js +8 -0
- package/dist/cli/{version-BCtlgDQx.js → version-hr88OZG0.js} +1 -1
- package/dist/client/index.d.ts +3 -2
- package/dist/client/index.js +11 -71
- package/dist/client/native/index.d.ts +228 -0
- package/dist/client/native/index.js +361 -0
- package/dist/client-CI0Z2zWX.js +277 -0
- package/dist/cloud-event-BnmD5fb9.js +46 -0
- package/dist/cloud-event-C1JCc1bH.d.ts +50 -0
- package/dist/{compiler-BXH_aZ7N.d.ts → compiler-DY_wkj4R.d.ts} +2 -2
- package/dist/compose-BIBNgAnq.js +53 -0
- package/dist/composite-Bt1YK3Ke.d.ts +146 -0
- package/dist/composite-DhE66P5u.js +130 -0
- package/dist/config/index.d.ts +6 -4
- package/dist/config/index.js +5 -5
- package/dist/{config-Bv7gvfzC.d.ts → config-D44Y7PAv.d.ts} +406 -214
- package/dist/{config-DreERlKo.js → config-Dn7F5k9A.js} +62 -14
- package/dist/connector-Dv8kK0MO.d.ts +107 -0
- package/dist/connectors-hkLCwyEz.d.ts +120 -0
- package/dist/context-BCrWe7ow.d.ts +237 -0
- package/dist/credentials/index.d.ts +31 -0
- package/dist/credentials/index.js +168 -0
- package/dist/cron-DVPUg5k3.js +145 -0
- package/dist/csv-CPAzEmB6.js +25 -0
- package/dist/db-exception-CnWcYPxL.js +15 -0
- package/dist/{define-cNKzqwL_.js → define-B-TVjC25.js} +784 -227
- package/dist/{define-Bp_BZa5h.d.ts → define-CJJkkr1-.d.ts} +353 -61
- package/dist/edge/index.d.ts +69 -11
- package/dist/edge/index.js +191 -75
- package/dist/elysia/index.d.ts +59 -0
- package/dist/elysia/index.js +62 -0
- package/dist/embeddings-4ZvFzjsU.js +127 -0
- package/dist/embeddings-C9KGQnXt.d.ts +68 -0
- package/dist/{refresh-CFPX0Qft.d.ts → ended-jLfvw8h-.d.ts} +20 -2
- package/dist/{entitlements-BiJZ4t49.d.ts → entitlements-DHrY2UDZ.d.ts} +16 -7
- package/dist/entitlements-DRl6ZJUm.js +24 -0
- package/dist/envelope-DqnYGRw7.js +47 -0
- package/dist/{errors-DtwXa6b8.js → errors-B11Duf73.js} +82 -13
- package/dist/errors-DLjJHPLK.js +44 -0
- package/dist/escape-like-DHfx6jRr.js +7 -0
- package/dist/escape-like-ImTK0ps_.d.ts +15 -0
- package/dist/eve/index.d.ts +220 -0
- package/dist/eve/index.js +504 -0
- package/dist/eve-gT2Fpjut.d.ts +151 -0
- package/dist/events/index.d.ts +4 -3
- package/dist/events/index.js +9 -50
- package/dist/events-DWDhx77e.d.ts +549 -0
- package/dist/executor-Bq28caBo.js +718 -0
- package/dist/executor-C-R4pucm.js +83 -0
- package/dist/executor-CaWigTyR.d.ts +148 -0
- package/dist/executor-D14tGopJ.d.ts +36 -0
- package/dist/{executor-Cghdstwu.d.ts → executor-DbvExCFC.d.ts} +7 -1
- package/dist/{sql-Coh6n4cC.js → executor-De1Of3m3.js} +210 -11
- package/dist/executor-DvsBbNBS.d.ts +261 -0
- package/dist/expo/index.d.ts +79 -0
- package/dist/expo/index.js +129 -0
- package/dist/expo-sqlite/index.d.ts +58 -0
- package/dist/expo-sqlite/index.js +104 -0
- package/dist/field-errors-BFOa7WhB.d.ts +99 -0
- package/dist/field-errors-VF1yb8yI.js +13 -0
- package/dist/framework-CONDyPYx.js +51 -0
- package/dist/framework-DuVb4Xa8.d.ts +25 -0
- package/dist/gateway-C1ssRHnf.d.ts +130 -0
- package/dist/graph-BGyLLFLX.js +265 -0
- package/dist/h3/index.d.ts +59 -0
- package/dist/h3/index.js +55 -0
- package/dist/h3/v1/index.d.ts +63 -0
- package/dist/h3/v1/index.js +49 -0
- package/dist/history-BZih6SFP.js +64 -0
- package/dist/hono/index.d.ts +56 -7
- package/dist/hono/index.js +97 -45
- package/dist/{react-C_s7kiNz.js → hooks-1BTCJsiw.js} +104 -133
- package/dist/http-Cv4T29xh.js +40 -0
- package/dist/http-DdYadv-o.js +126 -0
- package/dist/image-url-pIVlO6R1.js +41 -0
- package/dist/{impersonation-BgSHAYLf.js → impersonation-DcqaTBHx.js} +21 -6
- package/dist/inbox-i7E1ppo6.d.ts +384 -0
- package/dist/index-ACiNPN4Z.d.ts +74 -0
- package/dist/{index-DgILJRXj.d.ts → index-Bepi-Hgg.d.ts} +91 -28
- package/dist/index-BkWT_uBO.d.ts +51 -0
- package/dist/index-BpFYMsi0.d.ts +53 -0
- package/dist/{index-C7f8cOBM.d.ts → index-Bxih3l0u.d.ts} +2 -2
- package/dist/index-CaxrMD1A.d.ts +1 -0
- package/dist/index-ClmjK_o-.d.ts +55 -0
- package/dist/index-CtuakVRc.d.ts +267 -0
- package/dist/index-D48jVS-N.d.ts +246 -0
- package/dist/index-DDrYaUcq.d.ts +138 -0
- package/dist/index-DQh4VCc9.d.ts +98 -0
- package/dist/{live-D9W2M5aO.d.ts → index-DunNRdnT.d.ts} +136 -38
- package/dist/index.d.ts +65 -18
- package/dist/index.js +42 -14
- package/dist/invalidate-BBqhpxyB.d.ts +19 -0
- package/dist/json-CEnpTHlZ.js +16 -0
- package/dist/json-schema-CvER4kgT.js +502 -0
- package/dist/keys-4MnkWTTh.js +35 -0
- package/dist/kit-B5tid-q5.d.ts +60 -0
- package/dist/kit-Fm-Fv7q5.js +78 -0
- package/dist/knowledge-BXBbT0Tr.d.ts +176 -0
- package/dist/knowledge-CDmiMLew.js +255 -0
- package/dist/lint/index.d.ts +1 -1
- package/dist/lint/index.js +37 -0
- package/dist/list/index.d.ts +3 -2
- package/dist/list/index.js +1 -444
- package/dist/{index-fag8VJZ2.d.ts → list-query-BRt-zDF7.d.ts} +20 -12
- package/dist/list-query-gl808ac1.js +459 -0
- package/dist/{live-CdJBOc4f.js → live-CTAZQkwl.js} +99 -14
- package/dist/logger-DWVYUmwi.d.ts +18 -0
- package/dist/mcp/index.d.ts +38 -11
- package/dist/mcp/index.js +82 -46
- package/dist/mcp/sdk/index.d.ts +105 -0
- package/dist/mcp/sdk/index.js +126 -0
- package/dist/message-CaKlqdsX.js +219 -0
- package/dist/message-CfDoR0Y6.d.ts +94 -0
- package/dist/messages-B9-5887d.js +715 -0
- package/dist/messages-DYP0sUTe.d.ts +59 -0
- package/dist/mfa-Daak8ukG.d.ts +54 -0
- package/dist/modules-DpPwzC7E.d.ts +192 -0
- package/dist/modules-dxrVK3ID.js +228 -0
- package/dist/nestjs/index.d.ts +68 -0
- package/dist/nestjs/index.js +164 -0
- package/dist/next/client/index.d.ts +35 -0
- package/dist/next/client/index.js +36 -0
- package/dist/next/image/index.js +3 -24
- package/dist/next/index.d.ts +150 -51
- package/dist/next/index.js +261 -107
- package/dist/node/index.d.ts +4 -0
- package/dist/node/index.js +210 -0
- package/dist/node-headers-AO06VhqH.d.ts +24 -0
- package/dist/node-headers-C71zNQHe.js +55 -0
- package/dist/nuxt/index.d.ts +60 -0
- package/dist/nuxt/index.js +64 -0
- package/dist/onboarding-BZGHYJks.d.ts +62 -0
- package/dist/openapi/index.d.ts +1 -1
- package/dist/openapi/index.js +3 -3
- package/dist/orpc/index.d.ts +70 -11
- package/dist/orpc/index.js +73 -24
- package/dist/otel/index.d.ts +7 -7
- package/dist/otel/index.js +87 -57
- package/dist/outbox-Cq7pPYvl.d.ts +161 -0
- package/dist/{plugin-o144f322.d.ts → plugin-FGY8UhYm.d.ts} +30 -10
- package/dist/plugin-tfXW1-qO.js +45 -0
- package/dist/plugins/actor/index.d.ts +2 -2
- package/dist/plugins/actor/index.js +3 -3
- package/dist/plugins/rules/index.d.ts +11 -4
- package/dist/plugins/rules/index.js +40 -13
- package/dist/plugins/soft-delete/index.d.ts +5 -4
- package/dist/plugins/soft-delete/index.js +10 -5
- package/dist/plugins/tenant/index.d.ts +2 -2
- package/dist/plugins/tenant/index.js +18 -11
- package/dist/plugins/timestamps/index.d.ts +5 -3
- package/dist/plugins/timestamps/index.js +6 -4
- package/dist/plugins/validation/index.d.ts +2 -2
- package/dist/plugins/validation/index.js +10 -6
- package/dist/{pool-C9voM4Kw.d.ts → pool-CAu2nLrL.d.ts} +2 -2
- package/dist/postgres/index.d.ts +4 -4
- package/dist/postgres/index.js +1 -2
- package/dist/{postgrest-CruYKAvV.d.ts → postgrest-BLVdEnfe.d.ts} +13 -3
- package/dist/powersync/index.d.ts +43 -0
- package/dist/powersync/index.js +199 -0
- package/dist/powersync/react/index.d.ts +87 -0
- package/dist/powersync/react/index.js +108 -0
- package/dist/{problem-BW1KhzSG.js → problem-BreOl8b8.js} +14 -3
- package/dist/{problem-Blxe8S31.d.ts → problem-DD8LZFjc.d.ts} +21 -3
- package/dist/provider-BH5UWZJ6.d.ts +113 -0
- package/dist/provider-Di3sRutW.js +57 -0
- package/dist/query/index.d.ts +4 -2
- package/dist/query/index.js +4 -3
- package/dist/{query-CCbo44lG.js → query-BQ-NaZon.js} +141 -24
- package/dist/{queue-BYUCjr59.d.ts → queue-CLJurhdy.d.ts} +258 -83
- package/dist/react/index.d.ts +11 -7
- package/dist/react/index.js +7 -3
- package/dist/react/native/index.d.ts +116 -0
- package/dist/react/native/index.js +130 -0
- package/dist/react/server.d.ts +28 -10
- package/dist/react/server.js +22 -5
- package/dist/react/session.d.ts +2 -2
- package/dist/react-4ZqkP8da.js +79 -0
- package/dist/react-F80mF4-t.js +409 -0
- package/dist/react-router/index.d.ts +83 -0
- package/dist/react-router/index.js +89 -0
- package/dist/react-topic-seAWjL3_.js +38 -0
- package/dist/{read-set-DQFt1N_Y.js → read-set-DuKWC9Mj.js} +67 -25
- package/dist/realtime/index.d.ts +4 -5
- package/dist/realtime/index.js +144 -82
- package/dist/refusal-CeJWxmkH.js +61 -0
- package/dist/refusal-D1rgbzNq.d.ts +15 -0
- package/dist/replicas-CwrYp5Zj.js +94 -0
- package/dist/{view-Bn6HNKUe.js → resolve-D_IcnvFd.js} +248 -240
- package/dist/{impersonation-BmePsuxz.d.ts → resolve-KW8nCeL6.d.ts} +29 -69
- package/dist/{resource-mqfJW4YF.js → resource-BsvzCMlX.js} +4 -4
- package/dist/{resource-DNRFP7KR.d.ts → resource-D6Uc7Sk7.d.ts} +5 -5
- package/dist/respond-BUgsXNgk.js +91 -0
- package/dist/respond-DoBijysA.d.ts +93 -0
- package/dist/result-D7y2fT87.d.ts +282 -0
- package/dist/{result-BKiliPY7.js → result-DzUbqgdW.js} +27 -2
- package/dist/run-state-CPo-1X2B.d.ts +12 -0
- package/dist/run-state-CgBKzYMW.js +13 -0
- package/dist/search-CUWCRYKL.js +9 -0
- package/dist/server/index.d.ts +143 -22
- package/dist/server/index.js +46 -14
- package/dist/{respond-BR1fJWOB.d.ts → server-CeVeeXwz.d.ts} +224 -85
- package/dist/session-ChiXDZdZ.d.ts +72 -0
- package/dist/shared-CBxosKK3.js +75 -0
- package/dist/{shared-D1dRUrAg.js → shared-CxWmcyqB.js} +2 -1
- package/dist/shared-DDSjEGDR.d.ts +11 -0
- package/dist/sign-in-BCV5S2WZ.d.ts +49 -0
- package/dist/sign-in-Cb1mwv6X.js +86 -0
- package/dist/{signers-Cc6r98SW.d.ts → signers-4y62_VIi.d.ts} +13 -9
- package/dist/simplify-DDO0j0LI.js +83 -0
- package/dist/solid/index.d.ts +131 -0
- package/dist/solid/index.js +344 -0
- package/dist/solid-start/index.d.ts +75 -0
- package/dist/solid-start/index.js +58 -0
- package/dist/{spec-pins-C3oc-Tdp.js → spec-pins--j6mTok4.js} +8 -1
- package/dist/{spec-pins-CqorWDnO.d.ts → spec-pins-C89s6DyW.d.ts} +7 -0
- package/dist/sql/index.d.ts +313 -212
- package/dist/sql/index.js +34872 -6579
- package/dist/ssr/index.d.ts +6 -5
- package/dist/ssr/index.js +3 -3
- package/dist/{standard-D7r6dMUu.js → standard-BK0cqJ7y.js} +4 -3
- package/dist/{standard-J4YgMOLm.d.ts → standard-CYsm7xpT.d.ts} +1 -1
- package/dist/stats-D_AD9GGu.js +1123 -0
- package/dist/storage/index.d.ts +5 -5
- package/dist/storage/index.js +1027 -368
- package/dist/store-BdH5ruDx.js +66 -0
- package/dist/store-CGjgWu5n.d.ts +68 -0
- package/dist/streams/index.d.ts +76 -0
- package/dist/streams/index.js +77 -0
- package/dist/streams/redis/index.d.ts +52 -0
- package/dist/streams/redis/index.js +185 -0
- package/dist/stripe-BMj5LtsT.js +38 -0
- package/dist/stripe-D04h-QBT.d.ts +136 -0
- package/dist/{support-wHnK6zp-.js → support-BivNqSpK.js} +32 -11
- package/dist/{support-view-B8SlToQX.d.ts → support-view-33Oi07TT.d.ts} +1 -1
- package/dist/svelte/index.d.ts +114 -0
- package/dist/svelte/index.js +344 -0
- package/dist/sveltekit/index.d.ts +116 -0
- package/dist/sveltekit/index.js +106 -0
- package/dist/tags-B99_U9Pp.js +39 -0
- package/dist/tags-Cx-LnIQP.d.ts +30 -0
- package/dist/tanstack-db/index.d.ts +71 -0
- package/dist/tanstack-db/index.js +72 -0
- package/dist/tanstack-start/index.d.ts +75 -0
- package/dist/tanstack-start/index.js +95 -0
- package/dist/tee-CoEppCER.js +101 -0
- package/dist/{template-TxrGI5jR.js → template--jDkl3UP.js} +34 -13
- package/dist/template-CwO38Do8.d.ts +6 -0
- package/dist/temporal-BJGitm-h.js +79 -0
- package/dist/{temporal-required-CAt4Dc9Z.js → temporal-required-BVvW7SYJ.js} +3 -2
- package/dist/tenant-I1TEZ_BX.js +27 -0
- package/dist/tenant-icPeuUBN.d.ts +17 -0
- package/dist/testing/index.d.ts +315 -41
- package/dist/testing/index.js +1046 -79
- package/dist/timing-CyTQPubY.js +92 -0
- package/dist/ttl-Hr34gRu8.js +12 -0
- package/dist/{types-Dnf_wRDz.d.ts → types-6BdhkmZe.d.ts} +111 -45
- package/dist/types-BI-0Sl2v.d.ts +359 -0
- package/dist/types-C05bxW-W.d.ts +220 -0
- package/dist/types-DGs5L0VN.d.ts +192 -0
- package/dist/types-TaiEeBGA.d.ts +156 -0
- package/dist/usage-CrvrL2vw.d.ts +177 -0
- package/dist/user-change-XzvgafU4.js +130 -0
- package/dist/vault-CSO7ss5C.d.ts +50 -0
- package/dist/vercel-connect/index.d.ts +47 -0
- package/dist/vercel-connect/index.js +119 -0
- package/dist/{verify-Ce49ETPz.js → verify-DL41hbvY.js} +85 -13
- package/dist/verify-DPuWngD4.d.ts +172 -0
- package/dist/{version-DOYTYwHP.js → version-Bvv6aLjZ.js} +1 -1
- package/dist/view-BeFcRV6G.js +99 -0
- package/dist/{view-CbgObAeb.d.ts → view-DbxLNu8L.d.ts} +22 -5
- package/dist/vue/index.d.ts +141 -0
- package/dist/vue/index.js +362 -0
- package/dist/watch-DI34G86j.d.ts +60 -0
- package/dist/watch-Xuxrvxq-.js +122 -0
- package/dist/{wire-D2dT86vi.js → wire-DtZQ4sdE.js} +15 -5
- package/dist/workflow-builder-DJ1oHn8g.js +398 -0
- package/dist/workflow-sdk/builder/index.d.ts +85 -0
- package/dist/workflow-sdk/builder/index.js +250 -0
- package/dist/workflow-sdk/index.d.ts +14 -0
- package/dist/workflow-sdk/index.js +3 -0
- package/dist/workflow-sdk/world/index.d.ts +107 -0
- package/dist/workflow-sdk/world/index.js +702 -0
- package/dist/workflow-sdk-B3yLD6Br.d.ts +98 -0
- package/dist/workflow-sdk-C2WJ_G2O.js +173 -0
- package/dist/workflow-sdk-world-D-prir6O.js +1397 -0
- package/dist/workflows-B0j9WKPu.d.ts +185 -0
- package/dist/workflows-wu1WWBn7.js +216 -0
- package/package.json +461 -20
- package/schemas/ai-message-v1.json +258 -0
- package/schemas/config-v1.json +1074 -140
- package/schemas/doctor-report-v1.json +20 -1
- package/schemas/snapshot-v2.json +8 -0
- package/skills/better-supabase/SKILL.md +41 -12
- package/skills/better-supabase/references/frontend.md +96 -0
- package/skills/better-supabase/references/plugins.md +10 -6
- package/skills/better-supabase/references/troubleshooting.md +2 -1
- package/skills/better-supabase-api/SKILL.md +76 -22
- package/skills/better-supabase-api/references/adapters.md +207 -0
- package/skills/better-supabase-api/references/ai.md +125 -0
- package/skills/better-supabase-api/references/expo.md +65 -0
- package/skills/better-supabase-api/references/powersync.md +35 -0
- package/skills/better-supabase-api/references/streams-credentials.md +105 -0
- package/skills/better-supabase-auth/SKILL.md +47 -11
- package/skills/better-supabase-auth/references/authorization-providers.md +51 -0
- package/skills/better-supabase-testing/SKILL.md +3 -1
- package/dist/cli/config-dtoekM0v.js +0 -2
- package/dist/cli/gen-CIdSQZad.js +0 -2
- package/dist/cli/gen-CjElD1Vw.js +0 -2241
- package/dist/cli/read-sets-VQcPgxU9.js +0 -436
- package/dist/cli/sql-CVx-1O8G.js +0 -373
- package/dist/entitlements-CTEBm7IB.js +0 -20
- package/dist/events-aIO7vsIk.d.ts +0 -230
- package/dist/executor-BunPOO-k.d.ts +0 -402
- package/dist/executor-XycESQPH.js +0 -94
- package/dist/index-CWMDdEr4.d.ts +0 -50
- package/dist/index-DOESKG27.d.ts +0 -100
- package/dist/index-DmXSxbLi.d.ts +0 -62
- package/dist/index-Dp2VDwog.d.ts +0 -161
- package/dist/jobs/index.d.ts +0 -226
- package/dist/jobs/index.js +0 -1007
- package/dist/json-schema-m1fiwKgN.js +0 -208
- package/dist/kit-events-CTFDfSwb.js +0 -60
- package/dist/kit-transport-B82k1djG.d.ts +0 -33
- package/dist/kits-CGLLrNPd.js +0 -157
- package/dist/mfa-Cu96oJdw.d.ts +0 -30
- package/dist/notifications/index.d.ts +0 -162
- package/dist/notifications/index.js +0 -264
- package/dist/orgs/index.d.ts +0 -132
- package/dist/orgs/index.js +0 -245
- package/dist/permdock-sql-C2Inc6Xc.d.ts +0 -29
- package/dist/permdock-sql-DgOfeixi.js +0 -51
- package/dist/plugin-DwARMuQp.js +0 -59
- package/dist/server-B1mZ7CgQ.js +0 -660
- package/dist/stats-Bqob3WqT.js +0 -592
- package/dist/support-CEPwlw2G.d.ts +0 -89
- package/dist/template-mbvlysOo.d.ts +0 -6
- package/dist/temporal-CVlIV2DZ.js +0 -40
- package/dist/user-change-CHFEUro1.js +0 -49
- package/dist/webhooks/index.d.ts +0 -285
- package/skills/better-supabase-auth/references/permdock.md +0 -122
|
@@ -0,0 +1,1106 @@
|
|
|
1
|
+
import { c as mapDbError, n as dbError, o as isDbError } from "./errors-B11Duf73.js";
|
|
2
|
+
import { a as ok, r as err, t as AsyncResult } from "./result-DzUbqgdW.js";
|
|
3
|
+
import { a as postgrestExecutor, n as StatsRecorder } from "./stats-D_AD9GGu.js";
|
|
4
|
+
import { a as problemResponse } from "./problem-BreOl8b8.js";
|
|
5
|
+
import { loadEnv } from "./env/index.js";
|
|
6
|
+
import { a as actorOf, i as userContext, n as impersonatorClaim, t as actClaim } from "./impersonation-DcqaTBHx.js";
|
|
7
|
+
import { t as fromPgError } from "./executor-De1Of3m3.js";
|
|
8
|
+
import { n as forwardBlockEvents } from "./cloud-event-BnmD5fb9.js";
|
|
9
|
+
import { a as replicaState, i as primaryCookieWrite, o as routedExecutor, r as pinnedUntil, s as withPrimaryPin } from "./replicas-CwrYp5Zj.js";
|
|
10
|
+
import { c as AUTH_CACHE_HEADERS, d as clearSessionAtScopes, f as parseCookies, h as sessionCookieName, i as resolveAuth, m as serializeCookie, r as prefetchJwks, t as authContext } from "./resolve-D_IcnvFd.js";
|
|
11
|
+
import { t as credentialProviderOf } from "./provider-Di3sRutW.js";
|
|
12
|
+
import { a as respond, n as defaultPrefetchJwks, r as guard, t as defaultExpose } from "./respond-BUgsXNgk.js";
|
|
13
|
+
import { createClient } from "@supabase/supabase-js";
|
|
14
|
+
import { defineMiddleware, getEnv, pipeline, seedContext } from "@supabase/middleware";
|
|
15
|
+
import { PostgrestClient } from "@supabase/postgrest-js";
|
|
16
|
+
//#region src/server/delete-account.ts
|
|
17
|
+
const REMOVE_BATCH = 1e3;
|
|
18
|
+
/** Maps an `AuthApiError` from `auth.admin.deleteUser` to a `DbError`. */
|
|
19
|
+
function fromAuthError(raw) {
|
|
20
|
+
if (isDbError(raw)) return raw;
|
|
21
|
+
const failure = typeof raw === "object" && raw !== null ? raw : {};
|
|
22
|
+
const message = failure.message ?? "Auth request failed";
|
|
23
|
+
const base = {
|
|
24
|
+
table: "auth.users",
|
|
25
|
+
...failure.code ? { code: failure.code } : {}
|
|
26
|
+
};
|
|
27
|
+
if (failure.code === "user_not_found" || failure.status === 404) return dbError("not_found", message, base);
|
|
28
|
+
if (/database error deleting user/i.test(message)) return dbError("conflict", message, {
|
|
29
|
+
...base,
|
|
30
|
+
hint: "A foreign key to auth.users without on delete cascade or set null blocks the delete (doctor BS406)"
|
|
31
|
+
});
|
|
32
|
+
if (failure.status === 401) return dbError("unauthorized", message, base);
|
|
33
|
+
if (failure.status === 403) return dbError("forbidden", message, base);
|
|
34
|
+
return failure.name === "AuthRetryableFetchError" || (failure.status ?? 0) >= 500 ? dbError("network", message, base) : dbError("unexpected", message, base);
|
|
35
|
+
}
|
|
36
|
+
const UUID$1 = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
37
|
+
const PROBE = "better_supabase.delete_account_probe";
|
|
38
|
+
/**
|
|
39
|
+
* The database error that refuses deleting `userId`, from the delete and its
|
|
40
|
+
* deferred checks run in a `do` block that always fails, so nothing is
|
|
41
|
+
* deleted. Undefined when the delete itself would pass.
|
|
42
|
+
*/
|
|
43
|
+
async function databaseRefusal(sql, userId) {
|
|
44
|
+
if (!UUID$1.test(userId)) return void 0;
|
|
45
|
+
try {
|
|
46
|
+
await sql.queryRaw(`do $$
|
|
47
|
+
begin
|
|
48
|
+
delete from auth.users where id = '${userId}';
|
|
49
|
+
set constraints all immediate;
|
|
50
|
+
raise exception '${PROBE}';
|
|
51
|
+
end;
|
|
52
|
+
$$`);
|
|
53
|
+
} catch (cause) {
|
|
54
|
+
const raw = isDbError(cause) ? void 0 : fromPgError(cause);
|
|
55
|
+
const error = isDbError(cause) ? cause : raw && mapDbError(raw);
|
|
56
|
+
if (!error || error.message === PROBE) return void 0;
|
|
57
|
+
return {
|
|
58
|
+
...error,
|
|
59
|
+
table: "auth.users"
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Deletes the Auth user, then removes their Storage objects, then announces
|
|
65
|
+
* the delete. Rows referencing `auth.users` follow their foreign keys. The
|
|
66
|
+
* objects are listed first and removed only once the user is gone, so a
|
|
67
|
+
* delete the database refuses leaves them in place.
|
|
68
|
+
*
|
|
69
|
+
* `server.deleteAccount` calls it with the server's service client. Apps on
|
|
70
|
+
* the `@supabase/server` pipeline call it with theirs:
|
|
71
|
+
* `deleteAccount(betterSupabase, ctx.supabaseAdmin, userId)`.
|
|
72
|
+
*/
|
|
73
|
+
function deleteAccount(betterSupabase, service, userId, options = {}) {
|
|
74
|
+
return AsyncResult.from(async () => {
|
|
75
|
+
const client = typeof service === "function" ? service() : service;
|
|
76
|
+
const listedPaths = [];
|
|
77
|
+
for (const bucket of options.buckets ?? []) {
|
|
78
|
+
const param = bucket.owner;
|
|
79
|
+
if (!param) continue;
|
|
80
|
+
options.signal?.throwIfAborted();
|
|
81
|
+
const listed = await bucket.connect(client, { allTenants: true }).list({ [param]: userId }, options.signal ? { signal: options.signal } : {});
|
|
82
|
+
if (!listed.ok) return listed;
|
|
83
|
+
listedPaths.push([bucket, listed.data.map((object) => object.path).filter((path) => {
|
|
84
|
+
return bucket.match(path)?.[param] === userId;
|
|
85
|
+
})]);
|
|
86
|
+
}
|
|
87
|
+
options.signal?.throwIfAborted();
|
|
88
|
+
try {
|
|
89
|
+
const { error } = await client.auth.admin.deleteUser(userId);
|
|
90
|
+
if (error) {
|
|
91
|
+
const failure = fromAuthError(error);
|
|
92
|
+
const refusal = options.sql && (failure.kind === "conflict" || failure.kind === "network" || failure.kind === "unexpected") ? await databaseRefusal(options.sql, userId) : void 0;
|
|
93
|
+
return err(refusal ?? failure);
|
|
94
|
+
}
|
|
95
|
+
} catch (cause) {
|
|
96
|
+
return err(fromAuthError(cause));
|
|
97
|
+
}
|
|
98
|
+
const removed = {};
|
|
99
|
+
for (const [bucket, paths] of listedPaths) {
|
|
100
|
+
const storage = bucket.connect(client, { allTenants: true });
|
|
101
|
+
for (let start = 0; start < paths.length; start += REMOVE_BATCH) {
|
|
102
|
+
const batch = await storage.remove(paths.slice(start, start + REMOVE_BATCH));
|
|
103
|
+
if (!batch.ok) return batch;
|
|
104
|
+
}
|
|
105
|
+
removed[bucket.id] = paths.length;
|
|
106
|
+
}
|
|
107
|
+
const context = options.context ?? {};
|
|
108
|
+
betterSupabase.events.emit("mutation", {
|
|
109
|
+
table: "auth.users",
|
|
110
|
+
kind: "delete",
|
|
111
|
+
rows: [{ id: userId }],
|
|
112
|
+
context
|
|
113
|
+
});
|
|
114
|
+
for (const table of options.cascades ?? []) betterSupabase.events.emit("mutation", {
|
|
115
|
+
table,
|
|
116
|
+
kind: "delete",
|
|
117
|
+
rows: [],
|
|
118
|
+
context
|
|
119
|
+
});
|
|
120
|
+
return ok({
|
|
121
|
+
userId,
|
|
122
|
+
removed
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/server/suspend-account.ts
|
|
128
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
129
|
+
const FOREVER = "876000h";
|
|
130
|
+
const END_SESSIONS = `with ended as (
|
|
131
|
+
delete from auth.sessions where user_id = $1::uuid returning id
|
|
132
|
+
), tokens as (
|
|
133
|
+
delete from auth.refresh_tokens where user_id = $1::text
|
|
134
|
+
)
|
|
135
|
+
select count(*)::int as ended from ended`;
|
|
136
|
+
function invalidUser(userId) {
|
|
137
|
+
return dbError("invalid_request", `"${userId}" is not a user id`, { table: "auth.users" });
|
|
138
|
+
}
|
|
139
|
+
function fromSqlError(cause) {
|
|
140
|
+
if (isDbError(cause)) return cause;
|
|
141
|
+
const raw = fromPgError(cause);
|
|
142
|
+
if (raw) return {
|
|
143
|
+
...mapDbError(raw),
|
|
144
|
+
table: "auth.sessions"
|
|
145
|
+
};
|
|
146
|
+
return dbError("unexpected", cause instanceof Error ? cause.message : "Ending the sessions failed", { table: "auth.sessions" });
|
|
147
|
+
}
|
|
148
|
+
async function deleteSessions(sql, userId) {
|
|
149
|
+
const [row] = await sql.queryRaw(END_SESSIONS, [userId]);
|
|
150
|
+
return row?.ended ?? 0;
|
|
151
|
+
}
|
|
152
|
+
function endSessions(sql, userId) {
|
|
153
|
+
return AsyncResult.from(async () => {
|
|
154
|
+
if (!UUID.test(userId)) return err(invalidUser(userId));
|
|
155
|
+
try {
|
|
156
|
+
return ok({
|
|
157
|
+
userId,
|
|
158
|
+
ended: await deleteSessions(sql, userId)
|
|
159
|
+
});
|
|
160
|
+
} catch (cause) {
|
|
161
|
+
return err(fromSqlError(cause));
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
async function ban(client, userId, duration) {
|
|
166
|
+
try {
|
|
167
|
+
const { error } = await client.auth.admin.updateUserById(userId, { ban_duration: duration });
|
|
168
|
+
return error ? fromAuthError(error) : void 0;
|
|
169
|
+
} catch (cause) {
|
|
170
|
+
return fromAuthError(cause);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function suspendAccount(service, sql, userId, options) {
|
|
174
|
+
return AsyncResult.from(async () => {
|
|
175
|
+
if (!UUID.test(userId)) return err(invalidUser(userId));
|
|
176
|
+
const client = typeof service === "function" ? service() : service;
|
|
177
|
+
const { suspended } = options;
|
|
178
|
+
if (suspended) {
|
|
179
|
+
const refused = await ban(client, userId, options.duration ?? FOREVER);
|
|
180
|
+
if (refused) return err(refused);
|
|
181
|
+
}
|
|
182
|
+
let ended;
|
|
183
|
+
try {
|
|
184
|
+
ended = await deleteSessions(sql, userId);
|
|
185
|
+
} catch (cause) {
|
|
186
|
+
return err(fromSqlError(cause));
|
|
187
|
+
}
|
|
188
|
+
if (!suspended) {
|
|
189
|
+
const refused = await ban(client, userId, "none");
|
|
190
|
+
if (refused) return err(refused);
|
|
191
|
+
}
|
|
192
|
+
return ok({
|
|
193
|
+
userId,
|
|
194
|
+
suspended,
|
|
195
|
+
ended
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/server/audit.ts
|
|
201
|
+
/** The CloudEvents `source` of the events the server sends to its `audit` sink. */
|
|
202
|
+
const SERVER_EVENT_SOURCE = "/better-supabase/server";
|
|
203
|
+
const OFF = { account: () => void 0 };
|
|
204
|
+
/**
|
|
205
|
+
* The server's audit trail: account actions are sent to `sink` as CloudEvents
|
|
206
|
+
* typed `dev.better-supabase.account.*` with subject `users/<id>`, and
|
|
207
|
+
* `support.denied` block events are forwarded to it.
|
|
208
|
+
*/
|
|
209
|
+
function serverAudit(betterSupabase, sink) {
|
|
210
|
+
if (sink === void 0) return OFF;
|
|
211
|
+
const { events } = betterSupabase;
|
|
212
|
+
forwardBlockEvents(betterSupabase, sink, {
|
|
213
|
+
source: SERVER_EVENT_SOURCE,
|
|
214
|
+
types: ["support.denied"]
|
|
215
|
+
});
|
|
216
|
+
const report = (cause) => {
|
|
217
|
+
events.logger.error("audit sink failed", { cause });
|
|
218
|
+
};
|
|
219
|
+
return { account(type, userId, data) {
|
|
220
|
+
const event = {
|
|
221
|
+
specversion: "1.0",
|
|
222
|
+
id: crypto.randomUUID(),
|
|
223
|
+
source: SERVER_EVENT_SOURCE,
|
|
224
|
+
type: `dev.better-supabase.${type}`,
|
|
225
|
+
subject: `users/${userId}`,
|
|
226
|
+
time: (/* @__PURE__ */ new Date()).toISOString(),
|
|
227
|
+
datacontenttype: "application/json",
|
|
228
|
+
data: {
|
|
229
|
+
userId,
|
|
230
|
+
...data
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
try {
|
|
234
|
+
events.track(Promise.resolve(sink.send([event])).catch(report));
|
|
235
|
+
} catch (cause) {
|
|
236
|
+
report(cause);
|
|
237
|
+
}
|
|
238
|
+
} };
|
|
239
|
+
}
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/server/entries/core.ts
|
|
242
|
+
const cores = /* @__PURE__ */ new WeakMap();
|
|
243
|
+
/** Records the core behind `server`, so `serverCore` finds it on copies too. */
|
|
244
|
+
function registerCore(server, core) {
|
|
245
|
+
cores.set(server, core);
|
|
246
|
+
}
|
|
247
|
+
/** Copies the registration from `from` to `to` (`extendServer` builds a new object). */
|
|
248
|
+
function shareCore(from, to) {
|
|
249
|
+
const core = cores.get(from);
|
|
250
|
+
if (core) cores.set(to, core);
|
|
251
|
+
}
|
|
252
|
+
/** The core of a server built by `createServer` or one of the adapter factories. */
|
|
253
|
+
function serverCore(server) {
|
|
254
|
+
const core = cores.get(server);
|
|
255
|
+
if (!core) throw new TypeError("better-supabase: this server was not built by createServer(); pass the definition or a server from createServer");
|
|
256
|
+
return core;
|
|
257
|
+
}
|
|
258
|
+
const CALL = Symbol("better-supabase.call");
|
|
259
|
+
const supportLookups = /* @__PURE__ */ new WeakMap();
|
|
260
|
+
/**
|
|
261
|
+
* Starts the support lookup for a signed-in caller, so `withTenant` can run
|
|
262
|
+
* the tenant resolver while the support store answers. Other callers never
|
|
263
|
+
* have a support session, so they skip it.
|
|
264
|
+
*/
|
|
265
|
+
function startSupportLookup(core, request, session) {
|
|
266
|
+
if (session.auth.kind !== "user" || supportLookups.has(session)) return;
|
|
267
|
+
const lookup = core.support(request, session.auth);
|
|
268
|
+
lookup.catch(() => void 0);
|
|
269
|
+
supportLookups.set(session, lookup);
|
|
270
|
+
}
|
|
271
|
+
/** The support session of a resolved request: the started lookup, else a new one. */
|
|
272
|
+
function supportLookup(core, request, session) {
|
|
273
|
+
return supportLookups.get(session) ?? core.support(request, session.auth);
|
|
274
|
+
}
|
|
275
|
+
/** The call state on a context seeded by `server.context()`, if any. */
|
|
276
|
+
function callOf(ctx) {
|
|
277
|
+
return ctx[CALL];
|
|
278
|
+
}
|
|
279
|
+
//#endregion
|
|
280
|
+
//#region src/server/entries/context.ts
|
|
281
|
+
/**
|
|
282
|
+
* Entry contributing `ctx.bs`, the request's `ServerContext`: `auth`, `db`,
|
|
283
|
+
* `sql`, `supabase`, `replica`, `stats()` and the rest, each built on first
|
|
284
|
+
* read. In a support session `auth`, `db` and `sql` are the target's.
|
|
285
|
+
*/
|
|
286
|
+
function withServerContext(core) {
|
|
287
|
+
return defineMiddleware({
|
|
288
|
+
key: "bs",
|
|
289
|
+
run: () => (request, ctx) => {
|
|
290
|
+
const options = callOf(ctx)?.options ?? {};
|
|
291
|
+
const bs = core.context(ctx.session, request, ctx.support ? {
|
|
292
|
+
...options,
|
|
293
|
+
support: ctx.support
|
|
294
|
+
} : options, ctx.tenant);
|
|
295
|
+
return Promise.resolve({ bs });
|
|
296
|
+
}
|
|
297
|
+
})();
|
|
298
|
+
}
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/server/refresh.ts
|
|
301
|
+
/** A router or browser prefetch, by its request headers. */
|
|
302
|
+
function isPrefetch(request) {
|
|
303
|
+
const h = request.headers;
|
|
304
|
+
return h.has("next-router-prefetch") || h.get("purpose") === "prefetch" || (h.get("sec-purpose")?.includes("prefetch") ?? false);
|
|
305
|
+
}
|
|
306
|
+
const FORM_TYPES = ["application/x-www-form-urlencoded", "multipart/form-data"];
|
|
307
|
+
/**
|
|
308
|
+
* Whether a request should refresh an expired session cookie: page loads,
|
|
309
|
+
* client navigations (`rsc`, `x-sveltekit-*`, `_data` loaders), form posts
|
|
310
|
+
* and Server Actions. Prefetches, assets and `fetch` calls from scripts
|
|
311
|
+
* read the session as it is, so a burst of parallel requests never races
|
|
312
|
+
* on the single-use refresh token.
|
|
313
|
+
*/
|
|
314
|
+
function shouldRefresh(request) {
|
|
315
|
+
const h = request.headers;
|
|
316
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
317
|
+
if (h.has("next-action") || h.has("x-sveltekit-action")) return true;
|
|
318
|
+
if (h.get("sec-fetch-dest") === "document") return true;
|
|
319
|
+
const type = h.get("content-type") ?? "";
|
|
320
|
+
return FORM_TYPES.some((form) => type.startsWith(form));
|
|
321
|
+
}
|
|
322
|
+
if (isPrefetch(request)) return false;
|
|
323
|
+
if (h.get("rsc") === "1") return true;
|
|
324
|
+
const dest = h.get("sec-fetch-dest");
|
|
325
|
+
if (dest) return dest === "document";
|
|
326
|
+
return h.get("accept")?.includes("text/html") ?? false;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Whether a request should ask the Auth server if the session still exists
|
|
330
|
+
* (`checkSession`): full page loads, and `paths` you list for client
|
|
331
|
+
* navigations. Never prefetches or mutations.
|
|
332
|
+
*/
|
|
333
|
+
function shouldCheckSession(request, paths) {
|
|
334
|
+
if (request.method !== "GET" && request.method !== "HEAD") return false;
|
|
335
|
+
if (isPrefetch(request)) return false;
|
|
336
|
+
if (request.headers.get("sec-fetch-dest") === "document") return true;
|
|
337
|
+
return paths?.includes(new URL(request.url).pathname) ?? false;
|
|
338
|
+
}
|
|
339
|
+
/** `policy` for one request. */
|
|
340
|
+
function refreshFor(policy, request) {
|
|
341
|
+
if (policy === void 0 || typeof policy === "boolean") return policy;
|
|
342
|
+
return policy === "navigation" ? shouldRefresh(request) : policy(request);
|
|
343
|
+
}
|
|
344
|
+
//#endregion
|
|
345
|
+
//#region src/server/entries/session.ts
|
|
346
|
+
function checkFor(policy, request) {
|
|
347
|
+
if (policy === void 0 || typeof policy === "boolean") return policy ?? false;
|
|
348
|
+
return policy === "navigation" ? shouldCheckSession(request) : policy(request);
|
|
349
|
+
}
|
|
350
|
+
/** Appends `Max-Age=0` writes for the session cookie at the old scopes, and the no-store headers. */
|
|
351
|
+
function clearOldScopes(response, request, name, scopes) {
|
|
352
|
+
const writes = clearSessionAtScopes(parseCookies(request.headers.get("cookie")), name, scopes);
|
|
353
|
+
if (writes.length === 0) return response;
|
|
354
|
+
const headers = new Headers(response.headers);
|
|
355
|
+
for (const write of writes) headers.append("set-cookie", serializeCookie(write));
|
|
356
|
+
for (const [key, value] of Object.entries(AUTH_CACHE_HEADERS)) headers.set(key, value);
|
|
357
|
+
return new Response(response.body, {
|
|
358
|
+
status: response.status,
|
|
359
|
+
statusText: response.statusText,
|
|
360
|
+
headers
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Entry contributing `ctx.session`, the request's `AuthResolution`: a bearer
|
|
365
|
+
* token first, the session cookie second. It is the only entry with a
|
|
366
|
+
* response seam for cookies: on the way out it adds the refreshed session
|
|
367
|
+
* cookies and the no-store headers, and hands pending event sends to
|
|
368
|
+
* `waitUntil`.
|
|
369
|
+
*/
|
|
370
|
+
function withSession(core, config = {}) {
|
|
371
|
+
return defineMiddleware({
|
|
372
|
+
key: "session",
|
|
373
|
+
run: () => async function* (request, ctx) {
|
|
374
|
+
const call = callOf(ctx)?.options;
|
|
375
|
+
const refresh = call ? call.refresh ?? false : refreshFor(config.refresh, request);
|
|
376
|
+
const cookies = call ? call.cookies : config.cookies;
|
|
377
|
+
const checkSession = !call && checkFor(config.checkSession, request);
|
|
378
|
+
const session = await core.resolve(request, {
|
|
379
|
+
...refresh === void 0 ? {} : { refresh },
|
|
380
|
+
...cookies === void 0 ? {} : { cookies },
|
|
381
|
+
...checkSession ? { checkSession } : {},
|
|
382
|
+
...config.encode === void 0 ? {} : { encode: config.encode }
|
|
383
|
+
});
|
|
384
|
+
try {
|
|
385
|
+
const response = yield { session };
|
|
386
|
+
const applied = session.apply(response);
|
|
387
|
+
return config.cookieScopes?.length ? clearOldScopes(applied, request, core.sessionCookie(), config.cookieScopes) : applied;
|
|
388
|
+
} finally {
|
|
389
|
+
if (config.waitUntil && core.events.pending) config.waitUntil(core.events.settled());
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
})();
|
|
393
|
+
}
|
|
394
|
+
//#endregion
|
|
395
|
+
//#region src/server/entries/support.ts
|
|
396
|
+
/**
|
|
397
|
+
* Entry contributing `ctx.support`: the support session ("view as user") the
|
|
398
|
+
* request's support cookie names, when `ServerOptions.support` is set and
|
|
399
|
+
* the caller is the admin who started it. `undefined` otherwise.
|
|
400
|
+
*/
|
|
401
|
+
function withSupport(core) {
|
|
402
|
+
return defineMiddleware({
|
|
403
|
+
key: "support",
|
|
404
|
+
run: () => async (request, ctx) => {
|
|
405
|
+
return { support: callOf(ctx)?.options.support ?? await supportLookup(core, request, ctx.session) };
|
|
406
|
+
}
|
|
407
|
+
})();
|
|
408
|
+
}
|
|
409
|
+
//#endregion
|
|
410
|
+
//#region src/server/entries/tenant.ts
|
|
411
|
+
/**
|
|
412
|
+
* Entry contributing `ctx.tenant`: the active tenant from
|
|
413
|
+
* `ServerOptions.tenant` (a URL slug, a header), or `undefined`. It becomes
|
|
414
|
+
* `context.tenant` for the `tenant()` plugin, the `better_supabase.tenant`
|
|
415
|
+
* setting over Postgres and the `x-bs-tenant` header over PostgREST. It
|
|
416
|
+
* starts the support lookup that `withSupport` awaits, so both run at once.
|
|
417
|
+
*/
|
|
418
|
+
function withTenant(core) {
|
|
419
|
+
return defineMiddleware({
|
|
420
|
+
key: "tenant",
|
|
421
|
+
run: () => async (request, ctx) => {
|
|
422
|
+
const options = callOf(ctx)?.options;
|
|
423
|
+
if (options?.support === void 0) startSupportLookup(core, request, ctx.session);
|
|
424
|
+
return { tenant: options?.tenant ?? await core.tenant(request, ctx.session.auth) };
|
|
425
|
+
}
|
|
426
|
+
})();
|
|
427
|
+
}
|
|
428
|
+
//#endregion
|
|
429
|
+
//#region src/auth/support-cookie.ts
|
|
430
|
+
/** The cookie that carries the support session id. */
|
|
431
|
+
const SUPPORT_COOKIE = "bs-support";
|
|
432
|
+
/** Seconds until the session expires, rounded up so the cookie never outlives it by less than a second. */
|
|
433
|
+
function supportCookieMaxAge(session, now = Date.now()) {
|
|
434
|
+
return Math.max(0, Math.ceil((session.expiresAt.epochMilliseconds - now) / 1e3));
|
|
435
|
+
}
|
|
436
|
+
/** `Set-Cookie` for a session: HttpOnly, SameSite=Lax, gone when the session expires. */
|
|
437
|
+
function supportCookie(session, options = {}, now = Date.now()) {
|
|
438
|
+
return cookieLine(options, encodeURIComponent(session.id), supportCookieMaxAge(session, now));
|
|
439
|
+
}
|
|
440
|
+
/** `Set-Cookie` that removes the support cookie. */
|
|
441
|
+
function clearSupportCookie(options = {}) {
|
|
442
|
+
return cookieLine(options, "", 0);
|
|
443
|
+
}
|
|
444
|
+
function cookieLine(options, value, maxAge) {
|
|
445
|
+
return [
|
|
446
|
+
`${options.name ?? "bs-support"}=${value}`,
|
|
447
|
+
`Path=${options.path ?? "/"}`,
|
|
448
|
+
`Max-Age=${String(maxAge)}`,
|
|
449
|
+
"HttpOnly",
|
|
450
|
+
"SameSite=Lax",
|
|
451
|
+
...options.secure === false ? [] : ["Secure"]
|
|
452
|
+
].join("; ");
|
|
453
|
+
}
|
|
454
|
+
/** The support session id in a `Cookie` header, or `undefined`. */
|
|
455
|
+
function supportCookieValue(cookieHeader, name = SUPPORT_COOKIE) {
|
|
456
|
+
if (!cookieHeader) return void 0;
|
|
457
|
+
for (const part of cookieHeader.split(";")) {
|
|
458
|
+
const index = part.indexOf("=");
|
|
459
|
+
if (index === -1 || part.slice(0, index).trim() !== name) continue;
|
|
460
|
+
let value;
|
|
461
|
+
try {
|
|
462
|
+
value = decodeURIComponent(part.slice(index + 1).trim());
|
|
463
|
+
} catch {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
return value === "" ? void 0 : value;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
//#endregion
|
|
470
|
+
//#region src/server/support-off.ts
|
|
471
|
+
const NOT_CONFIGURED = "Support sessions need createServer(betterSupabase, { support: supportSessions({ store }) })";
|
|
472
|
+
const notConfigured = () => AsyncResult.err(dbError("unexpected", NOT_CONFIGURED));
|
|
473
|
+
/** `bs.support` without `ServerOptions.support`: every call fails. */
|
|
474
|
+
const supportOff = {
|
|
475
|
+
start: notConfigured,
|
|
476
|
+
stop: notConfigured,
|
|
477
|
+
revoke: notConfigured,
|
|
478
|
+
current: () => Promise.resolve(void 0),
|
|
479
|
+
list: notConfigured,
|
|
480
|
+
sessionIdOf: () => void 0,
|
|
481
|
+
clearCookie: () => clearSupportCookie()
|
|
482
|
+
};
|
|
483
|
+
//#endregion
|
|
484
|
+
//#region src/server/server.ts
|
|
485
|
+
/** The header `current_tenant_id()` reads over the Data API. */
|
|
486
|
+
const TENANT_HEADER = "x-bs-tenant";
|
|
487
|
+
/** The environment as `getEnv` resolves it, one variable at a time. */
|
|
488
|
+
const RUNTIME_ENV = new Proxy({}, { get: (_target, key) => typeof key === "string" ? getEnv(key) : void 0 });
|
|
489
|
+
const needsPostgres = (method) => dbError("invalid_request", `${method} needs createServer(betterSupabase, { postgres: createPostgres() })`);
|
|
490
|
+
const STATELESS = {
|
|
491
|
+
persistSession: false,
|
|
492
|
+
autoRefreshToken: false,
|
|
493
|
+
detectSessionInUrl: false
|
|
494
|
+
};
|
|
495
|
+
/**
|
|
496
|
+
* A framework adapter's server: `server` plus the adapter's own methods,
|
|
497
|
+
* without reading the lazy `env` getter. `extra` can't replace a
|
|
498
|
+
* `BetterServer` method.
|
|
499
|
+
*/
|
|
500
|
+
function extendServer(server, extra) {
|
|
501
|
+
const extended = Object.defineProperties({}, {
|
|
502
|
+
...Object.getOwnPropertyDescriptors(server),
|
|
503
|
+
...Object.getOwnPropertyDescriptors(extra)
|
|
504
|
+
});
|
|
505
|
+
shareCore(server, extended);
|
|
506
|
+
return extended;
|
|
507
|
+
}
|
|
508
|
+
/** `{ ...base, ...extra }` that keeps lazy getters lazy. */
|
|
509
|
+
function withExtra(base, extra) {
|
|
510
|
+
return Object.defineProperties({}, {
|
|
511
|
+
...Object.getOwnPropertyDescriptors(base),
|
|
512
|
+
...Object.getOwnPropertyDescriptors(extra)
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Server-side entry point for APIs, jobs, MCP servers and framework
|
|
517
|
+
* adapters. Holds the secrets; `betterSupabase` stays isomorphic.
|
|
518
|
+
*/
|
|
519
|
+
function createServer(betterSupabase, options = {}) {
|
|
520
|
+
const credentials = options.credentials === void 0 ? {} : { credentials: credentialProviderOf(options.credentials) };
|
|
521
|
+
let loaded;
|
|
522
|
+
const env = () => loaded ??= options.env ?? loadEnv(RUNTIME_ENV);
|
|
523
|
+
let adminClient;
|
|
524
|
+
let anonClient;
|
|
525
|
+
if ((options.prefetchJwks ?? defaultPrefetchJwks()) && options.auth?.jwks === void 0) try {
|
|
526
|
+
prefetchJwks({
|
|
527
|
+
...options.auth,
|
|
528
|
+
env: env()
|
|
529
|
+
});
|
|
530
|
+
} catch {}
|
|
531
|
+
const secretKey = () => {
|
|
532
|
+
const key = env().secretKey;
|
|
533
|
+
if (!key) throw new TypeError("admin() needs SUPABASE_SECRET_KEY; it is not set");
|
|
534
|
+
return key;
|
|
535
|
+
};
|
|
536
|
+
const customFetch = options.fetch ? { fetch: options.fetch } : void 0;
|
|
537
|
+
const definitionExecutor = betterSupabase.executorOptions();
|
|
538
|
+
const urlLengthLimit = options.db?.urlLengthLimit ?? definitionExecutor.urlLengthLimit;
|
|
539
|
+
const db = {
|
|
540
|
+
...options.db?.timeout === void 0 ? {} : { timeout: options.db.timeout },
|
|
541
|
+
...options.db?.retry === void 0 ? {} : { retry: options.db.retry },
|
|
542
|
+
...urlLengthLimit === void 0 ? {} : { urlLengthLimit }
|
|
543
|
+
};
|
|
544
|
+
const dbOptions = Object.keys(db).length > 0 ? { db } : {};
|
|
545
|
+
const sharedGlobal = {
|
|
546
|
+
...customFetch ? { global: customFetch } : {},
|
|
547
|
+
...dbOptions
|
|
548
|
+
};
|
|
549
|
+
/** postgrest-js options a bare `PostgrestClient` takes; `timeout` is the executor's. */
|
|
550
|
+
const restOptions = {
|
|
551
|
+
...db.retry === void 0 ? {} : { retry: db.retry },
|
|
552
|
+
...urlLengthLimit === void 0 ? {} : { urlLengthLimit }
|
|
553
|
+
};
|
|
554
|
+
const executorOptions = {
|
|
555
|
+
...definitionExecutor,
|
|
556
|
+
...urlLengthLimit === void 0 ? {} : { urlLengthLimit },
|
|
557
|
+
...db.timeout === void 0 ? {} : { timeout: db.timeout },
|
|
558
|
+
...db.retry === void 0 ? {} : { retry: db.retry }
|
|
559
|
+
};
|
|
560
|
+
/** Set when `db` options need an executor built with them. */
|
|
561
|
+
const tunedExecutor = options.db !== void 0;
|
|
562
|
+
const serviceClient = () => {
|
|
563
|
+
adminClient ??= createClient(env().url, secretKey(), {
|
|
564
|
+
auth: STATELESS,
|
|
565
|
+
...sharedGlobal
|
|
566
|
+
});
|
|
567
|
+
return adminClient;
|
|
568
|
+
};
|
|
569
|
+
const readUrl = () => options.readUrl === false ? void 0 : options.readUrl?.replace(/\/+$/, "") ?? env().readUrl;
|
|
570
|
+
const apiKeyClient = () => {
|
|
571
|
+
throw new TypeError("ctx.supabase and ctx.db.$client are not available for an API key: ctx.db and ctx.sql run over direct Postgres with the key's claims");
|
|
572
|
+
};
|
|
573
|
+
/** Clients for the primary share the service and anon clients; others don't. */
|
|
574
|
+
const supabaseAt = (url, auth, headers = {}) => {
|
|
575
|
+
const custom = Object.keys(headers).length > 0;
|
|
576
|
+
const global = custom ? {
|
|
577
|
+
global: {
|
|
578
|
+
headers,
|
|
579
|
+
...customFetch
|
|
580
|
+
},
|
|
581
|
+
...dbOptions
|
|
582
|
+
} : sharedGlobal;
|
|
583
|
+
const shared = !custom && url === env().url;
|
|
584
|
+
switch (auth.kind) {
|
|
585
|
+
case "user": return createClient(url, env().publishableKey, {
|
|
586
|
+
accessToken: () => Promise.resolve(auth.token),
|
|
587
|
+
...global
|
|
588
|
+
});
|
|
589
|
+
case "service": return shared ? serviceClient() : createClient(url, secretKey(), {
|
|
590
|
+
auth: STATELESS,
|
|
591
|
+
...global
|
|
592
|
+
});
|
|
593
|
+
case "anon":
|
|
594
|
+
case "invalid":
|
|
595
|
+
if (!shared) return createClient(url, env().publishableKey, {
|
|
596
|
+
auth: STATELESS,
|
|
597
|
+
...global
|
|
598
|
+
});
|
|
599
|
+
anonClient ??= createClient(url, env().publishableKey, {
|
|
600
|
+
auth: STATELESS,
|
|
601
|
+
...sharedGlobal
|
|
602
|
+
});
|
|
603
|
+
return anonClient;
|
|
604
|
+
case "apiKey": return apiKeyClient();
|
|
605
|
+
default: return auth;
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
const supabaseFor = (auth, headers = {}) => supabaseAt(env().url, auth, headers);
|
|
609
|
+
const restUrls = /* @__PURE__ */ new Map();
|
|
610
|
+
const restUrl = (url) => {
|
|
611
|
+
let rest = restUrls.get(url);
|
|
612
|
+
if (rest === void 0) {
|
|
613
|
+
rest = new URL("rest/v1", url.endsWith("/") ? url : `${url}/`).href;
|
|
614
|
+
restUrls.set(url, rest);
|
|
615
|
+
}
|
|
616
|
+
return rest;
|
|
617
|
+
};
|
|
618
|
+
/**
|
|
619
|
+
* The client queries run on. Requests need only PostgREST, so they skip
|
|
620
|
+
* building Realtime, Storage and Auth clients per request; the shared
|
|
621
|
+
* service and anon clients for the primary are built once anyway.
|
|
622
|
+
*/
|
|
623
|
+
const restAt = (url, auth, headers = {}) => {
|
|
624
|
+
let key;
|
|
625
|
+
let bearer;
|
|
626
|
+
switch (auth.kind) {
|
|
627
|
+
case "user":
|
|
628
|
+
key = env().publishableKey;
|
|
629
|
+
bearer = auth.token;
|
|
630
|
+
break;
|
|
631
|
+
case "service":
|
|
632
|
+
case "anon":
|
|
633
|
+
case "invalid":
|
|
634
|
+
if (Object.keys(headers).length === 0 && url === env().url) return supabaseAt(url, auth, headers);
|
|
635
|
+
key = auth.kind === "service" ? secretKey() : env().publishableKey;
|
|
636
|
+
bearer = key;
|
|
637
|
+
break;
|
|
638
|
+
case "apiKey": return apiKeyClient();
|
|
639
|
+
default: return auth;
|
|
640
|
+
}
|
|
641
|
+
return new PostgrestClient(restUrl(url), {
|
|
642
|
+
headers: {
|
|
643
|
+
...headers,
|
|
644
|
+
apikey: key,
|
|
645
|
+
Authorization: `Bearer ${bearer}`
|
|
646
|
+
},
|
|
647
|
+
...customFetch,
|
|
648
|
+
...restOptions
|
|
649
|
+
});
|
|
650
|
+
};
|
|
651
|
+
/** `$client` stays the full Supabase client, built when first read. */
|
|
652
|
+
const withLazyClient = (db, client) => Object.defineProperty(db, "$client", {
|
|
653
|
+
get: client,
|
|
654
|
+
enumerable: true,
|
|
655
|
+
configurable: true
|
|
656
|
+
});
|
|
657
|
+
const dbFor = (auth, context = {}) => {
|
|
658
|
+
let supabase;
|
|
659
|
+
const rest = restAt(env().url, auth);
|
|
660
|
+
const db = betterSupabase.connect(rest, {
|
|
661
|
+
...authContext(auth),
|
|
662
|
+
...context
|
|
663
|
+
}, tunedExecutor ? { executor: postgrestExecutor(rest, executorOptions) } : {});
|
|
664
|
+
return withLazyClient(db, () => supabase ??= supabaseFor(auth));
|
|
665
|
+
};
|
|
666
|
+
const sqlFor = (claims, context, stats, session) => {
|
|
667
|
+
if (!options.postgres) throw new TypeError("Direct Postgres access needs createServer(betterSupabase, { postgres: createPostgres() })");
|
|
668
|
+
return betterSupabase.connect(session ? options.postgres.executorFor(claims, session) : options.postgres.executorFor(claims), context, stats ? { stats } : {});
|
|
669
|
+
};
|
|
670
|
+
/** `ServerOptions.tenant` outside `context()`, where it can't be awaited. */
|
|
671
|
+
const syncTenant = (request, auth) => {
|
|
672
|
+
const tenant = options.tenant?.(request, auth);
|
|
673
|
+
if (tenant !== void 0 && typeof tenant !== "string") throw new TypeError("ServerOptions.tenant returned a promise: use context(request), or pass { tenant } to contextFromResolution");
|
|
674
|
+
return tenant;
|
|
675
|
+
};
|
|
676
|
+
const pinMs = options.replicas?.pinMs ?? 5e3;
|
|
677
|
+
if (options.support && !options.postgres) throw new TypeError("Support sessions need createServer(betterSupabase, { postgres: createPostgres(), support })");
|
|
678
|
+
const audit = serverAudit(betterSupabase, options.audit);
|
|
679
|
+
const support = options.support ? options.support.create(betterSupabase.events, options.auth?.claims ?? betterSupabase.claimsSchema) : supportOff;
|
|
680
|
+
/**
|
|
681
|
+
* The target's context in a support session. Queries run over direct
|
|
682
|
+
* Postgres with the target's claims plus `act`, read-only unless the
|
|
683
|
+
* session allows writes; nothing reaches Supabase with the admin's token.
|
|
684
|
+
*/
|
|
685
|
+
const supportContextFor = (resolution, active, parent, tenant) => {
|
|
686
|
+
const { session, claims } = active;
|
|
687
|
+
const record = (key) => {
|
|
688
|
+
const value = claims[key];
|
|
689
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? Object.fromEntries(Object.entries(value)) : void 0;
|
|
690
|
+
};
|
|
691
|
+
const appMetadata = record("app_metadata");
|
|
692
|
+
const userMetadata = record("user_metadata");
|
|
693
|
+
const user = {
|
|
694
|
+
id: session.targetUserId,
|
|
695
|
+
role: typeof claims["role"] === "string" ? claims["role"] : "authenticated",
|
|
696
|
+
...typeof claims["email"] === "string" ? { email: claims["email"] } : {},
|
|
697
|
+
...appMetadata ? { appMetadata } : {},
|
|
698
|
+
...userMetadata ? { userMetadata } : {}
|
|
699
|
+
};
|
|
700
|
+
const auth = {
|
|
701
|
+
kind: "user",
|
|
702
|
+
token: "",
|
|
703
|
+
claims,
|
|
704
|
+
user,
|
|
705
|
+
source: "support",
|
|
706
|
+
expiresAt: Math.floor(session.expiresAt.epochMilliseconds / 1e3)
|
|
707
|
+
};
|
|
708
|
+
const scope = tenant ?? session.tenant;
|
|
709
|
+
const base = userContext(user, claims);
|
|
710
|
+
const context = scope === void 0 ? base : {
|
|
711
|
+
...base,
|
|
712
|
+
tenant: scope
|
|
713
|
+
};
|
|
714
|
+
const sessionOptions = {
|
|
715
|
+
...session.readOnly ? { readOnly: true } : {},
|
|
716
|
+
...scope === void 0 ? {} : { settings: { "better_supabase.tenant": scope } }
|
|
717
|
+
};
|
|
718
|
+
const recorder = new StatsRecorder(parent);
|
|
719
|
+
const sqlClaims = claims;
|
|
720
|
+
const unavailable = () => {
|
|
721
|
+
throw new TypeError("ctx.supabase and ctx.db.$client are not available in a support session; ctx.db and ctx.sql run as the target");
|
|
722
|
+
};
|
|
723
|
+
let db;
|
|
724
|
+
let sql;
|
|
725
|
+
return {
|
|
726
|
+
...credentials,
|
|
727
|
+
auth,
|
|
728
|
+
resolution,
|
|
729
|
+
support: active,
|
|
730
|
+
get supabase() {
|
|
731
|
+
return unavailable();
|
|
732
|
+
},
|
|
733
|
+
get db() {
|
|
734
|
+
db ??= Object.defineProperty(sqlFor(sqlClaims, context, recorder, sessionOptions), "$client", {
|
|
735
|
+
get: unavailable,
|
|
736
|
+
enumerable: true,
|
|
737
|
+
configurable: true
|
|
738
|
+
});
|
|
739
|
+
return db;
|
|
740
|
+
},
|
|
741
|
+
get sql() {
|
|
742
|
+
sql ??= sqlFor(sqlClaims, context, recorder, sessionOptions);
|
|
743
|
+
return sql;
|
|
744
|
+
},
|
|
745
|
+
replica: void 0,
|
|
746
|
+
stats: () => recorder.snapshot(),
|
|
747
|
+
apply: (response) => resolution.apply(response),
|
|
748
|
+
cookies: () => resolution.cookies
|
|
749
|
+
};
|
|
750
|
+
};
|
|
751
|
+
/**
|
|
752
|
+
* An API key's context. There is no JWT for PostgREST, so `ctx.db` and
|
|
753
|
+
* `ctx.sql` run over direct Postgres with the key's synthesized claims,
|
|
754
|
+
* scoped to its organization unless the tenant option names another.
|
|
755
|
+
*/
|
|
756
|
+
const apiKeyContextFor = (resolution, auth, parent, tenant) => {
|
|
757
|
+
const scope = tenant ?? auth.organizationId;
|
|
758
|
+
const base = authContext(auth);
|
|
759
|
+
const context = scope === void 0 ? base : {
|
|
760
|
+
...base,
|
|
761
|
+
tenant: scope
|
|
762
|
+
};
|
|
763
|
+
const session = scope === void 0 ? void 0 : { settings: { "better_supabase.tenant": scope } };
|
|
764
|
+
const recorder = new StatsRecorder(parent);
|
|
765
|
+
let db;
|
|
766
|
+
let sql;
|
|
767
|
+
return {
|
|
768
|
+
...credentials,
|
|
769
|
+
auth: resolution.auth,
|
|
770
|
+
resolution,
|
|
771
|
+
get supabase() {
|
|
772
|
+
return apiKeyClient();
|
|
773
|
+
},
|
|
774
|
+
get db() {
|
|
775
|
+
db ??= Object.defineProperty(sqlFor(auth.claims, context, recorder, session), "$client", {
|
|
776
|
+
get: apiKeyClient,
|
|
777
|
+
enumerable: true,
|
|
778
|
+
configurable: true
|
|
779
|
+
});
|
|
780
|
+
return db;
|
|
781
|
+
},
|
|
782
|
+
get sql() {
|
|
783
|
+
sql ??= sqlFor(auth.claims, context, recorder, session);
|
|
784
|
+
return sql;
|
|
785
|
+
},
|
|
786
|
+
replica: void 0,
|
|
787
|
+
stats: () => recorder.snapshot(),
|
|
788
|
+
apply: (response) => resolution.apply(response),
|
|
789
|
+
cookies: () => resolution.cookies
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
/** Clients and repositories are built on first access: most scopes use one of them. */
|
|
793
|
+
const contextFor = (resolution, requestHeaders, parent, until = 0, tenant, active) => {
|
|
794
|
+
if (active) return supportContextFor(resolution, active, parent, tenant);
|
|
795
|
+
const { auth } = resolution;
|
|
796
|
+
if (auth.kind === "apiKey") return apiKeyContextFor(resolution, auth, parent, tenant);
|
|
797
|
+
const context = tenant === void 0 ? authContext(auth) : {
|
|
798
|
+
...authContext(auth),
|
|
799
|
+
tenant
|
|
800
|
+
};
|
|
801
|
+
const headers = tenant === void 0 ? requestHeaders : {
|
|
802
|
+
...requestHeaders,
|
|
803
|
+
[TENANT_HEADER]: tenant
|
|
804
|
+
};
|
|
805
|
+
const session = tenant === void 0 ? void 0 : { settings: { "better_supabase.tenant": tenant } };
|
|
806
|
+
const recorder = new StatsRecorder(parent);
|
|
807
|
+
let supabase;
|
|
808
|
+
let db;
|
|
809
|
+
let sql;
|
|
810
|
+
const sqlClaims = auth.kind === "user" ? auth.claims : auth.kind === "anon" ? { role: "anon" } : void 0;
|
|
811
|
+
const client = () => supabase ??= supabaseFor(auth, headers);
|
|
812
|
+
let replica;
|
|
813
|
+
const replicaUrl = () => {
|
|
814
|
+
const url = readUrl();
|
|
815
|
+
if (url) replica ??= replicaState(until);
|
|
816
|
+
return url;
|
|
817
|
+
};
|
|
818
|
+
return {
|
|
819
|
+
...credentials,
|
|
820
|
+
auth,
|
|
821
|
+
resolution,
|
|
822
|
+
get supabase() {
|
|
823
|
+
return client();
|
|
824
|
+
},
|
|
825
|
+
get db() {
|
|
826
|
+
if (db) return db;
|
|
827
|
+
const url = replicaUrl();
|
|
828
|
+
const rest = supabase ?? restAt(env().url, auth, headers);
|
|
829
|
+
const connected = betterSupabase.connect(rest, context, {
|
|
830
|
+
stats: recorder,
|
|
831
|
+
...url && replica ? { executor: routedExecutor(postgrestExecutor(rest, executorOptions), postgrestExecutor(restAt(url, auth, headers), executorOptions), replica) } : tunedExecutor ? { executor: postgrestExecutor(rest, executorOptions) } : {}
|
|
832
|
+
});
|
|
833
|
+
db = withLazyClient(connected, client);
|
|
834
|
+
return db;
|
|
835
|
+
},
|
|
836
|
+
get replica() {
|
|
837
|
+
replicaUrl();
|
|
838
|
+
return replica;
|
|
839
|
+
},
|
|
840
|
+
get sql() {
|
|
841
|
+
if (!options.postgres || !sqlClaims) return;
|
|
842
|
+
sql ??= sqlFor(sqlClaims, context, recorder, session);
|
|
843
|
+
return sql;
|
|
844
|
+
},
|
|
845
|
+
stats: () => recorder.snapshot(),
|
|
846
|
+
apply: (response) => withPrimaryPin(resolution.apply(response), replica, pinMs),
|
|
847
|
+
cookies: () => {
|
|
848
|
+
const pin = primaryCookieWrite(replica, pinMs);
|
|
849
|
+
return pin ? [...resolution.cookies, pin] : resolution.cookies;
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
};
|
|
853
|
+
const fromResolution = (resolution, request, contextOptions, tenant) => contextFor(resolution, options.headers?.(request) ?? {}, contextOptions.stats, contextOptions.pinnedUntil ?? (readUrl() ? pinnedUntil(resolution.requestCookies) : 0), tenant, contextOptions.support);
|
|
854
|
+
const resolve = async (request, resolveOptions = {}) => {
|
|
855
|
+
const claims = options.auth?.claims ?? betterSupabase.claimsSchema;
|
|
856
|
+
const userMetadata = betterSupabase.userMetadataSchema;
|
|
857
|
+
const resolution = await resolveAuth(request, {
|
|
858
|
+
logger: betterSupabase.events.logger,
|
|
859
|
+
...options.auth,
|
|
860
|
+
...claims ? { claims } : {},
|
|
861
|
+
...userMetadata ? { userMetadata } : {},
|
|
862
|
+
env: env(),
|
|
863
|
+
...resolveOptions.refresh === void 0 ? {} : { refresh: resolveOptions.refresh },
|
|
864
|
+
...resolveOptions.cookies === void 0 ? {} : { cookies: resolveOptions.cookies },
|
|
865
|
+
...resolveOptions.checkSession ? { checkSession: true } : {},
|
|
866
|
+
...resolveOptions.encode === void 0 ? {} : { cookie: {
|
|
867
|
+
...options.auth?.cookie,
|
|
868
|
+
encode: resolveOptions.encode
|
|
869
|
+
} },
|
|
870
|
+
onRefresh: (event) => {
|
|
871
|
+
options.auth?.onRefresh?.(event);
|
|
872
|
+
betterSupabase.events.emit("refresh", event);
|
|
873
|
+
}
|
|
874
|
+
});
|
|
875
|
+
if (betterSupabase.events.has("auth")) {
|
|
876
|
+
const { auth } = resolution;
|
|
877
|
+
betterSupabase.events.emit("auth", {
|
|
878
|
+
source: auth.kind === "user" ? auth.source === "cookie" ? "cookie" : "bearer" : auth.kind === "service" || auth.kind === "apiKey" ? "bearer" : "none",
|
|
879
|
+
ok: auth.kind !== "invalid",
|
|
880
|
+
...auth.kind === "user" ? {
|
|
881
|
+
userId: auth.user.id,
|
|
882
|
+
rawSource: auth.source
|
|
883
|
+
} : {},
|
|
884
|
+
...auth.kind === "anon" || auth.kind === "invalid" ? { reason: auth.reason } : {}
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
return resolution;
|
|
888
|
+
};
|
|
889
|
+
/** One context per request and `refresh`/`cookies` pair: Server Components ask repeatedly. */
|
|
890
|
+
const contextMemo = /* @__PURE__ */ new WeakMap();
|
|
891
|
+
const server = {
|
|
892
|
+
get env() {
|
|
893
|
+
return env();
|
|
894
|
+
},
|
|
895
|
+
events: betterSupabase.events,
|
|
896
|
+
resolve,
|
|
897
|
+
supabaseFor,
|
|
898
|
+
dbFor,
|
|
899
|
+
contextFor: (auth, contextOptions = {}) => contextFor({
|
|
900
|
+
auth,
|
|
901
|
+
cookies: [],
|
|
902
|
+
headers: {},
|
|
903
|
+
requestCookies: [],
|
|
904
|
+
apply: (response) => response
|
|
905
|
+
}, {}, contextOptions.stats, contextOptions.pinnedUntil, contextOptions.tenant, contextOptions.support),
|
|
906
|
+
context(request, contextOptions = {}) {
|
|
907
|
+
const { refresh, cookies, ...rest } = contextOptions;
|
|
908
|
+
const key = Object.keys(rest).length === 0 ? `${String(refresh ?? false)}:${String(cookies ?? true)}` : void 0;
|
|
909
|
+
let byOptions = key ? contextMemo.get(request) : void 0;
|
|
910
|
+
const known = key ? byOptions?.get(key) : void 0;
|
|
911
|
+
if (known) return known;
|
|
912
|
+
const resolving = (async () => {
|
|
913
|
+
const call = { options: contextOptions };
|
|
914
|
+
await fold(request, {
|
|
915
|
+
...seedContext(),
|
|
916
|
+
[CALL]: call
|
|
917
|
+
});
|
|
918
|
+
return call.context;
|
|
919
|
+
})();
|
|
920
|
+
if (key) {
|
|
921
|
+
if (!byOptions) {
|
|
922
|
+
byOptions = /* @__PURE__ */ new Map();
|
|
923
|
+
contextMemo.set(request, byOptions);
|
|
924
|
+
}
|
|
925
|
+
const memo = byOptions;
|
|
926
|
+
memo.set(key, resolving);
|
|
927
|
+
resolving.catch(() => {
|
|
928
|
+
if (memo.get(key) === resolving) memo.delete(key);
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
return resolving;
|
|
932
|
+
},
|
|
933
|
+
contextFromResolution: (resolution, request, contextOptions = {}) => fromResolution(resolution, request, contextOptions, contextOptions.tenant ?? syncTenant(request, resolution.auth)),
|
|
934
|
+
admin: (context = {}) => {
|
|
935
|
+
const client = serviceClient();
|
|
936
|
+
return betterSupabase.connect(client, {
|
|
937
|
+
actor: {
|
|
938
|
+
id: "service",
|
|
939
|
+
kind: "service",
|
|
940
|
+
role: "service_role"
|
|
941
|
+
},
|
|
942
|
+
claims: { role: "service_role" },
|
|
943
|
+
...context
|
|
944
|
+
}, tunedExecutor ? { executor: postgrestExecutor(client, executorOptions) } : {});
|
|
945
|
+
},
|
|
946
|
+
support,
|
|
947
|
+
deleteAccount: (userId, deleteOptions) => deleteAccount(betterSupabase, serviceClient, userId, {
|
|
948
|
+
...options.postgres ? { sql: options.postgres.admin } : {},
|
|
949
|
+
...deleteOptions
|
|
950
|
+
}).map((deleted) => {
|
|
951
|
+
audit.account("account.deleted", userId, { removed: deleted.removed });
|
|
952
|
+
return deleted;
|
|
953
|
+
}),
|
|
954
|
+
endSessions: (userId) => options.postgres ? endSessions(options.postgres.admin, userId).map((ended) => {
|
|
955
|
+
audit.account("account.sessions_ended", userId, { ended: ended.ended });
|
|
956
|
+
return ended;
|
|
957
|
+
}) : AsyncResult.err(needsPostgres("endSessions")),
|
|
958
|
+
suspendAccount: (userId, suspendOptions) => options.postgres ? suspendAccount(serviceClient, options.postgres.admin, userId, suspendOptions).map((suspended) => {
|
|
959
|
+
audit.account(suspended.suspended ? "account.suspended" : "account.unsuspended", userId, {
|
|
960
|
+
ended: suspended.ended,
|
|
961
|
+
...suspendOptions.duration === void 0 ? {} : { duration: suspendOptions.duration }
|
|
962
|
+
});
|
|
963
|
+
return suspended;
|
|
964
|
+
}) : AsyncResult.err(needsPostgres("suspendAccount")),
|
|
965
|
+
actingAs: (userId, claims = {}, impersonation) => {
|
|
966
|
+
const full = {
|
|
967
|
+
role: "authenticated",
|
|
968
|
+
...claims,
|
|
969
|
+
sub: userId,
|
|
970
|
+
...impersonation ? { act: actClaim(impersonation) } : {}
|
|
971
|
+
};
|
|
972
|
+
return sqlFor(full, {
|
|
973
|
+
actor: {
|
|
974
|
+
id: userId,
|
|
975
|
+
kind: "user",
|
|
976
|
+
role: full.role ?? "authenticated",
|
|
977
|
+
...impersonation ? { impersonator: impersonation.actor } : {}
|
|
978
|
+
},
|
|
979
|
+
claims: full
|
|
980
|
+
});
|
|
981
|
+
},
|
|
982
|
+
forContext: (context, forOptions = {}) => {
|
|
983
|
+
const { actor, tenant } = context;
|
|
984
|
+
if (actor?.kind !== "user") return AsyncResult.err(dbError("forbidden", `forContext needs a user actor, got ${actor ? actor.kind : "none"}: use admin() for service work`));
|
|
985
|
+
if (!options.postgres) return AsyncResult.err(dbError("invalid_request", "Direct Postgres access needs createServer(betterSupabase, { postgres: createPostgres() })"));
|
|
986
|
+
const recorded = actorOf(context.claims ?? {});
|
|
987
|
+
if (!recorded.ok) return AsyncResult.err(dbError("forbidden", "forContext got an invalid act claim"));
|
|
988
|
+
const support = recorded.actor?.kind === "support" ? recorded.actor : void 0;
|
|
989
|
+
const impersonator = support?.id ?? actor.impersonator;
|
|
990
|
+
return AsyncResult.from(async () => {
|
|
991
|
+
const claims = {
|
|
992
|
+
...await options.claimsFor?.(actor.id, context) ?? {},
|
|
993
|
+
role: "authenticated",
|
|
994
|
+
sub: actor.id,
|
|
995
|
+
...tenant === void 0 ? {} : { tenant_id: tenant },
|
|
996
|
+
...support ? { act: impersonatorClaim(support) } : actor.impersonator ? { act: actClaim({
|
|
997
|
+
actor: actor.impersonator,
|
|
998
|
+
reason: forOptions.reason ?? "job"
|
|
999
|
+
}) } : {}
|
|
1000
|
+
};
|
|
1001
|
+
const session = {
|
|
1002
|
+
...support?.readOnly ? { readOnly: true } : {},
|
|
1003
|
+
...tenant === void 0 ? {} : { settings: { "better_supabase.tenant": tenant } }
|
|
1004
|
+
};
|
|
1005
|
+
return ok(sqlFor(claims, {
|
|
1006
|
+
actor: {
|
|
1007
|
+
id: actor.id,
|
|
1008
|
+
kind: "user",
|
|
1009
|
+
role: "authenticated",
|
|
1010
|
+
...impersonator ? { impersonator } : {}
|
|
1011
|
+
},
|
|
1012
|
+
claims,
|
|
1013
|
+
...tenant === void 0 ? {} : { tenant }
|
|
1014
|
+
}, void 0, Object.keys(session).length === 0 ? void 0 : session));
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
const core = {
|
|
1019
|
+
events: betterSupabase.events,
|
|
1020
|
+
pinMs,
|
|
1021
|
+
resolve,
|
|
1022
|
+
sessionCookie: () => options.auth?.cookie?.name ?? sessionCookieName(env().url),
|
|
1023
|
+
tenant: (request, auth) => options.tenant?.(request, auth),
|
|
1024
|
+
support: (request, auth) => support.current(request, auth),
|
|
1025
|
+
context: (resolution, request, contextOptions, tenant) => fromResolution(resolution, request, contextOptions, tenant)
|
|
1026
|
+
};
|
|
1027
|
+
registerCore(server, core);
|
|
1028
|
+
const fold = pipeline([
|
|
1029
|
+
withSession(core),
|
|
1030
|
+
withTenant(core),
|
|
1031
|
+
withSupport(core),
|
|
1032
|
+
withServerContext(core)
|
|
1033
|
+
], (_request, ctx) => {
|
|
1034
|
+
const call = callOf(ctx);
|
|
1035
|
+
if (call) call.context = ctx.bs;
|
|
1036
|
+
return Promise.resolve(new Response(null, { status: 204 }));
|
|
1037
|
+
});
|
|
1038
|
+
return server;
|
|
1039
|
+
}
|
|
1040
|
+
//#endregion
|
|
1041
|
+
//#region src/server/adapter.ts
|
|
1042
|
+
/** Hands event sink sends still running to `waitUntil`; a no-op without either. */
|
|
1043
|
+
function flushEvents(server, waitUntil) {
|
|
1044
|
+
if (waitUntil && server.events.pending) waitUntil(server.events.settled());
|
|
1045
|
+
}
|
|
1046
|
+
/** The 500 for an error a handler threw; the message only when `expose` is set. */
|
|
1047
|
+
function unexpectedResponse(cause, options) {
|
|
1048
|
+
return problemResponse(dbError("unexpected", options.expose && cause instanceof Error ? cause.message : "Internal server error"), options);
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Runs `run` as the request's caller, the way every built-in adapter does:
|
|
1052
|
+
* resolves the context, applies the guard (401/403 Problem Details), turns
|
|
1053
|
+
* the return value into a response with `respond()`, turns other thrown
|
|
1054
|
+
* errors into a 500, adds the context's cookies with `ctx.apply()` and
|
|
1055
|
+
* hands pending event sink sends to `waitUntil`.
|
|
1056
|
+
*
|
|
1057
|
+
* ```ts
|
|
1058
|
+
* export default { fetch: (request, env, ctx) =>
|
|
1059
|
+
* handle(server, request, (c) => c.db.notes.findMany(), { waitUntil: (p) => ctx.waitUntil(p) }) }
|
|
1060
|
+
* ```
|
|
1061
|
+
*/
|
|
1062
|
+
async function handle(server, request, run, options = {}) {
|
|
1063
|
+
const expose = options.expose ?? defaultExpose();
|
|
1064
|
+
const instance = options.instance ?? new URL(request.url).pathname;
|
|
1065
|
+
const ctx = await server.context(request, {
|
|
1066
|
+
...options.context,
|
|
1067
|
+
refresh: refreshFor(options.refresh, request) ?? false
|
|
1068
|
+
});
|
|
1069
|
+
const denied = guard(ctx.auth, options.allow, options.aal, options.scopes);
|
|
1070
|
+
try {
|
|
1071
|
+
let response;
|
|
1072
|
+
try {
|
|
1073
|
+
response = denied ? problemResponse(denied, {
|
|
1074
|
+
instance,
|
|
1075
|
+
expose
|
|
1076
|
+
}) : await respond(() => run(ctx), {
|
|
1077
|
+
instance,
|
|
1078
|
+
expose,
|
|
1079
|
+
...options.status === void 0 ? {} : { status: options.status }
|
|
1080
|
+
});
|
|
1081
|
+
} catch (cause) {
|
|
1082
|
+
options.rethrow?.(cause);
|
|
1083
|
+
response = unexpectedResponse(cause, {
|
|
1084
|
+
instance,
|
|
1085
|
+
expose
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
return ctx.apply(response);
|
|
1089
|
+
} finally {
|
|
1090
|
+
flushEvents(server, options.waitUntil);
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
/** A request carrying only `token` as a bearer, for resolvers that read headers. */
|
|
1094
|
+
function bearerRequest(token) {
|
|
1095
|
+
return new Request("http://localhost/", { headers: token ? { authorization: `Bearer ${token}` } : {} });
|
|
1096
|
+
}
|
|
1097
|
+
/**
|
|
1098
|
+
* Verifies a bare access token (an MCP transport's, a WebSocket's) without
|
|
1099
|
+
* cookies or a refresh. An empty token resolves to `anon`.
|
|
1100
|
+
*/
|
|
1101
|
+
async function resolveToken(server, token) {
|
|
1102
|
+
const { auth } = await server.resolve(bearerRequest(token), { cookies: false });
|
|
1103
|
+
return auth;
|
|
1104
|
+
}
|
|
1105
|
+
//#endregion
|
|
1106
|
+
export { serverCore as C, deleteAccount as D, suspendAccount as E, withServerContext as S, endSessions as T, withSession as _, unexpectedResponse as a, shouldCheckSession as b, extendServer as c, clearSupportCookie as d, supportCookie as f, withSupport as g, withTenant as h, resolveToken as i, withExtra as l, supportCookieValue as m, flushEvents as n, TENANT_HEADER as o, supportCookieMaxAge as p, handle as r, createServer as s, bearerRequest as t, SUPPORT_COOKIE as u, isPrefetch as v, SERVER_EVENT_SOURCE as w, shouldRefresh as x, refreshFor as y };
|