Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

run-migrations: drop hardcoded creds, add sha256 + dollar-quote parsing

nirholase979ecd3de0c
diff --git a/scripts/run-migrations.mjs b/scripts/run-migrations.mjs
index b399d7de5..c970d8f0b 100644
--- a/scripts/run-migrations.mjs
+++ b/scripts/run-migrations.mjs
@@ -6,12 +6,67 @@ import { neon } from '@neondatabase/serverless';
import { readdir, readFile } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
+import { createHash } from 'crypto';
-const DB_URL = process.env.DATABASE_URL ||
- 'postgresql://neondb_owner:npg_GnQ0R4LJkZOg@ep-gentle-hill-akxaw862-pooler.c-3.us-west-2.aws.neon.tech/neondb?channel_binding=require&sslmode=require';
+const DB_URL = process.env.DATABASE_URL;
+if (!DB_URL) {
+ console.error('DATABASE_URL env var is required.');
+ process.exit(1);
+}
const sql = neon(DB_URL);
+const args = new Set(process.argv.slice(2));
+const DRY_RUN = args.has('--dry-run');
+const CHECK = args.has('--check');
+
+// Split SQL text into individual statements, respecting dollar-quoted blocks
+// and single-line (--) comments.
+function splitSql(text) {
+ const stmts = [];
+ let current = '';
+ let inDollarQuote = null;
+ let i = 0;
+ while (i < text.length) {
+ if (inDollarQuote === null) {
+ // Skip single-line comments (copy them verbatim up to the newline).
+ if (text[i] === '-' && text[i + 1] === '-') {
+ const end = text.indexOf('\n', i);
+ const line = end === -1 ? text.slice(i) : text.slice(i, end + 1);
+ current += line;
+ i += line.length;
+ continue;
+ }
+ // Detect dollar-quote opening tag.
+ const dollarMatch = text.slice(i).match(/^(\$[^$]*\$)/);
+ if (dollarMatch) {
+ inDollarQuote = dollarMatch[1];
+ current += inDollarQuote;
+ i += inDollarQuote.length;
+ continue;
+ }
+ if (text[i] === ';') {
+ const stmt = current.trim();
+ if (stmt) stmts.push(stmt + ';');
+ current = '';
+ i++;
+ continue;
+ }
+ } else {
+ if (text.slice(i).startsWith(inDollarQuote)) {
+ current += inDollarQuote;
+ i += inDollarQuote.length;
+ inDollarQuote = null;
+ continue;
+ }
+ }
+ current += text[i++];
+ }
+ const last = current.trim();
+ if (last) stmts.push(last);
+ return stmts;
+}
+
const __dir = dirname(fileURLToPath(import.meta.url));
const MIGRATIONS_DIR = join(__dir, '../api/_lib/migrations');
@@ -46,19 +101,17 @@ async function main() {
const sqlText = await readFile(path, 'utf8');
console.log(` → applying ${file} ...`);
try {
- // Neon HTTP client doesn't support multi-statement strings natively;
- // split on statement boundaries and execute each one.
- const statements = sqlText
- .split(/;(?=\s*(?:--|$|\n))/gm) // split on ; followed by newline/comment/EOF
- .map(s => s.trim())
- .filter(Boolean);
+ // Split on semicolons that are not inside dollar-quoted blocks ($$...$$).
+ const statements = splitSql(sqlText);
for (const stmt of statements) {
- if (stmt.replace(/--[^\n]*/g, '').trim()) {
- await sql.unsafe(stmt + ';');
+ const body = stmt.replace(/--[^\n]*/g, '').trim();
+ if (body) {
+ await sql(stmt);
}
}
- await sql`INSERT INTO schema_migrations (filename) VALUES (${file})`;
+ const sha256 = createHash('sha256').update(sqlText).digest('hex');
+ await sql`INSERT INTO schema_migrations (filename, sha256) VALUES (${file}, ${sha256})`;
console.log(` ✓ done`);
} catch (err) {
console.error(` ✗ FAILED: ${err.message}`);
diff --git a/tests/api/x402-spec.test.js b/tests/api/x402-spec.test.js
index 1eb3d2c79..1fd260928 100644
--- a/tests/api/x402-spec.test.js
+++ b/tests/api/x402-spec.test.js
@@ -15,17 +15,41 @@ import { afterEach, beforeAll, beforeEach, describe, it, expect, vi } from 'vite
// env.js exposes process.env via getters, so each test's env mutations are
// picked up immediately by api/_lib/x402-spec.js without needing a module
-// reset. We import the spec module once for the whole file — a fresh import
-// per test would re-walk @coinbase/x402 + @x402/extensions (cold-load > 30s
-// on a CI worker) for no behavioural benefit.
+// reset.
//
-// The cold-import alone routinely exceeds 60s on a fresh Codespace because it
-// transitively pulls @coinbase/x402, @x402/extensions, the BSC direct-payment
-// module, and (through that) the full ethers core. We warm the import in
-// `beforeAll` so individual tests start with the spec already resolved, and
-// give the warmup itself a generous 120s window. Tests still get 30s each —
-// that's plenty for the synchronous fast paths we actually exercise.
-vi.setConfig({ testTimeout: 30_000, hookTimeout: 120_000 });
+// Cold-loading the real x402-spec module transitively pulls @coinbase/x402,
+// @x402/extensions, and x402-bsc-direct (which loads the full ethers core).
+// On a fresh Codespace that chain alone can exceed 2 minutes, which is well
+// past any reasonable test timeout. None of the tests in this file actually
+// invoke those upstream deps — they exercise pure wire-format helpers
+// (permit2VariantOf, paymentRequirements, build402Body, send402). So we mock
+// the heavy upstream modules with the minimum shape the spec module needs at
+// *import time*, then run the real spec logic against it.
+vi.mock('@coinbase/x402', () => ({
+ createCdpAuthHeaders: vi.fn(async () => ({})),
+}));
+vi.mock('@x402/extensions', () => ({
+ EIP2612_GAS_SPONSORING: { key: 'eip2612GasSponsoring' },
+ ERC20_APPROVAL_GAS_SPONSORING: { key: 'erc20ApprovalGasSponsoring' },
+ declareEip2612GasSponsoringExtension: () => ({
+ eip2612GasSponsoring: { tokens: [], version: 1 },
+ }),
+ declareErc20ApprovalGasSponsoringExtension: () => ({
+ erc20ApprovalGasSponsoring: { tokens: [], version: 1 },
+ }),
+}));
+vi.mock('../../api/_lib/x402-bsc-direct.js', () => ({
+ PAYMENT_EVENT_TOPIC: '0x' + 'a'.repeat(64),
+ settleDirectPayment: vi.fn(async () => ({ success: true })),
+ verifyDirectPayment: vi.fn(async () => ({ isValid: true })),
+}));
+vi.mock('../../api/_lib/x402-builder-code.js', () => ({
+ BUILDER_CODE: 'three.ws',
+ declareBuilderCodeExtension: () => ({ builderCode: { code: 'three.ws' } }),
+ verifyClientEcho: vi.fn(() => true),
+}));
+
+vi.setConfig({ testTimeout: 10_000, hookTimeout: 30_000 });
const specPromise = import('../../api/_lib/x402-spec.js');
let spec;