Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
JavaScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283// Bundle Telemetry API// Collects performance data from inline bundles running on aesthetic.computer domain// POST /api/bundle-telemetry//// Payload: { type: 'boot' | 'perf' | 'error', data: {...} }//// Used for debugging FF1, OBJKT embeds, and other bundle deployments
import { MongoClient } from "mongodb";
const MONGODB_CONNECTION_STRING = process.env.MONGODB_CONNECTION_STRING;const MONGODB_NAME = process.env.MONGODB_NAME || "aesthetic";
function respond(statusCode, body) { return { statusCode, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "Content-Type", "Access-Control-Allow-Methods": "POST, OPTIONS", }, body: JSON.stringify(body), };}
export async function handler(event) { // Handle preflight if (event.httpMethod === "OPTIONS") { return respond(200, { ok: true }); }
if (event.httpMethod !== "POST") { return respond(405, { error: "Method not allowed" }); }
if (!MONGODB_CONNECTION_STRING) { return respond(500, { error: "MongoDB not configured" }); }
let payload; try { payload = JSON.parse(event.body); } catch (e) { return respond(400, { error: "Invalid JSON" }); }
const { type, data } = payload; if (!type || !data) { return respond(400, { error: "Missing type or data" }); }
const client = new MongoClient(MONGODB_CONNECTION_STRING);
try { await client.connect(); const db = client.db(MONGODB_NAME); const collection = db.collection("bundle-telemetry");
// Build document const doc = { type, timestamp: new Date(), ...data, // Add request metadata meta: { ip: event.headers["x-forwarded-for"] || event.headers["client-ip"] || "unknown", userAgent: event.headers["user-agent"] || "unknown", referer: event.headers["referer"] || null, }, };
await collection.insertOne(doc);
return respond(200, { ok: true, id: doc._id?.toString() }); } catch (e) { console.error("Bundle telemetry error:", e); return respond(500, { error: "Database error" }); } finally { await client.close(); }}