@selvajs/supabase-provider 0.14.4-beta.0 → 0.14.4-beta.1

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.
Files changed (144) hide show
  1. package/LICENSE +1 -1
  2. package/dist/auth/SupabaseAuthProvider.d.ts.map +1 -1
  3. package/dist/auth/SupabaseAuthProvider.js +7 -14
  4. package/dist/auth/SupabaseAuthProvider.js.map +1 -1
  5. package/dist/data/SupabaseDataProvider.d.ts +13 -1
  6. package/dist/data/SupabaseDataProvider.d.ts.map +1 -1
  7. package/dist/data/SupabaseDataProvider.js +59 -0
  8. package/dist/data/SupabaseDataProvider.js.map +1 -1
  9. package/dist/data/SupabaseDefinitionStore.d.ts +1 -0
  10. package/dist/data/SupabaseDefinitionStore.d.ts.map +1 -1
  11. package/dist/data/SupabaseDefinitionStore.js +27 -1
  12. package/dist/data/SupabaseDefinitionStore.js.map +1 -1
  13. package/dist/data/index.d.ts +1 -0
  14. package/dist/data/index.d.ts.map +1 -1
  15. package/dist/data/index.js +1 -0
  16. package/dist/data/index.js.map +1 -1
  17. package/dist/data/migrationHead.d.ts +14 -0
  18. package/dist/data/migrationHead.d.ts.map +1 -0
  19. package/dist/data/migrationHead.js +14 -0
  20. package/dist/data/migrationHead.js.map +1 -0
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +1 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/permissions/SupabasePlatformPermissionStore.d.ts +5 -3
  26. package/dist/permissions/SupabasePlatformPermissionStore.d.ts.map +1 -1
  27. package/dist/permissions/SupabasePlatformPermissionStore.js +17 -30
  28. package/dist/permissions/SupabasePlatformPermissionStore.js.map +1 -1
  29. package/dist/storage/SupabaseStorageProvider.d.ts.map +1 -1
  30. package/dist/storage/SupabaseStorageProvider.js +3 -0
  31. package/dist/storage/SupabaseStorageProvider.js.map +1 -1
  32. package/package.json +13 -14
  33. package/supabase/.branches/_current_branch +1 -0
  34. package/supabase/.temp/cli-latest +1 -0
  35. package/supabase/migrations/20260712120000_user_profiles_disabled_and_gin.sql +95 -0
  36. package/supabase/migrations/20260712123000_selva_migration_head_fn.sql +38 -0
  37. package/supabase/migrations/20260713120000_selva_next_version_number.sql +59 -0
  38. package/supabase/migrations/20260713140000_selva_solve_cache_limit.sql +15 -0
  39. package/dist/auth/__tests__/conformance.test.d.ts +0 -2
  40. package/dist/auth/__tests__/conformance.test.d.ts.map +0 -1
  41. package/dist/auth/__tests__/conformance.test.js +0 -76
  42. package/dist/auth/__tests__/conformance.test.js.map +0 -1
  43. package/dist/auth/__tests__/token-verification.test.d.ts +0 -13
  44. package/dist/auth/__tests__/token-verification.test.d.ts.map +0 -1
  45. package/dist/auth/__tests__/token-verification.test.js +0 -135
  46. package/dist/auth/__tests__/token-verification.test.js.map +0 -1
  47. package/dist/data/__tests__/compute-server-conformance.test.d.ts +0 -2
  48. package/dist/data/__tests__/compute-server-conformance.test.d.ts.map +0 -1
  49. package/dist/data/__tests__/compute-server-conformance.test.js +0 -45
  50. package/dist/data/__tests__/compute-server-conformance.test.js.map +0 -1
  51. package/dist/data/__tests__/compute-server-encryption.test.d.ts +0 -12
  52. package/dist/data/__tests__/compute-server-encryption.test.d.ts.map +0 -1
  53. package/dist/data/__tests__/compute-server-encryption.test.js +0 -143
  54. package/dist/data/__tests__/compute-server-encryption.test.js.map +0 -1
  55. package/dist/data/__tests__/definition-conformance.test.d.ts +0 -2
  56. package/dist/data/__tests__/definition-conformance.test.d.ts.map +0 -1
  57. package/dist/data/__tests__/definition-conformance.test.js +0 -85
  58. package/dist/data/__tests__/definition-conformance.test.js.map +0 -1
  59. package/dist/data/__tests__/event-sink-conformance.test.d.ts +0 -2
  60. package/dist/data/__tests__/event-sink-conformance.test.d.ts.map +0 -1
  61. package/dist/data/__tests__/event-sink-conformance.test.js +0 -27
  62. package/dist/data/__tests__/event-sink-conformance.test.js.map +0 -1
  63. package/dist/data/__tests__/invite-conformance.test.d.ts +0 -2
  64. package/dist/data/__tests__/invite-conformance.test.d.ts.map +0 -1
  65. package/dist/data/__tests__/invite-conformance.test.js +0 -45
  66. package/dist/data/__tests__/invite-conformance.test.js.map +0 -1
  67. package/dist/data/__tests__/org-conformance.test.d.ts +0 -2
  68. package/dist/data/__tests__/org-conformance.test.d.ts.map +0 -1
  69. package/dist/data/__tests__/org-conformance.test.js +0 -29
  70. package/dist/data/__tests__/org-conformance.test.js.map +0 -1
  71. package/dist/data/__tests__/project-conformance.test.d.ts +0 -2
  72. package/dist/data/__tests__/project-conformance.test.d.ts.map +0 -1
  73. package/dist/data/__tests__/project-conformance.test.js +0 -57
  74. package/dist/data/__tests__/project-conformance.test.js.map +0 -1
  75. package/dist/data/__tests__/share-link-concurrency.test.d.ts +0 -22
  76. package/dist/data/__tests__/share-link-concurrency.test.d.ts.map +0 -1
  77. package/dist/data/__tests__/share-link-concurrency.test.js +0 -115
  78. package/dist/data/__tests__/share-link-concurrency.test.js.map +0 -1
  79. package/dist/data/__tests__/share-link-conformance.test.d.ts +0 -2
  80. package/dist/data/__tests__/share-link-conformance.test.d.ts.map +0 -1
  81. package/dist/data/__tests__/share-link-conformance.test.js +0 -88
  82. package/dist/data/__tests__/share-link-conformance.test.js.map +0 -1
  83. package/dist/data/__tests__/solve-metric-sink-conformance.test.d.ts +0 -2
  84. package/dist/data/__tests__/solve-metric-sink-conformance.test.d.ts.map +0 -1
  85. package/dist/data/__tests__/solve-metric-sink-conformance.test.js +0 -52
  86. package/dist/data/__tests__/solve-metric-sink-conformance.test.js.map +0 -1
  87. package/dist/data/__tests__/test-helpers.d.ts +0 -52
  88. package/dist/data/__tests__/test-helpers.d.ts.map +0 -1
  89. package/dist/data/__tests__/test-helpers.js +0 -177
  90. package/dist/data/__tests__/test-helpers.js.map +0 -1
  91. package/dist/permissions/__tests__/conformance.test.d.ts +0 -2
  92. package/dist/permissions/__tests__/conformance.test.d.ts.map +0 -1
  93. package/dist/permissions/__tests__/conformance.test.js +0 -27
  94. package/dist/permissions/__tests__/conformance.test.js.map +0 -1
  95. package/dist/storage/__tests__/conformance.test.d.ts +0 -2
  96. package/dist/storage/__tests__/conformance.test.d.ts.map +0 -1
  97. package/dist/storage/__tests__/conformance.test.js +0 -149
  98. package/dist/storage/__tests__/conformance.test.js.map +0 -1
  99. package/dist/userProfile/__tests__/conformance.test.d.ts +0 -2
  100. package/dist/userProfile/__tests__/conformance.test.d.ts.map +0 -1
  101. package/dist/userProfile/__tests__/conformance.test.js +0 -38
  102. package/dist/userProfile/__tests__/conformance.test.js.map +0 -1
  103. package/src/auth/SupabaseAuthProvider.ts +0 -600
  104. package/src/auth/__tests__/conformance.test.ts +0 -78
  105. package/src/auth/__tests__/token-verification.test.ts +0 -158
  106. package/src/auth/index.ts +0 -2
  107. package/src/cli/sync-migrations.ts +0 -133
  108. package/src/data/SupabaseAuditQuery.ts +0 -103
  109. package/src/data/SupabaseComputeServerStore.ts +0 -382
  110. package/src/data/SupabaseDataProvider.ts +0 -153
  111. package/src/data/SupabaseDefinitionStore.ts +0 -527
  112. package/src/data/SupabaseEventSink.ts +0 -30
  113. package/src/data/SupabaseInviteStore.ts +0 -171
  114. package/src/data/SupabaseOrgStore.ts +0 -508
  115. package/src/data/SupabaseProjectStore.ts +0 -431
  116. package/src/data/SupabaseShareLinkStore.ts +0 -176
  117. package/src/data/SupabaseSolveMetricSink.ts +0 -38
  118. package/src/data/__tests__/compute-server-conformance.test.ts +0 -46
  119. package/src/data/__tests__/compute-server-encryption.test.ts +0 -173
  120. package/src/data/__tests__/definition-conformance.test.ts +0 -89
  121. package/src/data/__tests__/event-sink-conformance.test.ts +0 -32
  122. package/src/data/__tests__/invite-conformance.test.ts +0 -46
  123. package/src/data/__tests__/org-conformance.test.ts +0 -30
  124. package/src/data/__tests__/project-conformance.test.ts +0 -57
  125. package/src/data/__tests__/share-link-concurrency.test.ts +0 -124
  126. package/src/data/__tests__/share-link-conformance.test.ts +0 -87
  127. package/src/data/__tests__/solve-metric-sink-conformance.test.ts +0 -59
  128. package/src/data/__tests__/test-helpers.ts +0 -188
  129. package/src/data/client.ts +0 -173
  130. package/src/data/errors.ts +0 -45
  131. package/src/data/index.ts +0 -29
  132. package/src/data/pagination.ts +0 -68
  133. package/src/data/query.ts +0 -22
  134. package/src/data/rowStamp.ts +0 -48
  135. package/src/index.ts +0 -42
  136. package/src/permissions/SupabasePlatformPermissionStore.ts +0 -161
  137. package/src/permissions/__tests__/conformance.test.ts +0 -28
  138. package/src/permissions/index.ts +0 -1
  139. package/src/storage/SupabaseStorageProvider.ts +0 -182
  140. package/src/storage/__tests__/conformance.test.ts +0 -152
  141. package/src/storage/index.ts +0 -2
  142. package/src/userProfile/SupabaseUserProfileProvider.ts +0 -177
  143. package/src/userProfile/__tests__/conformance.test.ts +0 -39
  144. package/src/userProfile/index.ts +0 -1
