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.

1.8 KB
Raw Blame
// Shared verify-then-claim path for every /api/shopify/webhooks/* receiver:
// read the raw body, check the HMAC, 401 on failure (Shopify's own
// requirement: "if a mandatory compliance webhook sends a request with an
// invalid Shopify HMAC header, then the app must return a 401 Unauthorized"),
// then claim the delivery id so a retry is a 200 no-op instead of a second
// run of the handler.

import { readBody } from '../http.js';
import { verifyWebhookHmac } from './hmac.js';
import { apiSecret } from './config.js';
import { claimWebhookDelivery, releaseWebhookDelivery } from './store.js';

export class WebhookAuthError extends Error {}

/**
 * @returns {Promise<{ shop: string, body: object, fresh: boolean, topic: string, deliveryId: string }>}
 */
export async function verifyAndClaim(req, topic) {
	const raw = await readBody(req, 1_000_000);
	const headerSig = req.headers['x-shopify-hmac-sha256'];
	if (!verifyWebhookHmac(raw, headerSig, apiSecret())) {
		throw new WebhookAuthError('invalid HMAC');
	}
	const shop = String(req.headers['x-shopify-shop-domain'] || '');
	const deliveryId = String(req.headers['x-shopify-webhook-id'] || '');
	const fresh = deliveryId ? await claimWebhookDelivery(shop, topic, deliveryId) : true;
	let body = {};
	try {
		body = JSON.parse(raw.toString('utf8'));
	} catch {
		body = {};
	}
	return { shop, body, fresh, topic, deliveryId };
}

/**
 * Runs `effect` only for a freshly claimed delivery. If it throws, the claim is
 * released before rethrowing, so the 5xx makes Shopify's retry run the effect
 * again rather than hit the ledger and skip a GDPR erasure or uninstall.
 */
export async function applyClaimed(claim, effect) {
	if (!claim.fresh) return;
	try {
		await effect();
	} catch (err) {
		if (claim.deliveryId) await releaseWebhookDelivery(claim.topic, claim.deliveryId).catch(() => {});
		throw err;
	}
}