const BADGE_LABEL = '滑动变祖器'; const BADGE_VALUES = Object.freeze(['今天是梁子', '今天是梁圣']); function secureRandom() { const sample = new Uint32Array(1); crypto.getRandomValues(sample); return sample[0] / 0x1_0000_0000; } export function pickBadgeValue(random = secureRandom) { const sample = random(); if (!Number.isFinite(sample) || sample < 0 || sample >= 1) { throw new RangeError('Random samples must be finite numbers from 0 (inclusive) to 1 (exclusive).'); } return BADGE_VALUES[Math.floor(sample * BADGE_VALUES.length)]; } export function renderBadge(value) { if (!BADGE_VALUES.includes(value)) { throw new RangeError(`Unsupported badge value: ${value}`); } const accessibleLabel = `${BADGE_LABEL}: ${value}`; return `${accessibleLabel}${BADGE_LABEL}${value}`; } const NO_CACHE_HEADERS = { 'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0', 'CDN-Cache-Control': 'no-store', 'Cloudflare-CDN-Cache-Control': 'no-store', 'Content-Type': 'image/svg+xml; charset=utf-8', Expires: '0', Pragma: 'no-cache', 'Surrogate-Control': 'no-store', 'X-Content-Type-Options': 'nosniff', }; export function createBadgeResponse(method = 'GET') { if (method !== 'GET' && method !== 'HEAD') { return new Response('Method Not Allowed', { status: 405, headers: { ...NO_CACHE_HEADERS, Allow: 'GET, HEAD', 'Content-Type': 'text/plain; charset=utf-8', }, }); } const body = method === 'HEAD' ? null : renderBadge(pickBadgeValue()); return new Response(body, { headers: NO_CACHE_HEADERS }); } export default { fetch(request) { return createBadgeResponse(request.method); }, };