x402-listing / tree / src/worker.js ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
// Cloudflare Workers entry for YourAITools Listings (paid listing services for youraitools.co).
import { createApp } from './app.js';
import { DEFAULT_ENABLED } from './networks.js';
import { configFromEnv } from './config.js';
import { watchdog } from './deadline.js';
import { Stats } from './stats.js';
import { publicBaseHandler } from './publicbase.js';
import { x402TrustResponse } from './x402trust.js';
export { StatsCounter } from './counter-do.js';
export { ListingRegistry } from './registry-do.js';

let handler; let stats;
const statsFor = (env) => (stats ||= new Stats(undefined, { counter: env.COUNTER }));

export default {
  fetch: watchdog((request, env, ctx) => {
    const trust = x402TrustResponse(request, env);
    if (trust) return trust;
    if (!handler) handler = publicBaseHandler(env, (publicUrl) => createApp({ ...configFromEnv(env), enabledNetworks: env.ENABLED_NETWORKS ?? DEFAULT_ENABLED, solanaPayTo: env.SOLANA_PAY_TO || undefined, stats: statsFor(env), publicUrl }));
    if (request.method === 'HEAD') {
      // HEAD = the GET response without a body; payment and auth headers are dropped so a HEAD can never pay or act.
      const h = new Headers(request.headers);
      for (const k of ['payment-signature', 'x-payment', 'authorization']) h.delete(k);
      return Promise.resolve(handler(new Request(request.url, { method: 'GET', headers: h }), env, ctx))
        .then((res) => new Response(null, { status: res.status, headers: res.headers }));
    }
    return handler(request, env, ctx);
  }),
};

agentgate-source: code mirror on Cloudflare Pages. All repos