@@ -1,161 +0,0 @@
1
- import type {
2
- IPlatformPermissionStore,
3
- PlatformPermission,
4
- RequestContext,
5
- UserManagementResult
6
- } from '@selvajs/platform';
7
- import { ProviderError, hasPermission, PlatformPermissionSchema } from '@selvajs/platform';
8
- import type { ClientBundle } from '../data/client.js';
9
-
10
- /**
11
- * Supabase-backed platform-permission store. Reads and writes
12
- * `public.user_profiles.platform_permissions` (auto-seeded by the
13
- * `handle_new_auth_user` trigger). Service-role throughout — the store's own
14
- * `assertAdmin` / `assertCanRead` enforce the auth boundary at the app level
15
- * since RLS on `user_profiles` is coarse-grained for cross-user reads.
16
- *
17
- * Disabled-user state lives on `auth.users.user_metadata.disabled`, so the
18
- * invariant counters cross-reference the auth backend to exclude disabled
19
- * admins (matches `Permissions.md §10` "enabled instance_admin" semantics).
20
- */
21
- export class SupabasePlatformPermissionStore implements IPlatformPermissionStore {
22
- constructor(private readonly clients: ClientBundle) {}
23
-
24
- private client() {
25
- return this.clients.serviceClient;
26
- }
27
-
28
- async getFor(ctx: RequestContext, userId: string): Promise<PlatformPermission[]> {
29
- assertCanRead(ctx, userId);
30
- const { data, error } = await this.client()
31
- .from('user_profiles')
32
- .select('platform_permissions')
33
- .eq('user_id', userId)
34
- .maybeSingle();
35
- if (error) throw mapError(error);
36
- return filterValid(data?.platform_permissions ?? []);
37
- }
38
-
39
- async getForBatch(
40
- ctx: RequestContext,
41
- userIds: readonly string[]
42
- ): Promise<Map<string, PlatformPermission[]>> {
43
- assertAdmin(ctx);
44
- const out = new Map<string, PlatformPermission[]>();
45
- if (userIds.length === 0) return out;
46
- const { data, error } = await this.client()
47
- .from('user_profiles')
48
- .select('user_id, platform_permissions')
49
- .in('user_id', [...userIds]);
50
- if (error) throw mapError(error);
51
- for (const row of data ?? []) {
52
- out.set(row.user_id as string, filterValid((row.platform_permissions ?? []) as string[]));
53
- }
54
- return out;
55
- }
56
-
57
- async set(
58
- ctx: RequestContext,
59
- userId: string,
60
- permissions: readonly PlatformPermission[]
61
- ): Promise<UserManagementResult> {
62
- assertAdmin(ctx);
63
- // 1. Confirm the target exists in the auth backend (the FK + trigger
64
- // means the user_profiles row should exist post-signup; if not,
65
- // treat as not_found rather than auto-creating).
66
- const { data: existing, error: fetchError } = await this.client()
67
- .from('user_profiles')
68
- .select('platform_permissions')
69
- .eq('user_id', userId)
70
- .maybeSingle();
71
- if (fetchError) throw mapError(fetchError);
72
- if (!existing) return 'not_found';
73
-
74
- // 2. §2 invariant: refuse if dropping the last instance_admin.
75
- const wasAdmin = (existing.platform_permissions ?? []).includes('instance_admin');
76
- const willBeAdmin = permissions.includes('instance_admin');
77
- if (wasAdmin && !willBeAdmin) {
78
- const others = await this.countOtherEnabledAdmins(userId);
79
- if (others === 0) return 'last_admin';
80
- }
81
-
82
- // 3. Apply.
83
- const { error } = await this.client()
84
- .from('user_profiles')
85
- .update({ platform_permissions: [...permissions] })
86
- .eq('user_id', userId);
87
- if (error) throw mapError(error);
88
- return 'ok';
89
- }
90
-
91
- async hasInstanceAdmin(_ctx: RequestContext): Promise<boolean> {
92
- const { data, error } = await this.client()
93
- .from('user_profiles')
94
- .select('user_id')
95
- .contains('platform_permissions', ['instance_admin']);
96
- if (error) throw mapError(error);
97
- const candidates = (data ?? []).map((r) => r.user_id as string);
98
- if (candidates.length === 0) return false;
99
- // Check at least one isn't disabled.
100
- for (const id of candidates) {
101
- const { data: authData } = await this.clients.serviceClient.auth.admin.getUserById(id);
102
- if (authData.user && authData.user.user_metadata?.disabled !== true) return true;
103
- }
104
- return false;
105
- }
106
-
107
- async countInstanceAdminsExcluding(_ctx: RequestContext, excludeUserId: string): Promise<number> {
108
- return this.countOtherEnabledAdmins(excludeUserId);
109
- }
110
-
111
- private async countOtherEnabledAdmins(excludeUserId: string): Promise<number> {
112
- const { data, error } = await this.client()
113
- .from('user_profiles')
114
- .select('user_id')
115
- .contains('platform_permissions', ['instance_admin'])
116
- .neq('user_id', excludeUserId);
117
- if (error) throw mapError(error);
118
- const candidates = (data ?? []).map((r) => r.user_id as string);
119
- if (candidates.length === 0) return 0;
120
- let count = 0;
121
- for (const id of candidates) {
122
- const { data: authData } = await this.clients.serviceClient.auth.admin.getUserById(id);
123
- if (authData.user && authData.user.user_metadata?.disabled !== true) count += 1;
124
- }
125
- return count;
126
- }
127
- }
128
-
129
- function filterValid(raw: readonly string[]): PlatformPermission[] {
130
- return raw.filter((p): p is PlatformPermission => PlatformPermissionSchema.safeParse(p).success);
131
- }
132
-
133
- function assertCanRead(ctx: RequestContext, userId: string): void {
134
- if (ctx.system) return;
135
- if (ctx.userId === userId) return;
136
- if (hasPermission(ctx, 'instance_admin')) return;
137
- throw new ProviderError('Forbidden: cannot read another user’s permissions', 403);
138
- }
139
-
140
- function assertAdmin(ctx: RequestContext): void {
141
- if (ctx.system) return;
142
- if (hasPermission(ctx, 'instance_admin')) return;
143
- throw new ProviderError('Forbidden: instance admin required', 403);
144
- }
145
-
146
- interface PostgrestError {
147
- code?: string;
148
- message?: string;
149
- }
150
-
151
- function mapError(e: unknown): Error {
152
- const pg = e as PostgrestError;
153
- if (pg?.code === '23505') return new ProviderError(pg.message ?? 'Duplicate record', 409);
154
- if (pg?.code === '23503') return new ProviderError(pg.message ?? 'Foreign key violation', 409);
155
- if (e instanceof Error) return e;
156
- if (e && typeof e === 'object') {
157
- const obj = e as { message?: string; code?: string };
158
- return new Error(obj.code ? `[${obj.code}] ${obj.message ?? ''}` : (obj.message ?? String(e)));
159
- }
160
- return new Error(String(e));
161
- }
@@ -1,28 +0,0 @@
1
- import { describe, beforeEach, it } from 'vitest';
2
- import { runPlatformPermissionStoreConformance } from '@selvajs/platform/testing';
3
- import { SupabasePlatformPermissionStore } from '../SupabasePlatformPermissionStore.js';
4
- import { readEnv, resetAllData, seedPlainUser } from '../../data/__tests__/test-helpers.js';
5
-
6
- const envCtx = readEnv();
7
-
8
- if (!envCtx) {
9
- describe.skip('SupabasePlatformPermissionStore (skipped: no live stack)', () => {
10
- it('populate packages/providers/supabase/.env.test with Supabase creds to run these tests', () => {});
11
- });
12
- } else {
13
- describe('SupabasePlatformPermissionStore', () => {
14
- beforeEach(async () => {
15
- await resetAllData(envCtx);
16
- });
17
-
18
- runPlatformPermissionStoreConformance({
19
- name: 'SupabasePlatformPermissionStore',
20
- createStore: async () => {
21
- return {
22
- store: new SupabasePlatformPermissionStore(envCtx.bundle),
23
- seedUser: async () => (await seedPlainUser(envCtx, '')).userId
24
- };
25
- }
26
- });
27
- });
28
- }
@@ -1 +0,0 @@
1
- export { SupabasePlatformPermissionStore } from './SupabasePlatformPermissionStore.js';
@@ -1,182 +0,0 @@
1
- import type { SupabaseClient } from '@supabase/supabase-js';
2
- import { createClient } from '@supabase/supabase-js';
3
- import { transcodeImageIfNeeded, classifyAssetPath } from '@selvajs/platform/storage';
4
- import type { IStorageProvider } from '@selvajs/platform/storage';
5
-
6
- /**
7
- * Storage backend for Selva on Supabase Storage.
8
- *
9
- * Uses two buckets:
10
- * - {publicBucket} (public): cover images, archive thumbnails, anything
11
- * CDN-safe. `getPublicUrl` returns the direct CDN URL.
12
- * - {privateBucket} (private): .gh / .ghx source files. `getPublicUrl`
13
- * returns the authenticated proxy URL (`${privateUrlPrefix}/{path}`) —
14
- * the consuming app must have a route at that prefix that authenticates
15
- * the request and streams the bytes via `get()`.
16
- *
17
- * Path routing: any path whose basename matches `definition.{ext}` is
18
- * considered private. Covers (`cover.webp`) and archives (`archive/{ref}`)
19
- * stay public. `definitionPaths` from @selvajs/platform produces exactly these
20
- * names, so the routing is implicit — no caller has to think about buckets.
21
- */
22
- export interface SupabaseStorageProviderConfig {
23
- /** Supabase project URL (e.g. http://127.0.0.1:54321 for local stack). */
24
- supabaseUrl: string;
25
- /**
26
- * Service-role key. Required because the provider runs server-side and
27
- * must bypass RLS for server-internal actions (admin uploads, janitor
28
- * cleanup). The provider is never used directly from the browser.
29
- */
30
- serviceRoleKey: string;
31
- /** Bucket for CDN-readable objects. Default: "selva-public". */
32
- publicBucket?: string;
33
- /** Bucket for authenticated-only objects. Default: "selva-private". */
34
- privateBucket?: string;
35
- /**
36
- * URL prefix the app exposes for authenticated file downloads. Used by
37
- * `getPublicUrl` for private-bucket paths. Default: "/api/files".
38
- * The matching SvelteKit route is expected to call `get()` and stream
39
- * bytes back after an auth check.
40
- */
41
- privateUrlPrefix?: string;
42
- }
43
-
44
- export class SupabaseStorageProvider implements IStorageProvider {
45
- private readonly client: SupabaseClient;
46
- private readonly publicBucket: string;
47
- private readonly privateBucket: string;
48
- private readonly publicBaseUrl: string;
49
- private readonly privateUrlPrefix: string;
50
-
51
- static fromEnv(env: Record<string, string | undefined>): SupabaseStorageProvider {
52
- const supabaseUrl = env.SUPABASE_URL;
53
- const serviceRoleKey = env.SUPABASE_SERVICE_ROLE_KEY;
54
- if (!supabaseUrl) throw new Error('Missing required env var: SUPABASE_URL');
55
- if (!serviceRoleKey) throw new Error('Missing required env var: SUPABASE_SERVICE_ROLE_KEY');
56
- return new SupabaseStorageProvider({
57
- supabaseUrl,
58
- serviceRoleKey,
59
- publicBucket: env.SUPABASE_PUBLIC_BUCKET,
60
- privateBucket: env.SUPABASE_PRIVATE_BUCKET,
61
- privateUrlPrefix: env.SUPABASE_PRIVATE_URL_PREFIX
62
- });
63
- }
64
-
65
- constructor(config: SupabaseStorageProviderConfig) {
66
- this.client = createClient(config.supabaseUrl, config.serviceRoleKey, {
67
- auth: { persistSession: false, autoRefreshToken: false }
68
- });
69
- this.publicBucket = config.publicBucket ?? 'selva-public';
70
- this.privateBucket = config.privateBucket ?? 'selva-private';
71
- // Direct CDN URL format: {supabaseUrl}/storage/v1/object/public/{bucket}/{path}
72
- this.publicBaseUrl = `${config.supabaseUrl.replace(/\/$/, '')}/storage/v1/object/public/${this.publicBucket}`;
73
- this.privateUrlPrefix = (config.privateUrlPrefix ?? '/api/files').replace(/\/$/, '');
74
- }
75
-
76
- /**
77
- * Route a storage path to a bucket. A path is private when either:
78
- * - it's a `.gh`/`.ghx` source file — confidentiality by extension, so a
79
- * path-scheme rename can't silently move sources into the public bucket;
80
- * - it classifies as a non-`public` asset class (`classifyAssetPath`) —
81
- * e.g. `orgs/{id}/private/*` pricing sheets are members-only and must
82
- * never land in the CDN-readable bucket.
83
- * Everything else (branding, covers, archives, thumbnails) is public.
84
- */
85
- private bucketFor(storagePath: string): string {
86
- if (/\.(gh|ghx)$/i.test(storagePath)) return this.privateBucket;
87
- const cls = classifyAssetPath(storagePath);
88
- if (cls && cls.class.visibility !== 'public') return this.privateBucket;
89
- return this.publicBucket;
90
- }
91
-
92
- async get(storagePath: string): Promise<Uint8Array | null> {
93
- const bucket = this.bucketFor(storagePath);
94
- const { data, error } = await this.client.storage.from(bucket).download(storagePath);
95
- if (error) {
96
- // storage-js surfaces 404 as an error with message "Object not found".
97
- // Normalize any "not found" to null — we don't have a stable status code.
98
- const msg = (error as { message?: string }).message ?? '';
99
- if (/not found/i.test(msg) || /no such key/i.test(msg)) return null;
100
- throw error;
101
- }
102
- const buffer = await data.arrayBuffer();
103
- return new Uint8Array(buffer);
104
- }
105
-
106
- async put(storagePath: string, data: Uint8Array, contentType?: string): Promise<void> {
107
- // Normalize images through the shared helper so this provider produces
108
- // the same bytes / extensions as LocalStorageProvider for the same input.
109
- const transcoded = await transcodeImageIfNeeded(data, contentType, storagePath);
110
- const bucket = this.bucketFor(transcoded.path);
111
- const { error } = await this.client.storage
112
- .from(bucket)
113
- .upload(transcoded.path, transcoded.data, {
114
- contentType: transcoded.contentType,
115
- upsert: true
116
- });
117
- if (error) throw error;
118
- }
119
-
120
- async delete(storagePath: string): Promise<void> {
121
- const bucket = this.bucketFor(storagePath);
122
- const { error } = await this.client.storage.from(bucket).remove([storagePath]);
123
- // remove() does not error for missing paths — it just returns an empty array.
124
- if (error) throw error;
125
- }
126
-
127
- async deletePrefix(prefix: string): Promise<void> {
128
- // Hit both buckets: a definition's GUID prefix may have files in each.
129
- // Scoped by the trailing slash in the prefix, so siblings aren't touched.
130
- await Promise.all([
131
- this.deletePrefixInBucket(this.publicBucket, prefix),
132
- this.deletePrefixInBucket(this.privateBucket, prefix)
133
- ]);
134
- }
135
-
136
- private async deletePrefixInBucket(bucket: string, prefix: string): Promise<void> {
137
- const normalized = prefix.replace(/\/+$/, '');
138
- const keys = await this.listAllKeys(bucket, normalized);
139
- if (keys.length === 0) return;
140
- // `remove` accepts up to 1000 paths; batch just in case.
141
- for (let i = 0; i < keys.length; i += 1000) {
142
- const slice = keys.slice(i, i + 1000);
143
- const { error } = await this.client.storage.from(bucket).remove(slice);
144
- if (error) throw error;
145
- }
146
- }
147
-
148
- /**
149
- * Recursively list every key under a prefix. `storage-js`'s `list` API
150
- * only returns the immediate children of a "folder", so we DFS.
151
- */
152
- private async listAllKeys(bucket: string, prefix: string): Promise<string[]> {
153
- const out: string[] = [];
154
- const stack: string[] = [prefix];
155
- while (stack.length > 0) {
156
- const dir = stack.pop()!;
157
- const { data, error } = await this.client.storage.from(bucket).list(dir, {
158
- limit: 1000,
159
- sortBy: { column: 'name', order: 'asc' }
160
- });
161
- if (error) throw error;
162
- for (const entry of data ?? []) {
163
- const full = dir ? `${dir}/${entry.name}` : entry.name;
164
- // Folder entries have `id === null` in storage-js.
165
- if (entry.id === null) {
166
- stack.push(full);
167
- } else {
168
- out.push(full);
169
- }
170
- }
171
- }
172
- return out;
173
- }
174
-
175
- getPublicUrl(storagePath: string): string {
176
- if (this.bucketFor(storagePath) === this.publicBucket) {
177
- return `${this.publicBaseUrl}/${storagePath}`;
178
- }
179
- // Private files: the app's authenticated proxy route. See `privateUrlPrefix`.
180
- return `${this.privateUrlPrefix}/${storagePath}`;
181
- }
182
- }
@@ -1,152 +0,0 @@
1
- import { describe, beforeEach, it, expect } from 'vitest';
2
- import { createClient } from '@supabase/supabase-js';
3
- import { runStorageProviderConformance } from '@selvajs/platform/testing';
4
- import { definitionPaths } from '@selvajs/platform';
5
- import { SupabaseStorageProvider } from '../SupabaseStorageProvider.js';
6
-
7
- const SUPABASE_URL = process.env.SUPABASE_URL;
8
- const SERVICE_ROLE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY;
9
- const PUBLIC_BUCKET = process.env.SUPABASE_PUBLIC_BUCKET ?? 'selva-public';
10
- const PRIVATE_BUCKET = process.env.SUPABASE_PRIVATE_BUCKET ?? 'selva-private';
11
-
12
- // The conformance suite hits a live Supabase stack. If creds aren't present
13
- // (e.g. in CI without Docker, or a clean clone pre-`supabase start`), skip
14
- // the suite with a single explanatory test so the run surfaces the reason.
15
- // `createClient` transitively loads `@supabase/realtime-js`, which on Node < 22
16
- // throws at construction without native WebSocket — treat that as "no stack"
17
- // rather than crashing the suite during collection.
18
- function tryBuildAdminClient(): ReturnType<typeof createClient> | null {
19
- if (!SUPABASE_URL || !SERVICE_ROLE_KEY) return null;
20
- try {
21
- return createClient(SUPABASE_URL, SERVICE_ROLE_KEY, {
22
- auth: { persistSession: false, autoRefreshToken: false }
23
- });
24
- } catch (err) {
25
- const msg = err instanceof Error ? err.message : String(err);
26
- if (msg.includes('WebSocket')) return null;
27
- throw err;
28
- }
29
- }
30
- const _adminClient = tryBuildAdminClient();
31
-
32
- if (_adminClient === null) {
33
- describe.skip('SupabaseStorageProvider (skipped: no live stack)', () => {
34
- it('populate packages/providers/supabase/.env.test with SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY (from `npx supabase start`) to run these tests', () => {});
35
- });
36
- } else {
37
- // Re-bind in this branch so closures below see the non-null narrowed type.
38
- const adminClient = _adminClient;
39
-
40
- /**
41
- * Delete every object under a prefix in a bucket. The conformance suite
42
- * assumes fresh state per test; Supabase Storage is shared across runs,
43
- * so we reset both buckets before each test.
44
- */
45
- async function emptyBucket(bucket: string): Promise<void> {
46
- const keys: string[] = [];
47
- const stack: string[] = [''];
48
- while (stack.length > 0) {
49
- const dir = stack.pop()!;
50
- const { data, error } = await adminClient.storage.from(bucket).list(dir, {
51
- limit: 1000,
52
- sortBy: { column: 'name', order: 'asc' }
53
- });
54
- if (error) throw error;
55
- for (const entry of data ?? []) {
56
- const full = dir ? `${dir}/${entry.name}` : entry.name;
57
- if (entry.id === null) stack.push(full);
58
- else keys.push(full);
59
- }
60
- }
61
- if (keys.length === 0) return;
62
- for (let i = 0; i < keys.length; i += 1000) {
63
- const { error } = await adminClient.storage.from(bucket).remove(keys.slice(i, i + 1000));
64
- if (error) throw error;
65
- }
66
- }
67
-
68
- describe('SupabaseStorageProvider', () => {
69
- beforeEach(async () => {
70
- await Promise.all([emptyBucket(PUBLIC_BUCKET), emptyBucket(PRIVATE_BUCKET)]);
71
- });
72
-
73
- runStorageProviderConformance({
74
- name: 'SupabaseStorageProvider',
75
- createStorage: () =>
76
- new SupabaseStorageProvider({
77
- supabaseUrl: SUPABASE_URL!,
78
- serviceRoleKey: SERVICE_ROLE_KEY!,
79
- publicBucket: PUBLIC_BUCKET,
80
- privateBucket: PRIVATE_BUCKET
81
- })
82
- });
83
-
84
- // ============================================================================
85
- // Bucket routing — Supabase-specific (the conformance suite is provider-
86
- // agnostic and can't see which bucket a file lands in). Without these tests
87
- // a regex regression in `bucketFor` could silently move .gh source files
88
- // into the public/CDN bucket.
89
- // ============================================================================
90
- describe('bucket routing', () => {
91
- const storage = new SupabaseStorageProvider({
92
- supabaseUrl: SUPABASE_URL!,
93
- serviceRoleKey: SERVICE_ROLE_KEY!,
94
- publicBucket: PUBLIC_BUCKET,
95
- privateBucket: PRIVATE_BUCKET
96
- });
97
-
98
- async function existsInBucket(bucket: string, path: string): Promise<boolean> {
99
- const { data, error } = await adminClient.storage.from(bucket).download(path);
100
- if (error) return false;
101
- return Boolean(data);
102
- }
103
-
104
- it('versioned .gh files land in the private bucket', async () => {
105
- const guid = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa';
106
- const path = definitionPaths.version(guid, 1, 'gh');
107
- await storage.put(path, new TextEncoder().encode('GH_BYTES'));
108
- expect(await existsInBucket(PRIVATE_BUCKET, path)).toBe(true);
109
- expect(await existsInBucket(PUBLIC_BUCKET, path)).toBe(false);
110
- });
111
-
112
- it('versioned .ghx files land in the private bucket', async () => {
113
- const guid = 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb';
114
- const path = definitionPaths.version(guid, 3, 'ghx');
115
- await storage.put(path, new TextEncoder().encode('GHX_BYTES'));
116
- expect(await existsInBucket(PRIVATE_BUCKET, path)).toBe(true);
117
- expect(await existsInBucket(PUBLIC_BUCKET, path)).toBe(false);
118
- });
119
-
120
- it('cover images land in the public bucket', async () => {
121
- const guid = 'cccccccc-cccc-cccc-cccc-cccccccccccc';
122
- const path = definitionPaths.image(guid);
123
- // Real 1×1 transparent PNG. The provider runs every image through
124
- // transcodeImageIfNeeded → sharp, so a hand-crafted byte stub
125
- // would fail the decode. Routing (the test's actual concern) is
126
- // independent of which format the input arrives in.
127
- const tinyPng = Uint8Array.from([
128
- 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8,
129
- 6, 0, 0, 0, 31, 21, 196, 137, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 3, 232, 0, 0, 3, 232, 1,
130
- 181, 123, 82, 107, 0, 0, 0, 13, 73, 68, 65, 84, 120, 156, 99, 96, 96, 96, 96, 0, 0, 0, 5,
131
- 0, 1, 165, 246, 69, 64, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130
132
- ]);
133
- await storage.put(path, tinyPng, 'image/png');
134
- expect(await existsInBucket(PUBLIC_BUCKET, path)).toBe(true);
135
- expect(await existsInBucket(PRIVATE_BUCKET, path)).toBe(false);
136
- });
137
-
138
- it('getPublicUrl returns the proxy prefix for private files', () => {
139
- const path = definitionPaths.version('guid-1', 1, 'gh');
140
- const url = storage.getPublicUrl(path);
141
- expect(url.startsWith('/api/files/')).toBe(true);
142
- expect(url).not.toContain('/storage/v1/object/public/');
143
- });
144
-
145
- it('getPublicUrl returns the CDN URL for public files', () => {
146
- const path = definitionPaths.image('guid-1');
147
- const url = storage.getPublicUrl(path);
148
- expect(url).toContain(`/storage/v1/object/public/${PUBLIC_BUCKET}/`);
149
- });
150
- });
151
- });
152
- }
@@ -1,2 +0,0 @@
1
- export { SupabaseStorageProvider } from './SupabaseStorageProvider.js';
2
- export type { SupabaseStorageProviderConfig } from './SupabaseStorageProvider.js';