-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
769 lines (689 loc) · 25.8 KB
/
vite.config.ts
File metadata and controls
769 lines (689 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
import path from 'path';
import crypto from 'node:crypto';
import fs from 'node:fs';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { defineConfig, loadEnv, type Plugin } from 'vite';
import react from '@vitejs/plugin-react';
import mdx from '@mdx-js/rollup';
import remarkGfm from 'remark-gfm';
import rehypeHighlight from 'rehype-highlight';
const execFileAsync = promisify(execFile);
const json = (res: ServerResponse, status: number, body: unknown) => {
res.statusCode = status;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(JSON.stringify(body));
};
const readJsonBody = async (req: IncomingMessage) => {
const chunks: Buffer[] = [];
for await (const chunk of req) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
const raw = Buffer.concat(chunks).toString('utf8').trim();
if (!raw) return null;
try {
return JSON.parse(raw);
} catch {
throw new Error('Invalid JSON body');
}
};
const parseMaybeJson = (text: string) => {
const trimmed = text.trim();
if (!trimmed) return null;
try {
return JSON.parse(trimmed);
} catch {
// Some CLIs may print extra logs even with --output json. Try extracting the JSON slice.
const firstBrace = Math.min(
...[trimmed.indexOf('{'), trimmed.indexOf('[')].filter((n) => n >= 0)
);
if (!Number.isFinite(firstBrace)) return null;
const lastBrace = Math.max(trimmed.lastIndexOf('}'), trimmed.lastIndexOf(']'));
if (lastBrace <= firstBrace) return null;
const slice = trimmed.slice(firstBrace, lastBrace + 1);
try {
return JSON.parse(slice);
} catch {
return null;
}
}
};
const parseProjectRefFromSupabaseUrl = (supabaseUrl: string): string | null => {
try {
const url = new URL(supabaseUrl);
const host = url.hostname.toLowerCase();
if (!host.endsWith('.supabase.co')) return null;
const ref = host.split('.')[0];
return ref || null;
} catch {
return null;
}
};
const pickOrgId = (orgs: any[]): string | null => {
for (const o of orgs) {
const id = o?.id ?? o?.org_id ?? o?.organization_id ?? o?.slug;
if (typeof id === 'string' && id.trim()) return id.trim();
}
return null;
};
const extractProjectRef = (maybe: any): string | null => {
const candidates = [
maybe?.id,
maybe?.ref,
maybe?.project_ref,
maybe?.projectRef,
maybe?.project?.id,
maybe?.project?.ref,
maybe?.project?.project_ref,
maybe?.project?.projectRef,
];
for (const c of candidates) {
if (typeof c === 'string' && c.trim()) return c.trim();
}
return null;
};
const extractServiceRoleKey = (maybe: any): string | null => {
const list = Array.isArray(maybe)
? maybe
: (maybe?.keys ?? maybe?.data ?? maybe?.api_keys ?? null);
if (!Array.isArray(list)) return null;
for (const k of list) {
const name = String(k?.name ?? k?.role ?? k?.type ?? '').toLowerCase();
const key = k?.key ?? k?.api_key ?? k?.apiKey ?? k?.secret ?? k?.value;
if (typeof key === 'string' && key.trim() && name.includes('service')) return key.trim();
}
return null;
};
const randomToken = () => crypto.randomBytes(24).toString('base64url');
const randomPassword = () => {
// 24 chars, mixed (safe for CLI args)
return crypto.randomBytes(18).toString('base64url') + 'A1!';
};
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
// Simple setup using just DB password (no CLI login needed)
const simpleSupabaseSetupPlugin = (): Plugin => {
return {
name: 'openbento-supabase-simple-setup',
apply: 'serve',
configureServer(server) {
const configPath = path.join(server.config.root, '.supabase-config.json');
server.middlewares.use(async (req, res, next) => {
try {
if (!req.url) return next();
// Get saved config
if (req.method === 'GET' && req.url === '/__openbento/config') {
try {
const data = fs.readFileSync(configPath, 'utf8');
json(res, 200, { ok: true, config: JSON.parse(data) });
} catch {
json(res, 200, { ok: true, config: null });
}
return;
}
// Save config
if (req.method === 'POST' && req.url === '/__openbento/config') {
const body = await readJsonBody(req);
fs.writeFileSync(configPath, JSON.stringify(body, null, 2));
json(res, 200, { ok: true });
return;
}
// Fetch analytics data (dev only)
if (req.method === 'POST' && req.url === '/__openbento/analytics/fetch') {
const body = (await readJsonBody(req)) as any;
const projectUrl = body?.projectUrl?.trim();
const dbPassword = body?.dbPassword?.trim();
const siteId = body?.siteId?.trim();
// SECURITY: Validate days as strict integer within bounds
const daysRaw = parseInt(body?.days, 10);
const days = Number.isInteger(daysRaw) && daysRaw >= 1 && daysRaw <= 365 ? daysRaw : 30;
// SECURITY: Validate siteId format (alphanumeric, hyphens, underscores only)
const siteIdValid = siteId && /^[a-zA-Z0-9_-]+$/.test(siteId);
if (!projectUrl || !dbPassword) {
json(res, 400, { ok: false, error: 'Missing projectUrl or dbPassword' });
return;
}
const projectRef = parseProjectRefFromSupabaseUrl(projectUrl);
if (!projectRef) {
json(res, 400, { ok: false, error: 'Invalid Supabase URL' });
return;
}
const dbHost = `db.${projectRef}.supabase.co`;
try {
// Fetch all analytics data - using validated parameters only
const query = siteIdValid
? `SELECT * FROM public.openbento_analytics_events WHERE site_id = '${siteId.replace(/'/g, "''")}' AND created_at > NOW() - INTERVAL '${days} days' ORDER BY created_at DESC LIMIT 10000`
: `SELECT * FROM public.openbento_analytics_events WHERE created_at > NOW() - INTERVAL '${days} days' ORDER BY created_at DESC LIMIT 10000`;
const { stdout } = await execFileAsync(
'psql',
[
'-h',
dbHost,
'-p',
'5432',
'-U',
'postgres',
'-d',
'postgres',
'-t',
'-A',
'-F',
'|',
'-c',
query,
],
{
env: { ...process.env, PGPASSWORD: dbPassword },
timeout: 30000,
}
);
// Parse the pipe-delimited output
const lines = stdout
.trim()
.split('\n')
.filter((l) => l.trim());
const events = lines.map((line) => {
const parts = line.split('|');
return {
id: parts[0],
created_at: parts[1],
site_id: parts[2],
event_type: parts[3],
block_id: parts[4] || null,
destination_url: parts[5] || null,
page_url: parts[6] || null,
referrer: parts[7] || null,
utm_source: parts[8] || null,
utm_medium: parts[9] || null,
utm_campaign: parts[10] || null,
utm_term: parts[11] || null,
utm_content: parts[12] || null,
user_agent: parts[13] || null,
language: parts[14] || null,
screen_w: parts[15] ? parseInt(parts[15]) : null,
screen_h: parts[16] ? parseInt(parts[16]) : null,
visitor_id: parts[17] || null,
session_id: parts[18] || null,
viewport_w: parts[19] ? parseInt(parts[19]) : null,
viewport_h: parts[20] ? parseInt(parts[20]) : null,
timezone: parts[21] || null,
duration_seconds: parts[22] ? parseInt(parts[22]) : null,
scroll_depth: parts[23] ? parseInt(parts[23]) : null,
engaged: parts[24] === 't',
block_title: parts[25] || null,
};
});
json(res, 200, { ok: true, events, count: events.length });
} catch (e: any) {
json(res, 500, {
ok: false,
error: 'Failed to fetch analytics: ' + (e.message || 'Unknown error'),
});
}
return;
}
// Simple setup with just DB password
if (req.method === 'POST' && req.url === '/__openbento/supabase/simple-setup') {
const body = (await readJsonBody(req)) as any;
const projectUrl = body?.projectUrl?.trim();
const dbPassword = body?.dbPassword?.trim();
const anonKey = body?.anonKey?.trim();
if (!projectUrl || !dbPassword) {
json(res, 400, { ok: false, error: 'Missing projectUrl or dbPassword' });
return;
}
// Extract project ref from URL
const projectRef = parseProjectRefFromSupabaseUrl(projectUrl);
if (!projectRef) {
json(res, 400, { ok: false, error: 'Invalid Supabase URL' });
return;
}
const dbHost = `db.${projectRef}.supabase.co`;
const logs: string[] = [];
// Run SQL migration via psql
const migrationSql = `
create extension if not exists "pgcrypto";
create table if not exists public.openbento_analytics_events (
id uuid primary key default gen_random_uuid(),
created_at timestamptz not null default now(),
site_id text not null,
event_type text not null check (event_type in ('page_view', 'click')),
block_id text,
destination_url text,
page_url text,
referrer text,
utm_source text,
utm_medium text,
utm_campaign text,
utm_term text,
utm_content text,
user_agent text,
language text,
screen_w integer,
screen_h integer
);
create index if not exists openbento_analytics_events_site_time_idx
on public.openbento_analytics_events (site_id, created_at desc);
alter table public.openbento_analytics_events enable row level security;
DO $$ BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_policies WHERE tablename = 'openbento_analytics_events' AND policyname = 'Allow public inserts'
) THEN
CREATE POLICY "Allow public inserts" ON public.openbento_analytics_events FOR INSERT WITH CHECK (true);
END IF;
END $$;
-- Note: No SELECT policy for anon users = more secure
-- Only service_role key can read analytics data
`;
try {
logs.push('Connecting to database...');
const { stdout, stderr: _stderr } = await execFileAsync(
'psql',
[
'-h',
dbHost,
'-p',
'5432',
'-U',
'postgres',
'-d',
'postgres',
'-c',
migrationSql,
],
{
env: { ...process.env, PGPASSWORD: dbPassword },
timeout: 30000,
}
);
logs.push('Migration applied successfully!');
if (stdout) logs.push(stdout);
} catch (e: any) {
json(res, 500, {
ok: false,
error: 'Failed to run migration: ' + (e.message || 'Unknown error'),
logs,
stderr: e.stderr,
});
return;
}
// Save config
const config = {
projectUrl,
projectRef,
anonKey: anonKey || null,
setupAt: new Date().toISOString(),
};
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
logs.push('Config saved to .supabase-config.json');
json(res, 200, {
ok: true,
logs,
config,
});
return;
}
return next();
} catch (e) {
json(res, 500, { ok: false, error: e instanceof Error ? e.message : 'Internal error' });
return;
}
});
},
};
};
const openbentoSupabaseDevPlugin = (): Plugin => {
return {
name: 'openbento-supabase-dev',
apply: 'serve',
configureServer(server) {
const cwd = server.config.root;
const runSupabase = async (args: string[]) => {
return execFileAsync('supabase', args, {
cwd,
env: process.env,
maxBuffer: 10 * 1024 * 1024,
});
};
const verifyState = async (params: {
projectRef: string;
adminToken?: string;
serviceRoleKey: string;
}) => {
const supabaseUrl = `https://${params.projectRef}.supabase.co`;
const checks: Record<string, { ok: boolean; details?: string }> = {};
// Table exists?
try {
const res = await fetch(
`${supabaseUrl}/rest/v1/openbento_analytics_events?select=id&limit=1`,
{
headers: {
apikey: params.serviceRoleKey,
Authorization: `Bearer ${params.serviceRoleKey}`,
},
}
);
checks.table = { ok: res.ok, details: `${res.status} ${res.statusText}` };
} catch (e) {
checks.table = { ok: false, details: e instanceof Error ? e.message : 'Request failed' };
}
// Functions deployed?
const checkFn = async (name: string) => {
try {
const res = await fetch(`${supabaseUrl}/functions/v1/${name}`, { method: 'OPTIONS' });
checks[`fn:${name}`] = { ok: res.ok, details: `${res.status} ${res.statusText}` };
} catch (e) {
checks[`fn:${name}`] = {
ok: false,
details: e instanceof Error ? e.message : 'Request failed',
};
}
};
await checkFn('openbento-analytics-track');
await checkFn('openbento-analytics-admin');
// Admin token works?
if (!params.adminToken) {
checks.adminAuth = { ok: false, details: 'missing adminToken' };
} else {
try {
const res = await fetch(
`${supabaseUrl}/functions/v1/openbento-analytics-admin?siteId=${encodeURIComponent('openbento_dev')}&days=7`,
{
headers: { 'x-openbento-admin-token': params.adminToken },
}
);
checks.adminAuth = { ok: res.ok, details: `${res.status} ${res.statusText}` };
} catch (e) {
checks.adminAuth = {
ok: false,
details: e instanceof Error ? e.message : 'Request failed',
};
}
}
return { supabaseUrl, checks };
};
server.middlewares.use(async (req, res, next) => {
try {
if (!req.url) return next();
if (req.method === 'POST' && req.url === '/__openbento/supabase/setup') {
const body = (await readJsonBody(req)) as any;
const mode: 'existing' | 'create' = body?.mode === 'create' ? 'create' : 'existing';
const logs: string[] = [];
const pushLog = (line: string) => logs.push(line);
const adminToken =
typeof body?.adminToken === 'string' && body.adminToken.trim()
? body.adminToken.trim()
: randomToken();
const region =
typeof body?.region === 'string' && body.region.trim()
? body.region.trim()
: 'eu-west-1';
let projectRef: string | null = null;
let createdDbPassword: string | null = null;
// Ensure CLI exists + login
{
const { stdout } = await runSupabase(['--version']);
pushLog(`supabase ${stdout.trim()}`);
}
let orgs: any[] = [];
try {
const { stdout } = await runSupabase(['orgs', 'list', '--output', 'json']);
const parsed = parseMaybeJson(stdout);
orgs = Array.isArray(parsed) ? parsed : [];
} catch {
json(res, 401, {
ok: false,
error:
'Supabase CLI is not logged in. Run `supabase login` in your terminal, then retry.',
});
return;
}
if (mode === 'create') {
const orgId =
typeof body?.orgId === 'string' && body.orgId.trim()
? body.orgId.trim()
: pickOrgId(orgs);
if (!orgId) {
json(res, 400, {
ok: false,
error: 'No Supabase organization found. Create one first, then retry.',
});
return;
}
const projectName =
typeof body?.projectName === 'string' && body.projectName.trim()
? body.projectName.trim()
: `openbento-analytics-${Date.now()}`;
const dbPassword =
typeof body?.dbPassword === 'string' && body.dbPassword.trim()
? body.dbPassword.trim()
: randomPassword();
if (!body?.dbPassword) createdDbPassword = dbPassword;
pushLog(`Creating Supabase project "${projectName}" (${region})…`);
const { stdout } = await runSupabase([
'projects',
'create',
projectName,
'--org-id',
orgId,
'--db-password',
dbPassword,
'--region',
region,
'--output',
'json',
]);
const parsed = parseMaybeJson(stdout);
projectRef = extractProjectRef(parsed);
if (!projectRef) {
json(res, 500, {
ok: false,
error: 'Project created, but could not read project ref from CLI output.',
logs,
raw: stdout,
});
return;
}
pushLog(`Project ref: ${projectRef}`);
// Wait for API keys to be available (project warm-up)
pushLog('Waiting for project to be ready…');
let ready = false;
for (let i = 0; i < 20; i++) {
try {
await runSupabase([
'projects',
'api-keys',
'--project-ref',
projectRef,
'--output',
'json',
]);
ready = true;
break;
} catch {
await sleep(6000);
}
}
if (!ready) {
json(res, 504, {
ok: false,
error: 'Project is taking too long to become ready. Wait a bit and try again.',
logs,
projectRef,
});
return;
}
// Fill dbPassword for later steps
body.dbPassword = dbPassword;
} else {
projectRef =
(typeof body?.projectRef === 'string' && body.projectRef.trim()
? body.projectRef.trim()
: null) ??
(typeof body?.supabaseUrl === 'string' && body.supabaseUrl.trim()
? parseProjectRefFromSupabaseUrl(body.supabaseUrl.trim())
: null);
if (!projectRef) {
json(res, 400, {
ok: false,
error:
'Missing project ref. Provide a Supabase URL (https://<ref>.supabase.co) or a project ref.',
});
return;
}
}
const dbPassword =
typeof body?.dbPassword === 'string' && body.dbPassword.trim()
? body.dbPassword.trim()
: null;
if (!dbPassword) {
json(res, 400, {
ok: false,
error: 'Database password is required to apply migrations (db push).',
});
return;
}
pushLog('Linking project…');
await runSupabase(['link', '--project-ref', projectRef, '--password', dbPassword]);
pushLog('Applying migrations (db push)…');
await runSupabase(['db', 'push', '--password', dbPassword]);
pushLog('Fetching service role key…');
const apiKeysRaw = await runSupabase([
'projects',
'api-keys',
'--project-ref',
projectRef,
'--output',
'json',
]);
const apiKeysJson = parseMaybeJson(apiKeysRaw.stdout);
const serviceRoleKey = extractServiceRoleKey(apiKeysJson);
if (!serviceRoleKey) {
json(res, 500, {
ok: false,
error: 'Could not find service_role key for this project.',
logs,
});
return;
}
pushLog('Setting Edge Function secrets…');
await runSupabase([
'secrets',
'set',
`SUPABASE_SERVICE_ROLE_KEY=${serviceRoleKey}`,
`OPENBENTO_ANALYTICS_ADMIN_TOKEN=${adminToken}`,
]);
pushLog('Deploying Edge Functions…');
await runSupabase([
'functions',
'deploy',
'openbento-analytics-track',
'--project-ref',
projectRef,
'--use-api',
'--no-verify-jwt',
]);
await runSupabase([
'functions',
'deploy',
'openbento-analytics-admin',
'--project-ref',
projectRef,
'--use-api',
'--no-verify-jwt',
]);
pushLog('Verifying…');
const verified = await verifyState({ projectRef, adminToken, serviceRoleKey });
json(res, 200, {
ok: true,
logs,
projectRef,
supabaseUrl: verified.supabaseUrl,
adminToken,
generatedDbPassword: createdDbPassword,
checks: verified.checks,
});
return;
}
if (req.method === 'GET' && req.url.startsWith('/__openbento/supabase/status')) {
const u = new URL(req.url, 'http://localhost');
const projectRef =
u.searchParams.get('projectRef')?.trim() ||
(u.searchParams.get('supabaseUrl')?.trim()
? parseProjectRefFromSupabaseUrl(u.searchParams.get('supabaseUrl')!.trim())
: null);
const adminToken = u.searchParams.get('adminToken')?.trim();
if (!projectRef) {
json(res, 400, { ok: false, error: 'Missing projectRef or supabaseUrl' });
return;
}
const apiKeysRaw = await runSupabase([
'projects',
'api-keys',
'--project-ref',
projectRef,
'--output',
'json',
]);
const apiKeysJson = parseMaybeJson(apiKeysRaw.stdout);
const serviceRoleKey = extractServiceRoleKey(apiKeysJson);
if (!serviceRoleKey) {
json(res, 500, {
ok: false,
error: 'Could not find service_role key for this project.',
});
return;
}
const verified = await verifyState({
projectRef,
adminToken: adminToken || undefined,
serviceRoleKey,
});
json(res, 200, {
ok: true,
projectRef,
supabaseUrl: verified.supabaseUrl,
checks: verified.checks,
note: adminToken ? undefined : 'adminAuth check skipped (missing adminToken)',
});
return;
}
return next();
} catch (e) {
json(res, 500, { ok: false, error: e instanceof Error ? e.message : 'Internal error' });
return;
}
});
},
};
};
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, '.', '');
return {
// Base URL pour GitHub Pages (utilise le nom du repo)
base: process.env.GITHUB_ACTIONS ? '/openbento/' : '/',
server: {
port: 3000,
host: '0.0.0.0',
},
plugins: [
// MDX plugin must come before React plugin
mdx({
remarkPlugins: [remarkGfm],
rehypePlugins: [rehypeHighlight],
}),
react(),
simpleSupabaseSetupPlugin(),
openbentoSupabaseDevPlugin(),
],
define: {
'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY),
'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY),
},
resolve: {
alias: {
'@': path.resolve(__dirname, '.'),
},
},
};
});