Open History to see the last commit
1import { config, parse } from 'dotenv';
2import { execFileSync } from 'node:child_process';
3import { readFile, writeFile } from 'node:fs/promises';
4import { resolve } from 'node:path';
5
6config({ path: '.env.local', quiet: true });
7let result: any;
8try { result = JSON.parse(await readFile('.local/sandbox-project.json', 'utf8')); }
9catch {
10 result = JSON.parse(execFileSync(resolve('node_modules/.bin/neon'), ['projects', 'create', '--org-id', process.env.NEON_ORG_ID!, '--name', 'neon-glow-sandboxes', '--region-id', 'aws-eu-central-1', '--cu', '0.25-1', '--output', 'json'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }));
11 await writeFile('.local/sandbox-project.json', JSON.stringify(result), { mode: 0o600 });
12}
13const env = parse(await readFile('.env.local'));
14const response = await fetch(`https://console.neon.tech/api/v2/projects/${result.project.id}/branches`, { headers: { Authorization: `Bearer ${env.NEON_API_KEY}` } });
15if (!response.ok) throw new Error(`Sandbox branch lookup returned ${response.status}`);
16const branches = await response.json();
17const branch = branches.branches.find((branch: any) => branch.default || branch.name === 'main');
18if (!branch) throw new Error('Sandbox main branch was not found');
19const credentialsUrl = `https://console.neon.tech/api/v2/projects/${result.project.id}/branches/${branch.id}/credentials`;
20const headers = { Authorization: `Bearer ${env.NEON_API_KEY}` };
21const credentialResponse = await fetch(credentialsUrl, { headers });
22if (!credentialResponse.ok) throw new Error('Sandbox credential policy could not be verified');
23const credentials = await credentialResponse.json();
24for (const credential of credentials.credentials) {
25 if (credential.revoked_at || !credential.scopes.some((scope: string) => scope.startsWith('storage:') || scope.startsWith('ai_gateway:'))) continue;
26 const revoked = await fetch(`${credentialsUrl}/${credential.token_id}`, { method: 'DELETE', headers });
27 if (!revoked.ok) throw new Error('Sandbox storage/model credentials could not be revoked');
28}
29env.GLOW_SANDBOX_PROJECT_ID = result.project.id;
30env.GLOW_SANDBOX_BRANCH_ID = branch.id;
31env.GLOW_SANDBOX_DATABASE_HOST = new URL(result.connection_uris[0].connection_uri).hostname;
32await writeFile('.env.local', Object.entries(env).map(([key, value]) => `${key}=${JSON.stringify(value)}`).join('\n') + '\n', { mode: 0o600 });
33console.log(`Configured isolated sandbox carrier ${result.project.id}. No app data is stored in its database.`);