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.

fix(zauth): flush SDK telemetry per-request on Vercel serverless

nirholas2e0b5cacb2fd
diff --git a/api/_lib/env.js b/api/_lib/env.js
index 7c057c3f3..559977510 100644
--- a/api/_lib/env.js
+++ b/api/_lib/env.js
@@ -185,6 +185,11 @@ export const env = {
return opt('ZAUTH_API_KEY');
},
+ // Set to "1" to enable verbose [zauthSDK:*] logs in Vercel.
+ get ZAUTH_DEBUG() {
+ return opt('ZAUTH_DEBUG');
+ },
+
// Solana RPC URL used for SNS reads/writes. Falls back to public mainnet RPC.
get SOLANA_RPC_URL() {
return opt('SOLANA_RPC_URL', 'https://api.mainnet-beta.solana.com');
diff --git a/api/_lib/http.js b/api/_lib/http.js
index e10d56be7..1b3a9a4f2 100644
--- a/api/_lib/http.js
+++ b/api/_lib/http.js
@@ -2,7 +2,7 @@
import { env } from './env.js';
import { captureException } from './sentry.js';
-import { instrument as zauthInstrument } from './zauth.js';
+import { instrument as zauthInstrument, drain as zauthDrain } from './zauth.js';
export function json(res, status, body, headers = {}) {
res.statusCode = status;
@@ -116,7 +116,7 @@ function isAllowedOrigin(origin, allowed) {
// Wrap async handlers so uncaught errors return a consistent JSON envelope.
export function wrap(handler) {
return async (req, res) => {
- zauthInstrument(req, res);
+ const monitored = zauthInstrument(req, res);
try {
await handler(req, res);
} catch (err) {
@@ -134,6 +134,10 @@ export function wrap(handler) {
);
}
}
+ // Keep the lambda alive briefly so the zauth SDK's in-flight POST to
+ // back.zauthx402.com can finish. Cost: ~250ms of post-response runtime
+ // on monitored requests only. The user has already received the response.
+ if (monitored) await zauthDrain();
};
}
diff --git a/api/_lib/zauth.js b/api/_lib/zauth.js
index 7bbcde18d..792189ff0 100644
--- a/api/_lib/zauth.js
+++ b/api/_lib/zauth.js
@@ -27,8 +27,15 @@ function buildMiddleware() {
return null;
}
try {
+ // Vercel serverless freezes the function the moment res.end returns,
+ // killing the SDK's default 5-second batch timer (and any in-flight
+ // POST to back.zauthx402.com). Force flush-per-event so submission
+ // starts immediately; the `drain()` helper below keeps the lambda
+ // alive long enough for that POST to complete.
const mw = zauthProvider(apiKey, {
shouldMonitor: shouldMonitorReq,
+ debug: env.ZAUTH_DEBUG === '1',
+ batching: { maxBatchSize: 1, maxBatchWaitMs: 0, retry: false },
});
console.log('[zauth] middleware initialized, key prefix:', apiKey.slice(0, 14));
return mw;
@@ -99,19 +106,34 @@ function shimRequest(req) {
/**
* Run the zauth middleware once for this request. Safe to call on every
- * request — internal `shouldMonitor` filters non-x402 traffic.
+ * request — internal `shouldMonitor` filters non-x402 traffic. Returns
+ * `true` if this request will be reported (caller should `await drain()`
+ * after `res.end` to keep the lambda alive long enough to flush).
*
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
+ * @returns {boolean}
*/
export function instrument(req, res) {
const mw = getMiddleware();
- if (!mw) return;
+ if (!mw) return false;
try {
shimRequest(req);
shimResponse(res);
+ const monitored = shouldMonitorReq(req);
mw(req, res, () => {});
+ return monitored;
} catch (err) {
console.error('[zauth] middleware error:', err.message);
+ return false;
}
}
+
+/**
+ * Wait briefly for the SDK's fire-and-forget POST to `back.zauthx402.com`
+ * to complete before Vercel freezes the function. Only call this on
+ * requests where `instrument()` returned true.
+ */
+export function drain() {
+ return new Promise((resolve) => setTimeout(resolve, 250));
+}